From: Guus Sliepen Date: Tue, 22 Jul 2003 20:55:21 +0000 (+0000) Subject: Use bools and enums where appropriate. X-Git-Tag: release-1.0~45 X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=eefa28059ab989c915a7d95fb4ae728abd7ce713;p=tinc Use bools and enums where appropriate. --- diff --git a/src/conf.c b/src/conf.c index f5211521..d34a087c 100644 --- a/src/conf.c +++ b/src/conf.c @@ -19,7 +19,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: conf.c,v 1.9.4.67 2003/07/18 14:10:27 guus Exp $ + $Id: conf.c,v 1.9.4.68 2003/07/22 20:55:19 guus Exp $ */ #include "system.h" @@ -141,109 +141,109 @@ config_t *lookup_config_next(avl_tree_t *config_tree, config_t *cfg) return NULL; } -int get_config_bool(config_t *cfg, int *result) +bool get_config_bool(config_t *cfg, bool *result) { cp(); if(!cfg) - return 0; + return false; if(!strcasecmp(cfg->value, "yes")) { - *result = 1; - return 1; + *result = true; + return true; } else if(!strcasecmp(cfg->value, "no")) { - *result = 0; - return 1; + *result = false; + return true; } logger(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); - return 0; + return false; } -int get_config_int(config_t *cfg, int *result) +bool get_config_int(config_t *cfg, int *result) { cp(); if(!cfg) - return 0; + return false; if(sscanf(cfg->value, "%d", result) == 1) - return 1; + return true; logger(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); - return 0; + return false; } -int get_config_string(config_t *cfg, char **result) +bool get_config_string(config_t *cfg, char **result) { cp(); if(!cfg) - return 0; + return false; *result = xstrdup(cfg->value); - return 1; + return true; } -int get_config_address(config_t *cfg, struct addrinfo **result) +bool get_config_address(config_t *cfg, struct addrinfo **result) { struct addrinfo *ai; cp(); if(!cfg) - return 0; + return false; ai = str2addrinfo(cfg->value, NULL, 0); if(ai) { *result = ai; - return 1; + return true; } logger(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); - return 0; + return false; } -int get_config_subnet(config_t *cfg, subnet_t ** result) +bool get_config_subnet(config_t *cfg, subnet_t ** result) { subnet_t *subnet; cp(); if(!cfg) - return 0; + return false; subnet = str2net(cfg->value); if(!subnet) { logger(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); - return 0; + return false; } /* Teach newbies what subnets are... */ if(((subnet->type == SUBNET_IPV4) - && maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t))) + && !maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t))) || ((subnet->type == SUBNET_IPV6) - && maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) { + && !maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) { logger(LOG_ERR, _ ("Network address and prefix length do not match for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); free(subnet); - return 0; + return false; } *result = subnet; - return 1; + return true; } /* @@ -325,7 +325,8 @@ int read_config_file(avl_tree_t *config_tree, const char *fname) FILE *fp; char *buffer, *line; char *variable, *value; - int lineno = 0, ignore = 0; + int lineno = 0; + bool ignore = false; config_t *cfg; size_t bufsize; @@ -366,7 +367,7 @@ int read_config_file(avl_tree_t *config_tree, const char *fname) continue; /* comment: ignore */ if(!strcmp(variable, "-----BEGIN")) - ignore = 1; + ignore = true; if(!ignore) { value = strtok(NULL, "\t\n\r ="); @@ -387,7 +388,7 @@ int read_config_file(avl_tree_t *config_tree, const char *fname) } if(!strcmp(variable, "-----END")) - ignore = 0; + ignore = false; } free(buffer); @@ -396,7 +397,7 @@ int read_config_file(avl_tree_t *config_tree, const char *fname) return err; } -int read_server_config() +bool read_server_config() { char *fname; int x; @@ -412,10 +413,10 @@ int read_server_config() free(fname); - return x; + return x == 0; } -int is_safe_path(const char *file) +bool is_safe_path(const char *file) { #if !(defined(HAVE_CYGWIN) || defined(HAVE_MINGW)) char *p; @@ -426,7 +427,7 @@ int is_safe_path(const char *file) if(*file != '/') { logger(LOG_ERR, _("`%s' is not an absolute path"), file); - return 0; + return false; } p = strrchr(file, '/'); @@ -442,13 +443,13 @@ int is_safe_path(const char *file) check1: if(lstat(f, &s) < 0) { logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno)); - return 0; + return false; } if(s.st_uid != geteuid()) { logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"), f, s.st_uid, geteuid()); - return 0; + return false; } if(S_ISLNK(s.st_mode)) { @@ -457,7 +458,7 @@ check1: if(readlink(f, l, MAXBUFSIZE) < 0) { logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, strerror(errno)); - return 0; + return false; } f = l; @@ -470,16 +471,16 @@ check1: check2: if(lstat(f, &s) < 0 && errno != ENOENT) { logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno)); - return 0; + return false; } if(errno == ENOENT) - return 1; + return true; if(s.st_uid != geteuid()) { logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"), f, s.st_uid, geteuid()); - return 0; + return false; } if(S_ISLNK(s.st_mode)) { @@ -488,7 +489,7 @@ check2: if(readlink(f, l, MAXBUFSIZE) < 0) { logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, strerror(errno)); - return 0; + return false; } f = l; @@ -498,15 +499,14 @@ check2: if(s.st_mode & 0007) { /* Accessible by others */ logger(LOG_ERR, _("`%s' has unsecure permissions"), f); - return 0; + return false; } #endif - return 1; + return true; } -FILE *ask_and_safe_open(const char *filename, const char *what, - const char *mode) +FILE *ask_and_safe_open(const char *filename, const char *what, bool safe, const char *mode) { FILE *r; char *directory; @@ -562,12 +562,14 @@ FILE *ask_and_safe_open(const char *filename, const char *what, } /* Then check the file for nasty attacks */ - if(!is_safe_path(fn)) { /* Do not permit any directories that are readable or writeable by other users. */ - fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n" - "I will not create or overwrite this file.\n"), fn); - fclose(r); - free(fn); - return NULL; + if(safe) { + if(!is_safe_path(fn)) { /* Do not permit any directories that are readable or writeable by other users. */ + fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n" + "I will not create or overwrite this file.\n"), fn); + fclose(r); + free(fn); + return NULL; + } } free(fn); diff --git a/src/conf.h b/src/conf.h index 32bb9091..b4064cd1 100644 --- a/src/conf.h +++ b/src/conf.h @@ -17,14 +17,13 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: conf.h,v 1.6.4.39 2003/07/17 15:06:26 guus Exp $ + $Id: conf.h,v 1.6.4.40 2003/07/22 20:55:19 guus Exp $ */ #ifndef __TINC_CONF_H__ #define __TINC_CONF_H__ #include "avl_tree.h" -#include "subnet.h" typedef struct config_t { char *variable; @@ -33,11 +32,13 @@ typedef struct config_t { int line; } config_t; +#include "subnet.h" + extern avl_tree_t *config_tree; extern int pingtimeout; extern int maxtimeout; -extern int bypass_security; +extern bool bypass_security; extern char *confbase; extern char *netname; @@ -48,15 +49,15 @@ 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 *, config_t *); -extern int get_config_bool(config_t *, int *); -extern int get_config_int(config_t *, int *); -extern int get_config_string(config_t *, char **); -extern int get_config_address(config_t *, struct addrinfo **); -extern int get_config_subnet(config_t *, struct subnet_t **); +extern bool get_config_bool(config_t *, bool *); +extern bool get_config_int(config_t *, int *); +extern bool get_config_string(config_t *, char **); +extern bool get_config_address(config_t *, struct addrinfo **); +extern bool get_config_subnet(config_t *, struct subnet_t **); extern int read_config_file(avl_tree_t *, const char *); -extern int read_server_config(void); -extern FILE *ask_and_safe_open(const char *, const char *, const char *); -extern int is_safe_path(const char *); +extern bool read_server_config(void); +extern FILE *ask_and_safe_open(const char *, const char *, bool, const char *); +extern bool is_safe_path(const char *); #endif /* __TINC_CONF_H__ */ diff --git a/src/connection.c b/src/connection.c index 9890f51f..eba74ae4 100644 --- a/src/connection.c +++ b/src/connection.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: connection.c,v 1.1.2.40 2003/07/17 15:06:26 guus Exp $ + $Id: connection.c,v 1.1.2.41 2003/07/22 20:55:19 guus Exp $ */ #include "system.h" @@ -128,7 +128,7 @@ void dump_connections(void) logger(LOG_DEBUG, _("End of connections.")); } -int read_connection_config(connection_t *c) +bool read_connection_config(connection_t *c) { char *fname; int x; @@ -139,5 +139,5 @@ int read_connection_config(connection_t *c) x = read_config_file(c->config_tree, fname); free(fname); - return x; + return x == 0; } diff --git a/src/connection.h b/src/connection.h index 1b3ca367..0092f04e 100644 --- a/src/connection.h +++ b/src/connection.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: connection.h,v 1.1.2.34 2003/07/17 15:06:26 guus Exp $ + $Id: connection.h,v 1.1.2.35 2003/07/22 20:55:19 guus Exp $ */ #ifndef __TINC_CONNECTION_H__ @@ -27,11 +27,6 @@ #include #include "avl_tree.h" -#include "conf.h" -#include "edge.h" -#include "list.h" -#include "net.h" -#include "node.h" #define OPTION_INDIRECT 0x0001 #define OPTION_TCPONLY 0x0002 @@ -49,10 +44,15 @@ typedef struct connection_status_t { int unused:18; } connection_status_t; +#include "edge.h" +#include "list.h" +#include "net.h" +#include "node.h" + typedef struct connection_t { char *name; /* name he claims to have */ - sockaddr_t address; /* his real (internet) ip */ + union sockaddr_t address; /* his real (internet) ip */ char *hostname; /* the hostname of its real ip */ int protocol_version; /* used protocol */ @@ -105,6 +105,6 @@ extern void free_connection(connection_t *); extern void connection_add(connection_t *); extern void connection_del(connection_t *); extern void dump_connections(void); -extern int read_connection_config(connection_t *); +extern bool read_connection_config(connection_t *); #endif /* __TINC_CONNECTION_H__ */ diff --git a/src/cygwin/device.c b/src/cygwin/device.c index 4e73e4c4..49bbbea3 100644 --- a/src/cygwin/device.c +++ b/src/cygwin/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.9 2003/07/18 13:41:35 guus Exp $ + $Id: device.c,v 1.1.2.10 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -84,7 +84,7 @@ HANDLE handle; pid_t reader_pid; int sp[2]; -int setup_device(void) +bool setup_device(void) { HKEY key, key2, adapterkey; int i; @@ -108,7 +108,7 @@ int setup_device(void) if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, (OSTYPE > 4 ? NETCARD_REG_KEY_2000 : NETCARD_REG_KEY), 0, KEY_READ, &key)) { logger(LOG_ERR, _("Unable to read registry")); - return -1; + return false; } for (i = 0; ; i++) { @@ -120,7 +120,7 @@ int setup_device(void) if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) { logger(LOG_ERR, _("Unable to read registry")); - return -1; + return false; } len = sizeof(productname); @@ -146,7 +146,7 @@ skip: if(!found) { logger(LOG_ERR, _("No CIPE adapters found!")); - return -1; + return false; } /* Get adapter name */ @@ -166,14 +166,14 @@ skip: if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) { logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno)); - return -1; + return false; } reader_pid = fork(); if(reader_pid == -1) { logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno)); - return -1; + return false; } if(!reader_pid) { @@ -213,7 +213,7 @@ skip: if(handle == INVALID_HANDLE_VALUE) { logger(LOG_ERR, _("Could not open CIPE tap device for writing!")); - return -1; + return false; } device_fd = sp[0]; @@ -228,7 +228,7 @@ skip: read(device_fd, &gelukt, 1); if(gelukt != 1) { logger(LOG_DEBUG, "Tap reader failed!"); - return -1; + return false; } if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) @@ -238,7 +238,7 @@ skip: logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return false; } void close_device(void) @@ -252,7 +252,7 @@ void close_device(void) kill(reader_pid, SIGKILL); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -261,7 +261,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(sp[0], packet->data, MTU)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin; @@ -271,10 +271,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { int lenout; @@ -285,12 +285,12 @@ int write_packet(vpn_packet_t *packet) if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) { logger(LOG_ERR, "Error while writing to %s %s", device_info, device); - return -1; + return false; } device_total_out += packet->len; - return 0; + return true; } void dump_device_stats(void) diff --git a/src/darwin/device.c b/src/darwin/device.c index ef6c7777..00381a5f 100644 --- a/src/darwin/device.c +++ b/src/darwin/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.9 2003/07/18 13:41:36 guus Exp $ + $Id: device.c,v 1.1.2.10 2003/07/22 20:55:21 guus Exp $ */ #include "system.h" @@ -30,14 +30,13 @@ #define DEFAULT_DEVICE "/dev/tun0" int device_fd = -1; -int device_type; char *device; char *iface; char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { cp(); @@ -49,14 +48,14 @@ int setup_device(void) if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } device_info = _("MacOS/X tun device"); logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -66,7 +65,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -75,7 +74,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->data[12] = 0x08; @@ -88,10 +87,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -101,10 +100,12 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; + + return true; } void dump_device_stats(void) diff --git a/src/device.h b/src/device.h index 4dce250e..55a0c449 100644 --- a/src/device.h +++ b/src/device.h @@ -17,21 +17,23 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.h,v 1.1.2.10 2003/07/18 13:45:06 guus Exp $ + $Id: device.h,v 1.1.2.11 2003/07/22 20:55:19 guus Exp $ */ #ifndef __TINC_DEVICE_H__ #define __TINC_DEVICE_H__ +#include "net.h" + extern int device_fd; extern char *device; extern char *iface; -extern int setup_device(void); +extern bool setup_device(void); extern void close_device(void); -extern int read_packet(vpn_packet_t *); -extern int write_packet(vpn_packet_t *); +extern bool read_packet(struct vpn_packet_t *); +extern bool write_packet(struct vpn_packet_t *); extern void dump_device_stats(void); #endif /* __TINC_DEVICE_H__ */ diff --git a/src/freebsd/device.c b/src/freebsd/device.c index b85923b4..4ebdac2b 100644 --- a/src/freebsd/device.c +++ b/src/freebsd/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.12 2003/07/18 13:41:36 guus Exp $ + $Id: device.c,v 1.1.2.13 2003/07/22 20:55:21 guus Exp $ */ #include "system.h" @@ -30,14 +30,13 @@ #define DEFAULT_DEVICE "/dev/tap0" int device_fd = -1; -int device_type; char *device; char *iface; char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { cp(); @@ -49,14 +48,14 @@ int setup_device(void) if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } device_info = _("FreeBSD tap device"); logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -66,7 +65,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -75,7 +74,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(device_fd, packet->data, MTU)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin; @@ -85,10 +84,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -98,10 +97,12 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data, packet->len) < 0) { logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; + + return true; } void dump_device_stats(void) diff --git a/src/graph.c b/src/graph.c index 147586e5..e57ffe50 100644 --- a/src/graph.c +++ b/src/graph.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: graph.c,v 1.1.2.26 2003/07/18 13:45:06 guus Exp $ + $Id: graph.c,v 1.1.2.27 2003/07/22 20:55:19 guus Exp $ */ /* We need to generate two trees from the graph: @@ -69,7 +69,7 @@ void mst_kruskal(void) connection_t *c; int nodes = 0; int safe_edges = 0; - int skipped; + bool skipped; cp(); @@ -77,7 +77,7 @@ void mst_kruskal(void) for(node = connection_tree->head; node; node = node->next) { c = (connection_t *) node->data; - c->status.mst = 0; + c->status.mst = false; } /* Do we have something to do at all? */ @@ -91,33 +91,33 @@ void mst_kruskal(void) for(node = node_tree->head; node; node = node->next) { n = (node_t *) node->data; - n->status.visited = 0; + n->status.visited = false; nodes++; } /* Starting point */ - ((edge_t *) edge_weight_tree->head->data)->from->status.visited = 1; + ((edge_t *) edge_weight_tree->head->data)->from->status.visited = true; /* Add safe edges */ - for(skipped = 0, node = edge_weight_tree->head; node; node = next) { + for(skipped = false, node = edge_weight_tree->head; node; node = next) { next = node->next; e = (edge_t *) node->data; if(!e->reverse || e->from->status.visited == e->to->status.visited) { - skipped = 1; + skipped = true; continue; } - e->from->status.visited = 1; - e->to->status.visited = 1; + e->from->status.visited = true; + e->to->status.visited = true; if(e->connection) - e->connection->status.mst = 1; + e->connection->status.mst = true; if(e->reverse->connection) - e->reverse->connection->status.mst = 1; + e->reverse->connection->status.mst = true; safe_edges++; @@ -125,7 +125,7 @@ void mst_kruskal(void) e->to->name, e->weight); if(skipped) { - skipped = 0; + skipped = false; next = edge_weight_tree->head; continue; } @@ -145,7 +145,7 @@ void sssp_bfs(void) edge_t *e; node_t *n; avl_tree_t *todo_tree; - int indirect; + bool indirect; char *name; char *address, *port; char *envp[7]; @@ -159,14 +159,14 @@ void sssp_bfs(void) for(node = node_tree->head; node; node = node->next) { n = (node_t *) node->data; - n->status.visited = 0; - n->status.indirect = 1; + n->status.visited = false; + n->status.indirect = true; } /* Begin with myself */ - myself->status.visited = 1; - myself->status.indirect = 0; + myself->status.visited = true; + myself->status.indirect = false; myself->nexthop = myself; myself->via = myself; node = avl_alloc_node(); @@ -212,7 +212,7 @@ void sssp_bfs(void) && (!e->to->status.indirect || indirect)) continue; - e->to->status.visited = 1; + e->to->status.visited = true; e->to->status.indirect = indirect; e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop; e->to->via = indirect ? n->via : e->to; @@ -257,8 +257,8 @@ void sssp_bfs(void) n->name, n->hostname); } - n->status.validkey = 0; - n->status.waitingforkey = 0; + n->status.validkey = false; + n->status.waitingforkey = false; asprintf(&envp[0], "NETNAME=%s", netname ? : ""); asprintf(&envp[1], "DEVICE=%s", device ? : ""); diff --git a/src/linux/device.c b/src/linux/device.c index caee2548..904c36cb 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.19 2003/07/18 13:41:36 guus Exp $ + $Id: device.c,v 1.1.2.20 2003/07/22 20:55:21 guus Exp $ */ #include "system.h" @@ -39,14 +39,14 @@ #include "route.h" #include "utils.h" -enum { +typedef enum device_type_t { DEVICE_TYPE_ETHERTAP, DEVICE_TYPE_TUN, DEVICE_TYPE_TAP, -}; +} device_type_t; int device_fd = -1; -int device_type; +device_type_t device_type; char *device; char *iface; char ifrname[IFNAMSIZ]; @@ -55,7 +55,7 @@ char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { struct ifreq ifr; @@ -74,7 +74,7 @@ int setup_device(void) if(device_fd < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } #ifdef HAVE_TUNTAP @@ -105,7 +105,7 @@ int setup_device(void) #endif { if(routing_mode == RMODE_ROUTER) - overwrite_mac = 1; + overwrite_mac = true; device_info = _("Linux ethertap device"); device_type = DEVICE_TYPE_ETHERTAP; iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; @@ -113,7 +113,7 @@ int setup_device(void) logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -123,7 +123,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -136,7 +136,7 @@ int read_packet(vpn_packet_t *packet) if(lenin <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin + 10; @@ -147,7 +147,7 @@ int read_packet(vpn_packet_t *packet) if(lenin <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin; @@ -158,7 +158,7 @@ int read_packet(vpn_packet_t *packet) if(lenin <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin - 2; @@ -170,10 +170,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -186,14 +186,14 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data + 10, packet->len - 10) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } break; case DEVICE_TYPE_TAP: if(write(device_fd, packet->data, packet->len) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } break; case DEVICE_TYPE_ETHERTAP: @@ -202,14 +202,14 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data - 2, packet->len + 2) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } break; } device_total_out += packet->len; - return 0; + return true; } void dump_device_stats(void) diff --git a/src/logger.c b/src/logger.c index b8ea0922..032349d1 100644 --- a/src/logger.c +++ b/src/logger.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: logger.c,v 1.1.2.4 2003/07/17 15:06:26 guus Exp $ + $Id: logger.c,v 1.1.2.5 2003/07/22 20:55:19 guus Exp $ */ #include "system.h" @@ -25,18 +25,20 @@ #include "conf.h" #include "logger.h" -int debug_level = DEBUG_NOTHING; -static int logmode = LOGMODE_STDERR; +debug_t debug_level = DEBUG_NOTHING; +static logmode_t logmode = LOGMODE_STDERR; static pid_t logpid; extern char *logfilename; static FILE *logfile = NULL; static const char *logident = NULL; -void openlogger(const char *ident, int mode) { +void openlogger(const char *ident, logmode_t mode) { logident = ident; logmode = mode; switch(mode) { + case LOGMODE_NULL: + break; case LOGMODE_STDERR: logpid = getpid(); break; @@ -58,6 +60,8 @@ void logger(int priority, const char *format, ...) { va_start(ap, format); switch(logmode) { + case LOGMODE_NULL: + break; case LOGMODE_STDERR: vfprintf(stderr, format, ap); fprintf(stderr, "\n"); @@ -85,6 +89,9 @@ void logger(int priority, const char *format, ...) { void closelogger(void) { switch(logmode) { + case LOGMODE_NULL: + case LOGMODE_STDERR: + break; case LOGMODE_FILE: fclose(logfile); break; diff --git a/src/logger.h b/src/logger.h index 615d0c3a..eb02fb87 100644 --- a/src/logger.h +++ b/src/logger.h @@ -1,6 +1,6 @@ #ifndef __TINC_LOGGER_H__ -enum { +typedef enum debug_t { DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */ DEBUG_ALWAYS = 0, DEBUG_CONNECTIONS = 1, /* Show (dis)connects of other tinc daemons via TCP */ @@ -11,17 +11,17 @@ enum { DEBUG_TRAFFIC = 5, /* Show network traffic information */ DEBUG_PACKET = 6, /* Show contents of each packet that is being sent/received */ DEBUG_SCARY_THINGS = 10 /* You have been warned */ -}; +} debug_t; -enum { +typedef enum logmode_t { LOGMODE_NULL, LOGMODE_STDERR, LOGMODE_FILE, LOGMODE_SYSLOG -}; +} logmode_t; -extern int debug_level; -extern void openlogger(const char *, int); +extern debug_t debug_level; +extern void openlogger(const char *, logmode_t); extern void logger(int, const char *, ...) __attribute__ ((format(printf, 2, 3))); extern void closelogger(void); diff --git a/src/meta.c b/src/meta.c index 967477b5..e818b0b4 100644 --- a/src/meta.c +++ b/src/meta.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: meta.c,v 1.1.2.36 2003/07/17 15:06:26 guus Exp $ + $Id: meta.c,v 1.1.2.37 2003/07/22 20:55:19 guus Exp $ */ #include "system.h" @@ -32,7 +32,7 @@ #include "system.h" #include "utils.h" -int send_meta(connection_t *c, char *buffer, int length) +bool send_meta(connection_t *c, char *buffer, int length) { char *bufp; int outlen; @@ -58,13 +58,13 @@ int send_meta(connection_t *c, char *buffer, int length) continue; logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, c->hostname, strerror(errno)); - return -1; + return false; } bufp += result; length -= result; } - return 0; + return true; } void broadcast_meta(connection_t *from, char *buffer, int length) @@ -82,13 +82,13 @@ void broadcast_meta(connection_t *from, char *buffer, int length) } } -int receive_meta(connection_t *c) +bool receive_meta(connection_t *c) { int x; socklen_t l = sizeof(x); int oldlen, i; int lenin, reqlen; - int decrypted = 0; + bool decrypted = false; char inbuf[MAXBUFSIZE]; cp(); @@ -96,13 +96,13 @@ int receive_meta(connection_t *c) if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__, __LINE__, c->socket, strerror(errno), c->name, c->hostname); - return -1; + return false; } if(x) { logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"), c->name, c->hostname, strerror(x)); - return -1; + return false; } /* Strategy: @@ -121,12 +121,12 @@ int receive_meta(connection_t *c) ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"), c->name, c->hostname); } else if(errno == EINTR) - return 0; + return true; else logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"), c->name, c->hostname, strerror(errno)); - return -1; + return false; } oldlen = c->buflen; @@ -138,7 +138,7 @@ int receive_meta(connection_t *c) if(c->status.decryptin && !decrypted) { EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin); memcpy(c->buffer + oldlen, inbuf, lenin); - decrypted = 1; + decrypted = true; } /* Are we receiving a TCPpacket? */ @@ -172,8 +172,8 @@ int receive_meta(connection_t *c) if(reqlen) { c->reqlen = reqlen; - if(receive_request(c)) - return -1; + if(!receive_request(c)) + return false; c->buflen -= reqlen; lenin -= reqlen; @@ -188,10 +188,10 @@ int receive_meta(connection_t *c) if(c->buflen >= MAXBUFSIZE) { logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"), c->name, c->hostname); - return -1; + return false; } c->last_ping_time = now; - return 0; + return true; } diff --git a/src/meta.h b/src/meta.h index 8177e13b..f882f146 100644 --- a/src/meta.h +++ b/src/meta.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: meta.h,v 1.1.2.9 2003/07/12 17:41:45 guus Exp $ + $Id: meta.h,v 1.1.2.10 2003/07/22 20:55:19 guus Exp $ */ #ifndef __TINC_META_H__ @@ -25,8 +25,8 @@ #include "connection.h" -extern int send_meta(connection_t *, const char *, int); -extern int broadcast_meta(connection_t *, const char *, int); -extern int receive_meta(connection_t *); +extern bool send_meta(struct connection_t *, const char *, int); +extern bool broadcast_meta(struct connection_t *, const char *, int); +extern bool receive_meta(struct connection_t *); #endif /* __TINC_META_H__ */ diff --git a/src/mingw/device.c b/src/mingw/device.c index c2cdca84..d8aabcc4 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.1 2003/07/21 15:51:00 guus Exp $ + $Id: device.c,v 1.1.2.2 2003/07/22 20:55:21 guus Exp $ */ #error "Device driver for MinGW environment not written yet!" @@ -85,7 +85,7 @@ HANDLE handle; pid_t reader_pid; int sp[2]; -int setup_device(void) +bool setup_device(void) { HKEY key, key2, adapterkey; int i; @@ -109,7 +109,7 @@ int setup_device(void) if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, (OSTYPE > 4 ? NETCARD_REG_KEY_2000 : NETCARD_REG_KEY), 0, KEY_READ, &key)) { logger(LOG_ERR, _("Unable to read registry")); - return -1; + return false; } for (i = 0; ; i++) { @@ -121,7 +121,7 @@ int setup_device(void) if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) { logger(LOG_ERR, _("Unable to read registry")); - return -1; + return false; } len = sizeof(productname); @@ -147,7 +147,7 @@ skip: if(!found) { logger(LOG_ERR, _("No CIPE adapters found!")); - return -1; + return false; } /* Get adapter name */ @@ -167,14 +167,14 @@ skip: if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) { logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno)); - return -1; + return false; } reader_pid = fork(); if(reader_pid == -1) { logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno)); - return -1; + return false; } if(!reader_pid) { @@ -214,7 +214,7 @@ skip: if(handle == INVALID_HANDLE_VALUE) { logger(LOG_ERR, _("Could not open CIPE tap device for writing!")); - return -1; + return false; } device_fd = sp[0]; @@ -229,7 +229,7 @@ skip: read(device_fd, &gelukt, 1); if(gelukt != 1) { logger(LOG_DEBUG, "Tap reader failed!"); - return -1; + return false; } if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) @@ -239,7 +239,7 @@ skip: logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -253,7 +253,7 @@ void close_device(void) kill(reader_pid, SIGKILL); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -262,7 +262,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(sp[0], packet->data, MTU)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin; @@ -272,10 +272,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { int lenout; @@ -286,12 +286,12 @@ int write_packet(vpn_packet_t *packet) if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) { logger(LOG_ERR, "Error while writing to %s %s", device_info, device); - return -1; + return false; } device_total_out += packet->len; - return 0; + return true; } void dump_device_stats(void) diff --git a/src/net.c b/src/net.c index d43aede4..475ea0c9 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.c,v 1.35.4.191 2003/07/17 15:06:26 guus Exp $ + $Id: net.c,v 1.35.4.192 2003/07/22 20:55:19 guus Exp $ */ #include "system.h" @@ -40,9 +40,7 @@ #include "route.h" #include "subnet.h" -int do_purge = 0; -int sighup = 0; -int sigalrm = 0; +bool do_purge = false; time_t now = 0; @@ -134,11 +132,11 @@ static int build_fdset(fd_set * fs) /* Terminate a connection: - Close the socket - - Remove associated edge and tell other connections about it if report = 1 + - Remove associated edge and tell other connections about it if report = true - Check if we need to retry making an outgoing connection - Deactivate the host */ -void terminate_connection(connection_t *c, int report) +void terminate_connection(connection_t *c, bool report) { cp(); @@ -148,8 +146,8 @@ void terminate_connection(connection_t *c, int report) ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"), c->name, c->hostname); - c->status.remove = 1; - c->status.active = 0; + c->status.remove = true; + c->status.active = false; if(c->node) c->node->connection = NULL; @@ -200,8 +198,8 @@ static void check_dead_connections(void) if(c->status.pinged) { ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"), c->name, c->hostname); - c->status.timeout = 1; - terminate_connection(c, 1); + c->status.timeout = true; + terminate_connection(c, true); } else { send_ping(c); } @@ -214,7 +212,7 @@ static void check_dead_connections(void) } ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"), c->name, c->hostname); - terminate_connection(c, 0); + terminate_connection(c, false); } } } @@ -235,7 +233,7 @@ static void check_network_activity(fd_set * f) cp(); if(FD_ISSET(device_fd, f)) { - if(!read_packet(&packet)) + if(read_packet(&packet)) route_outgoing(&packet); } @@ -247,7 +245,7 @@ static void check_network_activity(fd_set * f) if(FD_ISSET(c->socket, f)) { if(c->status.connecting) { - c->status.connecting = 0; + c->status.connecting = false; getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len); if(!result) @@ -262,7 +260,7 @@ static void check_network_activity(fd_set * f) } } - if(receive_meta(c) < 0) { + if(!receive_meta(c)) { terminate_connection(c, c->status.active); continue; } @@ -321,7 +319,7 @@ void main_loop(void) if(do_purge) { purge(); - do_purge = 0; + do_purge = false; } /* Let's check if everybody is still alive */ @@ -361,7 +359,7 @@ void main_loop(void) event->handler(event->data); event_del(event); } - sigalrm = 0; + sigalrm = false; } if(sighup) { @@ -370,14 +368,14 @@ void main_loop(void) char *fname; struct stat s; - sighup = 0; + sighup = false; /* Reread our own configuration file */ exit_configuration(&config_tree); init_configuration(&config_tree); - if(read_server_config()) { + if(!read_server_config()) { logger(LOG_ERR, _("Unable to reread configuration file, exitting.")); exit(1); } diff --git a/src/net.h b/src/net.h index 28dd4eaf..22d78b2b 100644 --- a/src/net.h +++ b/src/net.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.h,v 1.9.4.65 2003/07/18 12:16:24 guus Exp $ + $Id: net.h,v 1.9.4.66 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_NET_H__ @@ -54,7 +54,7 @@ typedef struct ipv6_t { typedef short length_t; -typedef union { +typedef union sockaddr_t { struct sockaddr sa; struct sockaddr_in in; struct sockaddr_in6 in6; @@ -87,6 +87,12 @@ typedef struct packet_queue_t { queue_element_t *tail; } packet_queue_t; +typedef struct listen_socket_t { + int tcp; + int udp; + sockaddr_t sa; +} listen_socket_t; + #include "conf.h" typedef struct outgoing_t { @@ -97,12 +103,6 @@ typedef struct outgoing_t { struct addrinfo *aip; } outgoing_t; -typedef struct listen_socket_t { - int tcp; - int udp; - sockaddr_t sa; -} listen_socket_t; - extern int maxtimeout; extern int seconds_till_retry; extern int addressfamily; @@ -111,8 +111,8 @@ extern listen_socket_t listen_socket[MAXSOCKETS]; extern int listen_sockets; extern int keyexpires; extern int keylifetime; -extern int do_prune; -extern int do_purge; +extern bool do_prune; +extern bool do_purge; extern char *myport; extern time_t now; extern EVP_CIPHER_CTX packet_ctx; @@ -125,19 +125,19 @@ extern void retry_outgoing(outgoing_t *); extern void handle_incoming_vpn_data(int); extern void finish_connecting(struct connection_t *); extern void do_outgoing_connection(struct connection_t *); -extern int handle_new_meta_connection(int); +extern bool handle_new_meta_connection(int); extern int setup_listen_socket(sockaddr_t *); extern int setup_vpn_in_socket(sockaddr_t *); extern void send_packet(struct node_t *, vpn_packet_t *); extern void receive_tcppacket(struct connection_t *, char *, int); extern void broadcast_packet(struct node_t *, vpn_packet_t *); -extern int setup_network_connections(void); +extern bool setup_network_connections(void); extern void setup_outgoing_connection(struct outgoing_t *); extern void try_outgoing_connections(void); extern void close_network_connections(void); extern void main_loop(void); -extern void terminate_connection(struct connection_t *, int); +extern void terminate_connection(struct connection_t *, bool); extern void flush_queue(struct node_t *); -extern int read_rsa_public_key(struct connection_t *); +extern bool read_rsa_public_key(struct connection_t *); #endif /* __TINC_NET_H__ */ diff --git a/src/net_packet.c b/src/net_packet.c index e858daa0..cac24ee6 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_packet.c,v 1.1.2.34 2003/07/17 15:06:26 guus Exp $ + $Id: net_packet.c,v 1.1.2.35 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -239,7 +239,7 @@ static void send_udppacket(node_t *n, vpn_packet_t *inpkt) if(!n->status.waitingforkey) send_req_key(n->nexthop->connection, myself, n); - n->status.waitingforkey = 1; + n->status.waitingforkey = true; return; } @@ -350,8 +350,8 @@ void send_packet(node_t *n, vpn_packet_t *packet) n->name, via->name, n->via->hostname); if((myself->options | via->options) & OPTION_TCPONLY) { - if(send_tcppacket(via->connection, packet)) - terminate_connection(via->connection, 1); + if(!send_tcppacket(via->connection, packet)) + terminate_connection(via->connection, true); } else send_udppacket(via, packet); } diff --git a/src/net_setup.c b/src/net_setup.c index b3c565ba..5a5564e9 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_setup.c,v 1.1.2.36 2003/07/18 13:45:06 guus Exp $ + $Id: net_setup.c,v 1.1.2.37 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -44,7 +44,7 @@ char *myport; -int read_rsa_public_key(connection_t *c) +bool read_rsa_public_key(connection_t *c) { FILE *fp; char *fname; @@ -63,56 +63,50 @@ int read_rsa_public_key(connection_t *c) BN_hex2bn(&c->rsa_key->n, key); BN_hex2bn(&c->rsa_key->e, "FFFF"); free(key); - return 0; + return true; } /* Else, check for PublicKeyFile statement and read it */ - if(get_config_string - (lookup_config(c->config_tree, "PublicKeyFile"), &fname)) { - if(is_safe_path(fname)) { - fp = fopen(fname, "r"); - - if(!fp) { - logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"), - fname, strerror(errno)); - free(fname); - return -1; - } + if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) { + fp = fopen(fname, "r"); + if(!fp) { + logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"), + fname, strerror(errno)); free(fname); - c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL); - fclose(fp); + return false; + } - if(c->rsa_key) - return 0; /* Woohoo. */ + free(fname); + c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL); + fclose(fp); - /* If it fails, try PEM_read_RSA_PUBKEY. */ - fp = fopen(fname, "r"); + if(c->rsa_key) + return true; /* Woohoo. */ - if(!fp) { - logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"), - fname, strerror(errno)); - free(fname); - return -1; - } + /* If it fails, try PEM_read_RSA_PUBKEY. */ + fp = fopen(fname, "r"); + if(!fp) { + logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"), + fname, strerror(errno)); free(fname); - c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL); - fclose(fp); + return false; + } - if(c->rsa_key) { -// RSA_blinding_on(c->rsa_key, NULL); - return 0; - } + free(fname); + c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL); + fclose(fp); - logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"), - fname, strerror(errno)); - return -1; - } else { - free(fname); - return -1; + if(c->rsa_key) { +// RSA_blinding_on(c->rsa_key, NULL); + return true; } + + logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"), + fname, strerror(errno)); + return false; } /* Else, check if a harnessed public key is in the config file */ @@ -128,7 +122,7 @@ int read_rsa_public_key(connection_t *c) free(fname); if(c->rsa_key) - return 0; + return true; /* Try again with PEM_read_RSA_PUBKEY. */ @@ -144,14 +138,14 @@ int read_rsa_public_key(connection_t *c) free(fname); if(c->rsa_key) - return 0; + return true; logger(LOG_ERR, _("No public key for %s specified!"), c->name); - return -1; + return false; } -int read_rsa_private_key(void) +bool read_rsa_private_key(void) { FILE *fp; char *fname, *key; @@ -164,7 +158,7 @@ int read_rsa_private_key(void) BN_hex2bn(&myself->connection->rsa_key->d, key); BN_hex2bn(&myself->connection->rsa_key->e, "FFFF"); free(key); - return 0; + return true; } if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) @@ -177,7 +171,7 @@ int read_rsa_private_key(void) logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"), fname, strerror(errno)); free(fname); - return -1; + return false; } free(fname); @@ -188,27 +182,28 @@ int read_rsa_private_key(void) if(!myself->connection->rsa_key) { logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"), fname, strerror(errno)); - return -1; + return false; } - return 0; + return true; } free(fname); - return -1; + return false; } /* Configure node_t myself and set up the local sockets (listen only) */ -int setup_myself(void) +bool setup_myself(void) { config_t *cfg; subnet_t *subnet; char *name, *hostname, *mode, *afname, *cipher, *digest; char *address = NULL; struct addrinfo hint, *ai, *aip; - int choice, err; + bool choice; + int err; cp(); @@ -224,28 +219,28 @@ int setup_myself(void) if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */ logger(LOG_ERR, _("Name for tinc daemon required!")); - return -1; + return false; } - if(check_id(name)) { + if(!check_id(name)) { logger(LOG_ERR, _("Invalid name for myself!")); free(name); - return -1; + return false; } myself->name = name; myself->connection->name = xstrdup(name); - if(read_rsa_private_key()) - return -1; + if(!read_rsa_private_key()) + return false; - if(read_connection_config(myself->connection)) { + if(!read_connection_config(myself->connection)) { logger(LOG_ERR, _("Cannot open host configuration file for myself!")); - return -1; + return false; } - if(read_rsa_public_key(myself->connection)) - return -1; + if(!read_rsa_public_key(myself->connection)) + return false; if(!get_config_string (lookup_config(myself->connection->config_tree, "Port"), &myport)) @@ -257,7 +252,7 @@ int setup_myself(void) while(cfg) { if(!get_config_subnet(cfg, &subnet)) - return -1; + return false; subnet_add(myself, subnet); @@ -274,14 +269,11 @@ int setup_myself(void) if(choice) myself->options |= OPTION_TCPONLY; - if(get_config_bool - (lookup_config(myself->connection->config_tree, "IndirectData"), - &choice)) + if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice)) if(choice) myself->options |= OPTION_INDIRECT; - if(get_config_bool - (lookup_config(myself->connection->config_tree, "TCPOnly"), &choice)) + if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice)) if(choice) myself->options |= OPTION_TCPONLY; @@ -297,14 +289,14 @@ int setup_myself(void) routing_mode = RMODE_HUB; else { logger(LOG_ERR, _("Invalid routing mode!")); - return -1; + return false; } free(mode); } else routing_mode = RMODE_ROUTER; - get_config_bool(lookup_config(config_tree, "PriorityInheritance"), - &priorityinheritance); + get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance); + #if !defined(SOL_IP) || !defined(IP_TOS) if(priorityinheritance) logger(LOG_WARNING, _("PriorityInheritance not supported on this platform")); @@ -313,12 +305,10 @@ int setup_myself(void) if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) macexpire = 600; - if(get_config_int - (lookup_config(myself->connection->config_tree, "MaxTimeout"), - &maxtimeout)) { + if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) { if(maxtimeout <= 0) { logger(LOG_ERR, _("Bogus maximum timeout!")); - return -1; + return false; } } else maxtimeout = 900; @@ -332,7 +322,7 @@ int setup_myself(void) addressfamily = AF_UNSPEC; else { logger(LOG_ERR, _("Invalid address family!")); - return -1; + return false; } free(afname); } @@ -350,7 +340,7 @@ int setup_myself(void) if(!myself->cipher) { logger(LOG_ERR, _("Unrecognized cipher type!")); - return -1; + return false; } } } else @@ -385,7 +375,7 @@ int setup_myself(void) if(!myself->digest) { logger(LOG_ERR, _("Unrecognized digest type!")); - return -1; + return false; } } } else @@ -399,10 +389,10 @@ int setup_myself(void) if(myself->digest) { if(myself->maclength > myself->digest->md_size) { logger(LOG_ERR, _("MAC length exceeds size of digest!")); - return -1; + return false; } else if(myself->maclength < 0) { logger(LOG_ERR, _("Bogus MAC length!")); - return -1; + return false; } } } else @@ -417,7 +407,7 @@ int setup_myself(void) &myself->compression)) { if(myself->compression < 0 || myself->compression > 11) { logger(LOG_ERR, _("Bogus compression level!")); - return -1; + return false; } } else myself->compression = 0; @@ -428,8 +418,8 @@ int setup_myself(void) myself->nexthop = myself; myself->via = myself; - myself->status.active = 1; - myself->status.reachable = 1; + myself->status.active = true; + myself->status.reachable = true; node_add(myself); graph(); @@ -450,7 +440,7 @@ int setup_myself(void) if(err || !ai) { logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err)); - return -1; + return false; } listen_sockets = 0; @@ -484,16 +474,16 @@ int setup_myself(void) logger(LOG_NOTICE, _("Ready")); else { logger(LOG_ERR, _("Unable to create any listening socket!")); - return -1; + return false; } - return 0; + return true; } /* setup all initial network connections */ -int setup_network_connections(void) +bool setup_network_connections(void) { char *envp[5]; int i; @@ -516,11 +506,11 @@ int setup_network_connections(void) } else pingtimeout = 60; - if(setup_device() < 0) - return -1; + if(!setup_device()) + return false; - if(setup_myself() < 0) - return -1; + if(!setup_myself()) + return false; /* Run tinc-up script to further initialize the tap interface */ asprintf(&envp[0], "NETNAME=%s", netname ? : ""); @@ -536,7 +526,7 @@ int setup_network_connections(void) try_outgoing_connections(); - return 0; + return true; } /* @@ -557,11 +547,11 @@ void close_network_connections(void) if(c->outgoing) free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL; - terminate_connection(c, 0); + terminate_connection(c, false); } if(myself && myself->connection) - terminate_connection(myself->connection, 0); + terminate_connection(myself->connection, false); for(i = 0; i < listen_sockets; i++) { close(listen_socket[i].tcp); diff --git a/src/net_socket.c b/src/net_socket.c index 82875bba..98f5d1a2 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_socket.c,v 1.1.2.29 2003/07/18 13:45:06 guus Exp $ + $Id: net_socket.c,v 1.1.2.30 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -221,7 +221,7 @@ begin: if(!c->outgoing->cfg) { ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"), c->name); - c->status.remove = 1; + c->status.remove = true; retry_outgoing(c->outgoing); return; } @@ -292,7 +292,7 @@ begin: if(result == -1) { if(errno == EINPROGRESS) { - c->status.connecting = 1; + c->status.connecting = true; return; } @@ -357,7 +357,7 @@ void setup_outgoing_connection(outgoing_t *outgoing) accept a new tcp connect and create a new connection */ -int handle_new_meta_connection(int sock) +bool handle_new_meta_connection(int sock) { connection_t *c; sockaddr_t sa; @@ -370,7 +370,7 @@ int handle_new_meta_connection(int sock) if(fd < 0) { logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno)); - return -1; + return false; } sockaddrunmap(&sa); @@ -393,7 +393,7 @@ int handle_new_meta_connection(int sock) c->allow_request = ID; send_id(c); - return 0; + return true; } void try_outgoing_connections(void) @@ -408,7 +408,7 @@ void try_outgoing_connections(void) cfg = lookup_config_next(config_tree, cfg)) { get_config_string(cfg, &name); - if(check_id(name)) { + if(!check_id(name)) { logger(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line); diff --git a/src/netbsd/device.c b/src/netbsd/device.c index 0064fdef..d0998339 100644 --- a/src/netbsd/device.c +++ b/src/netbsd/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.11 2003/07/18 13:41:36 guus Exp $ + $Id: device.c,v 1.1.2.12 2003/07/22 20:55:21 guus Exp $ */ #include "system.h" @@ -33,7 +33,6 @@ #define DEVICE_TYPE_TUNTAP 1 int device_fd = -1; -int device_type; char *device; char *iface; char *device_info; @@ -41,7 +40,7 @@ char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { cp(); @@ -52,14 +51,14 @@ int setup_device(void) iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } device_info = _("NetBSD tun device"); logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -69,7 +68,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -78,7 +77,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->data[12] = 0x08; @@ -92,10 +91,10 @@ int read_packet(vpn_packet_t *packet) device_info); } - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -105,10 +104,12 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; + + return true; } void dump_device_stats(void) diff --git a/src/netutl.c b/src/netutl.c index 6784833d..0d34d8d6 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: netutl.c,v 1.12.4.47 2003/07/17 15:06:26 guus Exp $ + $Id: netutl.c,v 1.12.4.48 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -28,7 +28,7 @@ #include "utils.h" #include "xalloc.h" -int hostnames = 0; +bool hostnames = false; /* Turn a string into a struct addrinfo. @@ -244,7 +244,7 @@ void maskcpy(void *va, void *vb, int masklen, int len) a[i] = 0; } -int maskcheck(void *va, int masklen, int len) +bool maskcheck(void *va, int masklen, int len) { int i; char *a = va; @@ -255,11 +255,11 @@ int maskcheck(void *va, int masklen, int len) masklen %= 8; if(masklen && a[i++] & (0xff >> masklen)) - return -1; + return false; for(; i < len; i++) if(a[i] != 0) - return -2; + return false; - return 0; + return true; } diff --git a/src/netutl.h b/src/netutl.h index eb5aeebc..66a7c972 100644 --- a/src/netutl.h +++ b/src/netutl.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: netutl.h,v 1.2.4.16 2003/07/17 15:06:26 guus Exp $ + $Id: netutl.h,v 1.2.4.17 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_NETUTL_H__ @@ -25,7 +25,7 @@ #include "net.h" -extern int hostnames; +extern bool hostnames; extern struct addrinfo *str2addrinfo(char *, char *, int); extern sockaddr_t str2sockaddr(char *, char *); @@ -36,6 +36,6 @@ extern void sockaddrunmap(sockaddr_t *); extern int maskcmp(void *, void *, int, int); extern void maskcpy(void *, void *, int, int); extern void mask(void *, int, int); -extern int maskcheck(void *, int, int); +extern bool maskcheck(void *, int, int); #endif /* __TINC_NETUTL_H__ */ diff --git a/src/node.h b/src/node.h index a4e439a7..3dd112e6 100644 --- a/src/node.h +++ b/src/node.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: node.h,v 1.1.2.25 2003/07/17 15:06:26 guus Exp $ + $Id: node.h,v 1.1.2.26 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_NODE_H__ @@ -45,7 +45,7 @@ typedef struct node_t { sockaddr_t address; /* his real (internet) ip to send UDP packets to */ char *hostname; /* the hostname of its real ip */ - struct node_status_t status; + node_status_t status; const EVP_CIPHER *cipher; /* Cipher type for UDP packets */ char *key; /* Cipher key and iv */ diff --git a/src/openbsd/device.c b/src/openbsd/device.c index 9af2e3e3..e7670300 100644 --- a/src/openbsd/device.c +++ b/src/openbsd/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.16 2003/07/18 13:41:36 guus Exp $ + $Id: device.c,v 1.1.2.17 2003/07/22 20:55:21 guus Exp $ */ #include "system.h" @@ -33,7 +33,6 @@ #define DEVICE_TYPE_TUNTAP 1 int device_fd = -1; -int device_type; char *device; char *iface; char *device_info; @@ -41,7 +40,7 @@ char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { cp(); @@ -52,14 +51,14 @@ int setup_device(void) iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } device_info = _("OpenBSD tun device"); logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -69,7 +68,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; u_int32_t type; @@ -80,7 +79,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = readv(device_fd, vector, 2)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } switch (ntohl(type)) { @@ -98,7 +97,7 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_ERR, _ ("Unknown address family %d while reading packet from %s %s"), ntohl(type), device_info, device); - return -1; + return false; } packet->len = lenin + 10; @@ -109,10 +108,10 @@ int read_packet(vpn_packet_t *packet) device_info); } - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { u_int32_t type; struct iovec vector[2]; @@ -136,7 +135,7 @@ int write_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_ERR, _("Unknown address family %d while writing packet to %s %s"), af, device_info, device); - return -1; + return false; } vector[0].iov_base = &type; @@ -147,10 +146,12 @@ int write_packet(vpn_packet_t *packet) if(writev(device_fd, vector, 2) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; + + return true; } void dump_device_stats(void) diff --git a/src/process.c b/src/process.c index 9a918158..218df760 100644 --- a/src/process.c +++ b/src/process.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.c,v 1.1.2.56 2003/07/21 14:47:43 guus Exp $ + $Id: process.c,v 1.1.2.57 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -35,21 +35,19 @@ #include "xalloc.h" /* If zero, don't detach from the terminal. */ -int do_detach = 1; +bool do_detach = true; +bool sighup = false; +bool sigalrm = false; extern char *identname; extern char *pidfilename; extern char **g_argv; -extern int use_logfile; +extern bool use_logfile; sigset_t emptysigset; static int saved_debug_level = -1; -extern int sighup; -extern int sigalrm; -extern int do_purge; - static void memory_full(int size) { logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size); @@ -93,7 +91,7 @@ void cleanup_and_exit(int c) /* check for an existing tinc for this net, and write pid to pidfile */ -static int write_pidfile(void) +static bool write_pidfile(void) { int pid; @@ -107,20 +105,20 @@ static int write_pidfile(void) netname, pid); else fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid); - return 1; + return false; } /* if it's locked, write-protected, or whatever */ if(!write_pid(pidfilename)) - return 1; + return false; - return 0; + return true; } /* kill older tincd for this net */ -int kill_other(int signal) +bool kill_other(int signal) { int pid; @@ -134,7 +132,7 @@ int kill_other(int signal) netname); else fprintf(stderr, _("No other tincd is running.\n")); - return 1; + return false; } errno = 0; /* No error, sometimes errno is only changed on error */ @@ -151,13 +149,13 @@ int kill_other(int signal) remove_pid(pidfilename); } - return 0; + return true; } /* Detach from current terminal, write pidfile, kill parent */ -int detach(void) +bool detach(void) { cp(); @@ -165,24 +163,24 @@ int detach(void) /* First check if we can open a fresh new pidfile */ - if(write_pidfile()) - return -1; + if(!write_pidfile()) + return false; /* If we succeeded in doing that, detach */ closelogger(); if(do_detach) { - if(daemon(0, 0) < 0) { + if(daemon(0, 0)) { fprintf(stderr, _("Couldn't detach from terminal: %s"), strerror(errno)); - return -1; + return false; } /* Now UPDATE the pid in the pidfile, because we changed it... */ if(!write_pid(pidfilename)) - return -1; + return false; } openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR)); @@ -192,7 +190,7 @@ int detach(void) xalloc_fail_func = memory_full; - return 0; + return true; } /* @@ -230,7 +228,7 @@ static void _execute_script(const char *scriptname, char **envp) /* Fork and execute the program pointed to by name. */ -int execute_script(const char *name, char **envp) +bool execute_script(const char *name, char **envp) { pid_t pid; int status; @@ -244,14 +242,14 @@ int execute_script(const char *name, char **envp) /* First check if there is a script */ if(stat(scriptname, &s)) - return 0; + return true; pid = fork(); if(pid < 0) { logger(LOG_ERR, _("System call `%s' failed: %s"), "fork", strerror(errno)); - return -1; + return false; } if(pid) { @@ -264,26 +262,26 @@ int execute_script(const char *name, char **envp) if(WEXITSTATUS(status)) { logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status)); - return -1; + return false; } else - return 0; + return true; } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */ logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid, name, WTERMSIG(status), strsignal(WTERMSIG(status))); - return -1; + return false; } else { /* Something strange happened */ logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name); - return -1; + return false; } } else if (errno != EINTR) { logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid", strerror(errno)); - return -1; + return false; } /* Why do we get EINTR? */ - return 0; + return true; } /* Child here */ @@ -344,7 +342,7 @@ static RETSIGTYPE fatal_signal_handler(int a) static RETSIGTYPE sighup_handler(int a) { logger(LOG_NOTICE, _("Got HUP signal")); - sighup = 1; + sighup = true; } static RETSIGTYPE sigint_handler(int a) @@ -366,7 +364,7 @@ static RETSIGTYPE sigint_handler(int a) static RETSIGTYPE sigalrm_handler(int a) { logger(LOG_NOTICE, _("Got ALRM signal")); - sigalrm = 1; + sigalrm = true; } static RETSIGTYPE sigusr1_handler(int a) @@ -384,8 +382,7 @@ static RETSIGTYPE sigusr2_handler(int a) static RETSIGTYPE sigwinch_handler(int a) { - extern int do_purge; - do_purge = 1; + do_purge = true; } static RETSIGTYPE unexpected_signal_handler(int a) @@ -403,21 +400,20 @@ static struct { int signal; void (*handler)(int); } sighandlers[] = { - { - SIGHUP, sighup_handler}, { - SIGTERM, sigterm_handler}, { - SIGQUIT, sigquit_handler}, { - SIGSEGV, fatal_signal_handler}, { - SIGBUS, fatal_signal_handler}, { - SIGILL, fatal_signal_handler}, { - SIGPIPE, ignore_signal_handler}, { - SIGINT, sigint_handler}, { - SIGUSR1, sigusr1_handler}, { - SIGUSR2, sigusr2_handler}, { - SIGCHLD, ignore_signal_handler}, { - SIGALRM, sigalrm_handler}, { - SIGWINCH, sigwinch_handler}, { - 0, NULL} + {SIGHUP, sighup_handler}, + {SIGTERM, sigterm_handler}, + {SIGQUIT, sigquit_handler}, + {SIGSEGV, fatal_signal_handler}, + {SIGBUS, fatal_signal_handler}, + {SIGILL, fatal_signal_handler}, + {SIGPIPE, ignore_signal_handler}, + {SIGINT, sigint_handler}, + {SIGUSR1, sigusr1_handler}, + {SIGUSR2, sigusr2_handler}, + {SIGCHLD, ignore_signal_handler}, + {SIGALRM, sigalrm_handler}, + {SIGWINCH, sigwinch_handler}, + {0, NULL} }; void setup_signals(void) diff --git a/src/process.h b/src/process.h index cb5f7c7c..9b0b123f 100644 --- a/src/process.h +++ b/src/process.h @@ -17,18 +17,20 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.h,v 1.1.2.16 2003/07/17 15:06:26 guus Exp $ + $Id: process.h,v 1.1.2.17 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_PROCESS_H__ #define __TINC_PROCESS_H__ -extern int do_detach; +extern bool do_detach; +extern bool sighup; +extern bool sigalrm; extern void setup_signals(void); -extern int execute_script(const char *, char **); -extern int detach(void); -extern int kill_other(int); +extern bool execute_script(const char *, char **); +extern bool detach(void); +extern bool kill_other(int); extern void cleanup_and_exit(int) __attribute__ ((noreturn)); #endif /* __TINC_PROCESS_H__ */ diff --git a/src/protocol.c b/src/protocol.c index 47cc9487..ad7c57d7 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol.c,v 1.28.4.141 2003/07/17 15:06:26 guus Exp $ + $Id: protocol.c,v 1.28.4.142 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -32,7 +32,7 @@ /* Jumptable for the request handlers */ -static int (*request_handlers[])(connection_t *) = { +static bool (*request_handlers[])(connection_t *) = { id_h, metakey_h, challenge_h, chal_reply_h, ack_h, status_h, error_h, termreq_h, ping_h, pong_h, @@ -53,21 +53,21 @@ static char (*request_name[]) = { static avl_tree_t *past_request_tree; -int check_id(char *id) +bool check_id(char *id) { int i; for(i = 0; i < strlen(id); i++) if(!isalnum(id[i]) && id[i] != '_') - return -1; + return false; - return 0; + return true; } /* Generic request routines - takes care of logging and error detection as well */ -int send_request(connection_t *c, const char *format, ...) +bool send_request(connection_t *c, const char *format, ...) { va_list args; char buffer[MAXBUFSIZE]; @@ -86,7 +86,7 @@ int send_request(connection_t *c, const char *format, ...) if(len < 0 || len > MAXBUFSIZE - 1) { logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"), c->name, c->hostname); - return -1; + return false; } ifdebug(PROTOCOL) { @@ -101,16 +101,16 @@ int send_request(connection_t *c, const char *format, ...) buffer[len++] = '\n'; - if(c == broadcast) - return broadcast_meta(NULL, buffer, len); - else + if(c == broadcast) { + broadcast_meta(NULL, buffer, len); + return true; + } else return send_meta(c, buffer, len); } -int forward_request(connection_t *from) +void forward_request(connection_t *from) { int request; - cp(); cp(); @@ -127,10 +127,10 @@ int forward_request(connection_t *from) from->buffer[from->reqlen - 1] = '\n'; - return broadcast_meta(from, from->buffer, from->reqlen); + broadcast_meta(from, from->buffer, from->reqlen); } -int receive_request(connection_t *c) +bool receive_request(connection_t *c) { int request; @@ -145,7 +145,7 @@ int receive_request(connection_t *c) logger(LOG_ERR, _("Unknown request from %s (%s)"), c->name, c->hostname); - return -1; + return false; } else { ifdebug(PROTOCOL) { ifdebug(META) @@ -161,23 +161,23 @@ int receive_request(connection_t *c) if((c->allow_request != ALL) && (c->allow_request != request)) { logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname); - return -1; + return false; } - if(request_handlers[request] (c)) + if(!request_handlers[request](c)) { /* Something went wrong. Probably scriptkiddies. Terminate. */ - { + logger(LOG_ERR, _("Error while processing %s from %s (%s)"), request_name[request], c->name, c->hostname); - return -1; + return false; } } else { logger(LOG_ERR, _("Bogus data received from %s (%s)"), c->name, c->hostname); - return -1; + return false; } - return 0; + return true; } static int past_request_compare(past_request_t *a, past_request_t *b) @@ -209,7 +209,7 @@ void exit_requests(void) avl_delete_tree(past_request_tree); } -int seen_request(char *request) +bool seen_request(char *request) { past_request_t p, *new; @@ -219,13 +219,13 @@ int seen_request(char *request) if(avl_search(past_request_tree, &p)) { ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request")); - return 1; + return true; } else { new = (past_request_t *) xmalloc(sizeof(*new)); new->request = xstrdup(request); new->firstseen = now; avl_insert(past_request_tree, new); - return 0; + return false; } } diff --git a/src/protocol.h b/src/protocol.h index 6f01a76c..132a8e6e 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -17,17 +17,12 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol.h,v 1.5.4.39 2003/07/17 15:06:26 guus Exp $ + $Id: protocol.h,v 1.5.4.40 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_PROTOCOL_H__ #define __TINC_PROTOCOL_H__ -#include "edge.h" -#include "net.h" -#include "node.h" -#include "subnet.h" - /* Protocol version. Different versions are incompatible, incompatible version have different protocols. */ @@ -36,7 +31,7 @@ /* Request numbers */ -enum { +typedef enum request_t { ALL = -1, /* Guardian for allow_request */ ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK, STATUS, ERROR, TERMREQ, @@ -46,7 +41,7 @@ enum { KEY_CHANGED, REQ_KEY, ANS_KEY, PACKET, LAST /* Guardian for the highest request number */ -}; +} request_t; typedef struct past_request_t { char *request; @@ -58,58 +53,63 @@ typedef struct past_request_t { #define MAX_STRING_SIZE 2048 #define MAX_STRING "%2048s" +#include "edge.h" +#include "net.h" +#include "node.h" +#include "subnet.h" + /* Basic functions */ -extern int send_request(connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3))); -extern int forward_request(connection_t *); -extern int receive_request(connection_t *); -extern int check_id(char *); +extern bool send_request(struct connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3))); +extern void forward_request(struct connection_t *); +extern bool receive_request(struct connection_t *); +extern bool check_id(char *); extern void init_requests(void); extern void exit_requests(void); -extern int seen_request(char *); +extern bool seen_request(char *); extern void age_past_requests(void); /* Requests */ -extern int send_id(connection_t *); -extern int send_metakey(connection_t *); -extern int send_challenge(connection_t *); -extern int send_chal_reply(connection_t *); -extern int send_ack(connection_t *); -extern int send_status(connection_t *, int, char *); -extern int send_error(connection_t *, int, char *); -extern int send_termreq(connection_t *); -extern int send_ping(connection_t *); -extern int send_pong(connection_t *); -extern int send_add_subnet(connection_t *, subnet_t *); -extern int send_del_subnet(connection_t *, subnet_t *); -extern int send_add_edge(connection_t *, edge_t *); -extern int send_del_edge(connection_t *, edge_t *); -extern int send_key_changed(connection_t *, node_t *); -extern int send_req_key(connection_t *, node_t *, node_t *); -extern int send_ans_key(connection_t *, node_t *, node_t *); -extern int send_tcppacket(connection_t *, vpn_packet_t *); +extern bool send_id(struct connection_t *); +extern bool send_metakey(struct connection_t *); +extern bool send_challenge(struct connection_t *); +extern bool send_chal_reply(struct connection_t *); +extern bool send_ack(struct connection_t *); +extern bool send_status(struct connection_t *, int, char *); +extern bool send_error(struct connection_t *, int, char *); +extern bool send_termreq(struct connection_t *); +extern bool send_ping(struct connection_t *); +extern bool send_pong(struct connection_t *); +extern bool send_add_subnet(struct connection_t *, struct subnet_t *); +extern bool send_del_subnet(struct connection_t *, struct subnet_t *); +extern bool send_add_edge(struct connection_t *, struct edge_t *); +extern bool send_del_edge(struct connection_t *, struct edge_t *); +extern bool send_key_changed(struct connection_t *, struct node_t *); +extern bool send_req_key(struct connection_t *, struct node_t *, struct node_t *); +extern bool send_ans_key(struct connection_t *, struct node_t *, struct node_t *); +extern bool send_tcppacket(struct connection_t *, struct vpn_packet_t *); /* Request handlers */ -extern int id_h(connection_t *); -extern int metakey_h(connection_t *); -extern int challenge_h(connection_t *); -extern int chal_reply_h(connection_t *); -extern int ack_h(connection_t *); -extern int status_h(connection_t *); -extern int error_h(connection_t *); -extern int termreq_h(connection_t *); -extern int ping_h(connection_t *); -extern int pong_h(connection_t *); -extern int add_subnet_h(connection_t *); -extern int del_subnet_h(connection_t *); -extern int add_edge_h(connection_t *); -extern int del_edge_h(connection_t *); -extern int key_changed_h(connection_t *); -extern int req_key_h(connection_t *); -extern int ans_key_h(connection_t *); -extern int tcppacket_h(connection_t *); +extern bool id_h(struct connection_t *); +extern bool metakey_h(struct connection_t *); +extern bool challenge_h(struct connection_t *); +extern bool chal_reply_h(struct connection_t *); +extern bool ack_h(struct connection_t *); +extern bool status_h(struct connection_t *); +extern bool error_h(struct connection_t *); +extern bool termreq_h(struct connection_t *); +extern bool ping_h(struct connection_t *); +extern bool pong_h(struct connection_t *); +extern bool add_subnet_h(struct connection_t *); +extern bool del_subnet_h(struct connection_t *); +extern bool add_edge_h(struct connection_t *); +extern bool del_edge_h(struct connection_t *); +extern bool key_changed_h(struct connection_t *); +extern bool req_key_h(struct connection_t *); +extern bool ans_key_h(struct connection_t *); +extern bool tcppacket_h(struct connection_t *); #endif /* __TINC_PROTOCOL_H__ */ diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 88fba266..d2962e72 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_auth.c,v 1.1.4.24 2003/07/17 15:06:26 guus Exp $ + $Id: protocol_auth.c,v 1.1.4.25 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -39,7 +39,7 @@ #include "utils.h" #include "xalloc.h" -int send_id(connection_t *c) +bool send_id(connection_t *c) { cp(); @@ -47,25 +47,25 @@ int send_id(connection_t *c) myself->connection->protocol_version); } -int id_h(connection_t *c) +bool id_h(connection_t *c) { char name[MAX_STRING_SIZE]; - int bla; + bool choice; cp(); if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname); - return -1; + return false; } /* Check if identity is a valid name */ - if(check_id(name)) { + if(!check_id(name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name"); - return -1; + return false; } /* If we set c->name in advance, make sure we are connected to the right host */ @@ -74,7 +74,7 @@ int id_h(connection_t *c) if(strcmp(c->name, name)) { logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name); - return -1; + return false; } } else c->name = xstrdup(name); @@ -84,7 +84,7 @@ int id_h(connection_t *c) if(c->protocol_version != myself->connection->protocol_version) { logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"), c->name, c->hostname, c->protocol_version); - return -1; + return false; } if(bypass_security) { @@ -97,25 +97,23 @@ int id_h(connection_t *c) if(!c->config_tree) { init_configuration(&c->config_tree); - bla = read_connection_config(c); - - if(bla) { + if(!read_connection_config(c)) { logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name); - return -1; + return false; } } - if(read_rsa_public_key(c)) { - return -1; + if(!read_rsa_public_key(c)) { + return false; } /* Check some options */ - if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &bla) && bla) || myself->options & OPTION_INDIRECT) + if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) c->options |= OPTION_INDIRECT; - if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &bla) && bla) || myself->options & OPTION_TCPONLY) + if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) c->options |= OPTION_TCPONLY | OPTION_INDIRECT; c->allow_request = METAKEY; @@ -123,10 +121,11 @@ int id_h(connection_t *c) return send_metakey(c); } -int send_metakey(connection_t *c) +bool send_metakey(connection_t *c) { char buffer[MAX_STRING_SIZE]; - int len, x; + int len; + bool x; cp(); @@ -173,7 +172,7 @@ int send_metakey(connection_t *c) if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len) { logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname); - return -1; + return false; } /* Convert the encrypted random data to a hexadecimal formatted string */ @@ -196,13 +195,13 @@ int send_metakey(connection_t *c) c->outkey + len - c->outcipher->key_len - c->outcipher->iv_len); - c->status.encryptout = 1; + c->status.encryptout = true; } return x; } -int metakey_h(connection_t *c) +bool metakey_h(connection_t *c) { char buffer[MAX_STRING_SIZE]; int cipher, digest, maclength, compression; @@ -213,7 +212,7 @@ int metakey_h(connection_t *c) if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname); - return -1; + return false; } len = RSA_size(myself->connection->rsa_key); @@ -222,7 +221,7 @@ int metakey_h(connection_t *c) if(strlen(buffer) != len * 2) { logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength"); - return -1; + return false; } /* Allocate buffers for the meta key */ @@ -242,7 +241,7 @@ int metakey_h(connection_t *c) if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) { /* See challenge() */ logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname); - return -1; + return false; } ifdebug(SCARY_THINGS) { @@ -260,7 +259,7 @@ int metakey_h(connection_t *c) if(!c->incipher) { logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname); - return -1; + return false; } EVP_DecryptInit(c->inctx, c->incipher, @@ -268,7 +267,7 @@ int metakey_h(connection_t *c) c->inkey + len - c->incipher->key_len - c->incipher->iv_len); - c->status.decryptin = 1; + c->status.decryptin = true; } else { c->incipher = NULL; } @@ -280,12 +279,12 @@ int metakey_h(connection_t *c) if(!c->indigest) { logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname); - return -1; + return false; } if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0) { logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname); - return -1; + return false; } } else { c->indigest = NULL; @@ -298,10 +297,10 @@ int metakey_h(connection_t *c) return send_challenge(c); } -int send_challenge(connection_t *c) +bool send_challenge(connection_t *c) { char buffer[MAX_STRING_SIZE]; - int len, x; + int len; cp(); @@ -325,12 +324,10 @@ int send_challenge(connection_t *c) /* Send the challenge */ - x = send_request(c, "%d %s", CHALLENGE, buffer); - - return x; + return send_request(c, "%d %s", CHALLENGE, buffer); } -int challenge_h(connection_t *c) +bool challenge_h(connection_t *c) { char buffer[MAX_STRING_SIZE]; int len; @@ -340,7 +337,7 @@ int challenge_h(connection_t *c) if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname); - return -1; + return false; } len = RSA_size(myself->connection->rsa_key); @@ -350,7 +347,7 @@ int challenge_h(connection_t *c) if(strlen(buffer) != len * 2) { logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length"); - return -1; + return false; } /* Allocate buffers for the challenge */ @@ -369,7 +366,7 @@ int challenge_h(connection_t *c) return send_chal_reply(c); } -int send_chal_reply(connection_t *c) +bool send_chal_reply(connection_t *c) { char hash[EVP_MAX_MD_SIZE * 2 + 1]; EVP_MD_CTX ctx; @@ -393,7 +390,7 @@ int send_chal_reply(connection_t *c) return send_request(c, "%d %s", CHAL_REPLY, hash); } -int chal_reply_h(connection_t *c) +bool chal_reply_h(connection_t *c) { char hishash[MAX_STRING_SIZE]; char myhash[EVP_MAX_MD_SIZE]; @@ -404,7 +401,7 @@ int chal_reply_h(connection_t *c) if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname); - return -1; + return false; } /* Check if the length of the hash is all right */ @@ -412,7 +409,7 @@ int chal_reply_h(connection_t *c) if(strlen(hishash) != c->outdigest->md_size * 2) { logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length")); - return -1; + return false; } /* Convert the hash to binary format */ @@ -437,7 +434,7 @@ int chal_reply_h(connection_t *c) logger(LOG_DEBUG, _("Expected challenge reply: %s"), hishash); } - return -1; + return false; } /* Identity has now been positively verified. @@ -449,12 +446,11 @@ int chal_reply_h(connection_t *c) return send_ack(c); } -int send_ack(connection_t *c) +bool send_ack(connection_t *c) { /* ACK message contains rest of the information the other end needs to create node_t and edge_t structures. */ - int x; struct timeval now; cp(); @@ -462,13 +458,9 @@ int send_ack(connection_t *c) /* Estimate weight */ gettimeofday(&now, NULL); - c->estimated_weight = - (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - - c->start.tv_usec) / 1000; - x = send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, - c->options); + c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000; - return x; + return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options); } static void send_everything(connection_t *c) @@ -495,7 +487,7 @@ static void send_everything(connection_t *c) } } -int ack_h(connection_t *c) +bool ack_h(connection_t *c) { char hisport[MAX_STRING_SIZE]; char *hisaddress, *dummy; @@ -508,7 +500,7 @@ int ack_h(connection_t *c) if(sscanf(c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname); - return -1; + return false; } /* Check if we already have a node_t for him */ @@ -524,7 +516,7 @@ int ack_h(connection_t *c) /* Oh dear, we already have a connection to this node. */ ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname); - terminate_connection(n->connection, 0); + terminate_connection(n->connection, false); /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */ graph(); } @@ -537,7 +529,7 @@ int ack_h(connection_t *c) /* Activate this connection */ c->allow_request = ALL; - c->status.active = 1; + c->status.active = true; ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname); @@ -570,5 +562,5 @@ int ack_h(connection_t *c) graph(); - return 0; + return true; } diff --git a/src/protocol_edge.c b/src/protocol_edge.c index 75b49950..e1e2ca01 100644 --- a/src/protocol_edge.c +++ b/src/protocol_edge.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_edge.c,v 1.1.4.18 2003/07/17 15:06:26 guus Exp $ + $Id: protocol_edge.c,v 1.1.4.19 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -36,9 +36,9 @@ #include "utils.h" #include "xalloc.h" -int send_add_edge(connection_t *c, edge_t *e) +bool send_add_edge(connection_t *c, edge_t *e) { - int x; + bool x; char *address, *port; cp(); @@ -54,7 +54,7 @@ int send_add_edge(connection_t *c, edge_t *e) return x; } -int add_edge_h(connection_t *c) +bool add_edge_h(connection_t *c) { edge_t *e; node_t *from, *to; @@ -72,25 +72,25 @@ int add_edge_h(connection_t *c) from_name, to_name, to_address, to_port, &options, &weight) != 6) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname); - return -1; + return false; } /* Check if names are valid */ - if(check_id(from_name)) { + if(!check_id(from_name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name")); - return -1; + return false; } - if(check_id(to_name)) { + if(!check_id(to_name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name")); - return -1; + return false; } if(seen_request(c->buffer)) - return 0; + return true; /* Lookup nodes */ @@ -124,7 +124,7 @@ int add_edge_h(connection_t *c) ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname); send_add_edge(c, e); - return 0; + return true; } else { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname); @@ -132,7 +132,7 @@ int add_edge_h(connection_t *c) graph(); } } else - return 0; + return true; } else if(from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname); @@ -141,7 +141,7 @@ int add_edge_h(connection_t *c) e->to = to; send_del_edge(c, e); free_edge(e); - return 0; + return true; } e = new_edge(); @@ -160,10 +160,10 @@ int add_edge_h(connection_t *c) graph(); - return 0; + return true; } -int send_del_edge(connection_t *c, edge_t *e) +bool send_del_edge(connection_t *c, edge_t *e) { cp(); @@ -171,7 +171,7 @@ int send_del_edge(connection_t *c, edge_t *e) e->from->name, e->to->name); } -int del_edge_h(connection_t *c) +bool del_edge_h(connection_t *c) { edge_t *e; char from_name[MAX_STRING_SIZE]; @@ -183,25 +183,25 @@ int del_edge_h(connection_t *c) if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name, c->hostname); - return -1; + return false; } /* Check if names are valid */ - if(check_id(from_name)) { + if(!check_id(from_name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name")); - return -1; + return false; } - if(check_id(to_name)) { + if(!check_id(to_name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name")); - return -1; + return false; } if(seen_request(c->buffer)) - return 0; + return true; /* Lookup nodes */ @@ -210,7 +210,7 @@ int del_edge_h(connection_t *c) if(!from) { ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname); - return 0; + return true; } to = lookup_node(to_name); @@ -218,7 +218,7 @@ int del_edge_h(connection_t *c) if(!to) { ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname); - return 0; + return true; } /* Check if edge exists */ @@ -228,14 +228,14 @@ int del_edge_h(connection_t *c) if(!e) { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname); - return 0; + return true; } if(e->from == myself) { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname); send_add_edge(c, e); /* Send back a correction */ - return 0; + return true; } /* Tell the rest about the deleted edge */ @@ -250,5 +250,5 @@ int del_edge_h(connection_t *c) graph(); - return 0; + return true; } diff --git a/src/protocol_key.c b/src/protocol_key.c index 832b2af0..3b5cd467 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_key.c,v 1.1.4.19 2003/07/17 15:06:26 guus Exp $ + $Id: protocol_key.c,v 1.1.4.20 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -32,9 +32,9 @@ #include "utils.h" #include "xalloc.h" -int mykeyused = 0; +bool mykeyused = false; -int send_key_changed(connection_t *c, node_t *n) +bool send_key_changed(connection_t *c, node_t *n) { cp(); @@ -43,12 +43,12 @@ int send_key_changed(connection_t *c, node_t *n) */ if(n == myself && !mykeyused) - return 0; + return true; return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name); } -int key_changed_h(connection_t *c) +bool key_changed_h(connection_t *c) { char name[MAX_STRING_SIZE]; node_t *n; @@ -58,38 +58,38 @@ int key_changed_h(connection_t *c) if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED", c->name, c->hostname); - return -1; + return false; } if(seen_request(c->buffer)) - return 0; + return true; n = lookup_node(name); if(!n) { logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED", c->name, c->hostname, name); - return -1; + return false; } - n->status.validkey = 0; - n->status.waitingforkey = 0; + n->status.validkey = false; + n->status.waitingforkey = false; /* Tell the others */ forward_request(c); - return 0; + return true; } -int send_req_key(connection_t *c, node_t *from, node_t *to) +bool send_req_key(connection_t *c, node_t *from, node_t *to) { cp(); return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name); } -int req_key_h(connection_t *c) +bool req_key_h(connection_t *c) { char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; @@ -100,7 +100,7 @@ int req_key_h(connection_t *c) if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name, c->hostname); - return -1; + return false; } from = lookup_node(from_name); @@ -108,7 +108,7 @@ int req_key_h(connection_t *c) if(!from) { logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY", c->name, c->hostname, from_name); - return -1; + return false; } to = lookup_node(to_name); @@ -116,13 +116,13 @@ int req_key_h(connection_t *c) if(!to) { logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY", c->name, c->hostname, to_name); - return -1; + return false; } /* Check if this key request is for us */ if(to == myself) { /* Yes, send our own key back */ - mykeyused = 1; + mykeyused = true; from->received_seqno = 0; memset(from->late, 0, sizeof(from->late)); send_ans_key(c, myself, from); @@ -130,10 +130,10 @@ int req_key_h(connection_t *c) send_req_key(to->nexthop->connection, from, to); } - return 0; + return true; } -int send_ans_key(connection_t *c, node_t *from, node_t *to) +bool send_ans_key(connection_t *c, node_t *from, node_t *to) { char key[MAX_STRING_SIZE]; @@ -149,7 +149,7 @@ int send_ans_key(connection_t *c, node_t *from, node_t *to) from->compression); } -int ans_key_h(connection_t *c) +bool ans_key_h(connection_t *c) { char from_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE]; @@ -164,7 +164,7 @@ int ans_key_h(connection_t *c) &compression) != 7) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name, c->hostname); - return -1; + return false; } from = lookup_node(from_name); @@ -172,7 +172,7 @@ int ans_key_h(connection_t *c) if(!from) { logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY", c->name, c->hostname, from_name); - return -1; + return false; } to = lookup_node(to_name); @@ -180,7 +180,7 @@ int ans_key_h(connection_t *c) if(!to) { logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY", c->name, c->hostname, to_name); - return -1; + return false; } /* Forward it if necessary */ @@ -199,8 +199,8 @@ int ans_key_h(connection_t *c) hex2bin(from->key, from->key, from->keylength); from->key[from->keylength] = '\0'; - from->status.validkey = 1; - from->status.waitingforkey = 0; + from->status.validkey = true; + from->status.waitingforkey = false; from->sent_seqno = 0; /* Check and lookup cipher and digest algorithms */ @@ -211,13 +211,13 @@ int ans_key_h(connection_t *c) if(!from->cipher) { logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname); - return -1; + return false; } if(from->keylength != from->cipher->key_len + from->cipher->iv_len) { logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname); - return -1; + return false; } } else { from->cipher = NULL; @@ -231,13 +231,13 @@ int ans_key_h(connection_t *c) if(!from->digest) { logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname); - return -1; + return false; } if(from->maclength > from->digest->md_size || from->maclength < 0) { logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname); - return -1; + return false; } } else { from->digest = NULL; @@ -245,7 +245,7 @@ int ans_key_h(connection_t *c) if(compression < 0 || compression > 11) { logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname); - return -1; + return false; } from->compression = compression; @@ -254,5 +254,5 @@ int ans_key_h(connection_t *c) flush_queue(from); - return 0; + return true; } diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 934a1820..25a86abe 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_misc.c,v 1.1.4.11 2003/07/17 15:06:26 guus Exp $ + $Id: protocol_misc.c,v 1.1.4.12 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -33,7 +33,7 @@ /* Status and error notification routines */ -int send_status(connection_t *c, int statusno, char *statusstring) +bool send_status(connection_t *c, int statusno, char *statusstring) { cp(); @@ -43,7 +43,7 @@ int send_status(connection_t *c, int statusno, char *statusstring) return send_request(c, "%d %d %s", STATUS, statusno, statusstring); } -int status_h(connection_t *c) +bool status_h(connection_t *c) { int statusno; char statusstring[MAX_STRING_SIZE]; @@ -53,16 +53,16 @@ int status_h(connection_t *c) if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS", c->name, c->hostname); - return -1; + return false; } ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"), c->name, c->hostname, statusno, statusstring); - return 0; + return true; } -int send_error(connection_t *c, int err, char *errstring) +bool send_error(connection_t *c, int err, char *errstring) { cp(); @@ -72,7 +72,7 @@ int send_error(connection_t *c, int err, char *errstring) return send_request(c, "%d %d %s", ERROR, err, errstring); } -int error_h(connection_t *c) +bool error_h(connection_t *c) { int err; char errorstring[MAX_STRING_SIZE]; @@ -82,7 +82,7 @@ int error_h(connection_t *c) if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR", c->name, c->hostname); - return -1; + return false; } ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"), @@ -90,82 +90,78 @@ int error_h(connection_t *c) terminate_connection(c, c->status.active); - return 0; + return true; } -int send_termreq(connection_t *c) +bool send_termreq(connection_t *c) { cp(); return send_request(c, "%d", TERMREQ); } -int termreq_h(connection_t *c) +bool termreq_h(connection_t *c) { cp(); terminate_connection(c, c->status.active); - return 0; + return true; } -int send_ping(connection_t *c) +bool send_ping(connection_t *c) { cp(); - c->status.pinged = 1; + c->status.pinged = true; c->last_ping_time = now; return send_request(c, "%d", PING); } -int ping_h(connection_t *c) +bool ping_h(connection_t *c) { cp(); return send_pong(c); } -int send_pong(connection_t *c) +bool send_pong(connection_t *c) { cp(); return send_request(c, "%d", PONG); } -int pong_h(connection_t *c) +bool pong_h(connection_t *c) { cp(); - c->status.pinged = 0; + c->status.pinged = false; /* Succesful connection, reset timeout if this is an outgoing connection. */ if(c->outgoing) c->outgoing->timeout = 0; - return 0; + return true; } /* Sending and receiving packets via TCP */ -int send_tcppacket(connection_t *c, vpn_packet_t *packet) +bool send_tcppacket(connection_t *c, vpn_packet_t *packet) { - int x; - cp(); /* Evil hack. */ - x = send_request(c, "%d %hd", PACKET, packet->len); - - if(x) - return x; + if(!send_request(c, "%d %hd", PACKET, packet->len)) + return false; return send_meta(c, packet->data, packet->len); } -int tcppacket_h(connection_t *c) +bool tcppacket_h(connection_t *c) { short int len; @@ -174,12 +170,12 @@ int tcppacket_h(connection_t *c) if(sscanf(c->buffer, "%*d %hd", &len) != 1) { logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname); - return -1; + return false; } /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */ c->tcplen = len; - return 0; + return true; } diff --git a/src/protocol_subnet.c b/src/protocol_subnet.c index 79fbb417..48b5efef 100644 --- a/src/protocol_subnet.c +++ b/src/protocol_subnet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_subnet.c,v 1.1.4.13 2003/07/21 14:47:43 guus Exp $ + $Id: protocol_subnet.c,v 1.1.4.14 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -33,9 +33,9 @@ #include "utils.h" #include "xalloc.h" -int send_add_subnet(connection_t *c, subnet_t *subnet) +bool send_add_subnet(connection_t *c, subnet_t *subnet) { - int x; + bool x; char *netstr; cp(); @@ -48,7 +48,7 @@ int send_add_subnet(connection_t *c, subnet_t *subnet) return x; } -int add_subnet_h(connection_t *c) +bool add_subnet_h(connection_t *c) { char subnetstr[MAX_STRING_SIZE]; char name[MAX_STRING_SIZE]; @@ -60,15 +60,15 @@ int add_subnet_h(connection_t *c) 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, c->hostname); - return -1; + return false; } /* Check if owner name is a valid */ - if(check_id(name)) { + if(!check_id(name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name")); - return -1; + return false; } /* Check if subnet string is valid */ @@ -78,11 +78,11 @@ int add_subnet_h(connection_t *c) if(!s) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string")); - return -1; + return false; } if(seen_request(c->buffer)) - return 0; + return true; /* Check if the owner of the new subnet is in the connection list */ @@ -98,7 +98,7 @@ int add_subnet_h(connection_t *c) if(lookup_subnet(owner, s)) { free_subnet(s); - return 0; + return true; } /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */ @@ -108,7 +108,7 @@ int add_subnet_h(connection_t *c) "ADD_SUBNET", c->name, c->hostname); s->owner = myself; send_del_subnet(c, s); - return 0; + return true; } /* If everything is correct, add the subnet to the list of the owner */ @@ -119,12 +119,12 @@ int add_subnet_h(connection_t *c) forward_request(c); - return 0; + return true; } -int send_del_subnet(connection_t *c, subnet_t *s) +bool send_del_subnet(connection_t *c, subnet_t *s) { - int x; + bool x; char *netstr; cp(); @@ -138,7 +138,7 @@ int send_del_subnet(connection_t *c, subnet_t *s) return x; } -int del_subnet_h(connection_t *c) +bool del_subnet_h(connection_t *c) { char subnetstr[MAX_STRING_SIZE]; char name[MAX_STRING_SIZE]; @@ -150,15 +150,15 @@ int del_subnet_h(connection_t *c) 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, c->hostname); - return -1; + return false; } /* Check if owner name is a valid */ - if(check_id(name)) { + if(!check_id(name)) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name")); - return -1; + return false; } /* Check if the owner of the new subnet is in the connection list */ @@ -168,7 +168,7 @@ int del_subnet_h(connection_t *c) if(!owner) { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"), "DEL_SUBNET", c->name, c->hostname, name); - return 0; + return true; } /* Check if subnet string is valid */ @@ -178,11 +178,11 @@ int del_subnet_h(connection_t *c) if(!s) { logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string")); - return -1; + return false; } if(seen_request(c->buffer)) - return 0; + return true; /* If everything is correct, delete the subnet from the list of the owner */ @@ -195,7 +195,7 @@ int del_subnet_h(connection_t *c) if(!find) { ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"), "DEL_SUBNET", c->name, c->hostname, name); - return 0; + return true; } /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */ @@ -204,7 +204,7 @@ int del_subnet_h(connection_t *c) ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_SUBNET", c->name, c->hostname); send_add_subnet(c, find); - return 0; + return true; } /* Tell the rest */ @@ -215,5 +215,5 @@ int del_subnet_h(connection_t *c) subnet_del(owner, find); - return 0; + return true; } diff --git a/src/raw_socket/device.c b/src/raw_socket/device.c index 6a94ce38..77e0540d 100644 --- a/src/raw_socket/device.c +++ b/src/raw_socket/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.7 2003/07/12 17:41:48 guus Exp $ + $Id: device.c,v 1.1.2.8 2003/07/22 20:55:21 guus Exp $ */ #include "config.h" @@ -43,7 +43,6 @@ #include "system.h" int device_fd = -1; -int device_type; char *device; char *interface; char ifrname[IFNAMSIZ]; @@ -52,7 +51,7 @@ char *device_info; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { struct ifreq ifr; struct sockaddr_ll sa; @@ -71,7 +70,7 @@ int setup_device(void) if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device_info, strerror(errno)); - return -1; + return false; } memset(&ifr, 0, sizeof(ifr)); @@ -80,7 +79,7 @@ int setup_device(void) close(device_fd); logger(LOG_ERR, _("Can't find interface %s: %s"), interface, strerror(errno)); - return -1; + return false; } memset(&sa, '0', sizeof(sa)); @@ -90,12 +89,12 @@ int setup_device(void) if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) { logger(LOG_ERR, _("Could not bind to %s: %s"), device, strerror(errno)); - return -1; + return false; } logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -105,7 +104,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -114,7 +113,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(device_fd, packet->data, MTU)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->len = lenin; @@ -125,10 +124,10 @@ int read_packet(vpn_packet_t *packet) device_info); } - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -138,12 +137,12 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data, packet->len) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; - return 0; + return true; } void dump_device_stats(void) diff --git a/src/route.c b/src/route.c index 323edec2..7dd7635f 100644 --- a/src/route.c +++ b/src/route.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: route.c,v 1.1.2.61 2003/07/18 12:21:03 guus Exp $ + $Id: route.c,v 1.1.2.62 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -54,10 +54,10 @@ #include "subnet.h" #include "utils.h" -int routing_mode = RMODE_ROUTER; -int priorityinheritance = 0; +rmode_t routing_mode = RMODE_ROUTER; +bool priorityinheritance = false; int macexpire = 600; -int overwrite_mac = 0; +bool overwrite_mac = false; mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}}; /* RFC 1071 */ @@ -81,14 +81,14 @@ static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) return ~checksum; } -static int ratelimit(void) { +static bool ratelimit(void) { static time_t lasttime = 0; if(lasttime == now) - return 1; + return true; lasttime = now; - return 0; + return false; } static void learn_mac(mac_t *address) diff --git a/src/route.h b/src/route.h index 8daef5a4..6d1c034d 100644 --- a/src/route.h +++ b/src/route.h @@ -17,27 +17,30 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: route.h,v 1.1.2.12 2003/07/15 16:26:18 guus Exp $ + $Id: route.h,v 1.1.2.13 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_ROUTE_H__ #define __TINC_ROUTE_H__ -enum { +#include "net.h" +#include "node.h" + +typedef enum rmode_t { RMODE_HUB = 0, RMODE_SWITCH, RMODE_ROUTER, -}; +} rmode_t; -extern int routing_mode; -extern int overwrite_mac; -extern int priorityinheritance; +extern rmode_t routing_mode; +extern bool overwrite_mac; +extern bool priorityinheritance; extern int macexpire; extern mac_t mymac; extern void age_mac(void); -extern void route_incoming(node_t *, vpn_packet_t *); -extern void route_outgoing(vpn_packet_t *); +extern void route_incoming(struct node_t *, struct vpn_packet_t *); +extern void route_outgoing(struct vpn_packet_t *); #endif /* __TINC_ROUTE_H__ */ diff --git a/src/solaris/device.c b/src/solaris/device.c index 3cafa238..6b657273 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: device.c,v 1.1.2.15 2003/07/18 13:41:37 guus Exp $ + $Id: device.c,v 1.1.2.16 2003/07/22 20:55:21 guus Exp $ */ @@ -35,7 +35,6 @@ #define DEFAULT_DEVICE "/dev/tun" int device_fd = -1; -int device_type; char *device = NULL; char *iface = NULL; char ifrname[IFNAMSIZ]; @@ -44,7 +43,7 @@ char *device_info = NULL; int device_total_in = 0; int device_total_out = 0; -int setup_device(void) +bool setup_device(void) { int ip_fd = -1, if_fd = -1; int ppa; @@ -57,7 +56,7 @@ int setup_device(void) if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); - return -1; + return false; } ppa = 0; @@ -69,35 +68,35 @@ int setup_device(void) if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) { logger(LOG_ERR, _("Could not open /dev/ip: %s"), strerror(errno)); - return -1; + return false; } /* Assign a new PPA and get its unit number. */ if((ppa = ioctl(device_fd, TUNNEWPPA, ppa)) < 0) { logger(LOG_ERR, _("Can't assign new interface: %s"), strerror(errno)); - return -1; + return false; } if((if_fd = open(device, O_RDWR, 0)) < 0) { logger(LOG_ERR, _("Could not open %s twice: %s"), device, strerror(errno)); - return -1; + return false; } if(ioctl(if_fd, I_PUSH, "ip") < 0) { logger(LOG_ERR, _("Can't push IP module: %s"), strerror(errno)); - return -1; + return false; } /* Assign ppa according to the unit number returned by tun device */ if(ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0) { logger(LOG_ERR, _("Can't set PPA %d: %s"), ppa, strerror(errno)); - return -1; + return false; } if(ioctl(ip_fd, I_LINK, if_fd) < 0) { logger(LOG_ERR, _("Can't link TUN device to IP: %s"), strerror(errno)); - return -1; + return false; } if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) @@ -107,7 +106,7 @@ int setup_device(void) logger(LOG_INFO, _("%s is a %s"), device, device_info); - return 0; + return true; } void close_device(void) @@ -117,7 +116,7 @@ void close_device(void) close(device_fd); } -int read_packet(vpn_packet_t *packet) +bool read_packet(vpn_packet_t *packet) { int lenin; @@ -126,7 +125,7 @@ int read_packet(vpn_packet_t *packet) if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, device, strerror(errno)); - return -1; + return false; } packet->data[12] = 0x08; @@ -139,10 +138,10 @@ int read_packet(vpn_packet_t *packet) ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info); - return 0; + return true; } -int write_packet(vpn_packet_t *packet) +bool write_packet(vpn_packet_t *packet) { cp(); @@ -152,12 +151,12 @@ int write_packet(vpn_packet_t *packet) if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, packet->len, strerror(errno)); - return -1; + return false; } device_total_out += packet->len; - return 0; + return true; } void dump_device_stats(void) diff --git a/src/subnet.h b/src/subnet.h index a7acbae0..7cb0e9bb 100644 --- a/src/subnet.h +++ b/src/subnet.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: subnet.h,v 1.1.2.21 2003/07/06 23:16:29 guus Exp $ + $Id: subnet.h,v 1.1.2.22 2003/07/22 20:55:20 guus Exp $ */ #ifndef __TINC_SUBNET_H__ @@ -25,12 +25,12 @@ #include "net.h" -enum { +typedef enum subnet_type_t { SUBNET_MAC = 0, SUBNET_IPV4, SUBNET_IPV6, SUBNET_TYPES /* Guardian */ -}; +} subnet_type_t; typedef struct subnet_mac_t { mac_t address; @@ -53,7 +53,7 @@ typedef struct subnet_t { struct node_t *owner; /* the owner of this subnet */ struct node_t *uplink; /* the uplink which we should send packets to for this subnet */ - int type; /* subnet type (IPv4? IPv6? MAC? something even weirder?) */ + subnet_type_t type; /* subnet type (IPv4? IPv6? MAC? something even weirder?) */ /* And now for the actual subnet: */ diff --git a/src/tincd.c b/src/tincd.c index e71c3211..b9f9f4f4 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: tincd.c,v 1.10.4.75 2003/07/22 12:58:34 guus Exp $ + $Id: tincd.c,v 1.10.4.76 2003/07/22 20:55:20 guus Exp $ */ #include "system.h" @@ -51,10 +51,10 @@ char *program_name = NULL; /* If nonzero, display usage information and exit. */ -int show_help = 0; +bool show_help = false; /* If nonzero, print the version on standard output and exit. */ -int show_version = 0; +bool show_version = false; /* If nonzero, it will attempt to kill a running tincd and exit. */ int kill_tincd = 0; @@ -63,40 +63,39 @@ int kill_tincd = 0; int generate_keys = 0; /* If nonzero, use null ciphers and skip all key exchanges. */ -int bypass_security = 0; +bool bypass_security = false; /* If nonzero, disable swapping for this process. */ -int do_mlock = 0; +bool do_mlock = false; /* If nonzero, write log entries to a separate file. */ -int use_logfile = 0; +bool use_logfile = false; char *identname = NULL; /* program name for syslog */ char *pidfilename = NULL; /* pid file location */ char *logfilename = NULL; /* log file location */ char **g_argv; /* a copy of the cmdline arguments */ -char **environment; /* A pointer to the environment on - startup */ +char **environment; /* A pointer to the environment on startup */ static struct option const long_options[] = { {"config", required_argument, NULL, 'c'}, {"kill", optional_argument, NULL, 'k'}, {"net", required_argument, NULL, 'n'}, - {"help", no_argument, &show_help, 1}, - {"version", no_argument, &show_version, 1}, - {"no-detach", no_argument, &do_detach, 0}, + {"help", no_argument, NULL, 1}, + {"version", no_argument, NULL, 2}, + {"no-detach", no_argument, NULL, 'D'}, {"generate-keys", optional_argument, NULL, 'K'}, {"debug", optional_argument, NULL, 'd'}, - {"bypass-security", no_argument, &bypass_security, 1}, - {"mlock", no_argument, &do_mlock, 1}, - {"logfile", optional_argument, NULL, 'F'}, - {"pidfile", required_argument, NULL, 'P'}, + {"bypass-security", no_argument, NULL, 3}, + {"mlock", no_argument, NULL, 'L'}, + {"logfile", optional_argument, NULL, 4}, + {"pidfile", required_argument, NULL, 5}, {NULL, 0, NULL, 0} }; -static void usage(int status) +static void usage(bool status) { - if(status != 0) + if(status) fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name); else { @@ -133,11 +132,11 @@ static void parse_options(int argc, char **argv, char **envp) break; case 'D': /* no detach */ - do_detach = 0; + do_detach = false; break; case 'L': /* no detach */ - do_mlock = 1; + do_mlock = true; break; case 'd': /* inc debug level */ @@ -171,7 +170,7 @@ static void parse_options(int argc, char **argv, char **envp) if(!kill_tincd) { fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), optarg); - usage(1); + usage(true); } } } else @@ -189,7 +188,7 @@ static void parse_options(int argc, char **argv, char **envp) if(generate_keys < 512) { fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"), optarg); - usage(1); + usage(true); } generate_keys &= ~7; /* Round it to bytes */ @@ -197,18 +196,30 @@ static void parse_options(int argc, char **argv, char **envp) generate_keys = 1024; break; - case 'F': /* write log entries to a file */ - use_logfile = 1; + case 1: /* show help */ + show_help = true; + break; + + case 2: /* show version */ + show_version = true; + break; + + case 3: /* bypass security */ + bypass_security = true; + break; + + case 4: /* write log entries to a file */ + use_logfile = true; if(optarg) logfilename = xstrdup(optarg); break; - case 'P': /* write PID to a file */ + case 5: /* write PID to a file */ pidfilename = xstrdup(optarg); break; case '?': - usage(1); + usage(true); default: break; @@ -257,7 +268,7 @@ static void indicator(int a, int b, void *p) Generate a public/private RSA keypair, and ask for a file to store them in. */ -static int keygen(int bits) +static bool keygen(int bits) { RSA *rsa_key; FILE *f; @@ -269,43 +280,43 @@ static int keygen(int bits) if(!rsa_key) { fprintf(stderr, _("Error during key generation!\n")); - return -1; + return false; } else fprintf(stderr, _("Done.\n")); - get_config_string(lookup_config(config_tree, "Name"), &name); - - if(name) - asprintf(&filename, "%s/hosts/%s", confbase, name); - else - asprintf(&filename, "%s/rsa_key.pub", confbase); - - f = ask_and_safe_open(filename, _("public RSA key"), "a"); + asprintf(&filename, "%s/rsa_key.priv", confbase); + f = ask_and_safe_open(filename, _("private RSA key"), true, "a"); if(!f) - return -1; + return false; if(ftell(f)) fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n")); - PEM_write_RSAPublicKey(f, rsa_key); + PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL); fclose(f); free(filename); - asprintf(&filename, "%s/rsa_key.priv", confbase); - f = ask_and_safe_open(filename, _("private RSA key"), "a"); + get_config_string(lookup_config(config_tree, "Name"), &name); + + if(name) + asprintf(&filename, "%s/hosts/%s", confbase, name); + else + asprintf(&filename, "%s/rsa_key.pub", confbase); + + f = ask_and_safe_open(filename, _("public RSA key"), false, "a"); if(!f) - return -1; + return false; if(ftell(f)) fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n")); - PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL); + PEM_write_RSAPublicKey(f, rsa_key); fclose(f); free(filename); - return 0; + return true; } /* @@ -360,10 +371,10 @@ int main(int argc, char **argv, char **envp) } if(show_help) - usage(0); + usage(false); if(kill_tincd) - exit(kill_other(kill_tincd)); + exit(!kill_other(kill_tincd)); openlogger("tinc", LOGMODE_STDERR); @@ -393,10 +404,10 @@ int main(int argc, char **argv, char **envp) if(generate_keys) { read_server_config(); - exit(keygen(generate_keys)); + exit(!keygen(generate_keys)); } - if(read_server_config()) + if(!read_server_config()) exit(1); if(lzo_init() != LZO_E_OK) { @@ -404,11 +415,11 @@ int main(int argc, char **argv, char **envp) exit(1); } - if(detach()) - exit(0); + if(!detach()) + exit(1); for(;;) { - if(!setup_network_connections()) { + if(setup_network_connections()) { main_loop(); cleanup_and_exit(1); }