Skip to main content

c/
ns_parse.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// Parsing of DNS messages for the ns_* API from <arpa/nameser.h>.
5
6pub const NS_MAXDNAME_V: usize = 1025;
7const NS_INT16SZ_V: usize = 2;
8const NS_INT32SZ_V: usize = 4;
9const NS_S_MAX_V: usize = 4;
10
11#[repr(C)]
12pub struct NsMsg {
13    pub _msg: *const u8,
14    pub _eom: *const u8,
15    pub _id: u16,
16    pub _flags: u16,
17    pub _counts: [u16; NS_S_MAX_V],
18    pub _sections: [*const u8; NS_S_MAX_V],
19    pub _sect: c_int,
20    pub _rrnum: c_int,
21    pub _msg_ptr: *const u8,
22}
23
24#[repr(C)]
25pub struct NsRr {
26    pub name: [c_char; NS_MAXDNAME_V],
27    pub type_: u16,
28    pub rr_class: u16,
29    pub ttl: u32,
30    pub rdlength: u16,
31    __pad: u16,
32    pub rdata: *const u8,
33}
34
35#[no_mangle]
36pub static _ns_flagdata: [[c_int; 2]; 16] = [
37    [0x8000, 15],
38    [0x7800, 11],
39    [0x0400, 10],
40    [0x0200, 9],
41    [0x0100, 8],
42    [0x0080, 7],
43    [0x0040, 6],
44    [0x0020, 5],
45    [0x0010, 4],
46    [0x000f, 0],
47    [0x0000, 0],
48    [0x0000, 0],
49    [0x0000, 0],
50    [0x0000, 0],
51    [0x0000, 0],
52    [0x0000, 0],
53];
54
55fn ns_offset(base: *const u8, len: usize, p: *const u8) -> Option<usize> {
56    let off = (p as usize).checked_sub(base as usize)?;
57    if off <= len { Some(off) } else { None }
58}
59
60// Skips `count` resource records starting at `start`, returning the number
61// of bytes advanced.  All reads go through InBuf, so a truncated message
62// turns into None instead of an out-of-bounds access.
63fn ns_skiprr_inner(msg: &InBuf, start: usize, section: c_int, mut count: c_int) -> Option<usize> {
64    let mut p = start;
65    while count > 0 {
66        count -= 1;
67        p = dns_skip_name(msg, p)?;
68        // type + class
69        if msg.len() - p < 2 * NS_INT16SZ_V {
70            return None;
71        }
72        p += 2 * NS_INT16SZ_V;
73        if section != 0 {
74            // ttl + rdlength
75            if msg.len() - p < NS_INT32SZ_V + NS_INT16SZ_V {
76                return None;
77            }
78            let rdlen = msg.peek_u16_be(p + NS_INT32SZ_V)? as usize;
79            p += NS_INT32SZ_V + NS_INT16SZ_V;
80            if rdlen > msg.len() - p {
81                return None;
82            }
83            p += rdlen;
84        }
85    }
86    p.checked_sub(start)
87}
88
89#[no_mangle]
90pub unsafe extern "C" fn ns_skiprr(ptr: *const u8, eom: *const u8, section: c_int, count: c_int) -> c_int {
91    let len = match (eom as usize).checked_sub(ptr as usize) {
92        Some(len) => len,
93        None => {
94            ERRNO!() = EMSGSIZE;
95            return -1;
96        }
97    };
98    let msg = match InBuf::new(ptr, len) {
99        Some(msg) => msg,
100        None => {
101            ERRNO!() = EMSGSIZE;
102            return -1;
103        }
104    };
105    match ns_skiprr_inner(&msg, 0, section, count) {
106        Some(r) if r <= c_int::MAX as usize => r as c_int,
107        _ => {
108            ERRNO!() = EMSGSIZE;
109            -1
110        }
111    }
112}
113
114#[no_mangle]
115pub unsafe extern "C" fn ns_initparse(msg: *const u8, msglen: c_int, handle: *mut NsMsg) -> c_int {
116    if msg.is_null() || handle.is_null() || msglen < 0 {
117        ERRNO!() = EMSGSIZE;
118        return -1;
119    }
120    let len = msglen as usize;
121    let view = match InBuf::new(msg, len) {
122        Some(view) => view,
123        None => {
124            ERRNO!() = EMSGSIZE;
125            return -1;
126        }
127    };
128    (*handle)._msg = msg;
129    (*handle)._eom = msg.add(len);
130    if len < (2 + NS_S_MAX_V) * NS_INT16SZ_V {
131        ERRNO!() = EMSGSIZE;
132        return -1;
133    }
134    (*handle)._id = match view.peek_u16_be(0) {
135        Some(v) => v,
136        None => {
137            ERRNO!() = EMSGSIZE;
138            return -1;
139        }
140    };
141    (*handle)._flags = match view.peek_u16_be(2) {
142        Some(v) => v,
143        None => {
144            ERRNO!() = EMSGSIZE;
145            return -1;
146        }
147    };
148    for i in 0..NS_S_MAX_V {
149        (*handle)._counts[i] = match view.peek_u16_be(4 + 2 * i) {
150            Some(v) => v,
151            None => {
152                ERRNO!() = EMSGSIZE;
153                return -1;
154            }
155        };
156    }
157    let mut p = (2 + NS_S_MAX_V) * NS_INT16SZ_V;
158    for i in 0..NS_S_MAX_V {
159        let count = (*handle)._counts[i] as c_int;
160        if count != 0 {
161            (*handle)._sections[i] = msg.add(p);
162            let r = match ns_skiprr_inner(&view, p, i as c_int, count) {
163                Some(r) => r,
164                None => {
165                    ERRNO!() = EMSGSIZE;
166                    return -1;
167                }
168            };
169            p = match p.checked_add(r) {
170                Some(p) => p,
171                None => {
172                    ERRNO!() = EMSGSIZE;
173                    return -1;
174                }
175            };
176        } else {
177            (*handle)._sections[i] = core::ptr::null();
178        }
179    }
180    if p != len {
181        ERRNO!() = EMSGSIZE;
182        return -1;
183    }
184    (*handle)._sect = NS_S_MAX_V as c_int;
185    (*handle)._rrnum = -1;
186    (*handle)._msg_ptr = core::ptr::null();
187    0
188}
189
190#[no_mangle]
191pub unsafe extern "C" fn ns_name_uncompress(
192    msg: *const u8,
193    eom: *const u8,
194    src: *const u8,
195    dst: *mut c_char,
196    dstsiz: usize,
197) -> c_int {
198    let r = crate::dn_expand(msg, eom, src, dst, dstsiz as c_int);
199    if r < 0 { ERRNO!() = EMSGSIZE; }
200    r
201}
202
203#[no_mangle]
204pub unsafe extern "C" fn ns_parserr(handle: *mut NsMsg, section: c_int, rrnum_in: c_int, rr: *mut NsRr) -> c_int {
205    if handle.is_null() || rr.is_null() || section < 0 || section >= NS_S_MAX_V as c_int {
206        ERRNO!() = ENODEV;
207        return -1;
208    }
209    let base = (*handle)._msg;
210    let len = match ((*handle)._eom as usize).checked_sub(base as usize) {
211        Some(len) => len,
212        None => {
213            ERRNO!() = ENODEV;
214            return -1;
215        }
216    };
217    let view = match InBuf::new(base, len) {
218        Some(view) => view,
219        None => {
220            ERRNO!() = ENODEV;
221            return -1;
222        }
223    };
224    let sec = section as usize;
225    if section != (*handle)._sect {
226        (*handle)._sect = section;
227        (*handle)._rrnum = 0;
228        (*handle)._msg_ptr = (*handle)._sections[sec];
229    }
230    let mut rrnum = rrnum_in;
231    if rrnum == -1 { rrnum = (*handle)._rrnum; }
232    if rrnum < 0 || rrnum >= (*handle)._counts[sec] as c_int {
233        ERRNO!() = ENODEV;
234        return -1;
235    }
236    if rrnum < (*handle)._rrnum {
237        (*handle)._rrnum = 0;
238        (*handle)._msg_ptr = (*handle)._sections[sec];
239    }
240    if rrnum > (*handle)._rrnum {
241        let off = match ns_offset(base, len, (*handle)._msg_ptr) {
242            Some(off) => off,
243            None => {
244                ERRNO!() = ENODEV;
245                return -1;
246            }
247        };
248        let skip = match ns_skiprr_inner(&view, off, section, rrnum - (*handle)._rrnum) {
249            Some(skip) => skip,
250            None => {
251                ERRNO!() = EMSGSIZE;
252                return -1;
253            }
254        };
255        let next = match off.checked_add(skip) {
256            Some(next) if next <= len => next,
257            _ => {
258                ERRNO!() = EMSGSIZE;
259                return -1;
260            }
261        };
262        (*handle)._msg_ptr = base.add(next);
263        (*handle)._rrnum = rrnum;
264    }
265    let r = ns_name_uncompress(
266        (*handle)._msg,
267        (*handle)._eom,
268        (*handle)._msg_ptr,
269        (*rr).name.as_mut_ptr(),
270        NS_MAXDNAME_V,
271    );
272    if r < 0 { return -1; }
273    let mut off = match ns_offset(base, len, (*handle)._msg_ptr) {
274        Some(off) => off,
275        None => {
276            ERRNO!() = EMSGSIZE;
277            return -1;
278        }
279    };
280    off = match off.checked_add(r as usize) {
281        Some(off) if off <= len => off,
282        _ => {
283            ERRNO!() = EMSGSIZE;
284            return -1;
285        }
286    };
287    (*handle)._msg_ptr = base.add(off);
288    if len - off < 2 * NS_INT16SZ_V {
289        ERRNO!() = EMSGSIZE;
290        return -1;
291    }
292    (*rr).type_ = match view.peek_u16_be(off) {
293        Some(v) => v,
294        None => {
295            ERRNO!() = EMSGSIZE;
296            return -1;
297        }
298    };
299    (*rr).rr_class = match view.peek_u16_be(off + NS_INT16SZ_V) {
300        Some(v) => v,
301        None => {
302            ERRNO!() = EMSGSIZE;
303            return -1;
304        }
305    };
306    off += 2 * NS_INT16SZ_V;
307    if section != 0 {
308        if len - off < NS_INT32SZ_V + NS_INT16SZ_V {
309            ERRNO!() = EMSGSIZE;
310            return -1;
311        }
312        (*rr).ttl = match view.peek_u32_be(off) {
313            Some(v) => v,
314            None => {
315                ERRNO!() = EMSGSIZE;
316                return -1;
317            }
318        };
319        (*rr).rdlength = match view.peek_u16_be(off + NS_INT32SZ_V) {
320            Some(v) => v,
321            None => {
322                ERRNO!() = EMSGSIZE;
323                return -1;
324            }
325        };
326        off += NS_INT32SZ_V + NS_INT16SZ_V;
327        if (*rr).rdlength as usize > len - off {
328            ERRNO!() = EMSGSIZE;
329            return -1;
330        }
331        (*rr).rdata = base.add(off);
332        off += (*rr).rdlength as usize;
333    } else {
334        (*rr).ttl = 0;
335        (*rr).rdlength = 0;
336        (*rr).rdata = core::ptr::null();
337    }
338    (*handle)._msg_ptr = base.add(off);
339    (*handle)._rrnum += 1;
340    if (*handle)._rrnum > (*handle)._counts[sec] as c_int {
341        (*handle)._sect = section + 1;
342        if (*handle)._sect == NS_S_MAX_V as c_int {
343            (*handle)._rrnum = -1;
344            (*handle)._msg_ptr = core::ptr::null();
345        } else {
346            (*handle)._rrnum = 0;
347        }
348    }
349    0
350}