diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 09:52:23 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 09:52:23 +0000 |
commit | 30db423e11da340eadb4a55baa2037fc3b8c34e6 (patch) | |
tree | 024d622427d4e1a57372c7548e49309430024494 /libbb/inet_common.c | |
parent | efdd0aed23ab43d90c4237d5cb4b2c00fff77c4b (diff) | |
download | busybox-w32-30db423e11da340eadb4a55baa2037fc3b8c34e6.tar.gz busybox-w32-30db423e11da340eadb4a55baa2037fc3b8c34e6.tar.bz2 busybox-w32-30db423e11da340eadb4a55baa2037fc3b8c34e6.zip |
Style, minor cleanups
Diffstat (limited to 'libbb/inet_common.c')
-rw-r--r-- | libbb/inet_common.c | 318 |
1 files changed, 163 insertions, 155 deletions
diff --git a/libbb/inet_common.c b/libbb/inet_common.c index 52fd1349e..16dd1db7c 100644 --- a/libbb/inet_common.c +++ b/libbb/inet_common.c | |||
@@ -4,7 +4,7 @@ | |||
4 | * | 4 | * |
5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 | 5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
6 | * | 6 | * |
7 | * Version: $Id: inet_common.c,v 1.4 2002/11/26 02:35:15 bug1 Exp $ | 7 | * Version: $Id: inet_common.c,v 1.5 2002/11/28 09:52:23 bug1 Exp $ |
8 | * | 8 | * |
9 | */ | 9 | */ |
10 | 10 | ||
@@ -21,181 +21,187 @@ | |||
21 | #endif | 21 | #endif |
22 | 22 | ||
23 | 23 | ||
24 | const char bb_INET_default[]="default"; | 24 | const char bb_INET_default[] = "default"; |
25 | 25 | ||
26 | int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) | 26 | int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) |
27 | { | 27 | { |
28 | struct hostent *hp; | 28 | struct hostent *hp; |
29 | struct netent *np; | 29 | struct netent *np; |
30 | 30 | ||
31 | /* Grmpf. -FvK */ | 31 | /* Grmpf. -FvK */ |
32 | s_in->sin_family = AF_INET; | 32 | s_in->sin_family = AF_INET; |
33 | s_in->sin_port = 0; | 33 | s_in->sin_port = 0; |
34 | 34 | ||
35 | /* Default is special, meaning 0.0.0.0. */ | 35 | /* Default is special, meaning 0.0.0.0. */ |
36 | if (!strcmp(name, bb_INET_default)) { | 36 | if (!strcmp(name, bb_INET_default)) { |
37 | s_in->sin_addr.s_addr = INADDR_ANY; | 37 | s_in->sin_addr.s_addr = INADDR_ANY; |
38 | return (1); | 38 | return (1); |
39 | } | 39 | } |
40 | /* Look to see if it's a dotted quad. */ | 40 | /* Look to see if it's a dotted quad. */ |
41 | if (inet_aton(name, &s_in->sin_addr)) { | 41 | if (inet_aton(name, &s_in->sin_addr)) { |
42 | return 0; | 42 | return 0; |
43 | } | 43 | } |
44 | /* If we expect this to be a hostname, try hostname database first */ | 44 | /* If we expect this to be a hostname, try hostname database first */ |
45 | #ifdef DEBUG | 45 | #ifdef DEBUG |
46 | if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name); | 46 | if (hostfirst) { |
47 | error_msg("gethostbyname (%s)", name); | ||
48 | } | ||
47 | #endif | 49 | #endif |
48 | if (hostfirst && | 50 | if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) { |
49 | (hp = gethostbyname(name)) != (struct hostent *) NULL) { | 51 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], |
50 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], | 52 | sizeof(struct in_addr)); |
51 | sizeof(struct in_addr)); | 53 | return 0; |
52 | return 0; | 54 | } |
53 | } | 55 | /* Try the NETWORKS database to see if this is a known network. */ |
54 | /* Try the NETWORKS database to see if this is a known network. */ | ||
55 | #ifdef DEBUG | 56 | #ifdef DEBUG |
56 | fprintf (stderr, "getnetbyname (%s)\n", name); | 57 | error_msg("getnetbyname (%s)", name); |
57 | #endif | 58 | #endif |
58 | if ((np = getnetbyname(name)) != (struct netent *) NULL) { | 59 | if ((np = getnetbyname(name)) != (struct netent *) NULL) { |
59 | s_in->sin_addr.s_addr = htonl(np->n_net); | 60 | s_in->sin_addr.s_addr = htonl(np->n_net); |
60 | return 1; | 61 | return 1; |
61 | } | 62 | } |
62 | if (hostfirst) { | 63 | if (hostfirst) { |
63 | /* Don't try again */ | 64 | /* Don't try again */ |
64 | errno = h_errno; | 65 | errno = h_errno; |
65 | return -1; | 66 | return -1; |
66 | } | 67 | } |
67 | #ifdef DEBUG | 68 | #ifdef DEBUG |
68 | res_init(); | 69 | res_init(); |
69 | _res.options |= RES_DEBUG; | 70 | _res.options |= RES_DEBUG; |
70 | #endif | 71 | #endif |
71 | 72 | ||
72 | #ifdef DEBUG | 73 | #ifdef DEBUG |
73 | fprintf (stderr, "gethostbyname (%s)\n", name); | 74 | error_msg("gethostbyname (%s)", name); |
74 | #endif | 75 | #endif |
75 | if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { | 76 | if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { |
76 | errno = h_errno; | 77 | errno = h_errno; |
77 | return -1; | 78 | return -1; |
78 | } | 79 | } |
79 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], | 80 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], |
80 | sizeof(struct in_addr)); | 81 | sizeof(struct in_addr)); |
81 | 82 | ||
82 | return 0; | 83 | return 0; |
83 | } | 84 | } |
84 | 85 | ||
85 | /* cache */ | 86 | /* cache */ |
86 | struct addr { | 87 | struct addr { |
87 | struct sockaddr_in addr; | 88 | struct sockaddr_in addr; |
88 | char *name; | 89 | char *name; |
89 | int host; | 90 | int host; |
90 | struct addr *next; | 91 | struct addr *next; |
91 | }; | 92 | }; |
92 | 93 | ||
93 | static struct addr *INET_nn = NULL; /* addr-to-name cache */ | 94 | static struct addr *INET_nn = NULL; /* addr-to-name cache */ |
94 | 95 | ||
95 | /* numeric: & 0x8000: default instead of *, | 96 | /* numeric: & 0x8000: default instead of *, |
96 | * & 0x4000: host instead of net, | 97 | * & 0x4000: host instead of net, |
97 | * & 0x0fff: don't resolve | 98 | * & 0x0fff: don't resolve |
98 | */ | 99 | */ |
99 | int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, | 100 | int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, |
100 | int numeric, unsigned int netmask) | 101 | int numeric, unsigned int netmask) |
101 | { | 102 | { |
102 | struct hostent *ent; | 103 | struct hostent *ent; |
103 | struct netent *np; | 104 | struct netent *np; |
104 | struct addr *pn; | 105 | struct addr *pn; |
105 | unsigned long ad, host_ad; | 106 | unsigned long ad, host_ad; |
106 | int host = 0; | 107 | int host = 0; |
107 | 108 | ||
108 | /* Grmpf. -FvK */ | 109 | /* Grmpf. -FvK */ |
109 | if (s_in->sin_family != AF_INET) { | 110 | if (s_in->sin_family != AF_INET) { |
110 | #ifdef DEBUG | 111 | #ifdef DEBUG |
111 | fprintf(stderr, "rresolve: unsupport address family %d !\n", s_in->sin_family); | 112 | error_msg("rresolve: unsupport address family %d !", |
113 | s_in->sin_family); | ||
112 | #endif | 114 | #endif |
113 | errno = EAFNOSUPPORT; | 115 | errno = EAFNOSUPPORT; |
114 | return (-1); | 116 | return (-1); |
115 | } | 117 | } |
116 | ad = (unsigned long) s_in->sin_addr.s_addr; | 118 | ad = (unsigned long) s_in->sin_addr.s_addr; |
117 | #ifdef DEBUG | 119 | #ifdef DEBUG |
118 | fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric); | 120 | error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric); |
119 | #endif | 121 | #endif |
120 | if (ad == INADDR_ANY) { | 122 | if (ad == INADDR_ANY) { |
121 | if ((numeric & 0x0FFF) == 0) { | 123 | if ((numeric & 0x0FFF) == 0) { |
122 | if (numeric & 0x8000) | 124 | if (numeric & 0x8000) |
123 | safe_strncpy(name, bb_INET_default, len); | 125 | safe_strncpy(name, bb_INET_default, len); |
124 | else | 126 | else |
125 | safe_strncpy(name, "*", len); | 127 | safe_strncpy(name, "*", len); |
126 | return (0); | 128 | return (0); |
127 | } | 129 | } |
128 | } | 130 | } |
129 | if (numeric & 0x0FFF) { | 131 | if (numeric & 0x0FFF) { |
130 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); | 132 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); |
131 | return (0); | 133 | return (0); |
132 | } | 134 | } |
133 | 135 | ||
134 | if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) | 136 | if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) |
135 | host = 1; | 137 | host = 1; |
136 | #if 0 | 138 | #if 0 |
137 | INET_nn = NULL; | 139 | INET_nn = NULL; |
138 | #endif | 140 | #endif |
139 | pn = INET_nn; | 141 | pn = INET_nn; |
140 | while (pn != NULL) { | 142 | while (pn != NULL) { |
141 | if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { | 143 | if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { |
142 | safe_strncpy(name, pn->name, len); | 144 | safe_strncpy(name, pn->name, len); |
143 | #ifdef DEBUG | 145 | #ifdef DEBUG |
144 | fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad); | 146 | error_msg("rresolve: found %s %08lx in cache", |
147 | (host ? "host" : "net"), ad); | ||
145 | #endif | 148 | #endif |
146 | return (0); | 149 | return (0); |
150 | } | ||
151 | pn = pn->next; | ||
147 | } | 152 | } |
148 | pn = pn->next; | ||
149 | } | ||
150 | 153 | ||
151 | host_ad = ntohl(ad); | 154 | host_ad = ntohl(ad); |
152 | np = NULL; | 155 | np = NULL; |
153 | ent = NULL; | 156 | ent = NULL; |
154 | if (host) { | 157 | if (host) { |
155 | #ifdef DEBUG | 158 | #ifdef DEBUG |
156 | fprintf (stderr, "gethostbyaddr (%08lx)\n", ad); | 159 | error_msg("gethostbyaddr (%08lx)", ad); |
157 | #endif | 160 | #endif |
158 | ent = gethostbyaddr((char *) &ad, 4, AF_INET); | 161 | ent = gethostbyaddr((char *) &ad, 4, AF_INET); |
159 | if (ent != NULL) | 162 | if (ent != NULL) { |
160 | safe_strncpy(name, ent->h_name, len); | 163 | safe_strncpy(name, ent->h_name, len); |
161 | } else { | 164 | } |
165 | } else { | ||
162 | #ifdef DEBUG | 166 | #ifdef DEBUG |
163 | fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad); | 167 | error_msg("getnetbyaddr (%08lx)", host_ad); |
164 | #endif | 168 | #endif |
165 | np = getnetbyaddr(host_ad, AF_INET); | 169 | np = getnetbyaddr(host_ad, AF_INET); |
166 | if (np != NULL) | 170 | if (np != NULL) { |
167 | safe_strncpy(name, np->n_name, len); | 171 | safe_strncpy(name, np->n_name, len); |
168 | } | 172 | } |
169 | if ((ent == NULL) && (np == NULL)) | 173 | } |
170 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); | 174 | if ((ent == NULL) && (np == NULL)) { |
171 | pn = (struct addr *) xmalloc(sizeof(struct addr)); | 175 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); |
172 | pn->addr = *s_in; | 176 | } |
173 | pn->next = INET_nn; | 177 | pn = (struct addr *) xmalloc(sizeof(struct addr)); |
174 | pn->host = host; | 178 | pn->addr = *s_in; |
175 | pn->name = xstrdup(name); | 179 | pn->next = INET_nn; |
176 | INET_nn = pn; | 180 | pn->host = host; |
177 | 181 | pn->name = xstrdup(name); | |
178 | return (0); | 182 | INET_nn = pn; |
183 | |||
184 | return (0); | ||
179 | } | 185 | } |
180 | 186 | ||
181 | #ifdef CONFIG_FEATURE_IPV6 | 187 | #ifdef CONFIG_FEATURE_IPV6 |
182 | 188 | ||
183 | int INET6_resolve(char *name, struct sockaddr_in6 *sin6) | 189 | int INET6_resolve(char *name, struct sockaddr_in6 *sin6) |
184 | { | 190 | { |
185 | struct addrinfo req, *ai; | 191 | struct addrinfo req, *ai; |
186 | int s; | 192 | int s; |
187 | 193 | ||
188 | memset (&req, '\0', sizeof req); | 194 | memset(&req, '\0', sizeof req); |
189 | req.ai_family = AF_INET6; | 195 | req.ai_family = AF_INET6; |
190 | if ((s = getaddrinfo(name, NULL, &req, &ai))) { | 196 | if ((s = getaddrinfo(name, NULL, &req, &ai))) { |
191 | fprintf(stderr, "getaddrinfo: %s: %d\n", name, s); | 197 | error_msg("getaddrinfo: %s: %d", name, s); |
192 | return -1; | 198 | return -1; |
193 | } | 199 | } |
194 | memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); | 200 | memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); |
195 | 201 | ||
196 | freeaddrinfo(ai); | 202 | freeaddrinfo(ai); |
197 | 203 | ||
198 | return (0); | 204 | return (0); |
199 | } | 205 | } |
200 | 206 | ||
201 | #ifndef IN6_IS_ADDR_UNSPECIFIED | 207 | #ifndef IN6_IS_ADDR_UNSPECIFIED |
@@ -205,37 +211,39 @@ int INET6_resolve(char *name, struct sockaddr_in6 *sin6) | |||
205 | #endif | 211 | #endif |
206 | 212 | ||
207 | 213 | ||
208 | int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, int numeric) | 214 | int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, |
215 | int numeric) | ||
209 | { | 216 | { |
210 | int s; | 217 | int s; |
211 | 218 | ||
212 | /* Grmpf. -FvK */ | 219 | /* Grmpf. -FvK */ |
213 | if (sin6->sin6_family != AF_INET6) { | 220 | if (sin6->sin6_family != AF_INET6) { |
214 | #ifdef DEBUG | 221 | #ifdef DEBUG |
215 | fprintf(stderr, _("rresolve: unsupport address family %d !\n"), | 222 | error_msg(_("rresolve: unsupport address family %d !\n"), |
216 | sin6->sin6_family); | 223 | sin6->sin6_family); |
217 | #endif | 224 | #endif |
218 | errno = EAFNOSUPPORT; | 225 | errno = EAFNOSUPPORT; |
219 | return (-1); | 226 | return (-1); |
220 | } | 227 | } |
221 | if (numeric & 0x7FFF) { | 228 | if (numeric & 0x7FFF) { |
222 | inet_ntop(AF_INET6, &sin6->sin6_addr, name, len); | 229 | inet_ntop(AF_INET6, &sin6->sin6_addr, name, len); |
223 | return (0); | 230 | return (0); |
224 | } | 231 | } |
225 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { | 232 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
226 | if (numeric & 0x8000) | 233 | if (numeric & 0x8000) { |
227 | strcpy(name, "default"); | 234 | strcpy(name, "default"); |
228 | else | 235 | } else { |
229 | strcpy(name, "*"); | 236 | strcpy(name, "*"); |
237 | } | ||
238 | return (0); | ||
239 | } | ||
240 | |||
241 | s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0); | ||
242 | if (s) { | ||
243 | error_msg("getnameinfo failed"); | ||
244 | return -1; | ||
245 | } | ||
230 | return (0); | 246 | return (0); |
231 | } | ||
232 | |||
233 | if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), | ||
234 | name, len , NULL, 0, 0))) { | ||
235 | fputs("getnameinfo failed\n", stderr); | ||
236 | return -1; | ||
237 | } | ||
238 | return (0); | ||
239 | } | 247 | } |
240 | 248 | ||
241 | #endif /* CONFIG_FEATURE_IPV6 */ | 249 | #endif /* CONFIG_FEATURE_IPV6 */ |