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: getservent.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 servent *
getservent(void)510Sstevel@tonic-gate getservent(void) {
520Sstevel@tonic-gate struct net_data *net_data = init();
530Sstevel@tonic-gate
540Sstevel@tonic-gate return (getservent_p(net_data));
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate struct servent *
getservbyname(const char * name,const char * proto)580Sstevel@tonic-gate getservbyname(const char *name, const char *proto) {
590Sstevel@tonic-gate struct net_data *net_data = init();
600Sstevel@tonic-gate
610Sstevel@tonic-gate return (getservbyname_p(name, proto, net_data));
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate struct servent *
getservbyport(int port,const char * proto)650Sstevel@tonic-gate getservbyport(int port, const char *proto) {
660Sstevel@tonic-gate struct net_data *net_data = init();
670Sstevel@tonic-gate
680Sstevel@tonic-gate return (getservbyport_p(port, proto, net_data));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate void
setservent(int stayopen)720Sstevel@tonic-gate setservent(int stayopen) {
730Sstevel@tonic-gate struct net_data *net_data = init();
740Sstevel@tonic-gate
750Sstevel@tonic-gate setservent_p(stayopen, net_data);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate void
endservent()790Sstevel@tonic-gate endservent() {
800Sstevel@tonic-gate struct net_data *net_data = init();
810Sstevel@tonic-gate
820Sstevel@tonic-gate endservent_p(net_data);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate /* Shared private. */
860Sstevel@tonic-gate
870Sstevel@tonic-gate struct servent *
getservent_p(struct net_data * net_data)880Sstevel@tonic-gate getservent_p(struct net_data *net_data) {
890Sstevel@tonic-gate struct irs_sv *sv;
900Sstevel@tonic-gate
910Sstevel@tonic-gate if (!net_data || !(sv = net_data->sv))
920Sstevel@tonic-gate return (NULL);
930Sstevel@tonic-gate net_data->sv_last = (*sv->next)(sv);
940Sstevel@tonic-gate return (net_data->sv_last);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate struct servent *
getservbyname_p(const char * name,const char * proto,struct net_data * net_data)980Sstevel@tonic-gate getservbyname_p(const char *name, const char *proto,
990Sstevel@tonic-gate struct net_data *net_data) {
1000Sstevel@tonic-gate struct irs_sv *sv;
1010Sstevel@tonic-gate char **sap;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (!net_data || !(sv = net_data->sv))
1040Sstevel@tonic-gate return (NULL);
1050Sstevel@tonic-gate if (net_data->sv_stayopen && net_data->sv_last)
1060Sstevel@tonic-gate if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
1070Sstevel@tonic-gate if (!strcmp(net_data->sv_last->s_name, name))
1080Sstevel@tonic-gate return (net_data->sv_last);
1090Sstevel@tonic-gate for (sap = net_data->sv_last->s_aliases;
1100Sstevel@tonic-gate sap && *sap; sap++)
1110Sstevel@tonic-gate if (!strcmp(name, *sap))
1120Sstevel@tonic-gate return (net_data->sv_last);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate net_data->sv_last = (*sv->byname)(sv, name, proto);
1150Sstevel@tonic-gate if (!net_data->sv_stayopen)
1160Sstevel@tonic-gate endservent();
1170Sstevel@tonic-gate return (net_data->sv_last);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate struct servent *
getservbyport_p(int port,const char * proto,struct net_data * net_data)1210Sstevel@tonic-gate getservbyport_p(int port, const char *proto, struct net_data *net_data) {
1220Sstevel@tonic-gate struct irs_sv *sv;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (!net_data || !(sv = net_data->sv))
1250Sstevel@tonic-gate return (NULL);
1260Sstevel@tonic-gate if (net_data->sv_stayopen && net_data->sv_last)
1270Sstevel@tonic-gate if (port == net_data->sv_last->s_port &&
1280Sstevel@tonic-gate ( !proto ||
1290Sstevel@tonic-gate !strcmp(net_data->sv_last->s_proto, proto)))
1300Sstevel@tonic-gate return (net_data->sv_last);
1310Sstevel@tonic-gate net_data->sv_last = (*sv->byport)(sv, port, proto);
1320Sstevel@tonic-gate return (net_data->sv_last);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate void
setservent_p(int stayopen,struct net_data * net_data)1360Sstevel@tonic-gate setservent_p(int stayopen, struct net_data *net_data) {
1370Sstevel@tonic-gate struct irs_sv *sv;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (!net_data || !(sv = net_data->sv))
1400Sstevel@tonic-gate return;
1410Sstevel@tonic-gate (*sv->rewind)(sv);
1420Sstevel@tonic-gate net_data->sv_stayopen = (stayopen != 0);
1430Sstevel@tonic-gate if (stayopen == 0)
1440Sstevel@tonic-gate net_data_minimize(net_data);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate void
endservent_p(struct net_data * net_data)1480Sstevel@tonic-gate endservent_p(struct net_data *net_data) {
1490Sstevel@tonic-gate struct irs_sv *sv;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
1520Sstevel@tonic-gate (*sv->minimize)(sv);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* Private */
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate static struct net_data *
init()1580Sstevel@tonic-gate init() {
1590Sstevel@tonic-gate struct net_data *net_data;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate if (!(net_data = net_data_init(NULL)))
1620Sstevel@tonic-gate goto error;
1630Sstevel@tonic-gate if (!net_data->sv) {
1640Sstevel@tonic-gate net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (!net_data->sv || !net_data->res) {
1670Sstevel@tonic-gate error:
1680Sstevel@tonic-gate errno = EIO;
1690Sstevel@tonic-gate return (NULL);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate (*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate return (net_data);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
178*11038SRao.Shoaib@Sun.COM
179*11038SRao.Shoaib@Sun.COM /*! \file */
180