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

use ::libresolv_sys::__ns_msg as Message;
use ::libresolv_sys::__ns_sect as NSSection;
use ::libresolv_sys::__ns_rr as Rr;

use ::error::Error;
use ::record::{Record, RecordData};

pub struct Flags(pub u16);

impl Flags {
    #[inline]
    pub fn question_response(&self) -> bool {
        ((self.0 & 0x8000) >> 15) > 0
    }
    #[inline]
    pub fn operation_code(&self) -> u16 {
        ((self.0 & 0x7800) >> 11) as u16
    }
    #[inline]
    pub fn authoritative_answer(&self) -> bool {
        ((self.0 & 0x0400) >> 10) > 0
    }
    #[inline]
    pub fn truncation_occurred(&self) -> bool {
        ((self.0 & 0x0200) >> 9) > 0
    }
    #[inline]
    pub fn recursion_desired(&self) -> bool {
        ((self.0 & 0x0100) >> 8) > 0
    }
    #[inline]
    pub fn recursion_available(&self) -> bool {
        ((self.0 & 0x0080) >> 7) > 0
    }
    #[inline]
    pub fn must_be_zero(&self) -> bool {
        ((self.0 & 0x0040) >> 6) > 0
    }
    #[inline]
    pub fn authentic_data(&self) -> bool {
        ((self.0 & 0x0020) >> 5) > 0
    }
    #[inline]
    pub fn checking_disabled(&self) -> bool {
        ((self.0 & 0x0010) >> 4) > 0
    }
    #[inline]
    pub fn response_code(&self) -> u16 {
        (self.0 & 0x000f) as u16
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Section {
    Question,
    Answer,
    Authority,
    Additional
}
impl Section {
    fn ns_sect(&self) -> NSSection
    {
        match *self {
            Section::Question => NSSection::ns_s_qd,
            Section::Answer => NSSection::ns_s_an,
            Section::Authority => NSSection::ns_s_ns,
            Section::Additional => NSSection::ns_s_ar,
        }
    }
}


pub struct Response {
    msg: Message,
    #[allow(dead_code)] // buffer is where the real data is; keep it until Response drops.
    buffer: Box<Vec<u8>>,
}

impl Response {
    /// This is for internal use.
    pub fn new(msg: Message, buffer: Box<Vec<u8>>) -> Response {
        Response {
            msg: msg,
            buffer: buffer
        }
    }

    /// Gets the ID field of the Name Server response
    pub fn get_id(&self) -> u16
    {
        self.msg._id
    }

    /// Gets flags (and opcodes) in the header of the Name Server response
    pub fn get_flags(&self) -> Flags
    {
        Flags(self.msg._flags)
    }

    /// Returns a count of how many records exist in the given section
    pub fn get_section_count(&self, section: Section) -> usize
    {
        self.msg._counts[section.ns_sect() as usize] as usize
    }

    /// Gets a record from a section.  Returns an error if index is out of bounds
    /// (use get_section_count()).  Also returns an error (at run-time) if assigned into
    /// a Record of the wrong type.
    pub fn get_record<T>(&mut self, section: Section, index: usize)
                         -> Result<Record<T>, Error>
        where T: RecordData
    {
        if index >= self.get_section_count(section) {
            return Err(Error::NoSuchSectionIndex(section, index));
        }
        let mut rr: Rr = Rr::default();
        unsafe {
            if ::libresolv_sys::ns_parserr(&mut self.msg, section.ns_sect(),
                                           index as i32, &mut rr) < 0
            {
                return Err(Error::ParseError);
            }
        }

        Record::extract(&mut self.msg, &rr)
    }

    pub fn questions<T>(&mut self) -> RecordItems<T>
        where T: RecordData
    {
        RecordItems {
            msg: &mut self.msg,
            section: Section::Question,
            index: 0,
            _marker: ::std::marker::PhantomData,
        }
    }

    pub fn answers<T>(&mut self) -> RecordItems<T>
        where T: RecordData
    {
        RecordItems {
            msg: &mut self.msg,
            section: Section::Answer,
            index: 0,
            _marker: ::std::marker::PhantomData,
        }
    }

    pub fn authorities<T>(&mut self) -> RecordItems<T>
        where T: RecordData
    {
        RecordItems {
            msg: &mut self.msg,
            section: Section::Authority,
            index: 0,
            _marker: ::std::marker::PhantomData,
        }
    }

    pub fn additional_records<T>(&mut self) -> RecordItems<T>
        where T: RecordData
    {
        RecordItems {
            msg: &mut self.msg,
            section: Section::Additional,
            index: 0,
            _marker: ::std::marker::PhantomData,
        }
    }
}

/// An iterator to iterate through DNS records
pub struct RecordItems<'a, T: RecordData> {
    msg: &'a mut Message,
    section: Section,
    index: usize,
    _marker: ::std::marker::PhantomData<T>,
}

impl<'a, T: RecordData> Iterator for RecordItems<'a, T> {
    type Item = Record<T>;

    fn next(&mut self) -> Option<Record<T>>
    {
        let len = self.msg._counts[self.section.ns_sect() as usize] as usize;
        if self.index >= len {
            return None;
        }

        let mut rr: Rr = Rr::default();
        unsafe {
            if ::libresolv_sys::ns_parserr(self.msg, self.section.ns_sect(),
                                           self.index as i32, &mut rr) < 0
            {
                return None;
            }
        }
        self.index += 1;

        Record::extract(self.msg, &rr).ok()
    }
}