Book Image

Hands-On Network Programming with C

By : Lewis Van Winkle
Book Image

Hands-On Network Programming with C

By: Lewis Van Winkle

Overview of this book

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.
Table of Contents (26 chapters)
Title Page
Dedication
About Packt
Contributors
Preface
Index

Index

A

  • address / What's your address?
  • ad hoc mode / Wi-Fi
  • American Encryption Standard (AES) / Symmetric ciphers
  • Analog-to-Digital Converter (ADC) / Microcontrollers
  • anycast / Multicast, broadcast, and anycast
  • Apache / Alternatives to TLS
  • Application-Specific Integrated Circuit (ASIC) / FPGAs
  • application layer / OSI layer model
  • application programming interfaces (APIs) / What are sockets?
  • asymmetric ciphers / Asymmetric ciphers
  • asymmetric encryption / Asymmetric ciphers
  • Asynchronous Serial / Single-board computers
  • AT commands / External transceivers and modems

B

  • banner / Establishing a connection
  • Berkeley sockets
    • versus Winsock sockets / Berkeley sockets versus Winsock sockets
    • header files / Header files
    • socket data type / Socket data type
    • invalid sockets / Invalid sockets
    • closing sockets / Closing sockets
    • error handling / Error handling
  • Bluetooth / Bluetooth
  • Bluetooth smart watch connection / Bluetooth
  • broadcast addressing / Multicast, broadcast, and anycast
  • bulk encryption algorithm / How TLS uses ciphers

C

  • Central Processing Unit (CPU) / Single-board computers
  • certificate pinning / Self-signed certificates with OpenSSL
  • certificates / Certificates, Certificates, Certificates
  • CGI standard / Alternatives to TLS
  • chain-of-trust model / Certificates
  • chat room
    • building / Building a chat room
  • cipher / Encryption basics
  • cipher suites / How TLS uses ciphers
  • ciphertext / Encryption basics
  • classless inter-domain routing (CIDR) / What is an address?
  • client-server model / Clients and servers
  • client authentication
    • about / Client authentication
    • no authentication / Client authentication
    • password authentication / Client authentication
    • public key / Client authentication
    • keyboard-interactive / Client authentication
    • Generic Security Service Application Program Interface (GSS-API) / Client authentication
  • congestion-avoidance scheme / Congestion control
  • congestion control methods / Congestion control
  • connectivity options, IoT
    • about / Connectivity options
    • Wi-Fi / Wi-Fi
    • Ethernet / Ethernet
    • cellular / Cellular
    • Bluetooth / Bluetooth
    • IEEE 802.15.4 WPANs / IEEE 802.15.4 WPANs
  • Constrained Application Protocol (CoAP) / IoT protocols
  • Content-Type
    • returning, from filename / Returning Content-Type from a filename
  • content types / Content types
  • C programming language
    • about / The internet and C
    • network adapters, listing / Listing network adapters from C

D

  • data encapsulation / Data encapsulation
  • Data Encryption Standard (DES) / Symmetric ciphers
  • datagram / OSI layer model
  • Datagram Transport Layer Security (DTLS) / A simple HTTPS client
  • data link layer / OSI layer model
  • deadlock
    • avoiding / TCP flow control and avoiding deadlock
  • decryption / Encryption basics
  • deep packet inspection / Deep packet inspection
  • defense in depth approach / Application security and safety
  • Diffie-Hellman key exchange algorithm / Symmetric ciphers
  • Digital-to-Analog Converter (DAC) / Microcontrollers
  • Digital Rights Management (DRM) / Firmware updates
  • DNS message
    • printing / Printing a DNS message
  • DNS message name
    • printing / Printing a DNS message name
  • DNS over HTTPS (DoH) / DNS security
  • DNS protocol
    • about / The DNS protocol
    • message format / DNS message format
    • message header format / DNS message header format
    • question format / Question format
    • answer format / Answer format
    • endianness / Endianness
  • DNS query
    • about / A simple DNS query
    • sending / Sending the query
  • DNS query program / A DNS query program
  • DNS record types / DNS record types
  • DNS security / DNS security
  • DNS transport over TCP / The DNS protocol
  • Domain-based Message Authentication, Reporting, and Conformance (DMARC) / Spam-blocking pitfalls
  • DomainKeys Identified Mail (DKIM) / Spam-blocking pitfalls
  • domain name / Domain names
  • Domain Name System (DNS) / Domain names
  • Domain Name System Security Extensions (DNSSEC) / DNS security
  • domain validation / Certificates
  • dual-stack configuration / Internet Protocol

