@@ -0,0 +1,421 @@
1+--- a/nc.1
2++++ b/nc.1
3+@@ -204,9 +204,6 @@ Proxy authentication is only supported f
4+ Specifies the source port
5+ .Nm
6+ should use, subject to privilege restrictions and availability.
7+-It is an error to use this option in conjunction with the
8+-.Fl l
9+-option.
10+ .It Fl R Ar CAfile
11+ Specifies the filename from which the root CA bundle for certificate
12+ verification is loaded, in PEM format.
13+@@ -258,6 +255,7 @@ For IPv4 TOS value
14+ may be one of
15+ .Ar critical ,
16+ .Ar inetcontrol ,
17++.Ar lowcost ,
18+ .Ar lowdelay ,
19+ .Ar netcontrol ,
20+ .Ar throughput ,
21+--- a/netcat.c
22++++ b/netcat.c
23+@@ -32,6 +32,8 @@
24+ * *Hobbit* <hobbit@avian.org>.
25+ */
26+
27++#define _GNU_SOURCE
28++
29+ #include <sys/types.h>
30+ #include <sys/socket.h>
31+ #include <sys/uio.h>
32+@@ -41,6 +43,49 @@
33+ #include <netinet/tcp.h>
34+ #include <netinet/ip.h>
35+ #include <arpa/telnet.h>
36++#ifdef __linux__
37++# include <linux/in6.h>
38++#endif
39++
40++#ifndef IPTOS_LOWDELAY
41++# define IPTOS_LOWDELAY 0x10
42++# define IPTOS_THROUGHPUT 0x08
43++# define IPTOS_RELIABILITY 0x04
44++# define IPTOS_LOWCOST 0x02
45++# define IPTOS_MINCOST IPTOS_LOWCOST
46++#endif /* IPTOS_LOWDELAY */
47++
48++# ifndef IPTOS_DSCP_AF11
49++# define IPTOS_DSCP_AF11 0x28
50++# define IPTOS_DSCP_AF12 0x30
51++# define IPTOS_DSCP_AF13 0x38
52++# define IPTOS_DSCP_AF21 0x48
53++# define IPTOS_DSCP_AF22 0x50
54++# define IPTOS_DSCP_AF23 0x58
55++# define IPTOS_DSCP_AF31 0x68
56++# define IPTOS_DSCP_AF32 0x70
57++# define IPTOS_DSCP_AF33 0x78
58++# define IPTOS_DSCP_AF41 0x88
59++# define IPTOS_DSCP_AF42 0x90
60++# define IPTOS_DSCP_AF43 0x98
61++# define IPTOS_DSCP_EF 0xb8
62++#endif /* IPTOS_DSCP_AF11 */
63++
64++#ifndef IPTOS_DSCP_CS0
65++# define IPTOS_DSCP_CS0 0x00
66++# define IPTOS_DSCP_CS1 0x20
67++# define IPTOS_DSCP_CS2 0x40
68++# define IPTOS_DSCP_CS3 0x60
69++# define IPTOS_DSCP_CS4 0x80
70++# define IPTOS_DSCP_CS5 0xa0
71++# define IPTOS_DSCP_CS6 0xc0
72++# define IPTOS_DSCP_CS7 0xe0
73++#endif /* IPTOS_DSCP_CS0 */
74++
75++#ifndef IPTOS_DSCP_EF
76++# define IPTOS_DSCP_EF 0xb8
77++#endif /* IPTOS_DSCP_EF */
78++
79+
80+ #include <err.h>
81+ #include <errno.h>
82+@@ -268,10 +315,14 @@ main(int argc, char *argv[])
83+ uflag = 1;
84+ break;
85+ case 'V':
86++# if defined(RT_TABLEID_MAX)
87+ rtableid = (int)strtonum(optarg, 0,
88+ RT_TABLEID_MAX, &errstr);
89+ if (errstr)
90+ errx(1, "rtable %s: %s", errstr, optarg);
91++# else
92++ errx(1, "no alternate routing table support available");
93++# endif
94+ break;
95+ case 'v':
96+ vflag = 1;
97+@@ -320,7 +371,11 @@ main(int argc, char *argv[])
98+ oflag = optarg;
99+ break;
100+ case 'S':
101++# if defined(TCP_MD5SIG)
102+ Sflag = 1;
103++# else
104++ errx(1, "no TCP MD5 signature support available");
105++# endif
106+ break;
107+ case 'T':
108+ errstr = NULL;
109+@@ -345,35 +400,23 @@ main(int argc, char *argv[])
110+ argc -= optind;
111+ argv += optind;
112+
113++# if defined(RT_TABLEID_MAX)
114+ if (rtableid >= 0)
115+ if (setrtable(rtableid) == -1)
116+ err(1, "setrtable");
117+-
118+- if (family == AF_UNIX) {
119+- if (pledge("stdio rpath wpath cpath tmppath unix", NULL) == -1)
120+- err(1, "pledge");
121+- } else if (Fflag && Pflag) {
122+- if (pledge("stdio inet dns sendfd tty", NULL) == -1)
123+- err(1, "pledge");
124+- } else if (Fflag) {
125+- if (pledge("stdio inet dns sendfd", NULL) == -1)
126+- err(1, "pledge");
127+- } else if (Pflag && usetls) {
128+- if (pledge("stdio rpath inet dns tty", NULL) == -1)
129+- err(1, "pledge");
130+- } else if (Pflag) {
131+- if (pledge("stdio inet dns tty", NULL) == -1)
132+- err(1, "pledge");
133+- } else if (usetls) {
134+- if (pledge("stdio rpath inet dns", NULL) == -1)
135+- err(1, "pledge");
136+- } else if (pledge("stdio inet dns", NULL) == -1)
137+- err(1, "pledge");
138++# endif
139+
140+ /* Cruft to make sure options are clean, and used properly. */
141+ if (argv[0] && !argv[1] && family == AF_UNIX) {
142+ host = argv[0];
143+ uport = NULL;
144++ } else if (!argv[0] && lflag) {
145++ if (sflag)
146++ errx(1, "cannot use -s and -l");
147++ if (pflag)
148++ errx(1, "cannot use -p and -l");
149++ if (zflag)
150++ errx(1, "cannot use -z and -l");
151+ } else if (argv[0] && !argv[1]) {
152+ if (!lflag)
153+ usage(1);
154+@@ -385,12 +428,6 @@ main(int argc, char *argv[])
155+ } else
156+ usage(1);
157+
158+- if (lflag && sflag)
159+- errx(1, "cannot use -s and -l");
160+- if (lflag && pflag)
161+- errx(1, "cannot use -p and -l");
162+- if (lflag && zflag)
163+- errx(1, "cannot use -z and -l");
164+ if (!lflag && kflag)
165+ errx(1, "must use -l with -k");
166+ if (uflag && usetls)
167+@@ -425,8 +462,8 @@ main(int argc, char *argv[])
168+ } else {
169+ strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
170+ UNIX_DG_TMP_SOCKET_SIZE);
171+- if (mktemp(unix_dg_tmp_socket_buf) == NULL)
172+- err(1, "mktemp");
173++ if (mkstemp(unix_dg_tmp_socket_buf) == -1)
174++ err(1, "mkstemp");
175+ unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
176+ }
177+ }
178+@@ -901,8 +938,10 @@ remote_connect(const char *host, const c
179+ if (sflag || pflag) {
180+ struct addrinfo ahints, *ares;
181+
182++# if defined (SO_BINDANY)
183+ /* try SO_BINDANY, but don't insist */
184+ setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
185++# endif
186+ memset(&ahints, 0, sizeof(struct addrinfo));
187+ ahints.ai_family = res->ai_family;
188+ ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
189+@@ -994,9 +1033,15 @@ local_listen(char *host, char *port, str
190+ res->ai_protocol)) < 0)
191+ continue;
192+
193++ ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
194++ if (ret == -1)
195++ err(1, NULL);
196++
197++# if defined(SO_REUSEPORT)
198+ ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
199+ if (ret == -1)
200+ err(1, NULL);
201++# endif
202+
203+ set_common_sockopts(s, res->ai_family);
204+
205+@@ -1452,11 +1497,13 @@ set_common_sockopts(int s, int af)
206+ {
207+ int x = 1;
208+
209++# if defined(TCP_MD5SIG)
210+ if (Sflag) {
211+ if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
212+ &x, sizeof(x)) == -1)
213+ err(1, NULL);
214+ }
215++# endif
216+ if (Dflag) {
217+ if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
218+ &x, sizeof(x)) == -1)
219+@@ -1467,9 +1514,14 @@ set_common_sockopts(int s, int af)
220+ IP_TOS, &Tflag, sizeof(Tflag)) == -1)
221+ err(1, "set IP ToS");
222+
223++#if defined(IPV6_TCLASS)
224+ else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
225+ IPV6_TCLASS, &Tflag, sizeof(Tflag)) == -1)
226+ err(1, "set IPv6 traffic class");
227++#else
228++ else if (af == AF_INET6)
229++ errx(1, "can't set IPv6 traffic class (unavailable)");
230++#endif
231+ }
232+ if (Iflag) {
233+ if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
234+@@ -1487,19 +1539,34 @@ set_common_sockopts(int s, int af)
235+ IP_TTL, &ttl, sizeof(ttl)))
236+ err(1, "set IP TTL");
237+
238++#if defined(IPV6_UNICAST_HOPS)
239+ else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
240+ IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)))
241+ err(1, "set IPv6 unicast hops");
242++#else
243++ else if (af == AF_INET6)
244++ errx(1, "can't set IPv6 unicast hops (unavailable)");
245++#endif
246+ }
247+
248+ if (minttl != -1) {
249++#if defined(IP_MINTTL)
250+ if (af == AF_INET && setsockopt(s, IPPROTO_IP,
251+ IP_MINTTL, &minttl, sizeof(minttl)))
252+ err(1, "set IP min TTL");
253++#else
254++ if (af == AF_INET)
255++ errx(1, "can't set IP min TTL (unavailable)");
256++#endif
257+
258++#if defined(IPV6_MINHOPCOUNT)
259+ else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
260+ IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)))
261+ err(1, "set IPv6 min hop count");
262++#else
263++ else if (af == AF_INET6)
264++ errx(1, "can't set IPv6 min hop count (unavailable)");
265++#endif
266+ }
267+ }
268+
269+@@ -1534,6 +1601,7 @@ process_tos_opt(char *s, int *val)
270+ { "cs7", IPTOS_DSCP_CS7 },
271+ { "ef", IPTOS_DSCP_EF },
272+ { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
273++ { "lowcost", IPTOS_LOWCOST },
274+ { "lowdelay", IPTOS_LOWDELAY },
275+ { "netcontrol", IPTOS_PREC_NETCONTROL },
276+ { "reliability", IPTOS_RELIABILITY },
277+@@ -1741,7 +1812,7 @@ help(void)
278+ \t-Z Peer certificate file\n\
279+ \t-z Zero-I/O mode [used for scanning]\n\
280+ Port numbers can be individual or ranges: lo-hi [inclusive]\n");
281+- exit(1);
282++ exit(0);
283+ }
284+
285+ void
286+--- a/socks.c
287++++ b/socks.c
288+@@ -219,11 +219,11 @@ socks_connect(const char *host, const ch
289+ buf[2] = SOCKS_NOAUTH;
290+ cnt = atomicio(vwrite, proxyfd, buf, 3);
291+ if (cnt != 3)
292+- err(1, "write failed (%zu/3)", cnt);
293++ err(1, "write failed (%zu/3)", (size_t)cnt);
294+
295+ cnt = atomicio(read, proxyfd, buf, 2);
296+ if (cnt != 2)
297+- err(1, "read failed (%zu/3)", cnt);
298++ err(1, "read failed (%zu/3)", (size_t)cnt);
299+
300+ if (buf[1] == SOCKS_NOMETHOD)
301+ errx(1, "authentication method negotiation failed");
302+@@ -272,11 +272,11 @@ socks_connect(const char *host, const ch
303+
304+ cnt = atomicio(vwrite, proxyfd, buf, wlen);
305+ if (cnt != wlen)
306+- err(1, "write failed (%zu/%zu)", cnt, wlen);
307++ err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
308+
309+ cnt = atomicio(read, proxyfd, buf, 4);
310+ if (cnt != 4)
311+- err(1, "read failed (%zu/4)", cnt);
312++ err(1, "read failed (%zu/4)", (size_t)cnt);
313+ if (buf[1] != 0) {
314+ errx(1, "connection failed, SOCKSv5 error: %s",
315+ socks5_strerror(buf[1]));
316+@@ -285,12 +285,12 @@ socks_connect(const char *host, const ch
317+ case SOCKS_IPV4:
318+ cnt = atomicio(read, proxyfd, buf + 4, 6);
319+ if (cnt != 6)
320+- err(1, "read failed (%zu/6)", cnt);
321++ err(1, "read failed (%zu/6)", (size_t)cnt);
322+ break;
323+ case SOCKS_IPV6:
324+ cnt = atomicio(read, proxyfd, buf + 4, 18);
325+ if (cnt != 18)
326+- err(1, "read failed (%zu/18)", cnt);
327++ err(1, "read failed (%zu/18)", (size_t)cnt);
328+ break;
329+ default:
330+ errx(1, "connection failed, unsupported address type");
331+@@ -310,11 +310,11 @@ socks_connect(const char *host, const ch
332+
333+ cnt = atomicio(vwrite, proxyfd, buf, wlen);
334+ if (cnt != wlen)
335+- err(1, "write failed (%zu/%zu)", cnt, wlen);
336++ err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
337+
338+ cnt = atomicio(read, proxyfd, buf, 8);
339+ if (cnt != 8)
340+- err(1, "read failed (%zu/8)", cnt);
341++ err(1, "read failed (%zu/8)", (size_t)cnt);
342+ if (buf[1] != 90) {
343+ errx(1, "connection failed, SOCKSv4 error: %s",
344+ socks4_strerror(buf[1]));
345+@@ -328,39 +328,39 @@ socks_connect(const char *host, const ch
346+
347+ /* Try to be sane about numeric IPv6 addresses */
348+ if (strchr(host, ':') != NULL) {
349+- r = snprintf(buf, sizeof(buf),
350++ r = snprintf((char*)buf, sizeof(buf),
351+ "CONNECT [%s]:%d HTTP/1.0\r\n",
352+ host, ntohs(serverport));
353+ } else {
354+- r = snprintf(buf, sizeof(buf),
355++ r = snprintf((char*)buf, sizeof(buf),
356+ "CONNECT %s:%d HTTP/1.0\r\n",
357+ host, ntohs(serverport));
358+ }
359+ if (r == -1 || (size_t)r >= sizeof(buf))
360+ errx(1, "hostname too long");
361+- r = strlen(buf);
362++ r = strlen((char*)buf);
363+
364+ cnt = atomicio(vwrite, proxyfd, buf, r);
365+ if (cnt != r)
366+- err(1, "write failed (%zu/%d)", cnt, r);
367++ err(1, "write failed (%zu/%d)", (size_t)cnt, (int)r);
368+
369+ if (authretry > 1) {
370+ char resp[1024];
371+
372+ proxypass = getproxypass(proxyuser, proxyhost);
373+- r = snprintf(buf, sizeof(buf), "%s:%s",
374++ r = snprintf((char*)buf, sizeof(buf), "%s:%s",
375+ proxyuser, proxypass);
376+ if (r == -1 || (size_t)r >= sizeof(buf) ||
377+- b64_ntop(buf, strlen(buf), resp,
378++ b64_ntop(buf, strlen((char*)buf), resp,
379+ sizeof(resp)) == -1)
380+ errx(1, "Proxy username/password too long");
381+- r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
382++ r = snprintf((char*)buf, sizeof(buf), "Proxy-Authorization: "
383+ "Basic %s\r\n", resp);
384+ if (r == -1 || (size_t)r >= sizeof(buf))
385+ errx(1, "Proxy auth response too long");
386+- r = strlen(buf);
387++ r = strlen((char*)buf);
388+ if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
389+- err(1, "write failed (%zu/%d)", cnt, r);
390++ err(1, "write failed (%zu/%d)", (size_t)cnt, r);
391+ }
392+
393+ /* Terminate headers */
394+@@ -368,22 +368,22 @@ socks_connect(const char *host, const ch
395+ err(1, "write failed (%zu/2)", cnt);
396+
397+ /* Read status reply */
398+- proxy_read_line(proxyfd, buf, sizeof(buf));
399++ proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
400+ if (proxyuser != NULL &&
401+- strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
402++ strncmp((char*)buf, "HTTP/1.0 407 ", 12) == 0) {
403+ if (authretry > 1) {
404+ fprintf(stderr, "Proxy authentication "
405+ "failed\n");
406+ }
407+ close(proxyfd);
408+ goto again;
409+- } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
410+- strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
411++ } else if (strncmp((char*)buf, "HTTP/1.0 200 ", 12) != 0 &&
412++ strncmp((char*)buf, "HTTP/1.1 200 ", 12) != 0)
413+ errx(1, "Proxy error: \"%s\"", buf);
414+
415+ /* Headers continue until we hit an empty line */
416+ for (r = 0; r < HTTP_MAXHDRS; r++) {
417+- proxy_read_line(proxyfd, buf, sizeof(buf));
418++ proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
419+ if (*buf == '\0')
420+ break;
421+ }