Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / dns_sv.c
bloba6456c1b4c7a405a2c9cb749ca8daf836a6c3035
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* Imports */
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <netdb.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <errno.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <resolv.h>
37 #include <isc/memcluster.h>
38 #include <irs.h>
40 #include "port_after.h"
42 #include "irs_p.h"
43 #include "hesiod.h"
44 #include "dns_p.h"
46 /* Definitions */
48 struct pvt {
49 struct dns_p * dns;
50 struct servent serv;
51 char * svbuf;
52 struct __res_state * res;
53 void (*free_res)(void *);
56 /* Forward. */
58 static void sv_close(struct irs_sv *);
59 static struct servent * sv_byname(struct irs_sv *,
60 const char *, const char *);
61 static struct servent * sv_byport(struct irs_sv *, int, const char *);
62 static struct servent * sv_next(struct irs_sv *);
63 static void sv_rewind(struct irs_sv *);
64 static void sv_minimize(struct irs_sv *);
65 #ifdef SV_RES_SETGET
66 static struct __res_state * sv_res_get(struct irs_sv *);
67 static void sv_res_set(struct irs_sv *,
68 struct __res_state *,
69 void (*)(void *));
70 #endif
72 static struct servent * parse_hes_list(struct irs_sv *,
73 char **, const char *);
75 /* Public */
77 struct irs_sv *
78 irs_dns_sv(struct irs_acc *this) {
79 struct dns_p *dns = (struct dns_p *)this->private;
80 struct irs_sv *sv;
81 struct pvt *pvt;
83 if (!dns || !dns->hes_ctx) {
84 errno = ENODEV;
85 return (NULL);
87 if (!(pvt = memget(sizeof *pvt))) {
88 errno = ENOMEM;
89 return (NULL);
91 memset(pvt, 0, sizeof *pvt);
92 pvt->dns = dns;
93 if (!(sv = memget(sizeof *sv))) {
94 memput(pvt, sizeof *pvt);
95 errno = ENOMEM;
96 return (NULL);
98 memset(sv, 0x5e, sizeof *sv);
99 sv->private = pvt;
100 sv->byname = sv_byname;
101 sv->byport = sv_byport;
102 sv->next = sv_next;
103 sv->rewind = sv_rewind;
104 sv->close = sv_close;
105 sv->minimize = sv_minimize;
106 #ifdef SV_RES_SETGET
107 sv->res_get = sv_res_get;
108 sv->res_set = sv_res_set;
109 #else
110 sv->res_get = NULL; /*%< sv_res_get; */
111 sv->res_set = NULL; /*%< sv_res_set; */
112 #endif
113 return (sv);
116 /* Methods */
118 static void
119 sv_close(struct irs_sv *this) {
120 struct pvt *pvt = (struct pvt *)this->private;
122 free(pvt->serv.s_aliases);
123 free(pvt->svbuf);
125 if (pvt->res && pvt->free_res)
126 (*pvt->free_res)(pvt->res);
127 memput(pvt, sizeof *pvt);
128 memput(this, sizeof *this);
131 static struct servent *
132 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
133 struct pvt *pvt = (struct pvt *)this->private;
134 struct dns_p *dns = pvt->dns;
135 struct servent *s;
136 char **hes_list;
138 if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
139 return (NULL);
141 s = parse_hes_list(this, hes_list, proto);
142 hesiod_free_list(dns->hes_ctx, hes_list);
143 return (s);
146 static struct servent *
147 sv_byport(struct irs_sv *this, int port, const char *proto) {
148 struct pvt *pvt = (struct pvt *)this->private;
149 struct dns_p *dns = pvt->dns;
150 struct servent *s;
151 char portstr[16];
152 char **hes_list;
154 sprintf(portstr, "%d", ntohs(port));
155 if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
156 return (NULL);
158 s = parse_hes_list(this, hes_list, proto);
159 hesiod_free_list(dns->hes_ctx, hes_list);
160 return (s);
163 static struct servent *
164 sv_next(struct irs_sv *this) {
165 UNUSED(this);
166 errno = ENODEV;
167 return (NULL);
170 static void
171 sv_rewind(struct irs_sv *this) {
172 UNUSED(this);
173 /* NOOP */
176 /* Private */
178 static struct servent *
179 parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
180 struct pvt *pvt = (struct pvt *)this->private;
181 char *p, *cp, **cpp, **new;
182 int proto_len;
183 int num = 0;
184 int max = 0;
186 for (cpp = hes_list; *cpp; cpp++) {
187 cp = *cpp;
189 /* Strip away comments, if any. */
190 if ((p = strchr(cp, '#')))
191 *p = 0;
193 /* Check to make sure the protocol matches. */
194 p = cp;
195 while (*p && !isspace((unsigned char)*p))
196 p++;
197 if (!*p)
198 continue;
199 if (proto) {
200 proto_len = strlen(proto);
201 if (strncasecmp(++p, proto, proto_len) != 0)
202 continue;
203 if (p[proto_len] && !isspace(p[proto_len]&0xff))
204 continue;
206 /* OK, we've got a live one. Let's parse it for real. */
207 free(pvt->svbuf);
208 pvt->svbuf = strdup(cp);
210 p = pvt->svbuf;
211 pvt->serv.s_name = p;
212 while (*p && !isspace(*p&0xff))
213 p++;
214 if (!*p)
215 continue;
216 *p++ = '\0';
218 pvt->serv.s_proto = p;
219 while (*p && !isspace(*p&0xff))
220 p++;
221 if (!*p)
222 continue;
223 *p++ = '\0';
225 pvt->serv.s_port = htons((u_short) atoi(p));
226 while (*p && !isspace(*p&0xff))
227 p++;
228 if (*p)
229 *p++ = '\0';
231 while (*p) {
232 if ((num + 1) >= max || !pvt->serv.s_aliases) {
233 max += 10;
234 new = reallocarray(pvt->serv.s_aliases, max,
235 sizeof (char *));
236 if (!new) {
237 errno = ENOMEM;
238 goto cleanup;
240 pvt->serv.s_aliases = new;
242 pvt->serv.s_aliases[num++] = p;
243 while (*p && !isspace(*p&0xff))
244 p++;
245 if (*p)
246 *p++ = '\0';
248 if (!pvt->serv.s_aliases)
249 pvt->serv.s_aliases = malloc(sizeof(char *));
250 if (!pvt->serv.s_aliases)
251 goto cleanup;
252 pvt->serv.s_aliases[num] = NULL;
253 return (&pvt->serv);
256 cleanup:
257 if (pvt->serv.s_aliases) {
258 free(pvt->serv.s_aliases);
259 pvt->serv.s_aliases = NULL;
261 if (pvt->svbuf) {
262 free(pvt->svbuf);
263 pvt->svbuf = NULL;
265 return (NULL);
268 static void
269 sv_minimize(struct irs_sv *this) {
270 UNUSED(this);
271 /* NOOP */
274 #ifdef SV_RES_SETGET
275 static struct __res_state *
276 sv_res_get(struct irs_sv *this) {
277 struct pvt *pvt = (struct pvt *)this->private;
278 struct dns_p *dns = pvt->dns;
280 return (__hesiod_res_get(dns->hes_ctx));
283 static void
284 sv_res_set(struct irs_sv *this, struct __res_state * res,
285 void (*free_res)(void *)) {
286 struct pvt *pvt = (struct pvt *)this->private;
287 struct dns_p *dns = pvt->dns;
289 __hesiod_res_set(dns->hes_ctx, res, free_res);
291 #endif
293 /*! \file */