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 $";
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
*);
70 static struct __res_state
* sv_res_get(struct irs_sv
*);
71 static void sv_res_set(struct irs_sv
*,
76 static struct servent
* parse_hes_list(struct irs_sv
*,
77 char **, const char *);
82 irs_dns_sv(struct irs_acc
*this) {
83 struct dns_p
*dns
= (struct dns_p
*)this->private;
87 if (!dns
|| !dns
->hes_ctx
) {
91 if (!(pvt
= memget(sizeof *pvt
))) {
95 memset(pvt
, 0, sizeof *pvt
);
97 if (!(sv
= memget(sizeof *sv
))) {
98 memput(pvt
, sizeof *pvt
);
102 memset(sv
, 0x5e, sizeof *sv
);
104 sv
->byname
= sv_byname
;
105 sv
->byport
= sv_byport
;
107 sv
->rewind
= sv_rewind
;
108 sv
->close
= sv_close
;
109 sv
->minimize
= sv_minimize
;
111 sv
->res_get
= sv_res_get
;
112 sv
->res_set
= sv_res_set
;
114 sv
->res_get
= NULL
; /*%< sv_res_get; */
115 sv
->res_set
= NULL
; /*%< sv_res_set; */
123 sv_close(struct irs_sv
*this) {
124 struct pvt
*pvt
= (struct pvt
*)this->private;
126 if (pvt
->serv
.s_aliases
)
127 free(pvt
->serv
.s_aliases
);
131 if (pvt
->res
&& pvt
->free_res
)
132 (*pvt
->free_res
)(pvt
->res
);
133 memput(pvt
, sizeof *pvt
);
134 memput(this, sizeof *this);
137 static struct servent
*
138 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
139 struct pvt
*pvt
= (struct pvt
*)this->private;
140 struct dns_p
*dns
= pvt
->dns
;
144 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "service")))
147 s
= parse_hes_list(this, hes_list
, proto
);
148 hesiod_free_list(dns
->hes_ctx
, hes_list
);
152 static struct servent
*
153 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
154 struct pvt
*pvt
= (struct pvt
*)this->private;
155 struct dns_p
*dns
= pvt
->dns
;
160 sprintf(portstr
, "%d", ntohs(port
));
161 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, portstr
, "port")))
164 s
= parse_hes_list(this, hes_list
, proto
);
165 hesiod_free_list(dns
->hes_ctx
, hes_list
);
169 static struct servent
*
170 sv_next(struct irs_sv
*this) {
177 sv_rewind(struct irs_sv
*this) {
184 static struct servent
*
185 parse_hes_list(struct irs_sv
*this, char **hes_list
, const char *proto
) {
186 struct pvt
*pvt
= (struct pvt
*)this->private;
187 char *p
, *cp
, **cpp
, **new;
192 for (cpp
= hes_list
; *cpp
; cpp
++) {
195 /* Strip away comments, if any. */
196 if ((p
= strchr(cp
, '#')))
199 /* Check to make sure the protocol matches. */
201 while (*p
&& !isspace((unsigned char)*p
))
206 proto_len
= strlen(proto
);
207 if (strncasecmp(++p
, proto
, proto_len
) != 0)
209 if (p
[proto_len
] && !isspace(p
[proto_len
]&0xff))
212 /* OK, we've got a live one. Let's parse it for real. */
215 pvt
->svbuf
= strdup(cp
);
218 pvt
->serv
.s_name
= p
;
219 while (*p
&& !isspace(*p
&0xff))
225 pvt
->serv
.s_proto
= p
;
226 while (*p
&& !isspace(*p
&0xff))
232 pvt
->serv
.s_port
= htons((u_short
) atoi(p
));
233 while (*p
&& !isspace(*p
&0xff))
239 if ((num
+ 1) >= max
|| !pvt
->serv
.s_aliases
) {
241 new = realloc(pvt
->serv
.s_aliases
,
242 max
* sizeof(char *));
247 pvt
->serv
.s_aliases
= new;
249 pvt
->serv
.s_aliases
[num
++] = p
;
250 while (*p
&& !isspace(*p
&0xff))
255 if (!pvt
->serv
.s_aliases
)
256 pvt
->serv
.s_aliases
= malloc(sizeof(char *));
257 if (!pvt
->serv
.s_aliases
)
259 pvt
->serv
.s_aliases
[num
] = NULL
;
264 if (pvt
->serv
.s_aliases
) {
265 free(pvt
->serv
.s_aliases
);
266 pvt
->serv
.s_aliases
= NULL
;
276 sv_minimize(struct irs_sv
*this) {
282 static struct __res_state
*
283 sv_res_get(struct irs_sv
*this) {
284 struct pvt
*pvt
= (struct pvt
*)this->private;
285 struct dns_p
*dns
= pvt
->dns
;
287 return (__hesiod_res_get(dns
->hes_ctx
));
291 sv_res_set(struct irs_sv
*this, struct __res_state
* res
,
292 void (*free_res
)(void *)) {
293 struct pvt
*pvt
= (struct pvt
*)this->private;
294 struct dns_p
*dns
= pvt
->dns
;
296 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);