source: lib/ns_parse.c

Last change on this file was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[632627e]1/*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "bitlbee.h"
19
20#ifndef lint
[25c4c78]21//static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
[632627e]22#endif
23
24#ifdef HAVE_RESOLV_A
25#ifndef HAVE_RESOLV_A_WITH_NS
26/* Import. */
27
28
29#include <sys/types.h>
30
31#include <netinet/in.h>
32#include <arpa/nameser.h>
33
34#include <errno.h>
35#include <resolv.h>
36#include <string.h>
37
38
39/* Forward. */
40
[5ebff60]41static void     setsection(ns_msg *msg, ns_sect sect);
[632627e]42
43/* Macros. */
44
45#if !defined(SOLARIS2) || defined(__COVERITY__)
46#define RETERR(err) do { errno = (err); return (-1); } while (0)
47#else
48#define RETERR(err) \
[5ebff60]49        do { errno = (err); if (errno == errno) { return (-1); } } while (0)
[632627e]50#endif
51
[5ebff60]52#define PARSE_FMT_PRESO 0       /* Parse using presentation-format names */
53#define PARSE_FMT_WIRE 1        /* Parse using network-format names */
[632627e]54
55/* Public. */
56
57/* These need to be in the same order as the nres.h:ns_flag enum. */
58struct _ns_flagdata _ns_flagdata[16] = {
[5ebff60]59        { 0x8000, 15 },         /*%< qr. */
60        { 0x7800, 11 },         /*%< opcode. */
61        { 0x0400, 10 },         /*%< aa. */
62        { 0x0200, 9 },          /*%< tc. */
63        { 0x0100, 8 },          /*%< rd. */
64        { 0x0080, 7 },          /*%< ra. */
65        { 0x0040, 6 },          /*%< z. */
66        { 0x0020, 5 },          /*%< ad. */
67        { 0x0010, 4 },          /*%< cd. */
68        { 0x000f, 0 },          /*%< rcode. */
69        { 0x0000, 0 },          /*%< expansion (1/6). */
70        { 0x0000, 0 },          /*%< expansion (2/6). */
71        { 0x0000, 0 },          /*%< expansion (3/6). */
72        { 0x0000, 0 },          /*%< expansion (4/6). */
73        { 0x0000, 0 },          /*%< expansion (5/6). */
74        { 0x0000, 0 },          /*%< expansion (6/6). */
[632627e]75};
76
[5ebff60]77int ns_msg_getflag(ns_msg handle, int flag)
78{
[632627e]79        return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
80}
81
82int
[5ebff60]83ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count)
84{
[632627e]85        const u_char *optr = ptr;
86
[5ebff60]87        for ((void) NULL; count > 0; count--) {
[632627e]88                int b, rdlength;
89
90                b = dn_skipname(ptr, eom);
[5ebff60]91                if (b < 0) {
[632627e]92                        RETERR(EMSGSIZE);
[5ebff60]93                }
94                ptr += b /*Name*/ + NS_INT16SZ /*Type*/ + NS_INT16SZ /*Class*/;
[632627e]95                if (section != ns_s_qd) {
[5ebff60]96                        if (ptr + NS_INT32SZ + NS_INT16SZ > eom) {
[632627e]97                                RETERR(EMSGSIZE);
[5ebff60]98                        }
99                        ptr += NS_INT32SZ /*TTL*/;
[632627e]100                        NS_GET16(rdlength, ptr);
[5ebff60]101                        ptr += rdlength /*RData*/;
[632627e]102                }
103        }
[5ebff60]104        if (ptr > eom) {
[632627e]105                RETERR(EMSGSIZE);
[5ebff60]106        }
[632627e]107        return (ptr - optr);
108}
109
110int
[5ebff60]111ns_initparse(const u_char *msg, int msglen, ns_msg *handle)
112{
[632627e]113        const u_char *eom = msg + msglen;
114        int i;
115
116        handle->_msg = msg;
117        handle->_eom = eom;
[5ebff60]118        if (msg + NS_INT16SZ > eom) {
[632627e]119                RETERR(EMSGSIZE);
[5ebff60]120        }
[632627e]121        NS_GET16(handle->_id, msg);
[5ebff60]122        if (msg + NS_INT16SZ > eom) {
[632627e]123                RETERR(EMSGSIZE);
[5ebff60]124        }
[632627e]125        NS_GET16(handle->_flags, msg);
126        for (i = 0; i < ns_s_max; i++) {
[5ebff60]127                if (msg + NS_INT16SZ > eom) {
[632627e]128                        RETERR(EMSGSIZE);
[5ebff60]129                }
[632627e]130                NS_GET16(handle->_counts[i], msg);
131        }
[5ebff60]132        for (i = 0; i < ns_s_max; i++) {
133                if (handle->_counts[i] == 0) {
[632627e]134                        handle->_sections[i] = NULL;
[5ebff60]135                } else {
136                        int b = ns_skiprr(msg, eom, (ns_sect) i,
137                                          handle->_counts[i]);
[632627e]138
[5ebff60]139                        if (b < 0) {
[632627e]140                                return (-1);
[5ebff60]141                        }
[632627e]142                        handle->_sections[i] = msg;
143                        msg += b;
144                }
[5ebff60]145        }
146        if (msg != eom) {
[632627e]147                RETERR(EMSGSIZE);
[5ebff60]148        }
[632627e]149        setsection(handle, ns_s_max);
150        return (0);
151}
152
153int
[5ebff60]154ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr)
155{
[632627e]156        int b;
157        int tmp;
158
159        /* Make section right. */
160        tmp = section;
[5ebff60]161        if (tmp < 0 || section >= ns_s_max) {
[632627e]162                RETERR(ENODEV);
[5ebff60]163        }
164        if (section != handle->_sect) {
[632627e]165                setsection(handle, section);
[5ebff60]166        }
[632627e]167
168        /* Make rrnum right. */
[5ebff60]169        if (rrnum == -1) {
[632627e]170                rrnum = handle->_rrnum;
[5ebff60]171        }
172        if (rrnum < 0 || rrnum >= handle->_counts[(int) section]) {
[632627e]173                RETERR(ENODEV);
[5ebff60]174        }
175        if (rrnum < handle->_rrnum) {
[632627e]176                setsection(handle, section);
[5ebff60]177        }
[632627e]178        if (rrnum > handle->_rrnum) {
179                b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
[5ebff60]180                              rrnum - handle->_rrnum);
[632627e]181
[5ebff60]182                if (b < 0) {
[632627e]183                        return (-1);
[5ebff60]184                }
[632627e]185                handle->_msg_ptr += b;
186                handle->_rrnum = rrnum;
187        }
188
189        /* Do the parse. */
190        b = dn_expand(handle->_msg, handle->_eom,
[5ebff60]191                      handle->_msg_ptr, rr->name, NS_MAXDNAME);
192        if (b < 0) {
[632627e]193                return (-1);
[5ebff60]194        }
[632627e]195        handle->_msg_ptr += b;
[5ebff60]196        if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) {
[632627e]197                RETERR(EMSGSIZE);
[5ebff60]198        }
[632627e]199        NS_GET16(rr->type, handle->_msg_ptr);
200        NS_GET16(rr->rr_class, handle->_msg_ptr);
201        if (section == ns_s_qd) {
202                rr->ttl = 0;
203                rr->rdlength = 0;
204                rr->rdata = NULL;
205        } else {
[5ebff60]206                if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) {
[632627e]207                        RETERR(EMSGSIZE);
[5ebff60]208                }
[632627e]209                NS_GET32(rr->ttl, handle->_msg_ptr);
210                NS_GET16(rr->rdlength, handle->_msg_ptr);
[5ebff60]211                if (handle->_msg_ptr + rr->rdlength > handle->_eom) {
[632627e]212                        RETERR(EMSGSIZE);
[5ebff60]213                }
[632627e]214                rr->rdata = handle->_msg_ptr;
215                handle->_msg_ptr += rr->rdlength;
216        }
[5ebff60]217        if (++handle->_rrnum > handle->_counts[(int) section]) {
218                setsection(handle, (ns_sect) ((int) section + 1));
219        }
[632627e]220
221        /* All done. */
222        return (0);
223}
224
225/* Private. */
226
227static void
[5ebff60]228setsection(ns_msg *msg, ns_sect sect)
229{
[632627e]230        msg->_sect = sect;
231        if (sect == ns_s_max) {
232                msg->_rrnum = -1;
233                msg->_msg_ptr = NULL;
234        } else {
235                msg->_rrnum = 0;
[5ebff60]236                msg->_msg_ptr = msg->_sections[(int) sect];
[632627e]237        }
238}
239
240/*! \file */
241#endif
242#endif
Note: See TracBrowser for help on using the repository browser.