1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#define FOR_netcat
48#include "toys.h"
49
50GLOBALS(
51 char *f, *s;
52 long q, p, W, w;
53)
54
55static void timeout(int signum)
56{
57 if (TT.w) error_exit("Timeout");
58 xexit();
59}
60
61
62static int usock(char *name, int type, int out)
63{
64 int sockfd;
65 struct sockaddr_un sockaddr;
66
67 memset(&sockaddr, 0, sizeof(struct sockaddr_un));
68
69 if (strlen(name) + 1 > sizeof(sockaddr.sun_path))
70 error_exit("socket path too long %s", name);
71 strcpy(sockaddr.sun_path, name);
72 sockaddr.sun_family = AF_UNIX;
73
74 sockfd = xsocket(AF_UNIX, type, 0);
75 (out?xconnect:xbind)(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
76
77 return sockfd;
78}
79
80void netcat_main(void)
81{
82 int sockfd = -1, in1 = 0, in2 = 0, out1 = 1, out2 = 1, family = AF_UNSPEC,
83 ll = FLAG(L)|FLAG(l), type = FLAG(u) ? SOCK_DGRAM : SOCK_STREAM;
84 pid_t child;
85
86
87 TT.W = TT.W ? TT.W*1000 : -1;
88 TT.q = TT.q ? TT.q*1000 : -1;
89
90 xsignal(SIGCHLD, SIG_IGN);
91 if (TT.w) {
92 xsignal(SIGALRM, timeout);
93 alarm(TT.w);
94 }
95
96
97
98 if (FLAG(f) ? toys.optc : (!ll && toys.optc!=(FLAG(U)?1:2)))
99 help_exit("bad argument count");
100
101 if (FLAG(4)) family = AF_INET;
102 else if (FLAG(6)) family = AF_INET6;
103 else if (FLAG(U)) family = AF_UNIX;
104
105 if (TT.f) in1 = out2 = xopen(TT.f, O_RDWR);
106 else {
107
108 if (!ll) {
109 if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1);
110 else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0], toys.optargs[1],
111 family, type, 0, 0));
112
113
114 alarm(0);
115 in1 = out2 = sockfd;
116 pollinate(in1, in2, out1, out2, TT.W, TT.q);
117 } else {
118
119 if (FLAG(U)) {
120 if (!FLAG(s)) error_exit("-s must be provided if using -U with -L/-l");
121 sockfd = usock(TT.s, type, 0);
122 } else {
123 sprintf(toybuf, "%ld", TT.p);
124 sockfd = xbindany(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0));
125 }
126
127 if (listen(sockfd, 5)) error_exit("listen");
128 if (!TT.p && !FLAG(U)) {
129 struct sockaddr* address = (void*)toybuf;
130 socklen_t len = sizeof(struct sockaddr_storage);
131 short port_be;
132
133 getsockname(sockfd, address, &len);
134 if (address->sa_family == AF_INET)
135 port_be = ((struct sockaddr_in*)address)->sin_port;
136 else if (address->sa_family == AF_INET6)
137 port_be = ((struct sockaddr_in6*)address)->sin6_port;
138 else perror_exit("getsockname: bad family");
139
140 dprintf(1, "%d\n", SWAP_BE16(port_be));
141
142
143 if (CFG_TOYBOX_FORK && toys.optc && xfork()) goto cleanup;
144 }
145
146 do {
147 child = 0;
148 in1 = out2 = accept(sockfd, 0, 0);
149 if (in1<0) perror_exit("accept");
150
151
152 alarm(0);
153
154 if (toys.optc) {
155
156
157
158
159
160
161
162
163
164
165
166 if (FLAG(L)) NOEXIT(child = XVFORK());
167 if (child) {
168 close(in1);
169 continue;
170 }
171 close(sockfd);
172 dup2(in1, 0);
173 dup2(in1, 1);
174 if (FLAG(E)) dup2(in1, 2);
175 if (in1>2) close(in1);
176 xexec(toys.optargs);
177 }
178
179 pollinate(in1, in2, out1, out2, TT.W, TT.q);
180 close(in1);
181 } while (!FLAG(l));
182 }
183 }
184
185cleanup:
186 if (CFG_TOYBOX_FREE) {
187 close(in1);
188 close(sockfd);
189 }
190}
191