E

  • echo reply / Testing reachability
  • Echo Reply message / How traceroute works
  • echo request / Testing reachability
  • EEPROM / Microcontrollers
  • elliptic-curve cryptography (ECC) / Asymmetric ciphers
  • email
    • format / The format of an email
  • email file attachments / Email file attachments
  • email servers
    • about / Email servers
    • finding / Finding an email server
  • encrypted server name identification (ESNI) / Server name identification
  • encryption
    • about / Encryption basics
    • basics / Encryption basics
    • symmetric ciphers / Symmetric ciphers
    • asymmetric ciphers / Asymmetric ciphers
    • Transport Layer Security (TLS) / How TLS uses ciphers
  • endianness / Endianness
  • enhanced emails / Enhanced emails
  • ephemeral port / Putting it together
  • ephemeral port number / A simple UDP client
  • error descriptions
    • obtaining / Obtaining error descriptions
  • error handling / Error handling
  • ESP8266 microcontroller / Microcontrollers
  • Ethernet / Ethernet
  • Extended Validation (EV) certificates / Certificates
  • external transceivers / External transceivers and modems

F

  • FastCGI standard / Alternatives to TLS
  • Field-Programmable Gate Arrays (FPGAs) / FPGAs
  • file
    • downloading / Downloading a file
  • File Transfer Protocol (FTP) / Local networks and address translation
  • firewalls / Network security
  • firmware updates / Firmware updates
  • FLASH / Microcontrollers
  • frame / OSI layer model

G

  • General-Purpose Input/Output (GPIO) / Single-board computers

H

  • hardware choices, IoT
    • about / Hardware choices
    • Single-Board Computers (SBCs) / Single-board computers
    • microcontroller / Microcontrollers
    • Field-Programmable Gate Arrays (FPGAs) / FPGAs
  • Hardware Description Languages (HDL) / FPGAs
  • Hayes command set / External transceivers and modems
  • hostname resolution
    • working / How hostname resolution works
  • HTTP POST requests
    • about / HTTP POST requests
    • form data, encoding / Encoding form data
    • file uploads / File uploads
  • HTTP protocol
    • about / The HTTP protocol, HTTPS and OpenSSL summary
    • response body length / Response body length
  • HTTP request format / HTTP request format
  • HTTP request types
    • GET / HTTP request types
    • HEAD / HTTP request types
    • POST / HTTP request types
    • PUT / HTTP request types
    • DELETE / HTTP request types
    • TRACE / HTTP request types
    • CONNECT / HTTP request types
    • OPTIONS / HTTP request types
  • HTTP response codes / HTTP response codes
  • HTTP response format / HTTP response format
  • HTTPS client / A simple HTTPS client
  • HTTP server
    • about / The HTTP server
    • architecture / The server architecture
  • HTTPS server
    • about / A full HTTPS server
    • challenges / HTTPS server challenges
  • HTTPS server, with OpenSSL
    • about / HTTPS server with OpenSSL
    • time server example / Time server example
  • HTTP web client
    • implementing / Implementing a web client
  • Hypertext Markup Language (HTML) / HTTPS overview
  • Hypertext Transfer Protocol Secure (HTTPS)
    • overview / HTTPS overview
    • authentication / HTTPS overview
    • encryption / HTTPS overview
    • integrity / HTTPS overview
    • about / HTTPS and OpenSSL summary

