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 diff --git a/network.c b/network.c index 2c0f2c1..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; } @@ -207,7 +208,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 +219,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; @@ -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)) 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); }