source: npl/overig/netcat_openbsd/patches/0001-port-to-linux-with-libsd.patch

Last change on this file was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 13.6 KB
RevLine 
[c5c522c]1From: Aron Xu <aron@debian.org>
2Date: Mon, 13 Feb 2012 15:59:31 +0800
3Subject: port to linux with libsd
4
5---
6 Makefile |   17 ++++++++--
7 nc.1     |    4 +--
8 netcat.c |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------
9 socks.c  |   46 +++++++++++++--------------
10 4 files changed, 130 insertions(+), 42 deletions(-)
11
12diff --git a/Makefile b/Makefile
13index 150f829..96a6587 100644
14--- a/Makefile
15+++ b/Makefile
16@@ -1,6 +1,19 @@
17-#      $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
18+#       $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
19 
20 PROG=  nc
21 SRCS=  netcat.c atomicio.c socks.c
22 
23-.include <bsd.prog.mk>
24+LIBS=  `pkg-config --libs libbsd` -lresolv
25+OBJS=  $(SRCS:.c=.o)
26+CFLAGS=  -g -O2
27+LDFLAGS=  -Wl,--no-add-needed
28+
29+all: nc
30+nc: $(OBJS)
31+       $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o nc
32+
33+$(OBJS): %.o: %.c
34+       $(CC) $(CFLAGS) -c $< -o $@
35+
36+clean:
37+       rm -f $(OBJS) nc
38diff --git a/nc.1 b/nc.1
39index 75d1437..b7014a2 100644
40--- a/nc.1
41+++ b/nc.1
42@@ -146,9 +146,6 @@ Proxy authentication is only supported for HTTP CONNECT proxies at present.
43 Specifies the source port
44 .Nm
45 should use, subject to privilege restrictions and availability.
46-It is an error to use this option in conjunction with the
47-.Fl l
48-option.
49 .It Fl r
50 Specifies that source and/or destination ports should be chosen randomly
51 instead of sequentially within a range or in the order that the system
52@@ -170,6 +167,7 @@ Change IPv4 TOS value.
53 may be one of
54 .Ar critical ,
55 .Ar inetcontrol ,
56+.Ar lowcost ,
57 .Ar lowdelay ,
58 .Ar netcontrol ,
59 .Ar throughput ,
60diff --git a/netcat.c b/netcat.c
61index cc4683a..9b2def2 100644
62--- a/netcat.c
63+++ b/netcat.c
64@@ -42,6 +42,46 @@
65 #include <netinet/ip.h>
66 #include <arpa/telnet.h>
67 
68+#ifndef IPTOS_LOWDELAY
69+# define IPTOS_LOWDELAY 0x10
70+# define IPTOS_THROUGHPUT 0x08
71+# define IPTOS_RELIABILITY 0x04
72+# define IPTOS_LOWCOST 0x02
73+# define IPTOS_MINCOST IPTOS_LOWCOST
74+#endif /* IPTOS_LOWDELAY */
75+
76+# ifndef IPTOS_DSCP_AF11
77+# define       IPTOS_DSCP_AF11         0x28
78+# define       IPTOS_DSCP_AF12         0x30
79+# define       IPTOS_DSCP_AF13         0x38
80+# define       IPTOS_DSCP_AF21         0x48
81+# define       IPTOS_DSCP_AF22         0x50
82+# define       IPTOS_DSCP_AF23         0x58
83+# define       IPTOS_DSCP_AF31         0x68
84+# define       IPTOS_DSCP_AF32         0x70
85+# define       IPTOS_DSCP_AF33         0x78
86+# define       IPTOS_DSCP_AF41         0x88
87+# define       IPTOS_DSCP_AF42         0x90
88+# define       IPTOS_DSCP_AF43         0x98
89+# define       IPTOS_DSCP_EF           0xb8
90+#endif /* IPTOS_DSCP_AF11 */
91+
92+#ifndef IPTOS_DSCP_CS0
93+# define       IPTOS_DSCP_CS0          0x00
94+# define       IPTOS_DSCP_CS1          0x20
95+# define       IPTOS_DSCP_CS2          0x40
96+# define       IPTOS_DSCP_CS3          0x60
97+# define       IPTOS_DSCP_CS4          0x80
98+# define       IPTOS_DSCP_CS5          0xa0
99+# define       IPTOS_DSCP_CS6          0xc0
100+# define       IPTOS_DSCP_CS7          0xe0
101+#endif /* IPTOS_DSCP_CS0 */
102+
103+#ifndef IPTOS_DSCP_EF
104+# define       IPTOS_DSCP_EF           0xb8
105+#endif /* IPTOS_DSCP_EF */
106+
107+
108 #include <err.h>
109 #include <errno.h>
110 #include <netdb.h>
111@@ -53,6 +93,8 @@
112 #include <unistd.h>
113 #include <fcntl.h>
114 #include <limits.h>
115+#include <bsd/stdlib.h>
116+#include <bsd/string.h>
117 #include "atomicio.h"
118 
119 #ifndef SUN_LEN
120@@ -118,7 +160,7 @@ main(int argc, char *argv[])
121        struct servent *sv;
122        socklen_t len;
123        struct sockaddr_storage cliaddr;
124-       char *proxy;
125+       char *proxy = NULL;
126        const char *errstr, *proxyhost = "", *proxyport = NULL;
127        struct addrinfo proxyhints;
128        char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
129@@ -164,7 +206,11 @@ main(int argc, char *argv[])
130                                errx(1, "interval %s: %s", errstr, optarg);
131                        break;
132                case 'j':
133+# if defined(SO_JUMBO)
134                        jflag = 1;
135+# else
136+                       errx(1, "no jumbo frame support available");
137+# endif
138                        break;
139                case 'k':
140                        kflag = 1;
141@@ -194,10 +240,14 @@ main(int argc, char *argv[])
142                        uflag = 1;
143                        break;
144                case 'V':
145+# if defined(RT_TABLEID_MAX)
146                        rtableid = (unsigned int)strtonum(optarg, 0,
147                            RT_TABLEID_MAX, &errstr);
148                        if (errstr)
149                                errx(1, "rtable %s: %s", errstr, optarg);
150+# else
151+                       errx(1, "no alternate routing table support available");
152+# endif
153                        break;
154                case 'v':
155                        vflag = 1;
156@@ -232,7 +282,11 @@ main(int argc, char *argv[])
157                                    errstr, optarg);
158                        break;
159                case 'S':
160+# if defined(TCP_MD5SIG)
161                        Sflag = 1;
162+# else
163+                       errx(1, "no TCP MD5 signature support available");
164+# endif
165                        break;
166                case 'T':
167                        errstr = NULL;
168@@ -259,6 +313,15 @@ main(int argc, char *argv[])
169        if (argv[0] && !argv[1] && family == AF_UNIX) {
170                host = argv[0];
171                uport = NULL;
172+       } else if (!argv[0] && lflag) {
173+               if (sflag)
174+                       errx(1, "cannot use -s and -l");
175+               if (zflag)
176+                       errx(1, "cannot use -z and -l");
177+               if (pflag)
178+                       uport=pflag;
179+       } else if (!lflag && kflag) {
180+               errx(1, "cannot use -k without -l");
181        } else if (argv[0] && !argv[1]) {
182                if  (!lflag)
183                        usage(1);
184@@ -270,14 +333,7 @@ main(int argc, char *argv[])
185        } else
186                usage(1);
187 
188-       if (lflag && sflag)
189-               errx(1, "cannot use -s and -l");
190-       if (lflag && pflag)
191-               errx(1, "cannot use -p and -l");
192-       if (lflag && zflag)
193-               errx(1, "cannot use -z and -l");
194-       if (!lflag && kflag)
195-               errx(1, "must use -l with -k");
196+
197 
198        /* Get name of temporary socket for unix datagram client */
199        if ((family == AF_UNIX) && uflag && !lflag) {
200@@ -286,8 +342,8 @@ main(int argc, char *argv[])
201                } else {
202                        strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
203                                UNIX_DG_TMP_SOCKET_SIZE);
204-                       if (mktemp(unix_dg_tmp_socket_buf) == NULL)
205-                               err(1, "mktemp");
206+                       if (mkstemp(unix_dg_tmp_socket_buf) == -1)
207+                               err(1, "mkstemp");
208                        unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
209                }
210        }
211@@ -563,18 +619,22 @@ remote_connect(const char *host, const char *port, struct addrinfo hints)
212                    res0->ai_protocol)) < 0)
213                        continue;
214 
215+# if defined(RT_TABLEID_MAX)
216                if (rtableid) {
217                        if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid,
218                            sizeof(rtableid)) == -1)
219                                err(1, "setsockopt SO_RTABLE");
220                }
221+# endif
222 
223                /* Bind to a local port or source address if specified. */
224                if (sflag || pflag) {
225                        struct addrinfo ahints, *ares;
226 
227+# if defined (SO_BINDANY)
228                        /* try SO_BINDANY, but don't insist */
229                        setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
230+# endif
231                        memset(&ahints, 0, sizeof(struct addrinfo));
232                        ahints.ai_family = res0->ai_family;
233                        ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
234@@ -674,15 +734,23 @@ local_listen(char *host, char *port, struct addrinfo hints)
235                    res0->ai_protocol)) < 0)
236                        continue;
237 
238+# if defined(RT_TABLEID_MAX)
239                if (rtableid) {
240                        if (setsockopt(s, IPPROTO_IP, SO_RTABLE, &rtableid,
241                            sizeof(rtableid)) == -1)
242                                err(1, "setsockopt SO_RTABLE");
243                }
244+# endif
245+
246+               ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
247+               if (ret == -1)
248+                       err(1, NULL);
249 
250+# if defined(SO_REUSEPORT)
251                ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
252                if (ret == -1)
253                        err(1, NULL);
254+# endif
255 
256                set_common_sockopts(s);
257 
258@@ -886,21 +954,25 @@ set_common_sockopts(int s)
259 {
260        int x = 1;
261 
262+# if defined(TCP_MD5SIG)
263        if (Sflag) {
264                if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
265                        &x, sizeof(x)) == -1)
266                        err(1, NULL);
267        }
268+# endif
269        if (Dflag) {
270                if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
271                        &x, sizeof(x)) == -1)
272                        err(1, NULL);
273        }
274+# if defined(SO_JUMBO)
275        if (jflag) {
276                if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
277                        &x, sizeof(x)) == -1)
278                        err(1, NULL);
279        }
280+# endif
281        if (Tflag != -1) {
282                if (setsockopt(s, IPPROTO_IP, IP_TOS,
283                    &Tflag, sizeof(Tflag)) == -1)
284@@ -949,6 +1021,7 @@ map_tos(char *s, int *val)
285                { "cs7",                IPTOS_DSCP_CS7 },
286                { "ef",                 IPTOS_DSCP_EF },
287                { "inetcontrol",        IPTOS_PREC_INTERNETCONTROL },
288+               { "lowcost",            IPTOS_LOWCOST },
289                { "lowdelay",           IPTOS_LOWDELAY },
290                { "netcontrol",         IPTOS_PREC_NETCONTROL },
291                { "reliability",        IPTOS_RELIABILITY },
292@@ -969,6 +1042,9 @@ map_tos(char *s, int *val)
293 void
294 help(void)
295 {
296+# if defined(DEBIAN_VERSION)
297+        fprintf(stderr, "OpenBSD netcat (Debian patchlevel " DEBIAN_VERSION ")\n");
298+# endif
299        usage(0);
300        fprintf(stderr, "\tCommand Summary:\n\
301        \t-4            Use IPv4\n\
302@@ -978,6 +1054,7 @@ help(void)
303        \t-h            This help text\n\
304        \t-I length     TCP receive buffer length\n\
305        \t-i secs\t     Delay interval for lines sent, ports scanned\n\
306+       \t-j            Use jumbo frame\n\
307        \t-k            Keep inbound sockets open for multiple connects\n\
308        \t-l            Listen mode, for inbound connects\n\
309        \t-n            Suppress name/port resolutions\n\
310@@ -998,15 +1075,15 @@ help(void)
311        \t-x addr[:port]\tSpecify proxy address and port\n\
312        \t-z            Zero-I/O mode [used for scanning]\n\
313        Port numbers can be individual or ranges: lo-hi [inclusive]\n");
314-       exit(1);
315+       exit(0);
316 }
317 
318 void
319 usage(int ret)
320 {
321        fprintf(stderr,
322-           "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n"
323-           "\t  [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
324+           "usage: nc [-46DdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n"
325+           "\t  [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n"
326            "\t  [-V rtable] [-w timeout] [-X proxy_protocol]\n"
327            "\t  [-x proxy_address[:port]] [destination] [port]\n");
328        if (ret)
329diff --git a/socks.c b/socks.c
330index 71108d5..befd0a9 100644
331--- a/socks.c
332+++ b/socks.c
333@@ -38,7 +38,7 @@
334 #include <string.h>
335 #include <unistd.h>
336 #include <resolv.h>
337-#include <readpassphrase.h>
338+#include <bsd/readpassphrase.h>
339 #include "atomicio.h"
340 
341 #define SOCKS_PORT     "1080"
342@@ -167,11 +167,11 @@ socks_connect(const char *host, const char *port,
343                buf[2] = SOCKS_NOAUTH;
344                cnt = atomicio(vwrite, proxyfd, buf, 3);
345                if (cnt != 3)
346-                       err(1, "write failed (%zu/3)", cnt);
347+                       err(1, "write failed (%zu/3)", (size_t)cnt);
348 
349                cnt = atomicio(read, proxyfd, buf, 2);
350                if (cnt != 2)
351-                       err(1, "read failed (%zu/3)", cnt);
352+                       err(1, "read failed (%zu/3)", (size_t)cnt);
353 
354                if (buf[1] == SOCKS_NOMETHOD)
355                        errx(1, "authentication method negotiation failed");
356@@ -220,23 +220,23 @@ socks_connect(const char *host, const char *port,
357 
358                cnt = atomicio(vwrite, proxyfd, buf, wlen);
359                if (cnt != wlen)
360-                       err(1, "write failed (%zu/%zu)", cnt, wlen);
361+                       err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
362 
363                cnt = atomicio(read, proxyfd, buf, 4);
364                if (cnt != 4)
365-                       err(1, "read failed (%zu/4)", cnt);
366+                       err(1, "read failed (%zu/4)", (size_t)cnt);
367                if (buf[1] != 0)
368                        errx(1, "connection failed, SOCKS error %d", buf[1]);
369                switch (buf[3]) {
370                case SOCKS_IPV4:
371                        cnt = atomicio(read, proxyfd, buf + 4, 6);
372                        if (cnt != 6)
373-                               err(1, "read failed (%d/6)", cnt);
374+                               err(1, "read failed (%lu/6)", (unsigned long)cnt);
375                        break;
376                case SOCKS_IPV6:
377                        cnt = atomicio(read, proxyfd, buf + 4, 18);
378                        if (cnt != 18)
379-                               err(1, "read failed (%d/18)", cnt);
380+                               err(1, "read failed (%lu/18)", (unsigned long)cnt);
381                        break;
382                default:
383                        errx(1, "connection failed, unsupported address type");
384@@ -256,11 +256,11 @@ socks_connect(const char *host, const char *port,
385 
386                cnt = atomicio(vwrite, proxyfd, buf, wlen);
387                if (cnt != wlen)
388-                       err(1, "write failed (%zu/%zu)", cnt, wlen);
389+                       err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
390 
391                cnt = atomicio(read, proxyfd, buf, 8);
392                if (cnt != 8)
393-                       err(1, "read failed (%zu/8)", cnt);
394+                       err(1, "read failed (%zu/8)", (size_t)cnt);
395                if (buf[1] != 90)
396                        errx(1, "connection failed, SOCKS error %d", buf[1]);
397        } else if (socksv == -1) {
398@@ -272,39 +272,39 @@ socks_connect(const char *host, const char *port,
399 
400                /* Try to be sane about numeric IPv6 addresses */
401                if (strchr(host, ':') != NULL) {
402-                       r = snprintf(buf, sizeof(buf),
403+                       r = snprintf((char*)buf, sizeof(buf),
404                            "CONNECT [%s]:%d HTTP/1.0\r\n",
405                            host, ntohs(serverport));
406                } else {
407-                       r = snprintf(buf, sizeof(buf),
408+                       r = snprintf((char*)buf, sizeof(buf),
409                            "CONNECT %s:%d HTTP/1.0\r\n",
410                            host, ntohs(serverport));
411                }
412                if (r == -1 || (size_t)r >= sizeof(buf))
413                        errx(1, "hostname too long");
414-               r = strlen(buf);
415+               r = strlen((char*)buf);
416 
417                cnt = atomicio(vwrite, proxyfd, buf, r);
418                if (cnt != r)
419-                       err(1, "write failed (%zu/%d)", cnt, r);
420+                       err(1, "write failed (%zu/%d)", (size_t)cnt, (int)r);
421 
422                if (authretry > 1) {
423                        char resp[1024];
424 
425                        proxypass = getproxypass(proxyuser, proxyhost);
426-                       r = snprintf(buf, sizeof(buf), "%s:%s",
427+                       r = snprintf((char*)buf, sizeof(buf), "%s:%s",
428                            proxyuser, proxypass);
429                        if (r == -1 || (size_t)r >= sizeof(buf) ||
430-                           b64_ntop(buf, strlen(buf), resp,
431+                           b64_ntop(buf, strlen((char*)buf), resp,
432                            sizeof(resp)) == -1)
433                                errx(1, "Proxy username/password too long");
434-                       r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
435+                       r = snprintf((char*)buf, sizeof((char*)buf), "Proxy-Authorization: "
436                            "Basic %s\r\n", resp);
437                        if (r == -1 || (size_t)r >= sizeof(buf))
438                                errx(1, "Proxy auth response too long");
439-                       r = strlen(buf);
440+                       r = strlen((char*)buf);
441                        if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
442-                               err(1, "write failed (%zu/%d)", cnt, r);
443+                               err(1, "write failed (%zu/%d)", (size_t)cnt, r);
444                }
445 
446                /* Terminate headers */
447@@ -312,22 +312,22 @@ socks_connect(const char *host, const char *port,
448                        err(1, "write failed (2/%d)", r);
449 
450                /* Read status reply */
451-               proxy_read_line(proxyfd, buf, sizeof(buf));
452+               proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
453                if (proxyuser != NULL &&
454-                   strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
455+                   strncmp((char*)buf, "HTTP/1.0 407 ", 12) == 0) {
456                        if (authretry > 1) {
457                                fprintf(stderr, "Proxy authentication "
458                                    "failed\n");
459                        }
460                        close(proxyfd);
461                        goto again;
462-               } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
463-                   strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
464+               } else if (strncmp((char*)buf, "HTTP/1.0 200 ", 12) != 0 &&
465+                   strncmp((char*)buf, "HTTP/1.1 200 ", 12) != 0)
466                        errx(1, "Proxy error: \"%s\"", buf);
467 
468                /* Headers continue until we hit an empty line */
469                for (r = 0; r < HTTP_MAXHDRS; r++) {
470-                       proxy_read_line(proxyfd, buf, sizeof(buf));
471+                       proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
472                        if (*buf == '\0')
473                                break;
474                }
475--
Note: See TracBrowser for help on using the repository browser.