4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp";
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <netinet/in.h>
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <arpa/nameser.h>
43 #include <isc/memcluster.h>
46 #include "port_after.h"
58 struct __res_state
* res
;
59 void (*free_res
)(void *);
64 static void sv_close(struct irs_sv
*);
65 static struct servent
* sv_byname(struct irs_sv
*,
66 const char *, const char *);
67 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
68 static struct servent
* sv_next(struct irs_sv
*);
69 static void sv_rewind(struct irs_sv
*);
70 static void sv_minimize(struct irs_sv
*);
72 static struct __res_state
* sv_res_get(struct irs_sv
*);
73 static void sv_res_set(struct irs_sv
*,
78 static struct servent
* parse_hes_list(struct irs_sv
*,
79 char **, const char *);
84 irs_dns_sv(struct irs_acc
*this) {
85 struct dns_p
*dns
= (struct dns_p
*)this->private;
89 if (!dns
|| !dns
->hes_ctx
) {
93 if (!(pvt
= memget(sizeof *pvt
))) {
97 memset(pvt
, 0, sizeof *pvt
);
99 if (!(sv
= memget(sizeof *sv
))) {
100 memput(pvt
, sizeof *pvt
);
104 memset(sv
, 0x5e, sizeof *sv
);
106 sv
->byname
= sv_byname
;
107 sv
->byport
= sv_byport
;
109 sv
->rewind
= sv_rewind
;
110 sv
->close
= sv_close
;
111 sv
->minimize
= sv_minimize
;
113 sv
->res_get
= sv_res_get
;
114 sv
->res_set
= sv_res_set
;
116 sv
->res_get
= NULL
; /*%< sv_res_get; */
117 sv
->res_set
= NULL
; /*%< sv_res_set; */
125 sv_close(struct irs_sv
*this) {
126 struct pvt
*pvt
= (struct pvt
*)this->private;
128 if (pvt
->serv
.s_aliases
)
129 free(pvt
->serv
.s_aliases
);
133 if (pvt
->res
&& pvt
->free_res
)
134 (*pvt
->free_res
)(pvt
->res
);
135 memput(pvt
, sizeof *pvt
);
136 memput(this, sizeof *this);
139 static struct servent
*
140 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
141 struct pvt
*pvt
= (struct pvt
*)this->private;
142 struct dns_p
*dns
= pvt
->dns
;
146 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, name
, "service")))
149 s
= parse_hes_list(this, hes_list
, proto
);
150 hesiod_free_list(dns
->hes_ctx
, hes_list
);
154 static struct servent
*
155 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
156 struct pvt
*pvt
= (struct pvt
*)this->private;
157 struct dns_p
*dns
= pvt
->dns
;
162 sprintf(portstr
, "%d", ntohs(port
));
163 if (!(hes_list
= hesiod_resolve(dns
->hes_ctx
, portstr
, "port")))
166 s
= parse_hes_list(this, hes_list
, proto
);
167 hesiod_free_list(dns
->hes_ctx
, hes_list
);
171 static struct servent
*
172 sv_next(struct irs_sv
*this) {
179 sv_rewind(struct irs_sv
*this) {
186 static struct servent
*
187 parse_hes_list(struct irs_sv
*this, char **hes_list
, const char *proto
) {
188 struct pvt
*pvt
= (struct pvt
*)this->private;
189 char *p
, *cp
, **cpp
, **new;
194 for (cpp
= hes_list
; *cpp
; cpp
++) {
197 /* Strip away comments, if any. */
198 if ((p
= strchr(cp
, '#')))
201 /* Check to make sure the protocol matches. */
203 while (*p
&& !isspace((unsigned char)*p
))
208 proto_len
= strlen(proto
);
209 if (strncasecmp(++p
, proto
, proto_len
) != 0)
211 if (p
[proto_len
] && !isspace(p
[proto_len
]&0xff))
214 /* OK, we've got a live one. Let's parse it for real. */
217 pvt
->svbuf
= strdup(cp
);
220 pvt
->serv
.s_name
= p
;
221 while (*p
&& !isspace(*p
&0xff))
227 pvt
->serv
.s_proto
= p
;
228 while (*p
&& !isspace(*p
&0xff))
234 pvt
->serv
.s_port
= htons((u_short
) atoi(p
));
235 while (*p
&& !isspace(*p
&0xff))
241 if ((num
+ 1) >= max
|| !pvt
->serv
.s_aliases
) {
243 new = realloc(pvt
->serv
.s_aliases
,
244 max
* sizeof(char *));
249 pvt
->serv
.s_aliases
= new;
251 pvt
->serv
.s_aliases
[num
++] = p
;
252 while (*p
&& !isspace(*p
&0xff))
257 if (!pvt
->serv
.s_aliases
)
258 pvt
->serv
.s_aliases
= malloc(sizeof(char *));
259 if (!pvt
->serv
.s_aliases
)
261 pvt
->serv
.s_aliases
[num
] = NULL
;
266 if (pvt
->serv
.s_aliases
) {
267 free(pvt
->serv
.s_aliases
);
268 pvt
->serv
.s_aliases
= NULL
;
278 sv_minimize(struct irs_sv
*this) {
284 static struct __res_state
*
285 sv_res_get(struct irs_sv
*this) {
286 struct pvt
*pvt
= (struct pvt
*)this->private;
287 struct dns_p
*dns
= pvt
->dns
;
289 return (__hesiod_res_get(dns
->hes_ctx
));
293 sv_res_set(struct irs_sv
*this, struct __res_state
* res
,
294 void (*free_res
)(void *)) {
295 struct pvt
*pvt
= (struct pvt
*)this->private;
296 struct dns_p
*dns
= pvt
->dns
;
298 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);