Magellan Linux

Contents of /tags/mkinitrd-6_1_11/busybox/libbb/xconnect.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 928 - (show annotations) (download)
Wed Oct 28 13:31:19 2009 UTC (14 years, 6 months ago) by niro
File MIME type: text/plain
File size: 10379 byte(s)
tagged 'mkinitrd-6_1_11'
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
4 *
5 * Connect to host at port using address resolution from getaddrinfo
6 *
7 */
8
9 #include <netinet/in.h>
10 #include <net/if.h>
11 #include "libbb.h"
12
13 void FAST_FUNC setsockopt_reuseaddr(int fd)
14 {
15 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
16 }
17 int FAST_FUNC setsockopt_broadcast(int fd)
18 {
19 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
20 }
21 int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
22 {
23 int r;
24 struct ifreq ifr;
25 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
26 /* Actually, ifr_name is at offset 0, and in practice
27 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
28 * But just in case it's not true on some obscure arch... */
29 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
30 if (r)
31 bb_perror_msg("can't bind to interface %s", iface);
32 return r;
33 }
34
35
36 void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
37 {
38 if (connect(s, s_addr, addrlen) < 0) {
39 if (ENABLE_FEATURE_CLEAN_UP)
40 close(s);
41 if (s_addr->sa_family == AF_INET)
42 bb_perror_msg_and_die("%s (%s)",
43 "cannot connect to remote host",
44 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
45 bb_perror_msg_and_die("cannot connect to remote host");
46 }
47 }
48
49 /* Return port number for a service.
50 * If "port" is a number use it as the port.
51 * If "port" is a name it is looked up in /etc/services, if it isnt found return
52 * default_port */
53 unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
54 {
55 unsigned port_nr = default_port;
56 if (port) {
57 int old_errno;
58
59 /* Since this is a lib function, we're not allowed to reset errno to 0.
60 * Doing so could break an app that is deferring checking of errno. */
61 old_errno = errno;
62 port_nr = bb_strtou(port, NULL, 10);
63 if (errno || port_nr > 65535) {
64 struct servent *tserv = getservbyname(port, protocol);
65 port_nr = default_port;
66 if (tserv)
67 port_nr = ntohs(tserv->s_port);
68 }
69 errno = old_errno;
70 }
71 return (uint16_t)port_nr;
72 }
73
74
75 /* "Old" networking API - only IPv4 */
76
77 /*
78 void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
79 {
80 struct hostent *he;
81
82 memset(s_in, 0, sizeof(struct sockaddr_in));
83 s_in->sin_family = AF_INET;
84 he = xgethostbyname(host);
85 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
86 }
87
88
89 int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
90 {
91 int s = xsocket(AF_INET, SOCK_STREAM, 0);
92 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
93 return s;
94 }
95 */
96
97 /* "New" networking API */
98
99
100 int FAST_FUNC get_nport(const struct sockaddr *sa)
101 {
102 #if ENABLE_FEATURE_IPV6
103 if (sa->sa_family == AF_INET6) {
104 return ((struct sockaddr_in6*)sa)->sin6_port;
105 }
106 #endif
107 if (sa->sa_family == AF_INET) {
108 return ((struct sockaddr_in*)sa)->sin_port;
109 }
110 /* What? UNIX socket? IPX?? :) */
111 return -1;
112 }
113
114 void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
115 {
116 #if ENABLE_FEATURE_IPV6
117 if (lsa->u.sa.sa_family == AF_INET6) {
118 lsa->u.sin6.sin6_port = port;
119 return;
120 }
121 #endif
122 if (lsa->u.sa.sa_family == AF_INET) {
123 lsa->u.sin.sin_port = port;
124 return;
125 }
126 /* What? UNIX socket? IPX?? :) */
127 }
128
129 /* We hijack this constant to mean something else */
130 /* It doesn't hurt because we will remove this bit anyway */
131 #define DIE_ON_ERROR AI_CANONNAME
132
133 /* host: "1.2.3.4[:port]", "www.google.com[:port]"
134 * port: if neither of above specifies port # */
135 static len_and_sockaddr* str2sockaddr(
136 const char *host, int port,
137 USE_FEATURE_IPV6(sa_family_t af,)
138 int ai_flags)
139 {
140 int rc;
141 len_and_sockaddr *r = NULL;
142 struct addrinfo *result = NULL;
143 struct addrinfo *used_res;
144 const char *org_host = host; /* only for error msg */
145 const char *cp;
146 struct addrinfo hint;
147
148 /* Ugly parsing of host:addr */
149 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
150 /* Even uglier parsing of [xx]:nn */
151 host++;
152 cp = strchr(host, ']');
153 if (!cp || cp[1] != ':') { /* Malformed: must have [xx]:nn */
154 bb_error_msg("bad address '%s'", org_host);
155 if (ai_flags & DIE_ON_ERROR)
156 xfunc_die();
157 return NULL;
158 }
159 } else {
160 cp = strrchr(host, ':');
161 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
162 /* There is more than one ':' (e.g. "::1") */
163 cp = NULL; /* it's not a port spec */
164 }
165 }
166 if (cp) { /* points to ":" or "]:" */
167 int sz = cp - host + 1;
168 host = safe_strncpy(alloca(sz), host, sz);
169 if (ENABLE_FEATURE_IPV6 && *cp != ':')
170 cp++; /* skip ']' */
171 cp++; /* skip ':' */
172 port = bb_strtou(cp, NULL, 10);
173 if (errno || (unsigned)port > 0xffff) {
174 bb_error_msg("bad port spec '%s'", org_host);
175 if (ai_flags & DIE_ON_ERROR)
176 xfunc_die();
177 return NULL;
178 }
179 }
180
181 memset(&hint, 0 , sizeof(hint));
182 #if !ENABLE_FEATURE_IPV6
183 hint.ai_family = AF_INET; /* do not try to find IPv6 */
184 #else
185 hint.ai_family = af;
186 #endif
187 /* Needed. Or else we will get each address thrice (or more)
188 * for each possible socket type (tcp,udp,raw...): */
189 hint.ai_socktype = SOCK_STREAM;
190 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
191 rc = getaddrinfo(host, NULL, &hint, &result);
192 if (rc || !result) {
193 bb_error_msg("bad address '%s'", org_host);
194 if (ai_flags & DIE_ON_ERROR)
195 xfunc_die();
196 goto ret;
197 }
198 used_res = result;
199 #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
200 while (1) {
201 if (used_res->ai_family == AF_INET)
202 break;
203 used_res = used_res->ai_next;
204 if (!used_res) {
205 used_res = result;
206 break;
207 }
208 }
209 #endif
210 r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
211 r->len = used_res->ai_addrlen;
212 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
213 set_nport(r, htons(port));
214 ret:
215 freeaddrinfo(result);
216 return r;
217 }
218 #if !ENABLE_FEATURE_IPV6
219 #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
220 #endif
221
222 #if ENABLE_FEATURE_IPV6
223 len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
224 {
225 return str2sockaddr(host, port, af, 0);
226 }
227
228 len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
229 {
230 return str2sockaddr(host, port, af, DIE_ON_ERROR);
231 }
232 #endif
233
234 len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
235 {
236 return str2sockaddr(host, port, AF_UNSPEC, 0);
237 }
238
239 len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
240 {
241 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
242 }
243
244 len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
245 {
246 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
247 }
248
249 #undef xsocket_type
250 int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
251 {
252 SKIP_FEATURE_IPV6(enum { family = AF_INET };)
253 len_and_sockaddr *lsa;
254 int fd;
255 int len;
256
257 #if ENABLE_FEATURE_IPV6
258 if (family == AF_UNSPEC) {
259 fd = socket(AF_INET6, sock_type, 0);
260 if (fd >= 0) {
261 family = AF_INET6;
262 goto done;
263 }
264 family = AF_INET;
265 }
266 #endif
267 fd = xsocket(family, sock_type, 0);
268 len = sizeof(struct sockaddr_in);
269 #if ENABLE_FEATURE_IPV6
270 if (family == AF_INET6) {
271 done:
272 len = sizeof(struct sockaddr_in6);
273 }
274 #endif
275 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
276 lsa->len = len;
277 lsa->u.sa.sa_family = family;
278 *lsap = lsa;
279 return fd;
280 }
281
282 int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
283 {
284 return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
285 }
286
287 static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
288 {
289 int fd;
290 len_and_sockaddr *lsa;
291
292 if (bindaddr && bindaddr[0]) {
293 lsa = xdotted2sockaddr(bindaddr, port);
294 /* user specified bind addr dictates family */
295 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
296 } else {
297 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
298 set_nport(lsa, htons(port));
299 }
300 setsockopt_reuseaddr(fd);
301 xbind(fd, &lsa->u.sa, lsa->len);
302 free(lsa);
303 return fd;
304 }
305
306 int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
307 {
308 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
309 }
310
311 int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
312 {
313 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
314 }
315
316
317 int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
318 {
319 int fd;
320 len_and_sockaddr *lsa;
321
322 lsa = xhost2sockaddr(peer, port);
323 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
324 setsockopt_reuseaddr(fd);
325 xconnect(fd, &lsa->u.sa, lsa->len);
326 free(lsa);
327 return fd;
328 }
329
330 int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
331 {
332 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
333 xconnect(fd, &lsa->u.sa, lsa->len);
334 return fd;
335 }
336
337 /* We hijack this constant to mean something else */
338 /* It doesn't hurt because we will add this bit anyway */
339 #define IGNORE_PORT NI_NUMERICSERV
340 static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
341 {
342 char host[128];
343 char serv[16];
344 int rc;
345 socklen_t salen;
346
347 salen = LSA_SIZEOF_SA;
348 #if ENABLE_FEATURE_IPV6
349 if (sa->sa_family == AF_INET)
350 salen = sizeof(struct sockaddr_in);
351 if (sa->sa_family == AF_INET6)
352 salen = sizeof(struct sockaddr_in6);
353 #endif
354 rc = getnameinfo(sa, salen,
355 host, sizeof(host),
356 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
357 serv, sizeof(serv),
358 /* do not resolve port# into service _name_ */
359 flags | NI_NUMERICSERV
360 );
361 if (rc)
362 return NULL;
363 if (flags & IGNORE_PORT)
364 return xstrdup(host);
365 #if ENABLE_FEATURE_IPV6
366 if (sa->sa_family == AF_INET6) {
367 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
368 return xasprintf("[%s]:%s", host, serv);
369 /*return xasprintf("%s:%s", host, serv);*/
370 /* - fall through instead */
371 }
372 #endif
373 /* For now we don't support anything else, so it has to be INET */
374 /*if (sa->sa_family == AF_INET)*/
375 return xasprintf("%s:%s", host, serv);
376 /*return xstrdup(host);*/
377 }
378
379 char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
380 {
381 return sockaddr2str(sa, 0);
382 }
383
384 char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
385 {
386 return sockaddr2str(sa, IGNORE_PORT);
387 }
388
389 char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
390 {
391 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
392 }
393 char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
394 {
395 return sockaddr2str(sa, NI_NUMERICHOST);
396 }
397
398 char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
399 {
400 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
401 }