concept IP address in category rust

appears as: n IP address, IP address, IP addresses, IP addresses
Rust in Action

This is an excerpt from Manning's book Rust in Action.

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.

8.4.2. Converting a hostname to an IP address

So far, we’ve been providing the hostname www.rustinaction.com to Rust. But, to send messages over the Internet, the Internet Protocol requires IP addresses. TCP knows nothing about domain names. To convert a domain name to an IP address, we rely on the Domain Name System (DNS) and its process called domain name resolution.

Listing 8. 13. resolve: a simple DNS command-line utility that can resolve the IP address for domain names (ch8/ch8-resolve/src/main.rs)
extern crate clap;

use std::net::{SocketAddr, UdpSocket};
use std::time::Duration;

use clap::{App, Arg};
use rand;
use trust_dns::op::{Message, MessageType, OpCode, Query};
use trust_dns::rr::domain::Name;
use trust_dns::rr::record_type::RecordType;
use trust_dns::serialize::binary::*;

fn main() {
  let app = App::new("resolve")
    .about("A simple to use DNS resolver")
    .arg(Arg::with_name("dns-server").short("s").default_value("1.1.1.1"))
    .arg(Arg::with_name("domain-name").required(true))
    .get_matches();

  let domain_name_raw = app.value_of("domain-name").unwrap();
  let dns_server_raw = app.value_of("dns-server").unwrap();

  let mut request_as_bytes: Vec<u8> = Vec::with_capacity(512);  #1
  let mut response_as_bytes: [u8; 512] = [0; 512];              #2

  let domain_name = Name::from_ascii(&domain_name_raw).unwrap();  #3
  let mut msg = Message::new();                                   #4
  msg
    .set_id(rand::random::<u16>())
    .set_message_type(MessageType::Query)                         #5
    .add_query(Query::query(domain_name, RecordType::A))
    .set_op_code(OpCode::Query)
    .set_recursion_desired(true);                                 #6

  let mut encoder = BinEncoder::new(&mut request_as_bytes);       #7
  msg.emit(&mut encoder).unwrap();                                #7

  let localhost = UdpSocket::bind("0.0.0.0:0").expect("cannot bind to local socket");  #8
  let timeout = Duration::from_secs(3);
  localhost.set_read_timeout(Some(timeout)).unwrap();
  localhost.set_nonblocking(false).unwrap();

  let dns_server: SocketAddr = format!("{}:53", dns_server_raw).parse().expect("invalid address");
  let _amt = localhost.send_to(&request_as_bytes, dns_server).expect("socket misconfigured");

  let (_amt, _remote) = localhost.recv_from(&mut response_as_bytes).expect("timeout reached");

  let dns_message = Message::from_vec(&response_as_bytes).expect("unable to parse response");

  for answer in dns_message.answers() {
    if answer.record_type() == RecordType::A {
      let resource = answer.rdata();
      let ip = resource.to_ip_addr().expect("invalid IP address received");
      println!("{}", ip.to_string());
    }
  }
}