2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/socket.h>
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include "un-namespace.h"
48 #include "netdb_private.h"
54 static const ns_src defaultsrc[] = {
55 { NSSRC_FILES, NS_SUCCESS },
59 NETDB_THREAD_ALLOC(protoent_data)
60 NETDB_THREAD_ALLOC(protodata)
63 protoent_data_clear(struct protoent_data *ped)
72 protoent_data_free(void *ptr)
74 struct protoent_data *ped = ptr;
76 protoent_data_clear(ped);
81 protodata_free(void *ptr)
88 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
94 size_t desired_size, size;
95 enum nss_lookup_type lookup_type;
98 lookup_type = (enum nss_lookup_type)cache_mdata;
99 switch (lookup_type) {
101 name = va_arg(ap, char *);
104 desired_size = sizeof(enum nss_lookup_type) + size + 1;
105 if (desired_size > *buffer_size) {
110 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
111 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
116 proto = va_arg(ap, int);
118 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
119 if (desired_size > *buffer_size) {
124 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
125 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
131 /* should be unreachable */
136 *buffer_size = desired_size;
142 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
143 va_list ap, void *cache_mdata)
147 struct protoent *proto;
149 size_t orig_buf_size;
151 struct protoent new_proto;
152 size_t desired_size, size, aliases_size;
156 switch ((enum nss_lookup_type)cache_mdata) {
158 name = va_arg(ap, char *);
161 num = va_arg(ap, int);
166 /* should be unreachable */
170 proto = va_arg(ap, struct protoent *);
171 orig_buf = va_arg(ap, char *);
172 orig_buf_size = va_arg(ap, size_t);
174 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
175 if (proto->p_name != NULL)
176 desired_size += strlen(proto->p_name) + 1;
178 if (proto->p_aliases != NULL) {
180 for (alias = proto->p_aliases; *alias; ++alias) {
181 desired_size += strlen(*alias) + 1;
185 desired_size += _ALIGNBYTES + (aliases_size + 1) *
189 if (*buffer_size < desired_size) {
190 /* this assignment is here for future use */
191 *buffer_size = desired_size;
195 memcpy(&new_proto, proto, sizeof(struct protoent));
197 *buffer_size = desired_size;
198 memset(buffer, 0, desired_size);
199 p = buffer + sizeof(struct protoent) + sizeof(char *);
200 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
201 p = (char *)_ALIGN(p);
203 if (new_proto.p_name != NULL) {
204 size = strlen(new_proto.p_name);
205 memcpy(p, new_proto.p_name, size);
206 new_proto.p_name = p;
210 if (new_proto.p_aliases != NULL) {
211 p = (char *)_ALIGN(p);
212 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
213 new_proto.p_aliases = (char **)p;
214 p += sizeof(char *) * (aliases_size + 1);
216 for (alias = new_proto.p_aliases; *alias; ++alias) {
217 size = strlen(*alias);
218 memcpy(p, *alias, size);
224 memcpy(buffer, &new_proto, sizeof(struct protoent));
229 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
230 va_list ap, void *cache_mdata)
234 struct protoent *proto;
236 size_t orig_buf_size;
242 switch ((enum nss_lookup_type)cache_mdata) {
244 name = va_arg(ap, char *);
247 num = va_arg(ap, int);
252 /* should be unreachable */
256 proto = va_arg(ap, struct protoent *);
257 orig_buf = va_arg(ap, char *);
258 orig_buf_size = va_arg(ap, size_t);
259 ret_errno = va_arg(ap, int *);
262 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
267 memcpy(proto, buffer, sizeof(struct protoent));
268 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
270 orig_buf = (char *)_ALIGN(orig_buf);
271 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
272 _ALIGN(p) - (size_t)p,
273 buffer_size - sizeof(struct protoent) - sizeof(char *) -
274 _ALIGN(p) + (size_t)p);
275 p = (char *)_ALIGN(p);
277 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
278 if (proto->p_aliases != NULL) {
279 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
281 for (alias = proto->p_aliases; *alias; ++alias)
282 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
286 *((struct protoent **)retval) = proto;
291 NSS_MP_CACHE_HANDLING(protocols);
292 #endif /* NS_CACHING */
295 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
302 /* Find out the amount of space required to store the answer. */
303 numptr = 1; /* NULL ptr */
304 len = (char *)ALIGN(buf) - buf;
305 for (i = 0; pe->p_aliases[i]; i++, numptr++) {
306 len += strlen(pe->p_aliases[i]) + 1;
308 len += strlen(pe->p_name) + 1;
309 len += numptr * sizeof(char*);
311 if (len > (int)buflen) {
316 /* copy protocol value*/
317 pptr->p_proto = pe->p_proto;
319 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
321 /* copy official name */
322 n = strlen(pe->p_name) + 1;
323 strcpy(cp, pe->p_name);
328 pptr->p_aliases = (char **)ALIGN(buf);
329 for (i = 0 ; pe->p_aliases[i]; i++) {
330 n = strlen(pe->p_aliases[i]) + 1;
331 strcpy(cp, pe->p_aliases[i]);
332 pptr->p_aliases[i] = cp;
335 pptr->p_aliases[i] = NULL;
341 __setprotoent_p(int f, struct protoent_data *ped)
344 ped->fp = fopen(_PATH_PROTOCOLS, "re");
351 __endprotoent_p(struct protoent_data *ped)
361 __getprotoent_p(struct protoent *pe, struct protoent_data *ped)
364 char *cp, **q, *endp;
367 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL)
370 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
374 cp = strpbrk(p, "#\n");
378 cp = strpbrk(p, " \t");
382 while (*cp == ' ' || *cp == '\t')
384 p = strpbrk(cp, " \t");
387 l = strtol(cp, &endp, 10);
388 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
391 q = pe->p_aliases = ped->aliases;
395 if (*cp == ' ' || *cp == '\t') {
399 if (q < &ped->aliases[_MAXALIASES - 1])
401 cp = strpbrk(cp, " \t");
411 files_getprotoent_r(void *retval, void *mdata, va_list ap)
414 struct protoent_data *ped;
416 struct protoent *pptr;
421 pptr = va_arg(ap, struct protoent *);
422 buffer = va_arg(ap, char *);
423 buflen = va_arg(ap, size_t);
424 errnop = va_arg(ap, int *);
426 if ((ped = __protoent_data_init()) == NULL) {
428 return (NS_NOTFOUND);
431 if (__getprotoent_p(&pe, ped) != 0) {
433 return (NS_NOTFOUND);
436 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
441 *((struct protoent **)retval) = pptr;
446 files_setprotoent(void *retval, void *mdata, va_list ap)
448 struct protoent_data *ped;
452 if ((ped = __protoent_data_init()) == NULL)
455 __setprotoent_p(f, ped);
460 files_endprotoent(void *retval, void *mdata, va_list ap)
462 struct protoent_data *ped;
464 if ((ped = __protoent_data_init()) == NULL)
467 __endprotoent_p(ped);
472 getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
473 struct protoent **result)
476 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
477 protocols, (void *)nss_lt_all,
478 __proto_marshal_func, __proto_unmarshal_func);
480 static const ns_dtab dtab[] = {
481 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
483 NS_CACHE_CB(&cache_info)
491 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
492 defaultsrc, pptr, buffer, buflen, &ret_errno);
494 if (rv != NS_SUCCESS) {
502 setprotoent(int stayopen)
505 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
506 protocols, (void *)nss_lt_all,
510 static const ns_dtab dtab[] = {
511 { NSSRC_FILES, files_setprotoent, NULL },
513 NS_CACHE_CB(&cache_info)
518 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
526 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
527 protocols, (void *)nss_lt_all,
531 static const ns_dtab dtab[] = {
532 { NSSRC_FILES, files_endprotoent, NULL },
534 NS_CACHE_CB(&cache_info)
539 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
545 struct protodata *pd;
546 struct protoent *rval;
548 if ((pd = __protodata_init()) == NULL)
550 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)