Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 928 - (hide annotations) (download)
Wed Oct 28 13:31:19 2009 UTC (14 years, 7 months ago) by niro
File MIME type: text/plain
File size: 10379 byte(s)
tagged 'mkinitrd-6_1_11'
1 niro 532 /* 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 niro 816 #include <net/if.h>
11 niro 532 #include "libbb.h"
12    
13 niro 816 void FAST_FUNC setsockopt_reuseaddr(int fd)
14 niro 532 {
15 niro 816 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
16 niro 532 }
17 niro 816 int FAST_FUNC setsockopt_broadcast(int fd)
18 niro 532 {
19 niro 816 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
20 niro 532 }
21 niro 816 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 niro 532
35 niro 816
36     void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
37 niro 532 {
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 niro 816 unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
54 niro 532 {
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 niro 816 /*
78     void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
79 niro 532 {
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 niro 816
89     int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
90 niro 532 {
91     int s = xsocket(AF_INET, SOCK_STREAM, 0);
92     xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
93     return s;
94     }
95 niro 816 */
96 niro 532
97     /* "New" networking API */
98    
99    
100 niro 816 int FAST_FUNC get_nport(const struct sockaddr *sa)
101 niro 532 {
102     #if ENABLE_FEATURE_IPV6
103 niro 816 if (sa->sa_family == AF_INET6) {
104     return ((struct sockaddr_in6*)sa)->sin6_port;
105 niro 532 }
106     #endif
107 niro 816 if (sa->sa_family == AF_INET) {
108     return ((struct sockaddr_in*)sa)->sin_port;
109 niro 532 }
110 niro 816 /* What? UNIX socket? IPX?? :) */
111 niro 532 return -1;
112     }
113    
114 niro 816 void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
115 niro 532 {
116     #if ENABLE_FEATURE_IPV6
117 niro 816 if (lsa->u.sa.sa_family == AF_INET6) {
118     lsa->u.sin6.sin6_port = port;
119 niro 532 return;
120     }
121     #endif
122 niro 816 if (lsa->u.sa.sa_family == AF_INET) {
123     lsa->u.sin.sin_port = port;
124 niro 532 return;
125     }
126     /* What? UNIX socket? IPX?? :) */
127     }
128    
129 niro 816 /* 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 niro 532 {
140     int rc;
141 niro 816 len_and_sockaddr *r = NULL;
142 niro 532 struct addrinfo *result = NULL;
143 niro 816 struct addrinfo *used_res;
144 niro 532 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 niro 816 /* Even uglier parsing of [xx]:nn */
151 niro 532 host++;
152     cp = strchr(host, ']');
153 niro 816 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 niro 532 } 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 niro 816 if (cp) { /* points to ":" or "]:" */
167 niro 532 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 niro 816 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 niro 532 }
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 niro 816 #else
185     hint.ai_family = af;
186 niro 532 #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 niro 816 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
191 niro 532 rc = getaddrinfo(host, NULL, &hint, &result);
192 niro 816 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 niro 532 set_nport(r, htons(port));
214 niro 816 ret:
215 niro 532 freeaddrinfo(result);
216     return r;
217     }
218 niro 816 #if !ENABLE_FEATURE_IPV6
219     #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
220     #endif
221 niro 532
222 niro 816 #if ENABLE_FEATURE_IPV6
223     len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
224 niro 532 {
225 niro 816 return str2sockaddr(host, port, af, 0);
226 niro 532 }
227    
228 niro 816 len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
229 niro 532 {
230 niro 816 return str2sockaddr(host, port, af, DIE_ON_ERROR);
231 niro 532 }
232 niro 816 #endif
233 niro 532
234 niro 816 len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
235 niro 532 {
236 niro 816 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 niro 532 len_and_sockaddr *lsa;
254     int fd;
255 niro 816 int len;
256 niro 532
257     #if ENABLE_FEATURE_IPV6
258 niro 816 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 niro 532 len = sizeof(struct sockaddr_in6);
273 niro 816 }
274 niro 532 #endif
275 niro 816 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
276 niro 532 lsa->len = len;
277 niro 816 lsa->u.sa.sa_family = family;
278 niro 532 *lsap = lsa;
279     return fd;
280     }
281    
282 niro 816 int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
283 niro 532 {
284 niro 816 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 niro 532 int fd;
290     len_and_sockaddr *lsa;
291    
292     if (bindaddr && bindaddr[0]) {
293 niro 816 lsa = xdotted2sockaddr(bindaddr, port);
294 niro 532 /* user specified bind addr dictates family */
295 niro 816 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
296 niro 532 } else {
297 niro 816 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
298 niro 532 set_nport(lsa, htons(port));
299     }
300     setsockopt_reuseaddr(fd);
301 niro 816 xbind(fd, &lsa->u.sa, lsa->len);
302 niro 532 free(lsa);
303     return fd;
304     }
305    
306 niro 816 int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
307 niro 532 {
308 niro 816 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 niro 532 int fd;
320     len_and_sockaddr *lsa;
321    
322 niro 816 lsa = xhost2sockaddr(peer, port);
323     fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
324 niro 532 setsockopt_reuseaddr(fd);
325 niro 816 xconnect(fd, &lsa->u.sa, lsa->len);
326 niro 532 free(lsa);
327     return fd;
328     }
329    
330 niro 816 int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
331 niro 532 {
332 niro 816 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
333     xconnect(fd, &lsa->u.sa, lsa->len);
334 niro 532 return fd;
335     }
336    
337 niro 816 /* 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 niro 532 {
342     char host[128];
343     char serv[16];
344 niro 816 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 niro 532 host, sizeof(host),
356 niro 816 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
357 niro 532 serv, sizeof(serv),
358 niro 816 /* do not resolve port# into service _name_ */
359     flags | NI_NUMERICSERV
360 niro 532 );
361 niro 816 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 niro 532 }
378    
379 niro 816 char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
380 niro 532 {
381 niro 816 return sockaddr2str(sa, 0);
382 niro 532 }
383    
384 niro 816 char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
385 niro 532 {
386 niro 816 return sockaddr2str(sa, IGNORE_PORT);
387 niro 532 }
388 niro 816
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     }