--- netcat-4.0.20061122/usr.bin/nc/Makefile +++ netcat-4.0.20061122/usr.bin/nc/Makefile @@ -1,6 +1,25 @@ # $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $ +CC = gcc +LD = gcc +RM = rm -f +ifndef CFLAGS +CFLAGS = -c -O2 -fomit-frame-pointer +endif +CFLAGS += -Wall -Dlint +LDFLAGS = -s +LIBS += -lresolv + PROG= nc SRCS= netcat.c atomicio.c socks.c -.include +all: $(PROG) + +$(PROG): $(SRCS:.c=.o) + $(LD) $(LDFLAGS) $+ $(LIBS) -o $@ + +.c.o: + $(CC) $(CFLAGS) $< -o $@ + +clean: + $(RM) $(PROG) $(SRCS:.c=.o) --- netcat-4.0.20061122/usr.bin/nc/netcat.c +++ netcat-4.0.20061122/usr.bin/nc/netcat.c @@ -54,6 +54,7 @@ #include #include #include "atomicio.h" +#include #ifndef SUN_LEN #define SUN_LEN(su) \ @@ -175,6 +176,7 @@ main(int argc, char *argv[]) break; case 'r': rflag = 1; + srandom(time(NULL) + getpid()); break; case 's': sflag = optarg; @@ -294,7 +296,7 @@ main(int argc, char *argv[]) if (family != AF_UNIX) s = local_listen(host, uport, hints); if (s < 0) - err(1, NULL); + exit(1); /* * For UDP, we will use recvfrom() initially * to wait for a caller, then use the regular @@ -414,12 +416,10 @@ unix_connect(char *path) memset(&sun, 0, sizeof(struct sockaddr_un)); sun.sun_family = AF_UNIX; - if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= - sizeof(sun.sun_path)) { - close(s); - errno = ENAMETOOLONG; - return (-1); - } + if (strlen(path) >= sizeof(sun.sun_path)) + errx(1, "Socket path too long"); + strcpy(sun.sun_path, path); + if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) { close(s); return (-1); @@ -445,12 +445,9 @@ unix_listen(char *path) memset(&sun, 0, sizeof(struct sockaddr_un)); sun.sun_family = AF_UNIX; - if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= - sizeof(sun.sun_path)) { - close(s); - errno = ENAMETOOLONG; - return (-1); - } + if (strlen(path) >= sizeof(sun.sun_path)) + errx(1, "Socket path too long"); + strcpy(sun.sun_path, path); if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) { close(s); @@ -550,7 +547,7 @@ local_listen(char *host, char *port, struct addrinfo hints) res0->ai_protocol)) < 0) continue; - ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); + ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); if (ret == -1) err(1, NULL); @@ -719,7 +716,7 @@ build_ports(char *p) char *c; for (x = 0; x <= (hi - lo); x++) { - y = (arc4random() & 0xFFFF) % (hi - lo); + y = (random() & 0xFFFF) % (hi - lo); c = portlist[x]; portlist[x] = portlist[y]; portlist[y] = c;