| 1 |
use Name; |
| 2 |
|
| 3 |
#[derive(Debug, Clone, Copy)] |
| 4 |
pub struct Record<'a>(pub Name<'a>); |
| 5 |
|
| 6 |
impl<'a> ToString for Record<'a> { |
| 7 |
#[inline] |
| 8 |
fn to_string(&self) -> String { |
| 9 |
self.0.to_string() |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
impl<'a> super::Record<'a> for Record<'a> { |
| 14 |
|
| 15 |
const TYPE: isize = 5; |
| 16 |
|
| 17 |
fn parse(rdata: &'a [u8], original: &'a [u8]) -> super::RDataResult<'a> { |
| 18 |
let name = Name::scan(rdata, original)?; |
| 19 |
let record = Record(name); |
| 20 |
Ok(super::RData::CNAME(record)) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(test)] |
| 25 |
mod test { |
| 26 |
|
| 27 |
use std::net::Ipv4Addr; |
| 28 |
use {Packet, Header}; |
| 29 |
use Opcode::*; |
| 30 |
use ResponseCode::NoError; |
| 31 |
use QueryType as QT; |
| 32 |
use QueryClass as QC; |
| 33 |
use Class as C; |
| 34 |
use RData; |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn parse_response() { |
| 38 |
let response = b"\xfc\x9d\x81\x80\x00\x01\x00\x06\x00\x02\x00\x02\x03\ |
| 39 |
cdn\x07sstatic\x03net\x00\x00\x01\x00\x01\xc0\x0c\x00\x05\x00\x01\ |
| 40 |
\x00\x00\x00f\x00\x02\xc0\x10\xc0\x10\x00\x01\x00\x01\x00\x00\x00\ |
| 41 |
f\x00\x04h\x10g\xcc\xc0\x10\x00\x01\x00\x01\x00\x00\x00f\x00\x04h\ |
| 42 |
\x10k\xcc\xc0\x10\x00\x01\x00\x01\x00\x00\x00f\x00\x04h\x10h\xcc\ |
| 43 |
\xc0\x10\x00\x01\x00\x01\x00\x00\x00f\x00\x04h\x10j\xcc\xc0\x10\ |
| 44 |
\x00\x01\x00\x01\x00\x00\x00f\x00\x04h\x10i\xcc\xc0\x10\x00\x02\ |
| 45 |
\x00\x01\x00\x00\x99L\x00\x0b\x08cf-dns02\xc0\x10\xc0\x10\x00\x02\ |
| 46 |
\x00\x01\x00\x00\x99L\x00\x0b\x08cf-dns01\xc0\x10\xc0\xa2\x00\x01\ |
| 47 |
\x00\x01\x00\x00\x99L\x00\x04\xad\xf5:5\xc0\x8b\x00\x01\x00\x01\x00\ |
| 48 |
\x00\x99L\x00\x04\xad\xf5;\x04"; |
| 49 |
|
| 50 |
let packet = Packet::parse(response).unwrap(); |
| 51 |
assert_eq!(packet.header, Header { |
| 52 |
id: 64669, |
| 53 |
query: false, |
| 54 |
opcode: StandardQuery, |
| 55 |
authoritative: false, |
| 56 |
truncated: false, |
| 57 |
recursion_desired: true, |
| 58 |
recursion_available: true, |
| 59 |
authenticated_data: false, |
| 60 |
checking_disabled: false, |
| 61 |
response_code: NoError, |
| 62 |
questions: 1, |
| 63 |
answers: 6, |
| 64 |
nameservers: 2, |
| 65 |
additional: 2, |
| 66 |
}); |
| 67 |
|
| 68 |
assert_eq!(packet.questions.len(), 1); |
| 69 |
assert_eq!(packet.questions[0].qtype, QT::A); |
| 70 |
assert_eq!(packet.questions[0].qclass, QC::IN); |
| 71 |
assert_eq!(&packet.questions[0].qname.to_string()[..], "cdn.sstatic.net"); |
| 72 |
assert_eq!(packet.answers.len(), 6); |
| 73 |
assert_eq!(&packet.answers[0].name.to_string()[..], "cdn.sstatic.net"); |
| 74 |
assert_eq!(packet.answers[0].cls, C::IN); |
| 75 |
assert_eq!(packet.answers[0].ttl, 102); |
| 76 |
match packet.answers[0].data { |
| 77 |
RData::CNAME(cname) => { |
| 78 |
assert_eq!(&cname.0.to_string(), "sstatic.net"); |
| 79 |
} |
| 80 |
ref x => panic!("Wrong rdata {:?}", x), |
| 81 |
} |
| 82 |
|
| 83 |
let ips = vec![ |
| 84 |
Ipv4Addr::new(104, 16, 103, 204), |
| 85 |
Ipv4Addr::new(104, 16, 107, 204), |
| 86 |
Ipv4Addr::new(104, 16, 104, 204), |
| 87 |
Ipv4Addr::new(104, 16, 106, 204), |
| 88 |
Ipv4Addr::new(104, 16, 105, 204), |
| 89 |
]; |
| 90 |
for i in 1..6 { |
| 91 |
assert_eq!(&packet.answers[i].name.to_string()[..], "sstatic.net"); |
| 92 |
assert_eq!(packet.answers[i].cls, C::IN); |
| 93 |
assert_eq!(packet.answers[i].ttl, 102); |
| 94 |
match packet.answers[i].data { |
| 95 |
RData::A(addr) => { |
| 96 |
assert_eq!(addr.0, ips[i-1]); |
| 97 |
} |
| 98 |
ref x => panic!("Wrong rdata {:?}", x), |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |