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
29 * @(#)getprotoent.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libc/net/getprotoent.c,v 1.9 2007/01/09 00:28:02 imp Exp $
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include "un-namespace.h"
46 #include "netdb_private.h"
52 static const ns_src defaultsrc[] = {
53 { NSSRC_FILES, NS_SUCCESS },
57 NETDB_THREAD_ALLOC(protoent_data)
58 NETDB_THREAD_ALLOC(protodata)
61 protoent_data_clear(struct protoent_data *ped)
70 protoent_data_free(void *ptr)
72 struct protoent_data *ped = ptr;
74 protoent_data_clear(ped);
79 protodata_free(void *ptr)
86 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
92 size_t desired_size, size;
93 enum nss_lookup_type lookup_type;
96 lookup_type = (enum nss_lookup_type)cache_mdata;
97 switch (lookup_type) {
99 name = va_arg(ap, char *);
102 desired_size = sizeof(enum nss_lookup_type) + size + 1;
103 if (desired_size > *buffer_size) {
108 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
109 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
114 proto = va_arg(ap, int);
116 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
117 if (desired_size > *buffer_size) {
122 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
123 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
129 /* should be unreachable */
134 *buffer_size = desired_size;
140 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval __unused,
141 va_list ap, void *cache_mdata)
145 struct protoent *proto;
146 char *orig_buf __unused;
147 size_t orig_buf_size __unused;
149 struct protoent new_proto;
150 size_t desired_size, size, aliases_size;
154 switch ((enum nss_lookup_type)cache_mdata) {
156 name = va_arg(ap, char *);
159 num = va_arg(ap, int);
164 /* should be unreachable */
168 proto = va_arg(ap, struct protoent *);
169 orig_buf = va_arg(ap, char *);
170 orig_buf_size = va_arg(ap, size_t);
172 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
173 if (proto->p_name != NULL)
174 desired_size += strlen(proto->p_name) + 1;
176 if (proto->p_aliases != NULL) {
178 for (alias = proto->p_aliases; *alias; ++alias) {
179 desired_size += strlen(*alias) + 1;
183 desired_size += _ALIGNBYTES + (aliases_size + 1) *
187 if (*buffer_size < desired_size) {
188 /* this assignment is here for future use */
189 *buffer_size = desired_size;
193 memcpy(&new_proto, proto, sizeof(struct protoent));
195 *buffer_size = desired_size;
196 memset(buffer, 0, desired_size);
197 p = buffer + sizeof(struct protoent) + sizeof(char *);
198 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
199 p = (char *)_ALIGN(p);
201 if (new_proto.p_name != NULL) {
202 size = strlen(new_proto.p_name);
203 memcpy(p, new_proto.p_name, size);
204 new_proto.p_name = p;
208 if (new_proto.p_aliases != NULL) {
209 p = (char *)_ALIGN(p);
210 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
211 new_proto.p_aliases = (char **)p;
212 p += sizeof(char *) * (aliases_size + 1);
214 for (alias = new_proto.p_aliases; *alias; ++alias) {
215 size = strlen(*alias);
216 memcpy(p, *alias, size);
222 memcpy(buffer, &new_proto, sizeof(struct protoent));
227 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
228 va_list ap, void *cache_mdata)
232 struct protoent *proto;
234 size_t orig_buf_size;
240 switch ((enum nss_lookup_type)cache_mdata) {
242 name = va_arg(ap, char *);
245 num = va_arg(ap, int);
250 /* should be unreachable */
254 proto = va_arg(ap, struct protoent *);
255 orig_buf = va_arg(ap, char *);
256 orig_buf_size = va_arg(ap, size_t);
257 ret_errno = va_arg(ap, int *);
260 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
265 memcpy(proto, buffer, sizeof(struct protoent));
266 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
268 orig_buf = (char *)_ALIGN(orig_buf);
269 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
270 _ALIGN(p) - (size_t)p,
271 buffer_size - sizeof(struct protoent) - sizeof(char *) -
272 _ALIGN(p) + (size_t)p);
273 p = (char *)_ALIGN(p);
275 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
276 if (proto->p_aliases != NULL) {
277 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
279 for (alias = proto->p_aliases; *alias; ++alias)
280 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
284 *((struct protoent **)retval) = proto;
289 NSS_MP_CACHE_HANDLING(protocols);
290 #endif /* NS_CACHING */
293 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
300 /* Find out the amount of space required to store the answer. */
301 numptr = 1; /* NULL ptr */
302 len = (char *)ALIGN(buf) - buf;
303 for (i = 0; pe->p_aliases[i]; i++, numptr++) {
304 len += strlen(pe->p_aliases[i]) + 1;
306 len += strlen(pe->p_name) + 1;
307 len += numptr * sizeof(char*);
309 if (len > (int)buflen) {
314 /* copy protocol value*/
315 pptr->p_proto = pe->p_proto;
317 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
319 /* copy official name */
320 n = strlen(pe->p_name) + 1;
321 strcpy(cp, pe->p_name);
326 pptr->p_aliases = (char **)ALIGN(buf);
327 for (i = 0 ; pe->p_aliases[i]; i++) {
328 n = strlen(pe->p_aliases[i]) + 1;
329 strcpy(cp, pe->p_aliases[i]);
330 pptr->p_aliases[i] = cp;
333 pptr->p_aliases[i] = NULL;
339 __setprotoent_p(int f, struct protoent_data *ped)
342 ped->fp = fopen(_PATH_PROTOCOLS, "r");
349 __endprotoent_p(struct protoent_data *ped)
359 __getprotoent_p(struct protoent *pe, struct protoent_data *ped)
362 char *cp, **q, *endp;
365 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL)
368 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
372 cp = strpbrk(p, "#\n");
376 cp = strpbrk(p, " \t");
380 while (*cp == ' ' || *cp == '\t')
382 p = strpbrk(cp, " \t");
385 l = strtol(cp, &endp, 10);
386 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
389 q = pe->p_aliases = ped->aliases;
393 if (*cp == ' ' || *cp == '\t') {
397 if (q < &ped->aliases[_MAXALIASES - 1])
399 cp = strpbrk(cp, " \t");
409 files_getprotoent_r(void *retval, void *mdata __unused, va_list ap)
412 struct protoent_data *ped;
414 struct protoent *pptr;
419 pptr = va_arg(ap, struct protoent *);
420 buffer = va_arg(ap, char *);
421 buflen = va_arg(ap, size_t);
422 errnop = va_arg(ap, int *);
424 if ((ped = __protoent_data_init()) == NULL)
427 if (__getprotoent_p(&pe, ped) != 0) {
429 return (NS_NOTFOUND);
432 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
434 return (NS_NOTFOUND);
437 *((struct protoent **)retval) = pptr;
442 files_setprotoent(void *retval __unused, void *mdata __unused, va_list ap)
444 struct protoent_data *ped;
448 if ((ped = __protoent_data_init()) == NULL)
451 __setprotoent_p(f, ped);
456 files_endprotoent(void *retval __unused, void *mdata __unused,
459 struct protoent_data *ped;
461 if ((ped = __protoent_data_init()) == NULL)
464 __endprotoent_p(ped);
469 getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
470 struct protoent **result)
473 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
474 protocols, (void *)nss_lt_all,
475 __proto_marshal_func, __proto_unmarshal_func);
477 static const ns_dtab dtab[] = {
478 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
480 NS_CACHE_CB(&cache_info)
488 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
489 defaultsrc, pptr, buffer, buflen, &ret_errno);
491 if (rv == NS_SUCCESS)
498 setprotoent(int stayopen)
501 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
502 protocols, (void *)nss_lt_all,
506 static const ns_dtab dtab[] = {
507 { NSSRC_FILES, files_setprotoent, NULL },
509 NS_CACHE_CB(&cache_info)
514 nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
522 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
523 protocols, (void *)nss_lt_all,
527 static const ns_dtab dtab[] = {
528 { NSSRC_FILES, files_endprotoent, NULL },
530 NS_CACHE_CB(&cache_info)
535 nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
541 struct protodata *pd;
542 struct protoent *rval;
544 if ((pd = __protodata_init()) == NULL)
546 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)