Simplify CRC table generation
[oweals/busybox.git] / libbb / inet_common.c
1 /*
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  *
7  * Version:     $Id: inet_common.c,v 1.1 2001/11/10 12:18:42 andersen Exp $
8  *
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
19 const char bb_INET_default[]="default";
20
21 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
22 {
23     struct hostent *hp;
24     struct netent *np;
25
26     /* Grmpf. -FvK */
27     s_in->sin_family = AF_INET;
28     s_in->sin_port = 0;
29
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;
33         return (1);
34     }
35     /* Look to see if it's a dotted quad. */
36     if (inet_aton(name, &s_in->sin_addr)) {
37         return 0;
38     }
39     /* If we expect this to be a hostname, try hostname database first */
40 #ifdef DEBUG
41     if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
42 #endif
43     if (hostfirst &&
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));
47         return 0;
48     }
49     /* Try the NETWORKS database to see if this is a known network. */
50 #ifdef DEBUG
51     fprintf (stderr, "getnetbyname (%s)\n", name);
52 #endif
53     if ((np = getnetbyname(name)) != (struct netent *) NULL) {
54         s_in->sin_addr.s_addr = htonl(np->n_net);
55         return 1;
56     }
57     if (hostfirst) {
58         /* Don't try again */
59         errno = h_errno;
60         return -1;
61     }
62 #ifdef DEBUG
63     res_init();
64     _res.options |= RES_DEBUG;
65 #endif
66
67 #ifdef DEBUG
68     fprintf (stderr, "gethostbyname (%s)\n", name);
69 #endif
70     if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
71         errno = h_errno;
72         return -1;
73     }
74     memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
75            sizeof(struct in_addr));
76
77     return 0;
78 }
79
80 /* cache */
81 struct addr {
82     struct sockaddr_in addr;
83     char *name;
84     int host;
85     struct addr *next;
86 };
87
88 static struct addr *INET_nn = NULL;     /* addr-to-name cache           */
89
90 /* numeric: & 0x8000: default instead of *,
91  *          & 0x4000: host instead of net,
92  *          & 0x0fff: don't resolve
93  */
94 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
95                          int numeric, unsigned int netmask)
96 {
97     struct hostent *ent;
98     struct netent *np;
99     struct addr *pn;
100     unsigned long ad, host_ad;
101     int host = 0;
102
103     /* Grmpf. -FvK */
104     if (s_in->sin_family != AF_INET) {
105 #ifdef DEBUG
106         fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family);
107 #endif
108         errno = EAFNOSUPPORT;
109         return (-1);
110     }
111     ad = (unsigned long) s_in->sin_addr.s_addr;
112 #ifdef DEBUG
113     fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
114 #endif
115     if (ad == INADDR_ANY) {
116         if ((numeric & 0x0FFF) == 0) {
117             if (numeric & 0x8000)
118                 safe_strncpy(name, bb_INET_default, len);
119             else
120                 safe_strncpy(name, "*", len);
121             return (0);
122         }
123     }
124     if (numeric & 0x0FFF) {
125         safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
126         return (0);
127     }
128
129     if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
130         host = 1;
131 #if 0
132     INET_nn = NULL;
133 #endif
134     pn = INET_nn;
135     while (pn != NULL) {
136         if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
137             safe_strncpy(name, pn->name, len);
138 #ifdef DEBUG
139             fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
140 #endif
141             return (0);
142         }
143         pn = pn->next;
144     }
145
146     host_ad = ntohl(ad);
147     np = NULL;
148     ent = NULL;
149     if (host) {
150 #ifdef DEBUG
151         fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
152 #endif
153         ent = gethostbyaddr((char *) &ad, 4, AF_INET);
154         if (ent != NULL)
155             safe_strncpy(name, ent->h_name, len);
156     } else {
157 #ifdef DEBUG
158         fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
159 #endif
160         np = getnetbyaddr(host_ad, AF_INET);
161         if (np != NULL)
162             safe_strncpy(name, np->n_name, len);
163     }
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));
167     pn->addr = *s_in;
168     pn->next = INET_nn;
169     pn->host = host;
170     pn->name = xstrdup(name);
171     INET_nn = pn;
172
173     return (0);
174 }