UNIX Network Programming

Similar documents
Contents. Part 1. Introduction and TCP/IP 1. Foreword Preface. xix. I ntroduction 31

LESSON PLAN. Sub. Code & Name : IT2351 & Network Programming and Management Unit : I Branch: IT Year : III Semester: VI.

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

NETWORK PROGRAMMING AND MANAGEMENT 1 KINGS DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK

VALLIAMMAI ENGINEERING COLLEGE. SRM Nagar, Kattankulathur QUESTION BANK

Computer Networks SYLLABUS CHAPTER - 2 : NETWORK LAYER CHAPTER - 3 : INTERNETWORKING

Advanced Programming in the UNIX Environment W. Richard Stevens

Introduction! Overview! Signal-driven I/O for Sockets! Two different UDP servers!

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

IPv6 Core Protocols Implementation

Programming with POSIX Threads

The Unified Modeling Language User Guide

UNIT IV- SOCKETS Part A

Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

Light & NOS. Dan Li Tsinghua University

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Lecture 10 Overview!

Application Programming

Quality Code. Software Testing Principles, Practices, and Patterns. Stephen Vance. AAddison-Wesley

Internetworking With TCP/IP

Secure Coding in C and C++

Introduction... xiii Chapter 1: Introduction to Computer Networks and Internet Computer Networks Uses of Computer Networks...

IPv4 and ipv6 INTEROPERABILITY

MARIA COLLEGE OF ENGINEERING AND TECHNOLOGY, ATTOOR DEPARTMENT OF INFORMATION TECHNOLOGY NETWORK PROGRAMMING MANAGEMENT 2 MARKS QUESTIONS & ANSWERS

Computer Network Programming. The Transport Layer. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Internetworking With TCP/IP

Programming in Python 3

CCNA Cisco Certified Network Associate Study Guide

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Lecture Plan-1. Contents Methodology Time Queries Remarks. Question/ Answers. Discussion. Chalk Board. &Discussion. Discussion 10

NETWORKING KEITH W. ROSS. Polytechnic Institute of NYU. Addison-Wesley

NETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability

CS UDP: User Datagram Protocol, Other Transports, Sockets. congestion worse);

Integrated Approach. Operating Systems COMPUTER SYSTEMS. LEAHY, Jr. Georgia Institute of Technology. Umakishore RAMACHANDRAN. William D.

The Power of Events. An Introduction to Complex Event Processing in Distributed Enterprise Systems. David Luckham

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

Fit for Developing Software

Lecture 3. The Network Layer (cont d) Network Layer 1-1

Foundations of Python

ICS 351: Networking Protocols

Brief Contents. Acknowledgments... xv. Introduction...xvii. Chapter 1: Packet Analysis and Network Basics Chapter 2: Tapping into the Wire...

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Network programming(ii) Lenuta Alboaie

csdesign Documentation

Z/TPF TCP/IP SOCK Driver 12/14/10. z/tpf TCP/IP SOCKET Driver Users Guide. Copyright IBM Corp. 2010

Layer 4: UDP, TCP, and others. based on Chapter 9 of CompTIA Network+ Exam Guide, 4th ed., Mike Meyers

Chapter 8: I/O functions & socket options

Linux Kernel Application Interface

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

Socket Programming(2/2)

Foreword xxiii Preface xxvii IPv6 Rationale and Features

Modern C++ Design. Generic Programming and Design Patterns Applied. Andrei Alexandrescu. AAddison-Wesley

Topics for this Week

Lecture 8. Network Layer (cont d) Network Layer 1-1

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

Internet Layers. Physical Layer. Application. Application. Transport. Transport. Network. Network. Network. Network. Link. Link. Link.

Stream Control Transmission Protocol (SCTP)

Digital System Design with SystemVerilog

Operating Systems. 17. Sockets. Paul Krzyzanowski. Rutgers University. Spring /6/ Paul Krzyzanowski

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

User Datagram Protocol

DATA AND COMPUTER COMMUNICATIONS

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

(Refer Slide Time: 1:09)

Vorlesung Kommunikationsnetze

ICS 451: Today's plan

CSCD433/533 Advanced Networks Winter 2017 Lecture 13. Raw vs. Cooked Sockets

and Networks Data Communications Second Edition Tata McGraw Hill Education Private Limited Managing Director SoftExcel Services Limited, Mumbai

