You are previewing Linux Socket Programming by Example.
O'Reilly logo
Linux Socket Programming by Example

Book Description

Linux Socket Programming by Example begins with a very basic introduction to the fundamentals of socket level programming. As the chapters progress, you are introduced to related concepts, such as forming network addresses, Ipv6, the TCP/IP protocol suite and options, writing servers, and creating secure applications. You will also learn about socket fundamentals, domains and addresses, address conversion functions, socket types and protocols, Internet sockets, types and protocols, binding an address to a socket, using Datagram oriented protocols, and much more.

Table of Contents

  1. Copyright
    1. Dedication
  2. Preface>
    1. About the Author
    2. Acknowledgements
    3. Tell Us What You Think!
    4. Introduction
    5. The by Example Series
    6. Who Should Use This Book
    7. Conventions Used in This Book
    8. Where to Find the Code
    9. What's Next
  3. I. Basic Socket Concepts
    1. 1. Introducing Sockets
      1. A Brief Historical Introduction
      2. Understanding Sockets
        1. Defining a Socket
        2. Using Sockets
        3. Referencing Sockets
      3. Comparing Sockets to Pipes
      4. Creating Sockets
        1. Using socketpair(2) in an Example
        2. Running the Demonstration Program
      5. Performing I/O on Sockets
      6. Closing Sockets
        1. The shutdown(2) Function
        2. Shutting Down Writing to a Socket
        3. Dealing with Duplicated Sockets
        4. Shutting Down Reading from a Socket
        5. Knowing When Not to Use shutdown(2)
      7. Writing a Client/Server Example
        1. What's Next
    2. 2. Domains and Address Families
      1. Nameless Sockets
        1. Anonymous Calls
        2. Generating Addresses
      2. Understanding Domains
      3. Forming Socket Addresses
        1. Examining the Generic Socket Address
      4. Forming Local Addresses
        1. Forming Traditional Local Addresses
        2. Forming Abstract Local Addresses
      5. Forming Internet (IPv4) Socket Addresses
        1. Understanding Network Byte Order
        2. Performing Endian Conversions
        3. Initializing a Wild Internet Address
        4. Initializing a Specific Internet Address
      6. Specifying an X.25 Address
      7. Specifying Other Address Families
      8. The AF_UNSPEC Address Family
        1. What's Next
    3. 3. Address Conversion Functions
      1. Internet IP Numbers
        1. Internet Address Classes
        2. Understanding Netmask Values
      2. Allocating IP Addresses
        1. Private IP Numbers
        2. Reserved IP Numbers
      3. Manipulating IP Numbers
        1. Using the inet_addr(3) Function
        2. The inet_aton(3) Function
        3. Using the inet_ntoa(3) Function
        4. Using inet_network(3)
        5. Using the inet_lnaof(3) Function
        6. Using the inet_netof(3) Function
        7. Using the inet_makeaddr(3) Function
        8. What's Next
    4. 4. Socket Types and Protocols
      1. Specifying the Domain of a Socket
        1. Choosing PF_INET or AF_INET
        2. Using the PF_LOCAL and AF_LOCAL Macros
      2. Using the socket(2) Function
      3. Choosing a Socket Type
        1. Understanding the SOCK_STREAM Socket Type
        2. Understanding the SOCK_DGRAM Socket Type
        3. Understanding the SOCK_SEQPACKET Socket Type
      4. Choosing a Protocol
        1. Using PF_LOCAL and SOCK_STREAM
        2. Using PF_LOCAL and SOCK_DGRAM
        3. Using PF_INET and SOCK_STREAM
        4. Using PF_INET and SOCK_DGRAM
      5. Socket Domain and Type Summary
      6. Other Linux-Supported Protocols
        1. Researching Other Protocols
          1. Discovering Protocol Families
          2. Discovering Socket Types
        2. What's Next
    5. 5. Binding Addresses to a Socket
      1. The Purpose of the bind(2) Function
      2. Using the bind(2) Function
      3. Obtaining the Socket Address
        1. Writing a sock_addr() Function
        2. Obtaining a Peer Socket Address
      4. Interfaces and Addressing
        1. Specifying an Interface Address Example
        2. Binding a Specific Interface Address
        3. Binding for Any Interface
        4. What's Next
    6. 6. Connectionless-Oriented Protocols
      1. The Methods of Communication
        1. Understanding the Advantages
        2. Understanding the Disadvantages of Connectionless Communications
      2. Performing Input/Output of Datagrams
        1. Introducing the sendto(2) Function
        2. Introducing the recvfrom(2) Function
      3. Writing a UDP Datagram Server
      4. Writing a UDP Datagram Client
      5. Testing the Datagram Client and Server
        1. Testing with No Server
        2. Testing with Other IP Numbers
        3. Leaving Out bind(2) in Client Programs
        4. Replying to a Wild Address
        5. What's Next
    7. 7. Connection-Oriented Protocols for Clients
      1. Reviewing the Methods of Communication
        1. TCP/IP Handles Lost Packets
        2. TCP/IP Handles Duplicated Packets
        3. TCP/IP Handles Sequencing
        4. TCP/IP Handles Flow Control
        5. Understanding the Advantages of TCP/IP
      2. Internet Services
        1. Examining the /etc/services File
        2. Using Function getservent(3)
        3. Using the setservent(3) Function
        4. Using the endservent(3) Function
        5. Looking Up a Service by Name and Protocol
        6. Looking Up a Service by Port and Protocol
      3. Consulting the /etc/protocols File
        1. Using the setprotoent(3) Function
        2. Using the endprotoent(3) Function
        3. Looking Up a Protocol by Name
        4. Looking Up a Protocol by Number
      4. Writing a TCP/IP Client Program
        1. Introducing the connect(2) Function
        2. Preparing to Write the Client Program
        3. The daytime Client Program
      5. Using connect(2) on SOCK_DGRAM Sockets
      6. What's Next
    8. 8. Connection-Oriented Protocols for Servers
      1. Understanding the Role of the Server
      2. The listen(2) Function
        1. Understanding the Connect Queue
        2. Specifying a Value for backlog
      3. The accept(2) Function Call
        1. Understanding the Role of accept(2)
      4. Writing a TCP/IP Server
        1. Running a Wild Server
      5. Modifying the Client Program
        1. What's Next
    9. 9. Hostname and Network Name Lookups
      1. Understanding the Need for Names
      2. Using the uname(2) Function
      3. Obtaining Hostnames and Domain Names
        1. Using Function gethostname(2)
        2. Using the getdomainname(2) Function
        3. Testing gethostname(2) and getdomainname(2)
      4. Resolving Remote Addresses
        1. Error Reporting
        2. Reporting an h_errno Error
          1. Understanding the Error Codes
        3. Using the gethostbyname(3) Function
          1. The hostent h_name Member
          2. The hostent h_aliases Member
          3. The hostent h_addrtype Member
          4. The hostent h_length Member
          5. The hostent h_addr_list Member
        4. Applying the gethostbyname(3) Function
        5. The gethostbyaddr(3) Function
        6. Using the sethostent(3) Function
        7. Using the endhostent(3) Function
        8. What's Next
  4. II. Advanced Socket Programming
    1. 10. Using Standard I/O on Sockets
      1. Understanding the Need for Standard I/O
      2. Associating a Socket with a Stream
        1. Using fdopen(3) to Associate a Socket with a Stream
      3. Closing a Socket Stream
      4. Using Separate Read and Write Streams
        1. Duplicating a Socket
        2. Closing the Dual Streams
      5. Winding Up Communications
        1. Shutting Down the Write Side Only
        2. Shutting Down the Read Side Only
        3. Shutting Down Both Read and Write Sides
      6. Handling Interrupts
        1. Handling EINTR for Other Functions
      7. Defining Buffer Operation
      8. Applying FILE Streams to Sockets
        1. Presenting the mkaddr() Function
        2. The RPN Calculator Engine Code
        3. Trying Out the RPN Server
      9. What's Next
    2. 11. Concurrent Client Servers
      1. Understanding the Multiple-Client Problem
      2. Overview of Server Functions
      3. Using fork(2) to Service Multiple Clients
        1. Understanding the Overall Server Process
        2. Understanding the Child Server Process Flow
        3. Understanding Process Termination Processing
      4. Designing Servers That Use select(2)
        1. Introducing the select(2) Function
          1. The timeval Structure
        2. Manipulating File Descriptor Sets
          1. Using the FD_ZERO Macro
          2. Using the FD_SET Macro
          3. Using the FD_CLR Macro
          4. Testing File Descriptors with FD_ISSET Macro
      5. Applying select(2) to a Server
        1. Testing the select(2)-Based Server
        2. Limitations of the Example
      6. What's Next
    3. 12. Socket Options
      1. Getting Socket Options
        1. Applying getsockopt(2)
      2. Setting Socket Options
        1. Applying the setsockopt(2) Function
      3. Retrieving the Socket Type (SO_TYPE)
      4. Setting the SO_REUSEADDR Option
      5. Setting the SO_LINGER Option
      6. Setting the SO_KEEPALIVE Option
      7. Setting the SO_BROADCAST Option
      8. Setting the SO_OOBINLINE Option
      9. Options SO_PASSCRED and SO_PEERCRED
      10. What's Next
    4. 13. Broadcasting with UDP
      1. Understanding Broadcast Addresses
        1. Broadcasting on 255.255.255.255
        2. Enhancing the mkaddr.c Subroutine
      2. Broadcasting from a Server
      3. Receiving Broadcasts
      4. Demonstrating the Broadcasts
      5. Broadcasting to a Network
        1. Starting Broadcasts
        2. Receiving Broadcasts
        3. Receiving Broadcasts from a Remote Host
        4. Troubleshooting Hints
      6. What's Next
    5. 14. Out-of-Band Data
      1. Defining Out-of-Band
      2. Understanding the Need for Out-of-Band Data
      3. Sockets and Out-of-Band Data
      4. Variations in Implementation
      5. Using Out-of-Band Data
        1. Writing Out-of-Band Data
        2. Reading Out-of-Band Data
        3. Understanding the Signal SIGURG
        4. Supporting Subprograms
        5. Receiving with the SIGURG Signal
        6. Sending Out-of-Band Data
        7. Testing the oobrecv and oobsend Programs
      6. Understanding the Urgent Pointer
        1. Understanding TCP Urgent Mode
        2. Urgent Mode When tcp_stdurg=1
      7. Receiving Out-of-Band Data Inline
        1. Determining the Urgent Pointer
        2. Using Out-of-Band Data Inline
      8. Limitations of the Urgent Mode Pointer
        1. Processing Out-of-Band Data with select(2)
      9. What's Next
    6. 15. Using the inetd Daemon
      1. Steps Common to Most Servers
      2. Introducing inetd
        1. The /etc/inetd.conf Configuration File
          1. Internet Service Name Field
          2. The Socket Type Field
          3. The Protocol Field
          4. The Flags Field
          5. The UserID Field
          6. The Pathname Field
          7. The Server Arguments Field
        2. The Design Parameters of inetd Servers
      3. Implementing a Simple stream tcp Server
        1. Configuring /etc/inetd.conf to Invoke a New Server
          1. Establishing the Executable
          2. Establishing the Service
          3. Alerting inetd to Configuration Changes
          4. Testing the New Service
        2. Disabling the New Service
      4. Datagram Servers with inetd
        1. Understanding wait and nowait
      5. What's Next
    7. 16. Network Security Programming
      1. Defining Security
      2. The Challenges of Security
      3. Identifying Friend or Foe
        1. Securing by Hostname or Domain Name
        2. Identifying by IP Number
      4. Securing inetd Servers
        1. Centralized Network Policy
        2. Understanding the TCP Wrapper Concept
        3. Determining Access
          1. Determining the Service
          2. Determining the Client Identity
          3. Determining the Datagram Client Identity
      5. Installing Wrapper and Server Programs
        1. Examining Server and Wrapper Logging Code
        2. Examining the Datagram Server Code
        3. Examining the Simple TCP Wrapper Program
      6. Introducing the Client Program
      7. Installing and Testing the Wrapper
        1. Monitoring the Log Files
        2. Starting Your inetd Daemon
        3. Testing the Wrapper Program
          1. Denying a Request
        4. Testing the Server Timeout
        5. Uninstalling the Demonstration Programs
        6. Datagram Vulnerability
      8. What's Next
    8. 17. Passing Credentials and File Descriptors
      1. Problem Statement
      2. Introducing Ancillary Data
      3. Introducing I/O Vectors
        1. The I/O Vector (struct iovec)
        2. The readv(2) and writev(2) Functions
          1. Example Using writev(2)
      4. The sendmsg(2) and recvmsg(2) Functions
        1. The sendmsg(2) Function
        2. The recvmsg(2) Function
        3. Understanding struct msghdr
          1. Members msg_name and msg_namelen
          2. Members msg_iov and msg_iovlen
          3. Members msg_control and msg_controllen
          4. Member msg_flags
      5. Ancillary Data Structures and Macros
        1. Introducing the struct cmsghdr Structure
        2. Introducing the cmsg(3) Macros
          1. The CMSG_LEN() Macro
          2. The CMSG_SPACE() Macro
          3. The CMSG_DATA() Macro
          4. The CMSG_ALIGN() Macro
          5. The CMSG_FIRSTHDR() Macro
          6. The CMSG_NXTHDR() Macro
        3. Iterating Through Ancillary Data
        4. Creating Ancillary Data
      6. Presenting an Ancillary Data Example
        1. The Common Header File common.h
        2. The misc.c Module
        3. The recvcred.c Module
        4. The Simple Web Server web80
        5. The reqport() Function
        6. The recv_fd() Function
        7. The sockserv Server Program
        8. The send_fd() Function
      7. Testing the Socket Server
        1. Testing sockserv
      8. What's Next
    9. 18. A Practical Network Project
      1. Problem Statement
      2. Solving the Quote Service Problem
        1. Obtaining Stock Market Quotes
      3. Examining the Quote Server Program
      4. Fetching Quotations via get_tickinfo()
      5. Broadcasting Quotes via broadcast()
      6. Examining the Client Program
      7. Compiling and Running the Demonstration
        1. Starting the qserve Quotation Server
        2. Starting the mktwatch Client
        3. If the finance.yahoo.com Service Changes
      8. What's Next
  5. Appendixes
    1. A. Socket Function Quick Reference
      1. Socket-Specific Functions
        1. socketpair(2)
        2. socket(2)
        3. bind(2)
        4. connect(2)
        5. listen(2)
        6. accept(2)
      2. Socket Addressing
        1. getsockname(2)
        2. getpeername(2)
      3. Reading of Sockets
        1. read(2)
        2. readv(2)
        3. recv(2)
        4. recvfrom(2)
        5. recvmsg(2)
      4. Writing to Sockets
        1. write(2)
        2. writev(2)
        3. send(2)
        4. sendto(2)
        5. sendmsg(2)
      5. Other Socket I/O
        1. select(2)
        2. cmsg(3)
      6. Controlling Sockets
        1. shutdown(2)
        2. getsockopt(2)
        3. setsockopt(2)
        4. dup(2) and dup2(2)
        5. fcntl(2) Using F_SETOWN
        6. ioctl(2) to Test for Mark
      7. Network Support Functions
        1. byteorder(3)
        2. inet_addr(3)
        3. inet_aton(3)
        4. inet_ntoa(3)
        5. inet_network(3)
        6. inet_lnaof(3)
        7. inet_netof(3)
        8. inet_makeaddr(3)
        9. getservent(3)
        10. getprotoent(3)
      8. Standard I/O Support
      9. Hostname Support
        1. uname(2)
        2. gethostname(2)
        3. getdomainname(2)
        4. gethostbyname(3)
        5. sethostent(3)
    2. B. Socket-Related Structures Reference
      1. Socket Address Structures
        1. sockaddr
        2. sockaddr_un
        3. sockaddr_in and in_addr
          1. Special IPv4 Addresses
        4. sockaddr_x25 and x25_address
        5. sockaddr_in6 and in6_addr
        6. sockaddr_atalk and at_addr
        7. full_sockaddr_ax25, sockaddr_ax25, and ax25_address
      2. Miscellaneous Structures
        1. servent
        2. protoent
        3. utsname
        4. hostent
        5. timeval
        6. linger
      3. I/O-Related Structures
        1. iovec
        2. msghdr
        3. cmsghdr
    3. C. Useful Network Tables
    4. glossary