]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*-\r |
2 | * Copyright (c) 1994, Garrett Wollman\r | |
3 | *\r | |
4 | * Redistribution and use in source and binary forms, with or without\r | |
5 | * modification, are permitted provided that the following conditions\r | |
6 | * are met:\r | |
7 | * 1. Redistributions of source code must retain the above copyright\r | |
8 | * notice, this list of conditions and the following disclaimer.\r | |
9 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
10 | * notice, this list of conditions and the following disclaimer in the\r | |
11 | * documentation and/or other materials provided with the distribution.\r | |
12 | *\r | |
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND\r | |
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
23 | * SUCH DAMAGE.\r | |
24 | */\r | |
25 | \r | |
26 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
27 | static char sccsid[] = "@(#)$Id: getnetbynis.c,v 1.1.1.1 2003/11/19 01:51:28 kyu3 Exp $";\r | |
28 | static char rcsid[] = "$Id: getnetbynis.c,v 1.1.1.1 2003/11/19 01:51:28 kyu3 Exp $";\r | |
29 | #endif /* LIBC_SCCS and not lint */\r | |
30 | \r | |
31 | #include <sys/param.h>\r | |
32 | #include <sys/socket.h>\r | |
33 | #include <netinet/in.h>\r | |
34 | #include <arpa/inet.h>\r | |
35 | #include <netdb.h>\r | |
36 | #include <stdio.h>\r | |
37 | #include <stdlib.h>\r | |
38 | #include <ctype.h>\r | |
39 | #include <errno.h>\r | |
40 | #include <string.h>\r | |
41 | #include <arpa/nameser.h>\r | |
42 | #ifdef YP\r | |
43 | #include <rpc/rpc.h>\r | |
44 | #include <rpcsvc/yp_prot.h>\r | |
45 | #include <rpcsvc/ypclnt.h>\r | |
46 | #endif\r | |
47 | \r | |
48 | #define MAXALIASES 35\r | |
49 | #define MAXADDRS 35\r | |
50 | \r | |
51 | #ifdef YP\r | |
52 | static char *host_aliases[MAXALIASES];\r | |
53 | #endif /* YP */\r | |
54 | \r | |
55 | static struct netent *\r | |
56 | _getnetbynis(const char *name, char *map, int af)\r | |
57 | {\r | |
58 | #ifdef YP\r | |
59 | register char *cp, **q;\r | |
60 | static char *result;\r | |
61 | int resultlen;\r | |
62 | static struct netent h;\r | |
63 | static char *domain = (char *)NULL;\r | |
64 | static char ypbuf[YPMAXRECORD + 2];\r | |
65 | \r | |
66 | switch(af) {\r | |
67 | case AF_INET:\r | |
68 | break;\r | |
69 | default:\r | |
70 | case AF_INET6:\r | |
71 | errno = EAFNOSUPPORT;\r | |
72 | return NULL;\r | |
73 | }\r | |
74 | \r | |
75 | if (domain == (char *)NULL)\r | |
76 | if (yp_get_default_domain (&domain))\r | |
77 | return (NULL);\r | |
78 | \r | |
79 | if (yp_match(domain, map, name, strlen(name), &result, &resultlen))\r | |
80 | return (NULL);\r | |
81 | \r | |
82 | bcopy((char *)result, (char *)&ypbuf, resultlen);\r | |
83 | ypbuf[resultlen] = '\0';\r | |
84 | free(result);\r | |
85 | result = (char *)&ypbuf;\r | |
86 | \r | |
87 | if ((cp = index(result, '\n')))\r | |
88 | *cp = '\0';\r | |
89 | \r | |
90 | cp = strpbrk(result, " \t");\r | |
91 | *cp++ = '\0';\r | |
92 | h.n_name = result;\r | |
93 | \r | |
94 | while (*cp == ' ' || *cp == '\t')\r | |
95 | cp++;\r | |
96 | \r | |
97 | h.n_net = inet_network(cp);\r | |
98 | h.n_addrtype = AF_INET;\r | |
99 | \r | |
100 | q = h.n_aliases = host_aliases;\r | |
101 | cp = strpbrk(cp, " \t");\r | |
102 | if (cp != NULL)\r | |
103 | *cp++ = '\0';\r | |
104 | while (cp && *cp) {\r | |
105 | if (*cp == ' ' || *cp == '\t') {\r | |
106 | cp++;\r | |
107 | continue;\r | |
108 | }\r | |
109 | if (q < &host_aliases[MAXALIASES - 1])\r | |
110 | *q++ = cp;\r | |
111 | cp = strpbrk(cp, " \t");\r | |
112 | if (cp != NULL)\r | |
113 | *cp++ = '\0';\r | |
114 | }\r | |
115 | *q = NULL;\r | |
116 | return (&h);\r | |
117 | #else\r | |
118 | return (NULL);\r | |
119 | #endif\r | |
120 | }\r | |
121 | \r | |
122 | struct netent *\r | |
123 | _getnetbynisname(const char *name)\r | |
124 | {\r | |
125 | return _getnetbynis(name, "networks.byname", AF_INET);\r | |
126 | }\r | |
127 | \r | |
128 | struct netent *\r | |
129 | _getnetbynisaddr(unsigned long addr, int af)\r | |
130 | {\r | |
131 | char *str, *cp;\r | |
132 | unsigned long net2;\r | |
133 | int nn;\r | |
134 | unsigned int netbr[4];\r | |
135 | char buf[MAXDNAME];\r | |
136 | \r | |
137 | if (af != AF_INET) {\r | |
138 | errno = EAFNOSUPPORT;\r | |
139 | return (NULL);\r | |
140 | }\r | |
141 | \r | |
142 | for (nn = 4, net2 = addr; net2; net2 >>= 8) {\r | |
143 | netbr[--nn] = net2 & 0xff;\r | |
144 | }\r | |
145 | \r | |
146 | switch (nn) {\r | |
147 | case 3: /* Class A */\r | |
148 | sprintf(buf, "%u", netbr[3]);\r | |
149 | break;\r | |
150 | case 2: /* Class B */\r | |
151 | sprintf(buf, "%u.%u", netbr[2], netbr[3]);\r | |
152 | break;\r | |
153 | case 1: /* Class C */\r | |
154 | sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);\r | |
155 | break;\r | |
156 | case 0: /* Class D - E */\r | |
157 | sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],\r | |
158 | netbr[2], netbr[3]);\r | |
159 | break;\r | |
160 | }\r | |
161 | \r | |
162 | str = (char *)&buf;\r | |
163 | cp = str + (strlen(str) - 2);\r | |
164 | \r | |
165 | while(!strcmp(cp, ".0")) {\r | |
166 | *cp = '\0';\r | |
167 | cp = str + (strlen(str) - 2);\r | |
168 | }\r | |
169 | \r | |
170 | return _getnetbynis(str, "networks.byaddr", af);\r | |
171 | }\r |