Interprocess Communication

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION 1 PACKET ANALYSIS AND NETWORK BASICS 1 2 TAPPING INTO THE WIRE 17 3 INTRODUCTION TO WIRESHARK 35

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

TCP /IP Fundamentals Mr. Cantu

A Client-Server Exchange

ETSF10 Internet Protocols Network Layer Protocols

TRANSMISSION CONTROL PROTOCOL. ETI 2506 TELECOMMUNICATION SYSTEMS Monday, 7 November 2016

Introduction to Networking. Operating Systems In Depth XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Modern C++ Design. Generic Programming and Design Patterns Applied. Andrei Alexandrescu. .~Addison-Wesley

Preface to the First Edition Preface to the Second Edition Acknowledgments UNIX Operating System Environment p. 1 UNIX: Past and Present p.

CSC 634: Networks Programming

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

Transport Layer. The transport layer is responsible for the delivery of a message from one process to another. RSManiaol

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

CCNA R&S: Introduction to Networks. Chapter 7: The Transport Layer

Linux Kernel Application Interface

ETSF05/ETSF10 Internet Protocols Network Layer Protocols

Step 2. Manual configuration of global unicast and link-local addresses

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Concept Questions Demonstrate your knowledge of these concepts by answering the following questions in the space that is provided.

Socket Programming for TCP and UDP

Lecture 11: IP routing, IP protocols

Programming Wireless Devices with the Java 2 Platform, Micro Edition

LINUX INTERNALS & NETWORKING Weekend Workshop

Programming with TCP/IP. Ram Dantu

Transcription:

UNIX Network Programming The Sockets Networking API Volume 1 Third Edition W. Richard Stevens Bill Fenner Andrew M. Rudoff AAddison-Wesley Boston San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City

Contents Foreword Preface XVII xix Part 1. Introduction and TCP/IP Chapter 1. Introduction 1.1 Introduction 3 1.2 A Simple Daytime Client 6 1.3 Protocol Independence 10 1.4 Error Handling: Wrapper Functions 11 1.5 A Simple Daytime Server 13 1.6 Roadmap to Client/Server Examples in the Text 1.7 OSI Model 18 1.8 BSD Networking History 20 1.9 Test Networks and Hosts 22 1.10 Unix Standards 25 1.11 64-Bit Architectures 28 1.12 Summary 29 16 Chapter 2. The Transport Layer: TCP, UDP, and SCTP 2.1 Introduction 31 2.2 The Big Picture 32 2.3 User Datagram Protocol (UDP) 34 31

viii UNIX Network Programming Contents 2.4 Transmission Control Protocol (TCP) 35 2.5 Stream Control Transmission Protocol (SCTP) 36 2.6 TCP Connection Establishment and Termination 37 2.7 TIMEJA/AIT State 43 2.8 SCTP Association Establishment and Termination 44 2.9 Port Numbers 50 2.10 TCP Port Numbers and Concurrent Servers 52 2.11 Buffer Sizes and Limitations 55 2.12 Standard Internet Services 61 2.13 Protocol Usage by Common Internet Applications 62 2.14 Summary 63 Part 2. Elementary Sockets 65 Chapter 3. Sockets Introduction 67 3.1 Introduction 67 3.2 Socket Address Structures 67 3.3 Value-Result Arguments 74 3.4 Byte Ordering Functions 77 3.5 Byte Manipulation Functions 80 3.6 inet_aton, inet_addr, and inet_ntoa Functions 82 3.7 inet_pton and inet_ntop Functions 83 3.8 sock_ntop and Related Functions 86 3.9 readn, writen, and readline Functions 88 3.10 Summary 92 Chapter 4. Elementary TCP Sockets 95 4.1 Introduction 95 4.2 socket Function 95 4.3 connect Function 99 4.4 bind Function 101 4.5 listen Function 104 4.6 accept Function 109 4.7 fork and exec Functions 111 4.8 Concurrent Servers 114 4.9 close Function 117 4.10 getsockname and getpeername Functions 117 4.11 Summary 120 Chapter 5. TCP Client/Server Example 121 5.1 Introduction 121 5.2 TCP Echo Server: main Function 122 5.3 TCP Echo Server: str_echo Function 123 5.4 TCP Echo Client: main Function 124 5.5 TCP Echo Client: str_cli Function 125 5.6 Normal Startup 126 5.7 Normal Termination 128

