--- a/netcat.c 2010-08-07 23:22:36.504902769 -0400 +++ b/netcat.c 2010-08-07 23:33:22.934901172 -0400 @@ -805,61 +805,71 @@ atelnet(int nfd, unsigned char *buf, uns void build_ports(char *p) { - char *n, *endp; + char *n, *endp, *comma; int hi, lo, cp; int x = 0; - if ((n = strchr(p, '-')) != NULL) { - if (lflag) - errx(1, "Cannot use -l with multiple ports!"); - - *n = '\0'; - n++; - - /* 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"); - lo = (int)strtoul(p, &endp, 10); - if (lo <= 0 || lo > PORT_MAX || *endp != '\0') - errx(1, "port range not valid"); - - if (lo > hi) { - cp = hi; - hi = lo; - lo = cp; - } + do { + comma = strchr(p, ','); + if (comma) + *comma = '\0'; + + if ((n = strchr(p, '-')) != NULL) { + if (lflag) + errx(1, "Cannot use -l with multiple ports!"); + + *n = '\0'; + n++; + + /* 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"); + lo = (int)strtoul(p, &endp, 10); + if (lo <= 0 || lo > PORT_MAX || *endp != '\0') + errx(1, "port range not valid"); + + if (lo > hi) { + cp = hi; + hi = lo; + lo = cp; + } - /* Load ports sequentially. */ - for (cp = lo; cp <= hi; cp++) { + /* Load ports sequentially. */ + for (cp = lo; cp <= hi; cp++) { + portlist[x] = calloc(1, PORT_MAX_LEN); + if (portlist[x] == NULL) + err(1, NULL); + snprintf(portlist[x], PORT_MAX_LEN, "%d", cp); + x++; + } + } else { + hi = (int)strtoul(p, &endp, 10); + if (hi <= 0 || hi > PORT_MAX || *endp != '\0') + errx(1, "port range not valid"); portlist[x] = calloc(1, PORT_MAX_LEN); - if (portlist[x] == NULL) + if (portlist[0] == NULL) err(1, NULL); - snprintf(portlist[x], PORT_MAX_LEN, "%d", cp); + snprintf(portlist[x], PORT_MAX_LEN, "%d", hi); x++; } - - /* Randomly swap ports. */ - if (rflag) { - int y; - char *c; - - for (x = 0; x <= (hi - lo); x++) { - /* use random instead of arc4random */ - y = (random() & 0xFFFF) % (hi - lo); - c = portlist[x]; - portlist[x] = portlist[y]; - portlist[y] = c; - } + if (comma) + p = comma + 1; + } while (comma); + + /* Randomly swap ports. */ + if (rflag) { + int max = x; + int y; + char *c; + + for (x = 0; x < max; x++) { + /* use random instead of arc4random */ + y = (random() & 0xFFFF) % max; + c = portlist[x]; + portlist[x] = portlist[y]; + portlist[y] = c; } - } 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_LEN); - if (portlist[0] == NULL) - err(1, NULL); - portlist[0] = p; } } --- nc/nc.1 2010-10-08 15:33:47.092169419 +0200 +++ nc/nc.1.comma 2010-10-08 15:38:10.701171878 +0200 @@ -235,7 +235,7 @@ (in which case the local host is used). .Pp .Ar port Ns Op Ar s -can be single integers or ranges. +can be one or more comma-separated integers or ranges. Ranges are in the form nn-mm. In general, a destination port must be specified,