2 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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
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 $";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <arpa/nameser.h>
41 #include <isc/memcluster.h>
44 #include "port_after.h"
56 struct __res_state * res;
57 void (*free_res)(void *);
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 *,
74 static struct servent * parse_hes_list(struct irs_sv *,
75 char **, const char *);
80 irs_dns_sv(struct irs_acc *this) {
81 struct dns_p *dns = (struct dns_p *)this->private;
85 if (!dns || !dns->hes_ctx) {
89 if (!(pvt = memget(sizeof *pvt))) {
93 memset(pvt, 0, sizeof *pvt);
95 if (!(sv = memget(sizeof *sv))) {
96 memput(pvt, sizeof *pvt);
100 memset(sv, 0x5e, sizeof *sv);
102 sv->byname = sv_byname;
103 sv->byport = sv_byport;
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;
116 sv_close(struct irs_sv *this) {
117 struct pvt *pvt = (struct pvt *)this->private;
119 if (pvt->serv.s_aliases)
120 free(pvt->serv.s_aliases);
124 if (pvt->res && pvt->free_res)
125 (*pvt->free_res)(pvt->res);
126 memput(pvt, sizeof *pvt);
127 memput(this, sizeof *this);
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;
137 if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
140 s = parse_hes_list(this, hes_list, proto);
141 hesiod_free_list(dns->hes_ctx, hes_list);
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;
153 sprintf(portstr, "%d", port);
154 if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
157 s = parse_hes_list(this, hes_list, proto);
158 hesiod_free_list(dns->hes_ctx, hes_list);
162 static struct servent *
163 sv_next(struct irs_sv *this) {
169 sv_rewind(struct irs_sv *this) {
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;
183 for (cpp = hes_list; *cpp; cpp++) {
186 /* Strip away comments, if any. */
187 if ((p = strchr(cp, '#')))
190 /* Check to make sure the protocol matches. */
192 while (*p && !isspace(*p))
196 proto_len = strlen(proto);
197 if (strncasecmp(++p, proto, proto_len) != 0)
199 if (p[proto_len] && !isspace(p[proto_len]))
202 /* OK, we've got a live one. Let's parse it for real. */
205 pvt->svbuf = strdup(cp);
208 pvt->serv.s_name = p;
209 while (*p && !isspace(*p))
215 pvt->serv.s_proto = p;
216 while (*p && !isspace(*p))
222 pvt->serv.s_port = htons((u_short) atoi(p));
223 while (*p && !isspace(*p))
229 if ((num + 1) >= max || !pvt->serv.s_aliases) {
231 new = realloc(pvt->serv.s_aliases,
232 max * sizeof(char *));
237 pvt->serv.s_aliases = new;
239 pvt->serv.s_aliases[num++] = p;
240 while (*p && !isspace(*p))
245 if (!pvt->serv.s_aliases)
246 pvt->serv.s_aliases = malloc(sizeof(char *));
247 if (!pvt->serv.s_aliases)
249 pvt->serv.s_aliases[num] = NULL;
254 if (pvt->serv.s_aliases) {
255 free(pvt->serv.s_aliases);
256 pvt->serv.s_aliases = NULL;
266 sv_minimize(struct irs_sv *this) {
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;
275 return (__hesiod_res_get(dns->hes_ctx));
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;
284 __hesiod_res_set(dns->hes_ctx, res, free_res);