1 /* udns_rr_srv.c
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
dns_parse_srv(dnscc_t * qdn,dnscc_t * pkt,dnscc_t * cur,dnscc_t * end,void ** result)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. */
add_sname(dnsc_t * dn,const char * sn)90 static int add_sname(dnsc_t *dn, const char *sn) {
91   int l = dns_ptodn(sn, 0, dn + 1, DNS_MAXLABEL-1, NULL);
92   if (l <= 1 || l - 2 != dn[1])
93     /* Should we really check if sn is exactly one label?  Do we care? */
94     return 0;
95   dn[0] = l - 1;
96   dn[1] = '_';
97   return l;
98 }
99 
100 /* Construct a domain name for SRV query from the given name, service and proto.
101  * The code allows any combinations of srv and proto (both are non-NULL,
102  * both NULL, or either one is non-NULL).  Whenever it makes any sense or not
103  * is left as an exercise to programmer.
104  * Return negative value on error (malformed query) or addition query flag(s).
105  */
106 static int
build_srv_dn(dnsc_t * dn,const char * name,const char * srv,const char * proto)107 build_srv_dn(dnsc_t *dn, const char *name, const char *srv, const char *proto)
108 {
109   int p = 0, l, isabs;
110   if (srv) {
111     l = add_sname(dn + p, srv);
112     if (!l)
113       return -1;
114     p += l;
115   }
116   if (proto) {
117     l = add_sname(dn + p, proto);
118     if (!l)
119       return -1;
120     p += l;
121   }
122   l = dns_ptodn(name, 0, dn + p, DNS_MAXDN - p, &isabs);
123   if (l < 0)
124     return -1;
125   return isabs ? DNS_NOSRCH : 0;
126 }
127 
128 struct dns_query *
dns_submit_srv(struct dns_ctx * ctx,const char * name,const char * srv,const char * proto,int flags,dns_query_srv_fn * cbck,void * data)129 dns_submit_srv(struct dns_ctx *ctx,
130                const char *name, const char *srv, const char *proto,
131                int flags, dns_query_srv_fn *cbck, void *data) {
132   dnsc_t dn[DNS_MAXDN];
133   int r = build_srv_dn(dn, name, srv, proto);
134   if (r < 0) {
135     dns_setstatus (ctx, DNS_E_BADQUERY);
136     return NULL;
137   }
138   return
139     dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r,
140                   dns_parse_srv, (dns_query_fn *)cbck, data);
141 }
142 
143 struct dns_rr_srv *
dns_resolve_srv(struct dns_ctx * ctx,const char * name,const char * srv,const char * proto,int flags)144 dns_resolve_srv(struct dns_ctx *ctx,
145                 const char *name, const char *srv, const char *proto, int flags)
146 {
147   dnsc_t dn[DNS_MAXDN];
148   int r = build_srv_dn(dn, name, srv, proto);
149   if (r < 0) {
150     dns_setstatus(ctx, DNS_E_BADQUERY);
151     return NULL;
152   }
153   return (struct dns_rr_srv *)
154     dns_resolve_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r, dns_parse_srv);
155 }
156