liveBook · Manning

Chapter 8: Networking

What you will learn in chapter 8 of Rust in Action: Systems programming concepts and techniques

Introduces the chapter’s progression from high-level HTTP requests to raw TCP packets and previews related Rust topics.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s progression from high-level HTTP requests to raw TCP packets and previews related Rust topics.

8.1 All of networking in seven paragraphs

Introduces networking through HTTP, explaining the roles of protocol layers and why their boundaries are not always clean.

Key terms: HTTP, application-level protocol, TLS, TCP, HTTPS, layers, Connection: keep-alive, http-equiv, web frameworks

8.2 Generating an HTTP GET request with reqwest

This section uses reqwest to make an HTTP GET request and explains which HTTP details the library handles for the program.

Key terms: reqwest, HTTP headers, content redirects, TLS, content’s encoding, reqwest::get(), response, .text(), Result, HTTP body

8.3 Trait objects

Introduces the upcoming discussion of trait objects and the rpg project, while pointing networking-focused readers to section 8.4.

8.3.1 What do trait objects enable?

This subsection explains why trait objects are useful when a container must hold values of different types.

Key terms: trait objects, Vec<T>, struct, wrapper object

8.3.2 What is a trait object?

This section explains how trait objects provide polymorphism through dynamic dispatch, how they compare with generics, and why they appear behind pointers.

Key terms: trait objects, polymorphism, dynamic dispatch, static dispatch, pointer indirection, dynamically-sized types, &dyn Trait, &mut dyn Trait, Box<dyn Trait>, owned

8.3.3 Creating a tiny role-playing game: The rpg project

The rpg example shows how trait objects let a party contain different character types and explains how trait-object references differ from references to concrete types.

Key terms: trait object, Human, Elf, Dwarf, Enchanter, Thing, type erasure, dynamic dispatch, enchant(), trait

8.4 TCP

This section explains two choices in an HTTP GET request built over a TCP connection: using HTTP 1.0 and including a Host header.

Key terms: HTTP 1.0, HTTP 1.1, keep alive, TcpStream::connect(), IP address, Host HTTP header

8.4.1 What is a port number?

8.4.2 Converting a hostname to an IP address

This section explains why a hostname must be resolved before making an internet request and uses a small DNS application to illustrate the request, response, and buffer handling involved.

Key terms: hostname, IP address, Domain Name System (DNS), domain name resolution, resolve, trust-dns, DNS message, stream of bytes, Vec::with_capacity(512), vec![0; 512]

8.5 Ergonomic error handling for libraries

This introduction identifies a limitation of the ? operator when a function combines Result types from different crates.

Key terms: Result types, ? operator, upstream crates

8.5.1 Issue: Unable to return multiple error types

This subsection explains why a function returning one concrete error type cannot automatically handle a different error type, and how a boxed error trait object allows either error to be returned.

Key terms: Result<T, E>, ? operator, try! macro, std::net::AddrParseError, std::io::Error, Box<dyn Error>, trait object, Box, File::open()

8.5.2 Wrapping downstream errors by defining our own error type

This section shows how to preserve errors from dependencies in a custom error type and make those errors convenient to return.

Key terms: trait objects, type erasure, Box<dyn Error>, upstream errors, enum, #[derive(Debug)], Display, Error, map_err(), std::convert::From

8.5.3 Cheating with unwrap() and expect()

This subsection presents unwrap() and expect() as a shortcut for dealing with multiple error types, while warning against their use in libraries.

Key terms: unwrap(), expect(), main(), library authors

8.6 MAC addresses

This section explains what MAC addresses identify, how they differ from IP addresses, and how their administration and recipient modes work.

Key terms: MAC addresses, IP addresses, Ethernet, physical link, hierarchical, 48 bits, IPv4, IPv6, universally administered, locally administered

8.6.1 Generating MAC addresses

The section explains how macgen sets flag bits when generating MAC addresses for a virtual device.

Key terms: macgen, MAC address, octets[0], locally assigned, unicast

8.7 Implementing state machines with Rust’s enums

The section introduces a simple enum-based state machine for adapting network code to changes in connectivity.

Key terms: state machine, loop, match, enum, transitions

8.8 Raw TCP

The section explains why raw TCP access is restricted and introduces a Linux proxy device as a way for non-superusers to work with it.

Key terms: raw TCP packets, root/superuser access, proxy device

8.9 Creating a virtual networking device

This section explains why to use a virtual TAP device and outlines how to create, verify, configure, and remove one on Linux.

Key terms: virtual hardware, IP and MAC addresses, TAP device, tap-rust, ip tuntap list, del, add

8.10 “Raw” HTTP

Introduces mget as a manually assembled HTTP client, identifies each source file’s role, and contrasts it with a higher-level GET call.

Key terms: mget, main.rs, ethernet.rs, http.rs, dns.rs, DNS resolution, request::get(url)

8.11 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Files and storage Chapter 9: Time and timekeeping →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.