`UNIX
`
`W. RICHARD STEVENS
`NETWORK
`PROGRAMMING
`Networking APls: Sockets and XTI
`
`111e only guide
`to Unix network
`programming
`AP/s you'll ever
`need!
`
`Whether you write Web servers, client/server applications, or any other network
`software, you need to understand networking AP!s-especially sockets-In greater
`detail than ever before. You need UNIX Network Programming, Volume 1,
`Second Edhlon.
`In this book, leading Unix networking expert, W. Richard Stevens, offers unprecedented,
`start-to-finish guidance on making the most of sockets, the de facto standard for
`Unix network programmin~s well as extensive coverage of the X/Open Transport
`Interface (XTI).
`
`Stevens begins by introducing virtually every basic ca pability or TCP and UDP sockets,
`
`including socket functions and options. 1/0 multiplexing. and name and address conver(cid:173)
`
`sions. He presents detailed coverage or the Posix. l g standard for sockets ancl Posix. 1
`
`threads. He also introduces advanced techniques for:
`
`• I Pv4/I Pv6 interoperability
`• Nonblocking 1/0
`• Routing sockets
`• Broadcasting and multicastmg
`•IP options
`
`• Mult1thread1ng
`• Debugging techniques
`• Unix domain protocols
`• Raw sockets
`• ChenVserver design alternatives
`
`Learn how to choose among today's leading client/server design approaches, including
`TCP Iterative, concurrent, preforked and prethreaded servers. Master the X/Open
`Transport Interface, Including XTI TCP clients and servers, name and address functions,
`options, streams. and additional functions.
`The InterneVintranet revolution has dramatically increased the demand for developers
`with a sophisticated understanding of network programmlpg AP!s, especially
`sockets. One book contains all you need to know: UNIX Network PrOgramming,
`Volume 1, Second Edhlon.
`
`ABOUT THE AUTHOR
`W. RICHARD STEVENS Is author of UNIX Network Programming, First EdiUon,
`widely recognized as the classic text In Unix networking. He Is also the author of
`Advanced Programming in the UNIX Environment and the TCP/IP Illustrated Serles.
`He Is an acknowledged Unix and networking expert, sought-after instructor, and
`occasional consultant.
`
`PRENTICE HALL
`Upper Saddle River, NJ 07458
`http://www.phptr.com
`
`Yolume1
`
`SECOND EDITION
`
`NETWORK
`PROGRAMMING
`
`001
`
`
`
`UNIX Network Programming
`Volume 1
`Second Edition
`
`Networking APls:
`Sockets and XTI
`
`by W. Richard Stevens
`
`To join a Prentice Hall PTR Internet mailing list, point to
`http: I I www.prenhall.com/ mail_lists/
`
`Prentice Hall PTR
`Upper Saddle River, NJ 07458
`
`•
`
`ISBN 0-13-490012-X
`
`JI I Ill Ill' 90000
`978 0 134 90012~1 111111
`
`002
`
`
`
`To Sally, Bill, Ellen, and David.
`Aloha nui loa.
`
`j
`I
`
`Li b rary o f Congress Cataloging-in- Publication Data
`Stevens, W. Richard
`UNIX network programmi ng / by W. Richard Stevens . -- 2nd ed .
`cm.
`p.
`Includes i ndex.
`ISBN 0-1 3- 490012-X
`1 . UNIX (Computer file ) 2 . Computer networks . 3 . Internet
`programming.
`I . Title .
`QA7~ . 76. 0 63S7 55 1997
`005 . 7'12776 8--dc21
`
`97-31761
`CIP
`
`Editorial/Production Supervision: Eileen Clark
`Acquisitions Editor: Mary Franz
`Marketing Manager: Miles Williams
`Buyer: Alexis R. Heydt
`Cover Design: Scott Weiss
`Cover Design Direction: Jerry Votta
`Editorial Assistant: Noreen Regina
`
`I O 1998 P,mtitt lTull PTR
`=-
`
`-
`
`Prentice-Hall, Inc.
`A Simon & Schuster Company
`Upper Saddle River, NJ 07458
`
`Prentice Hall books are widely used by corporations and government agencies for training, marketing, and
`resale. The publisher offers discounts on this book when ordered in bulk quantities.
`For more information, contact
`Corporate Sales Department,
`Phone:800-382-3419;FAX:201-236-7141
`E-mail (Internet): corpsales@prenhall.com
`Or write: Prentice Hall PTR
`Corp. Sales Department
`One Lake Street
`Upper Saddle River, NJ 07458
`
`All rights reserved. No part of this book may be
`reproduced, in any form or by any means, without
`permission in writing from the publisher.
`
`Printed in the United States of America
`
`1098765
`
`ISBN 0-13-490012-X
`
`Prentice-Hall International (UK) Limited, London
`Prentice-Hall of Australia Pty. Limited, Sydney
`Prentice-Hall Canada Inc., Toronto
`Prentice-Hall Hispanoamericana, S.A.,Mexico
`Prentice-Hall of India Private Limited, New Delhi
`Prentice-Hall of Japan, Inc., Tokyo
`Simon & Schuster Asia Pte. Ltd., Singapore
`Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
`
`003
`
`
`
`Contents
`
`Preface
`
`Part 1.
`
`Introduction and TCP/IP
`
`Chapter 1.
`1.1
`1.2
`1.3
`1.4
`1.5
`1.6
`1.7
`1.8
`1.9
`1.10
`1.11
`1.12
`
`Chapter 2.
`2.1
`2.2
`2.3
`2.4
`2.5
`
`Introduction
`3
`Introduction
`A Simple Daytime Client
`9
`Protocol Independence
`Error Handling: Wrapper Functions
`A Simple Daytime Server
`13
`Road Map to Client-Server Examples in the Text
`OSI Model
`18
`BSD Networking History
`Test Networks and Hosts
`24
`Unix Standards
`64-bit Architectures
`Summary
`28
`
`11
`
`16
`
`6
`
`19
`20
`
`27
`
`The Transport Layer: TCP and UDP
`Introduction
`29
`30
`The Big Picture
`32
`UDP: User Datagram Protocol
`32
`TCP: Transmission Control Protocol
`TCP Connection Establishment and Termination
`
`34
`
`xv
`
`1
`
`3
`
`29
`
`v
`
`004
`
`
`
`l
`
`vi
`
`UNIX Network Programming
`
`Contents
`
`UNIX Network Programming
`
`Contents
`
`vii
`
`2.6
`2.7
`2.8
`2.9
`2.10
`2.11
`2.12
`
`40
`
`TIME_WAIT State
`41
`Port Numbers
`TCP Port Numbers and Concurrent Servers
`Buffer Sizes and Limitations
`46
`Standard Internet Services
`50
`Protocol Usage by Common Internet Applications
`Summary
`52
`
`44
`
`52
`
`5.11
`5.12
`5.13
`5.14
`5.15
`5.16
`5.17
`5.18
`5.19
`
`Connection Abort before accept Returns
`Termination of Server Process
`130
`SIGPIPE Signal
`132
`133
`Crashing of Server Host
`Crashing and Rebooting of Server Host
`Shutdown of Server Host
`135
`Summary of TCP Example
`135
`Data Format
`137
`Summary
`140
`
`129
`
`134
`
`Part 2. Elementary Sockets
`
`Chapter 3.
`3.1
`3.2
`3.3
`3.4
`3.5
`3.6
`3.7
`3.8
`3.9
`3.10
`3.11
`
`Chapter 4.
`4.1
`4.2
`4.3
`4.4
`4.5
`4.6
`4.7
`4.8
`4.9
`4.10
`4.11
`
`Chapter 5.
`5.1
`5.2
`5.3
`5.4
`5.5
`5.6
`5.7
`5.8
`5.9
`5.10
`
`57
`63
`66
`
`Sockets Introduction
`Introduction
`57
`Socket Address Structures
`Value-Result Arguments
`Byte Ordering Functions
`69
`Byte Manipulation Functions
`inet_aton, inet_addr, and inet_ntoa
`inet_pton and inet_ntop Functions
`75
`sock_ntop and Related Functions
`readn, wri ten, and readline Functions
`isfdtype Function
`81
`Summary
`82
`
`Functions
`72
`
`70
`
`77
`
`85
`89
`
`91
`
`Elementary TCP Sockets
`Introduction
`85
`socket Function
`connect Function
`bind Function
`93
`listen Function
`99
`accept Function
`fork and exec Functions
`Concurrent Servers
`104
`close Function
`107
`getsockname and getpeername Functions
`Summary
`110
`
`102
`
`TCP Cllent-Server Example
`Introduction
`111
`112
`TCP Echo Server: main Function
`TCP Echo Server: str_echo Function
`TCP Echo Client: main Function
`113
`TCP Echo Client: str_cli Function
`115
`Normal Startup
`115
`117
`Normal Termination
`Posix Signal Handling
`119
`Handling SIGCHLD Signals
`wait and wai tpid Functions
`
`122
`124
`
`113
`
`107
`
`55
`
`57
`
`85
`
`111
`
`Chapter 6.
`6.1
`6.2
`6.3
`6.4
`6.5
`6.6
`6.7
`6.8
`6.9
`6.10
`6.11
`6.12
`
`Chapter 7.
`7.1
`7.2
`7.3
`7.4
`7.5
`7.6
`7.7
`7.8
`7.9
`7. 10
`7. 11
`
`Chapter 8.
`8.1
`8.2
`8.3
`8.4
`8.5
`8.6
`8.7
`8.8
`8.9
`8.10
`
`1/0 Multiplexing: The select and poll Functions
`Introduction
`143
`1/0 Models
`144
`150
`select Function
`str_cli Function (Revisited)
`Batch Input
`157
`160
`shutdown Function
`str_cli Function (Revisited Again)
`TCP Echo Server (Revisited)
`162
`pselect Function
`168
`poll Function
`169
`TCP Echo Server (Revisited Again)
`Summary
`175
`
`155
`
`16
`
`172
`
`Socket Options
`Introduction
`177
`178
`getsockopt and setsockopt Functions
`Checking If an Option Is Supported and Obtaining the Default
`Socket States
`183
`Generic Socket Options
`I Pv4 Socket Options
`ICMPv6 Socket Option
`1Pv6 Socket Options
`TCP Socket Options
`fcntl Function
`205
`Summary
`207
`
`183
`197
`199
`199
`201
`
`Elementary UDP Sockets
`Introduction
`211
`recvfrom and sendto Functions
`UDP Echo Server: main Function
`UDP Echo Server: dg_echo Function
`UDP Echo Client: main Function
`216
`UDP Echo Client: dg_cli Function
`217
`Lost Datagrams
`217
`Verifying Received Response
`Server Not Running
`220
`Summary of UDP example
`
`218
`
`221
`
`212
`213
`214
`
`143
`
`177
`
`178
`
`211
`
`005
`
`
`
`l
`
`vi
`
`UNIX Network Programming
`
`Contents
`
`UNIX Network Programming
`
`Contents
`
`vii
`
`2.6
`2.7
`2.8
`2.9
`2.10
`2.11
`2.12
`
`40
`
`TIME_WAIT State
`41
`Port Numbers
`TCP Port Numbers and Concurrent Servers
`Buffer Sizes and Limitations
`46
`Standard Internet Services
`50
`Protocol Usage by Common Internet Applications
`Summary
`52
`
`44
`
`52
`
`5.11
`5.12
`5.13
`5.14
`5.15
`5.16
`5.17
`5.18
`5.19
`
`Connection Abort before accept Returns
`Termination of Server Process
`130
`SIGPIPE Signal
`132
`133
`Crashing of Server Host
`Crashing and Rebooting of Server Host
`Shutdown of Server Host
`135
`Summary of TCP Example
`135
`Data Format
`137
`Summary
`140
`
`129
`
`134
`
`Part 2. Elementary Sockets
`
`Chapter 3.
`3.1
`3.2
`3.3
`3.4
`3.5
`3.6
`3.7
`3.8
`3.9
`3.10
`3.11
`
`Chapter 4.
`4.1
`4.2
`4.3
`4.4
`4.5
`4.6
`4.7
`4.8
`4.9
`4.10
`4.11
`
`Chapter 5.
`5.1
`5.2
`5.3
`5.4
`5.5
`5.6
`5.7
`5.8
`5.9
`5.10
`
`57
`63
`66
`
`Sockets Introduction
`Introduction
`57
`Socket Address Structures
`Value-Result Arguments
`Byte Ordering Functions
`69
`Byte Manipulation Functions
`inet_aton, inet_addr, and inet_ntoa
`inet_pton and inet_ntop Functions
`75
`sock_ntop and Related Functions
`readn, wri ten, and readline Functions
`isfdtype Function
`81
`Summary
`82
`
`Functions
`72
`
`70
`
`77
`
`85
`89
`
`91
`
`Elementary TCP Sockets
`Introduction
`85
`socket Function
`connect Function
`bind Function
`93
`listen Function
`99
`accept Function
`fork and exec Functions
`Concurrent Servers
`104
`close Function
`107
`getsockname and getpeername Functions
`Summary
`110
`
`102
`
`TCP Cllent-Server Example
`Introduction
`111
`112
`TCP Echo Server: main Function
`TCP Echo Server: str_echo Function
`TCP Echo Client: main Function
`113
`TCP Echo Client: str_cli Function
`115
`Normal Startup
`115
`117
`Normal Termination
`Posix Signal Handling
`119
`Handling SIGCHLD Signals
`wait and wai tpid Functions
`
`122
`124
`
`113
`
`107
`
`55
`
`57
`
`85
`
`111
`
`Chapter 6.
`6.1
`6.2
`6.3
`6.4
`6.5
`6.6
`6.7
`6.8
`6.9
`6.10
`6.11
`6.12
`
`Chapter 7.
`7.1
`7.2
`7.3
`7.4
`7.5
`7.6
`7.7
`7.8
`7.9
`7. 10
`7. 11
`
`Chapter 8.
`8.1
`8.2
`8.3
`8.4
`8.5
`8.6
`8.7
`8.8
`8.9
`8.10
`
`1/0 Multiplexing: The select and poll Functions
`Introduction
`143
`1/0 Models
`144
`150
`select Function
`str_cli Function (Revisited)
`Batch Input
`157
`160
`shutdown Function
`str_cli Function (Revisited Again)
`TCP Echo Server (Revisited)
`162
`pselect Function
`168
`poll Function
`169
`TCP Echo Server (Revisited Again)
`Summary
`175
`
`155
`
`16
`
`172
`
`Socket Options
`Introduction
`177
`178
`getsockopt and setsockopt Functions
`Checking If an Option Is Supported and Obtaining the Default
`Socket States
`183
`Generic Socket Options
`I Pv4 Socket Options
`ICMPv6 Socket Option
`1Pv6 Socket Options
`TCP Socket Options
`fcntl Function
`205
`Summary
`207
`
`183
`197
`199
`199
`201
`
`Elementary UDP Sockets
`Introduction
`211
`recvfrom and sendto Functions
`UDP Echo Server: main Function
`UDP Echo Server: dg_echo Function
`UDP Echo Client: main Function
`216
`UDP Echo Client: dg_cli Function
`217
`Lost Datagrams
`217
`Verifying Received Response
`Server Not Running
`220
`Summary of UDP example
`
`218
`
`221
`
`212
`213
`214
`
`143
`
`177
`
`178
`
`211
`
`006
`
`
`
`viii
`
`UNIX Network Programming
`
`Contents
`
`8.11
`8.12
`8.13
`8.14
`8.15
`8.16
`
`224
`c onnect Function with UDP
`227
`dg_ cli Function (Revisited)
`Lack of Flow Control with UDP
`228
`Determining Outgoing Interface with UDP
`TCP and UDP Echo Server Using select
`Summary
`235
`
`231
`233
`
`237
`
`259
`
`261
`
`273
`
`Chapter 9.
`9.1
`9.2
`9.3
`9.4
`9.5
`9.6
`9.7
`9.8
`9.9
`9.10
`9.11
`
`Elementary Name and Address Conversions
`Introduction
`237
`237
`Domain Name System
`240
`ge t hostbyname Function
`245
`RES_USE_INET6 Resolver Option
`ge t hostbyname2 Function and 1Pv6 Support
`248
`gethostbyaddr Function
`249
`uname Function
`250
`gethostname Function
`getservbyname and getservbyport Functions
`Other Networking Information
`255
`Summary
`256
`
`246
`
`251
`
`Part 3. Advanced Sockets
`
`Chapter 10.
`10.1
`10.2
`10.3
`10.4
`10.5
`10.6
`10.7
`
`Chapter 11.
`11.1
`11.2
`11.3
`11.4
`11.5
`11.6
`11.7
`11.8
`11.9
`11 .10
`11.11
`11.12
`11.13
`11.14
`11.15
`
`1Pv4 and 1Pv6 Interoperability
`Introduction
`261
`262
`1Pv4 Client, 1Pv6 Server
`265
`1Pv6 Client, 1Pv4 Server
`1Pv6 Address Testing Macros
`IPV6_ADDRFORM Socket Option
`Source Code Portability
`270
`Summary
`271
`
`267
`268
`
`Advanced Name and Address Conversions
`Introduction
`273
`273
`getaddrinfo Function
`278
`gai_s trerror Function
`279
`freeaddrinfo Function
`getaddrinfo Function: 1Pv6 and Unix Domain
`282
`getaddrinfo Function: Examples
`284
`h o st_serv Function
`tcp_connect Function
`285
`tcp_listen Function
`288
`udp_client Function
`293
`udp_connect Function
`295
`udp_server Function
`296
`getnameinfo Function
`298
`300
`Reentrant Functions
`gethostbyname_r and gethostbyaddr_ r Functions
`
`279
`
`303
`
`UNIX Network Programming
`
`Contents
`
`ix
`
`11.16
`11.17
`
`Implementation of getaddrinfo and getnameinfo Functions
`Summary
`328
`
`305
`
`Chapter 12.
`12.1
`12.2
`12.3
`12.4
`12.5
`12.6
`12.7
`
`Chapter 13.
`13.1
`13.2
`13.3
`13.4
`13.5
`13.6
`13.7
`13.8
`13.9
`13.10
`
`Chapter 14.
`14.1
`14.2
`14.3
`14.4
`14.5
`14.6
`14.7
`14.8
`14.9
`
`Chapter 15.
`15.1
`15.2
`15.3
`15.4
`15.5
`15.6
`15.7
`
`Daemon Processes and inetd Superserver
`Introduction
`331
`332
`syslogd Daemon
`333
`syslog Function
`daemon_ ini t Function
`339
`inetd Daemon
`daemon_ inetd Function
`Summary
`346
`
`335
`
`344
`
`Advanced 110 Functions
`Introduction
`349
`349
`Socket Timeouts
`recv and send Functions
`readv and wri tev Functions
`recvmsg and sendmsg Functions
`Ancillary Data
`362
`How Much Data Is Queued?
`Sockets and Standard 1/0
`T/TCP: TCP for Transactions
`Summary
`371
`
`365
`366
`369
`
`354
`357
`358
`
`Unix Domain Protocols
`Introduction
`373
`Unix Domain Socket Address Structure
`376
`socketpair Function
`Socket Functions
`377
`Unix Domain Stream Client-Server
`Unix Domain Datagram Client-Server
`Passing Descriptors
`381
`Receiving Sender Credentials
`Summary
`394
`
`390
`
`374
`
`378
`379
`
`Nonblocking 110
`Introduction
`397
`Nonblocking
`Reads and Writes: str_cli Function (Revisited)
`Nonblocking
`409
`connect
`Nonblocking
`connect: Daytime Client
`Nonblocking
`connect: Web Client
`Nonblocking
`422
`accept
`Summary
`424
`
`410
`413
`
`•
`
`Chapter 16.
`16.1
`16.2
`16.3
`
`ioctl Operations
`Introduction
`425
`i octl Function
`Socket Operations
`
`426
`426
`
`331
`
`349
`
`373
`
`397
`
`399
`
`425
`
`007
`
`
`
`viii
`
`UNIX Network Programming
`
`Contents
`
`8.11
`8.12
`8.13
`8.14
`8.15
`8.16
`
`224
`c onnect Function with UDP
`227
`dg_ cli Function (Revisited)
`Lack of Flow Control with UDP
`228
`Determining Outgoing Interface with UDP
`TCP and UDP Echo Server Using select
`Summary
`235
`
`231
`233
`
`237
`
`259
`
`261
`
`273
`
`Chapter 9.
`9.1
`9.2
`9.3
`9.4
`9.5
`9.6
`9.7
`9.8
`9.9
`9.10
`9.11
`
`Elementary Name and Address Conversions
`Introduction
`237
`237
`Domain Name System
`240
`ge t hostbyname Function
`245
`RES_USE_INET6 Resolver Option
`ge t hostbyname2 Function and 1Pv6 Support
`248
`gethostbyaddr Function
`249
`uname Function
`250
`gethostname Function
`getservbyname and getservbyport Functions
`Other Networking Information
`255
`Summary
`256
`
`246
`
`251
`
`Part 3. Advanced Sockets
`
`Chapter 10.
`10.1
`10.2
`10.3
`10.4
`10.5
`10.6
`10.7
`
`Chapter 11.
`11.1
`11.2
`11.3
`11.4
`11.5
`11.6
`11.7
`11.8
`11.9
`11 .10
`11.11
`11.12
`11.13
`11.14
`11.15
`
`1Pv4 and 1Pv6 Interoperability
`Introduction
`261
`262
`1Pv4 Client, 1Pv6 Server
`265
`1Pv6 Client, 1Pv4 Server
`1Pv6 Address Testing Macros
`IPV6_ADDRFORM Socket Option
`Source Code Portability
`270
`Summary
`271
`
`267
`268
`
`Advanced Name and Address Conversions
`Introduction
`273
`273
`getaddrinfo Function
`278
`gai_s trerror Function
`279
`freeaddrinfo Function
`getaddrinfo Function: 1Pv6 and Unix Domain
`282
`getaddrinfo Function: Examples
`284
`h o st_serv Function
`tcp_connect Function
`285
`tcp_listen Function
`288
`udp_client Function
`293
`udp_connect Function
`295
`udp_server Function
`296
`getnameinfo Function
`298
`300
`Reentrant Functions
`gethostbyname_r and gethostbyaddr_ r Functions
`
`279
`
`303
`
`UNIX Network Programming
`
`Contents
`
`ix
`
`11.16
`11.17
`
`Implementation of getaddrinfo and getnameinfo Functions
`Summary
`328
`
`305
`
`Chapter 12.
`12.1
`12.2
`12.3
`12.4
`12.5
`12.6
`12.7
`
`Chapter 13.
`13.1
`13.2
`13.3
`13.4
`13.5
`13.6
`13.7
`13.8
`13.9
`13.10
`
`Chapter 14.
`14.1
`14.2
`14.3
`14.4
`14.5
`14.6
`14.7
`14.8
`14.9
`
`Chapter 15.
`15.1
`15.2
`15.3
`15.4
`15.5
`15.6
`15.7
`
`Daemon Processes and inetd Superserver
`Introduction
`331
`332
`syslogd Daemon
`333
`syslog Function
`daemon_ ini t Function
`339
`inetd Daemon
`daemon_ inetd Function
`Summary
`346
`
`335
`
`344
`
`Advanced 110 Functions
`Introduction
`349
`349
`Socket Timeouts
`recv and send Functions
`readv and wri tev Functions
`recvmsg and sendmsg Functions
`Ancillary Data
`362
`How Much Data Is Queued?
`Sockets and Standard 1/0
`T/TCP: TCP for Transactions
`Summary
`371
`
`365
`366
`369
`
`354
`357
`358
`
`Unix Domain Protocols
`Introduction
`373
`Unix Domain Socket Address Structure
`376
`socketpair Function
`Socket Functions
`377
`Unix Domain Stream Client-Server
`Unix Domain Datagram Client-Server
`Passing Descriptors
`381
`Receiving Sender Credentials
`Summary
`394
`
`390
`
`374
`
`378
`379
`
`Nonblocking 110
`Introduction
`397
`Nonblocking
`Reads and Writes: str_cli Function (Revisited)
`Nonblocking
`409
`connect
`Nonblocking
`connect: Daytime Client
`Nonblocking
`connect: Web Client
`Nonblocking
`422
`accept
`Summary
`424
`
`410
`413
`
`•
`
`Chapter 16.
`16.1
`16.2
`16.3
`
`ioctl Operations
`Introduction
`425
`i octl Function
`Socket Operations
`
`426
`426
`
`331
`
`349
`
`373
`
`397
`
`399
`
`425
`
`008
`
`
`
`x
`
`UNIX Network Programming
`
`Contents
`
`UNIX Network Programming
`
`Contents
`
`xi
`
`16.4
`File Operations
`427
`16.5
`Interface Configuration
`16.6
`get_ifi_info Function
`16.7
`Interface Operations
`439
`16.8
`ARP Cache Operations
`440
`16.9
`Routing Table Operations
`442
`16.10 Summary
`443
`
`428
`429
`
`Chapter 17. Routing Sockets
`17.1
`Introduction
`445
`17.2
`Datalink Socket Address Structure
`17.3
`Reading and Writing
`447
`17.4
`sysc t l Operations
`454
`17.5
`get_ifi_info Function
`459
`17.6
`Interface Name and Index Functions
`17.7
`Summary
`467
`
`Chapter 18. Broadcasting
`18.1
`Introduction
`469
`18.2
`Broadcast Addresses
`18.3
`Unicast versus Broadcast
`472
`18.4
`dg_ cli Function Using Broadcasting
`18.5
`Race Conditions
`478
`18.6
`Summary
`486
`
`470
`
`446
`
`463
`
`475
`
`Chapter 19. Multicasting
`19.1
`Introduction
`487
`19.2
`Multicast Addresses
`487
`19.3
`Multicasting versus Broadcasting on a LAN
`19.4
`Multicasting on a WAN
`493
`19.5
`Multicast Socket Options
`495
`19.6
`mc ast_join and Related Functions
`499
`19.7
`dg_ cli Function Using Multicasting
`502
`19.8
`Receiving MBone Session Announcements
`19.9
`Sending and Receiving
`507
`19.10 SNTP: Simple Network Time Protocol
`19.1 1 SNTP (Continued)
`515
`19.12 Summary
`528
`
`510
`
`490
`
`504
`
`445
`
`469
`
`487
`
`Chapter 20. Advanced UDP Sockets
`20.1
`Introduction
`531
`20.2
`Receiving Flags, Destination IP Address, and Interface Index
`20.3
`Datagram Truncation
`539
`20.4 When to Use UDP Instead Of TCP
`20.5
`Adding Reliability to a UDP Application
`20.6
`Binding Interface Addresses
`553
`20.7
`Concurrent UDP Servers
`557
`20.8
`1Pv6 Packet Information
`560
`20.9
`Summary
`562
`
`539
`542
`
`531
`
`532
`
`Chapter 21. Out-of-Band Data
`21.1
`Introduction
`565
`21.2
`TCP Out-of-Band Data
`565
`sockatmark Function
`21.3
`572
`21.4
`TCP Out-of-Band Data Summary
`21.5
`Client-Server Heartbeat Functions
`21.6
`Summary
`586
`
`580
`581
`
`Chapter 22. Signal-Driven 1/0
`22.1
`Introduction
`589
`22.2
`Signal-Driven 1/0 for Sockets
`22.3
`UDP Echo Server Using SIGIO
`22.4
`Summary
`598
`
`590
`592
`
`Threads
`Chapter 23.
`Introduction
`23.1
`601
`23.2
`Basic Thread Functions: Creation and Termination
`23.3
`str_cli Function Using Threads
`605
`23.4
`TCP Echo Server Using Threads
`607
`Thread-Specific Data
`23.5
`611
`23.6 Web Client and Simultaneous Connections (Continued)
`23.7
`Mutexes: Mutual Exclusion
`622
`23.8
`Condition Variables
`627
`23.9 Web Client and Simultaneous Connections (Continued)
`23.10 Summary
`633
`
`602
`
`620
`
`631
`
`Chapter 24.
`24.1
`24.2
`24.3
`24.4
`24.5
`24.6
`24.7
`24.8
`
`IP Options
`Introduction
`635
`I Pv4 Options
`635
`1Pv4 Source Route Options
`637
`1Pv6 Extension Headers
`645
`lf)v6 Hop-by-Hop Options and Destination Options
`1Pv6 Routing Header
`649
`I Pv6 Sticky Options
`653
`Summary
`654
`
`645
`
`Chapter 25. Raw Sockets
`25.1
`Introduction
`655
`25.2
`Raw Socket Creation
`25.3
`Raw Socket Output
`Raw Socket Input
`25.4
`25.5
`Ping Program
`661
`25.6
`Traceroute Program
`672
`25.7
`An ICMP Message Daemon
`25.8
`702
`Summary
`
`656
`657
`659
`
`685
`
`565
`
`589
`
`601
`
`635
`
`655
`
`009
`
`
`
`x
`
`UNIX Network Programming
`
`Contents
`
`UNIX Network Programming
`
`Contents
`
`xi
`
`16.4
`File Operations
`427
`16.5
`Interface Configuration
`16.6
`get_ifi_info Function
`16.7
`Interface Operations
`439
`16.8
`ARP Cache Operations
`440
`16.9
`Routing Table Operations
`442
`16.10 Summary
`443
`
`428
`429
`
`Chapter 17. Routing Sockets
`17.1
`Introduction
`445
`17.2
`Datalink Socket Address Structure
`17.3
`Reading and Writing
`447
`17.4
`sysc t l Operations
`454
`17.5
`get_ifi_info Function
`459
`17.6
`Interface Name and Index Functions
`17.7
`Summary
`467
`
`Chapter 18. Broadcasting
`18.1
`Introduction
`469
`18.2
`Broadcast Addresses
`18.3
`Unicast versus Broadcast
`472
`18.4
`dg_ cli Function Using Broadcasting
`18.5
`Race Conditions
`478
`18.6
`Summary
`486
`
`470
`
`446
`
`463
`
`475
`
`Chapter 19. Multicasting
`19.1
`Introduction
`487
`19.2
`Multicast Addresses
`487
`19.3
`Multicasting versus Broadcasting on a LAN
`19.4
`Multicasting on a WAN
`493
`19.5
`Multicast Socket Options
`495
`19.6
`mc ast_join and Related Functions
`499
`19.7
`dg_ cli Function Using Multicasting
`502
`19.8
`Receiving MBone Session Announcements
`19.9
`Sending and Receiving
`507
`19.10 SNTP: Simple Network Time Protocol
`19.1 1 SNTP (Continued)
`515
`19.12 Summary
`528
`
`510
`
`490
`
`504
`
`445
`
`469
`
`487
`
`Chapter 20. Advanced UDP Sockets
`20.1
`Introduction
`531
`20.2
`Receiving Flags, Destination IP Address, and Interface Index
`20.3
`Datagram Truncation
`539
`20.4 When to Use UDP Instead Of TCP
`20.5
`Adding Reliability to a UDP Application
`20.6
`Binding Interface Addresses
`553
`20.7
`Concurrent UDP Servers
`557
`20.8
`1Pv6 Packet Information
`560
`20.9
`Summary
`562
`
`539
`542
`
`531
`
`532
`
`Chapter 21. Out-of-Band Data
`21.1
`Introduction
`565
`21.2
`TCP Out-of-Band Data
`565
`sockatmark Function
`21.3
`572
`21.4
`TCP Out-of-Band Data Summary
`21.5
`Client-Server Heartbeat Functions
`21.6
`Summary
`586
`
`580
`581
`
`Chapter 22. Signal-Driven 1/0
`22.1
`Introduction
`589
`22.2
`Signal-Driven 1/0 for Sockets
`22.3
`UDP Echo Server Using SIGIO
`22.4
`Summary
`598
`
`590
`592
`
`Threads
`Chapter 23.
`Introduction
`23.1
`601
`23.2
`Basic Thread Functions: Creation and Termination
`23.3
`str_cli Function Using Threads
`605
`23.4
`TCP Echo Server Using Threads
`607
`Thread-Specific Data
`23.5
`611
`23.6 Web Client and Simultaneous Connections (Continued)
`23.7
`Mutexes: Mutual Exclusion
`622
`23.8
`Condition Variables
`627
`23.9 Web Client and Simultaneous Connections (Continued)
`23.10 Summary
`633
`
`602
`
`620
`
`631
`
`Chapter 24.
`24.1
`24.2
`24.3
`24.4
`24.5
`24.6
`24.7
`24.8
`
`IP Options
`Introduction
`635
`I Pv4 Options
`635
`1Pv4 Source Route Options
`637
`1Pv6 Extension Headers
`645
`lf)v6 Hop-by-Hop Options and Destination Options
`1Pv6 Routing Header
`649
`I Pv6 Sticky Options
`653
`Summary
`654
`
`645
`
`Chapter 25. Raw Sockets
`25.1
`Introduction
`655
`25.2
`Raw Socket Creation
`25.3
`Raw Socket Output
`Raw Socket Input
`25.4
`25.5
`Ping Program
`661
`25.6
`Traceroute Program
`672
`25.7
`An ICMP Message Daemon
`25.8
`702
`Summary
`
`656
`657
`659
`
`685
`
`565
`
`589
`
`601
`
`635
`
`655
`
`010
`
`
`
`xii
`
`UNIX Network Programming
`
`Chapter 26.
`26.1
`26.2
`26.3
`26.4
`26.5
`26.6
`26.7
`
`Chapter 27.
`27.1
`27.2
`27.3
`27.4
`27.5
`27.6
`27.7
`27.8
`27.9
`27.10
`27.11
`27.12
`27.13
`
`Datalink Access
`Introduction
`703
`704
`BPF: BSD Packet Filter
`DLPI: Data Link Provider Interface
`Linux: SOCK_PACKET
`707
`libpcap: Packet Capture Library
`Examining the UDP Checksum Field
`Summary
`725
`
`706
`
`707
`708
`
`730
`
`Client-Server Design Alternatives
`Introduction
`727
`TCP Client Alternatives
`730
`TCP Test Client
`TCP Iterative Server
`732
`732
`TCP Concurrent Server, One Child per Client
`TCP Preforked Server, No Locking around accept
`TCP Preforked Server, File Locking around accept
`TCP Preforked Server, Thread Locking around accept
`TCP Preforked Server, Descriptor Passing
`746
`TCP Concurrent Server, One Thread per Client
`TCP Prethreaded Server, per-Thread accept
`TCP Prethreaded Server, Main Thread accept
`Summary
`759
`
`736
`742
`745
`
`752
`754
`756
`
`Part 4. XTI: X/Open Transport Interface
`
`Chapter 28.
`28.1
`28.2
`28.3
`28.4
`28.5
`28.6
`28.7
`28.8
`28.9
`28.10
`28.11
`28.12
`28.13
`
`Chapter 29.
`29.1
`29.2
`29.3
`29.4
`
`XTI: TCP Clients
`Introduction
`763
`764
`t_open Function
`t_error and t_strerror Functions
`netbuf Structures and XTI Structures
`t_bind Function
`770
`t_connect Function
`772
`t_rcv and t_snd Functions
`t_look Function
`774
`t_sndrel and t _rcvrel Functions
`t_snddis and t _rcvdis Functions
`XTI TCP Daytime Client
`778
`xti_rdwr Function
`781
`Summary
`782
`
`773
`
`767
`769
`
`775
`777
`
`XTI: Name and Address Functions
`Introduction
`783
`I etc / netconfig File and netconfig Functions
`NETPATH Variable and netpath Functions
`785
`netdir Functions
`786
`
`784
`
`Contents
`
`703
`
`727
`
`761
`
`763
`
`783
`
`UNIX Network Programming
`
`Contents
`
`xiii
`
`29.5
`29.6
`29.7
`29.8
`29.9
`
`t_alloc and t _ free Functions
`t _getprotaddr Functions
`790
`xti_ntop Function
`791
`tcp_connect Function
`Summary
`796
`
`792
`
`788
`
`Chapter 30.
`30.1
`30.2
`30.3
`30.4
`30.5
`30.6
`30.7
`30.8
`30.9
`
`Chapter 31.
`31.1
`31.2
`31.3
`31.4
`31.5
`31.6
`31.7
`
`XTI: TCP Servers
`Introduction
`797
`799
`t_listen Function
`800
`tcp_listen Function
`802
`t_accept Function
`803
`xti_accept Function
`804
`Simple Daytime Server
`Multiple Pending Connections
`x ti_accep t Function (Revisited)
`Summary
`816
`
`806
`
`808
`
`XTI: UDP Clients and Servers
`Introduction
`819
`t_rcvudata and t_sndudata Functions
`udp_client Function
`820
`t_rcvuderr Function: Asynchronous Errors
`udp_server Function
`826
`Reading a Datagram in Pieces
`Summary
`831
`
`829
`
`819
`
`824
`
`Chapter 32. XTI Options
`833
`Introduction
`32.1
`t_opthdr Structure
`32.2
`XTI Options
`837
`32.3
`840
`t_optmgmt Function
`32.4
`Checking If an Option Is Supported and Obtaining the
`32.5
`Getting and Setting XTI Options
`844
`32.6
`Summary
`848
`32.7
`
`835
`
`Chapter 33.
`33.1
`33.2
`33.3
`33.4
`33.5
`33.6
`33.7
`
`Chapter 34.
`34.1
`34.2
`34.3
`
`Streams
`849
`Introduction
`850
`Overview
`getmsg and putmsg Functions
`getpmsg and putpmsg Functions
`ioctl Function
`855
`TPI: Transport Provider Interface
`Summary
`866
`
`XTI: Additional Functions
`Introduction
`867
`Nonblocking 1/0
`867
`t_rcvconnect Function
`
`868
`
`854
`855
`
`856
`
`797
`
`819
`
`833
`
`Default
`
`841
`
`849
`
`867
`
`011
`
`
`
`xii
`
`UNIX Network Programming
`
`Chapter 26.
`26.1
`26.2
`26.3
`26.4
`26.5
`26.6
`26.7
`
`Chapter 27.
`27.1
`27.2
`27.3
`27.4
`27.5
`27.6
`27.7
`27.8
`27.9
`27.10
`27.11
`27.12
`27.13
`
`Datalink Access
`Introduction
`703
`704
`BPF: BSD Packet Filter
`DLPI: Data Link Provider Interface
`Linux: SOCK_PACKET
`707
`libpcap: Packet Capture Library
`Examining the UDP Checksum Field
`Summary
`725
`
`706
`
`707
`708
`
`730
`
`Client-Server Design Alternatives
`Introduction
`727
`TCP Client Alternatives
`730
`TCP Test Client
`TCP Iterative Server
`732
`732
`TCP Concurrent Server, One Child per Client
`TCP Preforked Server, No Locking around accept
`TCP Preforked Server, File Locking around accept
`TCP Preforked Server, Thread Locking around accept
`TCP Preforked Server, Descriptor Passing
`746
`TCP Concurrent Server, One Thread per Client
`TCP Prethreaded Server, per-Thread accept
`TCP Prethreaded Server, Main Thread accept
`Summary
`759
`
`736
`742
`745
`
`752
`754
`756
`
`Part 4. XTI: X/Open Transport Interface
`
`Chapter 28.
`28.1
`28.2
`28.3
`28.4
`28.5
`28.6
`28.7
`28.8
`28.9
`28.10
`28.11
`28.12
`28.13
`
`Chapter 29.
`29.1
`29.2
`29.3
`29.4
`
`XTI: TCP Clients
`Introduction
`763
`764
`t_open Function
`t_error and t_strerror Functions
`netbuf Structures and XTI Structures
`t_bind Function
`770
`t_connect Function
`772
`t_rcv and t_snd Functions
`t_look Function
`774
`t_sndrel and t _rcvrel Functions
`t_snddis and t _rcvdis Functions
`XTI TCP Daytime Client
`778
`xti_rdwr Function
`781
`Summary
`782
`
`773
`
`767
`769
`
`775
`777
`
`XTI: Name and Address Functions
`Introduction
`783
`I etc / netconfig File and netconfig Functions
`NETPATH Variable and netpath Functions
`785
`netdir Functions
`786
`
`784
`
`Contents
`
`703
`
`727
`
`761
`
`763
`
`783
`
`UNIX Network Programming
`
`Contents
`
`xiii
`
`29.5
`29.6
`29.7
`29.8
`29.9
`
`t_alloc and t _ free Functions
`t _getprotaddr Functions
`790
`xti_ntop Function
`791
`tcp_connect Function
`Summary
`796
`
`792
`
`788
`
`Chapter 30.
`30.1
`30.2
`30.3
`30.4
`30.5
`30.6
`30.7
`30.8
`30.9
`
`Chapter 31.
`31.1
`31.2
`31.3
`31.4
`31.5
`31.6
`31.7
`
`XTI: TCP Servers
`Introduction
`797
`799
`t_listen Function
`800
`tcp_listen Function
`802
`t_accept Function
`803
`xti_accept Function
`804
`Simple Daytime Server
`Multiple Pending Connections
`x ti_accep t Function (Revisited)
`Summary
`816
`
`806
`
`808
`
`XTI: UDP Clients and Servers
`Introduction
`819
`t_rcvudata and t_sndudata Functions
`udp_client Function
`820
`t_rcvuderr Function: Asynchronous Errors
`udp_server Function
`826
`Reading a Datagram in Pieces
`Summary
`831
`
`829
`
`819
`
`824
`
`Chapter 32. XTI Options
`833
`Introduction
`32.1
`t_opthdr Structure
`32.2
`XTI Options
`837
`32.3
`840
`t_optmgmt Function
`32.4
`Checking If an Option Is Supported and Obtaining the
`32.5
`Getting and Setting XTI Options
`844
`32.6
`Summary
`848
`32.7
`
`835
`
`Chapter 33.
`33.1
`33.2
`33.3
`33.4
`33.5
`33.6
`33.7
`
`Chapter 34.
`34.1
`34.2
`34.3
`
`Streams
`849
`Introduction
`850
`Overview
`getmsg and putmsg Functions
`getpmsg and putpmsg Functions
`ioctl Function
`855
`TPI: Transport Provider Interface
`Summary
`866
`
`XTI: Additional Functions
`Introduction
`867
`Nonblocking 1/0
`867
`t_rcvconnect Function
`
`868
`
`854
`855
`
`856
`
`797
`
`819
`
`833
`
`Default
`
`841
`
`849
`
`867
`
`012
`
`
`
`xiv
`
`UNIX Network Programming
`
`Contents
`
`34.4
`34.5
`34.6
`34.7
`34.8
`34.9
`34.10
`34.11
`34.12
`34.13
`34.14
`
`869
`869
`
`t_getinfo Function
`t_getstate Function
`t_sync Function
`870
`t_unbind Function
`872
`872
`t_rcvv and t_rcvvudata Functions
`873
`t_sndv and t_sndvudata Functions
`t_rcvreldata and t_sndreldata Functions
`Signal-Driven 1/0
`874
`Out-of-Band Data
`875
`Loopback Transport Providers
`Summary
`881
`
`880
`
`874
`
`Appendix A.
`A.1
`A.2
`A.3
`A.4
`A.5
`A.6
`
`1Pv4, 1Pv6, ICMPv4, and ICMPv6
`Introduction
`883
`1Pv4 Header
`883
`1Pv6 Header
`885
`1Pv4 Addresses
`887
`1Pv6 Addresses
`892
`ICMPv4 and ICMPv6: Internet Control Message Protocol
`
`896
`
`Appendix B. Vlrtual Networks
`B.1
`Introduction
`899
`B.2
`The MBone
`899
`B.3
`The 6bone
`901
`
`Appendix C.
`C.1
`C.2
`C.3
`C.4
`C.5
`C.6
`C.7
`
`Appendix D.
`D.1
`D.2
`D.3
`D.4
`
`Debugging Techniques
`System Call Tracing
`903
`Standard Internet Services
`sock Program
`908
`Small Test Programs
`tcpdump Program
`netstat Program
`lsof Program
`
`911
`913
`914
`914
`
`908
`
`Miscellaneous Source Code
`unp. h Header
`915
`config.h Header
`919
`unpxti . h Header
`920
`Standard Error Functions
`
`922
`
`Appendix E. Solutions to Selected Exercises
`
`Blbllography
`
`Index
`
`883
`
`899
`
`903
`
`915
`
`925
`
`963
`
`971
`
`Preface
`
`Introduction
`
`Network programming involves writing programs that communicate with other pro(cid:173)
`grams across a computer network. One program is normally called the client and the
`other the server. Most operating systems provide precompiled programs that communi(cid:173)
`cate across a network-common examples in the TCP /IP world are Web clients
`(browsers) and Web servers, and the FTP and Telnet clients and servers-but this book
`describes how to write our own network programs.
`We write network programs