An IP address is a numerical address used to send messages over the internet. A hostname is easier to remember, but TCP does not understand domain names. Before connecting to a hostname, a program can use the Domain Name System (DNS) to find its IP address. section 8.4.2
An IP address appears in two practical jobs:
- Look one up: a DNS query can request the IPv4 address associated with a domain name. section 8.4.2
- Assign one: a network device needs an IP address before it can participate in the configured packet-forwarding setup. section 8.9
Listing 8.7 Constructing a DNS message in Rust
35 let mut msg = Message::new(); #1 36 msg 37 .set_id(rand::random::<u16>()) #2 38 .set_message_type(MessageType::Query) 39 .add_query( #3 40 Query::query(domain_name, RecordType::A) #4 41 ) 42 .set_op_code(OpCode::Query) 43 .set_recursion_desired(true); #5
The Rust DNS request uses an A-record query to ask for a domain name’s IPv4 address; the trust-dns crate handles the protocol details. section 8.4.2
Give a virtual device an address
On Linux, `sudo ip addr add 192.168.42.100/24 dev tap-rust` assigns an address to a TAP device. The setup also activates the device and configures packet forwarding. Virtual hardware lets you experiment without changing your physical network device’s settings. section 8.9
$ sudo ip link set tap-rust up #1 $ sudo ip addr add 192.168.42.100/24 dev tap-rust #2 $ sudo iptables \ #3 > -t nat\ #3 > -A POSTROUTING \ #3 > -s 192.168.42.0/24 \ #3 > -j MASQUERADE #3 $ sudo sysctl net.ipv4.ip_forward=1 #4 #1 Establishes a network device called tap-rust and activates it #2 Assigns the IP address 192.168.42.100 to the device #3 Enables internet packets to reach the source IP address mask (-s 192.168.42.100/24) by appending a rule (-A POSTROUTING) that dynamically maps IP addresses to a device (-j MASQUERADE) #4 Instructs the kernel to enable IPv4 packet forwarding
These commands show the address assignment alongside the forwarding configuration needed for the virtual-device exercise. section 8.9



