liveBook · Manning

IP address

As explained in Rust in Action: Systems programming concepts and techniques

An IP address is a numerical network-layer identifier assigned to a host or interface in an Internet Protocol network.

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

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Works through an IPv4 DNS query and Linux address configuration, without attempting comprehensive networking-stack coverage.
Examples
Uses a Rust DNS resolver and a Linux TAP device assigned 192.168.42.100/24.
What it assumes
Requires a Linux console for the virtual-device exercise and uses Rust and external crates for the resolver.
What you can do after
Apply it via trust-dns to request an IPv4 address for a hostname.
How it connects
Links IP addresses to DNS, domain names, TCP, MAC addresses, and packet forwarding.

Also explained in

Compare how 4 books explain IP address →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.