tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / rpc / getnetconfig.c
blob8414571122819f21662aa5b977cc59579427d7d5
1 /* $NetBSD: getnetconfig.c,v 1.21 2013/03/11 20:19:29 tron Exp $ */
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI";
38 #else
39 __RCSID("$NetBSD: getnetconfig.c,v 1.21 2013/03/11 20:19:29 tron Exp $");
40 #endif
41 #endif
44 * Copyright (c) 1989 by Sun Microsystems, Inc.
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <stdio.h>
50 #include <assert.h>
51 #include <errno.h>
52 #include <netconfig.h>
53 #include <stddef.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <rpc/rpc.h>
57 #include "rpc_internal.h"
59 #ifdef __weak_alias
60 __weak_alias(getnetconfig,_getnetconfig)
61 __weak_alias(setnetconfig,_setnetconfig)
62 __weak_alias(endnetconfig,_endnetconfig)
63 __weak_alias(getnetconfigent,_getnetconfigent)
64 __weak_alias(freenetconfigent,_freenetconfigent)
65 __weak_alias(nc_perror,_nc_perror)
66 __weak_alias(nc_sperror,_nc_sperror)
67 #endif
70 * The five library routines in this file provide application access to the
71 * system network configuration database, /etc/netconfig. In addition to the
72 * netconfig database and the routines for accessing it, the environment
73 * variable NETPATH and its corresponding routines in getnetpath.c may also be
74 * used to specify the network transport to be used.
79 * netconfig errors
82 #define NC_NONETCONFIG ENOENT
83 #define NC_NOMEM ENOMEM
84 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */
85 #define NC_BADFILE EBADF /* format for netconfig file is bad */
88 * semantics as strings (should be in netconfig.h)
90 #define NC_TPI_CLTS_S "tpi_clts"
91 #define NC_TPI_COTS_S "tpi_cots"
92 #define NC_TPI_COTS_ORD_S "tpi_cots_ord"
93 #define NC_TPI_RAW_S "tpi_raw"
96 * flags as characters (also should be in netconfig.h)
98 #define NC_NOFLAG_C '-'
99 #define NC_VISIBLE_C 'v'
100 #define NC_BROADCAST_C 'b'
103 * Character used to indicate there is no name-to-address lookup library
105 #define NC_NOLOOKUP "-"
107 static const char * const _nc_errors[] = {
108 "Netconfig database not found",
109 "Not enough memory",
110 "Not initialized",
111 "Netconfig database has invalid format"
114 struct netconfig_info {
115 int eof; /* all entries has been read */
116 int ref; /* # of times setnetconfig() has been called */
117 struct netconfig_list *head; /* head of the list */
118 struct netconfig_list *tail; /* last of the list */
121 struct netconfig_list {
122 char *linep; /* hold line read from netconfig */
123 struct netconfig *ncp;
124 struct netconfig_list *next;
127 struct netconfig_vars {
128 int valid; /* token that indicates valid netconfig_vars */
129 int flag; /* first time flag */
130 struct netconfig_list *nc_configs;
131 /* pointer to the current netconfig entry */
134 #define NC_VALID 0xfeed
135 #define NC_STORAGE 0xf00d
136 #define NC_INVALID 0
139 static int *__nc_error(void);
140 static int parse_ncp(char *, struct netconfig *);
141 static struct netconfig *dup_ncp(struct netconfig *);
144 static FILE *nc_file; /* for netconfig db */
145 static struct netconfig_info ni = { 0, 0, NULL, NULL};
147 #define MAXNETCONFIGLINE 1000
149 #ifdef _REENTRANT
150 static thread_key_t nc_key;
151 static once_t nc_once = ONCE_INITIALIZER;
153 static void
154 __nc_error_setup(void)
156 thr_keycreate(&nc_key, free);
158 #endif
160 static int *
161 __nc_error(void)
163 #ifdef _REENTRANT
164 int *nc_addr = NULL;
165 #endif
166 static int nc_error = 0;
168 #ifdef _REENTRANT
169 if (__isthreaded == 0)
170 return &nc_error;
171 thr_once(&nc_once, __nc_error_setup);
172 nc_addr = thr_getspecific(nc_key) ;
173 if (nc_addr == NULL) {
174 nc_addr = malloc(sizeof (int));
175 if (nc_addr == NULL)
176 return &nc_error;
177 if (thr_setspecific(nc_key, (void *) nc_addr) != 0) {
178 if (nc_addr)
179 free(nc_addr);
180 return &nc_error;
182 *nc_addr = 0;
184 return nc_addr;
185 #else
186 return &nc_error;
187 #endif
190 #define nc_error (*(__nc_error()))
192 * A call to setnetconfig() establishes a /etc/netconfig "session". A session
193 * "handle" is returned on a successful call. At the start of a session (after
194 * a call to setnetconfig()) searches through the /etc/netconfig database will
195 * proceed from the start of the file. The session handle must be passed to
196 * getnetconfig() to parse the file. Each call to getnetconfig() using the
197 * current handle will process one subsequent entry in /etc/netconfig.
198 * setnetconfig() must be called before the first call to getnetconfig().
199 * (Handles are used to allow for nested calls to setnetpath()).
201 * A new session is established with each call to setnetconfig(), with a new
202 * handle being returned on each call. Previously established sessions remain
203 * active until endnetconfig() is called with that session's handle as an
204 * argument.
206 * setnetconfig() need *not* be called before a call to getnetconfigent().
207 * setnetconfig() returns a NULL pointer on failure (for example, if
208 * the netconfig database is not present).
210 void *
211 setnetconfig(void)
213 struct netconfig_vars *nc_vars;
215 if ((nc_vars = malloc(sizeof(*nc_vars))) == NULL) {
216 return(NULL);
220 * For multiple calls, i.e. nc_file is not NULL, we just return the
221 * handle without reopening the netconfig db.
223 ni.ref++;
224 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "re")) != NULL) {
225 nc_vars->valid = NC_VALID;
226 nc_vars->flag = 0;
227 nc_vars->nc_configs = ni.head;
228 return ((void *)nc_vars);
230 ni.ref--;
231 nc_error = NC_NONETCONFIG;
232 free(nc_vars);
233 return (NULL);
238 * When first called, getnetconfig() returns a pointer to the first entry in
239 * the netconfig database, formatted as a struct netconfig. On each subsequent
240 * call, getnetconfig() returns a pointer to the next entry in the database.
241 * getnetconfig() can thus be used to search the entire netconfig file.
242 * getnetconfig() returns NULL at end of file.
245 struct netconfig *
246 getnetconfig(void *handlep)
248 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
249 char *stringp; /* tmp string pointer */
250 struct netconfig_list *list;
251 struct netconfig *np;
254 * Verify that handle is valid
256 if (ncp == NULL || nc_file == NULL) {
257 nc_error = NC_NOTINIT;
258 return (NULL);
261 switch (ncp->valid) {
262 case NC_VALID:
264 * If entry has already been read into the list,
265 * we return the entry in the linked list.
266 * If this is the first time call, check if there are any
267 * entries in linked list. If no entries, we need to read the
268 * netconfig db.
269 * If we have been here and the next entry is there, we just
270 * return it.
272 if (ncp->flag == 0) { /* first time */
273 ncp->flag = 1;
274 ncp->nc_configs = ni.head;
275 if (ncp->nc_configs != NULL) /* entry already exist */
276 return(ncp->nc_configs->ncp);
278 else if (ncp->nc_configs != NULL &&
279 ncp->nc_configs->next != NULL) {
280 ncp->nc_configs = ncp->nc_configs->next;
281 return(ncp->nc_configs->ncp);
285 * If we cannot find the entry in the list and is end of file,
286 * we give up.
288 if (ni.eof == 1)
289 return(NULL);
290 break;
291 default:
292 nc_error = NC_NOTINIT;
293 return (NULL);
296 stringp = malloc(MAXNETCONFIGLINE);
297 if (stringp == NULL)
298 return (NULL);
300 #ifdef MEM_CHK
301 if (malloc_verify() == 0) {
302 fprintf(stderr, "memory heap corrupted in getnetconfig\n");
303 exit(1);
305 #endif
308 * Read a line from netconfig file.
310 do {
311 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
312 free(stringp);
313 ni.eof = 1;
314 return (NULL);
316 } while (*stringp == '#');
318 list = malloc(sizeof(*list));
319 if (list == NULL) {
320 free(stringp);
321 return(NULL);
323 np = malloc(sizeof(*np));
324 if (np == NULL) {
325 free(stringp);
326 free(list);
327 return(NULL);
329 list->ncp = np;
330 list->next = NULL;
331 list->ncp->nc_lookups = NULL;
332 list->linep = stringp;
333 if (parse_ncp(stringp, list->ncp) == -1) {
334 free(stringp);
335 free(np);
336 free(list);
337 return (NULL);
338 } else {
340 * If this is the first entry that's been read, it is the
341 * head of the list. If not, put the entry at the end of
342 * the list. Reposition the current pointer of the handle to
343 * the last entry in the list.
345 if (ni.head == NULL) /* first entry */
346 ni.head = ni.tail = list;
347 else {
348 ni.tail->next = list;
349 ni.tail = ni.tail->next;
351 ncp->nc_configs = ni.tail;
352 return(ni.tail->ncp);
357 * endnetconfig() may be called to "unbind" or "close" the netconfig database
358 * when processing is complete, releasing resources for reuse. endnetconfig()
359 * may not be called before setnetconfig(). endnetconfig() returns 0 on
360 * success and -1 on failure (for example, if setnetconfig() was not called
361 * previously).
364 endnetconfig(void *handlep)
366 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
368 struct netconfig_list *q, *p;
371 * Verify that handle is valid
373 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
374 nc_handlep->valid != NC_STORAGE)) {
375 nc_error = NC_NOTINIT;
376 return (-1);
380 * Return 0 if anyone still needs it.
382 nc_handlep->valid = NC_INVALID;
383 nc_handlep->flag = 0;
384 nc_handlep->nc_configs = NULL;
385 if (--ni.ref > 0) {
386 free(nc_handlep);
387 return(0);
391 * Noone needs these entries anymore, then frees them.
392 * Make sure all info in netconfig_info structure has been
393 * reinitialized.
395 q = p = ni.head;
396 ni.eof = ni.ref = 0;
397 ni.head = NULL;
398 ni.tail = NULL;
399 while (q) {
400 p = q->next;
401 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
402 free(q->ncp);
403 free(q->linep);
404 free(q);
405 q = p;
407 free(nc_handlep);
409 fclose(nc_file);
410 nc_file = NULL;
411 return (0);
415 * getnetconfigent(netid) returns a pointer to the struct netconfig structure
416 * corresponding to netid. It returns NULL if netid is invalid (that is, does
417 * not name an entry in the netconfig database). It returns NULL and sets
418 * errno in case of failure (for example, if the netconfig database cannot be
419 * opened).
422 struct netconfig *
423 getnetconfigent(const char *netid)
425 FILE *file; /* NETCONFIG db's file pointer */
426 char *linep; /* holds current netconfig line */
427 char *stringp; /* temporary string pointer */
428 struct netconfig *ncp = NULL; /* returned value */
429 struct netconfig_list *list; /* pointer to cache list */
431 if (netid == NULL || strlen(netid) == 0)
432 return (NULL);
435 * Look up table if the entries have already been read and parsed in
436 * getnetconfig(), then copy this entry into a buffer and return it.
437 * If we cannot find the entry in the current list and there are more
438 * entries in the netconfig db that has not been read, we then read the
439 * db and try find the match netid.
440 * If all the netconfig db has been read and placed into the list and
441 * there is no match for the netid, return NULL.
443 if (ni.head != NULL) {
444 for (list = ni.head; list; list = list->next) {
445 if (strcmp(list->ncp->nc_netid, netid) == 0)
446 return(dup_ncp(list->ncp));
448 if (ni.eof == 1) /* that's all the entries */
449 return(NULL);
452 if ((file = fopen(NETCONFIG, "r")) == NULL)
453 return (NULL);
455 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
456 fclose(file);
457 return (NULL);
459 do {
460 ptrdiff_t len;
461 char *tmpp; /* tmp string pointer */
463 do {
464 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file))
465 == NULL)
466 break;
467 } while (*stringp == '#');
468 if (stringp == NULL) /* eof */
469 break;
470 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) {
471 /* can't parse file */
472 nc_error = NC_BADFILE;
473 break;
475 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */
476 strncmp(stringp, netid, (size_t)len) == 0) {
477 if ((ncp = malloc(sizeof(*ncp))) == NULL)
478 break;
479 ncp->nc_lookups = NULL;
480 if (parse_ncp(linep, ncp) == -1) {
481 free(ncp);
482 ncp = NULL;
484 break;
486 } while (stringp != NULL);
487 if (ncp == NULL)
488 free(linep);
489 fclose(file);
490 return(ncp);
494 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
495 * netconfigp (previously returned by getnetconfigent()).
498 void
499 freenetconfigent(struct netconfig *netconfigp)
501 if (netconfigp != NULL) {
502 /* holds all netconfigp's strings */
503 free(netconfigp->nc_netid);
504 if (netconfigp->nc_lookups != NULL)
505 free(netconfigp->nc_lookups);
506 free(netconfigp);
511 * Parse line and stuff it in a struct netconfig
512 * Typical line might look like:
513 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
515 * We return -1 if any of the tokens don't parse, or malloc fails.
517 * Note that we modify stringp (putting NULLs after tokens) and
518 * we set the ncp's string field pointers to point to these tokens within
519 * stringp.
522 static int
523 parse_ncp(
524 char *stringp, /* string to parse */
525 struct netconfig *ncp) /* where to put results */
527 char *tokenp; /* for processing tokens */
528 char *lasts;
530 _DIAGASSERT(stringp != NULL);
531 _DIAGASSERT(ncp != NULL);
533 nc_error = NC_BADFILE;
534 /* nearly anything that breaks is for this reason */
535 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */
536 /* netid */
537 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL)
538 return (-1);
540 /* semantics */
541 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
542 return (-1);
543 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
544 ncp->nc_semantics = NC_TPI_COTS_ORD;
545 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
546 ncp->nc_semantics = NC_TPI_COTS;
547 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
548 ncp->nc_semantics = NC_TPI_CLTS;
549 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
550 ncp->nc_semantics = NC_TPI_RAW;
551 else
552 return (-1);
554 /* flags */
555 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
556 return (-1);
557 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0'; tokenp++) {
558 switch (*tokenp) {
559 case NC_NOFLAG_C:
560 break;
561 case NC_VISIBLE_C:
562 ncp->nc_flag |= NC_VISIBLE;
563 break;
564 case NC_BROADCAST_C:
565 ncp->nc_flag |= NC_BROADCAST;
566 break;
567 default:
568 return (-1);
571 /* protocol family */
572 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL)
573 return (-1);
574 /* protocol name */
575 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL)
576 return (-1);
577 /* network device */
578 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL)
579 return (-1);
580 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
581 return (-1);
582 if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
583 ncp->nc_nlookups = 0;
584 ncp->nc_lookups = NULL;
585 } else {
586 char *cp; /* tmp string */
588 if (ncp->nc_lookups != NULL) /* from last visit */
589 free(ncp->nc_lookups);
590 /* preallocate one string pointer */
591 ncp->nc_lookups = malloc(sizeof(*ncp->nc_lookups));
592 ncp->nc_nlookups = 0;
593 while ((cp = tokenp) != NULL) {
594 tokenp = _get_next_token(cp, ',');
595 ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
596 ncp->nc_lookups = (char **)
597 realloc(ncp->nc_lookups,
598 (size_t)(ncp->nc_nlookups+1) *sizeof(char *));
599 /* for next loop */
602 return (0);
606 * Returns a string describing the reason for failure.
608 char *
609 nc_sperror(void)
611 const char *message;
613 switch(nc_error) {
614 case NC_NONETCONFIG:
615 message = _nc_errors[0];
616 break;
617 case NC_NOMEM:
618 message = _nc_errors[1];
619 break;
620 case NC_NOTINIT:
621 message = _nc_errors[2];
622 break;
623 case NC_BADFILE:
624 message = _nc_errors[3];
625 break;
626 default:
627 message = "Unknown network selection error";
629 return __UNCONST(message);
633 * Prints a message onto standard error describing the reason for failure.
635 void
636 nc_perror(const char *s)
639 _DIAGASSERT(s != NULL);
641 fprintf(stderr, "%s: %s", s, nc_sperror());
645 * Duplicates the matched netconfig buffer.
647 static struct netconfig *
648 dup_ncp(struct netconfig *ncp)
650 struct netconfig *p;
651 char *tmp;
652 u_int i;
654 _DIAGASSERT(ncp != NULL);
656 if ((tmp = malloc(MAXNETCONFIGLINE)) == NULL)
657 return(NULL);
658 if ((p = malloc(sizeof(*p))) == NULL) {
659 free(tmp);
660 return(NULL);
663 * First we dup all the data from matched netconfig buffer. Then we
664 * adjust some of the member pointer to a pre-allocated buffer where
665 * contains part of the data.
666 * To follow the convention used in parse_ncp(), we store all the
667 * necessary information in the pre-allocated buffer and let each
668 * of the netconfig char pointer member point to the right address
669 * in the buffer.
671 *p = *ncp;
672 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
673 tmp = strchr(tmp, '\0') + 1;
674 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
675 tmp = strchr(tmp, '\0') + 1;
676 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
677 tmp = strchr(tmp, '\0') + 1;
678 p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
679 p->nc_lookups = malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
680 if (p->nc_lookups == NULL) {
681 free(p->nc_netid);
682 free(p);
683 return(NULL);
685 for (i=0; i < p->nc_nlookups; i++) {
686 tmp = strchr(tmp, '\0') + 1;
687 p->nc_lookups[i] = strcpy(tmp,ncp->nc_lookups[i]);
689 return(p);