dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libresolv2 / common / irs / dns_sv.c
blob7cfcdf05b55236cfe409fbc1bdc9ba4a59e19e68
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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp $";
20 #endif
22 /* Imports */
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <errno.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #include <resolv.h>
41 #include <isc/memcluster.h>
42 #include <irs.h>
44 #include "port_after.h"
46 #include "irs_p.h"
47 #include "hesiod.h"
48 #include "dns_p.h"
50 /* Definitions */
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 *);
60 /* Forward. */
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 #ifdef SV_RES_SETGET
70 static struct __res_state * sv_res_get(struct irs_sv *);
71 static void sv_res_set(struct irs_sv *,
72 struct __res_state *,
73 void (*)(void *));
74 #endif
76 static struct servent * parse_hes_list(struct irs_sv *,
77 char **, const char *);
79 /* Public */
81 struct irs_sv *
82 irs_dns_sv(struct irs_acc *this) {
83 struct dns_p *dns = (struct dns_p *)this->private;
84 struct irs_sv *sv;
85 struct pvt *pvt;
87 if (!dns || !dns->hes_ctx) {
88 errno = ENODEV;
89 return (NULL);
91 if (!(pvt = memget(sizeof *pvt))) {
92 errno = ENOMEM;
93 return (NULL);
95 memset(pvt, 0, sizeof *pvt);
96 pvt->dns = dns;
97 if (!(sv = memget(sizeof *sv))) {
98 memput(pvt, sizeof *pvt);
99 errno = ENOMEM;
100 return (NULL);
102 memset(sv, 0x5e, sizeof *sv);
103 sv->private = pvt;
104 sv->byname = sv_byname;
105 sv->byport = sv_byport;
106 sv->next = sv_next;
107 sv->rewind = sv_rewind;
108 sv->close = sv_close;
109 sv->minimize = sv_minimize;
110 #ifdef SV_RES_SETGET
111 sv->res_get = sv_res_get;
112 sv->res_set = sv_res_set;
113 #else
114 sv->res_get = NULL; /*%< sv_res_get; */
115 sv->res_set = NULL; /*%< sv_res_set; */
116 #endif
117 return (sv);
120 /* Methods */
122 static void
123 sv_close(struct irs_sv *this) {
124 struct pvt *pvt = (struct pvt *)this->private;
126 free(pvt->serv.s_aliases);
127 free(pvt->svbuf);
129 if (pvt->res && pvt->free_res)
130 (*pvt->free_res)(pvt->res);
131 memput(pvt, sizeof *pvt);
132 memput(this, sizeof *this);
135 static struct servent *
136 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
137 struct pvt *pvt = (struct pvt *)this->private;
138 struct dns_p *dns = pvt->dns;
139 struct servent *s;
140 char **hes_list;
142 if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
143 return (NULL);
145 s = parse_hes_list(this, hes_list, proto);
146 hesiod_free_list(dns->hes_ctx, hes_list);
147 return (s);
150 static struct servent *
151 sv_byport(struct irs_sv *this, int port, const char *proto) {
152 struct pvt *pvt = (struct pvt *)this->private;
153 struct dns_p *dns = pvt->dns;
154 struct servent *s;
155 char portstr[16];
156 char **hes_list;
158 sprintf(portstr, "%d", ntohs(port));
159 if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
160 return (NULL);
162 s = parse_hes_list(this, hes_list, proto);
163 hesiod_free_list(dns->hes_ctx, hes_list);
164 return (s);
167 static struct servent *
168 sv_next(struct irs_sv *this) {
169 UNUSED(this);
170 errno = ENODEV;
171 return (NULL);
174 static void
175 sv_rewind(struct irs_sv *this) {
176 UNUSED(this);
177 /* NOOP */
180 /* Private */
182 static struct servent *
183 parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
184 struct pvt *pvt = (struct pvt *)this->private;
185 char *p, *cp, **cpp, **new;
186 int proto_len;
187 int num = 0;
188 int max = 0;
190 for (cpp = hes_list; *cpp; cpp++) {
191 cp = *cpp;
193 /* Strip away comments, if any. */
194 if ((p = strchr(cp, '#')))
195 *p = 0;
197 /* Check to make sure the protocol matches. */
198 p = cp;
199 while (*p && !isspace((unsigned char)*p))
200 p++;
201 if (!*p)
202 continue;
203 if (proto) {
204 proto_len = strlen(proto);
205 if (strncasecmp(++p, proto, proto_len) != 0)
206 continue;
207 if (p[proto_len] && !isspace(p[proto_len]&0xff))
208 continue;
210 /* OK, we've got a live one. Let's parse it for real. */
211 free(pvt->svbuf);
212 pvt->svbuf = strdup(cp);
214 p = pvt->svbuf;
215 pvt->serv.s_name = p;
216 while (*p && !isspace(*p&0xff))
217 p++;
218 if (!*p)
219 continue;
220 *p++ = '\0';
222 pvt->serv.s_proto = p;
223 while (*p && !isspace(*p&0xff))
224 p++;
225 if (!*p)
226 continue;
227 *p++ = '\0';
229 pvt->serv.s_port = htons((u_short) atoi(p));
230 while (*p && !isspace(*p&0xff))
231 p++;
232 if (*p)
233 *p++ = '\0';
235 while (*p) {
236 if ((num + 1) >= max || !pvt->serv.s_aliases) {
237 max += 10;
238 new = reallocarray(pvt->serv.s_aliases, max,
239 sizeof (char *));
240 if (!new) {
241 errno = ENOMEM;
242 goto cleanup;
244 pvt->serv.s_aliases = new;
246 pvt->serv.s_aliases[num++] = p;
247 while (*p && !isspace(*p&0xff))
248 p++;
249 if (*p)
250 *p++ = '\0';
252 if (!pvt->serv.s_aliases)
253 pvt->serv.s_aliases = malloc(sizeof(char *));
254 if (!pvt->serv.s_aliases)
255 goto cleanup;
256 pvt->serv.s_aliases[num] = NULL;
257 return (&pvt->serv);
260 cleanup:
261 if (pvt->serv.s_aliases) {
262 free(pvt->serv.s_aliases);
263 pvt->serv.s_aliases = NULL;
265 if (pvt->svbuf) {
266 free(pvt->svbuf);
267 pvt->svbuf = NULL;
269 return (NULL);
272 static void
273 sv_minimize(struct irs_sv *this) {
274 UNUSED(this);
275 /* NOOP */
278 #ifdef SV_RES_SETGET
279 static struct __res_state *
280 sv_res_get(struct irs_sv *this) {
281 struct pvt *pvt = (struct pvt *)this->private;
282 struct dns_p *dns = pvt->dns;
284 return (__hesiod_res_get(dns->hes_ctx));
287 static void
288 sv_res_set(struct irs_sv *this, struct __res_state * res,
289 void (*free_res)(void *)) {
290 struct pvt *pvt = (struct pvt *)this->private;
291 struct dns_p *dns = pvt->dns;
293 __hesiod_res_set(dns->hes_ctx, res, free_res);
295 #endif
297 /*! \file */