xref: /freebsd/lib/libc/net/getnetnamadr.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #if defined(LIBC_SCCS) && !defined(lint)
27 static char rcsid[] = "$FreeBSD$";
28 #endif /* LIBC_SCCS and not lint */
29 
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <nsswitch.h>
40 
41 extern int _ht_getnetbyname(void *, void *, va_list);
42 extern int _dns_getnetbyname(void *, void *, va_list);
43 extern int _nis_getnetbyname(void *, void *, va_list);
44 extern int _ht_getnetbyaddr(void *, void *, va_list);
45 extern int _dns_getnetbyaddr(void *, void *, va_list);
46 extern int _nis_getnetbyaddr(void *, void *, va_list);
47 
48 /* Network lookup order if nsswitch.conf is broken or nonexistant */
49 static const ns_src default_src[] = {
50 	{ NSSRC_FILES, NS_SUCCESS },
51 	{ NSSRC_DNS, NS_SUCCESS },
52 	{ 0 }
53 };
54 
55 struct netent *
56 getnetbyname(const char *name)
57 {
58 	struct netent *hp = 0;
59 	int rval;
60 
61 
62 	static const ns_dtab dtab[] = {
63 		NS_FILES_CB(_ht_getnetbyname, NULL)
64 		{ NSSRC_DNS, _dns_getnetbyname, NULL },
65 		NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */
66 		{ 0 }
67 	};
68 
69 	rval = nsdispatch((void *)&hp, dtab, NSDB_NETWORKS, "getnetbyname",
70 			  default_src, name);
71 
72 	if (rval != NS_SUCCESS)
73 		return NULL;
74 	else
75 		return hp;
76 }
77 
78 struct netent *
79 getnetbyaddr(u_long addr, int af)
80 {
81 	struct netent *hp = 0;
82 	int rval;
83 
84 	static const ns_dtab dtab[] = {
85 		NS_FILES_CB(_ht_getnetbyaddr, NULL)
86 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },
87 		NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */
88 		{ 0 }
89 	};
90 
91 	rval = nsdispatch((void *)&hp, dtab, NSDB_NETWORKS, "getnetbyaddr",
92 			  default_src, addr, af);
93 
94 	if (rval != NS_SUCCESS)
95 		return NULL;
96 	else
97 		return hp;
98 }
99 
100 void
101 setnetent(stayopen)
102 	int stayopen;
103 {
104 	_setnethtent(stayopen);
105 	_setnetdnsent(stayopen);
106 }
107 
108 void
109 endnetent()
110 {
111 	_endnethtent();
112 	_endnetdnsent();
113 }
114