root/src/udns/udns_rr_srv.c

Revision 5a9b91bc0a14543e0995eba973fbeacc05aaee8b, 4.6 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_srv.c,v 1.2 2005/09/12 12:26:22 mjt Exp $
2    parse/query SRV IN (rfc2782) records
3
4    Copyright (C) 2005  Michael Tokarev <mjt@corpit.ru>
5    This file is part of UDNS library, an async DNS stub resolver.
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this library, in file named COPYING.LGPL; if not,
19    write to the Free Software Foundation, Inc., 59 Temple Place,
20    Suite 330, Boston, MA  02111-1307  USA
21
22    Copyright 2005 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
23
24    2005-09-11:
25    Changed MX parser file into a SRV parser file
26
27  */
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include "udns.h"
33
34 int
35 dns_parse_srv(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
36               void **result) {
37   struct dns_rr_srv *ret;
38   struct dns_parse p;
39   struct dns_rr rr;
40   int r, l;
41   char *sp;
42   dnsc_t srv[DNS_MAXDN];
43
44   assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_SRV);
45
46   /* first, validate the answer and count size of the result */
47   l = 0;
48   dns_initparse(&p, qdn, pkt, cur, end);
49   while((r = dns_nextrr(&p, &rr)) > 0) {
50     cur = rr.dnsrr_dptr + 6;
51     r = dns_getdn(pkt, &cur, end, srv, sizeof(srv));
52     if (r <= 0 || cur != rr.dnsrr_dend)
53       return DNS_E_PROTOCOL;
54     l += dns_dntop_size(srv);
55   }
56   if (r < 0)
57     return DNS_E_PROTOCOL;
58   if (!p.dnsp_nrr)
59     return DNS_E_NODATA;
60
61   /* next, allocate and set up result */
62   l += dns_stdrr_size(&p);
63   ret = malloc(sizeof(*ret) + sizeof(struct dns_srv) * p.dnsp_nrr + l);
64   if (!ret)
65     return DNS_E_NOMEM;
66   ret->dnssrv_nrr = p.dnsp_nrr;
67   ret->dnssrv_srv = (struct dns_srv *)(ret+1);
68
69   /* and 3rd, fill in result, finally */
70   sp = (char*)(ret->dnssrv_srv + p.dnsp_nrr);
71   for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) {
72     ret->dnssrv_srv[r].name = sp;
73     cur = rr.dnsrr_dptr;
74     ret->dnssrv_srv[r].priority = dns_get16(cur);
75     ret->dnssrv_srv[r].weight = dns_get16(cur+2);
76     ret->dnssrv_srv[r].port = dns_get16(cur+4);
77     cur += 6;
78     dns_getdn(pkt, &cur, end, srv, sizeof(srv));
79     sp += dns_dntop(srv, sp, DNS_MAXNAME);
80   }
81   dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p);
82   *result = ret;
83   return 0;
84 }
85
86 /* Add a single service or proto name prepending an undescore (_),
87  * according to rfc2782 rules.
88  * Return 0 or the label length.
89  * Routing assumes dn holds enouth space for a single DN label. */
90 static unsigned add_sname(dnsc_t *dn, const char *sn) {
91   unsigned l;
92   l = dns_ptodn(sn, 0, dn + 1, DNS_MAXLABEL-1, NULL);
93   if (l <= 1 || l - 2 != dn[1])
94     /* Should we really check if sn is exactly one label?  Do we care? */
95     return 0;
96   dn[0] = l - 1;
97   dn[1] = '_';
98   return l;
99 }
100
101 /* Construct a domain name for SRV query from the given name, service and
102  * protocol (service may be NULL in which case protocol isn't used).
103  * Return negative value on error (malformed query),
104  * or addition query flag(s) to use.
105  */
106 static int
107 build_srv_dn(dnsc_t *dn, const char *name, const char *srv, const char *proto)
108 {
109   unsigned p = 0, l;
110   int isabs;
111   if (srv) {
112     l = add_sname(dn + p, srv);
113     if (!l)
114       return -1;
115     p += l;
116     l = add_sname(dn + p, proto);
117     if (!l)
118       return -1;
119     p += l;
120   }
121   l = dns_ptodn(name, 0, dn + p, DNS_MAXDN - p, &isabs);
122   if (!l)
123     return -1;
124   return isabs ? DNS_NOSRCH : 0;
125 }
126
127 struct dns_query *
128 dns_submit_srv(struct dns_ctx *ctx,
129                const char *name, const char *srv, const char *proto,
130                int flags, dns_query_srv_fn *cbck, void *data) {
131   dnsc_t dn[DNS_MAXDN];
132   int r = build_srv_dn(dn, name, srv, proto);
133   if (r < 0) {
134     dns_setstatus (ctx, DNS_E_BADQUERY);
135     return NULL;
136   }
137   return
138     dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r,
139                   dns_parse_srv, (dns_query_fn *)cbck, data);
140 }
141
142 struct dns_rr_srv *
143 dns_resolve_srv(struct dns_ctx *ctx,
144                 const char *name, const char *srv, const char *proto, int flags)
145 {
146   dnsc_t dn[DNS_MAXDN];
147   int r = build_srv_dn(dn, name, srv, proto);
148   if (r < 0) {
149     dns_setstatus(ctx, DNS_E_BADQUERY);
150     return NULL;
151   }
152   return (struct dns_rr_srv *)
153     dns_resolve_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r, dns_parse_srv);
154 }
Note: See TracBrowser for help on using the browser.