I

  • IEEE 802.15.4 WPANs / IEEE 802.15.4 WPANs
  • Industry 4.0 / What is the IoT?
  • inetd / Networking with inetd
  • infrastructure mode / Wi-Fi
  • Inter-Integrated Circuit (I2C) / Single-board computers
  • inter-process communication (IPC) / What are sockets?
  • International Data Encryption Algorithm (IDEA) / Symmetric ciphers
  • internet / The internet and C
  • Internet Assigned Numbers Authority (IANA) / Port numbers, Content types
  • Internet Control Message Protocol (ICMP) / Internet routing, Testing reachability
  • Internet Engineering Task Force (IETF) / Internet Protocol
  • Internet Message Access Protocol (IMAP) / Email servers
  • Internet of Things (IoT)
    • about / The internet and C, What is the IoT?
    • hardware choices / Hardware choices
  • Internet Protocol
    • about / Internet Protocol
    • address / What is an address?
    • local networks and address translation / Local networks and address translation
    • subnetting / Subnetting and CIDR
    • CIDR / Subnetting and CIDR
  • internet routing / Internet routing
  • Internet Service Providers (ISPs) / Internet Protocol, How hostname resolution works, SMTP security, Wi-Fi
  • IoT, ethics
    • about / Ethics of IoT
    • privacy / Privacy and data collection
    • data collection / Privacy and data collection
    • end-of-life planning / End-of-life planning
    • security / Security
  • IoT protocols / IoT protocols
  • ipconfig command / What's your address?
  • IP lookup
    • example program / IP lookup example program
  • IPv4 / Internet Protocol, Supporting both IPv4 and IPv6
  • IPv4 addresses / What is an address?
  • IPv6
    • about / Internet Protocol, Supporting both IPv4 and IPv6
    • working with / Working with IPv6
  • IPv6 addresses / What is an address?

K

  • key exchange algorithm / How TLS uses ciphers

L

  • Let's Encrypt / Certificates
  • libssh
    • about / libssh
    • testing / Testing out libssh
    • connection, establishing / Establishing a connection
  • link-local addresses / What is an address?
  • Local Area Networks (LANs) / Local networks and address translation
  • local connection
    • checking / Checking local connections
    • snooping / Snooping on connections
    • deep packet inspection / Deep packet inspection
    • network traffic, capturing / Capturing all network traffic
  • local port / Putting it together, A simple UDP server, A simple UDP client, Socket's local address
  • loopback address / What is an address?
  • low-rate wireless personal area networks (LR-WPAN) / IEEE 802.15.4 WPANs

M

  • mail submission / Email servers
  • mail transmission / Email servers
  • man-in-the-middle attack / Symmetric ciphers
  • Maximum Segment Size (MSS) / The Nagle algorithm
  • mesh network topology / IEEE 802.15.4 WPANs
  • message authentication algorithm (MAC) / How TLS uses ciphers
  • Message Authentication Code (MAC) / Security
  • Message Queuing Telemetry Transport (MQTT) / IoT protocols
  • microcontroller / Microcontrollers
  • microservices / A TCP server
  • MIME type / Content types
  • modem / External transceivers and modems
  • multicast / Multicast, broadcast, and anycast
  • multiple connections buffering
    • about / Multiple connections buffering
    • get_client() function / get_client()
    • drop_client() function / drop_client()
    • get_client_address() function / get_client_address()
    • wait_on_clients() function / wait_on_clients()
    • send_400() function / send_400()
    • send_404() function / send_404()
    • serve_resource() function / serve_resource()
  • Multipurpose Internet Mail Extensions (MIME) / Enhanced emails

N

  • Nagle algorithm / The Nagle algorithm
  • name/address translation functions
    • about / Name/address translation functions
    • getaddrinfo(), using / Using getaddrinfo()
    • getnameinfo(), using / Using getnameinfo()
  • Netstat / Checking local connections
  • network adapters
    • listing, from C programming language / Listing network adapters from C
    • listing, on Windows / Listing network adapters on Windows
    • listing, on Linux / Listing network adapters on Linux and macOS
    • listing, on macOS / Listing network adapters on Linux and macOS
  • Network Address Translation (NAT) / Local networks and address translation
  • network address translation (NAT) / A simple UDP server
  • networked code
    • security / Security and robustness
    • robustness / Security and robustness
  • networked program
    • about / Our first program
    • motivating example / A motivating example
    • creating / Making it networked
  • networking
    • with inetd / Networking with inetd
  • network layer / OSI layer model
  • network monitoring
    • purpose / The purpose of network monitoring
  • network security
    • about / Network security
    • application security / Application security and safety
    • application safety / Application security and safety
  • network testing
    • etiquette / Network-testing etiquette
  • network traffic
    • capturing / Capturing all network traffic
  • Nginx / Alternatives to TLS

