blob: feb0c42f3a5f97ca9d03f2cb933c68f8933710cf [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc223ced2001-11-10 12:18:42 +00002/*
3 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenc223ced2001-11-10 12:18:42 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +00008 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
Eric Andersenc223ced2001-11-10 12:18:42 +00009 *
10 */
11
Rob Landleyea224be2006-06-18 20:20:07 +000012#include "libbb.h"
Eric Andersenc223ced2001-11-10 12:18:42 +000013#include "inet_common.h"
14#include <stdio.h>
15#include <errno.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
Eric Andersenc223ced2001-11-10 12:18:42 +000019
Eric Andersen77d22c42002-06-06 12:11:55 +000020#ifdef DEBUG
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +000021# include <resolv.h>
Eric Andersen77d22c42002-06-06 12:11:55 +000022#endif
23
24
Glenn L McGrath30db4232002-11-28 09:52:23 +000025const char bb_INET_default[] = "default";
Eric Andersenc223ced2001-11-10 12:18:42 +000026
27int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
28{
Glenn L McGrath30db4232002-11-28 09:52:23 +000029 struct hostent *hp;
30 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +000031
Glenn L McGrath30db4232002-11-28 09:52:23 +000032 /* Grmpf. -FvK */
33 s_in->sin_family = AF_INET;
34 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000035
Glenn L McGrath30db4232002-11-28 09:52:23 +000036 /* Default is special, meaning 0.0.0.0. */
37 if (!strcmp(name, bb_INET_default)) {
38 s_in->sin_addr.s_addr = INADDR_ANY;
39 return (1);
40 }
41 /* Look to see if it's a dotted quad. */
42 if (inet_aton(name, &s_in->sin_addr)) {
43 return 0;
44 }
45 /* If we expect this to be a hostname, try hostname database first */
Eric Andersenc223ced2001-11-10 12:18:42 +000046#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000047 if (hostfirst) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000049 }
Eric Andersenc223ced2001-11-10 12:18:42 +000050#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000051 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
52 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
53 sizeof(struct in_addr));
54 return 0;
55 }
56 /* Try the NETWORKS database to see if this is a known network. */
57#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 bb_error_msg("getnetbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000059#endif
60 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
61 s_in->sin_addr.s_addr = htonl(np->n_net);
62 return 1;
63 }
64 if (hostfirst) {
65 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000066 return -1;
67 }
68#ifdef DEBUG
69 res_init();
70 _res.options |= RES_DEBUG;
71#endif
72
73#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000075#endif
76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000077 return -1;
78 }
Eric Andersenc223ced2001-11-10 12:18:42 +000079 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
Glenn L McGrath30db4232002-11-28 09:52:23 +000080 sizeof(struct in_addr));
81
Eric Andersenc223ced2001-11-10 12:18:42 +000082 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000083}
84
85/* cache */
86struct addr {
Glenn L McGrath30db4232002-11-28 09:52:23 +000087 struct sockaddr_in addr;
88 char *name;
89 int host;
90 struct addr *next;
Eric Andersenc223ced2001-11-10 12:18:42 +000091};
92
Glenn L McGrath30db4232002-11-28 09:52:23 +000093static struct addr *INET_nn = NULL; /* addr-to-name cache */
Eric Andersenc223ced2001-11-10 12:18:42 +000094
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,
Glenn L McGrath30db4232002-11-28 09:52:23 +0000100 int numeric, unsigned int netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +0000101{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000102 struct hostent *ent;
103 struct netent *np;
104 struct addr *pn;
105 unsigned long ad, host_ad;
106 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +0000107
Glenn L McGrath30db4232002-11-28 09:52:23 +0000108 /* Grmpf. -FvK */
109 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000110#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_error_msg("rresolve: unsupport address family %d !",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000112 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000113#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000114 errno = EAFNOSUPPORT;
115 return (-1);
Eric Andersenc223ced2001-11-10 12:18:42 +0000116 }
Glenn L McGrath30db4232002-11-28 09:52:23 +0000117 ad = (unsigned long) s_in->sin_addr.s_addr;
118#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000120#endif
121 if (ad == INADDR_ANY) {
122 if ((numeric & 0x0FFF) == 0) {
123 if (numeric & 0x8000)
124 safe_strncpy(name, bb_INET_default, len);
125 else
126 safe_strncpy(name, "*", len);
127 return (0);
128 }
129 }
130 if (numeric & 0x0FFF) {
131 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
132 return (0);
133 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000134
Glenn L McGrath30db4232002-11-28 09:52:23 +0000135 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
136 host = 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000137 pn = INET_nn;
138 while (pn != NULL) {
139 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
140 safe_strncpy(name, pn->name, len);
Eric Andersenc223ced2001-11-10 12:18:42 +0000141#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_error_msg("rresolve: found %s %08lx in cache",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000143 (host ? "host" : "net"), ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000144#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000145 return (0);
146 }
147 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000148 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000149
Glenn L McGrath30db4232002-11-28 09:52:23 +0000150 host_ad = ntohl(ad);
151 np = NULL;
152 ent = NULL;
153 if (host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000154#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 bb_error_msg("gethostbyaddr (%08lx)", ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000156#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000157 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
158 if (ent != NULL) {
159 safe_strncpy(name, ent->h_name, len);
160 }
161 } else {
Eric Andersenc223ced2001-11-10 12:18:42 +0000162#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000164#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000165 np = getnetbyaddr(host_ad, AF_INET);
166 if (np != NULL) {
167 safe_strncpy(name, np->n_name, len);
168 }
169 }
170 if ((ent == NULL) && (np == NULL)) {
171 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
172 }
173 pn = (struct addr *) xmalloc(sizeof(struct addr));
174 pn->addr = *s_in;
175 pn->next = INET_nn;
176 pn->host = host;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 pn->name = bb_xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000178 INET_nn = pn;
Eric Andersenc223ced2001-11-10 12:18:42 +0000179
Glenn L McGrath30db4232002-11-28 09:52:23 +0000180 return (0);
Eric Andersenc223ced2001-11-10 12:18:42 +0000181}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000182
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000183#ifdef CONFIG_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000184
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +0000185int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000187 struct addrinfo req, *ai;
188 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000189
Glenn L McGrath30db4232002-11-28 09:52:23 +0000190 memset(&req, '\0', sizeof req);
191 req.ai_family = AF_INET6;
192 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000194 return -1;
195 }
196 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000197
Glenn L McGrath30db4232002-11-28 09:52:23 +0000198 freeaddrinfo(ai);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000199
Glenn L McGrath30db4232002-11-28 09:52:23 +0000200 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201}
202
203#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000204# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000205 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
206 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000207#endif
208
209
Glenn L McGrath30db4232002-11-28 09:52:23 +0000210int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
211 int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000212{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000213 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000214
Glenn L McGrath30db4232002-11-28 09:52:23 +0000215 /* Grmpf. -FvK */
216 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000217#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
Glenn L McGrath30db4232002-11-28 09:52:23 +0000219 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000220#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000221 errno = EAFNOSUPPORT;
222 return (-1);
223 }
224 if (numeric & 0x7FFF) {
225 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
226 return (0);
227 }
228 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
229 if (numeric & 0x8000) {
230 strcpy(name, "default");
231 } else {
232 strcpy(name, "*");
233 }
234 return (0);
235 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000236
Glenn L McGrath30db4232002-11-28 09:52:23 +0000237 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
238 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 bb_error_msg("getnameinfo failed");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000240 return -1;
241 }
242 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000243}
244
Glenn L McGrath30db4232002-11-28 09:52:23 +0000245#endif /* CONFIG_FEATURE_IPV6 */