blob: c1e5953fe553a45ad59daef7a5511fce69d927da [file] [log] [blame]
Eric Andersenc223ced2001-11-10 12:18:42 +00001/*
2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
4 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
Eric Andersen77d22c42002-06-06 12:11:55 +00007 * Version: $Id: inet_common.c,v 1.2 2002/06/06 12:11:55 andersen Exp $
Eric Andersenc223ced2001-11-10 12:18:42 +00008 *
9 */
10
11#include "inet_common.h"
12#include <stdio.h>
13#include <errno.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include "libbb.h"
18
Eric Andersen77d22c42002-06-06 12:11:55 +000019#ifdef DEBUG
20#include <resolv.h>
21#endif
22
23
Eric Andersenc223ced2001-11-10 12:18:42 +000024const char bb_INET_default[]="default";
25
26int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27{
28 struct hostent *hp;
29 struct netent *np;
30
31 /* Grmpf. -FvK */
32 s_in->sin_family = AF_INET;
33 s_in->sin_port = 0;
34
35 /* Default is special, meaning 0.0.0.0. */
36 if (!strcmp(name, bb_INET_default)) {
37 s_in->sin_addr.s_addr = INADDR_ANY;
38 return (1);
39 }
40 /* Look to see if it's a dotted quad. */
41 if (inet_aton(name, &s_in->sin_addr)) {
42 return 0;
43 }
44 /* If we expect this to be a hostname, try hostname database first */
45#ifdef DEBUG
46 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
47#endif
48 if (hostfirst &&
49 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
50 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
51 sizeof(struct in_addr));
52 return 0;
53 }
54 /* Try the NETWORKS database to see if this is a known network. */
55#ifdef DEBUG
56 fprintf (stderr, "getnetbyname (%s)\n", name);
57#endif
58 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
59 s_in->sin_addr.s_addr = htonl(np->n_net);
60 return 1;
61 }
62 if (hostfirst) {
63 /* Don't try again */
64 errno = h_errno;
65 return -1;
66 }
67#ifdef DEBUG
68 res_init();
69 _res.options |= RES_DEBUG;
70#endif
71
72#ifdef DEBUG
73 fprintf (stderr, "gethostbyname (%s)\n", name);
74#endif
75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
76 errno = h_errno;
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 */
86struct addr {
87 struct sockaddr_in addr;
88 char *name;
89 int host;
90 struct addr *next;
91};
92
93static 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 */
99int 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
Eric Andersen77d22c42002-06-06 12:11:55 +0000111 fprintf(stderr, "rresolve: unsupport address family %d !\n", s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000112#endif
113 errno = EAFNOSUPPORT;
114 return (-1);
115 }
116 ad = (unsigned long) s_in->sin_addr.s_addr;
117#ifdef DEBUG
118 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
119#endif
120 if (ad == INADDR_ANY) {
121 if ((numeric & 0x0FFF) == 0) {
122 if (numeric & 0x8000)
123 safe_strncpy(name, bb_INET_default, len);
124 else
125 safe_strncpy(name, "*", len);
126 return (0);
127 }
128 }
129 if (numeric & 0x0FFF) {
130 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
131 return (0);
132 }
133
134 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
135 host = 1;
136#if 0
137 INET_nn = NULL;
138#endif
139 pn = INET_nn;
140 while (pn != NULL) {
141 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
142 safe_strncpy(name, pn->name, len);
143#ifdef DEBUG
144 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
145#endif
146 return (0);
147 }
148 pn = pn->next;
149 }
150
151 host_ad = ntohl(ad);
152 np = NULL;
153 ent = NULL;
154 if (host) {
155#ifdef DEBUG
156 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
157#endif
158 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
159 if (ent != NULL)
160 safe_strncpy(name, ent->h_name, len);
161 } else {
162#ifdef DEBUG
163 fprintf (stderr, "getnetbyaddr (%08lx)\n", 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 if ((ent == NULL) && (np == NULL))
170 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
171 pn = (struct addr *) xmalloc(sizeof(struct addr));
172 pn->addr = *s_in;
173 pn->next = INET_nn;
174 pn->host = host;
175 pn->name = xstrdup(name);
176 INET_nn = pn;
177
178 return (0);
179}