UNIX Network Programming Contents ix 5.8 POSIX Signal Handling 129 5.9 Handling SIGCHLD Signals 132 5.10 wait and waitpid Functions 135 5.11 Connection Abort betöre accept Returns 139 5.12 Termination of Server Process 141 5.13 SIGPIPE Signal 142 5.14 Crashing of Server Host 144 5.15 Crashing and Rebooting of Server Host 144 5.16 Shutdown of Server Host 145 5.17 Summary of TCP Example 146 5.18 Data Format 147 5.19 Summary 151 Chapter 6. I/O Multiplexing: The select and poll Functions 153 6.1 Introduction 153 6.2 I/O Models 154 6.3 select Function 160 6.4 str_cli Function (Revisited) 167 6.5 Batch Input and Buffering 169 6.6 shutdown Function 172 6.7 str_cli Function (Revisited Again) 173 6.8 TCP Echo Server (Revisited) 175 6.9 pselect Function 181 6.10 poll Function 182 6.11 TCP Echo Server (Revisited Again) 185 6.12 Summary 188 Chapter 7. Socket Options 191 7.1 Introduction 191 7.2 getsockopt and setsockopt Functions 192 7.3 Checking if an Option Is Supported and Obtaining the Default 194 7.4 Socket States 198 7.5 Generic Socket Options 198 7.6 IPv4 Socket Options 214 7.7 ICMPv6 Socket Option 216 7.8 IPv6 Socket Options 216 7.9 TCP Socket Options 219 7.10 SCTP Socket Options 222 7.11 fcntl Function 233 7.12 Summary 236 Chapter 8. Elementary UDP Sockets 239 8.1 Introduction 239 8.2 recvfrom and sendto Functions 240 8.3 UDP Echo Server: main Function 241 8.4 UDP Echo Server: dg_echo Function 242 8.5 UDP Echo Client: main Function 244 8.6 UDP Echo Client: dgcli Function 245

x UNIX Network Programming Contents 8.7 Lost Datagrams 245 8.8 Verifying Received Response 246 8.9 Server Not Running 248 8.10 Summary of UDP Example 250 8.11 connect Function with UDP 252 8.12 dg_cli Function (Revisited) 256 8.13 Lack of Flow Control with UDP 257 8.14 Determining Outgoing Interface with UDP 261 8.15 TCP and UDP Echo Server Using select 262 8.16 Summary 264 Chapter 9. Elementary SCTP Sockets 267 9.1 Introduction 267 9.2 Interface Models 268 9.3 sctpbindx Function 272 9.4 sctp_connectx Function 274 9.5 sctp_getpaddrs Function 275 9.6 sctp_freepaddrs Function 275 9.7 sctp_getladdrs Function 275 9.8 sctp_freeladdrs Function 276 9.9 sctp_sendmsg Function 276 9.10 sctp_recvmsg Function 277 9.11 sctp_opt_info Function 278 9.12 sctp_peeloff Function 278 9.13 shutdown Function 278 9.14 Notifications 280 9.15 Summary 286 Chapter 10. SCTP Client/Server Example 287 10.1 Introduction 287 10.2 SCTP One-to-Many-Style Streaming Echo Server: main Function 288 10.3 SCTP One-to-Many-Style Streaming Echo Client: main Function 290 10.4 SCTP Streaming Echo Client: strcli Function 292 10.5 Exploring Head-of-Line Blocking 293 10.6 Controlling the Number of Streams 299 10.7 Controlling Termination 300 10.8 Summary 301 Chapter 11. Name and Address Conversions 303 11.1 Introduction 303 11.2 Domain Name System (DNS) 303 11.3 gethostbyname Function 307 11.4 gethostbyaddr Function 310 11.5 getservbyname and getservbyport Functions 311 11.6 getaddrinfo Function 315 11.7 gai_strerror Function 320 11.8 freeaddrinfo Function 321 11.9 getaddrinfo Function: IPv6 322

