Hands-On Network Programming with C

Book description

A comprehensive guide to programming with network sockets, implementing Internet protocols, designing IoT devices, and much more with C

Key Features

  • Apply your C and C++ programming skills to build powerful network applications
  • Get to grips with a variety of network protocols that allow you to load web pages, send emails, and do much more
  • Write portable network code for Windows, Linux, and macOS

Book Description

Network programming enables processes to communicate with each other over a computer network, but it is a complex task that requires programming with multiple libraries and protocols. With its support for third-party libraries and structured documentation, C is an ideal language to write network programs.

Complete with step-by-step explanations of essential concepts and practical examples, this C network programming book begins with the fundamentals of Internet Protocol, TCP, and UDP. You'll explore client-server and peer-to-peer models for information sharing and connectivity with remote computers. The book will also cover HTTP and HTTPS for communicating between your browser and website, and delve into hostname resolution with DNS, which is crucial to the functioning of the modern web. As you advance, you'll gain insights into asynchronous socket programming and streams, and explore debugging and error handling. Finally, you'll study network monitoring and implement security best practices.

By the end of this book, you'll have experience of working with client-server applications and be able to implement new network programs in C.

The code in this book is compatible with the older C99 version as well as the latest C18 and C++17 standards. You'll work with robust, reliable, and secure code that is portable across operating systems, including Winsock sockets for Windows and POSIX sockets for Linux and macOS.

What you will learn

  • Uncover cross-platform socket programming APIs
  • Implement techniques for supporting IPv4 and IPv6
  • Understand how TCP and UDP connections work over IP
  • Discover how hostname resolution and DNS work
  • Interface with web APIs using HTTP and HTTPS
  • Explore Simple Mail Transfer Protocol (SMTP) for electronic mail transmission
  • Apply network programming to the Internet of Things (IoT)

Who this book is for

