Home | History | Annotate | Line # | Download | only in irs
      1      1.1  christos /*	$NetBSD: getprotoent.c,v 1.1.1.2 2012/09/09 16:07:54 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) 1996,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(LINT) && !defined(CODECENTER)
     21  1.1.1.2  christos static const char rcsid[] = "Id: getprotoent.c,v 1.4 2005/04/27 04:56:26 sra Exp ";
     22      1.1  christos #endif
     23      1.1  christos 
     24      1.1  christos /* Imports */
     25      1.1  christos 
     26      1.1  christos #include "port_before.h"
     27      1.1  christos 
     28      1.1  christos #if !defined(__BIND_NOSTATIC)
     29      1.1  christos 
     30      1.1  christos #include <sys/types.h>
     31      1.1  christos 
     32      1.1  christos #include <netinet/in.h>
     33      1.1  christos #include <arpa/nameser.h>
     34      1.1  christos 
     35      1.1  christos #include <errno.h>
     36      1.1  christos #include <resolv.h>
     37      1.1  christos #include <stdio.h>
     38      1.1  christos #include <string.h>
     39      1.1  christos 
     40      1.1  christos #include <irs.h>
     41      1.1  christos 
     42      1.1  christos #include "port_after.h"
     43      1.1  christos 
     44      1.1  christos #include "irs_data.h"
     45      1.1  christos 
     46      1.1  christos /* Forward */
     47      1.1  christos 
     48      1.1  christos static struct net_data *init(void);
     49      1.1  christos 
     50      1.1  christos /* Public */
     51      1.1  christos 
     52      1.1  christos struct protoent *
     53      1.1  christos getprotoent() {
     54      1.1  christos 	struct net_data *net_data = init();
     55      1.1  christos 
     56      1.1  christos 	return (getprotoent_p(net_data));
     57      1.1  christos }
     58      1.1  christos 
     59      1.1  christos struct protoent *
     60      1.1  christos getprotobyname(const char *name) {
     61      1.1  christos 	struct net_data *net_data = init();
     62      1.1  christos 
     63      1.1  christos 	return (getprotobyname_p(name, net_data));
     64      1.1  christos }
     65      1.1  christos 
     66      1.1  christos struct protoent *
     67      1.1  christos getprotobynumber(int proto) {
     68      1.1  christos 	struct net_data *net_data = init();
     69      1.1  christos 
     70      1.1  christos 	return (getprotobynumber_p(proto, net_data));
     71      1.1  christos }
     72      1.1  christos 
     73      1.1  christos void
     74      1.1  christos setprotoent(int stayopen) {
     75      1.1  christos 	struct net_data *net_data = init();
     76      1.1  christos 
     77      1.1  christos 	setprotoent_p(stayopen, net_data);
     78      1.1  christos }
     79      1.1  christos 
     80      1.1  christos void
     81      1.1  christos endprotoent() {
     82      1.1  christos 	struct net_data *net_data = init();
     83      1.1  christos 
     84      1.1  christos 	endprotoent_p(net_data);
     85      1.1  christos }
     86      1.1  christos 
     87      1.1  christos /* Shared private. */
     88      1.1  christos 
     89      1.1  christos struct protoent *
     90      1.1  christos getprotoent_p(struct net_data *net_data) {
     91      1.1  christos 	struct irs_pr *pr;
     92      1.1  christos 
     93      1.1  christos 	if (!net_data || !(pr = net_data->pr))
     94      1.1  christos 		return (NULL);
     95      1.1  christos 	net_data->pr_last = (*pr->next)(pr);
     96      1.1  christos 	return (net_data->pr_last);
     97      1.1  christos }
     98      1.1  christos 
     99      1.1  christos struct protoent *
    100      1.1  christos getprotobyname_p(const char *name, struct net_data *net_data) {
    101      1.1  christos 	struct irs_pr *pr;
    102      1.1  christos 	char **pap;
    103      1.1  christos 
    104      1.1  christos 	if (!net_data || !(pr = net_data->pr))
    105      1.1  christos 		return (NULL);
    106      1.1  christos 	if (net_data->pr_stayopen && net_data->pr_last) {
    107      1.1  christos 		if (!strcmp(net_data->pr_last->p_name, name))
    108      1.1  christos 			return (net_data->pr_last);
    109      1.1  christos 		for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
    110      1.1  christos 			if (!strcmp(name, *pap))
    111      1.1  christos 				return (net_data->pr_last);
    112      1.1  christos 	}
    113      1.1  christos 	net_data->pr_last = (*pr->byname)(pr, name);
    114      1.1  christos 	if (!net_data->pr_stayopen)
    115      1.1  christos 		endprotoent();
    116      1.1  christos 	return (net_data->pr_last);
    117      1.1  christos }
    118      1.1  christos 
    119      1.1  christos struct protoent *
    120      1.1  christos getprotobynumber_p(int proto, struct net_data *net_data) {
    121      1.1  christos 	struct irs_pr *pr;
    122      1.1  christos 
    123      1.1  christos 	if (!net_data || !(pr = net_data->pr))
    124      1.1  christos 		return (NULL);
    125      1.1  christos 	if (net_data->pr_stayopen && net_data->pr_last)
    126      1.1  christos 		if (net_data->pr_last->p_proto == proto)
    127      1.1  christos 			return (net_data->pr_last);
    128      1.1  christos 	net_data->pr_last = (*pr->bynumber)(pr, proto);
    129      1.1  christos 	if (!net_data->pr_stayopen)
    130      1.1  christos 		endprotoent();
    131      1.1  christos 	return (net_data->pr_last);
    132      1.1  christos }
    133      1.1  christos 
    134      1.1  christos void
    135      1.1  christos setprotoent_p(int stayopen, struct net_data *net_data) {
    136      1.1  christos 	struct irs_pr *pr;
    137      1.1  christos 
    138      1.1  christos 	if (!net_data || !(pr = net_data->pr))
    139      1.1  christos 		return;
    140      1.1  christos 	(*pr->rewind)(pr);
    141      1.1  christos 	net_data->pr_stayopen = (stayopen != 0);
    142      1.1  christos 	if (stayopen == 0)
    143      1.1  christos 		net_data_minimize(net_data);
    144      1.1  christos }
    145      1.1  christos 
    146      1.1  christos void
    147      1.1  christos endprotoent_p(struct net_data *net_data) {
    148      1.1  christos 	struct irs_pr *pr;
    149      1.1  christos 
    150      1.1  christos 	if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
    151      1.1  christos 		(*pr->minimize)(pr);
    152      1.1  christos }
    153      1.1  christos 
    154      1.1  christos /* Private */
    155      1.1  christos 
    156      1.1  christos static struct net_data *
    157      1.1  christos init() {
    158      1.1  christos 	struct net_data *net_data;
    159      1.1  christos 
    160      1.1  christos 	if (!(net_data = net_data_init(NULL)))
    161      1.1  christos 		goto error;
    162      1.1  christos 	if (!net_data->pr) {
    163      1.1  christos 		net_data->pr = (*net_data->irs->pr_map)(net_data->irs);
    164      1.1  christos 
    165      1.1  christos 		if (!net_data->pr || !net_data->res) {
    166      1.1  christos  error:
    167      1.1  christos 			errno = EIO;
    168      1.1  christos 			return (NULL);
    169      1.1  christos 		}
    170      1.1  christos 		(*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
    171      1.1  christos 	}
    172      1.1  christos 
    173      1.1  christos 	return (net_data);
    174      1.1  christos }
    175      1.1  christos 
    176      1.1  christos #endif /*__BIND_NOSTATIC*/
    177      1.1  christos 
    178      1.1  christos /*! \file */
    179