aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-12 20:59:31 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-12 20:59:31 +0000
commit5d68724d5b89fbf2856fdfbf36b85ac36a8f4464 (patch)
tree755b7393aab965f03de260a833f77cc4d51e6187
parent2c91652bbcc82c794c26230806058b04f1711033 (diff)
downloadbusybox-w32-5d68724d5b89fbf2856fdfbf36b85ac36a8f4464.tar.gz
busybox-w32-5d68724d5b89fbf2856fdfbf36b85ac36a8f4464.tar.bz2
busybox-w32-5d68724d5b89fbf2856fdfbf36b85ac36a8f4464.zip
next part of ipv6-ization. mostly netcat.
-rw-r--r--include/libbb.h13
-rw-r--r--include/usage.h2
-rw-r--r--libbb/xconnect.c50
-rw-r--r--networking/ftpgetput.c2
-rw-r--r--networking/nc.c103
-rw-r--r--networking/wget.c2
6 files changed, 95 insertions, 77 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 07b1d1158..c088946d9 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -307,6 +307,9 @@ typedef struct len_and_sockaddr {
307#endif 307#endif
308 }; 308 };
309} len_and_sockaddr; 309} len_and_sockaddr;
310/* Create stream socket, and allocated suitable lsa
311 * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6)) */
312int xsocket_stream(len_and_sockaddr **lsap);
310/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL, 313/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL,
311 * numeric IP ("N.N.N.N") or numeric IPv6 address, 314 * numeric IP ("N.N.N.N") or numeric IPv6 address,
312 * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). 315 * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
@@ -324,12 +327,14 @@ extern int xconnect_stream(const len_and_sockaddr *lsa);
324 * (depending on host), but in theory nothing prevents e.g. 327 * (depending on host), but in theory nothing prevents e.g.
325 * UNIX socket address being returned, IPX sockaddr etc... */ 328 * UNIX socket address being returned, IPX sockaddr etc... */
326extern len_and_sockaddr* host2sockaddr(const char *host, int port); 329extern len_and_sockaddr* host2sockaddr(const char *host, int port);
327/* assign sin[6]_port member if the socket is of corresponding type, 330/* Assign sin[6]_port member if the socket is of corresponding type,
328 * otherwise noop. Useful for ftp. 331 * otherwise noop. Useful for ftp.
329 * NB: does NOT do htons() internally, just direct assignment. */ 332 * NB: does NOT do htons() internally, just direct assignment. */
330extern void set_port(len_and_sockaddr *lsa, unsigned port); 333extern void set_nport(len_and_sockaddr *lsa, unsigned port);
331char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen); 334/* Retrieve sin[6]_port or return -1 for non-inet lsa's */
332char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen); 335extern int get_nport(len_and_sockaddr *lsa);
336extern char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen);
337extern char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen);
333 338
334 339
335extern char *xstrdup(const char *s); 340extern char *xstrdup(const char *s);
diff --git a/include/usage.h b/include/usage.h
index 4e5206558..0275df3f0 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2249,7 +2249,7 @@
2249 "\n -e Exec rest of command line after connect" \ 2249 "\n -e Exec rest of command line after connect" \
2250 "\n -i SECS Delay interval for lines sent" \ 2250 "\n -i SECS Delay interval for lines sent" \
2251 "\n -w SECS Timeout for connect" \ 2251 "\n -w SECS Timeout for connect" \
2252 "\n -f file Use file (ala /dev/ttyS0) instead of network" \ 2252 "\n -f FILE Use file (ala /dev/ttyS0) instead of network" \
2253 ) \ 2253 ) \
2254 USE_NC_SERVER( \ 2254 USE_NC_SERVER( \
2255 "\n -l Listen mode, for inbound connects" \ 2255 "\n -l Listen mode, for inbound connects" \
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index f5a7e6dc8..65554b24e 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -32,7 +32,7 @@ void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
32 } 32 }
33} 33}
34 34
35/* Return network byte ordered port number for a service. 35/* Return port number for a service.
36 * If "port" is a number use it as the port. 36 * If "port" is a number use it as the port.
37 * If "port" is a name it is looked up in /etc/services, if it isnt found return 37 * If "port" is a name it is looked up in /etc/services, if it isnt found return
38 * default_port */ 38 * default_port */
@@ -81,7 +81,21 @@ int xconnect_tcp_v4(struct sockaddr_in *s_addr)
81/* "New" networking API */ 81/* "New" networking API */
82 82
83 83
84void set_port(len_and_sockaddr *lsa, unsigned port) 84int get_nport(len_and_sockaddr *lsa)
85{
86#if ENABLE_FEATURE_IPV6
87 if (lsa->sa.sa_family == AF_INET6) {
88 return lsa->sin6.sin6_port;
89 }
90#endif
91 if (lsa->sa.sa_family == AF_INET) {
92 return lsa->sin.sin_port;
93 }
94 return -1;
95 /* What? UNIX socket? IPX?? :) */
96}
97
98void set_nport(len_and_sockaddr *lsa, unsigned port)
85{ 99{
86#if ENABLE_FEATURE_IPV6 100#if ENABLE_FEATURE_IPV6
87 if (lsa->sa.sa_family == AF_INET6) { 101 if (lsa->sa.sa_family == AF_INET6) {
@@ -146,7 +160,7 @@ static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
146 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen); 160 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
147 r->len = result->ai_addrlen; 161 r->len = result->ai_addrlen;
148 memcpy(&r->sa, result->ai_addr, result->ai_addrlen); 162 memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
149 set_port(r, htons(port)); 163 set_nport(r, htons(port));
150 freeaddrinfo(result); 164 freeaddrinfo(result);
151 return r; 165 return r;
152} 166}
@@ -161,19 +175,27 @@ static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
161 return str2sockaddr(host, port, NI_NUMERICHOST); 175 return str2sockaddr(host, port, NI_NUMERICHOST);
162} 176}
163 177
164static int xsocket_stream(len_and_sockaddr *lsa) 178int xsocket_stream(len_and_sockaddr **lsap)
165{ 179{
180 len_and_sockaddr *lsa;
166 int fd; 181 int fd;
182 int len = sizeof(struct sockaddr_in);
183 int family = AF_INET;
184
167#if ENABLE_FEATURE_IPV6 185#if ENABLE_FEATURE_IPV6
168 fd = socket(AF_INET6, SOCK_STREAM, 0); 186 fd = socket(AF_INET6, SOCK_STREAM, 0);
169 lsa->sa.sa_family = AF_INET6; 187 if (fd >= 0) {
170 lsa->len = sizeof(struct sockaddr_in6); 188 len = sizeof(struct sockaddr_in6);
171 if (fd >= 0) 189 family = AF_INET6;
172 return fd; 190 } else
173#endif 191#endif
174 fd = xsocket(AF_INET, SOCK_STREAM, 0); 192 {
175 lsa->sa.sa_family = AF_INET; 193 fd = xsocket(AF_INET, SOCK_STREAM, 0);
176 lsa->len = sizeof(struct sockaddr_in); 194 }
195 lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
196 lsa->len = len;
197 lsa->sa.sa_family = family;
198 *lsap = lsa;
177 return fd; 199 return fd;
178} 200}
179 201
@@ -190,11 +212,7 @@ int create_and_bind_stream_or_die(const char *bindaddr, int port)
190 /* user specified bind addr dictates family */ 212 /* user specified bind addr dictates family */
191 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); 213 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
192 } else { 214 } else {
193 lsa = xzalloc(offsetof(len_and_sockaddr, sa) + 215 fd = xsocket_stream(&lsa);
194 USE_FEATURE_IPV6(sizeof(struct sockaddr_in6))
195 SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in))
196 );
197 fd = xsocket_stream(lsa);
198 } 216 }
199 setsockopt_reuseaddr(fd); 217 setsockopt_reuseaddr(fd);
200 xbind(fd, &lsa->sa, lsa->len); 218 xbind(fd, &lsa->sa, lsa->len);
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 51e230296..0e2d4173f 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -88,7 +88,7 @@ static int xconnect_ftpdata(ftp_host_info_t *server, char *buf)
88 *buf_ptr = '\0'; 88 *buf_ptr = '\0';
89 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256; 89 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
90 90
91 set_port(server->lsa, htons(port_num)); 91 set_nport(server->lsa, htons(port_num));
92 return xconnect_stream(server->lsa); 92 return xconnect_stream(server->lsa);
93} 93}
94 94
diff --git a/networking/nc.c b/networking/nc.c
index e1c22839c..37e658d4b 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -9,6 +9,10 @@
9 9
10#include "busybox.h" 10#include "busybox.h"
11 11
12/* Lots of small differences in features
13 * when compared to "standard" nc
14 */
15
12static void timeout(int signum) 16static void timeout(int signum)
13{ 17{
14 bb_error_msg_and_die("timed out"); 18 bb_error_msg_and_die("timed out");
@@ -16,7 +20,8 @@ static void timeout(int signum)
16 20
17int nc_main(int argc, char **argv) 21int nc_main(int argc, char **argv)
18{ 22{
19 int sfd = 0; 23 /* sfd sits _here_ only because of "repeat" option (-l -l). */
24 int sfd = sfd; /* for gcc */
20 int cfd = 0; 25 int cfd = 0;
21 SKIP_NC_SERVER(const) unsigned do_listen = 0; 26 SKIP_NC_SERVER(const) unsigned do_listen = 0;
22 SKIP_NC_SERVER(const) unsigned lport = 0; 27 SKIP_NC_SERVER(const) unsigned lport = 0;
@@ -24,12 +29,10 @@ int nc_main(int argc, char **argv)
24 SKIP_NC_EXTRA (const) unsigned delay = 0; 29 SKIP_NC_EXTRA (const) unsigned delay = 0;
25 SKIP_NC_EXTRA (const int execparam = 0;) 30 SKIP_NC_EXTRA (const int execparam = 0;)
26 USE_NC_EXTRA (char **execparam = NULL;) 31 USE_NC_EXTRA (char **execparam = NULL;)
27 struct sockaddr_in address; 32 len_and_sockaddr *lsa;
28 fd_set readfds, testfds; 33 fd_set readfds, testfds;
29 int opt; /* must be signed (getopt returns -1) */ 34 int opt; /* must be signed (getopt returns -1) */
30 35
31 memset(&address, 0, sizeof(address));
32
33 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) { 36 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
34 /* getopt32 is _almost_ usable: 37 /* getopt32 is _almost_ usable:
35 ** it cannot handle "... -e prog -prog-opt" */ 38 ** it cannot handle "... -e prog -prog-opt" */
@@ -39,7 +42,6 @@ int nc_main(int argc, char **argv)
39 if (ENABLE_NC_SERVER && opt=='l') USE_NC_SERVER(do_listen++); 42 if (ENABLE_NC_SERVER && opt=='l') USE_NC_SERVER(do_listen++);
40 else if (ENABLE_NC_SERVER && opt=='p') { 43 else if (ENABLE_NC_SERVER && opt=='p') {
41 USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0)); 44 USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
42 USE_NC_SERVER(lport = htons(lport));
43 } 45 }
44 else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg)); 46 else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg));
45 else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg)); 47 else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg));
@@ -71,9 +73,11 @@ int nc_main(int argc, char **argv)
71 // -l and -f don't mix 73 // -l and -f don't mix
72 if (do_listen && cfd) bb_show_usage(); 74 if (do_listen && cfd) bb_show_usage();
73 // Listen or file modes need zero arguments, client mode needs 2 75 // Listen or file modes need zero arguments, client mode needs 2
74 opt = ((do_listen || cfd) ? 0 : 2); 76 if (do_listen || cfd) {
75 if (argc != opt) 77 if (argc) bb_show_usage();
76 bb_show_usage(); 78 } else {
79 if (!argc || argc > 2) bb_show_usage();
80 }
77 } else { 81 } else {
78 if (argc != 3) bb_show_usage(); 82 if (argc != 3) bb_show_usage();
79 argc--; 83 argc--;
@@ -86,46 +90,37 @@ int nc_main(int argc, char **argv)
86 } 90 }
87 91
88 if (!cfd) { 92 if (!cfd) {
89 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
90 fcntl(sfd, F_SETFD, FD_CLOEXEC);
91 setsockopt_reuseaddr(sfd);
92 address.sin_family = AF_INET;
93
94 // Set local port.
95
96 if (lport != 0) {
97 address.sin_port = lport;
98 xbind(sfd, (struct sockaddr *) &address, sizeof(address));
99 }
100
101 if (do_listen) { 93 if (do_listen) {
102 socklen_t addrlen = sizeof(address); 94 socklen_t addrlen;
103 95
104 xlisten(sfd, do_listen); 96 /* create_and_bind_stream_or_die(NULL, lport)
105 97 * would've work wonderfully, but we need
106 // If we didn't specify a port number, query and print it to stderr. 98 * to know lsa */
107 99 sfd = xsocket_stream(&lsa);
100 if (lport)
101 set_nport(lsa, htons(lport));
102 setsockopt_reuseaddr(sfd);
103 xbind(sfd, &lsa->sa, lsa->len);
104 xlisten(sfd, do_listen); /* can be > 1 */
105 /* If we didn't specify a port number,
106 * query and print it after listen() */
108 if (!lport) { 107 if (!lport) {
109 socklen_t len = sizeof(address); 108 addrlen = lsa->len;
110 getsockname(sfd, (struct sockaddr *) &address, &len); 109 getsockname(sfd, &lsa->sa, &addrlen);
111 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port)); 110 lport = get_nport(lsa);
111 fdprintf(2, "%d\n", ntohs(lport));
112 } 112 }
113 repeatyness: 113 fcntl(sfd, F_SETFD, FD_CLOEXEC);
114 cfd = accept(sfd, (struct sockaddr *) &address, &addrlen); 114 accept_again:
115 addrlen = lsa->len;
116 cfd = accept(sfd, NULL, 0); /* &lsa->sa, &addrlen); */
115 if (cfd < 0) 117 if (cfd < 0)
116 bb_perror_msg_and_die("accept"); 118 bb_perror_msg_and_die("accept");
117 119 if (!execparam)
118 if (!execparam) close(sfd); 120 close(sfd);
119 } else { 121 } else {
120 struct hostent *hostinfo; 122 cfd = create_and_connect_stream_or_die(argv[0],
121 hostinfo = xgethostbyname(argv[0]); 123 argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
122
123 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
124 address.sin_port = bb_lookup_port(argv[1], "tcp", 0);
125 address.sin_port = htons(address.sin_port);
126
127 xconnect(sfd, (struct sockaddr *) &address, sizeof(address));
128 cfd = sfd;
129 } 124 }
130 } 125 }
131 126
@@ -136,17 +131,10 @@ int nc_main(int argc, char **argv)
136 131
137 /* -e given? */ 132 /* -e given? */
138 if (execparam) { 133 if (execparam) {
139 if (cfd) { 134 signal(SIGCHLD, SIG_IGN);
140 signal(SIGCHLD, SIG_IGN);
141 dup2(cfd, 0);
142 close(cfd);
143 }
144 dup2(0, 1);
145 dup2(0, 2);
146
147 // With more than one -l, repeatedly act as server. 135 // With more than one -l, repeatedly act as server.
148
149 if (do_listen > 1 && vfork()) { 136 if (do_listen > 1 && vfork()) {
137 /* parent */
150 // This is a bit weird as cleanup goes, since we wind up with no 138 // This is a bit weird as cleanup goes, since we wind up with no
151 // stdin/stdout/stderr. But it's small and shouldn't hurt anything. 139 // stdin/stdout/stderr. But it's small and shouldn't hurt anything.
152 // We check for cfd == 0 above. 140 // We check for cfd == 0 above.
@@ -154,9 +142,15 @@ int nc_main(int argc, char **argv)
154 close(0); 142 close(0);
155 close(1); 143 close(1);
156 close(2); 144 close(2);
157 145 goto accept_again;
158 goto repeatyness;
159 } 146 }
147 /* child (or main thread if no multiple -l) */
148 if (cfd) {
149 dup2(cfd, 0);
150 close(cfd);
151 }
152 dup2(0, 1);
153 dup2(0, 2);
160 USE_NC_EXTRA(execvp(execparam[0], execparam);) 154 USE_NC_EXTRA(execvp(execparam[0], execparam);)
161 /* Don't print stuff or it will go over the wire.... */ 155 /* Don't print stuff or it will go over the wire.... */
162 _exit(127); 156 _exit(127);
@@ -184,7 +178,8 @@ int nc_main(int argc, char **argv)
184 sizeof(bb_common_bufsiz1)); 178 sizeof(bb_common_bufsiz1));
185 179
186 if (fd == cfd) { 180 if (fd == cfd) {
187 if (nread<1) exit(0); 181 if (nread < 1)
182 exit(0);
188 ofd = STDOUT_FILENO; 183 ofd = STDOUT_FILENO;
189 } else { 184 } else {
190 if (nread<1) { 185 if (nread<1) {
diff --git a/networking/wget.c b/networking/wget.c
index 22e610699..cc768db8b 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -432,7 +432,7 @@ int wget_main(int argc, char **argv)
432 s = strrchr(buf, ','); 432 s = strrchr(buf, ',');
433 if (!s) goto pasv_error; 433 if (!s) goto pasv_error;
434 port += xatou_range(s+1, 0, 255) * 256; 434 port += xatou_range(s+1, 0, 255) * 256;
435 set_port(lsa, htons(port)); 435 set_nport(lsa, htons(port));
436 dfp = open_socket(lsa); 436 dfp = open_socket(lsa);
437 437
438 if (beg_range) { 438 if (beg_range) {