chapter eight

8 Networking

 

This chapter implements making HTTP requests multiple times, stripping away a layer of abstraction each time. We start by using a user-friendly library, then boil that away until we’re left with manipulating raw TCP packets. Impress your friends your ability to distinguish an IP address from a MAC address. And learn why we went straight from IPv4 to IPv6.

You’ll also be learning lots of Rust in this chapter, most of it related to advanced error handling techniques which become essential for incorporating upstream crates. Several pages are devoted to error handling. This includes a through introduction to trait objects.

Networking is a difficult subject to cover in a single chapter. Each layer is a fractal of complexity. Networking experts will hopefully be kind of my lack of depth in my treatment of their topic of interest!

Figure 8.1  provides an overview of the topics that the chapter covers. Some of the projects that we cover include implementing DNS resolution, generating standards-compliant MAC addresses and multiple examples of generating HTTP requests.. A hint of a role-playing game is added for light relief.

Figure 8.1. Networking chapter map. The chapter incorporates a healthy mix of theory and practical exercises.
ch8 chapter map

8.1  Just enough HTTP

Generating an HTTP GET request with reqwest

8.3  Trait Objects

8.4  TCP

8.4.1  What is a port number?

8.4.2  Converting a hostname to an IP address

8.5  Ergonomic Error Handling for Libraries

8.5.1  Issue: unable to return multiple error types

8.5.2  Wrapping downstream errors by defining our own error type

8.5.3  Cheat with unwrap() and expect()

8.6  MAC addresses

8.6.1  Generating MAC addresses

8.7  Implementing state machines with Rust’s enums

8.8  Raw TCP

8.9  Creating a virtual networking device

8.10  “Raw” HTTP

8.11  Wrapping Up