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 $
31 * $DragonFly: src/lib/libc/net/getprotoent.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include "un-namespace.h"
47 #include "netdb_private.h"
53 static const ns_src defaultsrc[] = {
54 { NSSRC_FILES, NS_SUCCESS },
58 NETDB_THREAD_ALLOC(protoent_data)
59 NETDB_THREAD_ALLOC(protodata)
62 protoent_data_clear(struct protoent_data *ped)
71 protoent_data_free(void *ptr)
73 struct protoent_data *ped = ptr;
75 protoent_data_clear(ped);
80 protodata_free(void *ptr)
87 __proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
93 size_t desired_size, size;
94 enum nss_lookup_type lookup_type;
97 lookup_type = (enum nss_lookup_type)cache_mdata;
98 switch (lookup_type) {
100 name = va_arg(ap, char *);
103 desired_size = sizeof(enum nss_lookup_type) + size + 1;
104 if (desired_size > *buffer_size) {
109 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
110 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
115 proto = va_arg(ap, int);
117 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
118 if (desired_size > *buffer_size) {
123 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
124 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
130 /* should be unreachable */
135 *buffer_size = desired_size;
141 __proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
142 va_list ap, void *cache_mdata)
146 struct protoent *proto;
148 size_t orig_buf_size;
150 struct protoent new_proto;
151 size_t desired_size, size, aliases_size;
155 switch ((enum nss_lookup_type)cache_mdata) {
157 name = va_arg(ap, char *);
160 num = va_arg(ap, int);
165 /* should be unreachable */
169 proto = va_arg(ap, struct protoent *);
170 orig_buf = va_arg(ap, char *);
171 orig_buf_size = va_arg(ap, size_t);
173 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
174 if (proto->p_name != NULL)
175 desired_size += strlen(proto->p_name) + 1;
177 if (proto->p_aliases != NULL) {
179 for (alias = proto->p_aliases; *alias; ++alias) {
180 desired_size += strlen(*alias) + 1;
184 desired_size += _ALIGNBYTES + (aliases_size + 1) *
188 if (*buffer_size < desired_size) {
189 /* this assignment is here for future use */
190 *buffer_size = desired_size;
194 memcpy(&new_proto, proto, sizeof(struct protoent));
196 *buffer_size = desired_size;
197 memset(buffer, 0, desired_size);
198 p = buffer + sizeof(struct protoent) + sizeof(char *);
199 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
200 p = (char *)_ALIGN(p);
202 if (new_proto.p_name != NULL) {
203 size = strlen(new_proto.p_name);
204 memcpy(p, new_proto.p_name, size);
205 new_proto.p_name = p;
209 if (new_proto.p_aliases != NULL) {
210 p = (char *)_ALIGN(p);
211 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
212 new_proto.p_aliases = (char **)p;
213 p += sizeof(char *) * (aliases_size + 1);
215 for (alias = new_proto.p_aliases; *alias; ++alias) {
216 size = strlen(*alias);
217 memcpy(p, *alias, size);
223 memcpy(buffer, &new_proto, sizeof(struct protoent));
228 __proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
229 va_list ap, void *cache_mdata)
233 struct protoent *proto;
235 size_t orig_buf_size;
241 switch ((enum nss_lookup_type)cache_mdata) {
243 name = va_arg(ap, char *);
246 num = va_arg(ap, int);
251 /* should be unreachable */
255 proto = va_arg(ap, struct protoent *);
256 orig_buf = va_arg(ap, char *);
257 orig_buf_size = va_arg(ap, size_t);
258 ret_errno = va_arg(ap, int *);
261 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
266 memcpy(proto, buffer, sizeof(struct protoent));
267 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
269 orig_buf = (char *)_ALIGN(orig_buf);
270 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
271 _ALIGN(p) - (size_t)p,
272 buffer_size - sizeof(struct protoent) - sizeof(char *) -
273 _ALIGN(p) + (size_t)p);
274 p = (char *)_ALIGN(p);
276 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
277 if (proto->p_aliases != NULL) {
278 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
280 for (alias = proto->p_aliases; *alias; ++alias)
281 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
285 *((struct protoent **)retval) = proto;
290 NSS_MP_CACHE_HANDLING(protocols);
291 #endif /* NS_CACHING */
294 __copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
301 /* Find out the amount of space required to store the answer. */
302 numptr = 1; /* NULL ptr */
303 len = (char *)ALIGN(buf) - buf;
304 for (i = 0; pe->p_aliases[i]; i++, numptr++) {
305 len += strlen(pe->p_aliases[i]) + 1;
307 len += strlen(pe->p_name) + 1;
308 len += numptr * sizeof(char*);
310 if (len > (int)buflen) {
315 /* copy protocol value*/
316 pptr->p_proto = pe->p_proto;
318 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
320 /* copy official name */
321 n = strlen(pe->p_name) + 1;
322 strcpy(cp, pe->p_name);
327 pptr->p_aliases = (char **)ALIGN(buf);
328 for (i = 0 ; pe->p_aliases[i]; i++) {
329 n = strlen(pe->p_aliases[i]) + 1;
330 strcpy(cp, pe->p_aliases[i]);
331 pptr->p_aliases[i] = cp;
334 pptr->p_aliases[i] = NULL;
340 __setprotoent_p(int f, struct protoent_data *ped)
343 ped->fp = fopen(_PATH_PROTOCOLS, "r");
350 __endprotoent_p(struct protoent_data *ped)
360 __getprotoent_p(struct protoent *pe, struct protoent_data *ped)
363 char *cp, **q, *endp;
366 if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL)
369 if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
373 cp = strpbrk(p, "#\n");
377 cp = strpbrk(p, " \t");
381 while (*cp == ' ' || *cp == '\t')
383 p = strpbrk(cp, " \t");
386 l = strtol(cp, &endp, 10);
387 if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
390 q = pe->p_aliases = ped->aliases;
394 if (*cp == ' ' || *cp == '\t') {
398 if (q < &ped->aliases[_MAXALIASES - 1])
400 cp = strpbrk(cp, " \t");
410 files_getprotoent_r(void *retval, void *mdata, va_list ap)
413 struct protoent_data *ped;
415 struct protoent *pptr;
420 pptr = va_arg(ap, struct protoent *);
421 buffer = va_arg(ap, char *);
422 buflen = va_arg(ap, size_t);
423 errnop = va_arg(ap, int *);
425 if ((ped = __protoent_data_init()) == NULL)
428 if (__getprotoent_p(&pe, ped) != 0) {
430 return (NS_NOTFOUND);
433 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
435 return (NS_NOTFOUND);
438 *((struct protoent **)retval) = pptr;
443 files_setprotoent(void *retval, void *mdata, va_list ap)
445 struct protoent_data *ped;
449 if ((ped = __protoent_data_init()) == NULL)
452 __setprotoent_p(f, ped);
457 files_endprotoent(void *retval, void *mdata, va_list ap)
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)