UNIX Network Programming Contents xi 11.10 getaddrinfo Function: Examples 324 11.11 host_serv Function 325 11.12 tcp_connect Function 326 11.13 tcp listen Function 330 11.14 udp_client Function 334 11.15 udp_connect Function 337 11.16 udpserver Function 338 11.17 getnameinfo Function 340 11.18 Re-entrant Functions 341 11.19 gethostbyname_r and gethostbyaddr_r 11.20 Obsolete IPv6 Address Lookup Functions 11.21 Other Networking Information 348 11.22 Summary 349 Functions 346 344 Part 3. Advanced Sockets 351 Chapter 12. 12.1 12.2 12.3 12.4 12.5 12.6 Chapter 13. 13.1 13.2 13.3 13.4 13.5 13.6 13.7 Chapter 14. 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9 14.10 IPv4 and IPv6 Interoperability Introduction 353 IPv4 Client, IPv6 Server 354 IPv6 Client, IPv4 Server 357 IPv6 Address-Testing Macros 360 Source Code Portability 361 Summary 362 Daemon Processes and the inetd Superserver Introduction 363 syslogd Daemon 364 syslog Function 365 daemon_init Function 367 inetd Daemon 371 daemon_inetd Function 377 Summary 379 Advanced I/O Functions Introduction 381 Socket Timeouts 381 recv and send Functions 387 readv and writev Functions 389 recvmsg and sendmsg Functions 390 Ancillary Data 395 How Much Data Is Queued? 398 Sockets and Standard I/O 399 Advanced Polling 402 Summary 408 353 363 381

xii UNIX Network Programming Contents Chapter 15. 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 Chapter 16. 16.1 16.2 16.3 16.4 16.5 16.6 16.7 Unix Domain Protocols 411 Introduction 411 Unix Domain Socket Address Structure 412 socketpair Function 414 Socket Functions 415 Unix Domain Stream Client/Server 416 Unix Domain Datagram Client/Server 418 Passing Descriptors 420 Receiving Sender Credentials 429 Summary 432 Nonblocking I/O 435 Introduction 435 Nonblocking Reads and Writes: strcli Function (Revisited) 437 Nonblocking connect 448 Nonblocking connect: Daytime Client 449 Nonblocking connect: Web Client 452 Nonblocking accept 461 Summary 463 Chapter 17. 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10 Chapter 18. 18.1 18.2 18.3 18.4 18.5 18.6 18.7 ioctl Operations Introduction 465 ioctl Function 466 Socket Operations 466 File Operations 468 Interface Configuration 468 get_ifi_info Function 469 Interface Operations 480 ARP Cache Operations 481 Routing Table Operations 483 Summary 484 Routing Sockets Introduction 485 Datalink Socket Address Structure Reading and Writing 487 sysctl Operations 495 get_ifi_info Function (Revisited) Interface Name and Index Functions Summary 508 486 500 504 465 485 Chapter 19. 19.1 19.2 19.3 19.4 19.5 19.6 Key Management Sockets Introduction 511 Reading and Writing 512 Dumping the Security Association Database (SADB) Creating a Static Security Association (SA) 517 Dynamically Maintaining SAs 524 Summary 528 514 511

UNIX Network Programming Contents xiii Chapter 20. Broadcasting 20.1 Introduction 529 20.2 Broadcast Addresses 531 20.3 Unicast versus Broadcast 532 20.4 dgcli Function Using Broadcasting 20.5 Race Conditions 538 20.6 Summary 547 535 529 Chapter 21. Multicasting 21.1 Introduction 549 21.2 Multicast Addresses 549 21.3 Multicasting versus Broadcasting on a LAN 553 21.4 Multicasting on a WAN 556 21.5 Source-Specific Multicast 558 21.6 Multicast Socket Options 559 21.7 mcast_join and Related Functions 21.8 dgcli Function Using Multicasting 565 570 21.9 Receiving IP Multicast Infrastructure Session Announcements 21.10 Sending and Receiving 575 21.11 Simple Network Time Protocol (SNTP) 579 21.12 Summary 584 571 549 Chapter 22. Advanced UDP Sockets 22.1 Introduction 587 22.2 Receiving Flags, Destination IP Address, and Interface Index 22.3 Datagram Truncation 594 22.4 When to Use UDP Instead of TCP 594 22.5 Adding Reliability to a UDP Application 597 22.6 Binding Interface Addresses 608 22.7 Concurrent UDP Servers 612 22.8 IPv6 Packet Information 615 22.9 IPv6 Path MTU Control 618 22.10 Summary 620 588 587 Chapter 23. Advanced SCTP Sockets 23.1 Introduction 621 23.2 An Autoclosing One-to-Many-Style Server 621 23.3 Partial Delivery 622 23.4 Notifications 625 23.5 Unordered Data 629 23.6 Binding a Subset of Addresses 630 23.7 Determining Peer and Local Address Information 23.8 Finding an Association ID Given an IP Address 23.9 Heartbeating and Address Failure 636 23.10 Peeling Off an Association 637 23.11 Controlling Timing 639 23.12 When to Use SCTP Instead of TCP 641 23.13 Summary 643 631 635 621

