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 * 4. 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/types.h>
38 #include <sys/socket.h>
46 #include "namespace.h"
47 #include "reentrant.h"
48 #include "un-namespace.h"
49 #include "netdb_private.h"
55 static const ns_src defaultsrc[] = {
56 { NSSRC_FILES, NS_SUCCESS },
60 NETDB_THREAD_ALLOC(protoent_data)
61 NETDB_THREAD_ALLOC(protodata)
64 protoent_data_clear(struct protoent_data *ped)
73 protoent_data_free(void *ptr)
75 struct protoent_data *ped = ptr;
77 protoent_data_clear(ped);
82 protodata_free(void *ptr)
89 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
95 size_t desired_size, size;
96 enum nss_lookup_type lookup_type;
99 lookup_type = (enum nss_lookup_type)cache_mdata;
100 switch (lookup_type) {
102 name = va_arg(ap, char *);
105 desired_size = sizeof(enum nss_lookup_type) + size + 1;
106 if (desired_size > *buffer_size) {
111 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
112 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
117 proto = va_arg(ap, int);
119 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
120 if (desired_size > *buffer_size) {
125 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
126 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
132 /* should be unreachable */
137 *buffer_size = desired_size;
143 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
144 va_list ap, void *cache_mdata)
148 struct protoent *proto;
150 size_t orig_buf_size;
152 struct protoent new_proto;
153 size_t desired_size, size, aliases_size;
157 switch ((enum nss_lookup_type)cache_mdata) {
159 name = va_arg(ap, char *);
162 num = va_arg(ap, int);
167 /* should be unreachable */
171 proto = va_arg(ap, struct protoent *);
172 orig_buf = va_arg(ap, char *);
173 orig_buf_size = va_arg(ap, size_t);
175 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
176 if (proto->p_name != NULL)
177 desired_size += strlen(proto->p_name) + 1;
179 if (proto->p_aliases != NULL) {
181 for (alias = proto->p_aliases; *alias; ++alias) {
182 desired_size += strlen(*alias) + 1;
186 desired_size += _ALIGNBYTES + (aliases_size + 1) *
190 if (*buffer_size < desired_size) {
191 /* this assignment is here for future use */
192 *buffer_size = desired_size;
196 memcpy(&new_proto, proto, sizeof(struct protoent));
198 *buffer_size = desired_size;
199 memset(buffer, 0, desired_size);
200 p = buffer + sizeof(struct protoent) + sizeof(char *);
201 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
202 p = (char *)_ALIGN(p);
204 if (new_proto.p_name != NULL) {
205 size = strlen(new_proto.p_name);
206 memcpy(p, new_proto.p_name, size);
207 new_proto.p_name = p;
211 if (new_proto.p_aliases != NULL) {
212 p = (char *)_ALIGN(p);
213 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
214 new_proto.p_aliases = (char **)p;
215 p += sizeof(char *) * (aliases_size + 1);
217 for (alias = new_proto.p_aliases; *alias; ++alias) {
218 size = strlen(*alias);
219 memcpy(p, *alias, size);
225 memcpy(buffer, &new_proto, sizeof(struct protoent));
230 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
231 va_list ap, void *cache_mdata)
235 struct protoent *proto;
237 size_t orig_buf_size;
243 switch ((enum nss_lookup_type)cache_mdata) {
245 name = va_arg(ap, char *);
248 num = va_arg(ap, int);
253 /* should be unreachable */
257 proto = va_arg(ap, struct protoent *);
258 orig_buf = va_arg(ap, char *);
259 orig_buf_size = va_arg(ap, size_t);
260 ret_errno = va_arg(ap, int *);
263 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
268 memcpy(proto, buffer, sizeof(struct protoent));
269 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
271 orig_buf = (char *)_ALIGN(orig_buf);
272 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
273 _ALIGN(p) - (size_t)p,
274 buffer_size - sizeof(struct protoent) - sizeof(char *) -
275 _ALIGN(p) + (size_t)p);
276 p = (char *)_ALIGN(p);
278 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
279 if (proto->p_aliases != NULL) {
280 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
282 for (alias = proto->p_aliases; *alias; ++alias)
283 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
287 *((struct protoent **)retval) = proto;
292 NSS_MP_CACHE_HANDLING(protocols);
293 #endif /* NS_CACHING */
296 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
303 /* Find out the amount of space required to store the answer. */
304 numptr = 1; /* NULL ptr */
305 len = (char *)ALIGN(buf) - buf;
306 for (i = 0; pe->p_aliases[i]; i++, numptr++) {
307 len += strlen(pe->p_aliases[i]) + 1;
309 len += strlen(pe->p_name) + 1;
310 len += numptr * sizeof(char*);
312 if (len > (int)buflen) {
317 /* copy protocol value*/
318 pptr->p_proto = pe->p_proto;
320 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
322 /* copy official name */
323 n = strlen(pe->p_name) + 1;
324 strcpy(cp, pe->p_name);
329 pptr->p_aliases = (char **)ALIGN(buf);
330 for (i = 0 ; pe->p_aliases[i]; i++) {
331 n = strlen(pe->p_aliases[i]) + 1;
332 strcpy(cp, pe->p_aliases[i]);
333 pptr->p_aliases[i] = cp;
336 pptr->p_aliases[i] = NULL;
342 __setprotoent_p(int f, struct protoent_data *ped)
345 ped->fp = fopen(_PATH_PROTOCOLS, "re");
352 __endprotoent_p(struct protoent_data *ped)
362 __getprotoent_p(struct protoent *pe, struct protoent_data *ped)
365 char *cp, **q, *endp;
368 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL)
371 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
375 cp = strpbrk(p, "#\n");
379 cp = strpbrk(p, " \t");
383 while (*cp == ' ' || *cp == '\t')
385 p = strpbrk(cp, " \t");
388 l = strtol(cp, &endp, 10);
389 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
392 q = pe->p_aliases = ped->aliases;
396 if (*cp == ' ' || *cp == '\t') {
400 if (q < &ped->aliases[_MAXALIASES - 1])
402 cp = strpbrk(cp, " \t");
412 files_getprotoent_r(void *retval, void *mdata, va_list ap)
415 struct protoent_data *ped;
417 struct protoent *pptr;
422 pptr = va_arg(ap, struct protoent *);
423 buffer = va_arg(ap, char *);
424 buflen = va_arg(ap, size_t);
425 errnop = va_arg(ap, int *);
427 if ((ped = __protoent_data_init()) == NULL) {
429 return (NS_NOTFOUND);
432 if (__getprotoent_p(&pe, ped) != 0) {
434 return (NS_NOTFOUND);
437 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
442 *((struct protoent **)retval) = pptr;
447 files_setprotoent(void *retval, void *mdata, va_list ap)
449 struct protoent_data *ped;
453 if ((ped = __protoent_data_init()) == NULL)
456 __setprotoent_p(f, ped);
461 files_endprotoent(void *retval, void *mdata, va_list ap)
463 struct protoent_data *ped;
465 if ((ped = __protoent_data_init()) == NULL)
468 __endprotoent_p(ped);
473 getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
474 struct protoent **result)
477 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
478 protocols, (void *)nss_lt_all,
479 __proto_marshal_func, __proto_unmarshal_func);
481 static const ns_dtab dtab[] = {
482 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
484 NS_CACHE_CB(&cache_info)
492 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
493 defaultsrc, pptr, buffer, buflen, &ret_errno);
495 if (rv != NS_SUCCESS) {
503 setprotoent(int stayopen)
506 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
507 protocols, (void *)nss_lt_all,
511 static const ns_dtab dtab[] = {
512 { NSSRC_FILES, files_setprotoent, NULL },
514 NS_CACHE_CB(&cache_info)
519 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
527 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
528 protocols, (void *)nss_lt_all,
532 static const ns_dtab dtab[] = {
533 { NSSRC_FILES, files_endprotoent, NULL },
535 NS_CACHE_CB(&cache_info)
540 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
546 struct protodata *pd;
547 struct protoent *rval;
549 if ((pd = __protodata_init()) == NULL)
551 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)