root/src/udns/udns_rr_naptr.c

Revision 5a9b91bc0a14543e0995eba973fbeacc05aaee8b, 3.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

svn merge -r 327:330 https://labs.omniti.com/reconnoiter/branches/dev/udns .

closes #38

  • Property mode set to 100644
Line 
1 /* $Id: udns_rr_naptr.c,v 1.1 2006/11/28 22:58:04 mjt Exp $
2    parse/query NAPTR IN records
3
4    Copyright (C) 2005  Michael Tokarev <mjt@corpit.ru>
5    Copyright (C) 2006  Mikael Magnusson <mikma@users.sourceforge.net>
6    This file is part of UDNS library, an async DNS stub resolver.
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with this library, in file named COPYING.LGPL; if not,
20    write to the Free Software Foundation, Inc., 59 Temple Place,
21    Suite 330, Boston, MA  02111-1307  USA
22
23  */
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include "udns.h"
29
30 /* Get a single string for NAPTR record, pretty much like a DN label.
31  * String length is in first byte in *cur, so it can't be >255.
32  */
33 static int dns_getstr(dnscc_t **cur, dnscc_t *ep, char *buf)
34 {
35   unsigned l;
36   dnscc_t *cp = *cur;
37
38   l = *cp++;
39   if (cp + l > ep)
40     return DNS_E_PROTOCOL;
41   if (buf) {
42     memcpy(buf, cp, l);
43     buf[l] = '\0';
44   }
45   cp += l;
46
47   *cur = cp;
48   return l + 1;
49 }
50
51 int
52 dns_parse_naptr(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
53                 void **result) {
54   struct dns_rr_naptr *ret;
55   struct dns_parse p;
56   struct dns_rr rr;
57   int r, l;
58   char *sp;
59   dnsc_t dn[DNS_MAXDN];
60
61   assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_NAPTR);
62
63   /* first, validate the answer and count size of the result */
64   l = 0;
65   dns_initparse(&p, qdn, pkt, cur, end);
66   while((r = dns_nextrr(&p, &rr)) > 0) {
67     int i;
68     dnscc_t *ep = rr.dnsrr_dend;
69
70     /* first 4 bytes: order & preference */
71     cur = rr.dnsrr_dptr + 4;
72
73     /* flags, services and regexp */
74     for (i = 0; i < 3; i++) {
75       r = dns_getstr(&cur, ep, NULL);
76       if (r < 0)
77         return r;
78       l += r;
79     }
80     /* replacement */
81     r = dns_getdn(pkt, &cur, end, dn, sizeof(dn));
82     if (r <= 0 || cur != rr.dnsrr_dend)
83       return DNS_E_PROTOCOL;
84     l += dns_dntop_size(dn);
85   }
86   if (r < 0)
87     return DNS_E_PROTOCOL;
88   if (!p.dnsp_nrr)
89     return DNS_E_NODATA;
90
91   /* next, allocate and set up result */
92   l += dns_stdrr_size(&p);
93   ret = malloc(sizeof(*ret) + sizeof(struct dns_naptr) * p.dnsp_nrr + l);
94   if (!ret)
95     return DNS_E_NOMEM;
96   ret->dnsnaptr_nrr = p.dnsp_nrr;
97   ret->dnsnaptr_naptr = (struct dns_naptr *)(ret+1);
98
99   /* and 3rd, fill in result, finally */
100   sp = (char*)(&ret->dnsnaptr_naptr[p.dnsp_nrr]);
101   for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) {
102     cur = rr.dnsrr_dptr;
103     ret->dnsnaptr_naptr[r].order = dns_get16(cur); cur += 2;
104     ret->dnsnaptr_naptr[r].preference = dns_get16(cur); cur += 2;
105     sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].flags = sp));
106     sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].service = sp));
107     sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].regexp = sp));
108     dns_getdn(pkt, &cur, end, dn, sizeof(dn));
109     sp += dns_dntop(dn, (ret->dnsnaptr_naptr[r].replacement = sp), DNS_MAXNAME);
110   }
111   dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p);
112   *result = ret;
113   return 0;
114 }
115
116 struct dns_query *
117 dns_submit_naptr(struct dns_ctx *ctx, const char *name, int flags,
118                  dns_query_naptr_fn *cbck, void *data) {
119   return
120     dns_submit_p(ctx, name, DNS_C_IN, DNS_T_NAPTR, flags,
121                  dns_parse_naptr, (dns_query_fn *)cbck, data);
122 }
123
124 struct dns_rr_naptr *
125 dns_resolve_naptr(struct dns_ctx *ctx, const char *name, int flags) {
126   return (struct dns_rr_naptr *)
127     dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_NAPTR, flags, dns_parse_naptr);
128 }
Note: See TracBrowser for help on using the browser.