> 
> I certainly like to use names wherever i can.
>  

Allright allright.. so openssl s_client does actually do it.. So benno,
you and philip have guilted me into it..

Andras - your original diff does not apply to -current as mailed, and
you also have a bug when the -v option is used, because you end up
putting port names in the portlist array instead of numbers and
so -v doesn't work right. 

Try this instead:

Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.152
diff -u -p -u -p -r1.152 netcat.c
--- netcat.c    28 May 2016 20:14:58 -0000      1.152
+++ netcat.c    2 Jun 2016 03:35:01 -0000
@@ -1283,6 +1283,31 @@ atelnet(int nfd, unsigned char *buf, uns
        }
 }
 
+
+int
+strtoport(char *portstr, int udp)
+{
+       struct servent *entry;
+       const char *errstr;
+       char *proto;
+       int port = -1;
+
+       proto = udp ? "udp" : "tcp";
+
+       port = strtonum(portstr, 1, PORT_MAX, &errstr);
+       if (errstr != NULL) {
+               if (errno == EINVAL) {
+                       if ((entry = getservbyname(portstr, proto)) != NULL)
+                               port = ntohs(entry->s_port);
+                       else
+                               errx(1, "service \"%s\" unknown", portstr);
+               }
+               else
+                       errx(1, "port number %s: %s", errstr, portstr);
+       }
+       return port;
+}
+
 /*
  * build_ports()
  * Build an array of ports in portlist[], listing each port
@@ -1291,7 +1316,6 @@ atelnet(int nfd, unsigned char *buf, uns
 void
 build_ports(char *p)
 {
-       const char *errstr;
        char *n;
        int hi, lo, cp;
        int x = 0;
@@ -1301,13 +1325,8 @@ build_ports(char *p)
                n++;
 
                /* Make sure the ports are in order: lowest->highest. */
-               hi = strtonum(n, 1, PORT_MAX, &errstr);
-               if (errstr)
-                       errx(1, "port number %s: %s", errstr, n);
-               lo = strtonum(p, 1, PORT_MAX, &errstr);
-               if (errstr)
-                       errx(1, "port number %s: %s", errstr, p);
-
+               hi = strtoport(n, uflag);
+               lo = strtoport(p, uflag);
                if (lo > hi) {
                        cp = hi;
                        hi = lo;
@@ -1333,11 +1352,11 @@ build_ports(char *p)
                        }
                }
        } else {
-               hi = strtonum(p, 1, PORT_MAX, &errstr);
-               if (errstr)
-                       errx(1, "port number %s: %s", errstr, p);
-               portlist[0] = strdup(p);
-               if (portlist[0] == NULL)
+               char *tmp;
+               hi = strtoport(p, uflag);
+               if (asprintf(&tmp, "%d", hi) != -1)
+                       portlist[0] = tmp;
+               else
                        err(1, NULL);
        }
 }

Reply via email to