Pantek Library
Hosting Provided By
CybrHost
High Speed Hosting

oops... diff for netcat again

From: Andrushock <andrushock(at)korovino.net>
Date: Thu Dec 05 2002 - 17:47:26 EST


Changes the following:

- Add checks for calloc, strdup, strsep failure
- More clear and detailed output at use errx
- "Unix Domain Sockets" -> "UNIX domain sockets" in netcat.c and manpage
- KNF

Tested on:
OpenBSD 3.2-RELEASE/i386
netcat.c,v 1.53
socks.c,v 1.5
nc.1,v 1.23

  • src/usr.bin/nc/netcat.c.orig Fri Dec 6 00:43:44 2002 +++ src/usr.bin/nc/netcat.c Fri Dec 6 02:33:18 2002
    @@ -171,7 +171,8 @@
    break; case 'x': xflag = 1; - proxy = strdup(optarg); + if ((proxy = strdup(optarg)) == NULL) + err(1, "strdup"); break; case 'z': zflag = 1;
    @@ -186,7 +187,7 @@
    /* Cruft to make sure options are clean, and used properly. */ if (argv[0] && !argv[1] && family == AF_UNIX) { if (uflag) - errx(1, "cannot use -u and -U"); + errx(1, "the -u and -U options may not be specified together"); host = argv[0]; uport = NULL; } else if (argv[0] && !argv[1]) {
    @@ -201,13 +202,13 @@
    usage(1);
        if (lflag && sflag)
-               errx(1, "cannot use -s and -l");
+               errx(1, "the -s and -l options may not be specified together");
        if (lflag && pflag)
-               errx(1, "cannot use -p and -l");
+               errx(1, "the -p and -l options may not be specified together");
        if (lflag && zflag)
-               errx(1, "cannot use -z and -l");
+               errx(1, "the -z and -l options may not be specified together");
        if (!lflag && kflag)
-               errx(1, "must use -l with -k");
+               errx(1, "the -l and -k options must be specified together");
 
        /* Initialize addrinfo structure */
        if (family != AF_UNIX) {

@@ -225,10 +226,10 @@
errx(1, "no proxy support for UDP mode"); if (lflag) - errx(1, "no proxy support for listen"); + errx(1, "no proxy support for Listen mode"); if (family == AF_UNIX) - errx(1, "no proxy support for unix sockets"); + errx(1, "no proxy support for UNIX domain sockets"); /* XXX IPv6 transport to proxy would probably work */ if (family == AF_INET6)
@@ -237,7 +238,8 @@
if (sflag) errx(1, "no proxy support for local source address"); - proxyhost = strsep(&proxy, ":"); + if ((proxyhost = strsep(&proxy, ":")) == NULL || *proxyhost == '\0') + err(1, "strsep"); proxyport = proxy; memset(&proxyhints, 0, sizeof(struct addrinfo));
Do you need help?X

@@ -258,9 +260,8 @@
/* Allow only one connection at a time, but stay alive */ for (;;) { if (family != AF_UNIX) - s = local_listen(host, uport, hints); - if (s < 0) - err(1, NULL); + if ((s = local_listen(host, uport, hints)) < 0) + err(1, NULL); /* * For UDP, we will use recvfrom() initially * to wait for a caller, then use the regular
@@ -272,20 +273,16 @@
struct sockaddr_storage z; len = sizeof(z); - rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, - (struct sockaddr *)&z, &len); - if (rv < 0) - errx(1, "%s", strerror(errno)); + if ((rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, + (struct sockaddr *)&z, &len)) < 0) + errx(1, "%s", strerror(errno)); - rv = connect(s, (struct sockaddr *)&z, len); - if (rv < 0) + if ((rv = connect(s, (struct sockaddr *)&z, len)) < 0) errx(1, "%s", strerror(errno)); connfd = s; - } else { - connfd = accept(s, (struct sockaddr *)&cliaddr, - &len);
Do you need more help?X
- } + } else + connfd = accept(s, (struct sockaddr *)&cliaddr, &len); readwrite(connfd); close(connfd);
@@ -339,11 +336,10 @@
/* Don't lookup port if -n */ if (nflag) sv = NULL; - else { + else sv = getservbyport( ntohs(atoi(portlist[i])), uflag ? "udp" : "tcp"); - } printf("Connection to %s %s port [%s/%s] succeeded!\n", host, portlist[i], uflag ? "udp" : "tcp",

@@ -362,7 +358,7 @@
 
 /*
  * unix_connect()
- * Return's a socket connected to a local unix socket. Return's -1 on failure.
+ * Return's a socket connected to a local UNIX socket. Return's -1 on failure.
  */

 int
 unix_connect(char *path)
@@ -387,7 +383,7 @@
 
 /*
  * unix_listen()
- * create a unix domain socket, and listen on it.
+ * create a UNIX domain socket, and listen on it.
  */

 int
 unix_listen(char *path)
@@ -395,7 +391,7 @@
        struct sockaddr_un sun;
        int s;
 
-       /* create unix domain socket */
+       /* create UNIX domain socket */
        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
                return (-1);
 

@@ -504,8 +500,7 @@
res0->ai_protocol)) == 0) continue; - ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); - if (ret == -1) + if ((ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x))) == -1) err(1, NULL); if (bind(s, (struct sockaddr *)res0->ai_addr,
Can we help you?X

@@ -582,7 +577,7 @@
pfd[1].events = 0; } else { if((ret = atomicio(write, nfd, buf, n)) != n) - return; + return; } } }
@@ -613,7 +608,7 @@
p++; obuf[2] = *p; obuf[3] = '\0'; - if ((ret = atomicio(write , nfd, obuf, 3)) != 3) + if ((ret = atomicio(write, nfd, obuf, 3)) != 3) warnx("Write Error!"); obuf[0] = '\0'; }
@@ -642,10 +637,10 @@
/* Make sure the ports are in order: lowest->highest */ hi = (int)strtoul(n, &endp, 10); if (hi <= 0 || hi > PORT_MAX || *endp != '\0') - errx(1, "port range not valid"); + errx(1, "invalid port, range is 1 to %d", PORT_MAX); lo = (int)strtoul(p, &endp, 10); if (lo <= 0 || lo > PORT_MAX || *endp != '\0') - errx(1, "port range not valid"); + errx(1, "invalid port, range is 1 to %d", PORT_MAX); if (lo > hi) { cp = hi;

@@ -655,7 +650,8 @@
 
                /* Load ports sequentially */
                for (cp = lo; cp <= hi; cp++) {
-                       portlist[x] = calloc(1, PORT_MAX);
+                       if ((portlist[x] = calloc(1, PORT_MAX)) == NULL)
+                               err(1, "calloc");
                        snprintf(portlist[x], PORT_MAX, "%d", cp);
                        x++;
Can't find what you're looking for?X
}
@@ -675,8 +671,9 @@
} else { hi = (int)strtoul(p, &endp, 10); if (hi <= 0 || hi > PORT_MAX || *endp != '\0') - errx(1, "port range not valid"); - portlist[0] = calloc(1, PORT_MAX); + errx(1, "invalid port, range is 1 to %d", PORT_MAX); + if ((portlist[0] = calloc(1, PORT_MAX)) == NULL) + err(1, "calloc"); portlist[0] = p; }

 }
