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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! DNS resource record types

use std::mem::transmute;
use std::net::{Ipv4Addr, Ipv6Addr};

use message::{DecodeError, EncodeError, MsgReader, MsgWriter};

/// Represents the class of data in a message.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Class {
    /// Internet (`IN`)
    Internet,
    /// Any (`*`)
    Any,
    /// An unrecognized class
    Other(u16),
}

impl Class {
    /// Converts a `u16` to a `Class`.
    pub fn from_u16(u: u16) -> Class {
        match u {
            1 => Class::Internet,
            255 => Class::Any,
            n => Class::Other(n),
        }
    }

    /// Converts a `Class` to a `u16`.
    pub fn to_u16(&self) -> u16 {
        match *self {
            Class::Internet => 1,
            Class::Any => 255,
            Class::Other(n) => n,
        }
    }
}

/// Represents the type of data in a message.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RecordType {
    /// An IPv4 host address
    A,
    /// An IPv6 host address
    AAAA,
    /// Canonical name for an alias
    CName,
    /// Mail exchange
    Mx,
    /// Authoritative name server
    Ns,
    /// Domain name pointer
    Ptr,
    /// Start of authority
    Soa,
    /// Service record
    Srv,
    /// Text string
    Txt,
    /// Unrecognized record type
    Other(u16),
}

macro_rules! record_types {
    ( $( $name:ident => $code:expr , )+ ) => {
        impl RecordType {
            /// Converts a `u16` to a `RecordType`.
            pub fn from_u16(u: u16) -> RecordType {
                match u {
                    $( $code => RecordType::$name , )+
                    n => RecordType::Other(n),
                }
            }

            /// Converts a `RecordType` to a `u16`.
            pub fn to_u16(&self) -> u16 {
                match *self {
                    $( RecordType::$name => $code , )+
                    RecordType::Other(n) => n,
                }
            }
        }
    }
}

record_types!{
    A => 1,
    AAAA => 28,
    CName => 5,
    Mx => 15,
    Ns => 2,
    Ptr => 12,
    Soa => 6,
    Srv => 33,
    Txt => 16,
}

/// Represents resource record data.
pub trait Record: Sized {
    /// Decodes the `Record` from resource rdata.
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError>;

    /// Encodes the `Record` to resource rdata.
    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError>;

    /// Returns the `RecordType` of queries for this record.
    fn record_type() -> RecordType;
}

/// An IPv4 host address
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct A {
    /// The host address
    pub address: Ipv4Addr,
}

impl Record for A {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        let mut buf = [0; 4];
        try!(data.read(&mut buf));
        Ok(A{address: Ipv4Addr::new(buf[0], buf[1], buf[2], buf[3])})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        data.write(&self.address.octets())
    }

    fn record_type() -> RecordType { RecordType::A }
}

/// An IPv6 host address
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct AAAA {
    /// The host address
    pub address: Ipv6Addr,
}

impl Record for AAAA {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        let mut buf = [0; 16];
        try!(data.read(&mut buf));
        let segments: [u16; 8] = unsafe { transmute(buf) };
        Ok(AAAA{address: Ipv6Addr::new(
            u16::from_be(segments[0]), u16::from_be(segments[1]),
            u16::from_be(segments[2]), u16::from_be(segments[3]),
            u16::from_be(segments[4]), u16::from_be(segments[5]),
            u16::from_be(segments[6]), u16::from_be(segments[7]))})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        let mut segments = self.address.segments();
        for seg in &mut segments { *seg = seg.to_be() }
        let buf: [u8; 16] = unsafe { transmute(segments) };
        data.write(&buf)
    }

    fn record_type() -> RecordType { RecordType::AAAA }
}

/// Canonical name for an alias
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CName {
    /// Canonical host name
    pub name: String,
}

impl Record for CName {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(CName{name: try!(data.read_name())})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        data.write_name(&self.name)
    }

    fn record_type() -> RecordType { RecordType::CName }
}

/// Mail exchange data
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Mx {
    /// Represents the preference of this record among others.
    /// Lower values are preferred.
    pub preference: u16,
    /// Domain name willing to act as mail exchange for the host.
    pub exchange: String,
}

