annotate toys/netcat.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents d44eb064da41
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
2 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
3 * nc: mini-netcat - Forward stdin/stdout to a file or network connection.
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2007 Rob Landley <rob@landley.net>
194
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 189
diff changeset
6 *
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 189
diff changeset
7 * Not in SUSv3.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
8
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
9 USE_NETCAT(OLDTOY(nc, netcat, USE_NETCAT_LISTEN("tl^L^")"w#p#s:q#f:", TOYFLAG_BIN))
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
10 USE_NETCAT(NEWTOY(netcat, USE_NETCAT_LISTEN("tl^L^")"w#p#s:q#f:", TOYFLAG_BIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
11
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
12 config NETCAT
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 bool "netcat"
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
14 default y
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
15 help
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
16 usage: netcat [-wpq #] [-s addr] {IPADDR PORTNUM|-f FILENAME|-let} [-e COMMAND]
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
17
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
18 -w SECONDS timeout for connection
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
19 -p local port number
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
20 -s local ipv4 address
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
21 -q SECONDS quit this many seconds after EOF on stdin.
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
22 -f use FILENAME (ala /dev/ttyS0) instead of network
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
23
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
24 Use "stty 115200 -F /dev/ttyS0 && stty raw -echo -ctlecho" with
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
25 netcat -f to connect to a serial port.
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
26
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
27
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
28 config NETCAT_LISTEN
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
29 bool "netcat sever options (-let)"
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
30 default y
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
31 depends on NETCAT
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
32 help
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
33 -t allocate tty (must come before -l or -L)
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
34 -l listen for one incoming connection.
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
35 -L listen for multiple incoming connections (server mode).
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
36
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
37 Any additional command line arguments after -l or -L are executed
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
38 to handle each incoming connection. If none, the connection is
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
39 forwarded to stdin/stdout.
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
40
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
41 For a quick-and-dirty server, try something like:
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
42 netcat -s 127.0.0.1 -p 1234 -tL /bin/bash -l
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
43 */
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
44
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 #include "toys.h"
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 #include "toynet.h"
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
48 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
49 char *filename; // -f read from filename instead of network
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
50 long quit_delay; // -q Exit after EOF from stdin after # seconds.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
51 char *source_address; // -s Bind to a specific source address.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
52 long port; // -p Bind to a specific source port.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
53 long wait; // -w Wait # seconds for a connection.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
54 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
55
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
56 #define TT this.netcat
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
58 #define FLAG_f 1
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
59 #define FLAG_L 32
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
60 #define FLAG_l 64
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
61 #define FLAG_t 128
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
62
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 static void timeout(int signum)
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 {
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
65 if (TT.wait) error_exit("Timeout");
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
66 exit(0);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
67 }
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
68
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
69 static void set_alarm(int seconds)
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
70 {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
71 signal(SIGALRM, seconds ? timeout : SIG_DFL);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
72 alarm(seconds);
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
74
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 // Translate x.x.x.x numeric IPv4 address, or else DNS lookup an IPv4 name.
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
76 static void lookup_name(char *name, uint32_t *result)
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 struct hostent *hostbyname;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
79
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
80 hostbyname = gethostbyname(name);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
81 if (!hostbyname) error_exit("no host '%s'", name);
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 *result = *(uint32_t *)*hostbyname->h_addr_list;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
84
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 // Worry about a fancy lookup later.
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
86 static void lookup_port(char *str, uint16_t *port)
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 *port = SWAP_BE16(atoi(str));
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
90
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 void netcat_main(void)
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 {
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
93 int sockfd=-1, pollcount=2;
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 struct pollfd pollfds[2];
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
324
50c8afc263ee More than 4k of input can be queued up in a pipe, so loop needs to continue reading until POLLIN is exhausted before acknowledging POLLHUP.
Rob Landley <rob@landley.net>
parents: 323
diff changeset
96 memset(pollfds, 0, 2*sizeof(struct pollfd));
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
97 pollfds[0].events = pollfds[1].events = POLLIN;
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
98 set_alarm(TT.wait);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
99
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
100 // The argument parsing logic can't make "<2" conditional on other
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
101 // arguments like -f and -l, so we do it by hand here.
328
48e0f1f45181 Fix netcat -f
Rob Landley <rob@landley.net>
parents: 324
diff changeset
102 if (toys.optflags&FLAG_f) {
48e0f1f45181 Fix netcat -f
Rob Landley <rob@landley.net>
parents: 324
diff changeset
103 if (toys.optc) toys.exithelp++;
48e0f1f45181 Fix netcat -f
Rob Landley <rob@landley.net>
parents: 324
diff changeset
104 } else if (!(toys.optflags&(FLAG_l|FLAG_L)) && toys.optc!=2) toys.exithelp++;
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
105
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
106 if (toys.exithelp) error_exit("Argument count wrong");
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 if (TT.filename) pollfds[0].fd = xopen(TT.filename, O_RDWR);
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 else {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 int temp;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 struct sockaddr_in address;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
112
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 // Setup socket
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 sockfd = socket(AF_INET, SOCK_STREAM, 0);
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 if (-1 == sockfd) perror_exit("socket");
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 temp = 1;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 memset(&address, 0, sizeof(address));
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 address.sin_family = AF_INET;
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
121 if (TT.source_address || TT.port) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
122 address.sin_port = SWAP_BE16(TT.port);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
123 if (TT.source_address)
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
124 lookup_name(TT.source_address, (uint32_t *)&address.sin_addr);
532
d44eb064da41 Silence a couple pointless warnings.
Rob Landley <rob@landley.net>
parents: 328
diff changeset
125 if (bind(sockfd, (struct sockaddr *)&address, sizeof(address)))
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 perror_exit("bind");
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
128
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
129 // Dial out
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
130
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
131 if (!CFG_NETCAT_LISTEN || !(toys.optflags&(FLAG_L|FLAG_l))) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
132 // Figure out where to dial out to.
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
133 lookup_name(*toys.optargs, (uint32_t *)&address.sin_addr);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
134 lookup_port(toys.optargs[1], &address.sin_port);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
135 temp = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
136 if (temp<0) perror_exit("connect");
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
137 pollfds[0].fd = sockfd;
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
138
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
139 // Listen for incoming connections
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
140
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
141 } else {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
142 socklen_t len = sizeof(address);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
143
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
144 if (listen(sockfd, 5)) error_exit("listen");
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
145 if (!TT.port) {
532
d44eb064da41 Silence a couple pointless warnings.
Rob Landley <rob@landley.net>
parents: 328
diff changeset
146 getsockname(sockfd, (struct sockaddr *)&address, &len);
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
147 printf("%d\n", SWAP_BE16(address.sin_port));
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
148 fflush(stdout);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
149 }
322
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
150 // Do we need to return immediately because -l has arguments?
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
151
323
dd0b130edf6d Shell $(netcat) won't return unless child's stdin/stdout/stderr are closed.
Rob Landley <rob@landley.net>
parents: 322
diff changeset
152 if ((toys.optflags&FLAG_l) && toys.optc) {
322
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
153 if (fork()) goto cleanup;
323
dd0b130edf6d Shell $(netcat) won't return unless child's stdin/stdout/stderr are closed.
Rob Landley <rob@landley.net>
parents: 322
diff changeset
154 close(0);
dd0b130edf6d Shell $(netcat) won't return unless child's stdin/stdout/stderr are closed.
Rob Landley <rob@landley.net>
parents: 322
diff changeset
155 close(1);
dd0b130edf6d Shell $(netcat) won't return unless child's stdin/stdout/stderr are closed.
Rob Landley <rob@landley.net>
parents: 322
diff changeset
156 close(2);
dd0b130edf6d Shell $(netcat) won't return unless child's stdin/stdout/stderr are closed.
Rob Landley <rob@landley.net>
parents: 322
diff changeset
157 }
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
158
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
159 for (;;) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
160 pid_t child = 0;
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
161
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
162 // For -l, call accept from the _new_ thread.
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
163
322
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
164 pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address,
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
165 &len);
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
166 if (pollfds[0].fd<0) perror_exit("accept");
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
167
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
168 // Do we need a tty?
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
169
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
170 if (toys.optflags&FLAG_t)
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
171 child = forkpty(&(pollfds[1].fd), NULL, NULL, NULL);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
172
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
173 // Do we need to fork and/or redirect for exec?
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
174
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
175 else {
322
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
176 if (toys.optflags&FLAG_L) child = fork();
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
177 if (!child && toys.optc) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
178 int fd = pollfds[0].fd;
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
179
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
180 if (!temp) close(sockfd);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
181 dup2(fd, 0);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
182 dup2(fd, 1);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
183 dup2(fd, 2);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
184 if (fd>2) close(fd);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
185 }
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
186 }
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
187
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
188 if (child<0) error_msg("Fork failed\n");
322
fa0a95ae4303 Second attempt at making netcat -l work properly.
Rob Landley <rob@landley.net>
parents: 321
diff changeset
189 if (child<1) break;
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
190 close(pollfds[0].fd);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
191 }
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
192 }
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
194
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 // We have a connection. Disarm timeout.
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
196 // (Does not play well with -L, but what _should_ that do?)
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
197 set_alarm(0);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
198
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
199 if (CFG_NETCAT_LISTEN && (toys.optflags&(FLAG_L|FLAG_l) && toys.optc)) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
200 execvp(*toys.optargs, toys.optargs);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
201 error_exit("Exec failed");
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
203
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 // Poll loop copying stdin->socket and socket->stdout.
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 for (;;) {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 int i;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
207
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 if (0>poll(pollfds, pollcount, -1)) perror_exit("poll");
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
209
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 for (i=0; i<pollcount; i++) {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 if (pollfds[i].revents & POLLIN) {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 int len = read(pollfds[i].fd, toybuf, sizeof(toybuf));
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 if (len<1) goto dohupnow;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 xwrite(i ? pollfds[0].fd : 1, toybuf, len);
324
50c8afc263ee More than 4k of input can be queued up in a pipe, so loop needs to continue reading until POLLIN is exhausted before acknowledging POLLHUP.
Rob Landley <rob@landley.net>
parents: 323
diff changeset
215 } else if (pollfds[i].revents & POLLHUP) {
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 dohupnow:
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
217 // Close half-connection. This is needed for things like
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
218 // "echo GET / | netcat landley.net 80"
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 if (i) {
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 shutdown(pollfds[0].fd, SHUT_WR);
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 pollcount--;
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
222 set_alarm(TT.quit_delay);
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 } else goto cleanup;
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 }
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 cleanup:
321
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
228 if (CFG_TOYBOX_FREE) {
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
229 close(pollfds[0].fd);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
230 close(sockfd);
dfbfbaeb69c9 Add netcat server mode, -l, -L, and -t.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
231 }
189
22d22427dec6 Add first pass at netcat. Base applet, -f, and -w implemented.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 }