Home | History | Annotate | Line # | Download | only in irs
getservent.c revision 1.1.1.2
      1 /*	$NetBSD: getservent.c,v 1.1.1.2 2012/09/09 16:07:50 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #if !defined(LINT) && !defined(CODECENTER)
     21 static const char rcsid[] = "Id: getservent.c,v 1.4 2005/04/27 04:56:26 sra Exp ";
     22 #endif
     23 
     24 /* Imports */
     25 
     26 #include "port_before.h"
     27 
     28 #if !defined(__BIND_NOSTATIC)
     29 
     30 #include <sys/types.h>
     31 
     32 #include <netinet/in.h>
     33 #include <arpa/nameser.h>
     34 
     35 #include <errno.h>
     36 #include <resolv.h>
     37 #include <stdio.h>
     38 #include <string.h>
     39 
     40 #include <irs.h>
     41 
     42 #include "port_after.h"
     43 
     44 #include "irs_data.h"
     45 
     46 /* Forward */
     47 
     48 static struct net_data *init(void);
     49 
     50 /* Public */
     51 
     52 struct servent *
     53 getservent(void) {
     54 	struct net_data *net_data = init();
     55 
     56 	return (getservent_p(net_data));
     57 }
     58 
     59 struct servent *
     60 getservbyname(const char *name, const char *proto) {
     61 	struct net_data *net_data = init();
     62 
     63 	return (getservbyname_p(name, proto, net_data));
     64 }
     65 
     66 struct servent *
     67 getservbyport(int port, const char *proto) {
     68 	struct net_data *net_data = init();
     69 
     70 	return (getservbyport_p(port, proto, net_data));
     71 }
     72 
     73 void
     74 setservent(int stayopen) {
     75 	struct net_data *net_data = init();
     76 
     77 	setservent_p(stayopen, net_data);
     78 }
     79 
     80 void
     81 endservent() {
     82 	struct net_data *net_data = init();
     83 
     84 	endservent_p(net_data);
     85 }
     86 
     87 /* Shared private. */
     88 
     89 struct servent *
     90 getservent_p(struct net_data *net_data) {
     91 	struct irs_sv *sv;
     92 
     93 	if (!net_data || !(sv = net_data->sv))
     94 		return (NULL);
     95 	net_data->sv_last = (*sv->next)(sv);
     96 	return (net_data->sv_last);
     97 }
     98 
     99 struct servent *
    100 getservbyname_p(const char *name, const char *proto,
    101 		struct net_data *net_data) {
    102 	struct irs_sv *sv;
    103 	char **sap;
    104 
    105 	if (!net_data || !(sv = net_data->sv))
    106 		return (NULL);
    107 	if (net_data->sv_stayopen && net_data->sv_last)
    108 		if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
    109 			if (!strcmp(net_data->sv_last->s_name, name))
    110 				return (net_data->sv_last);
    111 			for (sap = net_data->sv_last->s_aliases;
    112 			     sap && *sap; sap++)
    113 				if (!strcmp(name, *sap))
    114 					return (net_data->sv_last);
    115 		}
    116 	net_data->sv_last = (*sv->byname)(sv, name, proto);
    117 	if (!net_data->sv_stayopen)
    118 		endservent();
    119 	return (net_data->sv_last);
    120 }
    121 
    122 struct servent *
    123 getservbyport_p(int port, const char *proto, struct net_data *net_data) {
    124 	struct irs_sv *sv;
    125 
    126 	if (!net_data || !(sv = net_data->sv))
    127 		return (NULL);
    128 	if (net_data->sv_stayopen && net_data->sv_last)
    129 		if (port == net_data->sv_last->s_port &&
    130 		    ( !proto ||
    131 		     !strcmp(net_data->sv_last->s_proto, proto)))
    132 			return (net_data->sv_last);
    133 	net_data->sv_last = (*sv->byport)(sv, port, proto);
    134 	return (net_data->sv_last);
    135 }
    136 
    137 void
    138 setservent_p(int stayopen, struct net_data *net_data) {
    139 	struct irs_sv *sv;
    140 
    141 	if (!net_data || !(sv = net_data->sv))
    142 		return;
    143 	(*sv->rewind)(sv);
    144 	net_data->sv_stayopen = (stayopen != 0);
    145 	if (stayopen == 0)
    146 		net_data_minimize(net_data);
    147 }
    148 
    149 void
    150 endservent_p(struct net_data *net_data) {
    151 	struct irs_sv *sv;
    152 
    153 	if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
    154 		(*sv->minimize)(sv);
    155 }
    156 
    157 /* Private */
    158 
    159 static struct net_data *
    160 init() {
    161 	struct net_data *net_data;
    162 
    163 	if (!(net_data = net_data_init(NULL)))
    164 		goto error;
    165 	if (!net_data->sv) {
    166 		net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
    167 
    168 		if (!net_data->sv || !net_data->res) {
    169  error:
    170 			errno = EIO;
    171 			return (NULL);
    172 		}
    173 		(*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
    174 	}
    175 
    176 	return (net_data);
    177 }
    178 
    179 #endif /*__BIND_NOSTATIC*/
    180 
    181 /*! \file */
    182