diff options
author | Matt Kraai <kraai@debian.org> | 2001-02-07 04:09:23 +0000 |
---|---|---|
committer | Matt Kraai <kraai@debian.org> | 2001-02-07 04:09:23 +0000 |
commit | 1d7026745028982980d17b1023c4ce8ec97ea946 (patch) | |
tree | e408c87ede7fc728458c316dfad2310fe68ddd3c | |
parent | df8ccb635039a6bc8cfd51308b01cc0e58bbdc4d (diff) | |
download | busybox-w32-1d7026745028982980d17b1023c4ce8ec97ea946.tar.gz busybox-w32-1d7026745028982980d17b1023c4ce8ec97ea946.tar.bz2 busybox-w32-1d7026745028982980d17b1023c4ce8ec97ea946.zip |
Add listening support.
-rw-r--r-- | applets/usage.c | 8 | ||||
-rw-r--r-- | docs/busybox.sgml | 7 | ||||
-rw-r--r-- | nc.c | 54 | ||||
-rw-r--r-- | networking/nc.c | 54 | ||||
-rw-r--r-- | usage.c | 8 |
5 files changed, 102 insertions, 29 deletions
diff --git a/applets/usage.c b/applets/usage.c index 215871b5a..1686910d5 100644 --- a/applets/usage.c +++ b/applets/usage.c | |||
@@ -959,9 +959,13 @@ const char mv_usage[] = | |||
959 | 959 | ||
960 | #if defined BB_NC | 960 | #if defined BB_NC |
961 | const char nc_usage[] = | 961 | const char nc_usage[] = |
962 | "nc [IP] [port]" | 962 | "nc [-p PORT] IP PORT\n" |
963 | " or: nc -l -p PORT" | ||
963 | #ifndef BB_FEATURE_TRIVIAL_HELP | 964 | #ifndef BB_FEATURE_TRIVIAL_HELP |
964 | "\n\nNetcat opens a pipe to IP:port" | 965 | "\n\nNetcat opens a pipe to IP:PORT\n" |
966 | "Options:\n" | ||
967 | "\t-l\tListen on the socket.\n" | ||
968 | "\t-p PORT\tBind the local port to PORT." | ||
965 | #endif | 969 | #endif |
966 | ; | 970 | ; |
967 | #endif | 971 | #endif |
diff --git a/docs/busybox.sgml b/docs/busybox.sgml index 0dc4bffa3..02d85e499 100644 --- a/docs/busybox.sgml +++ b/docs/busybox.sgml | |||
@@ -2452,7 +2452,12 @@ | |||
2452 | </para> | 2452 | </para> |
2453 | 2453 | ||
2454 | <para> | 2454 | <para> |
2455 | Open a pipe to HOST:PORT. | 2455 | or: nc -p PORT -l |
2456 | </para> | ||
2457 | |||
2458 | |||
2459 | <para> | ||
2460 | Open a pipe to HOST:PORT or listen for a connection on PORT. | ||
2456 | </para> | 2461 | </para> |
2457 | 2462 | ||
2458 | <para> | 2463 | <para> |
@@ -41,7 +41,7 @@ | |||
41 | 41 | ||
42 | int nc_main(int argc, char **argv) | 42 | int nc_main(int argc, char **argv) |
43 | { | 43 | { |
44 | int sfd; | 44 | int do_listen = 0, lport = 0, tmpfd, opt, sfd; |
45 | char buf[BUFSIZ]; | 45 | char buf[BUFSIZ]; |
46 | 46 | ||
47 | struct sockaddr_in address; | 47 | struct sockaddr_in address; |
@@ -49,24 +49,54 @@ int nc_main(int argc, char **argv) | |||
49 | 49 | ||
50 | fd_set readfds, testfds; | 50 | fd_set readfds, testfds; |
51 | 51 | ||
52 | argc--; | 52 | while ((opt = getopt(argc, argv, "lp:")) > 0) { |
53 | argv++; | 53 | switch (opt) { |
54 | if (argc < 2 || **argv == '-') { | 54 | case 'l': |
55 | usage(nc_usage); | 55 | do_listen++; |
56 | break; | ||
57 | case 'p': | ||
58 | lport = atoi(optarg); | ||
59 | break; | ||
60 | default: | ||
61 | usage(nc_usage); | ||
62 | } | ||
56 | } | 63 | } |
57 | 64 | ||
65 | if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc)) | ||
66 | usage(nc_usage); | ||
67 | |||
58 | if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | 68 | if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
59 | perror_msg_and_die("socket"); | 69 | perror_msg_and_die("socket"); |
60 | 70 | ||
61 | if ((hostinfo = gethostbyname(*argv)) == NULL) | ||
62 | error_msg_and_die("cannot resolve %s", *argv); | ||
63 | |||
64 | address.sin_family = AF_INET; | 71 | address.sin_family = AF_INET; |
65 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | ||
66 | address.sin_port = htons(atoi(*(++argv))); | ||
67 | 72 | ||
68 | if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | 73 | if (lport != 0) { |
69 | perror_msg_and_die("connect"); | 74 | memset(&address.sin_addr, 0, sizeof(address.sin_addr)); |
75 | address.sin_port = htons(lport); | ||
76 | |||
77 | if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | ||
78 | perror_msg_and_die("bind"); | ||
79 | } | ||
80 | |||
81 | if (do_listen) { | ||
82 | if (listen(sfd, 1) < 0) | ||
83 | perror_msg_and_die("listen"); | ||
84 | |||
85 | if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &opt)) < 0) | ||
86 | perror_msg_and_die("accept"); | ||
87 | |||
88 | close(sfd); | ||
89 | sfd = tmpfd; | ||
90 | } else { | ||
91 | if ((hostinfo = gethostbyname(argv[optind])) == NULL) | ||
92 | error_msg_and_die("cannot resolve %s\n", argv[optind]); | ||
93 | |||
94 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | ||
95 | address.sin_port = htons(atoi(argv[optind+1])); | ||
96 | |||
97 | if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | ||
98 | perror_msg_and_die("connect"); | ||
99 | } | ||
70 | 100 | ||
71 | FD_ZERO(&readfds); | 101 | FD_ZERO(&readfds); |
72 | FD_SET(sfd, &readfds); | 102 | FD_SET(sfd, &readfds); |
diff --git a/networking/nc.c b/networking/nc.c index 682da82bf..3f512d1cb 100644 --- a/networking/nc.c +++ b/networking/nc.c | |||
@@ -41,7 +41,7 @@ | |||
41 | 41 | ||
42 | int nc_main(int argc, char **argv) | 42 | int nc_main(int argc, char **argv) |
43 | { | 43 | { |
44 | int sfd; | 44 | int do_listen = 0, lport = 0, tmpfd, opt, sfd; |
45 | char buf[BUFSIZ]; | 45 | char buf[BUFSIZ]; |
46 | 46 | ||
47 | struct sockaddr_in address; | 47 | struct sockaddr_in address; |
@@ -49,24 +49,54 @@ int nc_main(int argc, char **argv) | |||
49 | 49 | ||
50 | fd_set readfds, testfds; | 50 | fd_set readfds, testfds; |
51 | 51 | ||
52 | argc--; | 52 | while ((opt = getopt(argc, argv, "lp:")) > 0) { |
53 | argv++; | 53 | switch (opt) { |
54 | if (argc < 2 || **argv == '-') { | 54 | case 'l': |
55 | usage(nc_usage); | 55 | do_listen++; |
56 | break; | ||
57 | case 'p': | ||
58 | lport = atoi(optarg); | ||
59 | break; | ||
60 | default: | ||
61 | usage(nc_usage); | ||
62 | } | ||
56 | } | 63 | } |
57 | 64 | ||
65 | if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc)) | ||
66 | usage(nc_usage); | ||
67 | |||
58 | if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | 68 | if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
59 | perror_msg_and_die("socket"); | 69 | perror_msg_and_die("socket"); |
60 | 70 | ||
61 | if ((hostinfo = gethostbyname(*argv)) == NULL) | ||
62 | error_msg_and_die("cannot resolve %s", *argv); | ||
63 | |||
64 | address.sin_family = AF_INET; | 71 | address.sin_family = AF_INET; |
65 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | ||
66 | address.sin_port = htons(atoi(*(++argv))); | ||
67 | 72 | ||
68 | if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | 73 | if (lport != 0) { |
69 | perror_msg_and_die("connect"); | 74 | memset(&address.sin_addr, 0, sizeof(address.sin_addr)); |
75 | address.sin_port = htons(lport); | ||
76 | |||
77 | if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | ||
78 | perror_msg_and_die("bind"); | ||
79 | } | ||
80 | |||
81 | if (do_listen) { | ||
82 | if (listen(sfd, 1) < 0) | ||
83 | perror_msg_and_die("listen"); | ||
84 | |||
85 | if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &opt)) < 0) | ||
86 | perror_msg_and_die("accept"); | ||
87 | |||
88 | close(sfd); | ||
89 | sfd = tmpfd; | ||
90 | } else { | ||
91 | if ((hostinfo = gethostbyname(argv[optind])) == NULL) | ||
92 | error_msg_and_die("cannot resolve %s\n", argv[optind]); | ||
93 | |||
94 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | ||
95 | address.sin_port = htons(atoi(argv[optind+1])); | ||
96 | |||
97 | if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) | ||
98 | perror_msg_and_die("connect"); | ||
99 | } | ||
70 | 100 | ||
71 | FD_ZERO(&readfds); | 101 | FD_ZERO(&readfds); |
72 | FD_SET(sfd, &readfds); | 102 | FD_SET(sfd, &readfds); |
@@ -959,9 +959,13 @@ const char mv_usage[] = | |||
959 | 959 | ||
960 | #if defined BB_NC | 960 | #if defined BB_NC |
961 | const char nc_usage[] = | 961 | const char nc_usage[] = |
962 | "nc [IP] [port]" | 962 | "nc [-p PORT] IP PORT\n" |
963 | " or: nc -l -p PORT" | ||
963 | #ifndef BB_FEATURE_TRIVIAL_HELP | 964 | #ifndef BB_FEATURE_TRIVIAL_HELP |
964 | "\n\nNetcat opens a pipe to IP:port" | 965 | "\n\nNetcat opens a pipe to IP:PORT\n" |
966 | "Options:\n" | ||
967 | "\t-l\tListen on the socket.\n" | ||
968 | "\t-p PORT\tBind the local port to PORT." | ||
965 | #endif | 969 | #endif |
966 | ; | 970 | ; |
967 | #endif | 971 | #endif |