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 free(pvt
->serv
.s_aliases
);
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
;
142 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "service")))
145 s
= parse_hes_list(this, hes_list
, proto
);
146 hesiod_free_list(dns
->hes_ctx
, hes_list
);
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
;
158 sprintf(portstr
, "%d", ntohs(port
));
159 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, portstr
, "port")))
162 s
= parse_hes_list(this, hes_list
, proto
);
163 hesiod_free_list(dns
->hes_ctx
, hes_list
);
167 static struct servent
*
168 sv_next(struct irs_sv
*this) {
175 sv_rewind(struct irs_sv
*this) {
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;
190 for (cpp
= hes_list
; *cpp
; cpp
++) {
193 /* Strip away comments, if any. */
194 if ((p
= strchr(cp
, '#')))
197 /* Check to make sure the protocol matches. */
199 while (*p
&& !isspace((unsigned char)*p
))
204 proto_len
= strlen(proto
);
205 if (strncasecmp(++p
, proto
, proto_len
) != 0)
207 if (p
[proto_len
] && !isspace(p
[proto_len
]&0xff))
210 /* OK, we've got a live one. Let's parse it for real. */
212 pvt
->svbuf
= strdup(cp
);
215 pvt
->serv
.s_name
= p
;
216 while (*p
&& !isspace(*p
&0xff))
222 pvt
->serv
.s_proto
= p
;
223 while (*p
&& !isspace(*p
&0xff))
229 pvt
->serv
.s_port
= htons((u_short
) atoi(p
));
230 while (*p
&& !isspace(*p
&0xff))
236 if ((num
+ 1) >= max
|| !pvt
->serv
.s_aliases
) {
238 new = reallocarray(pvt
->serv
.s_aliases
, max
,
244 pvt
->serv
.s_aliases
= new;
246 pvt
->serv
.s_aliases
[num
++] = p
;
247 while (*p
&& !isspace(*p
&0xff))
252 if (!pvt
->serv
.s_aliases
)
253 pvt
->serv
.s_aliases
= malloc(sizeof(char *));
254 if (!pvt
->serv
.s_aliases
)
256 pvt
->serv
.s_aliases
[num
] = NULL
;
261 if (pvt
->serv
.s_aliases
) {
262 free(pvt
->serv
.s_aliases
);
263 pvt
->serv
.s_aliases
= NULL
;
273 sv_minimize(struct irs_sv
*this) {
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
));
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
);