blob: c02c732f32273d3f07976b6608020378ea651dd9 [file] [log] [blame]
Eric Andersenc223ced2001-11-10 12:18:42 +00001/*
2 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00003 * Erik Andersen <andersen@codepoet.org>
Eric Andersenc223ced2001-11-10 12:18:42 +00004 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +00007 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 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
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +000020# include <resolv.h>
Eric Andersen77d22c42002-06-06 12:11:55 +000021#endif
22
23
Glenn L McGrath30db4232002-11-28 09:52:23 +000024const char bb_INET_default[] = "default";
Eric Andersenc223ced2001-11-10 12:18:42 +000025
26int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27{
Glenn L McGrath30db4232002-11-28 09:52:23 +000028 struct hostent *hp;
29 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +000030
Glenn L McGrath30db4232002-11-28 09:52:23 +000031 /* Grmpf. -FvK */
32 s_in->sin_family = AF_INET;
33 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000034
Glenn L McGrath30db4232002-11-28 09:52:23 +000035 /* 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 */
Eric Andersenc223ced2001-11-10 12:18:42 +000045#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000046 if (hostfirst) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000048 }
Eric Andersenc223ced2001-11-10 12:18:42 +000049#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000050 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
51 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
52 sizeof(struct in_addr));
53 return 0;
54 }
55 /* Try the NETWORKS database to see if this is a known network. */
56#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg("getnetbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000058#endif
59 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
60 s_in->sin_addr.s_addr = htonl(np->n_net);
61 return 1;
62 }
63 if (hostfirst) {
64 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000065 return -1;
66 }
67#ifdef DEBUG
68 res_init();
69 _res.options |= RES_DEBUG;
70#endif
71
72#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000074#endif
75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000076 return -1;
77 }
Eric Andersenc223ced2001-11-10 12:18:42 +000078 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
Glenn L McGrath30db4232002-11-28 09:52:23 +000079 sizeof(struct in_addr));
80
Eric Andersenc223ced2001-11-10 12:18:42 +000081 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000082}
83
84/* cache */
85struct addr {
Glenn L McGrath30db4232002-11-28 09:52:23 +000086 struct sockaddr_in addr;
87 char *name;
88 int host;
89 struct addr *next;
Eric Andersenc223ced2001-11-10 12:18:42 +000090};
91
Glenn L McGrath30db4232002-11-28 09:52:23 +000092static struct addr *INET_nn = NULL; /* addr-to-name cache */
Eric Andersenc223ced2001-11-10 12:18:42 +000093
94/* numeric: & 0x8000: default instead of *,
95 * & 0x4000: host instead of net,
96 * & 0x0fff: don't resolve
97 */
98int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Glenn L McGrath30db4232002-11-28 09:52:23 +000099 int numeric, unsigned int netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +0000100{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000101 struct hostent *ent;
102 struct netent *np;
103 struct addr *pn;
104 unsigned long ad, host_ad;
105 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +0000106
Glenn L McGrath30db4232002-11-28 09:52:23 +0000107 /* Grmpf. -FvK */
108 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000109#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_error_msg("rresolve: unsupport address family %d !",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000111 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000112#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000113 errno = EAFNOSUPPORT;
114 return (-1);
Eric Andersenc223ced2001-11-10 12:18:42 +0000115 }
Glenn L McGrath30db4232002-11-28 09:52:23 +0000116 ad = (unsigned long) s_in->sin_addr.s_addr;
117#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000119#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 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000133
Glenn L McGrath30db4232002-11-28 09:52:23 +0000134 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
135 host = 1;
Eric Andersenc223ced2001-11-10 12:18:42 +0000136#if 0
Glenn L McGrath30db4232002-11-28 09:52:23 +0000137 INET_nn = NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000138#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000139 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);
Eric Andersenc223ced2001-11-10 12:18:42 +0000143#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 bb_error_msg("rresolve: found %s %08lx in cache",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000145 (host ? "host" : "net"), ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000146#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000147 return (0);
148 }
149 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000150 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000151
Glenn L McGrath30db4232002-11-28 09:52:23 +0000152 host_ad = ntohl(ad);
153 np = NULL;
154 ent = NULL;
155 if (host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000156#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 bb_error_msg("gethostbyaddr (%08lx)", ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000158#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000159 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
160 if (ent != NULL) {
161 safe_strncpy(name, ent->h_name, len);
162 }
163 } else {
Eric Andersenc223ced2001-11-10 12:18:42 +0000164#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000166#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000167 np = getnetbyaddr(host_ad, AF_INET);
168 if (np != NULL) {
169 safe_strncpy(name, np->n_name, len);
170 }
171 }
172 if ((ent == NULL) && (np == NULL)) {
173 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
174 }
175 pn = (struct addr *) xmalloc(sizeof(struct addr));
176 pn->addr = *s_in;
177 pn->next = INET_nn;
178 pn->host = host;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 pn->name = bb_xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000180 INET_nn = pn;
Eric Andersenc223ced2001-11-10 12:18:42 +0000181
Glenn L McGrath30db4232002-11-28 09:52:23 +0000182 return (0);
Eric Andersenc223ced2001-11-10 12:18:42 +0000183}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000184
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000185#ifdef CONFIG_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +0000187int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000188{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000189 struct addrinfo req, *ai;
190 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000191
Glenn L McGrath30db4232002-11-28 09:52:23 +0000192 memset(&req, '\0', sizeof req);
193 req.ai_family = AF_INET6;
194 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000196 return -1;
197 }
198 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000199
Glenn L McGrath30db4232002-11-28 09:52:23 +0000200 freeaddrinfo(ai);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201
Glenn L McGrath30db4232002-11-28 09:52:23 +0000202 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000203}
204
205#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000206# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000207 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
208 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000209#endif
210
211
Glenn L McGrath30db4232002-11-28 09:52:23 +0000212int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
213 int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000214{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000215 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000216
Glenn L McGrath30db4232002-11-28 09:52:23 +0000217 /* Grmpf. -FvK */
218 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000219#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
Glenn L McGrath30db4232002-11-28 09:52:23 +0000221 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000222#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000223 errno = EAFNOSUPPORT;
224 return (-1);
225 }
226 if (numeric & 0x7FFF) {
227 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
228 return (0);
229 }
230 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
231 if (numeric & 0x8000) {
232 strcpy(name, "default");
233 } else {
234 strcpy(name, "*");
235 }
236 return (0);
237 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000238
Glenn L McGrath30db4232002-11-28 09:52:23 +0000239 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
240 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000241 bb_error_msg("getnameinfo failed");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000242 return -1;
243 }
244 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000245}
246
Glenn L McGrath30db4232002-11-28 09:52:23 +0000247#endif /* CONFIG_FEATURE_IPV6 */