blob: f8bf4265d684807ca409d6e9fe8965dbd791c5c6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
extern crate dns_parser;
use std::env;
use std::error::Error;
use std::net::UdpSocket;
use std::process;
use dns_parser::{Builder, Packet, RData, ResponseCode};
use dns_parser::rdata::a::Record;
use dns_parser::{QueryType, QueryClass};
fn main() {
let mut code = 0;
for name in env::args().skip(1) {
match resolve(&name) {
Ok(()) => {},
Err(e) => {
eprintln!("Error resolving {:?}: {}", name, e);
code = 1;
}
}
}
process::exit(code);
}
fn resolve(name: &str) -> Result<(), Box<Error>> {
let sock = UdpSocket::bind("127.0.0.1:0")?;
sock.connect("127.0.0.1:53")?;
let mut builder = Builder::new_query(1, true);
builder.add_question(name, false, QueryType::A, QueryClass::IN);
let packet = builder.build().map_err(|_| "truncated packet")?;
sock.send(&packet)?;
let mut buf = vec![0u8; 4096];
sock.recv(&mut buf)?;
let pkt = Packet::parse(&buf)?;
if pkt.header.response_code != ResponseCode::NoError {
return Err(pkt.header.response_code.into());
}
if pkt.answers.len() == 0 {
return Err("No records received".into());
}
for ans in pkt.answers {
match ans.data {
RData::A(Record(ip)) => {
println!("{}", ip);
}
_ => {} // ignore
}
}
Ok(())
}
|