Deleted Added
full compact
getprotoent.c (157779) getprotoent.c (158115)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 21 unchanged lines hidden (view full) ---

30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 21 unchanged lines hidden (view full) ---

30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/net/getprotoent.c 157779 2006-04-15 16:20:27Z ume $");
38__FBSDID("$FreeBSD: head/lib/libc/net/getprotoent.c 158115 2006-04-28 12:03:38Z ume $");
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <errno.h>
44#include <limits.h>
45#include <netdb.h>
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <errno.h>
44#include <limits.h>
45#include <netdb.h>
46#include <nsswitch.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include "namespace.h"
50#include "reentrant.h"
51#include "un-namespace.h"
52#include "netdb_private.h"
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include "namespace.h"
51#include "reentrant.h"
52#include "un-namespace.h"
53#include "netdb_private.h"
54#ifdef NS_CACHING
55#include "nscache.h"
56#endif
57#include "nss_tls.h"
53
58
59static const ns_src defaultsrc[] = {
60 { NSSRC_FILES, NS_SUCCESS },
61 { NULL, 0 }
62};
63
54NETDB_THREAD_ALLOC(protoent_data)
55NETDB_THREAD_ALLOC(protodata)
56
57static void
58protoent_data_clear(struct protoent_data *ped)
59{
60 if (ped->fp) {
61 fclose(ped->fp);

--- 11 unchanged lines hidden (view full) ---

73}
74
75static void
76protodata_free(void *ptr)
77{
78 free(ptr);
79}
80
64NETDB_THREAD_ALLOC(protoent_data)
65NETDB_THREAD_ALLOC(protodata)
66
67static void
68protoent_data_clear(struct protoent_data *ped)
69{
70 if (ped->fp) {
71 fclose(ped->fp);

--- 11 unchanged lines hidden (view full) ---

83}
84
85static void
86protodata_free(void *ptr)
87{
88 free(ptr);
89}
90
91#ifdef NS_CACHING
81int
92int
93__proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
94 void *cache_mdata)
95{
96 char *name;
97 int proto;
98
99 size_t desired_size, size;
100 enum nss_lookup_type lookup_type;
101 int res = NS_UNAVAIL;
102
103 lookup_type = (enum nss_lookup_type)cache_mdata;
104 switch (lookup_type) {
105 case nss_lt_name:
106 name = va_arg(ap, char *);
107
108 size = strlen(name);
109 desired_size = sizeof(enum nss_lookup_type) + size + 1;
110 if (desired_size > *buffer_size) {
111 res = NS_RETURN;
112 goto fin;
113 }
114
115 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
116 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
117
118 res = NS_SUCCESS;
119 break;
120 case nss_lt_id:
121 proto = va_arg(ap, int);
122
123 desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
124 if (desired_size > *buffer_size) {
125 res = NS_RETURN;
126 goto fin;
127 }
128
129 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
130 memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
131 sizeof(int));
132
133 res = NS_SUCCESS;
134 break;
135 default:
136 /* should be unreachable */
137 return (NS_UNAVAIL);
138 }
139
140fin:
141 *buffer_size = desired_size;
142 return (res);
143}
144
145
146int
147__proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
148 va_list ap, void *cache_mdata)
149{
150 char *name;
151 int num;
152 struct protoent *proto;
153 char *orig_buf;
154 size_t orig_buf_size;
155
156 struct protoent new_proto;
157 size_t desired_size, size, aliases_size;
158 char *p;
159 char **alias;
160
161 switch ((enum nss_lookup_type)cache_mdata) {
162 case nss_lt_name:
163 name = va_arg(ap, char *);
164 break;
165 case nss_lt_id:
166 num = va_arg(ap, int);
167 break;
168 case nss_lt_all:
169 break;
170 default:
171 /* should be unreachable */
172 return (NS_UNAVAIL);
173 }
174
175 proto = va_arg(ap, struct protoent *);
176 orig_buf = va_arg(ap, char *);
177 orig_buf_size = va_arg(ap, size_t);
178
179 desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
180 if (proto->p_name != NULL)
181 desired_size += strlen(proto->p_name) + 1;
182
183 if (proto->p_aliases != NULL) {
184 aliases_size = 0;
185 for (alias = proto->p_aliases; *alias; ++alias) {
186 desired_size += strlen(*alias) + 1;
187 ++aliases_size;
188 }
189
190 desired_size += _ALIGNBYTES + (aliases_size + 1) *
191 sizeof(char *);
192 }
193
194 if (*buffer_size < desired_size) {
195 /* this assignment is here for future use */
196 *buffer_size = desired_size;
197 return (NS_RETURN);
198 }
199
200 memcpy(&new_proto, proto, sizeof(struct protoent));
201
202 *buffer_size = desired_size;
203 memset(buffer, 0, desired_size);
204 p = buffer + sizeof(struct protoent) + sizeof(char *);
205 memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
206 p = (char *)_ALIGN(p);
207
208 if (new_proto.p_name != NULL) {
209 size = strlen(new_proto.p_name);
210 memcpy(p, new_proto.p_name, size);
211 new_proto.p_name = p;
212 p += size + 1;
213 }
214
215 if (new_proto.p_aliases != NULL) {
216 p = (char *)_ALIGN(p);
217 memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
218 new_proto.p_aliases = (char **)p;
219 p += sizeof(char *) * (aliases_size + 1);
220
221 for (alias = new_proto.p_aliases; *alias; ++alias) {
222 size = strlen(*alias);
223 memcpy(p, *alias, size);
224 *alias = p;
225 p += size + 1;
226 }
227 }
228
229 memcpy(buffer, &new_proto, sizeof(struct protoent));
230 return (NS_SUCCESS);
231}
232
233int
234__proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
235 va_list ap, void *cache_mdata)
236{
237 char *name;
238 int num;
239 struct protoent *proto;
240 char *orig_buf;
241 size_t orig_buf_size;
242 int *ret_errno;
243
244 char *p;
245 char **alias;
246
247 switch ((enum nss_lookup_type)cache_mdata) {
248 case nss_lt_name:
249 name = va_arg(ap, char *);
250 break;
251 case nss_lt_id:
252 num = va_arg(ap, int);
253 break;
254 case nss_lt_all:
255 break;
256 default:
257 /* should be unreachable */
258 return (NS_UNAVAIL);
259 }
260
261 proto = va_arg(ap, struct protoent *);
262 orig_buf = va_arg(ap, char *);
263 orig_buf_size = va_arg(ap, size_t);
264 ret_errno = va_arg(ap, int *);
265
266 if (orig_buf_size <
267 buffer_size - sizeof(struct protoent) - sizeof(char *)) {
268 *ret_errno = ERANGE;
269 return (NS_RETURN);
270 }
271
272 memcpy(proto, buffer, sizeof(struct protoent));
273 memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
274
275 orig_buf = (char *)_ALIGN(orig_buf);
276 memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
277 _ALIGN(p) - (size_t)p,
278 buffer_size - sizeof(struct protoent) - sizeof(char *) -
279 _ALIGN(p) + (size_t)p);
280 p = (char *)_ALIGN(p);
281
282 NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
283 if (proto->p_aliases != NULL) {
284 NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
285
286 for (alias = proto->p_aliases; *alias; ++alias)
287 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
288 }
289
290 if (retval != NULL)
291 *((struct protoent **)retval) = proto;
292
293 return (NS_SUCCESS);
294}
295
296NSS_MP_CACHE_HANDLING(protocols);
297#endif /* NS_CACHING */
298
299int
82__copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
83 size_t buflen)
84{
85 char *cp;
86 int i, n;
87 int numptr, len;
88
89 /* Find out the amount of space required to store the answer. */

--- 99 unchanged lines hidden (view full) ---

189 if (cp != NULL)
190 *cp++ = '\0';
191 }
192 }
193 *q = NULL;
194 return (0);
195}
196
300__copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
301 size_t buflen)
302{
303 char *cp;
304 int i, n;
305 int numptr, len;
306
307 /* Find out the amount of space required to store the answer. */

--- 99 unchanged lines hidden (view full) ---

407 if (cp != NULL)
408 *cp++ = '\0';
409 }
410 }
411 *q = NULL;
412 return (0);
413}
414
197int
198getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
199 struct protoent **result)
415static int
416files_getprotoent_r(void *retval, void *mdata, va_list ap)
200{
201 struct protoent pe;
202 struct protoent_data *ped;
203
417{
418 struct protoent pe;
419 struct protoent_data *ped;
420
421 struct protoent *pptr;
422 char *buffer;
423 size_t buflen;
424 int *errnop;
425
426 pptr = va_arg(ap, struct protoent *);
427 buffer = va_arg(ap, char *);
428 buflen = va_arg(ap, size_t);
429 errnop = va_arg(ap, int *);
430
204 if ((ped = __protoent_data_init()) == NULL)
205 return (-1);
206
431 if ((ped = __protoent_data_init()) == NULL)
432 return (-1);
433
207 if (__getprotoent_p(&pe, ped) != 0)
208 return (-1);
209 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0)
210 return (-1);
211 *result = pptr;
212 return (0);
434 if (__getprotoent_p(&pe, ped) != 0) {
435 *errnop = errno;
436 return (NS_NOTFOUND);
437 }
438
439 if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
440 *errnop = errno;
441 return (NS_NOTFOUND);
442 }
443
444 *((struct protoent **)retval) = pptr;
445 return (NS_SUCCESS);
213}
214
446}
447
215void
216setprotoent(int f)
448static int
449files_setprotoent(void *retval, void *mdata, va_list ap)
217{
218 struct protoent_data *ped;
450{
451 struct protoent_data *ped;
452 int f;
219
453
454 f = va_arg(ap, int);
220 if ((ped = __protoent_data_init()) == NULL)
455 if ((ped = __protoent_data_init()) == NULL)
221 return;
456 return (NS_UNAVAIL);
457
222 __setprotoent_p(f, ped);
458 __setprotoent_p(f, ped);
459 return (NS_UNAVAIL);
223}
224
460}
461
225void
226endprotoent(void)
462static int
463files_endprotoent(void *retval, void *mdata, va_list ap)
227{
228 struct protoent_data *ped;
229
230 if ((ped = __protoent_data_init()) == NULL)
464{
465 struct protoent_data *ped;
466
467 if ((ped = __protoent_data_init()) == NULL)
231 return;
468 return (NS_UNAVAIL);
469
232 __endprotoent_p(ped);
470 __endprotoent_p(ped);
471 return (NS_UNAVAIL);
233}
234
472}
473
474int
475getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
476 struct protoent **result)
477{
478#ifdef NS_CACHING
479 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
480 protocols, (void *)nss_lt_all,
481 __proto_marshal_func, __proto_unmarshal_func);
482#endif
483 static const ns_dtab dtab[] = {
484 { NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
485#ifdef NS_CACHING
486 NS_CACHE_CB(&cache_info)
487#endif
488 { NULL, NULL, NULL }
489 };
490 int rv, ret_errno;
491
492 ret_errno = 0;
493 *result = NULL;
494 rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
495 defaultsrc, pptr, buffer, buflen, &ret_errno);
496
497 if (rv == NS_SUCCESS)
498 return (0);
499 else
500 return (ret_errno);
501}
502
503void
504setprotoent(int stayopen)
505{
506#ifdef NS_CACHING
507 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
508 protocols, (void *)nss_lt_all,
509 NULL, NULL);
510#endif
511
512 static const ns_dtab dtab[] = {
513 { NSSRC_FILES, files_setprotoent, NULL },
514#ifdef NS_CACHING
515 NS_CACHE_CB(&cache_info)
516#endif
517 { NULL, NULL, NULL }
518 };
519
520 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
521 stayopen);
522}
523
524void
525endprotoent(void)
526{
527#ifdef NS_CACHING
528 static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
529 protocols, (void *)nss_lt_all,
530 NULL, NULL);
531#endif
532
533 static const ns_dtab dtab[] = {
534 { NSSRC_FILES, files_endprotoent, NULL },
535#ifdef NS_CACHING
536 NS_CACHE_CB(&cache_info)
537#endif
538 { NULL, NULL, NULL }
539 };
540
541 (void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
542}
543
235struct protoent *
236getprotoent(void)
237{
238 struct protodata *pd;
239 struct protoent *rval;
240
241 if ((pd = __protodata_init()) == NULL)
242 return (NULL);
243 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)
244 return (NULL);
245 return (rval);
246}
544struct protoent *
545getprotoent(void)
546{
547 struct protodata *pd;
548 struct protoent *rval;
549
550 if ((pd = __protodata_init()) == NULL)
551 return (NULL);
552 if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)
553 return (NULL);
554 return (rval);
555}