2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 * Version: $Id: inet_common.c,v 1.1 2001/11/10 12:18:42 andersen Exp $
11 #include "inet_common.h"
19 const char bb_INET_default[]="default";
21 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27 s_in->sin_family = AF_INET;
30 /* Default is special, meaning 0.0.0.0. */
31 if (!strcmp(name, bb_INET_default)) {
32 s_in->sin_addr.s_addr = INADDR_ANY;
35 /* Look to see if it's a dotted quad. */
36 if (inet_aton(name, &s_in->sin_addr)) {
39 /* If we expect this to be a hostname, try hostname database first */
41 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
44 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
45 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
46 sizeof(struct in_addr));
49 /* Try the NETWORKS database to see if this is a known network. */
51 fprintf (stderr, "getnetbyname (%s)\n", name);
53 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
54 s_in->sin_addr.s_addr = htonl(np->n_net);
64 _res.options |= RES_DEBUG;
68 fprintf (stderr, "gethostbyname (%s)\n", name);
70 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
74 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
75 sizeof(struct in_addr));
82 struct sockaddr_in addr;
88 static struct addr *INET_nn = NULL; /* addr-to-name cache */
90 /* numeric: & 0x8000: default instead of *,
91 * & 0x4000: host instead of net,
92 * & 0x0fff: don't resolve
94 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
95 int numeric, unsigned int netmask)
100 unsigned long ad, host_ad;
104 if (s_in->sin_family != AF_INET) {
106 fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
108 errno = EAFNOSUPPORT;
111 ad = (unsigned long) s_in->sin_addr.s_addr;
113 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
115 if (ad == INADDR_ANY) {
116 if ((numeric & 0x0FFF) == 0) {
117 if (numeric & 0x8000)
118 safe_strncpy(name, bb_INET_default, len);
120 safe_strncpy(name, "*", len);
124 if (numeric & 0x0FFF) {
125 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
129 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
136 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
137 safe_strncpy(name, pn->name, len);
139 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
151 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
153 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
155 safe_strncpy(name, ent->h_name, len);
158 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
160 np = getnetbyaddr(host_ad, AF_INET);
162 safe_strncpy(name, np->n_name, len);
164 if ((ent == NULL) && (np == NULL))
165 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
166 pn = (struct addr *) xmalloc(sizeof(struct addr));
170 pn->name = xstrdup(name);