Home | History | Annotate | Line # | Download | only in rpc
getnetconfig.c revision 1.8
      1  1.8      fvdl /*	$NetBSD: getnetconfig.c,v 1.8 2002/11/08 00:13:07 fvdl 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.8      fvdl #include "rpc_internal.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.6  jdolecek static const char * const _nc_errors[] = {
    102  1.5     lukem 	"Netconfig database not found",
    103  1.5     lukem 	"Not enough memory",
    104  1.5     lukem 	"Not initialized",
    105  1.5     lukem 	"Netconfig database has invalid format"
    106  1.1      fvdl };
    107  1.1      fvdl 
    108  1.1      fvdl struct netconfig_info {
    109  1.5     lukem 	int		eof;	/* all entries has been read */
    110  1.5     lukem 	int		ref;	/* # of times setnetconfig() has been called */
    111  1.5     lukem 	struct netconfig_list	*head;	/* head of the list */
    112  1.5     lukem 	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.5     lukem 	char			*linep;	/* hold line read from netconfig */
    117  1.5     lukem 	struct netconfig	*ncp;
    118  1.5     lukem 	struct netconfig_list	*next;
    119  1.1      fvdl };
    120  1.1      fvdl 
    121  1.1      fvdl struct netconfig_vars {
    122  1.5     lukem 	    int   valid;	/* token that indicates valid netconfig_vars */
    123  1.5     lukem 	    int   flag;		/* first time flag */
    124  1.5     lukem 	    struct netconfig_list *nc_configs;
    125  1.5     lukem 	   			 /* pointer to the current netconfig entry */
    126  1.1      fvdl };
    127  1.1      fvdl 
    128  1.1      fvdl #define NC_VALID	0xfeed
    129  1.1      fvdl #define NC_STORAGE	0xf00d
    130  1.1      fvdl #define NC_INVALID	0
    131  1.1      fvdl 
    132  1.1      fvdl 
    133  1.1      fvdl static int *__nc_error __P((void));
    134  1.1      fvdl static int parse_ncp __P((char *, struct netconfig *));
    135  1.1      fvdl static struct netconfig *dup_ncp __P((struct netconfig *));
    136  1.1      fvdl 
    137  1.1      fvdl 
    138  1.1      fvdl static FILE *nc_file;		/* for netconfig db */
    139  1.1      fvdl static struct netconfig_info	ni = { 0, 0, NULL, NULL};
    140  1.1      fvdl 
    141  1.1      fvdl #define MAXNETCONFIGLINE    1000
    142  1.1      fvdl 
    143  1.1      fvdl static int *
    144  1.1      fvdl __nc_error()
    145  1.1      fvdl {
    146  1.1      fvdl #ifdef __REENT
    147  1.1      fvdl 	static thread_key_t nc_key = 0;
    148  1.1      fvdl 	int *nc_addr = NULL;
    149  1.1      fvdl #endif
    150  1.1      fvdl 	static int nc_error = 0;
    151  1.1      fvdl 
    152  1.1      fvdl #ifdef __REENT
    153  1.1      fvdl 	if (_thr_getspecific(nc_key, (void **) &nc_addr) != 0) {
    154  1.1      fvdl 		mutex_lock(&nc_lock);
    155  1.1      fvdl 		if (_thr_keycreate(&rce_key, free) != 0) {
    156  1.1      fvdl 			mutex_unlock(&nc_lock);
    157  1.1      fvdl 			return nc_addr;
    158  1.1      fvdl 		}
    159  1.1      fvdl 		mutex_unlock(&nc_lock);
    160  1.1      fvdl 	}
    161  1.1      fvdl 	if (nc_addr == NULL) {
    162  1.1      fvdl 		nc_addr = (int *)malloc(sizeof (int));
    163  1.1      fvdl 		if (_thr_setspecific(nc_key, (void *) nc_addr) != 0) {
    164  1.1      fvdl 			if (nc_addr)
    165  1.1      fvdl 				free(nc_addr);
    166  1.1      fvdl 			return &nc_error;
    167  1.1      fvdl 		}
    168  1.1      fvdl 		*nc_addr = 0;
    169  1.1      fvdl 		return nc_addr;
    170  1.1      fvdl 	}
    171  1.1      fvdl 	return nc_addr;
    172  1.1      fvdl #else
    173  1.1      fvdl 	return &nc_error;
    174  1.1      fvdl #endif
    175  1.1      fvdl }
    176  1.1      fvdl 
    177  1.1      fvdl #define nc_error        (*(__nc_error()))
    178  1.1      fvdl /*
    179  1.1      fvdl  * A call to setnetconfig() establishes a /etc/netconfig "session".  A session
    180  1.1      fvdl  * "handle" is returned on a successful call.  At the start of a session (after
    181  1.1      fvdl  * a call to setnetconfig()) searches through the /etc/netconfig database will
    182  1.1      fvdl  * proceed from the start of the file.  The session handle must be passed to
    183  1.1      fvdl  * getnetconfig() to parse the file.  Each call to getnetconfig() using the
    184  1.1      fvdl  * current handle will process one subsequent entry in /etc/netconfig.
    185  1.1      fvdl  * setnetconfig() must be called before the first call to getnetconfig().
    186  1.1      fvdl  * (Handles are used to allow for nested calls to setnetpath()).
    187  1.1      fvdl  *
    188  1.1      fvdl  * A new session is established with each call to setnetconfig(), with a new
    189  1.1      fvdl  * handle being returned on each call.  Previously established sessions remain
    190  1.1      fvdl  * active until endnetconfig() is called with that session's handle as an
    191  1.1      fvdl  * argument.
    192  1.1      fvdl  *
    193  1.1      fvdl  * setnetconfig() need *not* be called before a call to getnetconfigent().
    194  1.1      fvdl  * setnetconfig() returns a NULL pointer on failure (for example, if
    195  1.1      fvdl  * the netconfig database is not present).
    196  1.1      fvdl  */
    197  1.1      fvdl void *
    198  1.1      fvdl setnetconfig()
    199  1.1      fvdl {
    200  1.5     lukem 	struct netconfig_vars *nc_vars;
    201  1.1      fvdl 
    202  1.5     lukem 	if ((nc_vars = (struct netconfig_vars *)
    203  1.5     lukem 	    malloc(sizeof (struct netconfig_vars))) == NULL) {
    204  1.5     lukem 		return(NULL);
    205  1.5     lukem 	}
    206  1.5     lukem 
    207  1.5     lukem 	/*
    208  1.5     lukem 	 * For multiple calls, i.e. nc_file is not NULL, we just return the
    209  1.5     lukem 	 * handle without reopening the netconfig db.
    210  1.5     lukem 	 */
    211  1.5     lukem 	ni.ref++;
    212  1.5     lukem 	if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
    213  1.5     lukem 		nc_vars->valid = NC_VALID;
    214  1.5     lukem 		nc_vars->flag = 0;
    215  1.5     lukem 		nc_vars->nc_configs = ni.head;
    216  1.5     lukem 		return ((void *)nc_vars);
    217  1.5     lukem 	}
    218  1.5     lukem 	ni.ref--;
    219  1.5     lukem 	nc_error = NC_NONETCONFIG;
    220  1.5     lukem 	free(nc_vars);
    221  1.5     lukem 	return (NULL);
    222  1.1      fvdl }
    223  1.1      fvdl 
    224  1.1      fvdl 
    225  1.1      fvdl /*
    226  1.1      fvdl  * When first called, getnetconfig() returns a pointer to the first entry in
    227  1.1      fvdl  * the netconfig database, formatted as a struct netconfig.  On each subsequent
    228  1.1      fvdl  * call, getnetconfig() returns a pointer to the next entry in the database.
    229  1.1      fvdl  * getnetconfig() can thus be used to search the entire netconfig file.
    230  1.1      fvdl  * getnetconfig() returns NULL at end of file.
    231  1.1      fvdl  */
    232  1.1      fvdl 
    233  1.1      fvdl struct netconfig *
    234  1.1      fvdl getnetconfig(handlep)
    235  1.5     lukem 	void *handlep;
    236  1.1      fvdl {
    237  1.5     lukem 	struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
    238  1.5     lukem 	char *stringp;		/* tmp string pointer */
    239  1.5     lukem 	struct netconfig_list	*list;
    240  1.5     lukem 	struct netconfig *np;
    241  1.1      fvdl 
    242  1.1      fvdl 	/*
    243  1.5     lukem 	 * Verify that handle is valid
    244  1.1      fvdl 	 */
    245  1.5     lukem 	if (ncp == NULL || nc_file == NULL) {
    246  1.5     lukem 		nc_error = NC_NOTINIT;
    247  1.5     lukem 		return (NULL);
    248  1.5     lukem 	}
    249  1.5     lukem 
    250  1.5     lukem 	switch (ncp->valid) {
    251  1.5     lukem 	case NC_VALID:
    252  1.5     lukem 		/*
    253  1.5     lukem 		 * If entry has already been read into the list,
    254  1.5     lukem 		 * we return the entry in the linked list.
    255  1.5     lukem 		 * If this is the first time call, check if there are any
    256  1.5     lukem 		 * entries in linked list.  If no entries, we need to read the
    257  1.5     lukem 		 * netconfig db.
    258  1.5     lukem 		 * If we have been here and the next entry is there, we just
    259  1.5     lukem 		 * return it.
    260  1.5     lukem 		 */
    261  1.5     lukem 		if (ncp->flag == 0) {	/* first time */
    262  1.5     lukem 			ncp->flag = 1;
    263  1.5     lukem 			ncp->nc_configs = ni.head;
    264  1.5     lukem 			if (ncp->nc_configs != NULL) /* entry already exist */
    265  1.5     lukem 				return(ncp->nc_configs->ncp);
    266  1.5     lukem 		}
    267  1.5     lukem 		else if (ncp->nc_configs != NULL &&
    268  1.5     lukem 		    ncp->nc_configs->next != NULL) {
    269  1.5     lukem 			ncp->nc_configs = ncp->nc_configs->next;
    270  1.5     lukem 			return(ncp->nc_configs->ncp);
    271  1.5     lukem 		}
    272  1.5     lukem 
    273  1.5     lukem 		/*
    274  1.5     lukem 		 * If we cannot find the entry in the list and is end of file,
    275  1.5     lukem 		 * we give up.
    276  1.5     lukem 		 */
    277  1.5     lukem 		if (ni.eof == 1)
    278  1.5     lukem 			return(NULL);
    279  1.5     lukem 		break;
    280  1.5     lukem 	default:
    281  1.5     lukem 		nc_error = NC_NOTINIT;
    282  1.5     lukem 		return (NULL);
    283  1.1      fvdl 	}
    284  1.1      fvdl 
    285  1.5     lukem 	stringp = (char *) malloc(MAXNETCONFIGLINE);
    286  1.5     lukem 	if (stringp == NULL)
    287  1.5     lukem 		return (NULL);
    288  1.1      fvdl 
    289  1.1      fvdl #ifdef MEM_CHK
    290  1.5     lukem 	if (malloc_verify() == 0) {
    291  1.5     lukem 		fprintf(stderr, "memory heap corrupted in getnetconfig\n");
    292  1.5     lukem 		exit(1);
    293  1.5     lukem 	}
    294  1.1      fvdl #endif
    295  1.1      fvdl 
    296  1.1      fvdl 	/*
    297  1.5     lukem 	 * Read a line from netconfig file.
    298  1.1      fvdl 	 */
    299  1.5     lukem 	do {
    300  1.5     lukem 		if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
    301  1.5     lukem 			free(stringp);
    302  1.5     lukem 			ni.eof = 1;
    303  1.5     lukem 			return (NULL);
    304  1.5     lukem 		}
    305  1.5     lukem 	} while (*stringp == '#');
    306  1.5     lukem 
    307  1.5     lukem 	list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
    308  1.5     lukem 	if (list == NULL) {
    309  1.5     lukem 		free(stringp);
    310  1.5     lukem 		return(NULL);
    311  1.5     lukem 	}
    312  1.5     lukem 	np = (struct netconfig *) malloc(sizeof (struct netconfig));
    313  1.5     lukem 	if (np == NULL) {
    314  1.5     lukem 		free(stringp);
    315  1.5     lukem 		free(list);
    316  1.5     lukem 		return(NULL);
    317  1.5     lukem 	}
    318  1.5     lukem 	list->ncp = np;
    319  1.5     lukem 	list->next = NULL;
    320  1.5     lukem 	list->ncp->nc_lookups = NULL;
    321  1.5     lukem 	list->linep = stringp;
    322  1.5     lukem 	if (parse_ncp(stringp, list->ncp) == -1) {
    323  1.5     lukem 		free(stringp);
    324  1.5     lukem 		free(np);
    325  1.5     lukem 		free(list);
    326  1.5     lukem 		return (NULL);
    327  1.5     lukem 	} else {
    328  1.5     lukem 		/*
    329  1.5     lukem 		 * If this is the first entry that's been read, it is the
    330  1.5     lukem 		 * head of the list.  If not, put the entry at the end of
    331  1.5     lukem 		 * the list.  Reposition the current pointer of the handle to
    332  1.5     lukem 		 * the last entry in the list.
    333  1.5     lukem 		 */
    334  1.5     lukem 		if (ni.head == NULL)		/* first entry */
    335  1.5     lukem 			ni.head = ni.tail = list;
    336  1.5     lukem 		else {
    337  1.5     lukem 			ni.tail->next = list;
    338  1.5     lukem 			ni.tail = ni.tail->next;
    339  1.5     lukem 		}
    340  1.5     lukem 		ncp->nc_configs = ni.tail;
    341  1.5     lukem 		return(ni.tail->ncp);
    342  1.1      fvdl 	}
    343  1.1      fvdl }
    344  1.1      fvdl 
    345  1.1      fvdl /*
    346  1.1      fvdl  * endnetconfig() may be called to "unbind" or "close" the netconfig database
    347  1.1      fvdl  * when processing is complete, releasing resources for reuse.  endnetconfig()
    348  1.1      fvdl  * may not be called before setnetconfig().  endnetconfig() returns 0 on
    349  1.1      fvdl  * success and -1 on failure (for example, if setnetconfig() was not called
    350  1.1      fvdl  * previously).
    351  1.1      fvdl  */
    352  1.1      fvdl int
    353  1.1      fvdl endnetconfig(handlep)
    354  1.5     lukem 	void *handlep;
    355  1.1      fvdl {
    356  1.5     lukem 	struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
    357  1.1      fvdl 
    358  1.5     lukem 	struct netconfig_list *q, *p;
    359  1.1      fvdl 
    360  1.5     lukem 	/*
    361  1.5     lukem 	 * Verify that handle is valid
    362  1.5     lukem 	 */
    363  1.5     lukem 	if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
    364  1.1      fvdl 	    nc_handlep->valid != NC_STORAGE)) {
    365  1.5     lukem 		nc_error = NC_NOTINIT;
    366  1.5     lukem 		return (-1);
    367  1.5     lukem 	}
    368  1.5     lukem 
    369  1.5     lukem 	/*
    370  1.5     lukem 	 * Return 0 if anyone still needs it.
    371  1.5     lukem 	 */
    372  1.5     lukem 	nc_handlep->valid = NC_INVALID;
    373  1.5     lukem 	nc_handlep->flag = 0;
    374  1.5     lukem 	nc_handlep->nc_configs = NULL;
    375  1.5     lukem 	if (--ni.ref > 0) {
    376  1.5     lukem 		free(nc_handlep);
    377  1.5     lukem 		return(0);
    378  1.5     lukem 	}
    379  1.5     lukem 
    380  1.5     lukem 	/*
    381  1.5     lukem 	 * Noone needs these entries anymore, then frees them.
    382  1.5     lukem 	 * Make sure all info in netconfig_info structure has been
    383  1.5     lukem 	 * reinitialized.
    384  1.5     lukem 	 */
    385  1.5     lukem 	q = p = ni.head;
    386  1.5     lukem 	ni.eof = ni.ref = 0;
    387  1.5     lukem 	ni.head = NULL;
    388  1.5     lukem 	ni.tail = NULL;
    389  1.5     lukem 	while (q) {
    390  1.5     lukem 		p = q->next;
    391  1.5     lukem 		if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups);
    392  1.5     lukem 		free(q->ncp);
    393  1.5     lukem 		free(q->linep);
    394  1.5     lukem 		free(q);
    395  1.5     lukem 		q = p;
    396  1.5     lukem 	}
    397  1.5     lukem 	free(nc_handlep);
    398  1.5     lukem 
    399  1.5     lukem 	fclose(nc_file);
    400  1.5     lukem 	nc_file = NULL;
    401  1.5     lukem 	return (0);
    402  1.1      fvdl }
    403  1.1      fvdl 
    404  1.1      fvdl /*
    405  1.1      fvdl  * getnetconfigent(netid) returns a pointer to the struct netconfig structure
    406  1.1      fvdl  * corresponding to netid.  It returns NULL if netid is invalid (that is, does
    407  1.1      fvdl  * not name an entry in the netconfig database).  It returns NULL and sets
    408  1.1      fvdl  * errno in case of failure (for example, if the netconfig database cannot be
    409  1.1      fvdl  * opened).
    410  1.1      fvdl  */
    411  1.1      fvdl 
    412  1.1      fvdl struct netconfig *
    413  1.1      fvdl getnetconfigent(netid)
    414  1.1      fvdl 	char *netid;
    415  1.1      fvdl {
    416  1.5     lukem 	FILE *file;			/* NETCONFIG db's file pointer */
    417  1.5     lukem 	char *linep;			/* holds current netconfig line */
    418  1.5     lukem 	char *stringp;			/* temporary string pointer */
    419  1.5     lukem 	struct netconfig *ncp = NULL;   /* returned value */
    420  1.5     lukem 	struct netconfig_list *list;	/* pointer to cache list */
    421  1.1      fvdl 
    422  1.5     lukem 	if (netid == NULL || strlen(netid) == 0)
    423  1.5     lukem 		return (NULL);
    424  1.1      fvdl 
    425  1.5     lukem 	/*
    426  1.5     lukem 	 * Look up table if the entries have already been read and parsed in
    427  1.5     lukem 	 * getnetconfig(), then copy this entry into a buffer and return it.
    428  1.5     lukem 	 * If we cannot find the entry in the current list and there are more
    429  1.5     lukem 	 * entries in the netconfig db that has not been read, we then read the
    430  1.5     lukem 	 * db and try find the match netid.
    431  1.5     lukem 	 * If all the netconfig db has been read and placed into the list and
    432  1.5     lukem 	 * there is no match for the netid, return NULL.
    433  1.5     lukem 	 */
    434  1.5     lukem 	if (ni.head != NULL) {
    435  1.5     lukem 		for (list = ni.head; list; list = list->next) {
    436  1.5     lukem 			if (strcmp(list->ncp->nc_netid, netid) == 0)
    437  1.5     lukem 			    return(dup_ncp(list->ncp));
    438  1.5     lukem 		}
    439  1.5     lukem 		if (ni.eof == 1)	/* that's all the entries */
    440  1.5     lukem 			return(NULL);
    441  1.1      fvdl 	}
    442  1.1      fvdl 
    443  1.5     lukem 	if ((file = fopen(NETCONFIG, "r")) == NULL)
    444  1.5     lukem 	    return (NULL);
    445  1.1      fvdl 
    446  1.5     lukem 	if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
    447  1.5     lukem 		fclose(file);
    448  1.5     lukem 		return (NULL);
    449  1.5     lukem 	}
    450  1.5     lukem 	do {
    451  1.5     lukem 		int len;
    452  1.5     lukem 		char *tmpp;	/* tmp string pointer */
    453  1.1      fvdl 
    454  1.5     lukem 		do {
    455  1.5     lukem 			if ((stringp = fgets(linep, MAXNETCONFIGLINE, file))
    456  1.5     lukem 			    == NULL)
    457  1.5     lukem 				break;
    458  1.5     lukem 		} while (*stringp == '#');
    459  1.5     lukem 		if (stringp == NULL)	/* eof */
    460  1.5     lukem 			break;
    461  1.5     lukem 		if ((tmpp = strpbrk(stringp, "\t ")) == NULL) {
    462  1.5     lukem 					/* can't parse file */
    463  1.5     lukem 			nc_error = NC_BADFILE;
    464  1.5     lukem 			break;
    465  1.5     lukem 		}
    466  1.5     lukem 		if (strlen(netid) == (len = tmpp - stringp) &&	/* a match */
    467  1.5     lukem 		    strncmp(stringp, netid, (size_t)len) == 0) {
    468  1.5     lukem 			if ((ncp = (struct netconfig *)
    469  1.5     lukem 			    malloc(sizeof (struct netconfig))) == NULL)
    470  1.5     lukem 				break;
    471  1.5     lukem 			ncp->nc_lookups = NULL;
    472  1.5     lukem 			if (parse_ncp(linep, ncp) == -1) {
    473  1.5     lukem 				free(ncp);
    474  1.5     lukem 				ncp = NULL;
    475  1.5     lukem 			}
    476  1.5     lukem 			break;
    477  1.5     lukem 		}
    478  1.5     lukem 	} while (stringp != NULL);
    479  1.5     lukem 	if (ncp == NULL)
    480  1.5     lukem 		free(linep);
    481  1.1      fvdl 	fclose(file);
    482  1.5     lukem 	return(ncp);
    483  1.1      fvdl }
    484  1.1      fvdl 
    485  1.1      fvdl /*
    486  1.1      fvdl  * freenetconfigent(netconfigp) frees the netconfig structure pointed to by
    487  1.1      fvdl  * netconfigp (previously returned by getnetconfigent()).
    488  1.1      fvdl  */
    489  1.1      fvdl 
    490  1.1      fvdl void
    491  1.1      fvdl freenetconfigent(netconfigp)
    492  1.1      fvdl 	struct netconfig *netconfigp;
    493  1.1      fvdl {
    494  1.5     lukem 	if (netconfigp != NULL) {
    495  1.5     lukem 				/* holds all netconfigp's strings */
    496  1.5     lukem 		free(netconfigp->nc_netid);
    497  1.5     lukem 		if (netconfigp->nc_lookups != NULL)
    498  1.5     lukem 			free(netconfigp->nc_lookups);
    499  1.5     lukem 		free(netconfigp);
    500  1.5     lukem 	}
    501  1.1      fvdl }
    502  1.1      fvdl 
    503  1.1      fvdl /*
    504  1.1      fvdl  * Parse line and stuff it in a struct netconfig
    505  1.1      fvdl  * Typical line might look like:
    506  1.1      fvdl  *	udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so
    507  1.1      fvdl  *
    508  1.1      fvdl  * We return -1 if any of the tokens don't parse, or malloc fails.
    509  1.1      fvdl  *
    510  1.1      fvdl  * Note that we modify stringp (putting NULLs after tokens) and
    511  1.1      fvdl  * we set the ncp's string field pointers to point to these tokens within
    512  1.1      fvdl  * stringp.
    513  1.1      fvdl  */
    514  1.1      fvdl 
    515  1.1      fvdl static int
    516  1.1      fvdl parse_ncp(stringp, ncp)
    517  1.5     lukem 	char *stringp;		/* string to parse */
    518  1.5     lukem 	struct netconfig *ncp;	/* where to put results */
    519  1.1      fvdl {
    520  1.5     lukem 	char    *tokenp;	/* for processing tokens */
    521  1.5     lukem 	char    *lasts;
    522  1.1      fvdl 
    523  1.5     lukem 	_DIAGASSERT(stringp != NULL);
    524  1.5     lukem 	_DIAGASSERT(ncp != NULL);
    525  1.4     lukem 
    526  1.5     lukem 	nc_error = NC_BADFILE;
    527  1.5     lukem 			/* nearly anything that breaks is for this reason */
    528  1.5     lukem 	stringp[strlen(stringp)-1] = '\0';	/* get rid of newline */
    529  1.5     lukem 	/* netid */
    530  1.5     lukem 	if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL)
    531  1.5     lukem 		return (-1);
    532  1.5     lukem 
    533  1.5     lukem 	/* semantics */
    534  1.5     lukem 	if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
    535  1.5     lukem 		return (-1);
    536  1.5     lukem 	if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
    537  1.5     lukem 		ncp->nc_semantics = NC_TPI_COTS_ORD;
    538  1.5     lukem 	else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
    539  1.5     lukem 		ncp->nc_semantics = NC_TPI_COTS;
    540  1.5     lukem 	else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
    541  1.5     lukem 		ncp->nc_semantics = NC_TPI_CLTS;
    542  1.5     lukem 	else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
    543  1.5     lukem 		ncp->nc_semantics = NC_TPI_RAW;
    544  1.5     lukem 	else
    545  1.5     lukem 		return (-1);
    546  1.5     lukem 
    547  1.5     lukem 	/* flags */
    548  1.5     lukem 	if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
    549  1.5     lukem 		return (-1);
    550  1.5     lukem 	for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0'; tokenp++) {
    551  1.5     lukem 		switch (*tokenp) {
    552  1.5     lukem 		case NC_NOFLAG_C:
    553  1.5     lukem 			break;
    554  1.5     lukem 		case NC_VISIBLE_C:
    555  1.5     lukem 			ncp->nc_flag |= NC_VISIBLE;
    556  1.5     lukem 			break;
    557  1.5     lukem 		case NC_BROADCAST_C:
    558  1.5     lukem 			ncp->nc_flag |= NC_BROADCAST;
    559  1.5     lukem 			break;
    560  1.5     lukem 		default:
    561  1.5     lukem 			return (-1);
    562  1.5     lukem 		}
    563  1.1      fvdl 	}
    564  1.5     lukem 	/* protocol family */
    565  1.5     lukem 	if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL)
    566  1.5     lukem 		return (-1);
    567  1.5     lukem 	/* protocol name */
    568  1.5     lukem 	if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL)
    569  1.5     lukem 		return (-1);
    570  1.5     lukem 	/* network device */
    571  1.5     lukem 	if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL)
    572  1.5     lukem 		return (-1);
    573  1.5     lukem 	if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL)
    574  1.5     lukem 		return (-1);
    575  1.5     lukem 	if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
    576  1.5     lukem 		ncp->nc_nlookups = 0;
    577  1.5     lukem 		ncp->nc_lookups = NULL;
    578  1.5     lukem 	} else {
    579  1.5     lukem 		char *cp;	    /* tmp string */
    580  1.5     lukem 
    581  1.5     lukem 		if (ncp->nc_lookups != NULL)	/* from last visit */
    582  1.5     lukem 			free(ncp->nc_lookups);
    583  1.5     lukem 		/* preallocate one string pointer */
    584  1.5     lukem 		ncp->nc_lookups = (char **)malloc(sizeof (char *));
    585  1.5     lukem 		ncp->nc_nlookups = 0;
    586  1.5     lukem 		while ((cp = tokenp) != NULL) {
    587  1.5     lukem 			tokenp = _get_next_token(cp, ',');
    588  1.5     lukem 			ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp;
    589  1.5     lukem 			ncp->nc_lookups = (char **)
    590  1.5     lukem 			    realloc(ncp->nc_lookups,
    591  1.5     lukem 			    (size_t)(ncp->nc_nlookups+1) *sizeof(char *));
    592  1.5     lukem 						/* for next loop */
    593  1.5     lukem 		}
    594  1.1      fvdl 	}
    595  1.5     lukem 	return (0);
    596  1.1      fvdl }
    597  1.1      fvdl 
    598  1.1      fvdl /*
    599  1.1      fvdl  * Returns a string describing the reason for failure.
    600  1.1      fvdl  */
    601  1.1      fvdl char *
    602  1.1      fvdl nc_sperror()
    603  1.1      fvdl {
    604  1.6  jdolecek 	const char *message;
    605  1.1      fvdl 
    606  1.5     lukem 	switch(nc_error) {
    607  1.5     lukem 	case NC_NONETCONFIG:
    608  1.5     lukem 		message = _nc_errors[0];
    609  1.5     lukem 		break;
    610  1.5     lukem 	case NC_NOMEM:
    611  1.5     lukem 		message = _nc_errors[1];
    612  1.5     lukem 		break;
    613  1.5     lukem 	case NC_NOTINIT:
    614  1.5     lukem 		message = _nc_errors[2];
    615  1.5     lukem 		break;
    616  1.5     lukem 	case NC_BADFILE:
    617  1.5     lukem 		message = _nc_errors[3];
    618  1.5     lukem 		break;
    619  1.5     lukem 	default:
    620  1.5     lukem 		message = "Unknown network selection error";
    621  1.5     lukem 	}
    622  1.6  jdolecek 	/* LINTED const castaway */
    623  1.6  jdolecek 	return ((char *)message);
    624  1.1      fvdl }
    625  1.1      fvdl 
    626  1.1      fvdl /*
    627  1.1      fvdl  * Prints a message onto standard error describing the reason for failure.
    628  1.1      fvdl  */
    629  1.1      fvdl void
    630  1.1      fvdl nc_perror(s)
    631  1.1      fvdl 	const char *s;
    632  1.1      fvdl {
    633  1.4     lukem 
    634  1.5     lukem 	_DIAGASSERT(s != NULL);
    635  1.4     lukem 
    636  1.5     lukem 	fprintf(stderr, "%s: %s", s, nc_sperror());
    637  1.1      fvdl }
    638  1.1      fvdl 
    639  1.1      fvdl /*
    640  1.1      fvdl  * Duplicates the matched netconfig buffer.
    641  1.1      fvdl  */
    642  1.1      fvdl static struct netconfig *
    643  1.5     lukem 	dup_ncp(ncp)
    644  1.5     lukem 	struct netconfig	*ncp;
    645  1.1      fvdl {
    646  1.5     lukem 	struct netconfig	*p;
    647  1.5     lukem 	char	*tmp;
    648  1.5     lukem 	int	i;
    649  1.5     lukem 
    650  1.5     lukem 	_DIAGASSERT(ncp != NULL);
    651  1.5     lukem 
    652  1.5     lukem 	if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
    653  1.5     lukem 		return(NULL);
    654  1.5     lukem 	if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
    655  1.5     lukem 		free(tmp);
    656  1.5     lukem 		return(NULL);
    657  1.5     lukem 	}
    658  1.5     lukem 	/*
    659  1.5     lukem 	 * First we dup all the data from matched netconfig buffer.  Then we
    660  1.5     lukem 	 * adjust some of the member pointer to a pre-allocated buffer where
    661  1.5     lukem 	 * contains part of the data.
    662  1.5     lukem 	 * To follow the convention used in parse_ncp(), we store all the
    663  1.7       wiz 	 * necessary information in the pre-allocated buffer and let each
    664  1.5     lukem 	 * of the netconfig char pointer member point to the right address
    665  1.5     lukem 	 * in the buffer.
    666  1.5     lukem 	 */
    667  1.5     lukem 	*p = *ncp;
    668  1.5     lukem 	p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
    669  1.5     lukem 	tmp = strchr(tmp, NULL) + 1;
    670  1.5     lukem 	p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
    671  1.5     lukem 	tmp = strchr(tmp, NULL) + 1;
    672  1.5     lukem 	p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
    673  1.5     lukem 	tmp = strchr(tmp, NULL) + 1;
    674  1.5     lukem 	p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
    675  1.5     lukem 	p->nc_lookups = (char **)
    676  1.5     lukem 	    malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
    677  1.5     lukem 	if (p->nc_lookups == NULL) {
    678  1.5     lukem 		free(p->nc_netid);
    679  1.5     lukem 		return(NULL);
    680  1.5     lukem 	}
    681  1.5     lukem 	for (i=0; i < p->nc_nlookups; i++) {
    682  1.5     lukem 		tmp = strchr(tmp, NULL) + 1;
    683  1.5     lukem 		p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);
    684  1.5     lukem 	}
    685  1.5     lukem 	return(p);
    686  1.1      fvdl }
    687