impl Record for Mx {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Mx{
            preference: try!(data.read_u16()),
            exchange: try!(data.read_name()),
        })
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        try!(data.write_u16(self.preference));
        data.write_name(&self.exchange)
    }

    fn record_type() -> RecordType { RecordType::Mx }
}

/// Authoritative name server
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Ns {
    /// Host which should be authoritative for the specified class and domain
    pub name: String,
}

impl Record for Ns {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Ns{name: try!(data.read_name())})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        data.write_name(&self.name)
    }

    fn record_type() -> RecordType { RecordType::Ns }
}

/// Domain name pointer
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Ptr {
    /// The name of the host
    pub name: String,
}

impl Record for Ptr {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Ptr{name: try!(data.read_name())})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        data.write_name(&self.name)
    }

    fn record_type() -> RecordType { RecordType::Ptr }
}

/// Start of authority
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Soa {
    /// Domain name of the name server that was the original or primary source
    /// of data for this zone.
    pub mname: String,
    /// Domain name which specifies the mailbox of the person responsible
    /// for this zone.
    pub rname: String,
    /// Version number of the original copy of the zone. This value wraps and
    /// should be compared using sequence space arithmetic.
    pub serial: u32,
    /// Time interval before the zone should be refreshed.
    pub refresh: u32,
    /// Time interval that should elapse before a failed refresh should be retried.
    pub retry: u32,
    /// Time value that specifies the upper limit on the time interval that can
    /// elapse before the zone is no longer authoritative.
    pub expire: u32,
    /// Minimum TTL that should be exported with any resource record from this zone.
    pub minimum: u32,
}

impl Record for Soa {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Soa{
            mname: try!(data.read_name()),
            rname: try!(data.read_name()),
            serial: try!(data.read_u32()),
            refresh: try!(data.read_u32()),
            retry: try!(data.read_u32()),
            expire: try!(data.read_u32()),
            minimum: try!(data.read_u32()),
        })
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        try!(data.write_name(&self.mname));
        try!(data.write_name(&self.rname));
        try!(data.write_u32(self.serial));
        try!(data.write_u32(self.refresh));
        try!(data.write_u32(self.retry));
        try!(data.write_u32(self.expire));
        try!(data.write_u32(self.minimum));
        Ok(())
    }

    fn record_type() -> RecordType { RecordType::Soa }
}

/// Service record
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Srv {
    /// Record priority
    pub priority: u16,
    /// Record weight
    pub weight: u16,
    /// Service port
    pub port: u16,
    /// Target host name
    pub target: String,
}

impl Record for Srv {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Srv{
            priority: try!(data.read_u16()),
            weight: try!(data.read_u16()),
            port: try!(data.read_u16()),
            target: try!(data.read_name()),
        })
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        try!(data.write_u16(self.priority));
        try!(data.write_u16(self.weight));
        try!(data.write_u16(self.port));
        try!(data.write_name(&self.target));
        Ok(())
    }

    fn record_type() -> RecordType { RecordType::Srv }
}

/// Text record
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Txt {
    /// One or more character strings
    pub data: Vec<u8>,
}

impl Record for Txt {
    fn decode(data: &mut MsgReader) -> Result<Self, DecodeError> {
        Ok(Txt{data: try!(data.read_character_string())})
    }

    fn encode(&self, data: &mut MsgWriter) -> Result<(), EncodeError> {
        data.write_character_string(&self.data)
    }

    fn record_type() -> RecordType { RecordType::Txt }
}

#[cfg(test)]
mod test {
    use super::Ns;

    #[test]
    fn test_record_eq() {
        //! Most records should implement Eq.
        //! Verify this with the Ns record.
        let a = Ns { name: "example.com".into() };
        let b = Ns { name: "example.com".into() };
        let c = Ns { name: "example.org".into() };
        assert!(a == b);
        assert!(a != c);
        assert!(b != c);
    }

    #[test]
    fn test_record_hash() {
        use std::hash::{Hash, SipHasher, Hasher};

        let a = Ns { name: "example.com".into() };
        let b = Ns { name: "example.com".into() };
        let c = Ns { name: "example.org".into() };

        fn hash<T: Hash>(t: &T) -> u64 {
            let mut s = SipHasher::new();
            t.hash(&mut s);
            s.finish()
        }

        assert!(hash(&a) == hash(&b));
        assert!(hash(&a) != hash(&c));
        assert!(hash(&b) != hash(&c));
    }
}