1From 36bb1d71b1e6963feb61fdf13c3e2f1cd55b0f5c Mon Sep 17 00:00:00 2001
2From: Michael Forney <mforney@mforney.org>
3Date: Mon, 2 Dec 2019 21:11:04 -0800
4Subject: [PATCH] nc: Portability fixes from libressl-portable
5
6---
7 usr.bin/nc/netcat.c | 59 +++++++++++++++++++++++++++++++++++++++------
8 1 file changed, 52 insertions(+), 7 deletions(-)
9
10diff --git a/usr.bin/nc/netcat.c b/usr.bin/nc/netcat.c
11index 9361ff50cd9..4fd399a6f89 100644
12--- a/usr.bin/nc/netcat.c
13+++ b/usr.bin/nc/netcat.c
14@@ -73,6 +73,10 @@
15 #define TLS_CCERT (1 << 3)
16 #define TLS_MUSTSTAPLE (1 << 4)
17
18+#ifndef IPTOS_DSCP_VA
19+#define IPTOS_DSCP_VA 0xb0
20+#endif
21+
22 /* Command Line Options */
23 int dflag; /* detached, no stdin */
24 int Fflag; /* fdpass sock to stdout */
25@@ -93,9 +97,13 @@ int zflag; /* Port Scan Flag */
26 int Dflag; /* sodebug */
27 int Iflag; /* TCP receive buffer size */
28 int Oflag; /* TCP send buffer size */
29+#ifdef TCP_MD5SIG
30 int Sflag; /* TCP MD5 signature option */
31+#endif
32 int Tflag = -1; /* IP Type of Service */
33+#ifdef SO_RTABLE
34 int rtableid = -1;
35+#endif
36
37 int usetls; /* use TLS */
38 const char *Cflag; /* Public cert file */
39@@ -271,12 +279,14 @@ main(int argc, char *argv[])
40 case 'u':
41 uflag = 1;
42 break;
43+#ifdef SO_RTABLE
44 case 'V':
45 rtableid = (int)strtonum(optarg, 0,
46 RT_TABLEID_MAX, &errstr);
47 if (errstr)
48 errx(1, "rtable %s: %s", errstr, optarg);
49 break;
50+#endif
51 case 'v':
52 vflag = 1;
53 break;
54@@ -323,9 +333,11 @@ main(int argc, char *argv[])
55 case 'o':
56 oflag = optarg;
57 break;
58+#ifdef TCP_MD5SIG
59 case 'S':
60 Sflag = 1;
61 break;
62+#endif
63 case 'T':
64 errstr = NULL;
65 errno = 0;
66@@ -349,9 +361,11 @@ main(int argc, char *argv[])
67 argc -= optind;
68 argv += optind;
69
70+#ifdef SO_RTABLE
71 if (rtableid >= 0)
72 if (setrtable(rtableid) == -1)
73 err(1, "setrtable");
74+#endif
75
76 /* Cruft to make sure options are clean, and used properly. */
77 if (argc == 1 && family == AF_UNIX) {
78@@ -930,7 +944,10 @@ remote_connect(const char *host, const char *port, struct addrinfo hints,
79 char *ipaddr)
80 {
81 struct addrinfo *res, *res0;
82- int s = -1, error, herr, on = 1, save_errno;
83+ int s = -1, error, herr, save_errno;
84+#ifdef SO_BINDANY
85+ int on = 1;
86+#endif
87
88 if ((error = getaddrinfo(host, port, &hints, &res0)))
89 errx(1, "getaddrinfo for host \"%s\" port %s: %s", host,
90@@ -945,8 +962,10 @@ remote_connect(const char *host, const char *port, struct addrinfo hints,
91 if (sflag || pflag) {
92 struct addrinfo ahints, *ares;
93
94+#ifdef SO_BINDANY
95 /* try SO_BINDANY, but don't insist */
96 setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
97+#endif
98 memset(&ahints, 0, sizeof(struct addrinfo));
99 ahints.ai_family = res->ai_family;
100 ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
101@@ -1038,8 +1057,11 @@ int
102 local_listen(const char *host, const char *port, struct addrinfo hints)
103 {
104 struct addrinfo *res, *res0;
105- int s = -1, ret, x = 1, save_errno;
106+ int s = -1, save_errno;
107 int error;
108+#ifdef SO_REUSEPORT
109+ int ret, x = 1;
110+#endif
111
112 /* Allow nodename to be null. */
113 hints.ai_flags |= AI_PASSIVE;
114@@ -1059,9 +1081,11 @@ local_listen(const char *host, const char *port, struct addrinfo hints)
115 res->ai_protocol)) == -1)
116 continue;
117
118+#ifdef SO_REUSEPORT
119 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
120 if (ret == -1)
121 err(1, NULL);
122+#endif
123
124 set_common_sockopts(s, res->ai_family);
125
126@@ -1571,11 +1595,13 @@ set_common_sockopts(int s, int af)
127 {
128 int x = 1;
129
130+#ifdef TCP_MD5SIG
131 if (Sflag) {
132 if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
133 &x, sizeof(x)) == -1)
134 err(1, NULL);
135 }
136+#endif
137 if (Dflag) {
138 if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
139 &x, sizeof(x)) == -1)
140@@ -1586,9 +1612,16 @@ set_common_sockopts(int s, int af)
141 IP_TOS, &Tflag, sizeof(Tflag)) == -1)
142 err(1, "set IP ToS");
143
144+#ifdef IPV6_TCLASS
145 else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
146 IPV6_TCLASS, &Tflag, sizeof(Tflag)) == -1)
147 err(1, "set IPv6 traffic class");
148+#else
149+ else if (af == AF_INET6) {
150+ errno = ENOPROTOOPT;
151+ err(1, "set IPv6 traffic class not supported");
152+ }
153+#endif
154 }
155 if (Iflag) {
156 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
157@@ -1612,13 +1645,17 @@ set_common_sockopts(int s, int af)
158 }
159
160 if (minttl != -1) {
161+#ifdef IP_MINTTL
162 if (af == AF_INET && setsockopt(s, IPPROTO_IP,
163 IP_MINTTL, &minttl, sizeof(minttl)))
164 err(1, "set IP min TTL");
165+#endif
166
167- else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
168+#ifdef IPV6_MINHOPCOUNT
169+ if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
170 IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)))
171 err(1, "set IPv6 min hop count");
172+#endif
173 }
174 }
175
176@@ -1849,14 +1886,22 @@ help(void)
177 \t-P proxyuser\tUsername for proxy authentication\n\
178 \t-p port\t Specify local port for remote connects\n\
179 \t-R CAfile CA bundle\n\
180- \t-r Randomize remote ports\n\
181- \t-S Enable the TCP MD5 signature option\n\
182+ \t-r Randomize remote ports\n"
183+#ifdef TCP_MD5SIG
184+ "\
185+ \t-S Enable the TCP MD5 signature option\n"
186+#endif
187+ "\
188 \t-s sourceaddr Local source address\n\
189 \t-T keyword TOS value or TLS options\n\
190 \t-t Answer TELNET negotiation\n\
191 \t-U Use UNIX domain socket\n\
192- \t-u UDP mode\n\
193- \t-V rtable Specify alternate routing table\n\
194+ \t-u UDP mode\n"
195+#ifdef SO_RTABLE
196+ "\
197+ \t-V rtable Specify alternate routing table\n"
198+#endif
199+ "\
200 \t-v Verbose\n\
201 \t-W recvlimit Terminate after receiving a number of packets\n\
202 \t-w timeout Timeout for connects and final net reads\n\
203--
2042.54.0
205