O

  • open source servers / Open source servers
  • OpenSSL
    • about / OpenSSL, HTTPS and OpenSSL summary
    • encrypted sockets / Encrypted sockets with OpenSSL
    • certificates / Certificates
    • self-signed certificates / Self-signed certificates with OpenSSL
    • HTTPS server / HTTPS server with OpenSSL
    • alternatives / OpenSSL alternatives
  • Open Systems Interconnection model (OSI model) / OSI layer model
  • Open Web Application Security Project (OWASP) / Security
  • OSI layer model
    • about / OSI layer model
    • physical layer / OSI layer model
    • data link layer / OSI layer model
    • network layer / OSI layer model
    • transport layer / OSI layer model
    • session layer / OSI layer model
    • presentation layer / OSI layer model
    • application layer / OSI layer model

P

  • packet / OSI layer model
  • percent encoding / Encoding form data
  • physical layer / OSI layer model
  • Ping utility / The purpose of network monitoring
  • plaintext / Encryption basics
  • polling / Polling non-blocking sockets
  • Portable Operating System Interface (POSIX) / What are sockets?
  • port numbers / Port numbers
  • postcards / Two types of sockets
  • Post Office Protocol (POP) / Email servers
  • Pre-Shared Key (PSK) / Security
  • presentation layer / OSI layer model
  • Private Key / Asymmetric ciphers
  • protocols / OSI layer model
  • public-key authentication / The SSH protocol
  • public-key encryption / Asymmetric ciphers
  • Public Key / Asymmetric ciphers
  • Pulse Width Modulation (PWM) / Single-board computers, Microcontrollers

R

  • Random Access Memory (RAM) / Single-board computers
  • Raspberry Pi Zero W / Single-board computers
  • raw sockets / Raw sockets
  • reachability
    • testing / Testing reachability
    • route, checking / Checking a route
    • traceroute, working / How traceroute works
    • raw sockets / Raw sockets
  • remote command
    • executing / Executing a remote command
  • request line / HTTP request format, The HTTP server
  • Rijndael / Symmetric ciphers
  • Rivest-Shamir-Adleman (RSA) / Asymmetric ciphers

S

  • Secure Copy Protocol (SCP) / Downloading a file
  • Secure Hypertext Internet of Things (SHIoT) / Security
  • Secure Shell fingerprint (SSHFP) / SSH authentication
  • Secure Socket Layer (SSL) / HTTPS overview
  • segment / OSI layer model
  • self-signed certificates
    • with OpenSSL / Self-signed certificates with OpenSSL
  • send() method
    • blocking behavior on / Blocking on send()
  • Sender Policy Framework (SPF) / DNS record types, Spam-blocking pitfalls
  • Serial Peripheral Interface (SPI) / Single-board computers
  • server authentication / Certificates, Server authentication
  • server name identification (SNI) / Server name identification
  • server socket
    • creating / Creating the server socket
  • session channel type / Executing a remote command
  • session layer / OSI layer model
  • Single-Board Computers (SBCs) / Single-board computers
  • site-local addresses / What is an address?
  • SMTP
    • about / Email servers
    • security / SMTP security
  • SMTP client program / A simple SMTP client program
  • SMTP dialog / SMTP dialog
  • SMTP relay / Email servers
  • socket
    • implementing / Putting it together
    • about / What are sockets?
    • setting up / Socket setup
    • connection-oriented / Two types of sockets
    • connectionless / Two types of sockets
    • types / Two types of sockets
    • local address / Socket's local address
    • multiplexing with / Multiplexing with a large number of sockets
  • socket functions
    • socket() / Socket functions
    • bind() / Socket functions
    • listen() / Socket functions
    • connect() / Socket functions
    • accept() / Socket functions
    • send() / Socket functions
    • recv() / Socket functions
    • sendto() / Socket functions
    • recvfrom() / Socket functions
    • close() / Socket functions
    • shutdown() / Socket functions
    • select() / Socket functions
    • getnameinfo() / Socket functions
    • getaddrinfo() / Socket functions
    • setsockopt() / Socket functions
    • fcntl() / Socket functions
    • ioctlsocket() / Socket functions
  • socket program
    • anatomy / Anatomy of a socket program
    • TCP program flow / TCP program flow
    • UDP program flow / UDP program flow
  • spam-blocking
    • pitfalls / Spam-blocking pitfalls
  • SSH authentication
    • about / SSH authentication
    • server authentication / Server authentication
    • client authentication / Client authentication
  • SSH protocol / The SSH protocol
  • symmetric cipher / Symmetric ciphers
  • synchronous multiplexing, with select()
    • about / Synchronous multiplexing with select()
    • select() timeout / select() timeout
    • iteration, through fd_set / Iterating through an fd_set
    • select(), on non-sockets / select() on non-sockets

