]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bind/lib/irs/dns_sv.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / contrib / bind / lib / irs / dns_sv.c
1 /*
2  * Copyright (c) 1996,1999 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns_sv.c,v 1.17 1999/09/04 22:06:14 vixie Exp $";
20 #endif
21
22 /* Imports */
23
24 #include "port_before.h"
25
26 #include <sys/types.h>
27 #include <netinet/in.h>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <errno.h>
35
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #include <resolv.h>
40
41 #include <isc/memcluster.h>
42 #include <irs.h>
43
44 #include "port_after.h"
45
46 #include "irs_p.h"
47 #include "hesiod.h"
48 #include "dns_p.h"
49
50 /* Definitions */
51
52 struct pvt {
53         struct dns_p *          dns;
54         struct servent          serv;
55         char *                  svbuf;
56         struct __res_state *    res;
57         void                    (*free_res)(void *);
58 };
59
60 /* Forward. */
61
62 static void                     sv_close(struct irs_sv *);
63 static struct servent *         sv_byname(struct irs_sv *,
64                                           const char *, const char *);
65 static struct servent *         sv_byport(struct irs_sv *, int, const char *);
66 static struct servent *         sv_next(struct irs_sv *);
67 static void                     sv_rewind(struct irs_sv *);
68 static void                     sv_minimize(struct irs_sv *);
69 static struct __res_state *     sv_res_get(struct irs_sv *);
70 static void                     sv_res_set(struct irs_sv *,
71                                            struct __res_state *,
72                                            void (*)(void *));
73
74 static struct servent *         parse_hes_list(struct irs_sv *,
75                                                char **, const char *);
76
77 /* Public */
78
79 struct irs_sv *
80 irs_dns_sv(struct irs_acc *this) {
81         struct dns_p *dns = (struct dns_p *)this->private;
82         struct irs_sv *sv;
83         struct pvt *pvt;
84
85         if (!dns || !dns->hes_ctx) {
86                 errno = ENODEV;
87                 return (NULL);
88         }
89         if (!(pvt = memget(sizeof *pvt))) {
90                 errno = ENOMEM;
91                 return (NULL);
92         }
93         memset(pvt, 0, sizeof *pvt);
94         pvt->dns = dns;
95         if (!(sv = memget(sizeof *sv))) {
96                 memput(pvt, sizeof *pvt);
97                 errno = ENOMEM;
98                 return (NULL);
99         }
100         memset(sv, 0x5e, sizeof *sv);
101         sv->private = pvt;
102         sv->byname = sv_byname;
103         sv->byport = sv_byport;
104         sv->next = sv_next;
105         sv->rewind = sv_rewind;
106         sv->close = sv_close;
107         sv->minimize = sv_minimize;
108         sv->res_get = sv_res_get;
109         sv->res_set = sv_res_set;
110         return (sv);
111 }
112
113 /* Methods */
114
115 static void
116 sv_close(struct irs_sv *this) {
117         struct pvt *pvt = (struct pvt *)this->private;
118
119         if (pvt->serv.s_aliases)
120                 free(pvt->serv.s_aliases);
121         if (pvt->svbuf)
122                 free(pvt->svbuf);
123
124         if (pvt->res && pvt->free_res)
125                 (*pvt->free_res)(pvt->res);
126         memput(pvt, sizeof *pvt);
127         memput(this, sizeof *this);
128 }
129
130 static struct servent *
131 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
132         struct pvt *pvt = (struct pvt *)this->private;
133         struct dns_p *dns = pvt->dns;
134         struct servent *s;
135         char **hes_list;
136
137         if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
138                 return (NULL);
139
140         s = parse_hes_list(this, hes_list, proto);
141         hesiod_free_list(dns->hes_ctx, hes_list);
142         return (s);
143 }
144
145 static struct servent *
146 sv_byport(struct irs_sv *this, int port, const char *proto) {
147         struct pvt *pvt = (struct pvt *)this->private;
148         struct dns_p *dns = pvt->dns;
149         struct servent *s;
150         char portstr[16];
151         char **hes_list;
152
153         sprintf(portstr, "%d", port);
154         if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
155                 return (NULL);
156         
157         s = parse_hes_list(this, hes_list, proto);
158         hesiod_free_list(dns->hes_ctx, hes_list);
159         return (s);
160 }
161
162 static struct servent *
163 sv_next(struct irs_sv *this) {
164         errno = ENODEV;
165         return (NULL);
166 }
167
168 static void
169 sv_rewind(struct irs_sv *this) {
170         /* NOOP */
171 }
172
173 /* Private */
174
175 static struct servent *
176 parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
177         struct pvt *pvt = (struct pvt *)this->private;
178         char *p, *cp, **cpp, **new;
179         int proto_len;
180         int num = 0;
181         int max = 0;
182         
183         for (cpp = hes_list; *cpp; cpp++) {
184                 cp = *cpp;
185
186                 /* Strip away comments, if any. */
187                 if ((p = strchr(cp, '#')))
188                         *p = 0;
189
190                 /* Check to make sure the protocol matches. */
191                 p = cp;
192                 while (*p && !isspace(*p))
193                         p++;
194                 if (!*p)
195                         continue;
196                 proto_len = strlen(proto);
197                 if (strncasecmp(++p, proto, proto_len) != 0)
198                         continue;
199                 if (p[proto_len] && !isspace(p[proto_len]))
200                         continue;
201
202                 /* OK, we've got a live one.  Let's parse it for real. */
203                 if (pvt->svbuf)
204                         free(pvt->svbuf);
205                 pvt->svbuf = strdup(cp);
206
207                 p = pvt->svbuf;
208                 pvt->serv.s_name = p;
209                 while (*p && !isspace(*p))
210                         p++;
211                 if (!*p)
212                         continue;
213                 *p++ = '\0';
214
215                 pvt->serv.s_proto = p;
216                 while (*p && !isspace(*p))
217                         p++;
218                 if (!*p)
219                         continue;
220                 *p++ = '\0';
221
222                 pvt->serv.s_port = htons((u_short) atoi(p));
223                 while (*p && !isspace(*p))
224                         p++;
225                 if (*p)
226                         *p++ = '\0';
227
228                 while (*p) {
229                         if ((num + 1) >= max || !pvt->serv.s_aliases) {
230                                 max += 10;
231                                 new = realloc(pvt->serv.s_aliases,
232                                               max * sizeof(char *));
233                                 if (!new) {
234                                         errno = ENOMEM;
235                                         goto cleanup;
236                                 }
237                                 pvt->serv.s_aliases = new;
238                         }
239                         pvt->serv.s_aliases[num++] = p;
240                         while (*p && !isspace(*p))
241                                 p++;
242                         if (*p)
243                                 *p++ = '\0';
244                 }
245                 if (!pvt->serv.s_aliases)
246                         pvt->serv.s_aliases = malloc(sizeof(char *));
247                 if (!pvt->serv.s_aliases)
248                         goto cleanup;
249                 pvt->serv.s_aliases[num] = NULL;
250                 return (&pvt->serv);
251         }
252         
253  cleanup:
254         if (pvt->serv.s_aliases) {
255                 free(pvt->serv.s_aliases);
256                 pvt->serv.s_aliases = NULL;
257         }
258         if (pvt->svbuf) {
259                 free(pvt->svbuf);
260                 pvt->svbuf = NULL;
261         }
262         return (NULL);
263 }
264
265 static void
266 sv_minimize(struct irs_sv *this) {
267         /* NOOP */
268 }
269
270 static struct __res_state *
271 sv_res_get(struct irs_sv *this) {
272         struct pvt *pvt = (struct pvt *)this->private;
273         struct dns_p *dns = pvt->dns;
274
275         return (__hesiod_res_get(dns->hes_ctx));
276 }
277
278 static void
279 sv_res_set(struct irs_sv *this, struct __res_state * res,
280            void (*free_res)(void *)) {
281         struct pvt *pvt = (struct pvt *)this->private;
282         struct dns_p *dns = pvt->dns;
283
284         __hesiod_res_set(dns->hes_ctx, res, free_res);
285 }