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