On Sat, Dec 22 2018, "Ted Unangst" <t...@tedunangst.com> wrote:
> Daniel Jakots wrote:
>> Hi,
>> 
>> With nc(1) you can do:
>> nc -zv example.com 80
>> or
>> nc -zv example.com http
>> which does the same. This works well unless the service name has a dash:
>> $ nc -zv example.com syslog-tls
>> nc: service "tls" unknown
>> 
>> This is because nc(1) is able to do some port scanning and the
>> delimiter used for the range is the dash. When it sees a dash, it
>> thinks it's a port range.
>> 
>> nc(1) is not the only software that takes an input that can be a port,
>> a range or a service name: pf is in this case too. In pf the delimiter
>> used is ":" so this works fine.
>> 
>> Here's a diff that change the delimiter to ":". This breaks existing
>> scripts but it would make the syntax like pf.conf instead of using
>> another symbol for a port range.
>> 
>> If you have a better idea how to solve this problem, please share!
>
> This preserves the low-high syntax, adds low:high syntax, and also tries to be
> smarter about service-name.
>
> First look for :. That's a range.
> Next look for -. Then check it's not a service name. That's a range.
> Otherwise it's a port.

Looks like the nicest way to handle it.

> It may work, but needs testing.

Diff below hopefully has the same behavior, but with less churn and no goto.


Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.199
diff -u -p -p -u -r1.199 netcat.c
--- netcat.c    29 Nov 2018 14:25:06 -0000      1.199
+++ netcat.c    23 Dec 2018 00:02:03 -0000
@@ -1427,7 +1427,15 @@ build_ports(char *p)
        int hi, lo, cp;
        int x = 0;
 
-       if ((n = strchr(p, '-')) != NULL) {
+       if ((n = strchr(p, ':')) == NULL) {
+               if ((n = strchr(p, '-')) != NULL &&
+                   getservbyname(p, uflag ? "udp" : "tcp") != NULL) {
+                       /* Really a service name, not a "lo-hi" port range. */
+                       n = NULL;
+               }
+       }
+
+       if (n != NULL) {
                *n = '\0';
                n++;
 

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to