From 03b7118139f57033659730afb740bf5cef7c961c Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 13 May 2011 12:37:26 +0200 Subject: [PATCH 1/8] Reorder checks for libraries to allow ./configure LDFLAGS=-static. OpenSSL depends on libdl and libz. When linking dynamically, libcrypto will automatically link with the other two libraries. However, when linking statically, these libraries need to be specified explicitly while linking. By moving the autoconf checks for libdl and libz before those for libcrypto, we ensure the latter test will be done with the proper libraries. --- configure.in | 2 +- m4/openssl.m4 | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/configure.in b/configure.in index a6dd6d69..0f9c2f31 100644 --- a/configure.in +++ b/configure.in @@ -153,9 +153,9 @@ AC_CACHE_SAVE dnl These are defined in files in m4/ -tinc_OPENSSL tinc_ZLIB tinc_LZO +tinc_OPENSSL dnl Check if support for jumbograms is requested AC_ARG_ENABLE(jumbograms, diff --git a/m4/openssl.m4 b/m4/openssl.m4 index 59f0d450..254ea4ff 100644 --- a/m4/openssl.m4 +++ b/m4/openssl.m4 @@ -2,6 +2,20 @@ dnl Check to find the OpenSSL headers/libraries AC_DEFUN([tinc_OPENSSL], [ + case $host_os in + *mingw*) + ;; + *) + AC_CHECK_FUNC(dlopen, + [], + [AC_CHECK_LIB(dl, dlopen, + [LIBS="$LIBS -ldl"], + [AC_MSG_ERROR([OpenSSL depends on libdl.]); break] + )] + ) + ;; + esac + AC_ARG_WITH(openssl, AS_HELP_STRING([--with-openssl=DIR], [OpenSSL base directory, or:]), [openssl="$withval" @@ -31,20 +45,6 @@ AC_DEFUN([tinc_OPENSSL], [AC_MSG_ERROR([OpenSSL libraries not found.])] ) -case $host_os in - *mingw*) - ;; - *) - AC_CHECK_FUNC(dlopen, - [], - [AC_CHECK_LIB(dl, dlopen, - [LIBS="$LIBS -ldl"], - [AC_MSG_ERROR([OpenSSL depends on libdl.]); break] - )] - ) - ;; -esac - AC_CHECK_FUNCS([RAND_pseudo_bytes EVP_EncryptInit_ex], , [AC_MSG_ERROR([Missing OpenSSL functionality, make sure you have installed the latest version.]); break], ) From 07ffb1a19859791d419b83a876ba552dadedbf46 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 22 May 2011 15:56:04 +0200 Subject: [PATCH 2/8] Make return value of SetPriorityClass() behave the same as setpriority(). --- NEWS | 4 ++++ configure.in | 2 +- src/tincd.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 792a71f1..4b2176e7 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +Version 1.0.15 not released yet + + * Fix ProcessPriority option under Windows. + Version 1.0.14 May 8 2011 * Fixed reading configuration files that do not end with a newline. Again. diff --git a/configure.in b/configure.in index 0f9c2f31..8836ca2d 100644 --- a/configure.in +++ b/configure.in @@ -3,7 +3,7 @@ dnl Process this file with autoconf to produce a configure script. AC_PREREQ(2.61) AC_INIT AC_CONFIG_SRCDIR([src/tincd.c]) -AM_INIT_AUTOMAKE(tinc, 1.0.14) +AM_INIT_AUTOMAKE(tinc, 1.0.14+git) AC_CONFIG_HEADERS([config.h]) AM_MAINTAINER_MODE diff --git a/src/tincd.c b/src/tincd.c index 40ac0383..b3d911b8 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -504,7 +504,7 @@ static bool drop_privs() { } #ifdef HAVE_MINGW -# define setpriority(level) SetPriorityClass(GetCurrentProcess(), (level)) +# define setpriority(level) !SetPriorityClass(GetCurrentProcess(), (level)) #else # define NORMAL_PRIORITY_CLASS 0 # define BELOW_NORMAL_PRIORITY_CLASS 10 From 6d08eb1614b59d5f86a43edda9db06fca72b76cd Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 28 May 2011 23:36:52 +0200 Subject: [PATCH 3/8] Fix sparse warnings and add an extra sprinkling of const. This is more or less the equivalent of Sven-Haegar Koch's fixes in the 1.1 branch. --- lib/pidfile.c | 10 ++++++---- lib/pidfile.h | 8 ++++---- lib/utils.c | 6 +++--- lib/utils.h | 2 +- lib/xalloc.h | 2 +- lib/xmalloc.c | 20 ++++++++------------ src/bsd/device.c | 1 + src/conf.c | 8 ++++---- src/conf.h | 4 ++-- src/connection.h | 20 ++++++++++---------- src/cygwin/device.c | 1 + src/dummy/device.c | 1 + src/event.c | 2 +- src/graph.c | 5 +++-- src/graph.h | 2 -- src/linux/device.c | 1 + src/mingw/device.c | 1 + src/net.c | 2 +- src/net.h | 6 ++++-- src/net_packet.c | 2 +- src/net_setup.c | 4 ++-- src/net_socket.c | 2 +- src/node.c | 6 +++--- src/node.h | 14 +++++++------- src/process.c | 4 ++-- src/protocol.c | 2 +- src/protocol.h | 4 ++-- src/protocol_key.c | 4 ++-- src/protocol_misc.c | 2 +- src/protocol_subnet.c | 4 ++-- src/raw_socket/device.c | 1 + src/solaris/device.c | 1 + src/subnet.c | 4 ++-- src/uml_socket/device.c | 1 + src/vde/device.c | 1 + 35 files changed, 83 insertions(+), 75 deletions(-) diff --git a/lib/pidfile.c b/lib/pidfile.c index 425b3043..6e24d5f0 100644 --- a/lib/pidfile.c +++ b/lib/pidfile.c @@ -27,6 +27,8 @@ #include "system.h" +#include "pidfile.h" + #ifndef HAVE_MINGW /* read_pid * @@ -34,7 +36,7 @@ * 0 is returned if either there's no pidfile, it's empty * or no pid can be read. */ -pid_t read_pid (char *pidfile) +pid_t read_pid (const char *pidfile) { FILE *f; long pid; @@ -53,7 +55,7 @@ pid_t read_pid (char *pidfile) * table (using /proc) to determine if the process already exists. If * so the pid is returned, otherwise 0. */ -pid_t check_pid (char *pidfile) +pid_t check_pid (const char *pidfile) { pid_t pid = read_pid(pidfile); @@ -79,7 +81,7 @@ pid_t check_pid (char *pidfile) * Writes the pid to the specified file. If that fails 0 is * returned, otherwise the pid. */ -pid_t write_pid (char *pidfile) +pid_t write_pid (const char *pidfile) { FILE *f; int fd; @@ -124,7 +126,7 @@ pid_t write_pid (char *pidfile) * Remove the the specified file. The result from unlink(2) * is returned */ -int remove_pid (char *pidfile) +int remove_pid (const char *pidfile) { return unlink (pidfile); } diff --git a/lib/pidfile.h b/lib/pidfile.h index d7b970c1..590a7bc7 100644 --- a/lib/pidfile.h +++ b/lib/pidfile.h @@ -26,7 +26,7 @@ * 0 is returned if either there's no pidfile, it's empty * or no pid can be read. */ -pid_t read_pid (char *pidfile); +extern pid_t read_pid (const char *pidfile); /* check_pid * @@ -34,19 +34,19 @@ pid_t read_pid (char *pidfile); * table (using /proc) to determine if the process already exists. If * so 1 is returned, otherwise 0. */ -pid_t check_pid (char *pidfile); +extern pid_t check_pid (const char *pidfile); /* write_pid * * Writes the pid to the specified file. If that fails 0 is * returned, otherwise the pid. */ -pid_t write_pid (char *pidfile); +extern pid_t write_pid (const char *pidfile); /* remove_pid * * Remove the the specified file. The result from unlink(2) * is returned */ -int remove_pid (char *pidfile); +extern int remove_pid (const char *pidfile); #endif diff --git a/lib/utils.c b/lib/utils.c index dd7e4de2..6ea904a5 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -23,9 +23,9 @@ #include "../src/logger.h" #include "utils.h" -const char hexadecimals[] = "0123456789ABCDEF"; +static const char hexadecimals[] = "0123456789ABCDEF"; -int charhex2bin(char c) { +static int charhex2bin(char c) { if(isdigit(c)) return c - '0'; else @@ -67,7 +67,7 @@ const char *winerror(int err) { } #endif -unsigned int bitfield_to_int(void *bitfield, size_t size) { +unsigned int bitfield_to_int(const void *bitfield, size_t size) { unsigned int value = 0; if(size > sizeof value) size = sizeof value; diff --git a/lib/utils.h b/lib/utils.h index 4456616d..f6ff7052 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -40,6 +40,6 @@ extern const char *winerror(int); #define sockinprogress(x) ((x) == EINPROGRESS) #endif -extern unsigned int bitfield_to_int(void *bitfield, size_t size); +extern unsigned int bitfield_to_int(const void *bitfield, size_t size); #endif /* __TINC_UTILS_H__ */ diff --git a/lib/xalloc.h b/lib/xalloc.h index 51f99bdf..952f9217 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -16,7 +16,7 @@ extern int xalloc_exit_failure; extern char *const xalloc_msg_memory_exhausted; /* FIXME: describe */ -extern void (*xalloc_fail_func) (); +extern void (*xalloc_fail_func) (int); void *xmalloc PARAMS ((size_t n)) __attribute__ ((__malloc__)); void *xmalloc_and_zero PARAMS ((size_t n)) __attribute__ ((__malloc__)); diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 4e79aff9..e4079ce4 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -56,7 +56,7 @@ int xalloc_exit_failure = EXIT_FAILURE; char *const xalloc_msg_memory_exhausted = "Memory exhausted"; /* FIXME: describe */ -void (*xalloc_fail_func) (int) = 0; +void (*xalloc_fail_func) (int) = NULL; static void xalloc_fail (int size) @@ -70,13 +70,12 @@ xalloc_fail (int size) /* Allocate N bytes of memory dynamically, with error checking. */ void * -xmalloc (n) - size_t n; +xmalloc (size_t n) { void *p; p = malloc (n); - if (p == 0) + if (p == NULL) xalloc_fail ((int)n); return p; } @@ -84,13 +83,12 @@ xmalloc (n) /* Allocate N bytes of memory dynamically, and set it all to zero. */ void * -xmalloc_and_zero (n) - size_t n; +xmalloc_and_zero (size_t n) { void *p; p = malloc (n); - if (p == 0) + if (p == NULL) xalloc_fail ((int)n); memset (p, '\0', n); return p; @@ -101,12 +99,10 @@ xmalloc_and_zero (n) If P is NULL, run xmalloc. */ void * -xrealloc (p, n) - void *p; - size_t n; +xrealloc (void *p, size_t n) { p = realloc (p, n); - if (p == 0) + if (p == NULL) xalloc_fail (n); return p; } @@ -134,7 +130,7 @@ xcalloc (n, s) void *p; p = calloc (n, s); - if (p == 0) + if (p == NULL) xalloc_fail (); return p; } diff --git a/src/bsd/device.c b/src/bsd/device.c index d6ef0e82..a41827ff 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -22,6 +22,7 @@ #include "system.h" #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "route.h" diff --git a/src/conf.c b/src/conf.c index 5f8fdb1e..b895b7f6 100644 --- a/src/conf.c +++ b/src/conf.c @@ -92,7 +92,7 @@ void config_add(avl_tree_t *config_tree, config_t *cfg) { avl_insert(config_tree, cfg); } -config_t *lookup_config(avl_tree_t *config_tree, char *variable) { +config_t *lookup_config(const avl_tree_t *config_tree, char *variable) { config_t cfg, *found; cfg.variable = variable; @@ -110,7 +110,7 @@ config_t *lookup_config(avl_tree_t *config_tree, char *variable) { return found; } -config_t *lookup_config_next(avl_tree_t *config_tree, const config_t *cfg) { +config_t *lookup_config_next(const avl_tree_t *config_tree, const config_t *cfg) { avl_node_t *node; config_t *found; @@ -188,7 +188,7 @@ bool get_config_address(const config_t *cfg, struct addrinfo **result) { } bool get_config_subnet(const config_t *cfg, subnet_t ** result) { - subnet_t subnet = {0}; + subnet_t subnet = {NULL}; if(!cfg) return false; @@ -368,7 +368,7 @@ void read_config_options(avl_tree_t *config_tree, const char *prefix) { } } -bool read_server_config() { +bool read_server_config(void) { char *fname; bool x; diff --git a/src/conf.h b/src/conf.h index 3eae4ad7..5b0796e5 100644 --- a/src/conf.h +++ b/src/conf.h @@ -48,8 +48,8 @@ extern void exit_configuration(avl_tree_t **); extern config_t *new_config(void) __attribute__ ((__malloc__)); extern void free_config(config_t *); extern void config_add(avl_tree_t *, config_t *); -extern config_t *lookup_config(avl_tree_t *, char *); -extern config_t *lookup_config_next(avl_tree_t *, const config_t *); +extern config_t *lookup_config(const avl_tree_t *, char *); +extern config_t *lookup_config_next(const avl_tree_t *, const config_t *); extern bool get_config_bool(const config_t *, bool *); extern bool get_config_int(const config_t *, int *); extern bool get_config_string(const config_t *, char **); diff --git a/src/connection.h b/src/connection.h index 05e8b4ba..eb13413b 100644 --- a/src/connection.h +++ b/src/connection.h @@ -32,16 +32,16 @@ #define OPTION_CLAMP_MSS 0x0008 typedef struct connection_status_t { - int pinged:1; /* sent ping */ - int active:1; /* 1 if active.. */ - int connecting:1; /* 1 if we are waiting for a non-blocking connect() to finish */ - int termreq:1; /* the termination of this connection was requested */ - int remove:1; /* Set to 1 if you want this connection removed */ - int timeout:1; /* 1 if gotten timeout */ - int encryptout:1; /* 1 if we can encrypt outgoing traffic */ - int decryptin:1; /* 1 if we have to decrypt incoming traffic */ - int mst:1; /* 1 if this connection is part of a minimum spanning tree */ - int unused:23; + unsigned int pinged:1; /* sent ping */ + unsigned int active:1; /* 1 if active.. */ + unsigned int connecting:1; /* 1 if we are waiting for a non-blocking connect() to finish */ + unsigned int termreq:1; /* the termination of this connection was requested */ + unsigned int remove:1; /* Set to 1 if you want this connection removed */ + unsigned int timeout:1; /* 1 if gotten timeout */ + unsigned int encryptout:1; /* 1 if we can encrypt outgoing traffic */ + unsigned int decryptin:1; /* 1 if we have to decrypt incoming traffic */ + unsigned int mst:1; /* 1 if this connection is part of a minimum spanning tree */ + unsigned int unused:23; } connection_status_t; #include "edge.h" diff --git a/src/cygwin/device.c b/src/cygwin/device.c index 90966cd2..42016cb5 100644 --- a/src/cygwin/device.c +++ b/src/cygwin/device.c @@ -24,6 +24,7 @@ #include #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "route.h" diff --git a/src/dummy/device.c b/src/dummy/device.c index c84e899b..25a38f2d 100644 --- a/src/dummy/device.c +++ b/src/dummy/device.c @@ -19,6 +19,7 @@ #include "system.h" +#include "device.h" #include "logger.h" #include "net.h" diff --git a/src/event.c b/src/event.c index 99e6a238..e03b04dc 100644 --- a/src/event.c +++ b/src/event.c @@ -28,7 +28,7 @@ avl_tree_t *event_tree; extern time_t now; -int id; +static int id; static int event_compare(const event_t *a, const event_t *b) { if(a->time > b->time) diff --git a/src/graph.c b/src/graph.c index 3495f23c..9aadcd86 100644 --- a/src/graph.c +++ b/src/graph.c @@ -49,6 +49,7 @@ #include "connection.h" #include "device.h" #include "edge.h" +#include "graph.h" #include "logger.h" #include "netutl.h" #include "node.h" @@ -65,7 +66,7 @@ static bool graph_changed = true; Please note that sorting on weight is already done by add_edge(). */ -void mst_kruskal(void) { +static void mst_kruskal(void) { avl_node_t *node, *next; edge_t *e; node_t *n; @@ -146,7 +147,7 @@ void mst_kruskal(void) { Running time: O(E) */ -void sssp_bfs(void) { +static void sssp_bfs(void) { avl_node_t *node, *next, *to; edge_t *e; node_t *n; diff --git a/src/graph.h b/src/graph.h index 26006015..fb410961 100644 --- a/src/graph.h +++ b/src/graph.h @@ -22,8 +22,6 @@ #define __TINC_GRAPH_H__ extern void graph(void); -extern void mst_kruskal(void); -extern void sssp_bfs(void); extern void dump_graph(void); #endif /* __TINC_GRAPH_H__ */ diff --git a/src/linux/device.c b/src/linux/device.c index 0632d51a..c7c1b65c 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -28,6 +28,7 @@ #endif #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "route.h" diff --git a/src/mingw/device.c b/src/mingw/device.c index fec53cf8..1dac3bfa 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -24,6 +24,7 @@ #include #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "route.h" diff --git a/src/net.c b/src/net.c index e6a1166f..3508dd73 100644 --- a/src/net.c +++ b/src/net.c @@ -423,7 +423,7 @@ int main_loop(void) { } } - send_key_changed(broadcast, myself); + send_key_changed(); keyexpires = now + keylifetime; } diff --git a/src/net.h b/src/net.h index 55856e2b..b831cdd8 100644 --- a/src/net.h +++ b/src/net.h @@ -121,6 +121,8 @@ extern time_t now; extern int contradicting_add_edge; extern int contradicting_del_edge; +extern volatile bool running; + /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ #include "connection.h" #include "node.h" @@ -133,7 +135,7 @@ extern bool handle_new_meta_connection(int); extern int setup_listen_socket(const sockaddr_t *); extern int setup_vpn_in_socket(const sockaddr_t *); extern void send_packet(const struct node_t *, vpn_packet_t *); -extern void receive_tcppacket(struct connection_t *, char *, int); +extern void receive_tcppacket(struct connection_t *, const char *, int); extern void broadcast_packet(const struct node_t *, vpn_packet_t *); extern bool setup_network(void); extern void setup_outgoing_connection(struct outgoing_t *); @@ -144,7 +146,7 @@ extern void terminate_connection(struct connection_t *, bool); extern void flush_queue(struct node_t *); extern bool read_rsa_public_key(struct connection_t *); extern void send_mtu_probe(struct node_t *); -extern void load_all_subnets(); +extern void load_all_subnets(void); #ifndef HAVE_MINGW #define closesocket(s) close(s) diff --git a/src/net_packet.c b/src/net_packet.c index c7efa886..062523e5 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -365,7 +365,7 @@ static void receive_udppacket(node_t *n, vpn_packet_t *inpkt) { receive_packet(n, inpkt); } -void receive_tcppacket(connection_t *c, char *buffer, int len) { +void receive_tcppacket(connection_t *c, const char *buffer, int len) { vpn_packet_t outpkt; outpkt.len = len; diff --git a/src/net_setup.c b/src/net_setup.c index cc6ef680..f18e3bfe 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -144,7 +144,7 @@ bool read_rsa_public_key(connection_t *c) { return false; } -bool read_rsa_private_key(void) { +static bool read_rsa_private_key(void) { FILE *fp; char *fname, *key, *pubkey; struct stat s; @@ -267,7 +267,7 @@ void load_all_subnets(void) { /* Configure node_t myself and set up the local sockets (listen only) */ -bool setup_myself(void) { +static bool setup_myself(void) { config_t *cfg; subnet_t *subnet; char *name, *hostname, *mode, *afname, *cipher, *digest; diff --git a/src/net_socket.c b/src/net_socket.c index 2e6b0685..a45bc204 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -535,7 +535,7 @@ bool handle_new_meta_connection(int sock) { return true; } -void free_outgoing(outgoing_t *outgoing) { +static void free_outgoing(outgoing_t *outgoing) { if(outgoing->ai) freeaddrinfo(outgoing->ai); diff --git a/src/node.c b/src/node.c index 36979ef3..d6156b6a 100644 --- a/src/node.c +++ b/src/node.c @@ -124,7 +124,7 @@ void node_del(node_t *n) { } node_t *lookup_node(char *name) { - node_t n = {0}; + node_t n = {NULL}; n.name = name; @@ -132,7 +132,7 @@ node_t *lookup_node(char *name) { } node_t *lookup_node_udp(const sockaddr_t *sa) { - node_t n = {0}; + node_t n = {NULL}; n.address = *sa; n.name = NULL; @@ -158,7 +158,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname); } else { memset(&n->address, 0, sizeof n->address); - n->hostname = 0; + n->hostname = NULL; ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s cleared", n->name); } } diff --git a/src/node.h b/src/node.h index 7bac28e3..107a59b6 100644 --- a/src/node.h +++ b/src/node.h @@ -28,13 +28,13 @@ #include "subnet.h" typedef struct node_status_t { - int unused_active:1; /* 1 if active (not used for nodes) */ - int validkey:1; /* 1 if we currently have a valid key for him */ - int unused_waitingforkey:1; /* 1 if we already sent out a request */ - int visited:1; /* 1 if this node has been visited by one of the graph algorithms */ - int reachable:1; /* 1 if this node is reachable in the graph */ - int indirect:1; /* 1 if this node is not directly reachable by us */ - int unused:26; + unsigned int unused_active:1; /* 1 if active (not used for nodes) */ + unsigned int validkey:1; /* 1 if we currently have a valid key for him */ + unsigned int unused_waitingforkey:1; /* 1 if we already sent out a request */ + unsigned int visited:1; /* 1 if this node has been visited by one of the graph algorithms */ + unsigned int reachable:1; /* 1 if this node is reachable in the graph */ + unsigned int indirect:1; /* 1 if this node is not directly reachable by us */ + unsigned int unused:26; } node_status_t; typedef struct node_t { diff --git a/src/process.c b/src/process.c index e74c1d9a..c6592371 100644 --- a/src/process.c +++ b/src/process.c @@ -25,6 +25,7 @@ #include "device.h" #include "edge.h" #include "logger.h" +#include "net.h" #include "node.h" #include "pidfile.h" #include "process.h" @@ -41,10 +42,9 @@ extern char *identname; extern char *pidfilename; extern char **g_argv; extern bool use_logfile; -extern volatile bool running; #ifndef HAVE_MINGW -sigset_t emptysigset; +static sigset_t emptysigset; #endif static int saved_debug_level = -1; diff --git a/src/protocol.c b/src/protocol.c index 9d7c349f..5de30d7d 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -189,7 +189,7 @@ void exit_requests(void) { } bool seen_request(char *request) { - past_request_t *new, p = {0}; + past_request_t *new, p = {NULL}; p.request = request; diff --git a/src/protocol.h b/src/protocol.h index 2aed26d1..6c46c726 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -96,10 +96,10 @@ extern bool send_add_subnet(struct connection_t *, const struct subnet_t *); extern bool send_del_subnet(struct connection_t *, const struct subnet_t *); extern bool send_add_edge(struct connection_t *, const struct edge_t *); extern bool send_del_edge(struct connection_t *, const struct edge_t *); -extern void send_key_changed(); +extern void send_key_changed(void); extern bool send_req_key(struct node_t *); extern bool send_ans_key(struct node_t *); -extern bool send_tcppacket(struct connection_t *, struct vpn_packet_t *); +extern bool send_tcppacket(struct connection_t *, const struct vpn_packet_t *); /* Request handlers */ diff --git a/src/protocol_key.c b/src/protocol_key.c index 6ff68d27..f34ebaa5 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -34,9 +34,9 @@ #include "utils.h" #include "xalloc.h" -bool mykeyused = false; +static bool mykeyused = false; -void send_key_changed() { +void send_key_changed(void) { avl_node_t *node; connection_t *c; diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 28571ef3..6f7ab03b 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -119,7 +119,7 @@ bool pong_h(connection_t *c) { /* Sending and receiving packets via TCP */ -bool send_tcppacket(connection_t *c, vpn_packet_t *packet) { +bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) { /* If there already is a lot of data in the outbuf buffer, discard this packet. We use a very simple Random Early Drop algorithm. */ diff --git a/src/protocol_subnet.c b/src/protocol_subnet.c index 9ae491d0..62656c38 100644 --- a/src/protocol_subnet.c +++ b/src/protocol_subnet.c @@ -45,7 +45,7 @@ bool add_subnet_h(connection_t *c) { char subnetstr[MAX_STRING_SIZE]; char name[MAX_STRING_SIZE]; node_t *owner; - subnet_t s = {0}, *new, *old; + subnet_t s = {NULL}, *new, *old; if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) { logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name, @@ -154,7 +154,7 @@ bool del_subnet_h(connection_t *c) { char subnetstr[MAX_STRING_SIZE]; char name[MAX_STRING_SIZE]; node_t *owner; - subnet_t s = {0}, *find; + subnet_t s = {NULL}, *find; if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) { logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name, diff --git a/src/raw_socket/device.c b/src/raw_socket/device.c index f68e4619..abe368ad 100644 --- a/src/raw_socket/device.c +++ b/src/raw_socket/device.c @@ -23,6 +23,7 @@ #include #include "conf.h" +#include "device.h" #include "net.h" #include "logger.h" #include "utils.h" diff --git a/src/solaris/device.c b/src/solaris/device.c index 3a50b169..0dfffb1b 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -26,6 +26,7 @@ #include #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "utils.h" diff --git a/src/subnet.c b/src/subnet.c index 67be0010..f010e4ea 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -52,7 +52,7 @@ static subnet_t *cache_mac_subnet[2]; static bool cache_mac_valid[2]; static int cache_mac_slot; -void subnet_cache_flush() { +void subnet_cache_flush(void) { cache_ipv4_valid[0] = cache_ipv4_valid[1] = false; cache_ipv6_valid[0] = cache_ipv6_valid[1] = false; cache_mac_valid[0] = cache_mac_valid[1] = false; @@ -451,7 +451,7 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) { void subnet_update(node_t *owner, subnet_t *subnet, bool up) { avl_node_t *node; int i; - char *envp[9] = {0}; + char *envp[9] = {NULL}; char netstr[MAXNETSTR]; char *name, *address, *port; char empty[] = ""; diff --git a/src/uml_socket/device.c b/src/uml_socket/device.c index 59551b4f..a2da7575 100644 --- a/src/uml_socket/device.c +++ b/src/uml_socket/device.c @@ -23,6 +23,7 @@ #include #include "conf.h" +#include "device.h" #include "net.h" #include "logger.h" #include "utils.h" diff --git a/src/vde/device.c b/src/vde/device.c index 6148ccbe..63171f9a 100644 --- a/src/vde/device.c +++ b/src/vde/device.c @@ -22,6 +22,7 @@ #include #include "conf.h" +#include "device.h" #include "net.h" #include "logger.h" #include "utils.h" From 5cff8c47c1781a88123c128a4cec6cdd39925aa5 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 28 May 2011 23:42:18 +0200 Subject: [PATCH 4/8] Remove newlines from log messages. --- src/node.c | 2 +- src/subnet.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node.c b/src/node.c index d6156b6a..cf70f838 100644 --- a/src/node.c +++ b/src/node.c @@ -142,7 +142,7 @@ node_t *lookup_node_udp(const sockaddr_t *sa) { void update_node_udp(node_t *n, const sockaddr_t *sa) { if(n == myself) { - logger(LOG_WARNING, "Trying to update UDP address of myself!\n"); + logger(LOG_WARNING, "Trying to update UDP address of myself!"); return; } diff --git a/src/subnet.c b/src/subnet.c index f010e4ea..47f14366 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -273,7 +273,7 @@ bool str2net(subnet_t *subnet, const char *subnetstr) { bool net2str(char *netstr, int len, const subnet_t *subnet) { if(!netstr || !subnet) { - logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!\n", netstr, subnet); + logger(LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet); return false; } From 64771f73ebbff04262defcde59263e98f89f0fa1 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 28 May 2011 23:46:56 +0200 Subject: [PATCH 5/8] Remove a few unnecessary #includes. Some spotted by Michael Tokarev. --- src/conf.c | 1 + src/connection.c | 3 --- src/connection.h | 1 - src/net_packet.c | 1 - src/node.h | 1 - 5 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/conf.c b/src/conf.c index b895b7f6..1560541a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -26,6 +26,7 @@ #include "avl_tree.h" #include "connection.h" #include "conf.h" +#include "list.h" #include "logger.h" #include "netutl.h" /* for str2address */ #include "protocol.h" diff --git a/src/connection.c b/src/connection.c index ac946abe..e7ea9b2a 100644 --- a/src/connection.c +++ b/src/connection.c @@ -23,10 +23,7 @@ #include "avl_tree.h" #include "conf.h" -#include "list.h" #include "logger.h" -#include "net.h" /* Don't ask. */ -#include "netutl.h" #include "subnet.h" #include "utils.h" #include "xalloc.h" diff --git a/src/connection.h b/src/connection.h index eb13413b..7916a710 100644 --- a/src/connection.h +++ b/src/connection.h @@ -45,7 +45,6 @@ typedef struct connection_status_t { } connection_status_t; #include "edge.h" -#include "list.h" #include "net.h" #include "node.h" diff --git a/src/net_packet.c b/src/net_packet.c index 062523e5..f2bcc3ef 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -43,7 +43,6 @@ #include "ethernet.h" #include "event.h" #include "graph.h" -#include "list.h" #include "logger.h" #include "net.h" #include "netutl.h" diff --git a/src/node.h b/src/node.h index 107a59b6..07c7c49f 100644 --- a/src/node.h +++ b/src/node.h @@ -24,7 +24,6 @@ #include "avl_tree.h" #include "connection.h" #include "event.h" -#include "list.h" #include "subnet.h" typedef struct node_status_t { From 50af33d01f425983dd2b1d7b61092a6325be3f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Greni=C3=A9?= Date: Sat, 4 Jun 2011 09:05:23 +0200 Subject: [PATCH 6/8] Nearly tickless tinc. Use pselect instead of select in main_loop (if available). This lets tincd sleeps as long as there is nothing to do. --- configure.in | 2 +- src/event.c | 12 +++++++--- src/event.h | 1 + src/net.c | 57 +++++++++++++++++++++++++++++++++++++++++------- src/net_packet.c | 2 +- src/protocol.c | 2 +- src/route.c | 2 +- 7 files changed, 63 insertions(+), 15 deletions(-) diff --git a/configure.in b/configure.in index 8836ca2d..45db5471 100644 --- a/configure.in +++ b/configure.in @@ -130,7 +130,7 @@ dnl Checks for library functions. AC_FUNC_MEMCMP AC_FUNC_ALLOCA AC_TYPE_SIGNAL -AC_CHECK_FUNCS([asprintf daemon fchmod flock ftime fork get_current_dir_name gettimeofday mlockall putenv random select strdup strerror strsignal strtol system unsetenv usleep vsyslog writev], +AC_CHECK_FUNCS([asprintf daemon fchmod flock ftime fork get_current_dir_name gettimeofday mlockall pselect putenv random select strdup strerror strsignal strtol system unsetenv usleep vsyslog writev], [], [], [#include "have.h"] ) AC_FUNC_MALLOC diff --git a/src/event.c b/src/event.c index e03b04dc..89ee0229 100644 --- a/src/event.c +++ b/src/event.c @@ -62,10 +62,10 @@ void expire_events(void) { return; event = event_tree->tail->data; - if(event->time < now) + if(event->time <= now) return; - diff = 1 + event->time - now; + diff = event->time - now; for(node = event_tree->head; node; node = node->next) { event = node->data; @@ -96,7 +96,7 @@ event_t *get_expired_event(void) { if(event_tree->head) { event = event_tree->head->data; - if(event->time < now) { + if(event->time <= now) { avl_node_t *node = event_tree->head; avl_unlink_node(event_tree, node); free(node); @@ -106,3 +106,9 @@ event_t *get_expired_event(void) { return NULL; } + +event_t *peek_next_event(void) { + if (event_tree->head) + return event_tree->head->data; + return NULL; +} diff --git a/src/event.h b/src/event.h index da2e741a..38a4eb45 100644 --- a/src/event.h +++ b/src/event.h @@ -42,5 +42,6 @@ extern void free_event(event_t *); extern void event_add(event_t *); extern void event_del(event_t *); extern event_t *get_expired_event(void); +extern event_t *peek_next_event(void); #endif /* __TINC_EVENT_H__ */ diff --git a/src/net.c b/src/net.c index 3508dd73..6700a297 100644 --- a/src/net.c +++ b/src/net.c @@ -42,6 +42,9 @@ bool do_purge = false; volatile bool running = false; +#ifdef HAVE_PSELECT +bool graph_dump = false; +#endif time_t now = 0; int contradicting_add_edge = 0; @@ -229,14 +232,14 @@ static void check_dead_connections(void) { next = node->next; c = node->data; - if(c->last_ping_time + pingtimeout < now) { + if(c->last_ping_time + pingtimeout <= now) { if(c->status.active) { if(c->status.pinged) { ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, now - c->last_ping_time); c->status.timeout = true; terminate_connection(c, true); - } else if(c->last_ping_time + pinginterval < now) { + } else if(c->last_ping_time + pinginterval <= now) { send_ping(c); } } else { @@ -258,7 +261,7 @@ static void check_dead_connections(void) { } } - if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) { + if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout <= now) { if(c->status.active) { ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) could not flush for %ld seconds (%d bytes remaining)", @@ -350,7 +353,13 @@ static void check_network_activity(fd_set * readset, fd_set * writeset) { */ int main_loop(void) { fd_set readset, writeset; +#ifdef HAVE_PSELECT + struct timespec tv; + sigset_t omask, block_mask; + time_t next_event; +#else struct timeval tv; +#endif int r, maxfd; time_t last_ping_check, last_config_check, last_graph_dump; event_t *event; @@ -361,21 +370,48 @@ int main_loop(void) { srand(now); +#ifdef HAVE_PSELECT + if(lookup_config(config_tree, "GraphDumpFile")) + graph_dump = true; + /* Block SIGHUP & SIGALRM */ + sigemptyset(&block_mask); + sigaddset(&block_mask, SIGHUP); + sigaddset(&block_mask, SIGALRM); + sigprocmask(SIG_BLOCK, &block_mask, &omask); +#endif + running = true; while(running) { - now = time(NULL); +#ifdef HAVE_PSELECT + next_event = last_ping_check + pingtimeout; + if(graph_dump && next_event > last_graph_dump + 60) + next_event = last_graph_dump + 60; - // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */ + if((event = peek_next_event()) && next_event > event->time) + next_event = event->time; + + if(next_event <= now) + tv.tv_sec = 0; + else + tv.tv_sec = next_event - now; + tv.tv_nsec = 0; +#else tv.tv_sec = 1; tv.tv_usec = 0; +#endif maxfd = build_fdset(&readset, &writeset); #ifdef HAVE_MINGW LeaveCriticalSection(&mutex); #endif +#ifdef HAVE_PSELECT + r = pselect(maxfd + 1, &readset, &writeset, NULL, &tv, &omask); +#else r = select(maxfd + 1, &readset, &writeset, NULL, &tv); +#endif + now = time(NULL); #ifdef HAVE_MINGW EnterCriticalSection(&mutex); #endif @@ -398,7 +434,7 @@ int main_loop(void) { /* Let's check if everybody is still alive */ - if(last_ping_check + pingtimeout < now) { + if(last_ping_check + pingtimeout <= now) { check_dead_connections(); last_ping_check = now; @@ -409,7 +445,7 @@ int main_loop(void) { /* Should we regenerate our key? */ - if(keyexpires < now) { + if(keyexpires <= now) { avl_node_t *node; node_t *n; @@ -550,11 +586,16 @@ int main_loop(void) { /* Dump graph if wanted every 60 seconds*/ - if(last_graph_dump + 60 < now) { + if(last_graph_dump + 60 <= now) { dump_graph(); last_graph_dump = now; } } +#ifdef HAVE_PSELECT + /* Restore SIGHUP & SIGALARM mask */ + sigprocmask(SIG_SETMASK, &omask, NULL); +#endif + return 0; } diff --git a/src/net_packet.c b/src/net_packet.c index f2bcc3ef..fe20a258 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -403,7 +403,7 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) { "No valid key known yet for %s (%s), forwarding via TCP", n->name, n->hostname); - if(n->last_req_key + 10 < now) { + if(n->last_req_key + 10 <= now) { send_req_key(n); n->last_req_key = now; } diff --git a/src/protocol.c b/src/protocol.c index 5de30d7d..aec00eee 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -214,7 +214,7 @@ void age_past_requests(void) { next = node->next; p = node->data; - if(p->firstseen + pinginterval < now) + if(p->firstseen + pinginterval <= now) avl_delete_node(past_request_tree, node), deleted++; else left++; diff --git a/src/route.c b/src/route.c index 1caf738f..e3bcf3bb 100644 --- a/src/route.c +++ b/src/route.c @@ -220,7 +220,7 @@ void age_subnets(void) { for(node = myself->subnet_tree->head; node; node = next) { next = node->next; s = node->data; - if(s->expires && s->expires < now) { + if(s->expires && s->expires <= now) { ifdebug(TRAFFIC) { char netstr[MAXNETSTR]; if(net2str(netstr, sizeof netstr, s)) From b3bbeab6e669795f6f5a6b98590da359178bfdce Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 4 Jun 2011 11:27:54 +0200 Subject: [PATCH 7/8] =?UTF-8?q?Attribution=20for=20Lo=C3=AFc=20Greni=C3=A9?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AUTHORS | 1 + THANKS | 1 + src/net.c | 1 + 3 files changed, 3 insertions(+) diff --git a/AUTHORS b/AUTHORS index e4189967..af113938 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,6 +11,7 @@ Significant contributions from: - Julien Muchembled - Timothy Redaelli - Brandon Black +- Loïc Grenié These files are from other sources: * lib/pidfile.h and lib/pidfile.c are by Martin Schulze, taken from diff --git a/THANKS b/THANKS index dc1297b3..4a6eae20 100644 --- a/THANKS +++ b/THANKS @@ -19,6 +19,7 @@ We would like to thank the following people for their contributions to tinc: * Jeroen Ubbink * Jerome Etienne * Julien Muchembled +* Loïc Grenié * Lubomír Bulej * Mads Kiilerich * Marc A. Lehmann diff --git a/src/net.c b/src/net.c index 6700a297..c2f70220 100644 --- a/src/net.c +++ b/src/net.c @@ -3,6 +3,7 @@ Copyright (C) 1998-2005 Ivo Timmermans, 2000-2011 Guus Sliepen 2006 Scott Lamb + 2011 Loïc Grenié This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by From 4b3fd94b1cc79c24c4092b6b10ed4627a2648d26 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 6 Jun 2011 16:26:11 +0200 Subject: [PATCH 8/8] Improved --logfile option. Instead of UNIX time, the log messages now start with the time in RFC3339 format, which human-readable and still easy for the computer to parse and sort. The HUP signal will also cause the log file to be closed and reopened, which is useful when log rotation is used. If there is an error while opening the log file, this is logged to stderr. --- configure.in | 2 +- doc/tinc.texi | 2 ++ doc/tincd.8.in | 4 ++++ have.h | 4 ++++ src/logger.c | 28 +++++++++++++++++++++++++--- src/logger.h | 1 + src/net.c | 2 ++ 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/configure.in b/configure.in index 45db5471..1421b16a 100644 --- a/configure.in +++ b/configure.in @@ -101,7 +101,7 @@ dnl Checks for header files. dnl We do this in multiple stages, because unlike Linux all the other operating systems really suck and don't include their own dependencies. AC_HEADER_STDC -AC_CHECK_HEADERS([stdbool.h syslog.h sys/file.h sys/ioctl.h sys/mman.h sys/param.h sys/resource.h sys/socket.h sys/time.h sys/uio.h sys/wait.h netdb.h arpa/inet.h dirent.h]) +AC_CHECK_HEADERS([stdbool.h syslog.h sys/file.h sys/ioctl.h sys/mman.h sys/param.h sys/resource.h sys/socket.h sys/time.h time.h sys/uio.h sys/wait.h netdb.h arpa/inet.h dirent.h]) AC_CHECK_HEADERS([net/if.h net/if_types.h linux/if_tun.h net/if_tun.h net/tun/if_tun.h net/if_tap.h net/tap/if_tap.h net/ethernet.h net/if_arp.h netinet/in_systm.h netinet/in.h netinet/in6.h], [], [], [#include "have.h"] ) diff --git a/doc/tinc.texi b/doc/tinc.texi index 6bbc2e24..52a0eccf 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -1638,6 +1638,8 @@ it defaults to the maximum time of 15 minutes. Partially rereads configuration files. Connections to hosts whose host config file are removed are closed. New outgoing connections specified in @file{tinc.conf} will be made. +If the --logfile option is used, this will also close and reopen the log file, +useful when log rotation is used. @item INT Temporarily increases debug level to 5. diff --git a/doc/tincd.8.in b/doc/tincd.8.in index a8ef2fb8..5ea08c01 100644 --- a/doc/tincd.8.in +++ b/doc/tincd.8.in @@ -130,6 +130,10 @@ Connections to hosts whose host config file are removed are closed. New outgoing connections specified in .Pa tinc.conf will be made. +If the +.Fl -logfile +option is used, this will also close and reopen the log file, +useful when log rotation is used. .It INT Temporarily increases debug level to 5. Send this signal again to revert to the original level. diff --git a/have.h b/have.h index 073fbaa3..72af0698 100644 --- a/have.h +++ b/have.h @@ -71,6 +71,10 @@ #include #endif +#ifdef HAVE_TIME_H +#include +#endif + #ifdef HAVE_SYS_TYPES_H #include #endif diff --git a/src/logger.c b/src/logger.c index bc20438c..f886ba4c 100644 --- a/src/logger.c +++ b/src/logger.c @@ -44,14 +44,18 @@ void openlogger(const char *ident, logmode_t mode) { case LOGMODE_FILE: logpid = getpid(); logfile = fopen(logfilename, "a"); - if(!logfile) + if(!logfile) { + fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno)); logmode = LOGMODE_NULL; + } break; case LOGMODE_SYSLOG: #ifdef HAVE_MINGW loghandle = RegisterEventSource(NULL, logident); - if(!loghandle) + if(!loghandle) { + fprintf(stderr, "Could not open log handle!"); logmode = LOGMODE_NULL; + } break; #else #ifdef HAVE_SYSLOG_H @@ -64,8 +68,24 @@ void openlogger(const char *ident, logmode_t mode) { } } +void reopenlogger() { + if(logmode != LOGMODE_FILE) + return; + + fflush(logfile); + FILE *newfile = fopen(logfilename, "a"); + if(!newfile) { + logger(LOG_ERR, "Unable to reopen log file %s: %s\n", logfilename, strerror(errno)); + return; + } + fclose(logfile); + logfile = newfile; +} + void logger(int priority, const char *format, ...) { va_list ap; + char timestr[32] = ""; + time_t now; va_start(ap, format); @@ -76,7 +96,9 @@ void logger(int priority, const char *format, ...) { fflush(stderr); break; case LOGMODE_FILE: - fprintf(logfile, "%ld %s[%ld]: ", time(NULL), logident, (long)logpid); + now = time(NULL); + strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now)); + fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid); vfprintf(logfile, format, ap); fprintf(logfile, "\n"); fflush(logfile); diff --git a/src/logger.h b/src/logger.h index 9c20eada..ff2cb345 100644 --- a/src/logger.h +++ b/src/logger.h @@ -47,6 +47,7 @@ enum { extern debug_t debug_level; extern void openlogger(const char *, logmode_t); +extern void reopenlogger(void); extern void logger(int, const char *, ...) __attribute__ ((__format__(printf, 2, 3))); extern void closelogger(void); diff --git a/src/net.c b/src/net.c index c2f70220..c5193c5d 100644 --- a/src/net.c +++ b/src/net.c @@ -501,6 +501,8 @@ int main_loop(void) { struct stat s; sighup = false; + + reopenlogger(); /* Reread our own configuration file */