T

  • tab feature / Multiplexing TCP connections
  • TCP/IP layer model
    • about / TCP/IP layer model
    • Network Access layer / TCP/IP layer model
    • Internet layer / TCP/IP layer model
    • Host-to-Host layer / TCP/IP layer model
    • Process/Application layer / TCP/IP layer model
  • TCP/IP protocol suite / TCP/IP layer model
  • TCP client
    • about / A TCP client
    • code / TCP client code
  • TCP connection
    • setup phase / TCP socket tips
    • data-transfer phase / TCP socket tips
    • tear-down phase / TCP socket tips
  • TCP connections, multiplexing
    • about / Multiplexing TCP connections
    • non-blocking sockets, polling / Polling non-blocking sockets
    • forking / Forking and multithreading
    • multithreading / Forking and multithreading
    • select() function / The select() function
  • TCP fallback / The DNS protocol
  • TCP flow control / TCP flow control and avoiding deadlock
  • TCP four-way closing handshake / Connection tear-down
  • TCP program flow / TCP program flow
  • TCP server
    • about / A TCP server
    • code / TCP server code
  • TCP slow start algorithm / Congestion control
  • TCP socket, tips
    • about / TCP socket tips
    • timeout, on connect() / Timeout on connect()
    • TCP flow control / TCP flow control and avoiding deadlock
    • deadlock, avoiding / TCP flow control and avoiding deadlock
    • congestion control / Congestion control
    • Nagle algorithm / The Nagle algorithm
    • delayed acknowledgment / Delayed acknowledgment
    • connection tear-down / Connection tear-down
    • shutdown() function / The shutdown() function
    • address-in-use errors, preventing / Preventing address-in-use errors
    • message, sending to disconnected peer / Sending to a disconnected peer
  • TCP stream protocol / TCP is a stream protocol
  • Telnet / The SSH protocol
  • time-to-live (TTL) / How hostname resolution works, How traceroute works
  • Time Exceeded message / How traceroute works
  • Tiny Encryption Algorithm (TEA) / Symmetric ciphers
  • TLS protocol
    • about / The TLS protocol
    • certificates / Certificates
    • server name identification (SNI) / Server name identification
    • alternatives / Alternatives to TLS
  • TLS termination proxy / Alternatives to TLS
  • tooling options
    • tcpdump / Snooping on connections
    • Wireshark / Snooping on connections
  • traceroute
    • working / How traceroute works
  • tracert / Checking a route
  • tracker / Anatomy of a socket program
  • Transmission Control Protocol (TCP) / OSI layer model, Two types of sockets, The HTTP server, The SSH protocol, TCP socket tips
  • transport layer / OSI layer model
  • Transport Layer Security (TLS)
    • ciphers, used / How TLS uses ciphers
  • Tshark
    • reference / Snooping on connections

U

  • UDP client
    • methods / UDP client methods
    • about / A first UDP client/server, A simple UDP client
  • UDP program flow / UDP program flow
  • UDP server
    • methods / UDP server methods
    • about / A first UDP client/server, A simple UDP server, A UDP server
  • UDP sockets / How UDP sockets differ
  • unicast addressing / Multicast, broadcast, and anycast
  • Uniform Resource Locators (URL)
    • about / What's in a URL
    • parsing / Parsing a URL
  • URL encoding / Encoding form data
  • User Datagram Protocol (UDP) / OSI layer model, Two types of sockets, A simple HTTPS client

V

  • Verilog / FPGAs
  • VHSIC Hardware Description Language (VHDL) / FPGAs
  • Virtual Private Network (VPN) / Cellular
  • Virtual Private Servers (VPS) / Establishing a connection

W

  • Watch-Dog Timers (WDT) / Microcontrollers
  • web server
    • main loop / The main loop
  • Wi-Fi / Wi-Fi
  • Winsock / Listing network adapters on Windows, What are sockets?
  • Winsock sockets
    • versus Berkeley sockets / Berkeley sockets versus Winsock sockets
  • Wireless Access Point (WAP) / Wi-Fi
  • Wireless Local Area Network (WLAN) / Wi-Fi
  • Wireless Personal Area Networks (WPAN) / Bluetooth
  • Wireshark / Deep packet inspection
  • wolfSSL / Security