@@ -702,7 +699,7 @@

 }  

 void
-help()
+help(void)
 {

        usage(0);
        fprintf(stderr, "\tCommand Summary:\n\

@@ -731,9 +728,13 @@

 void
 usage(int ret)
 {
-       fprintf(stderr, "usage: nc [-46Uhklnrtuvz] [-i interval] [-p source port]\n");
-       fprintf(stderr, "\t  [-s ip address] [-w timeout] [-X vers] [-x proxy address [:port]]\n");
-       fprintf(stderr, "\t  [hostname] [port[s...]]\n");
+       extern char *__progname;                /* from crt0.o */
+
+       fprintf(stderr,
+           "usage: %s [-46Uhklnrtuvz] [-i interval] [-p source port]\n"
+           "\t  [-s ip address] [-w timeout] [-X vers] [-x proxy address [:port]]\n"
+           "\t  [hostname] [port[s...]]\n", __progname);
+
        if (ret)
                exit(1);

 }
--- src/usr.bin/nc/socks.c.orig Thu Dec 5 06:15:49 2002 +++ src/usr.bin/nc/socks.c Fri Dec 6 00:25:26 2002
@@ -59,9 +59,9 @@

        struct in_addr retval;  

        if (hp)
-               return *(in_addr_t *)hp->h_addr_list[0];
+               return (*(in_addr_t *)hp->h_addr_list[0]);
        if (inet_aton (s, &retval))
-               return retval.s_addr;
+               return (retval.s_addr);
        errx (1, "cannot decode address \"%s\"", s);
 }  

@@ -74,12 +74,11 @@
 

        port = strtol (s, &p, 10);
        if (s == p) {
-               sp = getservbyname (s, "tcp");
-               if (sp)
-                       return sp->s_port;
+               if (sp = getservbyname (s, "tcp"))
+                       return (sp->s_port);
        }
        if (*s != '\0' && *p == '\0')
Don't know where to look next?X
- return htons (port); + return (htons (port)); errx (1, "cannot decode port \"%s\"", s);
 }  

@@ -100,7 +99,7 @@

                proxyfd = remote_connect(proxyhost, SOCKS_PORT, proxyhints);
 
        if (!proxyfd)
-               return -1;
+               return (-1);
 
        serveraddr = decode_addr (host);
        serverport = decode_port (port);

@@ -167,5 +166,5 @@
errx (1, "connection failed, SOCKS error %d", buf[1]); } - return proxyfd; + return (proxyfd);

 }
--- src/usr.bin/nc/nc.1.orig Fri Dec 6 00:41:34 2002 +++ src/usr.bin/nc/nc.1 Fri Dec 6 01:42:46 2002
@@ -141,7 +141,7 @@

 .Nm
 should just scan for listening daemons, without sending any data to them.  .It Fl U
-Specifies to use Unix Domain Sockets.
+Specifies to use UNIX domain sockets.
 .It Fl X Ar version
 Requests that
 .Nm
@@ -183,9 +183,9 @@

 followed by a newline, and move data from port 1000 of hostname to  stdout until hostname closes the connection.  .It Li "$ nc -U /var/tmp/dsocket"
-Connect to a Unix Domain Socket.
+Connect to a UNIX domain socket.
 .It Li "$ nc -lU /var/tmp/dsocket"
-Create and listen on a Unix Domain Socket. +Create and listen on a UNIX domain socket.
 .El
 .Sh SEE ALSO
 .Xr cat 1 , 
Received on Fri Dec 6 17:49:40 2002

This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 13:29:38 EDT


Contact Us  Legal Notices  Order Services Online 
Pantek Home  Privacy Policy  IT news  Site Map  Pantek Library