17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Imports */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <resolv.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <irs.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "port_after.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "irs_data.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* Forward */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static struct net_data *init(void);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Public */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate struct servent *
getservent(void)477c478bd9Sstevel@tonic-gate getservent(void) {
487c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (getservent_p(net_data));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct servent *
getservbyname(const char * name,const char * proto)547c478bd9Sstevel@tonic-gate getservbyname(const char *name, const char *proto) {
557c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	return (getservbyname_p(name, proto, net_data));
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct servent *
getservbyport(int port,const char * proto)617c478bd9Sstevel@tonic-gate getservbyport(int port, const char *proto) {
627c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	return (getservbyport_p(port, proto, net_data));
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate void
setservent(int stayopen)687c478bd9Sstevel@tonic-gate setservent(int stayopen) {
697c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	setservent_p(stayopen, net_data);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate void
endservent()757c478bd9Sstevel@tonic-gate endservent() {
767c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	endservent_p(net_data);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* Shared private. */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate struct servent *
getservent_p(struct net_data * net_data)847c478bd9Sstevel@tonic-gate getservent_p(struct net_data *net_data) {
857c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (!net_data || !(sv = net_data->sv))
887c478bd9Sstevel@tonic-gate 		return (NULL);
897c478bd9Sstevel@tonic-gate 	net_data->sv_last = (*sv->next)(sv);
907c478bd9Sstevel@tonic-gate 	return (net_data->sv_last);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate struct servent *
getservbyname_p(const char * name,const char * proto,struct net_data * net_data)947c478bd9Sstevel@tonic-gate getservbyname_p(const char *name, const char *proto,
957c478bd9Sstevel@tonic-gate 		struct net_data *net_data) {
967c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
977c478bd9Sstevel@tonic-gate 	char **sap;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (!net_data || !(sv = net_data->sv))
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 	if (net_data->sv_stayopen && net_data->sv_last)
1027c478bd9Sstevel@tonic-gate 		if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
1037c478bd9Sstevel@tonic-gate 			if (!strcmp(net_data->sv_last->s_name, name))
1047c478bd9Sstevel@tonic-gate 				return (net_data->sv_last);
1057c478bd9Sstevel@tonic-gate 			for (sap = net_data->sv_last->s_aliases;
1067c478bd9Sstevel@tonic-gate 			     sap && *sap; sap++)
1077c478bd9Sstevel@tonic-gate 				if (!strcmp(name, *sap))
1087c478bd9Sstevel@tonic-gate 					return (net_data->sv_last);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 	net_data->sv_last = (*sv->byname)(sv, name, proto);
1117c478bd9Sstevel@tonic-gate 	if (!net_data->sv_stayopen)
1127c478bd9Sstevel@tonic-gate 		endservent();
1137c478bd9Sstevel@tonic-gate 	return (net_data->sv_last);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate struct servent *
getservbyport_p(int port,const char * proto,struct net_data * net_data)1177c478bd9Sstevel@tonic-gate getservbyport_p(int port, const char *proto, struct net_data *net_data) {
1187c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (!net_data || !(sv = net_data->sv))
1217c478bd9Sstevel@tonic-gate 		return (NULL);
1227c478bd9Sstevel@tonic-gate 	if (net_data->sv_stayopen && net_data->sv_last)
1237c478bd9Sstevel@tonic-gate 		if (port == net_data->sv_last->s_port &&
1247c478bd9Sstevel@tonic-gate 		    ( !proto ||
1257c478bd9Sstevel@tonic-gate 		     !strcmp(net_data->sv_last->s_proto, proto)))
1267c478bd9Sstevel@tonic-gate 			return (net_data->sv_last);
1277c478bd9Sstevel@tonic-gate 	net_data->sv_last = (*sv->byport)(sv, port, proto);
1287c478bd9Sstevel@tonic-gate 	return (net_data->sv_last);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate void
setservent_p(int stayopen,struct net_data * net_data)1327c478bd9Sstevel@tonic-gate setservent_p(int stayopen, struct net_data *net_data) {
1337c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (!net_data || !(sv = net_data->sv))
1367c478bd9Sstevel@tonic-gate 		return;
1377c478bd9Sstevel@tonic-gate 	(*sv->rewind)(sv);
1387c478bd9Sstevel@tonic-gate 	net_data->sv_stayopen = (stayopen != 0);
1397c478bd9Sstevel@tonic-gate 	if (stayopen == 0)
1407c478bd9Sstevel@tonic-gate 		net_data_minimize(net_data);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate void
endservent_p(struct net_data * net_data)1447c478bd9Sstevel@tonic-gate endservent_p(struct net_data *net_data) {
1457c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
1487c478bd9Sstevel@tonic-gate 		(*sv->minimize)(sv);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* Private */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static struct net_data *
init()1547c478bd9Sstevel@tonic-gate init() {
1557c478bd9Sstevel@tonic-gate 	struct net_data *net_data;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
1587c478bd9Sstevel@tonic-gate 		goto error;
1597c478bd9Sstevel@tonic-gate 	if (!net_data->sv) {
1607c478bd9Sstevel@tonic-gate 		net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		if (!net_data->sv || !net_data->res) {
163*55fea89dSDan Cross  error:
1647c478bd9Sstevel@tonic-gate 			errno = EIO;
1657c478bd9Sstevel@tonic-gate 			return (NULL);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		(*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
1687c478bd9Sstevel@tonic-gate 	}
169*55fea89dSDan Cross 
1707c478bd9Sstevel@tonic-gate 	return (net_data);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
1749525b14bSRao Shoaib 
1759525b14bSRao Shoaib /*! \file */
176