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