Preface

Part 1.Introduction and TCP/IP 1


Chapter 1.Introduction 3

1.1Introduction3
1.2A Simple Daytime Client6
1.3Protocol Independence9
1.4Error Handling: Wrapper Functions11
1.5A Simple Daytime Server13
1.6Road Map to Client-Server Examples in the Text16
1.7OSI Model18
1.8BSD Networking History19
1.9Test Networks and Hosts20
1.10Unix Standards24
1.1164-bit Architectures27
1.12Summary28

Chapter 2.The Transport Layer: TCP and UDP 29

2.1Introduction29
2.2The Big Picture30
2.3UDP: User Datagram Protocol32
2.4TCP: Transmission Control Protocol32
2.5TCP Connection Establishment and Termination34
2.6TIME_WAIT State40
2.7Port Numbers41
2.8TCP Port Numbers and Concurrent Servers44
2.9Buffer Sizes and Limitations46
2.10Standard Internet Services50
2.11Protocol Usage by Common Internet Applications52
2.12Summary52

Part 2.Elementary Sockets 55


Chapter 3.Sockets Introduction 57

3.1Introduction57
3.2Socket Address Structures57
3.3Value-Result Arguments63
3.4Byte Ordering Functions66
3.5Byte Manipulation Functions69
3.6inet_aton, inet_addr, and inet_ntoa Functions70
3.7inet_pton and inet_ntop Functions72
3.8sock_ntop and Related Functions75
3.9readn, writen, and readline Functions77
3.10isfdtype Function81
3.11Summary82

Chapter 4.Elementary TCP Sockets 85

4.1Introduction85
4.2socket Function85
4.3connect Function89
4.4bind Function91
4.5listen Function93
4.6accept Function99
4.7fork and exec Functions102
4.8Concurrent Servers104
4.9close Function107
4.10getsockname and getpeername Functions107
4.11Summary110

Chapter 5.TCP Client-Server Example 111

5.1Introduction111
5.2TCP Echo Server: main Function112
5.3TCP Echo Server: str_echo Function113
5.4TCP Echo Client: main Function113
5.5TCP Echo Client: str_cli Function115
5.6Normal Startup115
5.7Normal Termination117
5.8Posix Signal Handling119
5.9Handling SIGCHLD Signals122
5.10wait and waitpid Functions124
5.11Connection Abort before accept Returns129
5.12Termination of Server Process130
5.13SIGPIPE Signal132
5.14Crashing of Server Host133
5.15Crashing and Rebooting of Server Host134
5.16Shutdown of Server Host135
5.17Summary of TCP Example135
5.18Data Format137
5.19Summary140

Chapter 6.I/O Multiplexing: The select and poll Functions 143

6.1Introduction143
6.2I/O Models144
6.3select Function150
6.4str_cli Function (Revisited)155
6.5Batch Input157
6.6shutdown Function160
6.7str_cli Function (Revisited Again)161
6.8TCP Echo Server (Revisited)162
6.9pselect Function168
6.10poll Function169
6.11TCP Echo Server (Revisited Again)172
6.12Summary175

Chapter 7.Socket Options 177

7.1Introduction177
7.2getsockopt and setsockopt Functions178
7.3Checking If an Option Is Supported and Obtaining the Default178
7.4Socket States183
7.5Generic Socket Options183
7.6IPv4 Socket Options197
7.7ICMPv6 Socket Option199
7.8IPv6 Socket Options199
7.9TCP Socket Options201
7.10fcntl Function205
7.11Summary207

Chapter 8.Elementary UDP Sockets 211

8.1Introduction211
8.2recvfrom and sendto Functions212
8.3UDP Echo Server: main Function213
8.4UDP Echo Server: dg_echo Function214
8.5UDP Echo Client: main Function216
8.6UDP Echo Client: dg_cli Function217
8.7Lost Datagrams217
8.8Verifying Received Response218
8.9Server Not Running220
8.10Summary of UDP example221
8.11connect Function with UDP224
8.12dg_cli Function (Revisited)227
8.13Lack of Flow Control with UDP228
8.14Determining Outgoing Interface with UDP231
8.15TCP and UDP Echo Server Using select233
8.16Summary235

Chapter 9.Elementary Name and Address Conversions 237

9.1Introduction237
9.2Domain Name System237
9.3gethostbyname Function240
9.4RES_USE_INET6 Resolver Option245
9.5gethostbyname2 Function and IPv6 Support246
9.6gethostbyaddr Function248
9.7uname Function249
9.8gethostname Function250
9.9getservbyname and getservbyport Functions251
9.10Other Networking Information255
9.11Summary256

Part 3.Advanced Sockets 259


Chapter 10.IPv4 and IPv6 Interoperability 261

10.1Introduction261
10.2IPv4 Client, IPv6 Server262
10.3IPv6 Client, IPv4 Server265
10.4IPv6 Address Testing Macros267
10.5IPV6_ADDRFORM Socket Option268
10.6Source Code Portability270
10.7Summary271

