Home | History | Annotate | Line # | Download | only in irs
getprotoent_r.c revision 1.1
      1  1.1  christos /*	$NetBSD: getprotoent_r.c,v 1.1 2009/04/12 15:33:43 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: getprotoent_r.c,v 1.6 2006/08/01 01:14:16 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 getprotoent_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 <port_after.h>
     35  1.1  christos 
     36  1.1  christos #ifdef PROTO_R_RETURN
     37  1.1  christos 
     38  1.1  christos static PROTO_R_RETURN
     39  1.1  christos copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS);
     40  1.1  christos 
     41  1.1  christos PROTO_R_RETURN
     42  1.1  christos getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
     43  1.1  christos 	struct protoent *pe = getprotobyname(name);
     44  1.1  christos #ifdef PROTO_R_SETANSWER
     45  1.1  christos 	int n = 0;
     46  1.1  christos 
     47  1.1  christos 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
     48  1.1  christos 		*answerp = NULL;
     49  1.1  christos 	else
     50  1.1  christos 		*answerp = pptr;
     51  1.1  christos 
     52  1.1  christos 	return (n);
     53  1.1  christos #else
     54  1.1  christos 	if (pe == NULL)
     55  1.1  christos 		return (PROTO_R_BAD);
     56  1.1  christos 
     57  1.1  christos 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
     58  1.1  christos #endif
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos PROTO_R_RETURN
     62  1.1  christos getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) {
     63  1.1  christos 	struct protoent *pe = getprotobynumber(proto);
     64  1.1  christos #ifdef PROTO_R_SETANSWER
     65  1.1  christos 	int n = 0;
     66  1.1  christos 
     67  1.1  christos 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
     68  1.1  christos 		*answerp = NULL;
     69  1.1  christos 	else
     70  1.1  christos 		*answerp = pptr;
     71  1.1  christos 
     72  1.1  christos 	return (n);
     73  1.1  christos #else
     74  1.1  christos 	if (pe == NULL)
     75  1.1  christos 		return (PROTO_R_BAD);
     76  1.1  christos 
     77  1.1  christos 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
     78  1.1  christos #endif
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos /*%
     82  1.1  christos  *	These assume a single context is in operation per thread.
     83  1.1  christos  *	If this is not the case we will need to call irs directly
     84  1.1  christos  *	rather than through the base functions.
     85  1.1  christos  */
     86  1.1  christos 
     87  1.1  christos PROTO_R_RETURN
     88  1.1  christos getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) {
     89  1.1  christos 	struct protoent *pe = getprotoent();
     90  1.1  christos #ifdef PROTO_R_SETANSWER
     91  1.1  christos 	int n = 0;
     92  1.1  christos 
     93  1.1  christos 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
     94  1.1  christos 		*answerp = NULL;
     95  1.1  christos 	else
     96  1.1  christos 		*answerp = pptr;
     97  1.1  christos 
     98  1.1  christos 	return (n);
     99  1.1  christos #else
    100  1.1  christos 	if (pe == NULL)
    101  1.1  christos 		return (PROTO_R_BAD);
    102  1.1  christos 
    103  1.1  christos 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
    104  1.1  christos #endif
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos PROTO_R_SET_RETURN
    108  1.1  christos #ifdef PROTO_R_ENT_ARGS
    109  1.1  christos setprotoent_r(int stay_open, PROTO_R_ENT_ARGS)
    110  1.1  christos #else
    111  1.1  christos setprotoent_r(int stay_open)
    112  1.1  christos #endif
    113  1.1  christos {
    114  1.1  christos #ifdef PROTO_R_ENT_UNUSED
    115  1.1  christos         PROTO_R_ENT_UNUSED;
    116  1.1  christos #endif
    117  1.1  christos 	setprotoent(stay_open);
    118  1.1  christos #ifdef PROTO_R_SET_RESULT
    119  1.1  christos 	return (PROTO_R_SET_RESULT);
    120  1.1  christos #endif
    121  1.1  christos }
    122  1.1  christos 
    123  1.1  christos PROTO_R_END_RETURN
    124  1.1  christos #ifdef PROTO_R_ENT_ARGS
    125  1.1  christos endprotoent_r(PROTO_R_ENT_ARGS)
    126  1.1  christos #else
    127  1.1  christos endprotoent_r()
    128  1.1  christos #endif
    129  1.1  christos {
    130  1.1  christos #ifdef PROTO_R_ENT_UNUSED
    131  1.1  christos         PROTO_R_ENT_UNUSED;
    132  1.1  christos #endif
    133  1.1  christos 	endprotoent();
    134  1.1  christos 	PROTO_R_END_RESULT(PROTO_R_OK);
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos /* Private */
    138  1.1  christos 
    139  1.1  christos #ifndef PROTOENT_DATA
    140  1.1  christos static PROTO_R_RETURN
    141  1.1  christos copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
    142  1.1  christos 	char *cp;
    143  1.1  christos 	int i, n;
    144  1.1  christos 	int numptr, len;
    145  1.1  christos 
    146  1.1  christos 	/* Find out the amount of space required to store the answer. */
    147  1.1  christos 	numptr = 1; /*%< NULL ptr */
    148  1.1  christos 	len = (char *)ALIGN(buf) - buf;
    149  1.1  christos 	for (i = 0; pe->p_aliases[i]; i++, numptr++) {
    150  1.1  christos 		len += strlen(pe->p_aliases[i]) + 1;
    151  1.1  christos 	}
    152  1.1  christos 	len += strlen(pe->p_name) + 1;
    153  1.1  christos 	len += numptr * sizeof(char*);
    154  1.1  christos 
    155  1.1  christos 	if (len > (int)buflen) {
    156  1.1  christos 		errno = ERANGE;
    157  1.1  christos 		return (PROTO_R_BAD);
    158  1.1  christos 	}
    159  1.1  christos 
    160  1.1  christos 	/* copy protocol value*/
    161  1.1  christos 	pptr->p_proto = pe->p_proto;
    162  1.1  christos 
    163  1.1  christos 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
    164  1.1  christos 
    165  1.1  christos 	/* copy official name */
    166  1.1  christos 	n = strlen(pe->p_name) + 1;
    167  1.1  christos 	strcpy(cp, pe->p_name);
    168  1.1  christos 	pptr->p_name = cp;
    169  1.1  christos 	cp += n;
    170  1.1  christos 
    171  1.1  christos 	/* copy aliases */
    172  1.1  christos 	pptr->p_aliases = (char **)ALIGN(buf);
    173  1.1  christos 	for (i = 0 ; pe->p_aliases[i]; i++) {
    174  1.1  christos 		n = strlen(pe->p_aliases[i]) + 1;
    175  1.1  christos 		strcpy(cp, pe->p_aliases[i]);
    176  1.1  christos 		pptr->p_aliases[i] = cp;
    177  1.1  christos 		cp += n;
    178  1.1  christos 	}
    179  1.1  christos 	pptr->p_aliases[i] = NULL;
    180  1.1  christos 
    181  1.1  christos 	return (PROTO_R_OK);
    182  1.1  christos }
    183  1.1  christos #else /* !PROTOENT_DATA */
    184  1.1  christos static int
    185  1.1  christos copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
    186  1.1  christos 	char *cp, *eob;
    187  1.1  christos 	int i, n;
    188  1.1  christos 
    189  1.1  christos 	/* copy protocol value */
    190  1.1  christos 	pptr->p_proto = pe->p_proto;
    191  1.1  christos 
    192  1.1  christos 	/* copy official name */
    193  1.1  christos 	cp = pdptr->line;
    194  1.1  christos 	eob = pdptr->line + sizeof(pdptr->line);
    195  1.1  christos 	if ((n = strlen(pe->p_name) + 1) < (eob - cp)) {
    196  1.1  christos 		strcpy(cp, pe->p_name);
    197  1.1  christos 		pptr->p_name = cp;
    198  1.1  christos 		cp += n;
    199  1.1  christos 	} else {
    200  1.1  christos 		return (-1);
    201  1.1  christos 	}
    202  1.1  christos 
    203  1.1  christos 	/* copy aliases */
    204  1.1  christos 	i = 0;
    205  1.1  christos 	pptr->p_aliases = pdptr->proto_aliases;
    206  1.1  christos 	while (pe->p_aliases[i] && i < (_MAXALIASES-1)) {
    207  1.1  christos 		if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) {
    208  1.1  christos 			strcpy(cp, pe->p_aliases[i]);
    209  1.1  christos 			pptr->p_aliases[i] = cp;
    210  1.1  christos 			cp += n;
    211  1.1  christos 		} else {
    212  1.1  christos 			break;
    213  1.1  christos 		}
    214  1.1  christos 		i++;
    215  1.1  christos 	}
    216  1.1  christos 	pptr->p_aliases[i] = NULL;
    217  1.1  christos 
    218  1.1  christos 	return (PROTO_R_OK);
    219  1.1  christos }
    220  1.1  christos #endif /* PROTOENT_DATA */
    221  1.1  christos #else /* PROTO_R_RETURN */
    222  1.1  christos 	static int getprotoent_r_unknown_system = 0;
    223  1.1  christos #endif /* PROTO_R_RETURN */
    224  1.1  christos #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
    225  1.1  christos /*! \file */
    226