Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / libbb / inet_common.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * stolen from net-tools-1.59 and stripped down for busybox by
4  *                      Erik Andersen <andersen@codepoet.org>
5  *
6  * Heavily modified by Manuel Novoa III       Mar 12, 2001
7  *
8  * Version:     $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
9  *
10  */
11
12 #include "libbb.h"
13 #include "inet_common.h"
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #ifdef DEBUG
21 # include <resolv.h>
22 #endif
23
24
25 const char bb_INET_default[] = "default";
26
27 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
28 {
29         struct hostent *hp;
30         struct netent *np;
31
32         /* Grmpf. -FvK */
33         s_in->sin_family = AF_INET;
34         s_in->sin_port = 0;
35
36         /* Default is special, meaning 0.0.0.0. */
37         if (!strcmp(name, bb_INET_default)) {
38                 s_in->sin_addr.s_addr = INADDR_ANY;
39                 return (1);
40         }
41         /* Look to see if it's a dotted quad. */
42         if (inet_aton(name, &s_in->sin_addr)) {
43                 return 0;
44         }
45         /* If we expect this to be a hostname, try hostname database first */
46 #ifdef DEBUG
47         if (hostfirst) {
48                 bb_error_msg("gethostbyname (%s)", name);
49         }
50 #endif
51         if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
52                 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
53                            sizeof(struct in_addr));
54                 return 0;
55         }
56         /* Try the NETWORKS database to see if this is a known network. */
57 #ifdef DEBUG
58         bb_error_msg("getnetbyname (%s)", name);
59 #endif
60         if ((np = getnetbyname(name)) != (struct netent *) NULL) {
61                 s_in->sin_addr.s_addr = htonl(np->n_net);
62                 return 1;
63         }
64         if (hostfirst) {
65                 /* Don't try again */
66                 return -1;
67         }
68 #ifdef DEBUG
69         res_init();
70         _res.options |= RES_DEBUG;
71 #endif
72
73 #ifdef DEBUG
74         bb_error_msg("gethostbyname (%s)", name);
75 #endif
76         if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
77                 return -1;
78         }
79         memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
80                    sizeof(struct in_addr));
81
82         return 0;
83 }
84
85 /* cache */
86 struct addr {
87         struct sockaddr_in addr;
88         char *name;
89         int host;
90         struct addr *next;
91 };
92
93 static struct addr *INET_nn = NULL;     /* addr-to-name cache           */
94
95 /* numeric: & 0x8000: default instead of *,
96  *          & 0x4000: host instead of net,
97  *          & 0x0fff: don't resolve
98  */
99 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
100                                   int numeric, unsigned int netmask)
101 {
102         struct hostent *ent;
103         struct netent *np;
104         struct addr *pn;
105         unsigned long ad, host_ad;
106         int host = 0;
107
108         /* Grmpf. -FvK */
109         if (s_in->sin_family != AF_INET) {
110 #ifdef DEBUG
111                 bb_error_msg("rresolve: unsupport address family %d !",
112                                   s_in->sin_family);
113 #endif
114                 errno = EAFNOSUPPORT;
115                 return (-1);
116         }
117         ad = (unsigned long) s_in->sin_addr.s_addr;
118 #ifdef DEBUG
119         bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
120 #endif
121         if (ad == INADDR_ANY) {
122                 if ((numeric & 0x0FFF) == 0) {
123                         if (numeric & 0x8000)
124                                 safe_strncpy(name, bb_INET_default, len);
125                         else
126                                 safe_strncpy(name, "*", len);
127                         return (0);
128                 }
129         }
130         if (numeric & 0x0FFF) {
131                 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
132                 return (0);
133         }
134
135         if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
136                 host = 1;
137         pn = INET_nn;
138         while (pn != NULL) {
139                 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
140                         safe_strncpy(name, pn->name, len);
141 #ifdef DEBUG
142                         bb_error_msg("rresolve: found %s %08lx in cache",
143                                           (host ? "host" : "net"), ad);
144 #endif
145                         return (0);
146                 }
147                 pn = pn->next;
148         }
149
150         host_ad = ntohl(ad);
151         np = NULL;
152         ent = NULL;
153         if (host) {
154 #ifdef DEBUG
155                 bb_error_msg("gethostbyaddr (%08lx)", ad);
156 #endif
157                 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
158                 if (ent != NULL) {
159                         safe_strncpy(name, ent->h_name, len);
160                 }
161         } else {
162 #ifdef DEBUG
163                 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
164 #endif
165                 np = getnetbyaddr(host_ad, AF_INET);
166                 if (np != NULL) {
167                         safe_strncpy(name, np->n_name, len);
168                 }
169         }
170         if ((ent == NULL) && (np == NULL)) {
171                 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
172         }
173         pn = (struct addr *) xmalloc(sizeof(struct addr));
174         pn->addr = *s_in;
175         pn->next = INET_nn;
176         pn->host = host;
177         pn->name = bb_xstrdup(name);
178         INET_nn = pn;
179
180         return (0);
181 }
182
183 #ifdef CONFIG_FEATURE_IPV6
184
185 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
186 {
187         struct addrinfo req, *ai;
188         int s;
189
190         memset(&req, '\0', sizeof req);
191         req.ai_family = AF_INET6;
192         if ((s = getaddrinfo(name, NULL, &req, &ai))) {
193                 bb_error_msg("getaddrinfo: %s: %d", name, s);
194                 return -1;
195         }
196         memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
197
198         freeaddrinfo(ai);
199
200         return (0);
201 }
202
203 #ifndef IN6_IS_ADDR_UNSPECIFIED
204 # define IN6_IS_ADDR_UNSPECIFIED(a) \
205         (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
206          ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
207 #endif
208
209
210 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
211                                    int numeric)
212 {
213         int s;
214
215         /* Grmpf. -FvK */
216         if (sin6->sin6_family != AF_INET6) {
217 #ifdef DEBUG
218                 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
219                                   sin6->sin6_family);
220 #endif
221                 errno = EAFNOSUPPORT;
222                 return (-1);
223         }
224         if (numeric & 0x7FFF) {
225                 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
226                 return (0);
227         }
228         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
229                 if (numeric & 0x8000) {
230                         strcpy(name, "default");
231                 } else {
232                         strcpy(name, "*");
233                 }
234                 return (0);
235         }
236
237         s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
238         if (s) {
239                 bb_error_msg("getnameinfo failed");
240                 return -1;
241         }
242         return (0);
243 }
244
245 #endif                                                  /* CONFIG_FEATURE_IPV6 */