xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/getprotoent.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: getprotoent.c,v 1.4 2005/04/27 04:56:26 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <arpa/nameser.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <resolv.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <irs.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "port_after.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "irs_data.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* Forward */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static struct net_data *init(void);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* Public */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate struct protoent *
getprotoent()510Sstevel@tonic-gate getprotoent() {
520Sstevel@tonic-gate 	struct net_data *net_data = init();
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	return (getprotoent_p(net_data));
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct protoent *
getprotobyname(const char * name)580Sstevel@tonic-gate getprotobyname(const char *name) {
590Sstevel@tonic-gate 	struct net_data *net_data = init();
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	return (getprotobyname_p(name, net_data));
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate struct protoent *
getprotobynumber(int proto)650Sstevel@tonic-gate getprotobynumber(int proto) {
660Sstevel@tonic-gate 	struct net_data *net_data = init();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	return (getprotobynumber_p(proto, net_data));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void
setprotoent(int stayopen)720Sstevel@tonic-gate setprotoent(int stayopen) {
730Sstevel@tonic-gate 	struct net_data *net_data = init();
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	setprotoent_p(stayopen, net_data);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate void
endprotoent()790Sstevel@tonic-gate endprotoent() {
800Sstevel@tonic-gate 	struct net_data *net_data = init();
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	endprotoent_p(net_data);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* Shared private. */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate struct protoent *
getprotoent_p(struct net_data * net_data)880Sstevel@tonic-gate getprotoent_p(struct net_data *net_data) {
890Sstevel@tonic-gate 	struct irs_pr *pr;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
920Sstevel@tonic-gate 		return (NULL);
930Sstevel@tonic-gate 	net_data->pr_last = (*pr->next)(pr);
940Sstevel@tonic-gate 	return (net_data->pr_last);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate struct protoent *
getprotobyname_p(const char * name,struct net_data * net_data)980Sstevel@tonic-gate getprotobyname_p(const char *name, struct net_data *net_data) {
990Sstevel@tonic-gate 	struct irs_pr *pr;
1000Sstevel@tonic-gate 	char **pap;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
1030Sstevel@tonic-gate 		return (NULL);
1040Sstevel@tonic-gate 	if (net_data->pr_stayopen && net_data->pr_last) {
1050Sstevel@tonic-gate 		if (!strcmp(net_data->pr_last->p_name, name))
1060Sstevel@tonic-gate 			return (net_data->pr_last);
1070Sstevel@tonic-gate 		for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
1080Sstevel@tonic-gate 			if (!strcmp(name, *pap))
1090Sstevel@tonic-gate 				return (net_data->pr_last);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	net_data->pr_last = (*pr->byname)(pr, name);
1120Sstevel@tonic-gate 	if (!net_data->pr_stayopen)
1130Sstevel@tonic-gate 		endprotoent();
1140Sstevel@tonic-gate 	return (net_data->pr_last);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct protoent *
getprotobynumber_p(int proto,struct net_data * net_data)1180Sstevel@tonic-gate getprotobynumber_p(int proto, struct net_data *net_data) {
1190Sstevel@tonic-gate 	struct irs_pr *pr;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
1220Sstevel@tonic-gate 		return (NULL);
1230Sstevel@tonic-gate 	if (net_data->pr_stayopen && net_data->pr_last)
1240Sstevel@tonic-gate 		if (net_data->pr_last->p_proto == proto)
1250Sstevel@tonic-gate 			return (net_data->pr_last);
1260Sstevel@tonic-gate 	net_data->pr_last = (*pr->bynumber)(pr, proto);
1270Sstevel@tonic-gate 	if (!net_data->pr_stayopen)
1280Sstevel@tonic-gate 		endprotoent();
1290Sstevel@tonic-gate 	return (net_data->pr_last);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate void
setprotoent_p(int stayopen,struct net_data * net_data)1330Sstevel@tonic-gate setprotoent_p(int stayopen, struct net_data *net_data) {
1340Sstevel@tonic-gate 	struct irs_pr *pr;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (!net_data || !(pr = net_data->pr))
1370Sstevel@tonic-gate 		return;
1380Sstevel@tonic-gate 	(*pr->rewind)(pr);
1390Sstevel@tonic-gate 	net_data->pr_stayopen = (stayopen != 0);
1400Sstevel@tonic-gate 	if (stayopen == 0)
1410Sstevel@tonic-gate 		net_data_minimize(net_data);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate void
endprotoent_p(struct net_data * net_data)1450Sstevel@tonic-gate endprotoent_p(struct net_data *net_data) {
1460Sstevel@tonic-gate 	struct irs_pr *pr;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
1490Sstevel@tonic-gate 		(*pr->minimize)(pr);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /* Private */
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static struct net_data *
init()1550Sstevel@tonic-gate init() {
1560Sstevel@tonic-gate 	struct net_data *net_data;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
1590Sstevel@tonic-gate 		goto error;
1600Sstevel@tonic-gate 	if (!net_data->pr) {
1610Sstevel@tonic-gate 		net_data->pr = (*net_data->irs->pr_map)(net_data->irs);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if (!net_data->pr || !net_data->res) {
1640Sstevel@tonic-gate  error:
1650Sstevel@tonic-gate 			errno = EIO;
1660Sstevel@tonic-gate 			return (NULL);
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 		(*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return (net_data);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
175*11038SRao.Shoaib@Sun.COM 
176*11038SRao.Shoaib@Sun.COM /*! \file */
177