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