Home | History | Annotate | Line # | Download | only in rpc
getnetconfig.c revision 1.4
      1  1.4     lukem /*	$NetBSD: getnetconfig.c,v 1.4 2001/01/04 14:42:19 lukem Exp $	*/
      2  1.1      fvdl 
      3  1.1      fvdl /*
      4  1.1      fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.1      fvdl  * unrestricted use provided that this legend is included on all tape
      6  1.1      fvdl  * media and as a part of the software program in whole or part.  Users
      7  1.1      fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.1      fvdl  * to license or distribute it to anyone else except as part of a product or
      9  1.1      fvdl  * program developed by the user or with the express written consent of
     10  1.1      fvdl  * Sun Microsystems, Inc.
     11  1.1      fvdl  *
     12  1.1      fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     13  1.1      fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     14  1.1      fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     15  1.1      fvdl  *
     16  1.1      fvdl  * Sun RPC is provided with no support and without any obligation on the
     17  1.1      fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     18  1.1      fvdl  * modification or enhancement.
     19  1.1      fvdl  *
     20  1.1      fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     21  1.1      fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     22  1.1      fvdl  * OR ANY PART THEREOF.
     23  1.1      fvdl  *
     24  1.1      fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     25  1.1      fvdl  * or profits or other special, indirect and consequential damages, even if
     26  1.1      fvdl  * Sun has been advised of the possibility of such damages.
     27  1.1      fvdl  *
     28  1.1      fvdl  * Sun Microsystems, Inc.
     29  1.1      fvdl  * 2550 Garcia Avenue
     30  1.1      fvdl  * Mountain View, California  94043
     31  1.1      fvdl  */
     32  1.1      fvdl /*
     33  1.1      fvdl #ifndef lint
     34  1.1      fvdl static        char sccsid[] = "@(#)getnetconfig.c	1.12 91/12/19 SMI";
     35  1.1      fvdl #endif
     36  1.1      fvdl */
     37  1.1      fvdl 
     38  1.1      fvdl /*
     39  1.1      fvdl  * Copyright (c) 1989 by Sun Microsystems, Inc.
     40  1.1      fvdl  */
     41  1.1      fvdl 
     42  1.1      fvdl #include "namespace.h"
     43  1.1      fvdl #include <sys/cdefs.h>
     44  1.1      fvdl #include <stdio.h>
     45  1.4     lukem #include <assert.h>
     46  1.1      fvdl #include <errno.h>
     47  1.1      fvdl #include <netconfig.h>
     48  1.1      fvdl #include <stdlib.h>
     49  1.1      fvdl #include <string.h>
     50  1.3  christos #include <rpc/rpc.h>
     51  1.3  christos #include "rpc_com.h"
     52  1.1      fvdl 
     53  1.1      fvdl #ifdef __weak_alias
     54  1.1      fvdl __weak_alias(getnetconfig,_getnetconfig)
     55  1.1      fvdl __weak_alias(setnetconfig,_setnetconfig)
     56  1.1      fvdl __weak_alias(endnetconfig,_endnetconfig)
     57  1.1      fvdl __weak_alias(getnetconfigent,_getnetconfigent)
     58  1.1      fvdl __weak_alias(freenetconfigent,_freenetconfigent)
     59  1.1      fvdl __weak_alias(nc_perror,_nc_perror)
     60  1.1      fvdl __weak_alias(nc_sperror,_nc_sperror)
     61  1.1      fvdl #endif
     62  1.1      fvdl 
     63  1.1      fvdl /*
     64  1.1      fvdl  * The five library routines in this file provide application access to the
     65  1.1      fvdl  * system network configuration database, /etc/netconfig.  In addition to the
     66  1.1      fvdl  * netconfig database and the routines for accessing it, the environment
     67  1.1      fvdl  * variable NETPATH and its corresponding routines in getnetpath.c may also be
     68  1.1      fvdl  * used to specify the network transport to be used.
     69  1.1      fvdl  */
     70  1.1      fvdl 
     71  1.1      fvdl 
     72  1.1      fvdl /*
     73  1.1      fvdl  * netconfig errors
     74  1.1      fvdl  */
     75  1.1      fvdl 
     76  1.1      fvdl #define NC_NONETCONFIG	ENOENT
     77  1.1      fvdl #define NC_NOMEM	ENOMEM
     78  1.1      fvdl #define NC_NOTINIT	EINVAL	    /* setnetconfig was not called first */
     79  1.1      fvdl #define NC_BADFILE	EBADF	    /* format for netconfig file is bad */
     80  1.1      fvdl 
     81  1.1      fvdl /*
     82  1.1      fvdl  * semantics as strings (should be in netconfig.h)
     83  1.1      fvdl  */
     84  1.1      fvdl #define NC_TPI_CLTS_S	    "tpi_clts"
     85  1.1      fvdl #define	NC_TPI_COTS_S	    "tpi_cots"
     86  1.1      fvdl #define	NC_TPI_COTS_ORD_S   "tpi_cots_ord"
     87  1.1      fvdl #define	NC_TPI_RAW_S        "tpi_raw"
     88  1.1      fvdl 
     89  1.1      fvdl /*
     90  1.1      fvdl  * flags as characters (also should be in netconfig.h)
     91  1.1      fvdl  */
     92  1.1      fvdl #define	NC_NOFLAG_C	'-'
     93  1.1      fvdl #define	NC_VISIBLE_C	'v'
     94  1.1      fvdl #define	NC_BROADCAST_C	'b'
     95  1.1      fvdl 
     96  1.1      fvdl /*
     97  1.1      fvdl  * Character used to indicate there is no name-to-address lookup library
     98  1.1      fvdl  */
     99  1.1      fvdl #define NC_NOLOOKUP	"-"
    100  1.1      fvdl 
    101  1.1      fvdl static char *_nc_errors[] = {
    102  1.1      fvdl     "Netconfig database not found",
    103  1.1      fvdl     "Not enough memory",
    104  1.1      fvdl     "Not initialized",
    105  1.1      fvdl     "Netconfig database has invalid format"
    106  1.1      fvdl };
    107  1.1      fvdl 
    108  1.1      fvdl struct netconfig_info {
    109  1.1      fvdl     int		eof;	/* all entries has been read */
    110  1.1      fvdl     int		ref;	/* # of times setnetconfig() has been called */
    111  1.1      fvdl     struct netconfig_list	*head;	/* head of the list */
    112  1.1      fvdl     struct netconfig_list	*tail;	/* last of the list */
    113  1.1      fvdl };
    114  1.1      fvdl 
    115  1.1      fvdl struct netconfig_list {
    116  1.1      fvdl     char			*linep;	/* hold line read from netconfig */
    117  1.1      fvdl     struct netconfig		*ncp;
    118  1.1      fvdl     struct netconfig_list	*next;
    119  1.1      fvdl };
    120  1.1      fvdl 
    121  1.1      fvdl struct netconfig_vars {
    122  1.1      fvdl     int   valid;	/* token that indicates a valid netconfig_vars */
    123  1.1      fvdl     int   flag;		/* first time flag */
    124  1.1      fvdl     struct netconfig_list *nc_configs;  /* pointer to the current netconfig entry */
    125  1.1      fvdl };
    126  1.1      fvdl 
    127  1.1      fvdl #define NC_VALID	0xfeed
    128  1.1      fvdl #define NC_STORAGE	0xf00d
    129  1.1      fvdl #define NC_INVALID	0
    130  1.1      fvdl 
    131  1.1      fvdl 
    132  1.1      fvdl static int *__nc_error __P((void));
    133  1.1      fvdl static int parse_ncp __P((char *, struct netconfig *));
    134  1.1      fvdl static struct netconfig *dup_ncp __P((struct netconfig *));
    135  1.1      fvdl 
    136  1.1      fvdl 
    137  1.1      fvdl static FILE *nc_file;		/* for netconfig db */
    138  1.1      fvdl static struct netconfig_info	ni = { 0, 0, NULL, NULL};
    139  1.1      fvdl 
    140  1.1      fvdl #define MAXNETCONFIGLINE    1000
    141  1.1      fvdl 
    142  1.1      fvdl static int *
    143  1.1      fvdl __nc_error()
    144  1.1      fvdl {
    145  1.1      fvdl #ifdef __REENT
    146  1.1      fvdl 	static thread_key_t nc_key = 0;
    147  1.1      fvdl 	int *nc_addr = NULL;
    148  1.1      fvdl #endif
    149  1.1      fvdl 	static int nc_error = 0;
    150  1.1      fvdl 
    151  1.1      fvdl #ifdef __REENT
    152  1.1      fvdl 	if (_thr_getspecific(nc_key, (void **) &nc_addr) != 0) {
    153  1.1      fvdl 		mutex_lock(&nc_lock);
    154  1.1      fvdl 		if (_thr_keycreate(&rce_key, free) != 0) {
    155  1.1      fvdl 			mutex_unlock(&nc_lock);
    156  1.1      fvdl 			return nc_addr;
    157  1.1      fvdl 		}
    158  1.1      fvdl 		mutex_unlock(&nc_lock);
    159  1.1      fvdl 	}
    160  1.1      fvdl 	if (nc_addr == NULL) {
    161  1.1      fvdl 		nc_addr = (int *)malloc(sizeof (int));
    162  1.1      fvdl 		if (_thr_setspecific(nc_key, (void *) nc_addr) != 0) {
    163  1.1      fvdl 			if (nc_addr)
    164  1.1      fvdl 				free(nc_addr);
    165  1.1      fvdl 			return &nc_error;
    166  1.1      fvdl 		}
    167  1.1      fvdl 		*nc_addr = 0;
    168  1.1      fvdl 		return nc_addr;
    169  1.1      fvdl 	}
    170  1.1      fvdl 	return nc_addr;
    171  1.1      fvdl #else
    172  1.1      fvdl 	return &nc_error;
    173  1.1      fvdl #endif
    174  1.1      fvdl }
    175  1.1      fvdl 
    176  1.1      fvdl #define nc_error        (*(__nc_error()))
    177  1.1      fvdl /*
    178  1.1      fvdl  * A call to setnetconfig() establishes a /etc/netconfig "session".  A session
    179  1.1      fvdl  * "handle" is returned on a successful call.  At the start of a session (after
    180  1.1      fvdl  * a call to setnetconfig()) searches through the /etc/netconfig database will
    181  1.1      fvdl  * proceed from the start of the file.  The session handle must be passed to
    182  1.1      fvdl  * getnetconfig() to parse the file.  Each call to getnetconfig() using the
    183  1.1      fvdl  * current handle will process one subsequent entry in /etc/netconfig.
    184  1.1      fvdl  * setnetconfig() must be called before the first call to getnetconfig().
    185  1.1      fvdl  * (Handles are used to allow for nested calls to setnetpath()).
    186  1.1      fvdl  *
    187  1.1      fvdl  * A new session is established with each call to setnetconfig(), with a new
    188  1.1      fvdl  * handle being returned on each call.  Previously established sessions remain
    189  1.1      fvdl  * active until endnetconfig() is called with that session's handle as an
    190  1.1      fvdl  * argument.
    191  1.1      fvdl  *
    192  1.1      fvdl  * setnetconfig() need *not* be called before a call to getnetconfigent().
    193  1.1      fvdl  * setnetconfig() returns a NULL pointer on failure (for example, if
    194  1.1      fvdl  * the netconfig database is not present).
    195  1.1      fvdl  */
    196  1.1      fvdl void *
    197  1.1      fvdl setnetconfig()
    198  1.1      fvdl {
    199  1.1      fvdl     struct netconfig_vars *nc_vars;
    200  1.1      fvdl 
    201  1.1      fvdl     if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
    202  1.1      fvdl 		(struct netconfig_vars))) == NULL) {
    203  1.1      fvdl 	return(NULL);
    204  1.1      fvdl     }
    205  1.1      fvdl 
    206  1.1      fvdl     /*
    207  1.1      fvdl      * For multiple calls, i.e. nc_file is not NULL, we just return the
    208  1.1      fvdl      * handle without reopening the netconfig db.
    209  1.1      fvdl      */
    210  1.1      fvdl     ni.ref++;
    211  1.1      fvdl     if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
    212  1.1      fvdl 	nc_vars->valid = NC_VALID;
    213  1.1      fvdl 	nc_vars->flag = 0;
    214  1.1      fvdl 	nc_vars->nc_configs = ni.head;
    215  1.1      fvdl 	return ((void *)nc_vars);
    216  1.1      fvdl     }
    217  1.1      fvdl     ni.ref--;
    218  1.1      fvdl     nc_error = NC_NONETCONFIG;
    219  1.1      fvdl     free(nc_vars);
    220  1.1      fvdl     return (NULL);
    221  1.1      fvdl }
    222  1.1      fvdl 
    223  1.1      fvdl 
    224  1.1      fvdl /*
    225  1.1      fvdl  * When first called, getnetconfig() returns a pointer to the first entry in
    226  1.1      fvdl  * the netconfig database, formatted as a struct netconfig.  On each subsequent
    227  1.1      fvdl  * call, getnetconfig() returns a pointer to the next entry in the database.
    228  1.1      fvdl  * getnetconfig() can thus be used to search the entire netconfig file.
    229  1.1      fvdl  * getnetconfig() returns NULL at end of file.
    230  1.1      fvdl  */
    231  1.1      fvdl 
    232  1.1      fvdl struct netconfig *
    233  1.1      fvdl getnetconfig(handlep)
    234  1.1      fvdl void *handlep;
    235  1.1      fvdl {
    236  1.1      fvdl     struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
    237  1.1      fvdl     char *stringp;		/* tmp string pointer */
    238  1.1      fvdl     struct netconfig_list	*list;
    239  1.1      fvdl     struct netconfig *np;
    240  1.1      fvdl 
    241  1.1      fvdl     /*
    242  1.1      fvdl      * Verify that handle is valid
    243  1.1      fvdl      */
    244  1.1      fvdl     if (ncp == NULL || nc_file == NULL) {
    245  1.1      fvdl 	nc_error = NC_NOTINIT;
    246  1.1      fvdl 	return (NULL);
    247  1.1      fvdl     }
    248  1.1      fvdl 
    249  1.1      fvdl     switch (ncp->valid) {
    250  1.1      fvdl     case NC_VALID:
    251  1.1      fvdl 	/*
    252  1.1      fvdl 	 * If entry has already been read into the list,
    253  1.1      fvdl 	 * we return the entry in the linked list.
    254  1.1      fvdl 	 * If this is the first time call, check if there are any entries in
    255  1.1      fvdl 	 * linked list.  If no entries, we need to read the netconfig db.
    256  1.1      fvdl 	 * If we have been here and the next entry is there, we just return
    257  1.1      fvdl 	 * it.
    258  1.1      fvdl 	 */
    259  1.1      fvdl 	if (ncp->flag == 0) {	/* first time */
    260  1.1      fvdl 	    ncp->flag = 1;
    261  1.1      fvdl 	    ncp->nc_configs = ni.head;
    262  1.1      fvdl 	    if (ncp->nc_configs != NULL)	/* entry already exist */
    263  1.1      fvdl 		return(ncp->nc_configs->ncp);
    264  1.1      fvdl 	}
    265  1.1      fvdl 	else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
    266  1.1      fvdl 	    ncp->nc_configs = ncp->nc_configs->next;
    267  1.1      fvdl 	    return(ncp->nc_configs->ncp);
    268  1.1      fvdl 	}
    269  1.1      fvdl 
    270  1.1      fvdl 	/*
    271  1.1      fvdl 	 * If we cannot find the entry in the list and is end of file,
    272  1.1      fvdl 	 * we give up.
    273  1.1      fvdl 	 */
    274  1.1      fvdl 	if (ni.eof == 1)	return(NULL);
    275  1.1      fvdl 	break;
    276  1.1      fvdl     default:
    277  1.1      fvdl 	nc_error = NC_NOTINIT;
    278  1.1      fvdl 	return (NULL);
    279  1.1      fvdl     }
    280  1.1      fvdl 
    281  1.1      fvdl     stringp = (char *) malloc(MAXNETCONFIGLINE);
    282  1.1      fvdl     if (stringp == NULL)
    283  1.1      fvdl     	return (NULL);
    284  1.1      fvdl 
    285  1.1      fvdl #ifdef MEM_CHK
    286  1.1      fvdl     if (malloc_verify() == 0) {
    287  1.1      fvdl 	fprintf(stderr, "memory heap corrupted in getnetconfig\n");
    288  1.1      fvdl 	exit(1);
    289  1.1      fvdl     }
    290  1.1      fvdl #endif
    291  1.1      fvdl 
    292  1.1      fvdl     /*
    293  1.1      fvdl      * Read a line from netconfig file.
    294  1.1      fvdl      */
    295  1.1      fvdl     do {
    296  1.1      fvdl 	if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
    297  1.1      fvdl 	    free(stringp);
    298  1.1      fvdl 	    ni.eof = 1;
    299  1.1      fvdl 	    return (NULL);
    300  1.1      fvdl         }
    301  1.1      fvdl     } while (*stringp == '#');
    302  1.1      fvdl 
    303  1.1      fvdl     list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
    304  1.1      fvdl     if (list == NULL) {
    305  1.1      fvdl     	free(stringp);
    306  1.1      fvdl     	return(NULL);
    307  1.1      fvdl     }
    308  1.1      fvdl     np = (struct netconfig *) malloc(sizeof (struct netconfig));
    309  1.1      fvdl     if (np == NULL) {
    310  1.1      fvdl     	free(stringp);
    311  1.1      fvdl 	free(list);
    312  1.1      fvdl     	return(NULL);
    313  1.1      fvdl     }
    314  1.1      fvdl     list->ncp = np;
    315  1.1      fvdl     list->next = NULL;
    316  1.1      fvdl     list->ncp->nc_lookups = NULL;
    317  1.1      fvdl     list->linep = stringp;
    318  1.1      fvdl     if (parse_ncp(stringp, list->ncp) == -1) {
    319  1.1      fvdl 	free(stringp);
    320  1.1      fvdl 	free(np);
    321  1.1      fvdl 	free(list);
    322  1.1      fvdl 	return (NULL);
    323  1.1      fvdl     }
    324  1.1      fvdl     else {
    325  1.1      fvdl 	/*
    326  1.1      fvdl 	 * If this is the first entry that's been read, it is the head of
    327  1.1      fvdl 	 * the list.  If not, put the entry at the end of the list.
    328  1.1      fvdl 	 * Reposition the current pointer of the handle to the last entry
    329  1.1      fvdl 	 * in the list.
    330  1.1      fvdl 	 */
    331  1.1      fvdl 	if (ni.head == NULL) {	/* first entry */
    332  1.1      fvdl 	    ni.head = ni.tail = list;
    333  1.1      fvdl 	}
    334  1.1      fvdl     	else {
    335  1.1      fvdl     	    ni.tail->next = list;
    336  1.1      fvdl     	    ni.tail = ni.tail->next;
    337  1.1      fvdl     	}
    338  1.1      fvdl 	ncp->nc_configs = ni.tail;
    339  1.1      fvdl 	return(ni.tail->ncp);
    340  1.1      fvdl     }
    341  1.1      fvdl }
    342  1.1      fvdl 
    343  1.1      fvdl /*
    344  1.1      fvdl  * endnetconfig() may be called to "unbind" or "close" the netconfig database
    345  1.1      fvdl  * when processing is complete, releasing resources for reuse.  endnetconfig()
    346  1.1      fvdl  * may not be called before setnetconfig().  endnetconfig() returns 0 on
    347  1.1      fvdl  * success and -1 on failure (for example, if setnetconfig() was not called
    348  1.1      fvdl  * previously).
    349  1.1      fvdl  */
    350  1.1      fvdl int
    351  1.1      fvdl endnetconfig(handlep)
    352  1.1      fvdl void *handlep;
    353  1.1      fvdl {
    354  1.1      fvdl     struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
    355  1.1      fvdl 
    356  1.1      fvdl     struct netconfig_list *q, *p;
    357  1.1      fvdl 
    358  1.1      fvdl     /*
    359  1.1      fvdl      * Verify that handle is valid
    360  1.1      fvdl      */
    361  1.1      fvdl     if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
    362  1.1      fvdl 	    nc_handlep->valid != NC_STORAGE)) {
    363  1.1      fvdl 	nc_error = NC_NOTINIT;
    364  1.1      fvdl 	return (-1);
    365  1.1      fvdl     }
    366  1.1      fvdl 
    367  1.1      fvdl     /*
    368  1.1      fvdl      * Return 0 if anyone still needs it.
    369  1.1      fvdl      */
    370  1.1      fvdl     nc_handlep->valid = NC_INVALID;
    371  1.1      fvdl     nc_handlep->flag = 0;
    372  1.1      fvdl     nc_handlep->nc_configs = NULL;
    373  1.1      fvdl     if (--ni.ref > 0) {
    374  1.1      fvdl     	free(nc_handlep);
    375  1.1      fvdl 	return(0);
    376  1.1      fvdl     }
    377  1.1      fvdl 
    378  1.1      fvdl     /*
    379  1.1      fvdl      * Noone needs these entries anymore, then frees them.
    380  1.1      fvdl      * Make sure all info in netconfig_info structure has been reinitialized.
    381  1.1      fvdl      */
    382  1.1      fvdl     q = p = ni.head;
    383  1.1      fvdl     ni.eof = ni.ref = 0;
    384  1.1      fvdl     ni.head = NULL;
    385  1.1      fvdl     ni.tail = NULL;
    386  1.1      fvdl     while (q) {
    387  1.1      fvdl 	p = q->next;
    388  1.1      fvdl 	if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
    389  1.1      fvdl 	free(q->ncp);
    390  1.1      fvdl 	free(q->linep);
    391  1.1      fvdl 	free(q);
    392  1.1      fvdl 	q = p;
    393  1.1      fvdl     }
    394  1.1      fvdl     free(nc_handlep);
    395  1.1      fvdl 
    396  1.1      fvdl     fclose(nc_file);
    397  1.1      fvdl     nc_file = NULL;
    398  1.1      fvdl     return (0);
    399  1.1      fvdl }
    400  1.1      fvdl 
    401  1.1      fvdl /*
    402  1.1      fvdl  * getnetconfigent(netid) returns a pointer to the struct netconfig structure
    403  1.1      fvdl  * corresponding to netid.  It returns NULL if netid is invalid (that is, does
    404  1.1      fvdl  * not name an entry in the netconfig database).  It returns NULL and sets
    405  1.1      fvdl  * errno in case of failure (for example, if the netconfig database cannot be
    406  1.1      fvdl  * opened).
    407  1.1      fvdl  */
    408  1.1      fvdl 
    409  1.1      fvdl struct netconfig *
    410  1.1      fvdl getnetconfigent(netid)
    411  1.1      fvdl 	char *netid;
    412  1.1      fvdl {
    413  1.1      fvdl     FILE *file;		/* NETCONFIG db's file pointer */
    414  1.1      fvdl     char *linep;	/* holds current netconfig line */
    415  1.1      fvdl     char *stringp;	/* temporary string pointer */
    416  1.1      fvdl     struct netconfig *ncp = NULL;   /* returned value */
    417  1.1      fvdl     struct netconfig_list *list;	/* pointer to cache list */
    418  1.1      fvdl 
    419  1.1      fvdl     if (netid == NULL || strlen(netid) == 0) {
    420  1.1      fvdl 	return (NULL);
    421  1.1      fvdl     }
    422  1.1      fvdl 
    423  1.1      fvdl     /*
    424  1.1      fvdl      * Look up table if the entries have already been read and parsed in
    425  1.1      fvdl      * getnetconfig(), then copy this entry into a buffer and return it.
    426  1.1      fvdl      * If we cannot find the entry in the current list and there are more
    427  1.1      fvdl      * entries in the netconfig db that has not been read, we then read the
    428  1.1      fvdl      * db and try find the match netid.
    429  1.1      fvdl      * If all the netconfig db has been read and placed into the list and
    430  1.1      fvdl      * there is no match for the netid, return NULL.
    431  1.1      fvdl      */
    432  1.1      fvdl     if (ni.head != NULL) {
    433  1.1      fvdl 	for (list = ni.head; list; list = list->next) {
    434  1.1      fvdl 	    if (strcmp(list->ncp->nc_netid, netid) == 0) {
    435  1.1      fvdl 	        return(dup_ncp(list->ncp));
    436  1.1      fvdl 	    }
    437  1.1      fvdl 	}
    438  1.1      fvdl 	if (ni.eof == 1)	/* that's all the entries */
    439  1.1      fvdl 		return(NULL);
    440  1.1      fvdl     }
    441  1.1      fvdl 
    442  1.1      fvdl 
    443  1.1      fvdl     if ((file = fopen(NETCONFIG, "r")) == NULL) {
    444  1.1      fvdl 	return (NULL);
    445  1.1      fvdl     }
    446  1.1      fvdl 
    447  1.1      fvdl     if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
    448  1.1      fvdl 	fclose(file);
    449  1.1      fvdl 	return (NULL);
    450  1.1      fvdl     }
    451  1.1      fvdl     do {
    452  1.1      fvdl 	int len;
    453  1.1      fvdl 	char *tmpp;	/* tmp string pointer */
    454  1.1      fvdl 
    455  1.1      fvdl 	do {
    456  1.1      fvdl 	    if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
    457  1.1      fvdl 		break;
    458  1.1      fvdl 	    }
    459  1.1      fvdl 	} while (*stringp == '#');
    460  1.1      fvdl 	if (stringp == NULL) {	    /* eof */
    461  1.1      fvdl 	    break;
    462  1.1      fvdl 	}
    463  1.1      fvdl 	if ((tmpp = strpbrk(stringp, "\t ")) == NULL) {	/* can't parse file */
    464  1.1      fvdl 	    nc_error = NC_BADFILE;
    465  1.1      fvdl 	    break;
    466  1.1      fvdl 	}
    467  1.1      fvdl 	if (strlen(netid) == (len = tmpp - stringp) &&	/* a match */
    468  1.3  christos 		strncmp(stringp, netid, (size_t)len) == 0) {
    469  1.1      fvdl 	    if ((ncp = (struct netconfig *)
    470  1.1      fvdl 		    malloc(sizeof (struct netconfig))) == NULL) {
    471  1.1      fvdl 		break;
    472  1.1      fvdl 	    }
    473  1.1      fvdl 	    ncp->nc_lookups = NULL;
    474  1.1      fvdl 	    if (parse_ncp(linep, ncp) == -1) {
    475  1.1      fvdl 		free(ncp);
    476  1.1      fvdl 		ncp = NULL;
    477  1.1      fvdl 	    }
    478  1.1      fvdl 	    break;
    479  1.1      fvdl 	}
    480  1.1      fvdl     } while (stringp != NULL);
    481  1.1      fvdl     if (ncp == NULL) {
    482  1.1      fvdl 	free(linep);
    483  1.1      fvdl     }
    484  1.1      fvdl     fclose(file);
    485  1.1      fvdl     return(ncp);
    486  1.1      fvdl }
    487  1.1      fvdl 
    488  1.1      fvdl /*
    489  1.1      fvdl  * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
    490  1.1      fvdl  * netconfigp (previously returned by getnetconfigent()).
    491  1.1      fvdl  */
    492  1.1      fvdl 
    493  1.1      fvdl void
    494  1.1      fvdl freenetconfigent(netconfigp)
    495  1.1      fvdl 	struct netconfig *netconfigp;
    496  1.1      fvdl {
    497  1.1      fvdl     if (netconfigp != NULL) {
    498  1.1      fvdl 	free(netconfigp->nc_netid);	/* holds all netconfigp's strings */
    499  1.1      fvdl 	if (netconfigp->nc_lookups != NULL)
    500  1.1      fvdl 	    free(netconfigp->nc_lookups);
    501  1.1      fvdl 	free(netconfigp);
    502  1.1      fvdl     }
    503  1.1      fvdl     return;
    504  1.1      fvdl }
    505  1.1      fvdl 
    506  1.1      fvdl /*
    507  1.1      fvdl  * Parse line and stuff it in a struct netconfig
    508  1.1      fvdl  * Typical line might look like:
    509  1.1      fvdl  *	udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
    510  1.1      fvdl  *
    511  1.1      fvdl  * We return -1 if any of the tokens don't parse, or malloc fails.
    512  1.1      fvdl  *
    513  1.1      fvdl  * Note that we modify stringp (putting NULLs after tokens) and
    514  1.1      fvdl  * we set the ncp's string field pointers to point to these tokens within
    515  1.1      fvdl  * stringp.
    516  1.1      fvdl  */
    517  1.1      fvdl 
    518  1.1      fvdl static int
    519  1.1      fvdl parse_ncp(stringp, ncp)
    520  1.1      fvdl char *stringp;		/* string to parse */
    521  1.1      fvdl struct netconfig *ncp;	/* where to put results */
    522  1.1      fvdl {
    523  1.1      fvdl     char    *tokenp;	/* for processing tokens */
    524  1.2  christos     char    *lasts;
    525  1.1      fvdl 
    526  1.4     lukem     _DIAGASSERT(stringp != NULL);
    527  1.4     lukem     _DIAGASSERT(ncp != NULL);
    528  1.4     lukem 
    529  1.1      fvdl     nc_error = NC_BADFILE;	/* nearly anything that breaks is for this reason */
    530  1.1      fvdl     stringp[strlen(stringp)-1] = '\0';	/* get rid of newline */
    531  1.1      fvdl     /* netid */
    532  1.2  christos     if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
    533  1.1      fvdl 	return (-1);
    534  1.1      fvdl     }
    535  1.1      fvdl 
    536  1.1      fvdl     /* semantics */
    537  1.2  christos     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    538  1.1      fvdl 	return (-1);
    539  1.1      fvdl     }
    540  1.1      fvdl     if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
    541  1.1      fvdl 	ncp->nc_semantics = NC_TPI_COTS_ORD;
    542  1.1      fvdl     else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
    543  1.1      fvdl 	ncp->nc_semantics = NC_TPI_COTS;
    544  1.1      fvdl     else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
    545  1.1      fvdl 	ncp->nc_semantics = NC_TPI_CLTS;
    546  1.1      fvdl     else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
    547  1.1      fvdl 	ncp->nc_semantics = NC_TPI_RAW;
    548  1.1      fvdl     else
    549  1.1      fvdl 	return (-1);
    550  1.1      fvdl 
    551  1.1      fvdl     /* flags */
    552  1.2  christos     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    553  1.1      fvdl 	return (-1);
    554  1.1      fvdl     }
    555  1.1      fvdl     for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
    556  1.1      fvdl 	    tokenp++) {
    557  1.1      fvdl 	switch (*tokenp) {
    558  1.1      fvdl 	case NC_NOFLAG_C:
    559  1.1      fvdl 	    break;
    560  1.1      fvdl 	case NC_VISIBLE_C:
    561  1.1      fvdl 	    ncp->nc_flag |= NC_VISIBLE;
    562  1.1      fvdl 	    break;
    563  1.1      fvdl 	case NC_BROADCAST_C:
    564  1.1      fvdl 	    ncp->nc_flag |= NC_BROADCAST;
    565  1.1      fvdl 	    break;
    566  1.1      fvdl 	default:
    567  1.1      fvdl 	    return (-1);
    568  1.1      fvdl 	}
    569  1.1      fvdl     }
    570  1.1      fvdl     /* protocol family */
    571  1.2  christos     if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    572  1.1      fvdl 	return (-1);
    573  1.1      fvdl     }
    574  1.1      fvdl     /* protocol name */
    575  1.2  christos     if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    576  1.1      fvdl 	return (-1);
    577  1.1      fvdl     }
    578  1.1      fvdl     /* network device */
    579  1.2  christos     if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    580  1.1      fvdl 	return (-1);
    581  1.1      fvdl     }
    582  1.2  christos     if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
    583  1.1      fvdl 	return (-1);
    584  1.1      fvdl     }
    585  1.1      fvdl     if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
    586  1.1      fvdl 	ncp->nc_nlookups = 0;
    587  1.1      fvdl 	ncp->nc_lookups = NULL;
    588  1.1      fvdl     } else {
    589  1.1      fvdl 	char *cp;	    /* tmp string */
    590  1.1      fvdl 
    591  1.1      fvdl 	if (ncp->nc_lookups != NULL)	/* from last visit */
    592  1.1      fvdl 	    free(ncp->nc_lookups);
    593  1.1      fvdl 	/* preallocate one string pointer */
    594  1.1      fvdl 	ncp->nc_lookups = (char **)malloc(sizeof (char *));
    595  1.1      fvdl 	ncp->nc_nlookups = 0;
    596  1.1      fvdl 	while ((cp = tokenp) != NULL) {
    597  1.1      fvdl 	    tokenp = _get_next_token(cp, ',');
    598  1.3  christos 	    ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
    599  1.1      fvdl 	    ncp->nc_lookups = (char **)realloc(ncp->nc_lookups,
    600  1.3  christos 		(size_t)(ncp->nc_nlookups+1) *sizeof(char *));	/* for next loop */
    601  1.1      fvdl 	}
    602  1.1      fvdl     }
    603  1.1      fvdl     return (0);
    604  1.1      fvdl }
    605  1.1      fvdl 
    606  1.1      fvdl 
    607  1.1      fvdl /*
    608  1.1      fvdl  * Returns a string describing the reason for failure.
    609  1.1      fvdl  */
    610  1.1      fvdl char *
    611  1.1      fvdl nc_sperror()
    612  1.1      fvdl {
    613  1.1      fvdl     char *message;
    614  1.1      fvdl 
    615  1.1      fvdl     switch(nc_error) {
    616  1.1      fvdl     case NC_NONETCONFIG:
    617  1.1      fvdl 	message = _nc_errors[0];
    618  1.1      fvdl 	break;
    619  1.1      fvdl     case NC_NOMEM:
    620  1.1      fvdl 	message = _nc_errors[1];
    621  1.1      fvdl 	break;
    622  1.1      fvdl     case NC_NOTINIT:
    623  1.1      fvdl 	message = _nc_errors[2];
    624  1.1      fvdl 	break;
    625  1.1      fvdl     case NC_BADFILE:
    626  1.1      fvdl 	message = _nc_errors[3];
    627  1.1      fvdl 	break;
    628  1.1      fvdl     default:
    629  1.1      fvdl 	message = "Unknown network selection error";
    630  1.1      fvdl     }
    631  1.1      fvdl     return (message);
    632  1.1      fvdl }
    633  1.1      fvdl 
    634  1.1      fvdl /*
    635  1.1      fvdl  * Prints a message onto standard error describing the reason for failure.
    636  1.1      fvdl  */
    637  1.1      fvdl void
    638  1.1      fvdl nc_perror(s)
    639  1.1      fvdl 	const char *s;
    640  1.1      fvdl {
    641  1.4     lukem 
    642  1.4     lukem     _DIAGASSERT(s != NULL);
    643  1.4     lukem 
    644  1.1      fvdl     fprintf(stderr, "%s: %s", s, nc_sperror());
    645  1.1      fvdl }
    646  1.1      fvdl 
    647  1.1      fvdl /*
    648  1.1      fvdl  * Duplicates the matched netconfig buffer.
    649  1.1      fvdl  */
    650  1.1      fvdl static struct netconfig *
    651  1.1      fvdl dup_ncp(ncp)
    652  1.1      fvdl struct netconfig	*ncp;
    653  1.1      fvdl {
    654  1.1      fvdl     struct netconfig	*p;
    655  1.1      fvdl     char	*tmp;
    656  1.1      fvdl     int	i;
    657  1.4     lukem 
    658  1.4     lukem     _DIAGASSERT(ncp != NULL);
    659  1.1      fvdl 
    660  1.1      fvdl     if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
    661  1.1      fvdl 	return(NULL);
    662  1.1      fvdl     if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
    663  1.1      fvdl 	free(tmp);
    664  1.1      fvdl 	return(NULL);
    665  1.1      fvdl     }
    666  1.1      fvdl     /*
    667  1.1      fvdl      * First we dup all the data from matched netconfig buffer.  Then we
    668  1.1      fvdl      * adjust some of the member pointer to a pre-allocated buffer where
    669  1.1      fvdl      * contains part of the data.
    670  1.1      fvdl      * To follow the convention used in parse_ncp(), we store all the
    671  1.1      fvdl      * neccessary information in the pre-allocated buffer and let each
    672  1.1      fvdl      * of the netconfig char pointer member point to the right address
    673  1.1      fvdl      * in the buffer.
    674  1.1      fvdl      */
    675  1.1      fvdl     *p = *ncp;
    676  1.1      fvdl     p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
    677  1.1      fvdl     tmp = strchr(tmp, NULL) + 1;
    678  1.1      fvdl     p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
    679  1.1      fvdl     tmp = strchr(tmp, NULL) + 1;
    680  1.1      fvdl     p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
    681  1.1      fvdl     tmp = strchr(tmp, NULL) + 1;
    682  1.1      fvdl     p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
    683  1.3  christos     p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
    684  1.1      fvdl     if (p->nc_lookups == NULL) {
    685  1.1      fvdl 	free(p->nc_netid);
    686  1.1      fvdl 	return(NULL);
    687  1.1      fvdl     }
    688  1.1      fvdl     for (i=0; i < p->nc_nlookups; i++) {
    689  1.1      fvdl     	tmp = strchr(tmp, NULL) + 1;
    690  1.1      fvdl     	p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);
    691  1.1      fvdl     }
    692  1.1      fvdl     return(p);
    693  1.1      fvdl }
    694