File: udns_rr_srv.c

package info (click to toggle)
udns 0.4-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch, trixie
  • size: 404 kB
  • ctags: 593
  • sloc: ansic: 4,243; makefile: 208; sh: 130
file content (155 lines) | stat: -rw-r--r-- 4,813 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* udns_rr_srv.c
   parse/query SRV IN (rfc2782) records

   Copyright (C) 2005  Michael Tokarev <mjt@corpit.ru>
   This file is part of UDNS library, an async DNS stub resolver.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library, in file named COPYING.LGPL; if not,
   write to the Free Software Foundation, Inc., 59 Temple Place,
   Suite 330, Boston, MA  02111-1307  USA

   Copyright 2005 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>

   2005-09-11:
   Changed MX parser file into a SRV parser file

 */

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "udns.h"

int
dns_parse_srv(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
              void **result) {
  struct dns_rr_srv *ret;
  struct dns_parse p;
  struct dns_rr rr;
  int r, l;
  char *sp;
  dnsc_t srv[DNS_MAXDN];

  assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_SRV);

  /* first, validate the answer and count size of the result */
  l = 0;
  dns_initparse(&p, qdn, pkt, cur, end);
  while((r = dns_nextrr(&p, &rr)) > 0) {
    cur = rr.dnsrr_dptr + 6;
    r = dns_getdn(pkt, &cur, end, srv, sizeof(srv));
    if (r <= 0 || cur != rr.dnsrr_dend)
      return DNS_E_PROTOCOL;
    l += dns_dntop_size(srv);
  }
  if (r < 0)
    return DNS_E_PROTOCOL;
  if (!p.dnsp_nrr)
    return DNS_E_NODATA;

  /* next, allocate and set up result */
  l += dns_stdrr_size(&p);
  ret = malloc(sizeof(*ret) + sizeof(struct dns_srv) * p.dnsp_nrr + l);
  if (!ret)
    return DNS_E_NOMEM;
  ret->dnssrv_nrr = p.dnsp_nrr;
  ret->dnssrv_srv = (struct dns_srv *)(ret+1);

  /* and 3rd, fill in result, finally */
  sp = (char*)(ret->dnssrv_srv + p.dnsp_nrr);
  for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) {
    ret->dnssrv_srv[r].name = sp;
    cur = rr.dnsrr_dptr;
    ret->dnssrv_srv[r].priority = dns_get16(cur);
    ret->dnssrv_srv[r].weight = dns_get16(cur+2);
    ret->dnssrv_srv[r].port = dns_get16(cur+4);
    cur += 6;
    dns_getdn(pkt, &cur, end, srv, sizeof(srv));
    sp += dns_dntop(srv, sp, DNS_MAXNAME);
  }
  dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p);
  *result = ret;
  return 0;
}

/* Add a single service or proto name prepending an undescore (_),
 * according to rfc2782 rules.
 * Return 0 or the label length.
 * Routing assumes dn holds enouth space for a single DN label. */
static int add_sname(dnsc_t *dn, const char *sn) {
  int l = dns_ptodn(sn, 0, dn + 1, DNS_MAXLABEL-1, NULL);
  if (l <= 1 || l - 2 != dn[1])
    /* Should we really check if sn is exactly one label?  Do we care? */
    return 0;
  dn[0] = l - 1;
  dn[1] = '_';
  return l;
}

/* Construct a domain name for SRV query from the given name, service and proto.
 * The code allows any combinations of srv and proto (both are non-NULL,
 * both NULL, or either one is non-NULL).  Whenever it makes any sense or not
 * is left as an exercise to programmer.
 * Return negative value on error (malformed query) or addition query flag(s).
 */
static int
build_srv_dn(dnsc_t *dn, const char *name, const char *srv, const char *proto)
{
  int p = 0, l, isabs;
  if (srv) {
    l = add_sname(dn + p, srv);
    if (!l)
      return -1;
    p += l;
  }
  if (proto) {
    l = add_sname(dn + p, proto);
    if (!l)
      return -1;
    p += l;
  }
  l = dns_ptodn(name, 0, dn + p, DNS_MAXDN - p, &isabs);
  if (l < 0)
    return -1;
  return isabs ? DNS_NOSRCH : 0;
}

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) {
  dnsc_t dn[DNS_MAXDN];
  int r = build_srv_dn(dn, name, srv, proto);
  if (r < 0) {
    dns_setstatus (ctx, DNS_E_BADQUERY);
    return NULL;
  }
  return
    dns_submit_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r,
                  dns_parse_srv, (dns_query_fn *)cbck, data);
}

struct dns_rr_srv *
dns_resolve_srv(struct dns_ctx *ctx,
                const char *name, const char *srv, const char *proto, int flags)
{
  dnsc_t dn[DNS_MAXDN];
  int r = build_srv_dn(dn, name, srv, proto);
  if (r < 0) {
    dns_setstatus(ctx, DNS_E_BADQUERY);
    return NULL;
  }
  return (struct dns_rr_srv *)
    dns_resolve_dn(ctx, dn, DNS_C_IN, DNS_T_SRV, flags | r, dns_parse_srv);
}