Initial public busybox upstream commit
[busybox4maemo] / 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  *
9  */
10
11 #include "libbb.h"
12 #include "inet_common.h"
13
14 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
15 {
16         struct hostent *hp;
17 #if ENABLE_FEATURE_ETC_NETWORKS
18         struct netent *np;
19 #endif
20
21         /* Grmpf. -FvK */
22         s_in->sin_family = AF_INET;
23         s_in->sin_port = 0;
24
25         /* Default is special, meaning 0.0.0.0. */
26         if (!strcmp(name, bb_str_default)) {
27                 s_in->sin_addr.s_addr = INADDR_ANY;
28                 return 1;
29         }
30         /* Look to see if it's a dotted quad. */
31         if (inet_aton(name, &s_in->sin_addr)) {
32                 return 0;
33         }
34         /* If we expect this to be a hostname, try hostname database first */
35 #ifdef DEBUG
36         if (hostfirst) {
37                 bb_error_msg("gethostbyname(%s)", name);
38         }
39 #endif
40         if (hostfirst) {
41                 hp = gethostbyname(name);
42                 if (hp != NULL) {
43                         memcpy(&s_in->sin_addr, hp->h_addr_list[0],
44                                 sizeof(struct in_addr));
45                         return 0;
46                 }
47         }
48 #if ENABLE_FEATURE_ETC_NETWORKS
49         /* Try the NETWORKS database to see if this is a known network. */
50 #ifdef DEBUG
51         bb_error_msg("getnetbyname(%s)", name);
52 #endif
53         np = getnetbyname(name);
54         if (np != NULL) {
55                 s_in->sin_addr.s_addr = htonl(np->n_net);
56                 return 1;
57         }
58 #endif
59         if (hostfirst) {
60                 /* Don't try again */
61                 return -1;
62         }
63 #ifdef DEBUG
64         res_init();
65         _res.options |= RES_DEBUG;
66 #endif
67
68 #ifdef DEBUG
69         bb_error_msg("gethostbyname(%s)", name);
70 #endif
71         hp = gethostbyname(name);
72         if (hp == NULL) {
73                 return -1;
74         }
75         memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
76         return 0;
77 }
78
79
80 /* numeric: & 0x8000: default instead of *,
81  *          & 0x4000: host instead of net,
82  *          & 0x0fff: don't resolve
83  */
84 char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
85 {
86         /* addr-to-name cache */
87         struct addr {
88                 struct addr *next;
89                 struct sockaddr_in addr;
90                 int host;
91                 char name[1];
92         };
93         static struct addr *cache = NULL;
94
95         struct addr *pn;
96         char *name;
97         uint32_t ad, host_ad;
98         int host = 0;
99
100         if (s_in->sin_family != AF_INET) {
101 #ifdef DEBUG
102                 bb_error_msg("rresolve: unsupported address family %d!",
103                                   s_in->sin_family);
104 #endif
105                 errno = EAFNOSUPPORT;
106                 return NULL;
107         }
108         ad = s_in->sin_addr.s_addr;
109 #ifdef DEBUG
110         bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
111 #endif
112         if (ad == INADDR_ANY) {
113                 if ((numeric & 0x0FFF) == 0) {
114                         if (numeric & 0x8000)
115                                 return xstrdup(bb_str_default);
116                         return xstrdup("*");
117                 }
118         }
119         if (numeric & 0x0FFF)
120                 return xstrdup(inet_ntoa(s_in->sin_addr));
121
122         if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
123                 host = 1;
124         pn = cache;
125         while (pn) {
126                 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
127 #ifdef DEBUG
128                         bb_error_msg("rresolve: found %s %08x in cache",
129                                           (host ? "host" : "net"), (unsigned)ad);
130 #endif
131                         return xstrdup(pn->name);
132                 }
133                 pn = pn->next;
134         }
135
136         host_ad = ntohl(ad);
137         name = NULL;
138         if (host) {
139                 struct hostent *ent;
140 #ifdef DEBUG
141                 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
142 #endif
143                 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
144                 if (ent)
145                         name = xstrdup(ent->h_name);
146         } else if (ENABLE_FEATURE_ETC_NETWORKS) {
147                 struct netent *np;
148 #ifdef DEBUG
149                 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
150 #endif
151                 np = getnetbyaddr(host_ad, AF_INET);
152                 if (np)
153                         name = xstrdup(np->n_name);
154         }
155         if (!name)
156                 name = xstrdup(inet_ntoa(s_in->sin_addr));
157         pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
158         pn->next = cache;
159         pn->addr = *s_in;
160         pn->host = host;
161         strcpy(pn->name, name);
162         cache = pn;
163         return name;
164 }
165
166 #if ENABLE_FEATURE_IPV6
167
168 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
169 {
170         struct addrinfo req, *ai;
171         int s;
172
173         memset(&req, '\0', sizeof req);
174         req.ai_family = AF_INET6;
175         s = getaddrinfo(name, NULL, &req, &ai);
176         if (s) {
177                 bb_error_msg("getaddrinfo: %s: %d", name, s);
178                 return -1;
179         }
180         memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
181         freeaddrinfo(ai);
182         return 0;
183 }
184
185 #ifndef IN6_IS_ADDR_UNSPECIFIED
186 # define IN6_IS_ADDR_UNSPECIFIED(a) \
187         (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
188          ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
189 #endif
190
191
192 char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
193 {
194         char name[128];
195         int s;
196
197         if (sin6->sin6_family != AF_INET6) {
198 #ifdef DEBUG
199                 bb_error_msg("rresolve: unsupport address family %d!",
200                                   sin6->sin6_family);
201 #endif
202                 errno = EAFNOSUPPORT;
203                 return NULL;
204         }
205         if (numeric & 0x7FFF) {
206                 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
207                 return xstrdup(name);
208         }
209         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
210                 if (numeric & 0x8000)
211                         return xstrdup(bb_str_default);
212                 return xstrdup("*");
213         }
214
215         s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
216                                 name, sizeof(name), NULL, 0, 0);
217         if (s) {
218                 bb_error_msg("getnameinfo failed");
219                 return NULL;
220         }
221         return xstrdup(name);
222 }
223
224 #endif          /* CONFIG_FEATURE_IPV6 */