Home | History | Annotate | Line # | Download | only in irs
getnetent_r.c revision 1.1
      1  1.1  christos /*	$NetBSD: getnetent_r.c,v 1.1 2009/04/12 15:33:41 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  * Copyright (c) 1998-1999 by Internet Software Consortium.
      6  1.1  christos  *
      7  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  christos  * copyright notice and this permission notice appear in all copies.
     10  1.1  christos  *
     11  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  christos  */
     19  1.1  christos 
     20  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1  christos static const char rcsid[] = "Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp";
     22  1.1  christos #endif /* LIBC_SCCS and not lint */
     23  1.1  christos 
     24  1.1  christos #include <port_before.h>
     25  1.1  christos #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
     26  1.1  christos 	static int getnetent_r_not_required = 0;
     27  1.1  christos #else
     28  1.1  christos #include <errno.h>
     29  1.1  christos #include <string.h>
     30  1.1  christos #include <stdio.h>
     31  1.1  christos #include <sys/types.h>
     32  1.1  christos #include <netinet/in.h>
     33  1.1  christos #include <netdb.h>
     34  1.1  christos #include <sys/param.h>
     35  1.1  christos #include <port_after.h>
     36  1.1  christos 
     37  1.1  christos #ifdef NET_R_RETURN
     38  1.1  christos 
     39  1.1  christos static NET_R_RETURN
     40  1.1  christos copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS);
     41  1.1  christos 
     42  1.1  christos NET_R_RETURN
     43  1.1  christos getnetbyname_r(const char *name,  struct netent *nptr, NET_R_ARGS) {
     44  1.1  christos 	struct netent *ne = getnetbyname(name);
     45  1.1  christos #ifdef NET_R_SETANSWER
     46  1.1  christos 	int n = 0;
     47  1.1  christos 
     48  1.1  christos 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
     49  1.1  christos 		*answerp = NULL;
     50  1.1  christos 	else
     51  1.1  christos 		*answerp = ne;
     52  1.1  christos 	if (ne == NULL)
     53  1.1  christos 		*h_errnop = h_errno;
     54  1.1  christos 	return (n);
     55  1.1  christos #else
     56  1.1  christos 	if (ne == NULL)
     57  1.1  christos 		return (NET_R_BAD);
     58  1.1  christos 
     59  1.1  christos 	return (copy_netent(ne, nptr, NET_R_COPY));
     60  1.1  christos #endif
     61  1.1  christos }
     62  1.1  christos 
     63  1.1  christos #ifndef GETNETBYADDR_ADDR_T
     64  1.1  christos #define GETNETBYADDR_ADDR_T long
     65  1.1  christos #endif
     66  1.1  christos NET_R_RETURN
     67  1.1  christos getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) {
     68  1.1  christos 	struct netent *ne = getnetbyaddr(addr, type);
     69  1.1  christos #ifdef NET_R_SETANSWER
     70  1.1  christos 	int n = 0;
     71  1.1  christos 
     72  1.1  christos 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
     73  1.1  christos 		*answerp = NULL;
     74  1.1  christos 	else
     75  1.1  christos 		*answerp = ne;
     76  1.1  christos 	if (ne == NULL)
     77  1.1  christos 		*h_errnop = h_errno;
     78  1.1  christos 	return (n);
     79  1.1  christos #else
     80  1.1  christos 
     81  1.1  christos 	if (ne == NULL)
     82  1.1  christos 		return (NET_R_BAD);
     83  1.1  christos 
     84  1.1  christos 	return (copy_netent(ne, nptr, NET_R_COPY));
     85  1.1  christos #endif
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos /*%
     89  1.1  christos  *	These assume a single context is in operation per thread.
     90  1.1  christos  *	If this is not the case we will need to call irs directly
     91  1.1  christos  *	rather than through the base functions.
     92  1.1  christos  */
     93  1.1  christos 
     94  1.1  christos NET_R_RETURN
     95  1.1  christos getnetent_r(struct netent *nptr, NET_R_ARGS) {
     96  1.1  christos 	struct netent *ne = getnetent();
     97  1.1  christos #ifdef NET_R_SETANSWER
     98  1.1  christos 	int n = 0;
     99  1.1  christos 
    100  1.1  christos 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
    101  1.1  christos 		*answerp = NULL;
    102  1.1  christos 	else
    103  1.1  christos 		*answerp = ne;
    104  1.1  christos 	if (ne == NULL)
    105  1.1  christos 		*h_errnop = h_errno;
    106  1.1  christos 	return (n);
    107  1.1  christos #else
    108  1.1  christos 
    109  1.1  christos 	if (ne == NULL)
    110  1.1  christos 		return (NET_R_BAD);
    111  1.1  christos 
    112  1.1  christos 	return (copy_netent(ne, nptr, NET_R_COPY));
    113  1.1  christos #endif
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos NET_R_SET_RETURN
    117  1.1  christos #ifdef NET_R_ENT_ARGS
    118  1.1  christos setnetent_r(int stay_open, NET_R_ENT_ARGS)
    119  1.1  christos #else
    120  1.1  christos setnetent_r(int stay_open)
    121  1.1  christos #endif
    122  1.1  christos {
    123  1.1  christos #ifdef NET_R_ENT_ARGS
    124  1.1  christos 	UNUSED(ndptr);
    125  1.1  christos #endif
    126  1.1  christos 	setnetent(stay_open);
    127  1.1  christos #ifdef NET_R_SET_RESULT
    128  1.1  christos 	return (NET_R_SET_RESULT);
    129  1.1  christos #endif
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos NET_R_END_RETURN
    133  1.1  christos #ifdef NET_R_ENT_ARGS
    134  1.1  christos endnetent_r(NET_R_ENT_ARGS)
    135  1.1  christos #else
    136  1.1  christos endnetent_r()
    137  1.1  christos #endif
    138  1.1  christos {
    139  1.1  christos #ifdef NET_R_ENT_ARGS
    140  1.1  christos 	UNUSED(ndptr);
    141  1.1  christos #endif
    142  1.1  christos 	endnetent();
    143  1.1  christos 	NET_R_END_RESULT(NET_R_OK);
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos /* Private */
    147  1.1  christos 
    148  1.1  christos #ifndef NETENT_DATA
    149  1.1  christos static NET_R_RETURN
    150  1.1  christos copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
    151  1.1  christos 	char *cp;
    152  1.1  christos 	int i, n;
    153  1.1  christos 	int numptr, len;
    154  1.1  christos 
    155  1.1  christos 	/* Find out the amount of space required to store the answer. */
    156  1.1  christos 	numptr = 1; /*%< NULL ptr */
    157  1.1  christos 	len = (char *)ALIGN(buf) - buf;
    158  1.1  christos 	for (i = 0; ne->n_aliases[i]; i++, numptr++) {
    159  1.1  christos 		len += strlen(ne->n_aliases[i]) + 1;
    160  1.1  christos 	}
    161  1.1  christos 	len += strlen(ne->n_name) + 1;
    162  1.1  christos 	len += numptr * sizeof(char*);
    163  1.1  christos 
    164  1.1  christos 	if (len > (int)buflen) {
    165  1.1  christos 		errno = ERANGE;
    166  1.1  christos 		return (NET_R_BAD);
    167  1.1  christos 	}
    168  1.1  christos 
    169  1.1  christos 	/* copy net value and type */
    170  1.1  christos 	nptr->n_addrtype = ne->n_addrtype;
    171  1.1  christos 	nptr->n_net = ne->n_net;
    172  1.1  christos 
    173  1.1  christos 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
    174  1.1  christos 
    175  1.1  christos 	/* copy official name */
    176  1.1  christos 	n = strlen(ne->n_name) + 1;
    177  1.1  christos 	strcpy(cp, ne->n_name);
    178  1.1  christos 	nptr->n_name = cp;
    179  1.1  christos 	cp += n;
    180  1.1  christos 
    181  1.1  christos 	/* copy aliases */
    182  1.1  christos 	nptr->n_aliases = (char **)ALIGN(buf);
    183  1.1  christos 	for (i = 0 ; ne->n_aliases[i]; i++) {
    184  1.1  christos 		n = strlen(ne->n_aliases[i]) + 1;
    185  1.1  christos 		strcpy(cp, ne->n_aliases[i]);
    186  1.1  christos 		nptr->n_aliases[i] = cp;
    187  1.1  christos 		cp += n;
    188  1.1  christos 	}
    189  1.1  christos 	nptr->n_aliases[i] = NULL;
    190  1.1  christos 
    191  1.1  christos 	return (NET_R_OK);
    192  1.1  christos }
    193  1.1  christos #else /* !NETENT_DATA */
    194  1.1  christos static int
    195  1.1  christos copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
    196  1.1  christos 	char *cp, *eob;
    197  1.1  christos 	int i, n;
    198  1.1  christos 
    199  1.1  christos 	/* copy net value and type */
    200  1.1  christos 	nptr->n_addrtype = ne->n_addrtype;
    201  1.1  christos 	nptr->n_net = ne->n_net;
    202  1.1  christos 
    203  1.1  christos 	/* copy official name */
    204  1.1  christos 	cp = ndptr->line;
    205  1.1  christos 	eob = ndptr->line + sizeof(ndptr->line);
    206  1.1  christos 	if ((n = strlen(ne->n_name) + 1) < (eob - cp)) {
    207  1.1  christos 		strcpy(cp, ne->n_name);
    208  1.1  christos 		nptr->n_name = cp;
    209  1.1  christos 		cp += n;
    210  1.1  christos 	} else {
    211  1.1  christos 		return (-1);
    212  1.1  christos 	}
    213  1.1  christos 
    214  1.1  christos 	/* copy aliases */
    215  1.1  christos 	i = 0;
    216  1.1  christos 	nptr->n_aliases = ndptr->net_aliases;
    217  1.1  christos 	while (ne->n_aliases[i] && i < (_MAXALIASES-1)) {
    218  1.1  christos 		if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) {
    219  1.1  christos 			strcpy(cp, ne->n_aliases[i]);
    220  1.1  christos 			nptr->n_aliases[i] = cp;
    221  1.1  christos 			cp += n;
    222  1.1  christos 		} else {
    223  1.1  christos 			break;
    224  1.1  christos 		}
    225  1.1  christos 		i++;
    226  1.1  christos 	}
    227  1.1  christos 	nptr->n_aliases[i] = NULL;
    228  1.1  christos 
    229  1.1  christos 	return (NET_R_OK);
    230  1.1  christos }
    231  1.1  christos #endif /* !NETENT_DATA */
    232  1.1  christos #else /* NET_R_RETURN */
    233  1.1  christos 	static int getnetent_r_unknown_system = 0;
    234  1.1  christos #endif /* NET_R_RETURN */
    235  1.1  christos #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
    236  1.1  christos /*! \file */
    237