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

perl-5.22
Last change on this file since e013060 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
  • Makefile

    From: Aron Xu <aron@debian.org>
    Date: Mon, 13 Feb 2012 15:59:31 +0800
    Subject: port to linux with libsd
    
    ---
     Makefile |   17 ++++++++--
     nc.1     |    4 +--
     netcat.c |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------
     socks.c  |   46 +++++++++++++--------------
     4 files changed, 130 insertions(+), 42 deletions(-)
    
    diff --git a/Makefile b/Makefile
    index 150f829..96a6587 100644
    a b  
    1 #       $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
     1#       $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $
    22
    33PROG=   nc
    44SRCS=   netcat.c atomicio.c socks.c
    55
    6 .include <bsd.prog.mk>
     6LIBS=  `pkg-config --libs libbsd` -lresolv
     7OBJS=  $(SRCS:.c=.o)
     8CFLAGS=  -g -O2
     9LDFLAGS=  -Wl,--no-add-needed
     10
     11all: nc
     12nc: $(OBJS)
     13        $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o nc
     14
     15$(OBJS): %.o: %.c
     16        $(CC) $(CFLAGS) -c $< -o $@
     17
     18clean:
     19        rm -f $(OBJS) nc
  • nc.1

    diff --git a/nc.1 b/nc.1
    index 75d1437..b7014a2 100644
    a b Proxy authentication is only supported for HTTP CONNECT proxies at present. 
    146146Specifies the source port
    147147.Nm
    148148should use, subject to privilege restrictions and availability.
    149 It is an error to use this option in conjunction with the
    150 .Fl l
    151 option.
    152149.It Fl r
    153150Specifies that source and/or destination ports should be chosen randomly
    154151instead of sequentially within a range or in the order that the system
    Change IPv4 TOS value. 
    170167may be one of
    171168.Ar critical ,
    172169.Ar inetcontrol ,
     170.Ar lowcost ,
    173171.Ar lowdelay ,
    174172.Ar netcontrol ,
    175173.Ar throughput ,
  • netcat.c

    diff --git a/netcat.c b/netcat.c
    index cc4683a..9b2def2 100644
    a b  
    4242#include <netinet/ip.h>
    4343#include <arpa/telnet.h>
    4444
     45#ifndef IPTOS_LOWDELAY
     46# define IPTOS_LOWDELAY 0x10
     47# define IPTOS_THROUGHPUT 0x08
     48# define IPTOS_RELIABILITY 0x04
     49# define IPTOS_LOWCOST 0x02
     50# define IPTOS_MINCOST IPTOS_LOWCOST
     51#endif /* IPTOS_LOWDELAY */
     52
     53# ifndef IPTOS_DSCP_AF11
     54# define        IPTOS_DSCP_AF11         0x28
     55# define        IPTOS_DSCP_AF12         0x30
     56# define        IPTOS_DSCP_AF13         0x38
     57# define        IPTOS_DSCP_AF21         0x48
     58# define        IPTOS_DSCP_AF22         0x50
     59# define        IPTOS_DSCP_AF23         0x58
     60# define        IPTOS_DSCP_AF31         0x68
     61# define        IPTOS_DSCP_AF32         0x70
     62# define        IPTOS_DSCP_AF33         0x78
     63# define        IPTOS_DSCP_AF41         0x88
     64# define        IPTOS_DSCP_AF42         0x90
     65# define        IPTOS_DSCP_AF43         0x98
     66# define        IPTOS_DSCP_EF           0xb8
     67#endif /* IPTOS_DSCP_AF11 */
     68
     69#ifndef IPTOS_DSCP_CS0
     70# define        IPTOS_DSCP_CS0          0x00
     71# define        IPTOS_DSCP_CS1          0x20
     72# define        IPTOS_DSCP_CS2          0x40
     73# define        IPTOS_DSCP_CS3          0x60
     74# define        IPTOS_DSCP_CS4          0x80
     75# define        IPTOS_DSCP_CS5          0xa0
     76# define        IPTOS_DSCP_CS6          0xc0
     77# define        IPTOS_DSCP_CS7          0xe0
     78#endif /* IPTOS_DSCP_CS0 */
     79
     80#ifndef IPTOS_DSCP_EF
     81# define        IPTOS_DSCP_EF           0xb8
     82#endif /* IPTOS_DSCP_EF */
     83
     84
    4585#include <err.h>
    4686#include <errno.h>
    4787#include <netdb.h>
     
    5393#include <unistd.h>
    5494#include <fcntl.h>
    5595#include <limits.h>
     96#include <bsd/stdlib.h>
     97#include <bsd/string.h>
    5698#include "atomicio.h"
    5799
    58100#ifndef SUN_LEN
    main(int argc, char *argv[]) 
    118160        struct servent *sv;
    119161        socklen_t len;
    120162        struct sockaddr_storage cliaddr;
    121         char *proxy;
     163        char *proxy = NULL;
    122164        const char *errstr, *proxyhost = "", *proxyport = NULL;
    123165        struct addrinfo proxyhints;
    124166        char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
    main(int argc, char *argv[]) 
    164206                                errx(1, "interval %s: %s", errstr, optarg);
    165207                        break;
    166208                case 'j':
     209# if defined(SO_JUMBO)
    167210                        jflag = 1;
     211# else
     212                        errx(1, "no jumbo frame support available");
     213# endif
    168214                        break;
    169215                case 'k':
    170216                        kflag = 1;
    main(int argc, char *argv[]) 
    194240                        uflag = 1;
    195241                        break;
    196242                case 'V':
     243# if defined(RT_TABLEID_MAX)
    197244                        rtableid = (unsigned int)strtonum(optarg, 0,
    198245                            RT_TABLEID_MAX, &errstr);
    199246                        if (errstr)
    200247                                errx(1, "rtable %s: %s", errstr, optarg);
     248# else
     249                        errx(1, "no alternate routing table support available");
     250# endif
    201251                        break;
    202252                case 'v':
    203253                        vflag = 1;
    main(int argc, char *argv[]) 
    232282                                    errstr, optarg);
    233283                        break;
    234284                case 'S':
     285# if defined(TCP_MD5SIG)
    235286                        Sflag = 1;
     287# else
     288                        errx(1, "no TCP MD5 signature support available");
     289# endif
    236290                        break;
    237291                case 'T':
    238292                        errstr = NULL;
    main(int argc, char *argv[]) 
    259313        if (argv[0] && !argv[1] && family == AF_UNIX) {
    260314                host = argv[0];
    261315                uport = NULL;
     316        } else if (!argv[0] && lflag) {
     317                if (sflag)
     318                        errx(1, "cannot use -s and -l");
     319                if (zflag)
     320                        errx(1, "cannot use -z and -l");
     321                if (pflag)
     322                        uport=pflag;
     323        } else if (!lflag && kflag) {
     324                errx(1, "cannot use -k without -l");
    262325        } else if (argv[0] && !argv[1]) {
    263326                if  (!lflag)
    264327                        usage(1);
    main(int argc, char *argv[]) 
    270333        } else
    271334                usage(1);
    272335
    273         if (lflag && sflag)
    274                 errx(1, "cannot use -s and -l");
    275         if (lflag && pflag)
    276                 errx(1, "cannot use -p and -l");
    277         if (lflag && zflag)
    278                 errx(1, "cannot use -z and -l");
    279         if (!lflag && kflag)
    280                 errx(1, "must use -l with -k");
     336
    281337
    282338        /* Get name of temporary socket for unix datagram client */
    283339        if ((family == AF_UNIX) && uflag && !lflag) {
    main(int argc, char *argv[]) 
    286342                } else {
    287343                        strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
    288344                                UNIX_DG_TMP_SOCKET_SIZE);
    289                         if (mktemp(unix_dg_tmp_socket_buf) == NULL)
    290                                 err(1, "mktemp");
     345                        if (mkstemp(unix_dg_tmp_socket_buf) == -1)
     346                                err(1, "mkstemp");
    291347                        unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
    292348                }
    293349        }
    remote_connect(const char *host, const char *port, struct addrinfo hints) 
    563619                    res0->ai_protocol)) < 0)
    564620                        continue;
    565621
     622# if defined(RT_TABLEID_MAX)
    566623                if (rtableid) {
    567624                        if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid,
    568625                            sizeof(rtableid)) == -1)
    569626                                err(1, "setsockopt SO_RTABLE");
    570627                }
     628# endif
    571629
    572630                /* Bind to a local port or source address if specified. */
    573631                if (sflag || pflag) {
    574632                        struct addrinfo ahints, *ares;
    575633
     634# if defined (SO_BINDANY)
    576635                        /* try SO_BINDANY, but don't insist */
    577636                        setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
     637# endif
    578638                        memset(&ahints, 0, sizeof(struct addrinfo));
    579639                        ahints.ai_family = res0->ai_family;
    580640                        ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
    local_listen(char *host, char *port, struct addrinfo hints) 
    674734                    res0->ai_protocol)) < 0)
    675735                        continue;
    676736
     737# if defined(RT_TABLEID_MAX)
    677738                if (rtableid) {
    678739                        if (setsockopt(s, IPPROTO_IP, SO_RTABLE, &rtableid,
    679740                            sizeof(rtableid)) == -1)
    680741                                err(1, "setsockopt SO_RTABLE");
    681742                }
     743# endif
     744
     745                ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
     746                if (ret == -1)
     747                        err(1, NULL);
    682748
     749# if defined(SO_REUSEPORT)
    683750                ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
    684751                if (ret == -1)
    685752                        err(1, NULL);
     753# endif
    686754
    687755                set_common_sockopts(s);
    688756
    set_common_sockopts(int s) 
    886954{
    887955        int x = 1;
    888956
     957# if defined(TCP_MD5SIG)
    889958        if (Sflag) {
    890959                if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
    891960                        &x, sizeof(x)) == -1)
    892961                        err(1, NULL);
    893962        }
     963# endif
    894964        if (Dflag) {
    895965                if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
    896966                        &x, sizeof(x)) == -1)
    897967                        err(1, NULL);
    898968        }
     969# if defined(SO_JUMBO)
    899970        if (jflag) {
    900971                if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
    901972                        &x, sizeof(x)) == -1)
    902973                        err(1, NULL);
    903974        }
     975# endif
    904976        if (Tflag != -1) {
    905977                if (setsockopt(s, IPPROTO_IP, IP_TOS,
    906978                    &Tflag, sizeof(Tflag)) == -1)
    map_tos(char *s, int *val) 
    9491021                { "cs7",                IPTOS_DSCP_CS7 },
    9501022                { "ef",                 IPTOS_DSCP_EF },
    9511023                { "inetcontrol",        IPTOS_PREC_INTERNETCONTROL },
     1024                { "lowcost",            IPTOS_LOWCOST },
    9521025                { "lowdelay",           IPTOS_LOWDELAY },
    9531026                { "netcontrol",         IPTOS_PREC_NETCONTROL },
    9541027                { "reliability",        IPTOS_RELIABILITY },
    map_tos(char *s, int *val) 
    9691042void
    9701043help(void)
    9711044{
     1045# if defined(DEBIAN_VERSION)
     1046        fprintf(stderr, "OpenBSD netcat (Debian patchlevel " DEBIAN_VERSION ")\n");
     1047# endif
    9721048        usage(0);
    9731049        fprintf(stderr, "\tCommand Summary:\n\
    9741050        \t-4            Use IPv4\n\
    help(void) 
    9781054        \t-h            This help text\n\
    9791055        \t-I length     TCP receive buffer length\n\
    9801056        \t-i secs\t     Delay interval for lines sent, ports scanned\n\
     1057        \t-j            Use jumbo frame\n\
    9811058        \t-k            Keep inbound sockets open for multiple connects\n\
    9821059        \t-l            Listen mode, for inbound connects\n\
    9831060        \t-n            Suppress name/port resolutions\n\
    help(void) 
    9981075        \t-x addr[:port]\tSpecify proxy address and port\n\
    9991076        \t-z            Zero-I/O mode [used for scanning]\n\
    10001077        Port numbers can be individual or ranges: lo-hi [inclusive]\n");
    1001         exit(1);
     1078        exit(0);
    10021079}
    10031080
    10041081void
    10051082usage(int ret)
    10061083{
    10071084        fprintf(stderr,
    1008             "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n"
    1009             "\t  [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
     1085            "usage: nc [-46DdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n"
     1086            "\t  [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n"
    10101087            "\t  [-V rtable] [-w timeout] [-X proxy_protocol]\n"
    10111088            "\t  [-x proxy_address[:port]] [destination] [port]\n");
    10121089        if (ret)
  • socks.c

    diff --git a/socks.c b/socks.c
    index 71108d5..befd0a9 100644
    a b  
    3838#include <string.h>
    3939#include <unistd.h>
    4040#include <resolv.h>
    41 #include <readpassphrase.h>
     41#include <bsd/readpassphrase.h>
    4242#include "atomicio.h"
    4343
    4444#define SOCKS_PORT      "1080"
    socks_connect(const char *host, const char *port, 
    167167                buf[2] = SOCKS_NOAUTH;
    168168                cnt = atomicio(vwrite, proxyfd, buf, 3);
    169169                if (cnt != 3)
    170                         err(1, "write failed (%zu/3)", cnt);
     170                        err(1, "write failed (%zu/3)", (size_t)cnt);
    171171
    172172                cnt = atomicio(read, proxyfd, buf, 2);
    173173                if (cnt != 2)
    174                         err(1, "read failed (%zu/3)", cnt);
     174                        err(1, "read failed (%zu/3)", (size_t)cnt);
    175175
    176176                if (buf[1] == SOCKS_NOMETHOD)
    177177                        errx(1, "authentication method negotiation failed");
    socks_connect(const char *host, const char *port, 
    220220
    221221                cnt = atomicio(vwrite, proxyfd, buf, wlen);
    222222                if (cnt != wlen)
    223                         err(1, "write failed (%zu/%zu)", cnt, wlen);
     223                        err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
    224224
    225225                cnt = atomicio(read, proxyfd, buf, 4);
    226226                if (cnt != 4)
    227                         err(1, "read failed (%zu/4)", cnt);
     227                        err(1, "read failed (%zu/4)", (size_t)cnt);
    228228                if (buf[1] != 0)
    229229                        errx(1, "connection failed, SOCKS error %d", buf[1]);
    230230                switch (buf[3]) {
    231231                case SOCKS_IPV4:
    232232                        cnt = atomicio(read, proxyfd, buf + 4, 6);
    233233                        if (cnt != 6)
    234                                 err(1, "read failed (%d/6)", cnt);
     234                                err(1, "read failed (%lu/6)", (unsigned long)cnt);
    235235                        break;
    236236                case SOCKS_IPV6:
    237237                        cnt = atomicio(read, proxyfd, buf + 4, 18);
    238238                        if (cnt != 18)
    239                                 err(1, "read failed (%d/18)", cnt);
     239                                err(1, "read failed (%lu/18)", (unsigned long)cnt);
    240240                        break;
    241241                default:
    242242                        errx(1, "connection failed, unsupported address type");
    socks_connect(const char *host, const char *port, 
    256256
    257257                cnt = atomicio(vwrite, proxyfd, buf, wlen);
    258258                if (cnt != wlen)
    259                         err(1, "write failed (%zu/%zu)", cnt, wlen);
     259                        err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen);
    260260
    261261                cnt = atomicio(read, proxyfd, buf, 8);
    262262                if (cnt != 8)
    263                         err(1, "read failed (%zu/8)", cnt);
     263                        err(1, "read failed (%zu/8)", (size_t)cnt);
    264264                if (buf[1] != 90)
    265265                        errx(1, "connection failed, SOCKS error %d", buf[1]);
    266266        } else if (socksv == -1) {
    socks_connect(const char *host, const char *port, 
    272272
    273273                /* Try to be sane about numeric IPv6 addresses */
    274274                if (strchr(host, ':') != NULL) {
    275                         r = snprintf(buf, sizeof(buf),
     275                        r = snprintf((char*)buf, sizeof(buf),
    276276                            "CONNECT [%s]:%d HTTP/1.0\r\n",
    277277                            host, ntohs(serverport));
    278278                } else {
    279                         r = snprintf(buf, sizeof(buf),
     279                        r = snprintf((char*)buf, sizeof(buf),
    280280                            "CONNECT %s:%d HTTP/1.0\r\n",
    281281                            host, ntohs(serverport));
    282282                }
    283283                if (r == -1 || (size_t)r >= sizeof(buf))
    284284                        errx(1, "hostname too long");
    285                 r = strlen(buf);
     285                r = strlen((char*)buf);
    286286
    287287                cnt = atomicio(vwrite, proxyfd, buf, r);
    288288                if (cnt != r)
    289                         err(1, "write failed (%zu/%d)", cnt, r);
     289                        err(1, "write failed (%zu/%d)", (size_t)cnt, (int)r);
    290290
    291291                if (authretry > 1) {
    292292                        char resp[1024];
    293293
    294294                        proxypass = getproxypass(proxyuser, proxyhost);
    295                         r = snprintf(buf, sizeof(buf), "%s:%s",
     295                        r = snprintf((char*)buf, sizeof(buf), "%s:%s",
    296296                            proxyuser, proxypass);
    297297                        if (r == -1 || (size_t)r >= sizeof(buf) ||
    298                             b64_ntop(buf, strlen(buf), resp,
     298                            b64_ntop(buf, strlen((char*)buf), resp,
    299299                            sizeof(resp)) == -1)
    300300                                errx(1, "Proxy username/password too long");
    301                         r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
     301                        r = snprintf((char*)buf, sizeof((char*)buf), "Proxy-Authorization: "
    302302                            "Basic %s\r\n", resp);
    303303                        if (r == -1 || (size_t)r >= sizeof(buf))
    304304                                errx(1, "Proxy auth response too long");
    305                         r = strlen(buf);
     305                        r = strlen((char*)buf);
    306306                        if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
    307                                 err(1, "write failed (%zu/%d)", cnt, r);
     307                                err(1, "write failed (%zu/%d)", (size_t)cnt, r);
    308308                }
    309309
    310310                /* Terminate headers */
    socks_connect(const char *host, const char *port, 
    312312                        err(1, "write failed (2/%d)", r);
    313313
    314314                /* Read status reply */
    315                 proxy_read_line(proxyfd, buf, sizeof(buf));
     315                proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
    316316                if (proxyuser != NULL &&
    317                     strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
     317                    strncmp((char*)buf, "HTTP/1.0 407 ", 12) == 0) {
    318318                        if (authretry > 1) {
    319319                                fprintf(stderr, "Proxy authentication "
    320320                                    "failed\n");
    321321                        }
    322322                        close(proxyfd);
    323323                        goto again;
    324                 } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
    325                     strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
     324                } else if (strncmp((char*)buf, "HTTP/1.0 200 ", 12) != 0 &&
     325                    strncmp((char*)buf, "HTTP/1.1 200 ", 12) != 0)
    326326                        errx(1, "Proxy error: \"%s\"", buf);
    327327
    328328                /* Headers continue until we hit an empty line */
    329329                for (r = 0; r < HTTP_MAXHDRS; r++) {
    330                         proxy_read_line(proxyfd, buf, sizeof(buf));
     330                        proxy_read_line(proxyfd, (char*)buf, sizeof(buf));
    331331                        if (*buf == '\0')
    332332                                break;
    333333                }
Note: See TracBrowser for help on using the repository browser.