Chapter 11.Advanced Name and Address Conversions 273

11.1Introduction273
11.2getaddrinfo Function273
11.3gai_strerror Function278
11.4freeaddrinfo Function279
11.5getaddrinfo Function: IPv6 and Unix Domain279
11.6getaddrinfo Function: Examples282
11.7host_serv Function284
11.8tcp_connect Function285
11.9tcp_listen Function288
11.10udp_client Function293
11.11udp_connect Function295
11.12udp_server Function296
11.13getnameinfo Function298
11.14Reentrant Functions300
11.15gethostbyname_r and gethostbyaddr_r Functions303
11.16Implementation of getaddrinfo and getnameinfo Functions305
11.17Summary328

Chapter 12.Daemon Processes and inetd Superserver 331

12.1Introduction331
12.2syslogd Daemon332
12.3syslog Function333
12.4daemon_init Function335
12.5inetd Daemon339
12.6daemon_inetd Function344
12.7Summary346

Chapter 13.Advanced I/O Functions 349

13.1Introduction349
13.2Socket Timeouts349
13.3recv and send Functions354
13.4readv and writev Functions357
13.5recvmsg and sendmsg Functions358
13.6Ancillary Data362
13.7How Much Data Is Queued?365
13.8Sockets and Standard I/O366
13.9T/TCP: TCP for Transactions369
13.10Summary371

Chapter 14.Unix Domain Protocols 373

14.1Introduction373
14.2Unix Domain Socket Address Structure374
14.3socketpair Function376
14.4Socket Functions377
14.5Unix Domain Stream Client-Server378
14.6Unix Domain Datagram Client-Server379
14.7Passing Descriptors381
14.8Receiving Sender Credentials390
14.9Summary394

Chapter 15.Nonblocking I/O 397

15.1Introduction397
15.2Nonblocking Reads and Writes: str_cli Function (Revisited)399
15.3Nonblocking connect409
15.4Nonblocking connect: Daytime Client410
15.5Nonblocking connect: Web Client413
15.6Nonblocking accept422
15.7Summary424

Chapter 16.ioctl Operations 425

16.1Introduction425
16.2ioctl Function426
16.3Socket Operations426
16.4File Operations427
16.5Interface Configuration428
16.6get_ifi_info Function429
16.7Interface Operations439
16.8ARP Cache Operations440
16.9Routing Table Operations442
16.10Summary443

Chapter 17.Routing Sockets 445

17.1Introduction445
17.2Datalink Socket Address Structure446
17.3Reading and Writing447
17.4sysctl Operations454
17.5get_ifi_info Function459
17.6Interface Name and Index Functions463
17.7Summary467

Chapter 18.Broadcasting 469

18.1Introduction469
18.2Broadcast Addresses470
18.3Unicast versus Broadcast472
18.4dg_cli Function Using Broadcasting475
18.5Race Conditions478
18.6Summary486

Chapter 19.Multicasting 487

19.1Introduction487
19.2Multicast Addresses487
19.3Multicasting versus Broadcasting on A LAN490
19.4Multicasting on a WAN493
19.5Multicast Socket Options495
19.6mcast_join and Related Functions499
19.7dg_cli Function Using Multicasting502
19.8Receiving MBone Session Announcements504
19.9Sending and Receiving507
19.10SNTP: Simple Network Time Protocol510
19.11SNTP (Continued)515
19.12Summary528

Chapter 20.Advanced UDP Sockets 531

20.1Introduction531
20.2Receiving Flags, Destination IP Address, and Interface Index532
20.3Datagram Truncation539
20.4When to Use UDP Instead Of TCP539
20.5Adding Reliability to a UDP Application542
20.6Binding Interface Addresses553
20.7Concurrent UDP Servers557
20.8IPv6 Packet Information560
20.9Summary562

Chapter 21.Out-of-Band Data 565

21.1Introduction565
21.2TCP Out-of-Band Data565
21.3sockatmark Function572
21.4TCP Out-of-Band Data Summary580
21.5Client-Server Heartbeat Functions581
21.6Summary586

Chapter 22.Signal-Driven I/O 589

22.1Introduction589
22.2Signal-Driven I/O for Sockets590
22.3UDP Echo Server Using SIGIO592
22.4Summary598

Chapter 23.Threads 601

23.1Introduction601
23.2Basic Thread Functions: Creation and Termination602
23.3str_cli Function Using Threads605
23.4TCP Echo Server Using Threads607
23.5Thread-Specific Data611
23.6Web Client and Simultaneous Connections (Continued)620
23.7Mutexes: Mutual Exclusion622
23.8Condition Variables627
23.9Web Client and Simultaneous Connections (Continued)631
23.10Summary633

Chapter 24.IP Options 635

24.1Introduction635
24.2IPv4 Options635
24.3IPv4 Source Route Options637
24.4IPv6 Extension Headers645
24.5IPv6 Hop-by-Hop Options and Destination Options645
24.6IPv6 Routing Header649
24.7IPv6 Sticky Options653
24.8Summary654