xiv UNIX Network Programming Contents Chapter 24. Out-of-Band Data 24.1 Introduction 645 24.2 TCP Out-of-Band Data 645 24.3 sockatmark Function 654 24.4 TCP Out-of-Band Data Recap 24.5 Summary 662 661 645 Chapter 25. 25.1 25.2 25.3 25.4 Signal-Driven I/O Introduction 663 Signal-Driven I/O for Sockets UDP Echo Server Using SIGIO Summary 672 664 666 663 Chapter 26. Threads 26.1 Introduction 675 26.2 Basic Thread Functions: Creation and Termination 676 26.3 strcli Function Using Threads 679 26.4 TCP Echo Server Using Threads 681 26.5 Thread-Specific Data 686 26.6 Web Client and Simultaneous Connections (Continued) 694 26.7 Mutexes: Mutual Exclusion 697 26.8 Condition Variables 701 26.9 Web Client and Simultaneous Connections (Continued) 705 26.10 Summary 707 675 Chapter 27. IP Options 27.1 Introduction 709 27.2 IPv4 Options 709 27.3 IPv4 Source Route Options 711 27.4 IPv6 Extension Headers 719 27.5 IPv6 Hop-by-Hop Options and Destination Options 27.6 IPv6 Routing Header 725 27.7 IPv6 Sticky Options 731 27.8 Historical IPv6 Advanced API 732 27.9 Summary 733 719 709 Chapter 28. Raw Sockets 28.1 Introduction 735 28.2 Raw Socket Creation 736 28.3 Raw Socket Output 737 28.4 Raw Socket Input 739 28.5 ping Program 741 28.6 traceroute Program 755 28.7 An ICMP Message Daemon 769 28.8 Summary 786 735

UNIX Network Programming Contents xv Chapter 29. Datalink Access 29.1 Introduction 787 29.2 BSD Packet Filter (BPF) 788 29.3 Datalink Provider Interface (DLPI) 790 29.4 Linux: SOCK_PACKET and PF_PACKET 791 29.5 libpcap: Packet Capture Library 792 29.6 libnet: Packet Creation and Injection Library 29.7 Examining the UDP Checksum Field 793 29.8 Summary 815 793 787 Chapter 30. 30.1 30.2 30.3 30.4 30.5 30.6 30.7 30.8 30.9 30.10 30.11 30.12 30.13 Client/Server Design Alternatives Introduction 817 TCP Client Alternatives 819 TCP Test Client 820 TCP Iterative Server 821 TCP Concurrent Server, One Child per Client 822 TCP Preforked Server, No Locking Around accept 826 TCP Preforked Server, File Locking Around accept 832 TCP Preforked Server, Thread Locking Around accept 835 TCP Preforked Server, Descriptor Passing 836 TCP Concurrent Server, One Thread per Client 842 TCP Prethreaded Server, per-thread accept 844 TCP Prethreaded Server, Main Thread accept 846 Summary 849 817 Chapter 31. STREAMS 31.1 Introduction 851 31.2 Overview 851 31.3 getmsg and putmsg Functions 31.4 getpmsg and putpmsg Functions 31.5 ioctl Function 857 31.6 Transport Provider Interface (TPI) 31.7 Summary 868 856 857 858 851 Appendix A. IPv4, IPv6, ICMPv4, and ICMPv6 A.1 Introduction 869 A.2 IPv4 Header 869 A.3 IPv6 Header 871 A.4 IPv4 Addresses 874 A.5 IPv6 Addresses 877 A.6 Internet Control Message Protocols (ICMPv4 and ICMPv6) Appendix B. Virtual Networks B.1 Introduction 885 B.2 The MBone 885 B.3 The 6bone 887 B.4 IPv6 Transition: 6to4 889 882 869 885

xvi UNIX Network Programming Contents Appendix C. C.1 C.2 C.3 C.4 C.5 C.6 C.7 Appendix D, D.1 D.2 D.3 Debugging Techniques System Call Tracing 891 Standard Internet Services 893 sock Program 893 Small Test Programs 896 tcpdump Program 896 netstat Program 896 lsof Program 897 Miscellaneous Source Code unp.h Header 899 config.h Header 904 Standard Error Functions 910 891 899 Appendix E. Solutions to Selected Exercises 913 Bibliography Index 947 955