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.
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
37 #include <isc/memcluster.h>
40 #include "port_after.h"
52 struct __res_state
* res
;
53 void (*free_res
)(void *);
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
*);
66 static struct __res_state
* sv_res_get(struct irs_sv
*);
67 static void sv_res_set(struct irs_sv
*,
72 static struct servent
* parse_hes_list(struct irs_sv
*,
73 char **, const char *);
78 irs_dns_sv(struct irs_acc
*this) {
79 struct dns_p
*dns
= (struct dns_p
*)this->private;
83 if (!dns
|| !dns
->hes_ctx
) {
87 if (!(pvt
= memget(sizeof *pvt
))) {
91 memset(pvt
, 0, sizeof *pvt
);
93 if (!(sv
= memget(sizeof *sv
))) {
94 memput(pvt
, sizeof *pvt
);
98 memset(sv
, 0x5e, sizeof *sv
);
100 sv
->byname
= sv_byname
;
101 sv
->byport
= sv_byport
;
103 sv
->rewind
= sv_rewind
;
104 sv
->close
= sv_close
;
105 sv
->minimize
= sv_minimize
;
107 sv
->res_get
= sv_res_get
;
108 sv
->res_set
= sv_res_set
;
110 sv
->res_get
= NULL
; /*%< sv_res_get; */
111 sv
->res_set
= NULL
; /*%< sv_res_set; */
119 sv_close(struct irs_sv
*this) {
120 struct pvt
*pvt
= (struct pvt
*)this->private;
122 if (pvt
->serv
.s_aliases
)
123 free(pvt
->serv
.s_aliases
);
127 if (pvt
->res
&& pvt
->free_res
)
128 (*pvt
->free_res
)(pvt
->res
);
129 memput(pvt
, sizeof *pvt
);
130 memput(this, sizeof *this);
133 static struct servent
*
134 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
135 struct pvt
*pvt
= (struct pvt
*)this->private;
136 struct dns_p
*dns
= pvt
->dns
;
140 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "service")))
143 s
= parse_hes_list(this, hes_list
, proto
);
144 hesiod_free_list(dns
->hes_ctx
, hes_list
);
148 static struct servent
*
149 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
150 struct pvt
*pvt
= (struct pvt
*)this->private;
151 struct dns_p
*dns
= pvt
->dns
;
156 sprintf(portstr
, "%d", ntohs(port
));
157 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, portstr
, "port")))
160 s
= parse_hes_list(this, hes_list
, proto
);
161 hesiod_free_list(dns
->hes_ctx
, hes_list
);
165 static struct servent
*
166 sv_next(struct irs_sv
*this) {
173 sv_rewind(struct irs_sv
*this) {
180 static struct servent
*
181 parse_hes_list(struct irs_sv
*this, char **hes_list
, const char *proto
) {
182 struct pvt
*pvt
= (struct pvt
*)this->private;
183 char *p
, *cp
, **cpp
, **new;
188 for (cpp
= hes_list
; *cpp
; cpp
++) {
191 /* Strip away comments, if any. */
192 if ((p
= strchr(cp
, '#')))
195 /* Check to make sure the protocol matches. */
197 while (*p
&& !isspace((unsigned char)*p
))
202 proto_len
= strlen(proto
);
203 if (strncasecmp(++p
, proto
, proto_len
) != 0)
205 if (p
[proto_len
] && !isspace(p
[proto_len
]&0xff))
208 /* OK, we've got a live one. Let's parse it for real. */
211 pvt
->svbuf
= strdup(cp
);
214 pvt
->serv
.s_name
= p
;
215 while (*p
&& !isspace(*p
&0xff))
221 pvt
->serv
.s_proto
= p
;
222 while (*p
&& !isspace(*p
&0xff))
228 pvt
->serv
.s_port
= htons((u_short
) atoi(p
));
229 while (*p
&& !isspace(*p
&0xff))
235 if ((num
+ 1) >= max
|| !pvt
->serv
.s_aliases
) {
237 new = realloc(pvt
->serv
.s_aliases
,
238 max
* sizeof(char *));
243 pvt
->serv
.s_aliases
= new;
245 pvt
->serv
.s_aliases
[num
++] = p
;
246 while (*p
&& !isspace(*p
&0xff))
251 if (!pvt
->serv
.s_aliases
)
252 pvt
->serv
.s_aliases
= malloc(sizeof(char *));
253 if (!pvt
->serv
.s_aliases
)
255 pvt
->serv
.s_aliases
[num
] = NULL
;
260 if (pvt
->serv
.s_aliases
) {
261 free(pvt
->serv
.s_aliases
);
262 pvt
->serv
.s_aliases
= NULL
;
272 sv_minimize(struct irs_sv
*this) {
278 static struct __res_state
*
279 sv_res_get(struct irs_sv
*this) {
280 struct pvt
*pvt
= (struct pvt
*)this->private;
281 struct dns_p
*dns
= pvt
->dns
;
283 return (__hesiod_res_get(dns
->hes_ctx
));
287 sv_res_set(struct irs_sv
*this, struct __res_state
* res
,
288 void (*free_res
)(void *)) {
289 struct pvt
*pvt
= (struct pvt
*)this->private;
290 struct dns_p
*dns
= pvt
->dns
;
292 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);