1 From: Aron Xu <aron@debian.org>
2 Date: Mon, 13 Feb 2012 15:59:31 +0800
3 Subject: [PATCH] port to linux with libsd
6 Makefile | 17 +++++++++-
8 netcat.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------
9 socks.c | 46 +++++++++++++-------------
10 4 files changed, 130 insertions(+), 40 deletions(-)
15 -# $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
16 +# $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
19 SRCS= netcat.c atomicio.c socks.c
21 -.include <bsd.prog.mk>
22 +LIBS= `pkg-config --libs libbsd` -lresolv
25 +LDFLAGS= -Wl,--no-add-needed
29 + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o nc
32 + $(CC) $(CFLAGS) -c $< -o $@
39 Specifies the source port
41 should use, subject to privilege restrictions and availability.
42 -It is an error to use this option in conjunction with the
46 Specifies that source and/or destination ports should be chosen randomly
47 instead of sequentially within a range or in the order that the system
59 #include <netinet/ip.h>
60 #include <arpa/telnet.h>
62 +#ifndef IPTOS_LOWDELAY
63 +# define IPTOS_LOWDELAY 0x10
64 +# define IPTOS_THROUGHPUT 0x08
65 +# define IPTOS_RELIABILITY 0x04
66 +# define IPTOS_LOWCOST 0x02
67 +# define IPTOS_MINCOST IPTOS_LOWCOST
68 +#endif /* IPTOS_LOWDELAY */
70 +# ifndef IPTOS_DSCP_AF11
71 +# define IPTOS_DSCP_AF11 0x28
72 +# define IPTOS_DSCP_AF12 0x30
73 +# define IPTOS_DSCP_AF13 0x38
74 +# define IPTOS_DSCP_AF21 0x48
75 +# define IPTOS_DSCP_AF22 0x50
76 +# define IPTOS_DSCP_AF23 0x58
77 +# define IPTOS_DSCP_AF31 0x68
78 +# define IPTOS_DSCP_AF32 0x70
79 +# define IPTOS_DSCP_AF33 0x78
80 +# define IPTOS_DSCP_AF41 0x88
81 +# define IPTOS_DSCP_AF42 0x90
82 +# define IPTOS_DSCP_AF43 0x98
83 +# define IPTOS_DSCP_EF 0xb8
84 +#endif /* IPTOS_DSCP_AF11 */
86 +#ifndef IPTOS_DSCP_CS0
87 +# define IPTOS_DSCP_CS0 0x00
88 +# define IPTOS_DSCP_CS1 0x20
89 +# define IPTOS_DSCP_CS2 0x40
90 +# define IPTOS_DSCP_CS3 0x60
91 +# define IPTOS_DSCP_CS4 0x80
92 +# define IPTOS_DSCP_CS5 0xa0
93 +# define IPTOS_DSCP_CS6 0xc0
94 +# define IPTOS_DSCP_CS7 0xe0
95 +#endif /* IPTOS_DSCP_CS0 */
97 +#ifndef IPTOS_DSCP_EF
98 +# define IPTOS_DSCP_EF 0xb8
99 +#endif /* IPTOS_DSCP_EF */
111 #include "atomicio.h"
117 struct sockaddr_storage cliaddr;
119 + char *proxy = NULL;
120 const char *errstr, *proxyhost = "", *proxyport = NULL;
121 struct addrinfo proxyhints;
122 char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
124 errx(1, "interval %s: %s", errstr, optarg);
127 +# if defined(SO_JUMBO)
130 + errx(1, "no jumbo frame support available");
135 @@ -194,10 +240,14 @@
139 +# if defined(RT_TABLEID_MAX)
140 rtableid = (unsigned int)strtonum(optarg, 0,
141 RT_TABLEID_MAX, &errstr);
143 errx(1, "rtable %s: %s", errstr, optarg);
145 + errx(1, "no alternate routing table support available");
154 +# if defined(TCP_MD5SIG)
157 + errx(1, "no TCP MD5 signature support available");
163 if (argv[0] && !argv[1] && family == AF_UNIX) {
166 + } else if (!argv[0] && lflag) {
168 + errx(1, "cannot use -s and -l");
170 + errx(1, "cannot use -z and -l");
173 + } else if (!lflag && kflag) {
174 + errx(1, "cannot use -k without -l");
175 } else if (argv[0] && !argv[1]) {
182 - if (lflag && sflag)
183 - errx(1, "cannot use -s and -l");
184 - if (lflag && pflag)
185 - errx(1, "cannot use -p and -l");
186 - if (lflag && zflag)
187 - errx(1, "cannot use -z and -l");
188 - if (!lflag && kflag)
189 - errx(1, "must use -l with -k");
192 /* Get name of temporary socket for unix datagram client */
193 if ((family == AF_UNIX) && uflag && !lflag) {
196 strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
197 UNIX_DG_TMP_SOCKET_SIZE);
198 - if (mktemp(unix_dg_tmp_socket_buf) == NULL)
200 + if (mkstemp(unix_dg_tmp_socket_buf) == -1)
202 unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
205 @@ -563,18 +619,22 @@
206 res0->ai_protocol)) < 0)
209 +# if defined(RT_TABLEID_MAX)
211 if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid,
212 sizeof(rtableid)) == -1)
213 err(1, "setsockopt SO_RTABLE");
217 /* Bind to a local port or source address if specified. */
218 if (sflag || pflag) {
219 struct addrinfo ahints, *ares;
221 +# if defined (SO_BINDANY)
222 /* try SO_BINDANY, but don't insist */
223 setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
225 memset(&ahints, 0, sizeof(struct addrinfo));
226 ahints.ai_family = res0->ai_family;
227 ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
228 @@ -674,15 +734,23 @@
229 res0->ai_protocol)) < 0)
232 +# if defined(RT_TABLEID_MAX)
234 if (setsockopt(s, IPPROTO_IP, SO_RTABLE, &rtableid,
235 sizeof(rtableid)) == -1)
236 err(1, "setsockopt SO_RTABLE");
240 + ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
244 +# if defined(SO_REUSEPORT)
245 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
250 set_common_sockopts(s);
252 @@ -886,21 +954,25 @@
256 +# if defined(TCP_MD5SIG)
258 if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
259 &x, sizeof(x)) == -1)
264 if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
265 &x, sizeof(x)) == -1)
268 +# if defined(SO_JUMBO)
270 if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
271 &x, sizeof(x)) == -1)
276 if (setsockopt(s, IPPROTO_IP, IP_TOS,
277 &Tflag, sizeof(Tflag)) == -1)
279 { "cs7", IPTOS_DSCP_CS7 },
280 { "ef", IPTOS_DSCP_EF },
281 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
282 + { "lowcost", IPTOS_LOWCOST },
283 { "lowdelay", IPTOS_LOWDELAY },
284 { "netcontrol", IPTOS_PREC_NETCONTROL },
285 { "reliability", IPTOS_RELIABILITY },
290 +# if defined(DEBIAN_VERSION)
291 + fprintf(stderr, "OpenBSD netcat (Debian patchlevel " DEBIAN_VERSION ")\n");
294 fprintf(stderr, "\tCommand Summary:\n\
297 \t-h This help text\n\
298 \t-I length TCP receive buffer length\n\
299 \t-i secs\t Delay interval for lines sent, ports scanned\n\
300 + \t-j Use jumbo frame\n\
301 \t-k Keep inbound sockets open for multiple connects\n\
302 \t-l Listen mode, for inbound connects\n\
303 \t-n Suppress name/port resolutions\n\
304 @@ -998,15 +1075,15 @@
305 \t-x addr[:port]\tSpecify proxy address and port\n\
306 \t-z Zero-I/O mode [used for scanning]\n\
307 Port numbers can be individual or ranges: lo-hi [inclusive]\n");
316 - "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n"
317 - "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
318 + "usage: nc [-46DdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n"
319 + "\t [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n"
320 "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n"
321 "\t [-x proxy_address[:port]] [destination] [port]\n");
329 -#include <readpassphrase.h>
330 +#include <bsd/readpassphrase.h>
331 #include "atomicio.h"
333 #define SOCKS_PORT "1080"
334 @@ -167,11 +167,11 @@
335 buf[2] = SOCKS_NOAUTH;
336 cnt = atomicio(vwrite, proxyfd, buf, 3);
338 - err(1, "write failed (%zu/3)", cnt);
339 + err(1, "write failed (%zu/3)", (size_t)cnt);
341 cnt = atomicio(read, proxyfd, buf, 2);
343 - err(1, "read failed (%zu/3)", cnt);
344 + err(1, "read failed (%zu/3)", (size_t)cnt);
346 if (buf[1] == SOCKS_NOMETHOD)
347 errx(1, "authentication method negotiation failed");
348 @@ -220,23 +220,23 @@
350 cnt = atomicio(vwrite, proxyfd, buf, wlen);
352 - err(1, "write failed (%zu/%zu)", cnt, wlen);
353 + err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
355 cnt = atomicio(read, proxyfd, buf, 4);
357 - err(1, "read failed (%zu/4)", cnt);
358 + err(1, "read failed (%zu/4)", (size_t)cnt);
360 errx(1, "connection failed, SOCKS error %d", buf[1]);
363 cnt = atomicio(read, proxyfd, buf + 4, 6);
365 - err(1, "read failed (%d/6)", cnt);
366 + err(1, "read failed (%lu/6)", (unsigned long)cnt);
369 cnt = atomicio(read, proxyfd, buf + 4, 18);
371 - err(1, "read failed (%d/18)", cnt);
372 + err(1, "read failed (%lu/18)", (unsigned long)cnt);
375 errx(1, "connection failed, unsupported address type");
376 @@ -256,11 +256,11 @@
378 cnt = atomicio(vwrite, proxyfd, buf, wlen);
380 - err(1, "write failed (%zu/%zu)", cnt, wlen);
381 + err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
383 cnt = atomicio(read, proxyfd, buf, 8);
385 - err(1, "read failed (%zu/8)", cnt);
386 + err(1, "read failed (%zu/8)", (size_t)cnt);
388 errx(1, "connection failed, SOCKS error %d", buf[1]);
389 } else if (socksv == -1) {
390 @@ -272,39 +272,39 @@
392 /* Try to be sane about numeric IPv6 addresses */
393 if (strchr(host, ':') != NULL) {
394 - r = snprintf(buf, sizeof(buf),
395 + r = snprintf((char*)buf, sizeof(buf),
396 "CONNECT [%s]:%d HTTP/1.0\r\n",
397 host, ntohs(serverport));
399 - r = snprintf(buf, sizeof(buf),
400 + r = snprintf((char*)buf, sizeof(buf),
401 "CONNECT %s:%d HTTP/1.0\r\n",
402 host, ntohs(serverport));
404 if (r == -1 || (size_t)r >= sizeof(buf))
405 errx(1, "hostname too long");
407 + r = strlen((char*)buf);
409 cnt = atomicio(vwrite, proxyfd, buf, r);
411 - err(1, "write failed (%zu/%d)", cnt, r);
412 + err(1, "write failed (%zu/%d)", (size_t)cnt, (int)r);
417 proxypass = getproxypass(proxyuser, proxyhost);
418 - r = snprintf(buf, sizeof(buf), "%s:%s",
419 + r = snprintf((char*)buf, sizeof(buf), "%s:%s",
420 proxyuser, proxypass);
421 if (r == -1 || (size_t)r >= sizeof(buf) ||
422 - b64_ntop(buf, strlen(buf), resp,
423 + b64_ntop(buf, strlen((char*)buf), resp,
425 errx(1, "Proxy username/password too long");
426 - r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
427 + r = snprintf((char*)buf, sizeof((char*)buf), "Proxy-Authorization: "
428 "Basic %s\r\n", resp);
429 if (r == -1 || (size_t)r >= sizeof(buf))
430 errx(1, "Proxy auth response too long");
432 + r = strlen((char*)buf);
433 if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
434 - err(1, "write failed (%zu/%d)", cnt, r);
435 + err(1, "write failed (%zu/%d)", (size_t)cnt, r);
438 /* Terminate headers */
439 @@ -312,22 +312,22 @@
440 err(1, "write failed (2/%d)", r);
442 /* Read status reply */
443 - proxy_read_line(proxyfd, buf, sizeof(buf));
444 + proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
445 if (proxyuser != NULL &&
446 - strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
447 + strncmp((char*)buf, "HTTP/1.0 407 ", 12) == 0) {
449 fprintf(stderr, "Proxy authentication "
454 - } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
455 - strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
456 + } else if (strncmp((char*)buf, "HTTP/1.0 200 ", 12) != 0 &&
457 + strncmp((char*)buf, "HTTP/1.1 200 ", 12) != 0)
458 errx(1, "Proxy error: \"%s\"", buf);
460 /* Headers continue until we hit an empty line */
461 for (r = 0; r < HTTP_MAXHDRS; r++) {
462 - proxy_read_line(proxyfd, buf, sizeof(buf));
463 + proxy_read_line(proxyfd, (char*)buf, sizeof(buf));