Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / irp_sv.c
bloba53dc66cf17fd4526a552ebc12a7ab1795e8d074
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996,1998 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: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
24 /* extern */
26 #include "port_before.h"
28 #include <syslog.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #ifdef IRS_LCL_SV_DB
33 #include <db.h>
34 #endif
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <syslog.h>
43 #include <irs.h>
44 #include <irp.h>
45 #include <isc/irpmarshall.h>
46 #include <isc/memcluster.h>
48 #include "irs_p.h"
49 #include "lcl_p.h"
50 #include "irp_p.h"
52 #include "port_after.h"
54 /* Types */
56 struct pvt {
57 struct irp_p *girpdata;
58 int warned;
59 struct servent service;
62 /* Forward */
64 static void sv_close(struct irs_sv*);
65 static struct servent * sv_next(struct irs_sv *);
66 static struct servent * sv_byname(struct irs_sv *, const char *,
67 const char *);
68 static struct servent * sv_byport(struct irs_sv *, int, const char *);
69 static void sv_rewind(struct irs_sv *);
70 static void sv_minimize(struct irs_sv *);
72 static void free_service(struct servent *sv);
76 /* Public */
78 /*%
79 * struct irs_sv * irs_irp_sv(struct irs_acc *this)
83 struct irs_sv *
84 irs_irp_sv(struct irs_acc *this) {
85 struct irs_sv *sv;
86 struct pvt *pvt;
88 if ((sv = memget(sizeof *sv)) == NULL) {
89 errno = ENOMEM;
90 return (NULL);
92 memset(sv, 0x0, sizeof *sv);
94 if ((pvt = memget(sizeof *pvt)) == NULL) {
95 memput(sv, sizeof *sv);
96 errno = ENOMEM;
97 return (NULL);
99 memset(pvt, 0, sizeof *pvt);
100 pvt->girpdata = this->private;
102 sv->private = pvt;
103 sv->close = sv_close;
104 sv->next = sv_next;
105 sv->byname = sv_byname;
106 sv->byport = sv_byport;
107 sv->rewind = sv_rewind;
108 sv->minimize = sv_minimize;
110 return (sv);
113 /* Methods */
116 * void sv_close(struct irs_sv *this)
120 static void
121 sv_close(struct irs_sv *this) {
122 struct pvt *pvt = (struct pvt *)this->private;
124 sv_minimize(this);
126 free_service(&pvt->service);
128 memput(pvt, sizeof *pvt);
129 memput(this, sizeof *this);
133 * Fills the cache if necessary and returns the next item from it.
137 static struct servent *
138 sv_next(struct irs_sv *this) {
139 struct pvt *pvt = (struct pvt *)this->private;
140 struct servent *sv = &pvt->service;
141 char *body;
142 size_t bodylen;
143 int code;
144 char text[256];
146 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
147 return (NULL);
150 if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
151 return (NULL);
154 if (irs_irp_get_full_response(pvt->girpdata, &code,
155 text, sizeof text,
156 &body, &bodylen) != 0) {
157 return (NULL);
160 if (code == IRPD_GETSERVICE_OK) {
161 free_service(sv);
162 if (irp_unmarshall_sv(sv, body) != 0) {
163 sv = NULL;
165 } else {
166 sv = NULL;
169 if (body != NULL) {
170 memput(body, bodylen);
173 return (sv);
177 * struct servent * sv_byname(struct irs_sv *this, const char *name,
178 * const char *proto)
182 static struct servent *
183 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
184 struct pvt *pvt = (struct pvt *)this->private;
185 struct servent *sv = &pvt->service;
186 char *body;
187 char text[256];
188 size_t bodylen;
189 int code;
191 if (sv->s_name != NULL &&
192 strcmp(name, sv->s_name) == 0 &&
193 strcasecmp(proto, sv->s_proto) == 0) {
194 return (sv);
197 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
198 return (NULL);
201 if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
202 name, proto) != 0)
203 return (NULL);
205 if (irs_irp_get_full_response(pvt->girpdata, &code,
206 text, sizeof text,
207 &body, &bodylen) != 0) {
208 return (NULL);
211 if (code == IRPD_GETSERVICE_OK) {
212 free_service(sv);
213 if (irp_unmarshall_sv(sv, body) != 0) {
214 sv = NULL;
216 } else {
217 sv = NULL;
220 if (body != NULL) {
221 memput(body, bodylen);
224 return (sv);
228 * struct servent * sv_byport(struct irs_sv *this, int port,
229 * const char *proto)
233 static struct servent *
234 sv_byport(struct irs_sv *this, int port, const char *proto) {
235 struct pvt *pvt = (struct pvt *)this->private;
236 struct servent *sv = &pvt->service;
237 char *body;
238 size_t bodylen;
239 char text[256];
240 int code;
242 if (sv->s_name != NULL &&
243 port == sv->s_port &&
244 strcasecmp(proto, sv->s_proto) == 0) {
245 return (sv);
248 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
249 return (NULL);
252 if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
253 ntohs((short)port), proto) != 0) {
254 return (NULL);
257 if (irs_irp_get_full_response(pvt->girpdata, &code,
258 text, sizeof text,
259 &body, &bodylen) != 0) {
260 return (NULL);
263 if (code == IRPD_GETSERVICE_OK) {
264 free_service(sv);
265 if (irp_unmarshall_sv(sv, body) != 0) {
266 sv = NULL;
268 } else {
269 sv = NULL;
272 if (body != NULL) {
273 memput(body, bodylen);
276 return (sv);
280 * void sv_rewind(struct irs_sv *this)
284 static void
285 sv_rewind(struct irs_sv *this) {
286 struct pvt *pvt = (struct pvt *)this->private;
287 char text[256];
288 int code;
290 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
291 return;
294 if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
295 return;
298 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
299 if (code != IRPD_GETSERVICE_SETOK) {
300 if (irp_log_errors) {
301 syslog(LOG_WARNING, "setservent failed: %s", text);
305 return;
309 * void sv_minimize(struct irs_sv *this)
313 static void
314 sv_minimize(struct irs_sv *this) {
315 struct pvt *pvt = (struct pvt *)this->private;
317 irs_irp_disconnect(pvt->girpdata);
325 static void
326 free_service(struct servent *sv) {
327 char **p;
329 if (sv == NULL) {
330 return;
333 if (sv->s_name != NULL) {
334 free(sv->s_name);
337 for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
338 free(*p);
341 if (sv->s_proto != NULL) {
342 free(sv->s_proto);
348 /*! \file */