If you're a developer or a system administrator who wants to get started with network programming, this book is for you. Basic knowledge of C programming is assumed.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Network Programming with C
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1 - Getting Started with Network Programming
  8. Introducing Networks and Protocols
    1. Technical requirements
    2. The internet and C
    3. OSI layer model
    4. TCP/IP layer model
    5. Data encapsulation
    6. Internet Protocol
      1. What is an address?
    7. Domain names
    8. Internet routing
      1. Local networks and address translation
      2. Subnetting and CIDR
      3. Multicast, broadcast, and anycast
    9. Port numbers
    10. Clients and servers
    11. Putting it together
    12. What's your address?
    13. Listing network adapters from C
      1. Listing network adapters on Windows
      2. Listing network adapters on Linux and macOS
    14. Summary
    15. Questions
  9. Getting to Grips with Socket APIs
    1. Technical requirements
    2. What are sockets?
    3. Socket setup
    4. Two types of sockets
    5. Socket functions
    6. Anatomy of a socket program
      1. TCP program flow
      2. UDP program flow
    7. Berkeley sockets versus Winsock sockets
      1. Header files
      2. Socket data type
      3. Invalid sockets
      4. Closing sockets
      5. Error handling
    8. Our first program
      1. A motivating example
      2. Making it networked
      3. Working with IPv6
      4. Supporting both IPv4 and IPv6
      5. Networking with inetd
    9. Summary
    10. Questions
  10. An In-Depth Overview of TCP Connections
    1. Technical requirements
    2. Multiplexing TCP connections
      1. Polling non-blocking sockets
      2. Forking and multithreading
      3. The select() function
    3. Synchronous multiplexing with select()
      1. select() timeout
      2. Iterating through an fd_set
      3. select() on non-sockets
    4. A TCP client
      1. TCP client code
    5. A TCP server
      1. TCP server code
      2. Building a chat room
    6. Blocking on send()
    7. TCP is a stream protocol
    8. Summary
    9. Questions
  11. Establishing UDP Connections
    1. Technical requirements
    2. How UDP sockets differ
      1. UDP client methods
      2. UDP server methods
    3. A first UDP client/server
      1. A simple UDP server
      2. A simple UDP client
    4. A UDP server
    5. Summary
    6. Questions
  12. Hostname Resolution and DNS
    1. Technical requirements
    2. How hostname resolution works
      1. DNS record types
      2. DNS security
    3. Name/address translation functions
      1. Using getaddrinfo()
      2. Using getnameinfo()
      3. Alternative functions
      4. IP lookup example program
    4. The DNS protocol
      1. DNS message format
      2. DNS message header format
      3. Question format
      4. Answer format
      5. Endianness
      6. A simple DNS query
    5. A DNS query program
      1. Printing a DNS message name
      2. Printing a DNS message
      3. Sending the query
    6. Summary
    7. Questions
    8. Further reading
  13. Section 2 - An Overview of Application Layer Protocols
  14. Building a Simple Web Client
    1. Technical requirements
    2. The HTTP protocol
      1. HTTP request types
      2. HTTP request format
      3. HTTP response format
      4. HTTP response codes
      5. Response body length
    3. What's in a URL
      1. Parsing a URL
    4. Implementing a web client
    5. HTTP POST requests
      1. Encoding form data
      2. File uploads
    6. Summary
    7. Questions
    8. Further reading
  15. Building a Simple Web Server
    1. Technical requirements
    2. The HTTP server
      1. The server architecture
    3. Content types
      1. Returning Content-Type from a filename
    4. Creating the server socket
    5. Multiple connections buffering
      1. get_client()
      2. drop_client()
      3. get_client_address()
      4. wait_on_clients()
      5. send_400()
      6. send_404()
      7. serve_resource()
    6. The main loop
    7. Security and robustness
      1. Open source servers
    8. Summary
    9. Questions
    10. Further reading
  16. Making Your Program Send Email
    1. Technical requirements
    2. Email servers
      1. SMTP security
      2. Finding an email server
    3. SMTP dialog
    4. The format of an email
    5. A simple SMTP client program
    6. Enhanced emails
      1. Email file attachments
    7. Spam-blocking pitfalls
    8. Summary
    9. Questions
    10. Further reading
  17. Section 3 - Understanding Encrypted Protocols and OpenSSL
  18. Loading Secure Web Pages with HTTPS and OpenSSL
    1. Technical requirements
    2. HTTPS overview
    3. Encryption basics
      1. Symmetric ciphers
      2. Asymmetric ciphers
      3. How TLS uses ciphers
    4. The TLS protocol
      1. Certificates
      2. Server name identification
    5. OpenSSL
      1. Encrypted sockets with OpenSSL
      2. Certificates
    6. A simple HTTPS client
    7. Other examples
    8. Summary
    9. Questions
    10. Further reading
  19. Implementing a Secure Web Server
    1. Technical requirements
    2. HTTPS and OpenSSL summary
      1. Certificates
      2. Self-signed certificates with OpenSSL
    3. HTTPS server with OpenSSL
      1. Time server example
      2. A full HTTPS server
    4. HTTPS server challenges
      1. OpenSSL alternatives
      2. Alternatives to TLS
    5. Summary
    6. Questions
    7. Further reading
  20. Establishing SSH Connections with libssh
    1. Technical requirements
    2. The SSH protocol
    3. libssh
      1. Testing out libssh
      2. Establishing a connection
    4. SSH authentication
      1. Server authentication
      2. Client authentication
    5. Executing a remote command
    6. Downloading a file
    7. Summary
    8. Questions
    9. Further reading
  21. Section 4 - Odds and Ends
  22. Network Monitoring and Security
    1. Technical requirements
    2. The purpose of network monitoring
    3. Testing reachability
      1. Checking a route
      2. How traceroute works
      3. Raw sockets
    4. Checking local connections
    5. Snooping on connections
      1. Deep packet inspection
      2. Capturing all network traffic
    6. Network security
      1. Application security and safety
    7. Network-testing etiquette
    8. Summary
    9. Questions
    10. Further reading
  23. Socket Programming Tips and Pitfalls
    1. Technical requirements
    2. Error handling
      1. Obtaining error descriptions
    3. TCP socket tips
      1. Timeout on connect()
      2. TCP flow control and avoiding deadlock
      3. Congestion control
      4. The Nagle algorithm
      5. Delayed acknowledgment
      6. Connection tear-down
      7. The shutdown() function
      8. Preventing address-in-use errors
      9. Sending to a disconnected peer
    4. Socket's local address
    5. Multiplexing with a large number of sockets
    6. Summary
    7. Questions
  24. Web Programming for the Internet of Things
    1. Technical requirements
    2. What is the IoT?
    3. Connectivity options
      1. Wi-Fi
      2. Ethernet
      3. Cellular
      4. Bluetooth
      5. IEEE 802.15.4 WPANs
    4. Hardware choices
      1. Single-board computers
      2. Microcontrollers
      3. FPGAs
    5. External transceivers and modems
    6. IoT protocols
    7. Firmware updates
    8. Ethics of IoT
      1. Privacy and data collection
      2. End-of-life planning
      3. Security
    9. Summary
    10. Questions
  25. Answers to Questions
    1. Chapter 1, Introducing Networks and Protocols
    2. Chapter 2, Getting to Grips with Socket APIs
    3. Chapter 3, An In-Depth Overview of TCP Connections
    4. Chapter 4, Establishing UDP Connections
    5. Chapter 5, Hostname Resolution and DNS
    6. Chapter 6, Building a Simple Web Client
    7. Chapter 7, Building a Simple Web Server
    8. Chapter 8, Making Your Program Send Email
    9. Chapter 9, Loading Secure Web Pages with HTTPS and OpenSSL
    10. Chapter 10, Implementing a Secure Web Server
    11. Chapter 11, Establishing SSH Connections with libssh
    12. Chapter 12, Network Monitoring and Security
    13. Chapter 13, Socket Programming Tips and Pitfalls
    14. Chapter 14, Web Programming for the Internet of Things
  26. Setting Up Your C Compiler on Windows
    1. Installing MinGW GCC
    2. Installing Git
    3. Installing OpenSSL
    4. Installing libssh
    5. Alternatives
  27. Setting Up Your C Compiler on Linux
    1. Installing GCC
    2. Installing Git
    3. Installing OpenSSL
    4. Installing libssh
  28. Setting Up Your C Compiler on macOS
    1. Installing Homebrew and the C compiler
    2. Installing OpenSSL
    3. Installing libssh
  29. Example Programs
    1. Code license
    2. Code included with this book
      1. Chapter 1 – Introducing Networks and Protocols
      2. Chapter 2 – Getting to Grips with Socket APIs
      3. Chapter 3 – An In-Depth Overview of TCP Connections
      4. Chapter 4 – Establishing UDP Connections
      5. Chapter 5 – Hostname Resolution and DNS
      6. Chapter 6 – Building a Simple Web Client
      7. Chapter 7 – Building a Simple Web Server
      8. Chapter 8 – Making Your Program Send Email
      9. Chapter 9 – Loading Secure Web Pages with HTTPS and OpenSSL
      10. Chapter 10 – Implementing a Secure Web Server
      11. Chapter 11 – Establishing SSH Connections with libssh
      12. Chapter 12 – Network Monitoring and Security
      13. Chapter 13 – Socket Programming Tips and Pitfalls
      14. Chapter 14 – Web Programming for the Internet of Things
  30. Other Book You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Network Programming with C
  • Author(s): Lewis Van Winkle
  • Release date: May 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789349863