source: MondoRescue/branches/2.2.5/mindi-busybox/libbb/inet_common.c@ 1854

Last change on this file since 1854 was 1765, checked in by Bruno Cornec, 17 years ago

Update to busybox 1.7.2

File size: 4.9 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]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
14int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
15{
16 struct hostent *hp;
[1765]17#if ENABLE_FEATURE_ETC_NETWORKS
[821]18 struct netent *np;
[1765]19#endif
[821]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. */
[1765]26 if (!strcmp(name, bb_str_default)) {
[821]27 s_in->sin_addr.s_addr = INADDR_ANY;
[1765]28 return 1;
[821]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) {
[1765]37 bb_error_msg("gethostbyname(%s)", name);
[821]38 }
39#endif
[1765]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 }
[821]47 }
[1765]48#if ENABLE_FEATURE_ETC_NETWORKS
[821]49 /* Try the NETWORKS database to see if this is a known network. */
50#ifdef DEBUG
[1765]51 bb_error_msg("getnetbyname(%s)", name);
[821]52#endif
[1765]53 np = getnetbyname(name);
54 if (np != NULL) {
[821]55 s_in->sin_addr.s_addr = htonl(np->n_net);
56 return 1;
57 }
[1765]58#endif
[821]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
[1765]69 bb_error_msg("gethostbyname(%s)", name);
[821]70#endif
[1765]71 hp = gethostbyname(name);
72 if (hp == NULL) {
[821]73 return -1;
74 }
[1765]75 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
[821]76 return 0;
77}
78
79
80/* numeric: & 0x8000: default instead of *,
81 * & 0x4000: host instead of net,
82 * & 0x0fff: don't resolve
83 */
[1765]84char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
[821]85{
[1765]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
[821]95 struct addr *pn;
[1765]96 char *name;
97 uint32_t ad, host_ad;
[821]98 int host = 0;
99
100 /* Grmpf. -FvK */
101 if (s_in->sin_family != AF_INET) {
102#ifdef DEBUG
[1765]103 bb_error_msg("rresolve: unsupported address family %d!",
[821]104 s_in->sin_family);
105#endif
106 errno = EAFNOSUPPORT;
[1765]107 return NULL;
[821]108 }
[1765]109 ad = s_in->sin_addr.s_addr;
[821]110#ifdef DEBUG
[1765]111 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
[821]112#endif
113 if (ad == INADDR_ANY) {
114 if ((numeric & 0x0FFF) == 0) {
115 if (numeric & 0x8000)
[1765]116 return xstrdup(bb_str_default);
117 return xstrdup("*");
[821]118 }
119 }
[1765]120 if (numeric & 0x0FFF)
121 return xstrdup(inet_ntoa(s_in->sin_addr));
[821]122
123 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
124 host = 1;
[1765]125 pn = cache;
126 while (pn) {
[821]127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
128#ifdef DEBUG
[1765]129 bb_error_msg("rresolve: found %s %08x in cache",
130 (host ? "host" : "net"), (unsigned)ad);
[821]131#endif
[1765]132 return xstrdup(pn->name);
[821]133 }
134 pn = pn->next;
135 }
136
137 host_ad = ntohl(ad);
[1765]138 name = NULL;
[821]139 if (host) {
[1765]140 struct hostent *ent;
[821]141#ifdef DEBUG
[1765]142 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
[821]143#endif
144 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
[1765]145 if (ent)
146 name = xstrdup(ent->h_name);
147 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
148 struct netent *np;
[821]149#ifdef DEBUG
[1765]150 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
[821]151#endif
152 np = getnetbyaddr(host_ad, AF_INET);
[1765]153 if (np)
154 name = xstrdup(np->n_name);
[821]155 }
[1765]156 if (!name)
157 name = xstrdup(inet_ntoa(s_in->sin_addr));
158 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
159 pn->next = cache;
[821]160 pn->addr = *s_in;
161 pn->host = host;
[1765]162 strcpy(pn->name, name);
163 cache = pn;
164 return name;
[821]165}
166
[1765]167#if ENABLE_FEATURE_IPV6
[821]168
169int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
170{
171 struct addrinfo req, *ai;
172 int s;
173
174 memset(&req, '\0', sizeof req);
175 req.ai_family = AF_INET6;
[1765]176 s = getaddrinfo(name, NULL, &req, &ai);
177 if (s) {
[821]178 bb_error_msg("getaddrinfo: %s: %d", name, s);
179 return -1;
180 }
181 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
182 freeaddrinfo(ai);
[1765]183 return 0;
[821]184}
185
186#ifndef IN6_IS_ADDR_UNSPECIFIED
187# define IN6_IS_ADDR_UNSPECIFIED(a) \
188 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
189 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
190#endif
191
192
[1765]193char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
[821]194{
[1765]195 char name[128];
[821]196 int s;
197
198 /* Grmpf. -FvK */
199 if (sin6->sin6_family != AF_INET6) {
200#ifdef DEBUG
[1765]201 bb_error_msg("rresolve: unsupport address family %d!",
[821]202 sin6->sin6_family);
203#endif
204 errno = EAFNOSUPPORT;
[1765]205 return NULL;
[821]206 }
207 if (numeric & 0x7FFF) {
[1765]208 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
209 return xstrdup(name);
[821]210 }
211 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
[1765]212 if (numeric & 0x8000)
213 return xstrdup(bb_str_default);
214 return xstrdup("*");
[821]215 }
216
[1765]217 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
218 name, sizeof(name), NULL, 0, 0);
[821]219 if (s) {
220 bb_error_msg("getnameinfo failed");
[1765]221 return NULL;
[821]222 }
[1765]223 return xstrdup(name);
[821]224}
225
226#endif /* CONFIG_FEATURE_IPV6 */
Note: See TracBrowser for help on using the repository browser.