Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Chapter 16 IO::Poll Content Using IO::Poll IO::Poll is a little like IO::Select turned inside out With the IO::Select API, you create multiple IO::Select sets—typically one each for reading and writing—and monitor them with a call to IO::Select->select() With IO::Poll, you create a single IO::Poll object and add filehandles to it one at a time, each with a mask that indicates the conditions you are interested in monitoring You then call the IO::Poll object's poll() method, which blocks until one or more of the conditions is met After poll() returns, you interrogate the object to learn which handles were affected A typical program begins like this: use IO::Poll qw(POLLIN POLLOUT POLLHUP); This loads the IO::Poll module and brings in the three constants POLLIN, POLLOUT, and POLLHUP These constants will be used in forming a mask to indicate what conditions of filehandles you are interested in monitoring The next step is to create an IO::Poll object, then add to it the handle(s) you wish to monitor: my $poll = IO::Poll->new; poll->mask(\*STDIN => POLLIN); $poll->mask(\*STDOUT => POLLOUT); $poll->mask($socket => POLLIN|POLLOUT); The mask() method is used both to add handles to the IO::Poll object and to remove them It takes two arguments: the handle to be watched and a bitmask designating the conditions to monitor In the example, STDIN is monitored for the POLLIN condition, STDOUT for the POLLOUT condition, and the handle named $socket is monitored for both POLLIN or POLLOUT events, formed by logically ORing the two constants As described in more detail later, POLLIN and POLLOUT conditions occur when the handle is ready for reading or writing, respectively Having set up the IO::Poll object, you usually enter an I/O loop Each time through the loop, you call the poll object's poll() method to wait for an event to occur and then call handles() to determine which handles were affected: while (1) { $poll->poll(); my @readers = $poll->handles(POLLIN|POLLHUP|POLLERR); my @writers = $poll->handles(POLLOUT); foreach (@readers) { do_reader($_); } foreach (@writers) { do_writers($_); } } The poll() method waits until one of the requested conditions becomes true and returns the number of handles that had events As with select(), you can provide an optional timeout value to return if no events occur within a designated period The handles() method returns all the handles that have conditions indicated by the passed bitmask This example calls handles() twice using different bitmasks The first checks for handles that are ready to be read from (POLLIN), those that were closed by the peer (POLLHUP), and those that have some other error (POLLERR) The second call looks for handles that are ready for writing The remainder of the example loop processes these handles in an application-specific manner Like select(), poll() must be used with sysread() and syswrite() only Mixing poll() with routines that use standard I/O buffering (the operator or plain read() and write()) does not work Top Network Programming with Perl By Lincoln D Stein • Table of Contents Copyright Preface Publisher : Addison Wesley Pub Date : December 15, 2000 ISBN : 0-201-61571-1 Pages : 784 Slots : 1 This Book's Audience Roadmap The Many Versions of Perl Getting the Code for the Code Examples Installing Modules Online Documentation Acknowledgments Part 1: Basics Chapter 1 Input/Output Basics Chapter 2 Processes, Pipes, and Signals Chapter 3 Introduction to Berkeley Sockets Perl and Networking Networking Made Easy Filehandles Using Object-Oriented Syntax with the IO::Handle and IO::File Modules Summary Processes Pipes Signals Summary Clients, Servers, and Protocols Berkeley Sockets Socket Addressing A Simple Network Client Network Names and Services Network Analysis Tools Summary Chapter 4 The TCP Protocol Chapter 5 The IO::Socket API A TCP Echo Client Socket Functions Related to Outgoing Connections A TCP Echo Server Adjusting Socket Options Other Socket-Related Functions Exceptional Conditions during TCP Communications Summary Using IO::Socket IO::Socket Methods More Practical Examples Concurrent Clients Summary Part 2: Developing Clients for Common Services Chapter 6 FTP and Telnet Chapter 7 SMTP: Sending Mail Chapter 8 POP, IMAP, and NNTP Chapter 9 Web Clients Net::FTP Net::Telnet Summary Introduction to the Mail Modules Net::SMTP MailTools MIME-Tools Summary The Post Office Protocol The IMAP Protocol Internet News Clients A News-to-Mail Gateway Summary Installing LWP LWP Basics LWP Examples Part 3: Developing TCP Client/Server Systems Chapter 10 Forking Servers and the inetd Daemon Standard Techniques for Concurrency Running Example: A Psychotherapist Server The Psychotherapist as a Forking Server A Client Script for the Psychotherapist Server Daemonization on UNIX Systems Starting Network Servers Automatically Using the inetd Super Daemon Summary Chapter 11 Multithreaded Applications Chapter 12 Multiplexed Applications Chapter 13 Nonblocking I/O Chapter 14 Bulletproofing Servers Chapter 15 Preforking and Prethreading About Threads A Multithreaded Psychiatrist Server A Multithreaded Client Summary A Multiplexed Client The IO::Select Module A Multiplexed Psychiatrist Server Summary Creating Nonblocking I/O Handles Using Nonblocking Handles Using Nonblocking Handles with Line-Oriented I/O A Generic Nonblocking I/O Module Nonblocking Connects and Accepts Summary Using the System Log Setting User Privileges Taint Mode Using chroot() Handling HUP and Other Signals Summary Preforking Prethreading Performance Measures Summary Chapter 16 IO::Poll Using IO::Poll IO::Poll Events IO::Poll Methods A Nonblocking TCP Client Using IO::Poll Summary Part 4: Advanced Topics Chapter 17 TCP Urgent Data Chapter 18 The UDP Protocol Chapter 19 UDP Servers "Out-of-Band" Data and the Urgent Pointer Using TCP Urgent Data The sockatmark() Function A Travesty Server Summary A Time of Day Client Creating and Using UDP Sockets UDP Errors Using UDP Sockets with IO::Socket Sending to Multiple Hosts UDP Servers Increasing the Robustness of UDP Applications Summary An Internet Chat System The Chat Client The Chat Server Detecting Dead Clients Summary Chapter 20 Broadcasting Unicasting versus Broadcasting Broadcasting Explained Sending and Receiving Broadcasts Broadcasting Without the Broadcast Address Enhancing the Chat Client to Support Resource Discovery Summary Chapter 21 Multicasting Sample Multicast Applications Summary Multicast Basics Using Multicast Chapter 22 UNIX-Domain Sockets Using UNIX-Domain Sockets A "Wrap" Server Using UNIX-Domain Sockets for Datagrams Summary Appendix A Additonal Source Code Net::NetmaskLite (Chapter 3) PromptUtil.pm (Chapters 8 and 9) IO::LineBufferedSet (Chapter 13) IO::LineBufferedSessionData (Chapter 13) DaemonDebug (Chapter 14) Text::Travesty (Chapter 17) mchat_client.pl (Chapter 21) Appendix B Perl Error Codes and Special Variables System Error Constants Magic Variables Affecting I/O Other Perl Globals Appendix C Internet Reference Tables Assigned Port Numbers Registered Port Numbers Internet Multicast Addresses Appendix D Bibliography Top Perl Programming TCP/IP and Berkeley Sockets Network Server Design Multicasting Application-Level Protocols Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Part 3: Developing TCP Client/Server Systems Content Chapter 16 IO::Poll We've used select() and IO::Select extensively to multiplex among multiple I/O streams However, the select() system call has some design limitations related to its use of a bit vector to represent the filehandles to be monitored On an ordinary host, such as a desktop machine, the maximum number of files is usually a small number, such as 256, and the bit vectors will therefore be no longer than 32 bytes However, on a host that is tuned for network applications, such as a Web server, this limit may be in the thousands The bit vectors necessary to describe every possible filehandle then become quite large, forcing the operating system to scan through a large, sparsely populated bit vector each time select() is called This may have an impact on performance For this reason, the POSIX standard calls for an alternative API called poll() It does much the same thing as select() but uses arrays rather than bit vectors to represent sets of filehandles Because only the filehandles of interest are placed in the arrays, the poll() call doesn't waste time scanning through a large data structure to determine which filehandles to watch You might also want to use poll() if you prefer its API, which is more elegant in some ways than select() poll() is available to Perl programmers only via its object-oriented interface, IO::Poll It was introduced during the development of Perl version 5.6 Be sure to use IO::Poll version 0.04 and higher because earlier versions weren't completely functional This version can be found in Perl versions 5.7 and higher Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Chapter 16 IO::Poll Content IO::Poll Events IO::Poll allows you to monitor handles for a richer set of conditions than those made available by IO::Select In addition to watching a handle for incoming data and the ability to accept outgoing data without blocking, IO::Poll allows you to watch handles for two levels of incoming "priority data," for end-of-file conditions, and for several different types of error Each condition is known as an "event." Each event is designated by one of the constants summarized in Table 16.1 They are divided into constants that can be added to bitmasks sent to poll() using the mask() method, and constants that are returned from poll() via the handles() method Table 16.1 IO::Poll Mask Constants Input conditions POLLIN TO poll() FROM poll() Description X X POLLRDNORM X POLLRDBAND X POLLPRI X X X X Output conditions POLLOUT normal or priority data readable normal data readable priority data readable high priority data readable X X POLLWRNORM X POLLWRBAND X X X normal or priority data writable normal data writable priority data writable afs3-kaserver 7004/udp afs3-volser afs3-volser afs3-errors 7005/tcp 7005/udp 7006/tcp afs3-errors afs3-bos afs3-bos afs3-update afs3-update afs3-rmtsys 7006/udp 7007/tcp 7007/udp 7008/tcp 7008/udp 7009/tcp afs3-rmtsys 7009/udp ups-onlinet 7010/tcp ups-onlinet 7010/udp font-service font-service fodms fodms man man isode-dua isode-dua 7100/tcp 7100/udp 7200/tcp 7200/udp 9535/tcp 9535/udp 17007/tcp 17007/udp service AFS/Kerberos authentication service volume managment server volume managment server error interpretation service error interpretation service basic overseer process basic overseer process server-to-server updater server-to-server updater remote cache manager service remote cache manager service onlinet uninterruptable power supplies onlinet uninterruptable power supplies X Font Service X Font Service FODMS FLIP FODMS FLIP Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix C Internet Reference Tables Content Internet Multicast Addresses This table lists multicast addresses that are reserved for use in registered multi-cast applications address 224.0.0.0 224.0.0.1 224.0.0.2 224.0.0.3 224.0.0.4 224.0.0.5 224.0.0.6 224.0.0.7 224.0.0.8 224.0.0.9 224.0.0.10 224.0.0.11 224.0.0.12–224.0.0.255 224.0.1.0 224.0.1.1 224.0.1.2 224.0.1.3 224.0.1.4 Description Base Address (Reserved) All Systems on this Subnet All Routers on this Subnet Unassigned DVMRP Routers OSPFIGP All Routers OSPFIGP Designated Routers ST Routers ST Hosts RIP2 Routers IGRP Routers Mobile-Agents Unassigned VMTP Managers Group NTP Network Time Protocol SGI-Dogfight Rwhod VNP 224.0.1.5 224.0.1.6 224.0.1.7 224.0.1.8 224.0.1.9 224.0.1.10 224.0.1.11 224.0.1.12 224.0.1.13 224.0.1.14 224.0.1.15 224.0.1.16 224.0.1.17 224.0.1.18 224.0.1.19 224.0.1.20 224.0.1.21 224.0.1.22 224.0.1.23 224.0.1.24 224.0.1.25 224.0.1.26 224.0.1.27–224.0.1.255 224.0.2.1 224.0.2.2 224.0.3.000– 224.0.3.255 Artificial Horizons—Aviator NSS—Name Service Server AUDIONEWS—Audio News Multicast SUN NIS+ Information Service MTP Multicast Transport Protocol IETF-1-LOW-AUDIO IETF-1-AUDIO IETF-1-VIDEO IETF-2-LOW-AUDIO IETF-2-AUDIO IETF-2-VIDEO MUSIC-SERVICE SEANET-TELEMETRY SEANET-IMAGE MLOADD any private experiment DVMRP on MOSPF SVRLOC XINGTV Microsoft-ds NBC-pro NBC-pfn Unassigned "rwho" Group (BSD) (unofficial) SUN RPC PMAPPROC_CALLIT RFE Generic Service 224.0.4.000– 224.0.4.255 RFE Individual Conferences 224.0.5.000– 224.0.5.127 CDPD Groups 224.0.5.128– 224.0.5.255 224.0.6.000– 224.0.6.127 224.0.6.128– 224.0.6.255 224.1.0.0– 224.1.255.255 224.2.0.0– 224.2.255.255 224.252.0.0– 224.255.255.255 232.0.0.0– 232.255.255.255 Unassigned Cornell ISIS Project Unassigned ST Multicast Groups Multimedia Conference Calls DIS transient groups VMTP transient groups Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Content Appendix D Bibliography Perl Programming TCP/IP and Berkeley Sockets Network Server Design Multicasting Application-Level Protocols Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix D Bibliography Content Perl Programming The books and online sources listed here are recommended as guides to Perl Books Christiansen T, Torkington N, Wall L (1998) Perl Cookbook O'Reilly & Associates (ISBN 1565922433) Conway D (1999) Object Oriented Perl Manning Publications (ISBN 1884777791) Hall J (1998) Effective Perl Programming: Writing Better Programs with Perl Addison-Wesley (ISBN 0201419750) Schwartz R, Christiansen T, Wall R (1997) Learning Perl, 2nd ed O'Reilly & Associates (ISBN 1565922840) Wall L, Christiansen T, Orwant J (2000) Programming Perl, 3rd ed O'Reilly & Associates (ISBN 0596000278) Online Resources Perl documentation, reference manuals—http://www.perl.org The Comprehensive Perl Archive Network—http://www.cpan.org ActiveState Corporation (commercial support)—http://www.activestate.com Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix D Bibliography Content TCP/IP and Berkeley Sockets Books Comer D (2000) Internetworking with TCP/IP, Vol 1: Principles, Protocols, and Architecture Prentice-Hall (ISBN 0130183806) Comer D (1998) Internetworking with TCP/IP, Vol 2: ANSI C Version: Design, Implementation, and Internals Prentice-Hall (ISBN 0139738436) Comer D (1996) Internetworking with TCP/IP, Vol 3: Client-Server Programming and Applications—BSD Socket Version Prentice-Hall (ISBN 013260969X) Hunt C (1998) TCP/IP Network Administration O'Reilly & Associates (ISBN 1565923227) Stevens WR (1994) TCP/IP Illustrated, Volume 1: The Protocols Addison-Wesley (ISBN 0201633469) Stevens WR (1996) TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX© Domain Protocols Addison-Wesley (ISBN 0201634953) Stevens WR (1997) UNIX Network Programming, Volume 1: Networking APIs: Sockets and XTI Prentice-Hall (ISBN 013490012X) Wright GR, Stevens WR (1995) TCP/IP Illustrated, Volume 2: The Implementation Addison-Wesley (ISBN 020163354X) Online Resources CIDR Address FAQs—http://www.ibm.net.il/~hank/cidr.html; http://public.pacbell.net/dedicated/cidr.html Deering S, Hinden R (1998) Internet Protocol, Version 6 (IPv6) Specification RFC 2460—http://www.faqs.org/rfcs/rfc2460.html Kessler G, Shepard S (1997) A Primer on Internet and TCP/IP Tools and Utilities RFC 2151—http://www.faqs.org/rfcs/rfc2151.html Postel J (1980) User Datagram Protocol RFC 768— http://www.faqs.org/rfcs/rfc0768.html Postel J (1981) Transmission Control Protocol RFC 793— http://www.faqs.org/rfcs/rfc0793.html PostelJ (1983) Echo Protocol RFC 862—http://www.faqs.org/rfcs/rfc0862.html Postel J (1983) Daytime Protocol RFC 867—http://www.faqs.org/rfcs/rfc0867.html Reynolds J, Postel J (1994) Assigned Numbers RFC 1700— http://www.faqs.org/rfcs/rfc1700.html Socolofsky TJ, Kale CJ (1991) TCP/IP Tutorial RFC 1180— http://www.faqs.org/rfcs/rfc1180.html Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix D Bibliography Content Network Server Design Comer D (1996) Internetworking with TCP/IP Vol 3: Client-Server Programming and Applications—BSD Socket Version Prentice-Hall (ISBN013260969X) Stevens WR (1992) Advanced Programming in the UNIX Environment AddisonWesley (ISBN 0201563177) Stevens WR (1997) UNIX Network Programming, Volume 1: Networking APIs: Sockets and XTI Prentice-Hall (ISBN 013490012X) Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix D Bibliography Content Multicasting Books Maufer T (1997) Deploying IP Multicast in the Enterprise Prentice-Hall (ISBN 0138976872) Miller CK (1998) Multicast Networking & Applications Addison-Wesley (ISBN 0201309793) Online Resources Deering SE (1989) Host Extensions for IP Multicasting RFC 1112— http://www.faqs.org/rfcs/rfc1112.html Fenner W (1997) Internet Group Management Protocol, Version 2 RFC 2236— http://www.faqs.org/rfcs/rfc2236.html Meyer D (1998) Administratively Scoped IP Multicast RFC 2365— http://www.faqs.org/rfcs/rfc2365.html Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Appendix D Bibliography Content Application-Level Protocols FTP Online Resources Allman M, Ostermann S (1999) FTP Security Considerations RFC 2577— http://www.faqs.org/rfcs/rfc2577.html Allman M, Ostermann S, Metz C (1998) FTP Extensions for IPv6 and NATs RFC 2428—http://www.faqs.org/rfcs/rfc2428.html Bellovin S (1994) Firewall-Friendly FTP RFC 1579— http://www.faqs.org/rfcs/rfc1579.html Postel J, Reynolds JK (1985) File Transfer Protocol RFC 959— http://www.faqs.org/rfcs/rfc0959.html Telnet Online Resources Postel J, Reynolds JK (1983) Telnet Protocol Specification RFC 854— http://www.faqs.org/rfcs/rfc0854.html Postel J, Reynolds JK (1983) Telnet Option Specifications RFC 855— http://www.faqs.org/rfcs/rfc0855.html Postel J, Reynolds JK (1983) Telnet Binary Transmission RFC 856— http://www.faqs.org/rfcs/rfc0856.html Postel J, Reynolds JK (1983) Telnet Echo Option RFC 857— http://www.faqs.org/rfcs/rfc0857.html Secure Shell Books Barrett DJ, Silverman R (2000) SSH, The Secure Shell: The Definitive Guide O'Reilly & Associates (ISBN 0596000111) Online Resources FreeSSH Project—http://www.freessh.org OpenSSH Project—http://www.openssh.com Secure Shell, Inc.—http://www.ssh.com SMTP Books Costales B, Allman E (1997) Sendmail O'Reilly & Associates (ISBN 1565922220) Online Resources Klensin J, Freed N, Rose M, Stefferud E, Crocker D (1995) SMTP Service Extensions RFC 1869—http://www.faqs.org/rfcs/rfc1869.html Postel J (1982) Simple Mail Transfer Protocol RFC 821— http://www.faqs.org/rfcs/rfc0821.html procmail—ftp://ftp.informatik.rwthaachen.de/pub/packages/procmail/procmail.tar.gz MIME Online Resources Freed N, Borenstein N (1996) MIME (Multipurpose Internet Mail Extensions (MIME), Part 1: Format of Internet Message Bodies RFC 2045— http://www.faqs.org/rfcs/rfc2045.html Freed N, Borenstein N (1996) MIME (Multipurpose Internet Mail Extensions), Part 2: Media Types RFC 2046—http://www.faqs.org/rfcs/rfc2046.html Freed N, Borenstein N (1996) MIME (Multipurpose Internet Mail Extensions), Part 3: Message Header Extensions for Non-ASCII Text RFC 2047— http://www.faqs.org/rfcs/rfc2047.html Freed N, Borenstein N (1996) Multipurpose Internet Mail Extensions (MIME), Part 4: Registration Procedures RFC 2048—http://www.faqs.org/rfcs/rfc2048.html Freed N, Borenstein N (1996) Multipurpose Internet Mail Extensions (MIME), Part 5: Conformance Criteria and Examples RFC 2049— http://www.faqs.org/rfcs/rfc2049.html POP Online Resources Meyers J (1994) POP3 AUTHentication Command RFC 1734— http://www.faqs.org/rfcs/rfc1734.html Myers J, Rose M (1996) Post Office Protocol, Version 3 RFC 1939— http://www.faqs.org/rfcs/rfc1939.html Nelson R (1996) Some Observations on Implementations of the Post Office Protocol (POP3) RFC 1957—http://www.faqs.org/rfcs/rfc1957.html IMAP Online Resources Crispin M (1996) Internet Message Access Protocol, Version 4rev1 RFC 2060— http://www.faqs.org/rfcs/rfc2060.html Klensin J, Catoe R, Krumviede P (1997) IMAP/POP AUTHorize Extension for Simple Challenge/Response RFC 2195—http://www.faqs.org/rfcs/rfc2195.html NNTP Book Spencer H, Lawrence D (1998) Managing Usenet: An Administrator's Guide to Netnews O'Reilly & Associates (ISBN 1565921984) Online Resource Kantor B, Lapsley P (1986) Network News Transfer Protocol RFC 977— http://www.faqs.org/rfcs/rfc0977.html HTTP, HTML, and XML Books Birbeck M (2000) Professional XML Wrox Press (ISBN 1861003110) Marchal B (1999) XML by Example Que Education & Training (ISBN 0789722429) Musciana C, Kennedy B, Loukides M (1998) HTML: The Definitive Guide, 3rd ed O'Reilly & Associates (ISBN 1565924924) Stein L (1997) How to Set Up and Maintain a Web Site Addison-Wesley (ISBN 0201634627) Wong C (1999) Web Client Programming with Perl O'Reilly & Associates (ISBN 156592214X) Online Resources Bray T, Paoli J, Sperberg-McQueen CM (1998) Extensible Markup Language (XML) 1.0 http://www.w3.org/TR/REC-xml Fielding R, Gettys J, Mogul J, Frystyk H, Masinter L, Leach P, Berners-Lee T (1999) Hypertext Transfer Protocol—HTTP/1.1 RFC 2616— http://www.faqs.org/rfcs/rfc2616.html Franks J, Hallam-Baker P, Hostetler J, Lawrence S, Leach P, Luotonen A, Stewart L (1999) HTTP Authentication: Basic and Digest Access Authentication RFC 2617— http://www.faqs.org/rfcs/rfc2617.html Network Security Books Anonymous (1998) Maximum Security: A Hacker's Guide to Protecting Your Internet Site and Network Sams (ISBN 0672313413) Garfinkel S, Spafford G (1996) Practical UNIX and Internet Security O'Reilly & Associates (ISBN 1565921488) Russell D, Gangemi GT (1991) Computer Security Basics O'Reilly & Associates (ISBN 0937175714) Top ... in C or Java, you'll be delighted to discover how much of the Perl API carries over into these languages Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Preface Content This Book's Audience Network Programming with Perl is written for novice and intermediate Perl. .. handle is open or has an error Top Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Content Copyright Network Programming with Perl Many of the designations used by manufacturers and sellers to distinguish their... installling them Network Programming with PerlBy Lincoln D Stein Slots : 1 Table of Contents Preface Content Online Documentation In addition to books and Web sites, Network Programming with Perl refers to two