Chapter 25.Raw Sockets 655

25.1Introduction655
25.2Raw Socket Creation656
25.3Raw Socket Output657
25.4Raw Socket Input659
25.5Ping Program661
25.6Traceroute Program672
25.7An ICMP Message Daemon685
25.8Summary702

Chapter 26.Datalink Access 703

26.1Introduction703
26.2BPF: BSD Packet Filter704
26.3DLPI: Data Link Provider Interface706
26.4Linux: SOCK_PACKET707
26.5libpcap: Packet Capture Library707
26.6Examining the UDP Checksum Field708
26.7Summary725

Chapter 27.Client-Server Design Alternatives 727

27.1Introduction727
27.2TCP Client Alternatives730
27.3TCP Test Client730
27.4TCP Iterative Server732
27.5TCP Concurrent Server, One Child per Client732
27.6TCP Preforked Server, No Locking around accept736
27.7TCP Preforked Server, File Locking around accept742
27.8TCP Preforked Server, Thread Locking around accept745
27.9TCP Preforked Server, Descriptor Passing746
27.10TCP Concurrent Server, One Thread per Client752
27.11TCP Prethreaded Server, per-Thread accept754
27.12TCP Prethreaded Server, Main Thread accept756
27.13Summary759

Part 4.XTI: X/Open Transport Interface 761


Chapter 28.XTI: TCP Clients 763

28.1Introduction763
28.2t_open Function764
28.3t_error and t_strerror Functions767
28.4netbuf Structures and XTI Structures769
28.5t_bind Function770
28.6t_connect Function772
28.7t_rcv and t_snd Functions773
28.8t_look Function774
28.9t_sndrel and t_rcvrel Functions775
28.10t_snddis and t_rcvdis Functions777
28.11XTI TCP Daytime Client778
28.12xti_rdwr Function781
28.13Summary782

Chapter 29.XTI: Name and Address Functions 783

29.1Introduction783
29.2/etc/netconfig File and netconfig Functions784
29.3NETPATH Variable and netpath Functions785
29.4netdir Functions786
29.5t_alloc and t_free Functions788
29.6t_getprotaddr Functions790
29.7xti_ntop Function791
29.8tcp_connect Function792
29.9Summary796

Chapter 30.XTI: TCP Servers 797

30.1Introduction797
30.2t_listen Function799
30.3tcp_listen Function800
30.4t_accept Function802
30.5xti_accept Function803
30.6Simple Daytime Server804
30.7Multiple Pending Connections806
30.8xti_accept Function (Revisited)808
30.9Summary816

Chapter 31.XTI: UDP Clients and Servers 819

31.1Introduction819
31.2t_rcvudata and t_sndudata Functions819
31.3udp_client Function820
31.4t_rcvuderr Function: Asynchronous Errors824
31.5udp_server Function826
31.6Reading a Datagram in Pieces829
31.7Summary831

Chapter 32.XTI Options 833

32.1Introduction833
32.2t_opthdr Structure835
32.3XTI Options837
32.4t_optmgmt Function840
32.5Checking If an Option Is Supported and Obtaining the Default841
32.6Getting and Setting XTI Options844
32.7Summary848

Chapter 33.Streams 849

33.1Introduction849
33.2Overview850
33.3getmsg and putmsg Functions854
33.4getpmsg and putpmsg Functions855
33.5ioctl Function855
33.6TPI: Transport Provider Interface856
33.7Summary866

Chapter 34.XTI: Additional Functions 867

34.1Introduction867
34.2Nonblocking I/O867
34.3t_rcvconnect Function868
34.4t_getinfo Function869
34.5t_getstate Function869
34.6t_sync Function870
34.7t_unbind Function872
34.8t_rcvv and t_rcvvudata Functions872
34.9t_sndv and t_sndvudata Functions873
34.10t_rcvreldata and t_sndreldata Functions874
34.11Signal-Driven I/O874
34.12Out-of-Band Data875
34.13Loopback Transport Providers880
34.14Summary881

Appendix A.IPv4, IPv6, ICMPv4, and ICMPv6 883

A.1Introduction883
A.2IPv4 Header883
A.3IPv6 Header885
A.4IPv4 Addresses887
A.5IPv6 Addresses892
A.6ICMPv4 and ICMPv6: Internet Control Message Protocol896

Appendix B.Virtual Networks 899

B.1Introduction899
B.2The MBone899
B.3The 6bone901

Appendix C.Debugging Techniques 903

C.1System Call Tracing903
C.2Standard Internet Services908
C.3sock Program908
C.4Small Test Programs911
C.5tcpdump Program913
C.6netstat Program914
C.7lsof Program914

Appendix D.Miscellaneous Source Code 915

D.1unp.h Header915
D.2config.h Header919
D.3unpxti.h Header920
D.4Standard Error Functions922

Appendix E.Solutions to Selected Exercises 925

Bibliography 963

Index 971