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 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 be ready to overcome challenges in network programming with the powerful C language.
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.
=====
Lewis Van Winkle is a software programming consultant, entrepreneur, and founder of a successful IoT company. He has over 20 years of programming experience after publishing his first successful software product at age 12. He has over 15 years of programming experience with the C programming language on a variety of operating systems and platforms. He is active in the open-source community and has published several popular open-source programs and libraries-many of them in C. Today, Lewis spends much of his time consulting, where he loves taking on difficult projects that other programmers have given up on. He specializes in network systems, financial systems, machine learning, and interoperation between different programming languages.
Author(s): Lewis Van Winkle
Edition: 1
Publisher: Packt
Year: 2020
Language: English
Commentary: c c18 c99 c11 tcp ip networking [ true pdf ]
Pages: 478
Tags: c c18 c99 c11 tcp ip networking
Cover
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Table of Contents
Preface
Section 1 - Getting Started with Network Programming
Chapter 1: Introducing Networks and Protocols
Technical requirements
The internet and C
OSI layer model
TCP/IP layer model
Data encapsulation
Internet Protocol
What is an address?
Domain names
Internet routing
Local networks and address translation
Subnetting and CIDR
Multicast, broadcast, and anycast
Port numbers
Clients and servers
Putting it together
What's your address?
Listing network adapters from C
Listing network adapters on Windows
Listing network adapters on Linux and macOS
Summary
Questions
Chapter 2: Getting to Grips with Socket APIs
Technical requirements
What are sockets?
Socket setup
Two types of sockets
Socket functions
Anatomy of a socket program
TCP program flow
UDP program flow
Berkeley sockets versus Winsock sockets
Header files
Socket data type
Invalid sockets
Closing sockets
Error handling
Our first program
A motivating example
Making it networked
Working with IPv6
Supporting both IPv4 and IPv6
Networking with inetd
Summary
Questions
Chapter 3: An In-Depth Overview of TCP Connections
Technical requirements
Multiplexing TCP connections
Polling non-blocking sockets
Forking and multithreading
The select() function
Synchronous multiplexing with select()
select() timeout
Iterating through an fd_set
select() on non-sockets
A TCP client
TCP client code
A TCP server
TCP server code
Building a chat room
Blocking on send()
TCP is a stream protocol
Summary
Questions
Chapter 4: Establishing UDP Connections
Technical requirements
How UDP sockets differ
UDP client methods
UDP server methods
A first UDP client/server
A simple UDP server
A simple UDP client
A UDP server
Summary
Questions
Chapter 5: Hostname Resolution and DNS
Technical requirements
How hostname resolution works
DNS record types
DNS security
Name/address translation functions
Using getaddrinfo()
Using getnameinfo()
Alternative functions
IP lookup example program
The DNS protocol
DNS message format
DNS message header format
Question format
Answer format
Endianness
A simple DNS query
A DNS query program
Printing a DNS message name
Printing a DNS message
Sending the query
Summary
Questions
Further reading
Section 2 - An Overview of Application Layer Protocols
Chapter 6: Building a Simple Web Client
Technical requirements
The HTTP protocol
HTTP request types
HTTP request format
HTTP response format
HTTP response codes
Response body length
What's in a URL
Parsing a URL
Implementing a web client
HTTP POST requests
Encoding form data
File uploads
Summary
Questions
Further reading
Chapter 7: Building a Simple Web Server
Technical requirements
The HTTP server
The server architecture
Content types
Returning Content-Type from a filename
Creating the server socket
Multiple connections buffering
get_client()
drop_client()
get_client_address()
wait_on_clients()
send_400()
send_404()
serve_resource()
The main loop
Security and robustness
Open source servers
Summary
Questions
Further reading
Chapter 8: Making Your Program Send Email
Technical requirements
Email servers
SMTP security
Finding an email server
SMTP dialog
The format of an email
A simple SMTP client program
Enhanced emails
Email file attachments
Spam-blocking pitfalls
Summary
Questions
Further reading
Section 3 - Understanding Encrypted Protocols and OpenSSL
Chapter 9: Loading Secure Web Pages with HTTPS and OpenSSL
Technical requirements
HTTPS overview
Encryption basics
Symmetric ciphers
Asymmetric ciphers
How TLS uses ciphers
The TLS protocol
Certificates
Server name identification
OpenSSL
Encrypted sockets with OpenSSL
Certificates
A simple HTTPS client
Other examples
Summary
Questions
Further reading
Chapter 10: Implementing a Secure Web Server
Technical requirements
HTTPS and OpenSSL summary
Certificates
Self-signed certificates with OpenSSL
HTTPS server with OpenSSL
Time server example
A full HTTPS server
HTTPS server challenges
OpenSSL alternatives
Alternatives to TLS
Summary
Questions
Further reading
Chapter 11: Establishing SSH Connections with libssh
Technical requirements
The SSH protocol
libssh
Testing out libssh
Establishing a connection
SSH authentication
Server authentication
Client authentication
Executing a remote command
Downloading a file
Summary
Questions
Further reading
Section 4 - Odds and Ends
Chapter 12: Network Monitoring and Security
Technical requirements
The purpose of network monitoring
Testing reachability
Checking a route
How traceroute works
Raw sockets
Checking local connections
Snooping on connections
Deep packet inspection
Capturing all network traffic
Network security
Application security and safety
Network-testing etiquette
Summary
Questions
Further reading
Chapter 13: Socket Programming Tips and Pitfalls
Technical requirements
Error handling
Obtaining error descriptions
TCP socket tips
Timeout on connect()
TCP flow control and avoiding deadlock
Congestion control
The Nagle algorithm
Delayed acknowledgment
Connection tear-down
The shutdown() function
Preventing address-in-use errors
Sending to a disconnected peer
Socket's local address
Multiplexing with a large number of sockets
Summary
Questions
Chapter 14: Web Programming for the Internet of Things
Technical requirements
What is the IoT?
Connectivity options
Wi-Fi
Ethernet
Cellular
Bluetooth
IEEE 802.15.4 WPANs
Hardware choices
Single-board computers
Microcontrollers
FPGAs
External transceivers and modems
IoT protocols
Firmware updates
Ethics of IoT
Privacy and data collection
End-of-life planning
Security
Summary
Questions
Appendix A: Answers to Questions
Chapter 1, Introducing Networks and Protocols
Chapter 2, Getting to Grips with Socket APIs
Chapter 3, An In-Depth Overview of TCP Connections
Chapter 4, Establishing UDP Connections
Chapter 5, Hostname Resolution and DNS
Chapter 6, Building a Simple Web Client
Chapter 7, Building a Simple Web Server
Chapter 8, Making Your Program Send Email
Chapter 9, Loading Secure Web Pages with HTTPS and OpenSSL
Chapter 10, Implementing a Secure Web Server
Chapter 11, Establishing SSH Connections with libssh
Chapter 12, Network Monitoring and Security
Chapter 13, Socket Programming Tips and Pitfalls
Chapter 14, Web Programming for the Internet of Things
Appendix B: Setting Up Your C Compiler on Windows
Installing MinGW GCC
Installing Git
Installing OpenSSL
Installing libssh
Alternatives
Appendix C: Setting Up Your C Compiler on Linux
Installing GCC
Installing Git
Installing OpenSSL
Installing libssh
Appendix D: Setting Up Your C Compiler on macOS
Installing Homebrew and the C compiler
Installing OpenSSL
Installing libssh
Appendix E: Example Programs
Code license
Code included with this book
Chapter 1 – Introducing Networks and Protocols
Chapter 2 – Getting to Grips with Socket APIs
Chapter 3 – An In-Depth Overview of TCP Connections
Chapter 4 – Establishing UDP Connections
Chapter 5 – Hostname Resolution and DNS
Chapter 6 – Building a Simple Web Client
Chapter 7 – Building a Simple Web Server
Chapter 8 – Making Your Program Send Email
Chapter 9 – Loading Secure Web Pages with HTTPS and OpenSSL
Chapter 10 – Implementing a Secure Web Server
Chapter 11 – Establishing SSH Connections with libssh
Chapter 12 – Network Monitoring and Security
Chapter 13 – Socket Programming Tips and Pitfalls
Chapter 14 – Web Programming for the Internet of Things
Other Book You May Enjoy
Index