From 743bb4143c6b7c1cb93e5fd25e391f07beb6b3a4 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 19 Nov 2018 04:18:30 +0100 Subject: [PATCH 1/3] Makefile: modernize with usual conventions Signed-off-by: Jason A. Donenfeld --- .gitignore | 2 +- Makefile | 42 ++++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index a58f699..f2d070d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o -Makefile.depend +*.d udptunnel diff --git a/Makefile b/Makefile index 0a70e29..b1310e8 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,38 @@ -prefix = /usr/local - +PREFIX ?= /usr/local +DESTDIR ?= +BINDIR ?= $(PREFIX)/bin CFLAGS ?= -g -O2 - INSTALL ?= install PKG_CONFIG ?= pkg-config ifeq ($(shell $(PKG_CONFIG) --exists libsystemd || echo NO),) -DEFS += -DHAVE_SYSTEMD_SD_DAEMON_H $(shell $(PKG_CONFIG) --cflags libsystemd) -LDADD += $(shell $(PKG_CONFIG) --libs libsystemd) +CPPFLAGS += -DHAVE_SYSTEMD_SD_DAEMON_H $(shell $(PKG_CONFIG) --cflags libsystemd) +LDLIBS += $(shell $(PKG_CONFIG) --libs libsystemd) endif -CPPFLAGS += $(DEFS) $(INCLUDES) +CFLAGS += -MMD -MP +CFLAGS += -Wall -Wextra OBJECTS := log.o network.o utils.o udptunnel.o -all: depend udptunnel +ifneq ($(V),1) +BUILT_IN_LINK.o := $(LINK.o) +LINK.o = @echo " LD $@"; +LINK.o += $(BUILT_IN_LINK.o) +BUILT_IN_COMPILE.c := $(COMPILE.c) +COMPILE.c = @echo " CC $@"; +COMPILE.c += $(BUILT_IN_COMPILE.c) +endif + +all: udptunnel -install: - $(INSTALL) -d $(BASEDIR)$(prefix)/sbin/ - $(INSTALL) -m 0755 udptunnel $(BASEDIR)$(prefix)/sbin/ +install: udptunnel + @$(INSTALL) -v -d "$(DESTDIR)$(BINDIR)" && install -v -m 0755 udptunnel "$(DESTDIR)$(BINDIR)/udptunnel" clean: - rm -f Makefile.depend $(OBJECTS) udptunnel - -%.o: %.c - $(CC) $(CPPFLAGS) $(CFLAGS) -c $< + @$(RM) -vf $(OBJECTS) $(OBJECTS:%.o=%.d) udptunnel udptunnel: $(OBJECTS) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDADD) $(LIBS) - -depend: Makefile.depend -Makefile.depend: - $(CC) $(CPPFLAGS) $(CFLAGS) -MM -MG *.c > $@ --include Makefile.depend +.PHONY: all install clean +-include *.d From b18a295e15b617fd67dc2722383e740a262f18b6 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 19 Nov 2018 04:21:50 +0100 Subject: [PATCH 2/3] Squelch compiler warnings Unused variable, signed/unsigned comparison. Signed-off-by: Jason A. Donenfeld --- network.c | 4 ++-- udptunnel.c | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/network.c b/network.c index 2c0f2c1..a32d173 100644 --- a/network.c +++ b/network.c @@ -207,7 +207,7 @@ int *tcp_listener(const char *s) if (listen(fd, 128) < 0) err_sys("listen"); - if (allocated_fds < fd_num + 1 + 1) { + if (allocated_fds < (size_t)fd_num + 1 + 1) { allocated_fds += 8; fd_list = realloc(fd_list, allocated_fds * sizeof(int)); } @@ -218,7 +218,7 @@ int *tcp_listener(const char *s) } /* and then add -1 as the list terminator */ - if (allocated_fds < fd_num + 1 + 1) + if (allocated_fds < (size_t)fd_num + 1 + 1) fd_list = realloc(fd_list, ++allocated_fds * sizeof(int)); fd_list[fd_num] = -1; diff --git a/udptunnel.c b/udptunnel.c index 8dd9c17..39c9803 100644 --- a/udptunnel.c +++ b/udptunnel.c @@ -387,6 +387,7 @@ static void send_handshake(struct relay *relay) static void wait_for_child(int sig) { + (void)sig; while (waitpid(-1, NULL, WNOHANG) > 0); } From 5a3c103505f7a84ffd154ab0b73dbd237b91e6a4 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 19 Nov 2018 04:33:03 +0100 Subject: [PATCH 3/3] network: clean up address printing clang-analyzer complains about sockaddr_storage being uninitalized, so we zero that out first. Then, while we're at it, we use the right constants for getnameinfo output sizes, and note the fact that the null byte is part of snprintf's calculations. Signed-off-by: Jason A. Donenfeld --- network.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/network.c b/network.c index a32d173..602dc33 100644 --- a/network.c +++ b/network.c @@ -37,7 +37,8 @@ char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen) { - static char buf[1100], address[1025], port[32]; + static char buf[NI_MAXHOST + NI_MAXSERV + 4]; + char address[NI_MAXHOST], port[NI_MAXSERV]; int err; err = getnameinfo(addr, addrlen, address, sizeof(address), @@ -48,9 +49,9 @@ char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen) log_printf_exit(1, log_err, "getnameinfo: %s", gai_strerror(err)); if (addr->sa_family == AF_INET6) - snprintf(buf, sizeof(buf) - 1, "[%s]:%s", address, port); + snprintf(buf, sizeof(buf), "[%s]:%s", address, port); else - snprintf(buf, sizeof(buf) - 1, "%s:%s", address, port); + snprintf(buf, sizeof(buf), "%s:%s", address, port); return buf; } @@ -262,7 +263,7 @@ int accept_connections(int listening_sockets[]) for (i = 0; listening_sockets[i] != -1; i++) { int listen_sock; - struct sockaddr_storage client_addr; + struct sockaddr_storage client_addr = { 0 }; socklen_t addrlen = sizeof(client_addr); if (!FD_ISSET(listening_sockets[i], &readfds))