Use bools and enums where appropriate.

This commit is contained in:
Guus Sliepen 2003-07-22 20:55:21 +00:00
parent 471308e163
commit eefa28059a
40 changed files with 767 additions and 765 deletions

View file

@ -19,7 +19,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -141,109 +141,109 @@ config_t *lookup_config_next(avl_tree_t *config_tree, config_t *cfg)
return NULL; return NULL;
} }
int get_config_bool(config_t *cfg, int *result) bool get_config_bool(config_t *cfg, bool *result)
{ {
cp(); cp();
if(!cfg) if(!cfg)
return 0; return false;
if(!strcasecmp(cfg->value, "yes")) { if(!strcasecmp(cfg->value, "yes")) {
*result = 1; *result = true;
return 1; return true;
} else if(!strcasecmp(cfg->value, "no")) { } else if(!strcasecmp(cfg->value, "no")) {
*result = 0; *result = false;
return 1; return true;
} }
logger(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"), logger(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line); 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(); cp();
if(!cfg) if(!cfg)
return 0; return false;
if(sscanf(cfg->value, "%d", result) == 1) if(sscanf(cfg->value, "%d", result) == 1)
return 1; return true;
logger(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"), logger(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line); 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(); cp();
if(!cfg) if(!cfg)
return 0; return false;
*result = xstrdup(cfg->value); *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; struct addrinfo *ai;
cp(); cp();
if(!cfg) if(!cfg)
return 0; return false;
ai = str2addrinfo(cfg->value, NULL, 0); ai = str2addrinfo(cfg->value, NULL, 0);
if(ai) { if(ai) {
*result = ai; *result = ai;
return 1; return true;
} }
logger(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"), logger(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line); 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; subnet_t *subnet;
cp(); cp();
if(!cfg) if(!cfg)
return 0; return false;
subnet = str2net(cfg->value); subnet = str2net(cfg->value);
if(!subnet) { if(!subnet) {
logger(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"), logger(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line); cfg->variable, cfg->file, cfg->line);
return 0; return false;
} }
/* Teach newbies what subnets are... */ /* Teach newbies what subnets are... */
if(((subnet->type == SUBNET_IPV4) 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) || ((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"), 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); cfg->variable, cfg->file, cfg->line);
free(subnet); free(subnet);
return 0; return false;
} }
*result = subnet; *result = subnet;
return 1; return true;
} }
/* /*
@ -325,7 +325,8 @@ int read_config_file(avl_tree_t *config_tree, const char *fname)
FILE *fp; FILE *fp;
char *buffer, *line; char *buffer, *line;
char *variable, *value; char *variable, *value;
int lineno = 0, ignore = 0; int lineno = 0;
bool ignore = false;
config_t *cfg; config_t *cfg;
size_t bufsize; size_t bufsize;
@ -366,7 +367,7 @@ int read_config_file(avl_tree_t *config_tree, const char *fname)
continue; /* comment: ignore */ continue; /* comment: ignore */
if(!strcmp(variable, "-----BEGIN")) if(!strcmp(variable, "-----BEGIN"))
ignore = 1; ignore = true;
if(!ignore) { if(!ignore) {
value = strtok(NULL, "\t\n\r ="); 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")) if(!strcmp(variable, "-----END"))
ignore = 0; ignore = false;
} }
free(buffer); free(buffer);
@ -396,7 +397,7 @@ int read_config_file(avl_tree_t *config_tree, const char *fname)
return err; return err;
} }
int read_server_config() bool read_server_config()
{ {
char *fname; char *fname;
int x; int x;
@ -412,10 +413,10 @@ int read_server_config()
free(fname); 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)) #if !(defined(HAVE_CYGWIN) || defined(HAVE_MINGW))
char *p; char *p;
@ -426,7 +427,7 @@ int is_safe_path(const char *file)
if(*file != '/') { if(*file != '/') {
logger(LOG_ERR, _("`%s' is not an absolute path"), file); logger(LOG_ERR, _("`%s' is not an absolute path"), file);
return 0; return false;
} }
p = strrchr(file, '/'); p = strrchr(file, '/');
@ -442,13 +443,13 @@ int is_safe_path(const char *file)
check1: check1:
if(lstat(f, &s) < 0) { if(lstat(f, &s) < 0) {
logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno)); logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
return 0; return false;
} }
if(s.st_uid != geteuid()) { if(s.st_uid != geteuid()) {
logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"), logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"),
f, s.st_uid, geteuid()); f, s.st_uid, geteuid());
return 0; return false;
} }
if(S_ISLNK(s.st_mode)) { if(S_ISLNK(s.st_mode)) {
@ -457,7 +458,7 @@ check1:
if(readlink(f, l, MAXBUFSIZE) < 0) { if(readlink(f, l, MAXBUFSIZE) < 0) {
logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f,
strerror(errno)); strerror(errno));
return 0; return false;
} }
f = l; f = l;
@ -470,16 +471,16 @@ check1:
check2: check2:
if(lstat(f, &s) < 0 && errno != ENOENT) { if(lstat(f, &s) < 0 && errno != ENOENT) {
logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno)); logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
return 0; return false;
} }
if(errno == ENOENT) if(errno == ENOENT)
return 1; return true;
if(s.st_uid != geteuid()) { if(s.st_uid != geteuid()) {
logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"), logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"),
f, s.st_uid, geteuid()); f, s.st_uid, geteuid());
return 0; return false;
} }
if(S_ISLNK(s.st_mode)) { if(S_ISLNK(s.st_mode)) {
@ -488,7 +489,7 @@ check2:
if(readlink(f, l, MAXBUFSIZE) < 0) { if(readlink(f, l, MAXBUFSIZE) < 0) {
logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f,
strerror(errno)); strerror(errno));
return 0; return false;
} }
f = l; f = l;
@ -498,15 +499,14 @@ check2:
if(s.st_mode & 0007) { if(s.st_mode & 0007) {
/* Accessible by others */ /* Accessible by others */
logger(LOG_ERR, _("`%s' has unsecure permissions"), f); logger(LOG_ERR, _("`%s' has unsecure permissions"), f);
return 0; return false;
} }
#endif #endif
return 1; return true;
} }
FILE *ask_and_safe_open(const char *filename, const char *what, FILE *ask_and_safe_open(const char *filename, const char *what, bool safe, const char *mode)
const char *mode)
{ {
FILE *r; FILE *r;
char *directory; char *directory;
@ -562,12 +562,14 @@ FILE *ask_and_safe_open(const char *filename, const char *what,
} }
/* Then check the file for nasty attacks */ /* 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. */ if(safe) {
fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n" if(!is_safe_path(fn)) { /* Do not permit any directories that are readable or writeable by other users. */
"I will not create or overwrite this file.\n"), fn); fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n"
fclose(r); "I will not create or overwrite this file.\n"), fn);
free(fn); fclose(r);
return NULL; free(fn);
return NULL;
}
} }
free(fn); free(fn);

View file

@ -17,14 +17,13 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_CONF_H__
#define __TINC_CONF_H__ #define __TINC_CONF_H__
#include "avl_tree.h" #include "avl_tree.h"
#include "subnet.h"
typedef struct config_t { typedef struct config_t {
char *variable; char *variable;
@ -33,11 +32,13 @@ typedef struct config_t {
int line; int line;
} config_t; } config_t;
#include "subnet.h"
extern avl_tree_t *config_tree; extern avl_tree_t *config_tree;
extern int pingtimeout; extern int pingtimeout;
extern int maxtimeout; extern int maxtimeout;
extern int bypass_security; extern bool bypass_security;
extern char *confbase; extern char *confbase;
extern char *netname; extern char *netname;
@ -48,15 +49,15 @@ extern void free_config(config_t *);
extern void config_add(avl_tree_t *, 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(avl_tree_t *, char *);
extern config_t *lookup_config_next(avl_tree_t *, config_t *); extern config_t *lookup_config_next(avl_tree_t *, config_t *);
extern int get_config_bool(config_t *, int *); extern bool get_config_bool(config_t *, bool *);
extern int get_config_int(config_t *, int *); extern bool get_config_int(config_t *, int *);
extern int get_config_string(config_t *, char **); extern bool get_config_string(config_t *, char **);
extern int get_config_address(config_t *, struct addrinfo **); extern bool get_config_address(config_t *, struct addrinfo **);
extern int get_config_subnet(config_t *, struct subnet_t **); extern bool get_config_subnet(config_t *, struct subnet_t **);
extern int read_config_file(avl_tree_t *, const char *); extern int read_config_file(avl_tree_t *, const char *);
extern int read_server_config(void); extern bool read_server_config(void);
extern FILE *ask_and_safe_open(const char *, const char *, const char *); extern FILE *ask_and_safe_open(const char *, const char *, bool, const char *);
extern int is_safe_path(const char *); extern bool is_safe_path(const char *);
#endif /* __TINC_CONF_H__ */ #endif /* __TINC_CONF_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -128,7 +128,7 @@ void dump_connections(void)
logger(LOG_DEBUG, _("End of connections.")); logger(LOG_DEBUG, _("End of connections."));
} }
int read_connection_config(connection_t *c) bool read_connection_config(connection_t *c)
{ {
char *fname; char *fname;
int x; int x;
@ -139,5 +139,5 @@ int read_connection_config(connection_t *c)
x = read_config_file(c->config_tree, fname); x = read_config_file(c->config_tree, fname);
free(fname); free(fname);
return x; return x == 0;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_CONNECTION_H__
@ -27,11 +27,6 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#include "avl_tree.h" #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_INDIRECT 0x0001
#define OPTION_TCPONLY 0x0002 #define OPTION_TCPONLY 0x0002
@ -49,10 +44,15 @@ typedef struct connection_status_t {
int unused:18; int unused:18;
} connection_status_t; } connection_status_t;
#include "edge.h"
#include "list.h"
#include "net.h"
#include "node.h"
typedef struct connection_t { typedef struct connection_t {
char *name; /* name he claims to have */ 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 */ char *hostname; /* the hostname of its real ip */
int protocol_version; /* used protocol */ int protocol_version; /* used protocol */
@ -105,6 +105,6 @@ extern void free_connection(connection_t *);
extern void connection_add(connection_t *); extern void connection_add(connection_t *);
extern void connection_del(connection_t *); extern void connection_del(connection_t *);
extern void dump_connections(void); extern void dump_connections(void);
extern int read_connection_config(connection_t *); extern bool read_connection_config(connection_t *);
#endif /* __TINC_CONNECTION_H__ */ #endif /* __TINC_CONNECTION_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -84,7 +84,7 @@ HANDLE handle;
pid_t reader_pid; pid_t reader_pid;
int sp[2]; int sp[2];
int setup_device(void) bool setup_device(void)
{ {
HKEY key, key2, adapterkey; HKEY key, key2, adapterkey;
int i; 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)) { 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")); logger(LOG_ERR, _("Unable to read registry"));
return -1; return false;
} }
for (i = 0; ; i++) { for (i = 0; ; i++) {
@ -120,7 +120,7 @@ int setup_device(void)
if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) { if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) {
logger(LOG_ERR, _("Unable to read registry")); logger(LOG_ERR, _("Unable to read registry"));
return -1; return false;
} }
len = sizeof(productname); len = sizeof(productname);
@ -146,7 +146,7 @@ skip:
if(!found) { if(!found) {
logger(LOG_ERR, _("No CIPE adapters found!")); logger(LOG_ERR, _("No CIPE adapters found!"));
return -1; return false;
} }
/* Get adapter name */ /* Get adapter name */
@ -166,14 +166,14 @@ skip:
if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) { if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) {
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno)); logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno));
return -1; return false;
} }
reader_pid = fork(); reader_pid = fork();
if(reader_pid == -1) { if(reader_pid == -1) {
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno)); logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno));
return -1; return false;
} }
if(!reader_pid) { if(!reader_pid) {
@ -213,7 +213,7 @@ skip:
if(handle == INVALID_HANDLE_VALUE) { if(handle == INVALID_HANDLE_VALUE) {
logger(LOG_ERR, _("Could not open CIPE tap device for writing!")); logger(LOG_ERR, _("Could not open CIPE tap device for writing!"));
return -1; return false;
} }
device_fd = sp[0]; device_fd = sp[0];
@ -228,7 +228,7 @@ skip:
read(device_fd, &gelukt, 1); read(device_fd, &gelukt, 1);
if(gelukt != 1) { if(gelukt != 1) {
logger(LOG_DEBUG, "Tap reader failed!"); logger(LOG_DEBUG, "Tap reader failed!");
return -1; return false;
} }
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) 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); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return false;
} }
void close_device(void) void close_device(void)
@ -252,7 +252,7 @@ void close_device(void)
kill(reader_pid, SIGKILL); kill(reader_pid, SIGKILL);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -261,7 +261,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(sp[0], packet->data, MTU)) <= 0) { if((lenin = read(sp[0], packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->len = lenin; 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, ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info); device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
int lenout; int lenout;
@ -285,12 +285,12 @@ int write_packet(vpn_packet_t *packet)
if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) { if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) {
logger(LOG_ERR, "Error while writing to %s %s", device_info, device); logger(LOG_ERR, "Error while writing to %s %s", device_info, device);
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return 0; return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -30,14 +30,13 @@
#define DEFAULT_DEVICE "/dev/tun0" #define DEFAULT_DEVICE "/dev/tun0"
int device_fd = -1; int device_fd = -1;
int device_type;
char *device; char *device;
char *iface; char *iface;
char *device_info; char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
cp(); cp();
@ -49,14 +48,14 @@ int setup_device(void)
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
device_info = _("MacOS/X tun device"); device_info = _("MacOS/X tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -66,7 +65,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -75,7 +74,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->data[12] = 0x08; 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"), ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
packet->len, device_info); packet->len, device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -101,10 +100,12 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,21 +17,23 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_DEVICE_H__
#define __TINC_DEVICE_H__ #define __TINC_DEVICE_H__
#include "net.h"
extern int device_fd; extern int device_fd;
extern char *device; extern char *device;
extern char *iface; extern char *iface;
extern int setup_device(void); extern bool setup_device(void);
extern void close_device(void); extern void close_device(void);
extern int read_packet(vpn_packet_t *); extern bool read_packet(struct vpn_packet_t *);
extern int write_packet(vpn_packet_t *); extern bool write_packet(struct vpn_packet_t *);
extern void dump_device_stats(void); extern void dump_device_stats(void);
#endif /* __TINC_DEVICE_H__ */ #endif /* __TINC_DEVICE_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -30,14 +30,13 @@
#define DEFAULT_DEVICE "/dev/tap0" #define DEFAULT_DEVICE "/dev/tap0"
int device_fd = -1; int device_fd = -1;
int device_type;
char *device; char *device;
char *iface; char *iface;
char *device_info; char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
cp(); cp();
@ -49,14 +48,14 @@ int setup_device(void)
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
device_info = _("FreeBSD tap device"); device_info = _("FreeBSD tap device");
logger(LOG_INFO, _("%s is a %s"), device, device_info); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -66,7 +65,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -75,7 +74,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(device_fd, packet->data, MTU)) <= 0) { if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->len = lenin; 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"), ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
packet->len, device_info); packet->len, device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -98,10 +97,12 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data, packet->len) < 0) { if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info, logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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: /* We need to generate two trees from the graph:
@ -69,7 +69,7 @@ void mst_kruskal(void)
connection_t *c; connection_t *c;
int nodes = 0; int nodes = 0;
int safe_edges = 0; int safe_edges = 0;
int skipped; bool skipped;
cp(); cp();
@ -77,7 +77,7 @@ void mst_kruskal(void)
for(node = connection_tree->head; node; node = node->next) { for(node = connection_tree->head; node; node = node->next) {
c = (connection_t *) node->data; c = (connection_t *) node->data;
c->status.mst = 0; c->status.mst = false;
} }
/* Do we have something to do at all? */ /* 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) { for(node = node_tree->head; node; node = node->next) {
n = (node_t *) node->data; n = (node_t *) node->data;
n->status.visited = 0; n->status.visited = false;
nodes++; nodes++;
} }
/* Starting point */ /* 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 */ /* 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; next = node->next;
e = (edge_t *) node->data; e = (edge_t *) node->data;
if(!e->reverse || e->from->status.visited == e->to->status.visited) { if(!e->reverse || e->from->status.visited == e->to->status.visited) {
skipped = 1; skipped = true;
continue; continue;
} }
e->from->status.visited = 1; e->from->status.visited = true;
e->to->status.visited = 1; e->to->status.visited = true;
if(e->connection) if(e->connection)
e->connection->status.mst = 1; e->connection->status.mst = true;
if(e->reverse->connection) if(e->reverse->connection)
e->reverse->connection->status.mst = 1; e->reverse->connection->status.mst = true;
safe_edges++; safe_edges++;
@ -125,7 +125,7 @@ void mst_kruskal(void)
e->to->name, e->weight); e->to->name, e->weight);
if(skipped) { if(skipped) {
skipped = 0; skipped = false;
next = edge_weight_tree->head; next = edge_weight_tree->head;
continue; continue;
} }
@ -145,7 +145,7 @@ void sssp_bfs(void)
edge_t *e; edge_t *e;
node_t *n; node_t *n;
avl_tree_t *todo_tree; avl_tree_t *todo_tree;
int indirect; bool indirect;
char *name; char *name;
char *address, *port; char *address, *port;
char *envp[7]; char *envp[7];
@ -159,14 +159,14 @@ void sssp_bfs(void)
for(node = node_tree->head; node; node = node->next) { for(node = node_tree->head; node; node = node->next) {
n = (node_t *) node->data; n = (node_t *) node->data;
n->status.visited = 0; n->status.visited = false;
n->status.indirect = 1; n->status.indirect = true;
} }
/* Begin with myself */ /* Begin with myself */
myself->status.visited = 1; myself->status.visited = true;
myself->status.indirect = 0; myself->status.indirect = false;
myself->nexthop = myself; myself->nexthop = myself;
myself->via = myself; myself->via = myself;
node = avl_alloc_node(); node = avl_alloc_node();
@ -212,7 +212,7 @@ void sssp_bfs(void)
&& (!e->to->status.indirect || indirect)) && (!e->to->status.indirect || indirect))
continue; continue;
e->to->status.visited = 1; e->to->status.visited = true;
e->to->status.indirect = indirect; e->to->status.indirect = indirect;
e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop; e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
e->to->via = indirect ? n->via : e->to; e->to->via = indirect ? n->via : e->to;
@ -257,8 +257,8 @@ void sssp_bfs(void)
n->name, n->hostname); n->name, n->hostname);
} }
n->status.validkey = 0; n->status.validkey = false;
n->status.waitingforkey = 0; n->status.waitingforkey = false;
asprintf(&envp[0], "NETNAME=%s", netname ? : ""); asprintf(&envp[0], "NETNAME=%s", netname ? : "");
asprintf(&envp[1], "DEVICE=%s", device ? : ""); asprintf(&envp[1], "DEVICE=%s", device ? : "");

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -39,14 +39,14 @@
#include "route.h" #include "route.h"
#include "utils.h" #include "utils.h"
enum { typedef enum device_type_t {
DEVICE_TYPE_ETHERTAP, DEVICE_TYPE_ETHERTAP,
DEVICE_TYPE_TUN, DEVICE_TYPE_TUN,
DEVICE_TYPE_TAP, DEVICE_TYPE_TAP,
}; } device_type_t;
int device_fd = -1; int device_fd = -1;
int device_type; device_type_t device_type;
char *device; char *device;
char *iface; char *iface;
char ifrname[IFNAMSIZ]; char ifrname[IFNAMSIZ];
@ -55,7 +55,7 @@ char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
struct ifreq ifr; struct ifreq ifr;
@ -74,7 +74,7 @@ int setup_device(void)
if(device_fd < 0) { if(device_fd < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
#ifdef HAVE_TUNTAP #ifdef HAVE_TUNTAP
@ -105,7 +105,7 @@ int setup_device(void)
#endif #endif
{ {
if(routing_mode == RMODE_ROUTER) if(routing_mode == RMODE_ROUTER)
overwrite_mac = 1; overwrite_mac = true;
device_info = _("Linux ethertap device"); device_info = _("Linux ethertap device");
device_type = DEVICE_TYPE_ETHERTAP; device_type = DEVICE_TYPE_ETHERTAP;
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; 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); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -123,7 +123,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -136,7 +136,7 @@ int read_packet(vpn_packet_t *packet)
if(lenin <= 0) { if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno)); device_info, device, strerror(errno));
return -1; return false;
} }
packet->len = lenin + 10; packet->len = lenin + 10;
@ -147,7 +147,7 @@ int read_packet(vpn_packet_t *packet)
if(lenin <= 0) { if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno)); device_info, device, strerror(errno));
return -1; return false;
} }
packet->len = lenin; packet->len = lenin;
@ -158,7 +158,7 @@ int read_packet(vpn_packet_t *packet)
if(lenin <= 0) { if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno)); device_info, device, strerror(errno));
return -1; return false;
} }
packet->len = lenin - 2; 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, ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info); device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -186,14 +186,14 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data + 10, packet->len - 10) < 0) { if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
break; break;
case DEVICE_TYPE_TAP: case DEVICE_TYPE_TAP:
if(write(device_fd, packet->data, packet->len) < 0) { if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
break; break;
case DEVICE_TYPE_ETHERTAP: 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) { if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
break; break;
} }
device_total_out += packet->len; device_total_out += packet->len;
return 0; return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -25,18 +25,20 @@
#include "conf.h" #include "conf.h"
#include "logger.h" #include "logger.h"
int debug_level = DEBUG_NOTHING; debug_t debug_level = DEBUG_NOTHING;
static int logmode = LOGMODE_STDERR; static logmode_t logmode = LOGMODE_STDERR;
static pid_t logpid; static pid_t logpid;
extern char *logfilename; extern char *logfilename;
static FILE *logfile = NULL; static FILE *logfile = NULL;
static const char *logident = NULL; static const char *logident = NULL;
void openlogger(const char *ident, int mode) { void openlogger(const char *ident, logmode_t mode) {
logident = ident; logident = ident;
logmode = mode; logmode = mode;
switch(mode) { switch(mode) {
case LOGMODE_NULL:
break;
case LOGMODE_STDERR: case LOGMODE_STDERR:
logpid = getpid(); logpid = getpid();
break; break;
@ -58,6 +60,8 @@ void logger(int priority, const char *format, ...) {
va_start(ap, format); va_start(ap, format);
switch(logmode) { switch(logmode) {
case LOGMODE_NULL:
break;
case LOGMODE_STDERR: case LOGMODE_STDERR:
vfprintf(stderr, format, ap); vfprintf(stderr, format, ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -85,6 +89,9 @@ void logger(int priority, const char *format, ...) {
void closelogger(void) { void closelogger(void) {
switch(logmode) { switch(logmode) {
case LOGMODE_NULL:
case LOGMODE_STDERR:
break;
case LOGMODE_FILE: case LOGMODE_FILE:
fclose(logfile); fclose(logfile);
break; break;

View file

@ -1,6 +1,6 @@
#ifndef __TINC_LOGGER_H__ #ifndef __TINC_LOGGER_H__
enum { typedef enum debug_t {
DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */ DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */
DEBUG_ALWAYS = 0, DEBUG_ALWAYS = 0,
DEBUG_CONNECTIONS = 1, /* Show (dis)connects of other tinc daemons via TCP */ 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_TRAFFIC = 5, /* Show network traffic information */
DEBUG_PACKET = 6, /* Show contents of each packet that is being sent/received */ DEBUG_PACKET = 6, /* Show contents of each packet that is being sent/received */
DEBUG_SCARY_THINGS = 10 /* You have been warned */ DEBUG_SCARY_THINGS = 10 /* You have been warned */
}; } debug_t;
enum { typedef enum logmode_t {
LOGMODE_NULL, LOGMODE_NULL,
LOGMODE_STDERR, LOGMODE_STDERR,
LOGMODE_FILE, LOGMODE_FILE,
LOGMODE_SYSLOG LOGMODE_SYSLOG
}; } logmode_t;
extern int debug_level; extern debug_t debug_level;
extern void openlogger(const char *, int); extern void openlogger(const char *, logmode_t);
extern void logger(int, const char *, ...) __attribute__ ((format(printf, 2, 3))); extern void logger(int, const char *, ...) __attribute__ ((format(printf, 2, 3)));
extern void closelogger(void); extern void closelogger(void);

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -32,7 +32,7 @@
#include "system.h" #include "system.h"
#include "utils.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; char *bufp;
int outlen; int outlen;
@ -58,13 +58,13 @@ int send_meta(connection_t *c, char *buffer, int length)
continue; continue;
logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
c->hostname, strerror(errno)); c->hostname, strerror(errno));
return -1; return false;
} }
bufp += result; bufp += result;
length -= result; length -= result;
} }
return 0; return true;
} }
void broadcast_meta(connection_t *from, char *buffer, int length) 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; int x;
socklen_t l = sizeof(x); socklen_t l = sizeof(x);
int oldlen, i; int oldlen, i;
int lenin, reqlen; int lenin, reqlen;
int decrypted = 0; bool decrypted = false;
char inbuf[MAXBUFSIZE]; char inbuf[MAXBUFSIZE];
cp(); cp();
@ -96,13 +96,13 @@ int receive_meta(connection_t *c)
if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { 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__, logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__,
__LINE__, c->socket, strerror(errno), c->name, c->hostname); __LINE__, c->socket, strerror(errno), c->name, c->hostname);
return -1; return false;
} }
if(x) { if(x) {
logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"), logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
c->name, c->hostname, strerror(x)); c->name, c->hostname, strerror(x));
return -1; return false;
} }
/* Strategy: /* Strategy:
@ -121,12 +121,12 @@ int receive_meta(connection_t *c)
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"), ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
} else if(errno == EINTR) } else if(errno == EINTR)
return 0; return true;
else else
logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"), logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
c->name, c->hostname, strerror(errno)); c->name, c->hostname, strerror(errno));
return -1; return false;
} }
oldlen = c->buflen; oldlen = c->buflen;
@ -138,7 +138,7 @@ int receive_meta(connection_t *c)
if(c->status.decryptin && !decrypted) { if(c->status.decryptin && !decrypted) {
EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin); EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
memcpy(c->buffer + oldlen, inbuf, lenin); memcpy(c->buffer + oldlen, inbuf, lenin);
decrypted = 1; decrypted = true;
} }
/* Are we receiving a TCPpacket? */ /* Are we receiving a TCPpacket? */
@ -172,8 +172,8 @@ int receive_meta(connection_t *c)
if(reqlen) { if(reqlen) {
c->reqlen = reqlen; c->reqlen = reqlen;
if(receive_request(c)) if(!receive_request(c))
return -1; return false;
c->buflen -= reqlen; c->buflen -= reqlen;
lenin -= reqlen; lenin -= reqlen;
@ -188,10 +188,10 @@ int receive_meta(connection_t *c)
if(c->buflen >= MAXBUFSIZE) { if(c->buflen >= MAXBUFSIZE) {
logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"), logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
c->last_ping_time = now; c->last_ping_time = now;
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_META_H__
@ -25,8 +25,8 @@
#include "connection.h" #include "connection.h"
extern int send_meta(connection_t *, const char *, int); extern bool send_meta(struct connection_t *, const char *, int);
extern int broadcast_meta(connection_t *, const char *, int); extern bool broadcast_meta(struct connection_t *, const char *, int);
extern int receive_meta(connection_t *); extern bool receive_meta(struct connection_t *);
#endif /* __TINC_META_H__ */ #endif /* __TINC_META_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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!" #error "Device driver for MinGW environment not written yet!"
@ -85,7 +85,7 @@ HANDLE handle;
pid_t reader_pid; pid_t reader_pid;
int sp[2]; int sp[2];
int setup_device(void) bool setup_device(void)
{ {
HKEY key, key2, adapterkey; HKEY key, key2, adapterkey;
int i; 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)) { 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")); logger(LOG_ERR, _("Unable to read registry"));
return -1; return false;
} }
for (i = 0; ; i++) { for (i = 0; ; i++) {
@ -121,7 +121,7 @@ int setup_device(void)
if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) { if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) {
logger(LOG_ERR, _("Unable to read registry")); logger(LOG_ERR, _("Unable to read registry"));
return -1; return false;
} }
len = sizeof(productname); len = sizeof(productname);
@ -147,7 +147,7 @@ skip:
if(!found) { if(!found) {
logger(LOG_ERR, _("No CIPE adapters found!")); logger(LOG_ERR, _("No CIPE adapters found!"));
return -1; return false;
} }
/* Get adapter name */ /* Get adapter name */
@ -167,14 +167,14 @@ skip:
if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) { if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) {
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno)); logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno));
return -1; return false;
} }
reader_pid = fork(); reader_pid = fork();
if(reader_pid == -1) { if(reader_pid == -1) {
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno)); logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno));
return -1; return false;
} }
if(!reader_pid) { if(!reader_pid) {
@ -214,7 +214,7 @@ skip:
if(handle == INVALID_HANDLE_VALUE) { if(handle == INVALID_HANDLE_VALUE) {
logger(LOG_ERR, _("Could not open CIPE tap device for writing!")); logger(LOG_ERR, _("Could not open CIPE tap device for writing!"));
return -1; return false;
} }
device_fd = sp[0]; device_fd = sp[0];
@ -229,7 +229,7 @@ skip:
read(device_fd, &gelukt, 1); read(device_fd, &gelukt, 1);
if(gelukt != 1) { if(gelukt != 1) {
logger(LOG_DEBUG, "Tap reader failed!"); logger(LOG_DEBUG, "Tap reader failed!");
return -1; return false;
} }
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) 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); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -253,7 +253,7 @@ void close_device(void)
kill(reader_pid, SIGKILL); kill(reader_pid, SIGKILL);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -262,7 +262,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(sp[0], packet->data, MTU)) <= 0) { if((lenin = read(sp[0], packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->len = lenin; 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, ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info); device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
int lenout; int lenout;
@ -286,12 +286,12 @@ int write_packet(vpn_packet_t *packet)
if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) { if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) {
logger(LOG_ERR, "Error while writing to %s %s", device_info, device); logger(LOG_ERR, "Error while writing to %s %s", device_info, device);
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return 0; return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -40,9 +40,7 @@
#include "route.h" #include "route.h"
#include "subnet.h" #include "subnet.h"
int do_purge = 0; bool do_purge = false;
int sighup = 0;
int sigalrm = 0;
time_t now = 0; time_t now = 0;
@ -134,11 +132,11 @@ static int build_fdset(fd_set * fs)
/* /*
Terminate a connection: Terminate a connection:
- Close the socket - 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 - Check if we need to retry making an outgoing connection
- Deactivate the host - Deactivate the host
*/ */
void terminate_connection(connection_t *c, int report) void terminate_connection(connection_t *c, bool report)
{ {
cp(); cp();
@ -148,8 +146,8 @@ void terminate_connection(connection_t *c, int report)
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"), ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
c->status.remove = 1; c->status.remove = true;
c->status.active = 0; c->status.active = false;
if(c->node) if(c->node)
c->node->connection = NULL; c->node->connection = NULL;
@ -200,8 +198,8 @@ static void check_dead_connections(void)
if(c->status.pinged) { if(c->status.pinged) {
ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"), ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
c->name, c->hostname); c->name, c->hostname);
c->status.timeout = 1; c->status.timeout = true;
terminate_connection(c, 1); terminate_connection(c, true);
} else { } else {
send_ping(c); send_ping(c);
} }
@ -214,7 +212,7 @@ static void check_dead_connections(void)
} }
ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"), ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
c->name, c->hostname); 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(); cp();
if(FD_ISSET(device_fd, f)) { if(FD_ISSET(device_fd, f)) {
if(!read_packet(&packet)) if(read_packet(&packet))
route_outgoing(&packet); route_outgoing(&packet);
} }
@ -247,7 +245,7 @@ static void check_network_activity(fd_set * f)
if(FD_ISSET(c->socket, f)) { if(FD_ISSET(c->socket, f)) {
if(c->status.connecting) { if(c->status.connecting) {
c->status.connecting = 0; c->status.connecting = false;
getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len); getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
if(!result) 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); terminate_connection(c, c->status.active);
continue; continue;
} }
@ -321,7 +319,7 @@ void main_loop(void)
if(do_purge) { if(do_purge) {
purge(); purge();
do_purge = 0; do_purge = false;
} }
/* Let's check if everybody is still alive */ /* Let's check if everybody is still alive */
@ -361,7 +359,7 @@ void main_loop(void)
event->handler(event->data); event->handler(event->data);
event_del(event); event_del(event);
} }
sigalrm = 0; sigalrm = false;
} }
if(sighup) { if(sighup) {
@ -370,14 +368,14 @@ void main_loop(void)
char *fname; char *fname;
struct stat s; struct stat s;
sighup = 0; sighup = false;
/* Reread our own configuration file */ /* Reread our own configuration file */
exit_configuration(&config_tree); exit_configuration(&config_tree);
init_configuration(&config_tree); init_configuration(&config_tree);
if(read_server_config()) { if(!read_server_config()) {
logger(LOG_ERR, _("Unable to reread configuration file, exitting.")); logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
exit(1); exit(1);
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_NET_H__
@ -54,7 +54,7 @@ typedef struct ipv6_t {
typedef short length_t; typedef short length_t;
typedef union { typedef union sockaddr_t {
struct sockaddr sa; struct sockaddr sa;
struct sockaddr_in in; struct sockaddr_in in;
struct sockaddr_in6 in6; struct sockaddr_in6 in6;
@ -87,6 +87,12 @@ typedef struct packet_queue_t {
queue_element_t *tail; queue_element_t *tail;
} packet_queue_t; } packet_queue_t;
typedef struct listen_socket_t {
int tcp;
int udp;
sockaddr_t sa;
} listen_socket_t;
#include "conf.h" #include "conf.h"
typedef struct outgoing_t { typedef struct outgoing_t {
@ -97,12 +103,6 @@ typedef struct outgoing_t {
struct addrinfo *aip; struct addrinfo *aip;
} outgoing_t; } outgoing_t;
typedef struct listen_socket_t {
int tcp;
int udp;
sockaddr_t sa;
} listen_socket_t;
extern int maxtimeout; extern int maxtimeout;
extern int seconds_till_retry; extern int seconds_till_retry;
extern int addressfamily; extern int addressfamily;
@ -111,8 +111,8 @@ extern listen_socket_t listen_socket[MAXSOCKETS];
extern int listen_sockets; extern int listen_sockets;
extern int keyexpires; extern int keyexpires;
extern int keylifetime; extern int keylifetime;
extern int do_prune; extern bool do_prune;
extern int do_purge; extern bool do_purge;
extern char *myport; extern char *myport;
extern time_t now; extern time_t now;
extern EVP_CIPHER_CTX packet_ctx; 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 handle_incoming_vpn_data(int);
extern void finish_connecting(struct connection_t *); extern void finish_connecting(struct connection_t *);
extern void do_outgoing_connection(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_listen_socket(sockaddr_t *);
extern int setup_vpn_in_socket(sockaddr_t *); extern int setup_vpn_in_socket(sockaddr_t *);
extern void send_packet(struct node_t *, vpn_packet_t *); extern void send_packet(struct node_t *, vpn_packet_t *);
extern void receive_tcppacket(struct connection_t *, char *, int); extern void receive_tcppacket(struct connection_t *, char *, int);
extern void broadcast_packet(struct node_t *, vpn_packet_t *); 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 setup_outgoing_connection(struct outgoing_t *);
extern void try_outgoing_connections(void); extern void try_outgoing_connections(void);
extern void close_network_connections(void); extern void close_network_connections(void);
extern void main_loop(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 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__ */ #endif /* __TINC_NET_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -239,7 +239,7 @@ static void send_udppacket(node_t *n, vpn_packet_t *inpkt)
if(!n->status.waitingforkey) if(!n->status.waitingforkey)
send_req_key(n->nexthop->connection, myself, n); send_req_key(n->nexthop->connection, myself, n);
n->status.waitingforkey = 1; n->status.waitingforkey = true;
return; return;
} }
@ -350,8 +350,8 @@ void send_packet(node_t *n, vpn_packet_t *packet)
n->name, via->name, n->via->hostname); n->name, via->name, n->via->hostname);
if((myself->options | via->options) & OPTION_TCPONLY) { if((myself->options | via->options) & OPTION_TCPONLY) {
if(send_tcppacket(via->connection, packet)) if(!send_tcppacket(via->connection, packet))
terminate_connection(via->connection, 1); terminate_connection(via->connection, true);
} else } else
send_udppacket(via, packet); send_udppacket(via, packet);
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -44,7 +44,7 @@
char *myport; char *myport;
int read_rsa_public_key(connection_t *c) bool read_rsa_public_key(connection_t *c)
{ {
FILE *fp; FILE *fp;
char *fname; 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->n, key);
BN_hex2bn(&c->rsa_key->e, "FFFF"); BN_hex2bn(&c->rsa_key->e, "FFFF");
free(key); free(key);
return 0; return true;
} }
/* Else, check for PublicKeyFile statement and read it */ /* Else, check for PublicKeyFile statement and read it */
if(get_config_string if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) { fp = fopen(fname, "r");
if(is_safe_path(fname)) {
fp = fopen(fname, "r");
if(!fp) { if(!fp) {
logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"), logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
fname, strerror(errno));
free(fname);
return -1;
}
free(fname);
c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
fclose(fp);
if(c->rsa_key)
return 0; /* Woohoo. */
/* 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);
return -1;
}
free(fname);
c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
fclose(fp);
if(c->rsa_key) {
// RSA_blinding_on(c->rsa_key, NULL);
return 0;
}
logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
fname, strerror(errno)); fname, strerror(errno));
return -1;
} else {
free(fname); free(fname);
return -1; return false;
} }
free(fname);
c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
fclose(fp);
if(c->rsa_key)
return true; /* Woohoo. */
/* 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);
return false;
}
free(fname);
c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
fclose(fp);
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 */ /* 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); free(fname);
if(c->rsa_key) if(c->rsa_key)
return 0; return true;
/* Try again with PEM_read_RSA_PUBKEY. */ /* Try again with PEM_read_RSA_PUBKEY. */
@ -144,14 +138,14 @@ int read_rsa_public_key(connection_t *c)
free(fname); free(fname);
if(c->rsa_key) if(c->rsa_key)
return 0; return true;
logger(LOG_ERR, _("No public key for %s specified!"), c->name); 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; FILE *fp;
char *fname, *key; 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->d, key);
BN_hex2bn(&myself->connection->rsa_key->e, "FFFF"); BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
free(key); free(key);
return 0; return true;
} }
if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) 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"), logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
fname, strerror(errno)); fname, strerror(errno));
free(fname); free(fname);
return -1; return false;
} }
free(fname); free(fname);
@ -188,27 +182,28 @@ int read_rsa_private_key(void)
if(!myself->connection->rsa_key) { if(!myself->connection->rsa_key) {
logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"), logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
fname, strerror(errno)); fname, strerror(errno));
return -1; return false;
} }
return 0; return true;
} }
free(fname); free(fname);
return -1; return false;
} }
/* /*
Configure node_t myself and set up the local sockets (listen only) Configure node_t myself and set up the local sockets (listen only)
*/ */
int setup_myself(void) bool setup_myself(void)
{ {
config_t *cfg; config_t *cfg;
subnet_t *subnet; subnet_t *subnet;
char *name, *hostname, *mode, *afname, *cipher, *digest; char *name, *hostname, *mode, *afname, *cipher, *digest;
char *address = NULL; char *address = NULL;
struct addrinfo hint, *ai, *aip; struct addrinfo hint, *ai, *aip;
int choice, err; bool choice;
int err;
cp(); cp();
@ -224,28 +219,28 @@ int setup_myself(void)
if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */ if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
logger(LOG_ERR, _("Name for tinc daemon required!")); 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!")); logger(LOG_ERR, _("Invalid name for myself!"));
free(name); free(name);
return -1; return false;
} }
myself->name = name; myself->name = name;
myself->connection->name = xstrdup(name); myself->connection->name = xstrdup(name);
if(read_rsa_private_key()) if(!read_rsa_private_key())
return -1; return false;
if(read_connection_config(myself->connection)) { if(!read_connection_config(myself->connection)) {
logger(LOG_ERR, _("Cannot open host configuration file for myself!")); logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
return -1; return false;
} }
if(read_rsa_public_key(myself->connection)) if(!read_rsa_public_key(myself->connection))
return -1; return false;
if(!get_config_string if(!get_config_string
(lookup_config(myself->connection->config_tree, "Port"), &myport)) (lookup_config(myself->connection->config_tree, "Port"), &myport))
@ -257,7 +252,7 @@ int setup_myself(void)
while(cfg) { while(cfg) {
if(!get_config_subnet(cfg, &subnet)) if(!get_config_subnet(cfg, &subnet))
return -1; return false;
subnet_add(myself, subnet); subnet_add(myself, subnet);
@ -274,14 +269,11 @@ int setup_myself(void)
if(choice) if(choice)
myself->options |= OPTION_TCPONLY; myself->options |= OPTION_TCPONLY;
if(get_config_bool if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
(lookup_config(myself->connection->config_tree, "IndirectData"),
&choice))
if(choice) if(choice)
myself->options |= OPTION_INDIRECT; myself->options |= OPTION_INDIRECT;
if(get_config_bool if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
if(choice) if(choice)
myself->options |= OPTION_TCPONLY; myself->options |= OPTION_TCPONLY;
@ -297,14 +289,14 @@ int setup_myself(void)
routing_mode = RMODE_HUB; routing_mode = RMODE_HUB;
else { else {
logger(LOG_ERR, _("Invalid routing mode!")); logger(LOG_ERR, _("Invalid routing mode!"));
return -1; return false;
} }
free(mode); free(mode);
} else } else
routing_mode = RMODE_ROUTER; routing_mode = RMODE_ROUTER;
get_config_bool(lookup_config(config_tree, "PriorityInheritance"), get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
&priorityinheritance);
#if !defined(SOL_IP) || !defined(IP_TOS) #if !defined(SOL_IP) || !defined(IP_TOS)
if(priorityinheritance) if(priorityinheritance)
logger(LOG_WARNING, _("PriorityInheritance not supported on this platform")); 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)) if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
macexpire = 600; macexpire = 600;
if(get_config_int if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) {
(lookup_config(myself->connection->config_tree, "MaxTimeout"),
&maxtimeout)) {
if(maxtimeout <= 0) { if(maxtimeout <= 0) {
logger(LOG_ERR, _("Bogus maximum timeout!")); logger(LOG_ERR, _("Bogus maximum timeout!"));
return -1; return false;
} }
} else } else
maxtimeout = 900; maxtimeout = 900;
@ -332,7 +322,7 @@ int setup_myself(void)
addressfamily = AF_UNSPEC; addressfamily = AF_UNSPEC;
else { else {
logger(LOG_ERR, _("Invalid address family!")); logger(LOG_ERR, _("Invalid address family!"));
return -1; return false;
} }
free(afname); free(afname);
} }
@ -350,7 +340,7 @@ int setup_myself(void)
if(!myself->cipher) { if(!myself->cipher) {
logger(LOG_ERR, _("Unrecognized cipher type!")); logger(LOG_ERR, _("Unrecognized cipher type!"));
return -1; return false;
} }
} }
} else } else
@ -385,7 +375,7 @@ int setup_myself(void)
if(!myself->digest) { if(!myself->digest) {
logger(LOG_ERR, _("Unrecognized digest type!")); logger(LOG_ERR, _("Unrecognized digest type!"));
return -1; return false;
} }
} }
} else } else
@ -399,10 +389,10 @@ int setup_myself(void)
if(myself->digest) { if(myself->digest) {
if(myself->maclength > myself->digest->md_size) { if(myself->maclength > myself->digest->md_size) {
logger(LOG_ERR, _("MAC length exceeds size of digest!")); logger(LOG_ERR, _("MAC length exceeds size of digest!"));
return -1; return false;
} else if(myself->maclength < 0) { } else if(myself->maclength < 0) {
logger(LOG_ERR, _("Bogus MAC length!")); logger(LOG_ERR, _("Bogus MAC length!"));
return -1; return false;
} }
} }
} else } else
@ -417,7 +407,7 @@ int setup_myself(void)
&myself->compression)) { &myself->compression)) {
if(myself->compression < 0 || myself->compression > 11) { if(myself->compression < 0 || myself->compression > 11) {
logger(LOG_ERR, _("Bogus compression level!")); logger(LOG_ERR, _("Bogus compression level!"));
return -1; return false;
} }
} else } else
myself->compression = 0; myself->compression = 0;
@ -428,8 +418,8 @@ int setup_myself(void)
myself->nexthop = myself; myself->nexthop = myself;
myself->via = myself; myself->via = myself;
myself->status.active = 1; myself->status.active = true;
myself->status.reachable = 1; myself->status.reachable = true;
node_add(myself); node_add(myself);
graph(); graph();
@ -450,7 +440,7 @@ int setup_myself(void)
if(err || !ai) { if(err || !ai) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
gai_strerror(err)); gai_strerror(err));
return -1; return false;
} }
listen_sockets = 0; listen_sockets = 0;
@ -484,16 +474,16 @@ int setup_myself(void)
logger(LOG_NOTICE, _("Ready")); logger(LOG_NOTICE, _("Ready"));
else { else {
logger(LOG_ERR, _("Unable to create any listening socket!")); logger(LOG_ERR, _("Unable to create any listening socket!"));
return -1; return false;
} }
return 0; return true;
} }
/* /*
setup all initial network connections setup all initial network connections
*/ */
int setup_network_connections(void) bool setup_network_connections(void)
{ {
char *envp[5]; char *envp[5];
int i; int i;
@ -516,11 +506,11 @@ int setup_network_connections(void)
} else } else
pingtimeout = 60; pingtimeout = 60;
if(setup_device() < 0) if(!setup_device())
return -1; return false;
if(setup_myself() < 0) if(!setup_myself())
return -1; return false;
/* Run tinc-up script to further initialize the tap interface */ /* Run tinc-up script to further initialize the tap interface */
asprintf(&envp[0], "NETNAME=%s", netname ? : ""); asprintf(&envp[0], "NETNAME=%s", netname ? : "");
@ -536,7 +526,7 @@ int setup_network_connections(void)
try_outgoing_connections(); try_outgoing_connections();
return 0; return true;
} }
/* /*
@ -557,11 +547,11 @@ void close_network_connections(void)
if(c->outgoing) if(c->outgoing)
free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL; free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
terminate_connection(c, 0); terminate_connection(c, false);
} }
if(myself && myself->connection) if(myself && myself->connection)
terminate_connection(myself->connection, 0); terminate_connection(myself->connection, false);
for(i = 0; i < listen_sockets; i++) { for(i = 0; i < listen_sockets; i++) {
close(listen_socket[i].tcp); close(listen_socket[i].tcp);

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -221,7 +221,7 @@ begin:
if(!c->outgoing->cfg) { if(!c->outgoing->cfg) {
ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"), ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
c->name); c->name);
c->status.remove = 1; c->status.remove = true;
retry_outgoing(c->outgoing); retry_outgoing(c->outgoing);
return; return;
} }
@ -292,7 +292,7 @@ begin:
if(result == -1) { if(result == -1) {
if(errno == EINPROGRESS) { if(errno == EINPROGRESS) {
c->status.connecting = 1; c->status.connecting = true;
return; return;
} }
@ -357,7 +357,7 @@ void setup_outgoing_connection(outgoing_t *outgoing)
accept a new tcp connect and create a accept a new tcp connect and create a
new connection new connection
*/ */
int handle_new_meta_connection(int sock) bool handle_new_meta_connection(int sock)
{ {
connection_t *c; connection_t *c;
sockaddr_t sa; sockaddr_t sa;
@ -370,7 +370,7 @@ int handle_new_meta_connection(int sock)
if(fd < 0) { if(fd < 0) {
logger(LOG_ERR, _("Accepting a new connection failed: %s"), logger(LOG_ERR, _("Accepting a new connection failed: %s"),
strerror(errno)); strerror(errno));
return -1; return false;
} }
sockaddrunmap(&sa); sockaddrunmap(&sa);
@ -393,7 +393,7 @@ int handle_new_meta_connection(int sock)
c->allow_request = ID; c->allow_request = ID;
send_id(c); send_id(c);
return 0; return true;
} }
void try_outgoing_connections(void) void try_outgoing_connections(void)
@ -408,7 +408,7 @@ void try_outgoing_connections(void)
cfg = lookup_config_next(config_tree, cfg)) { cfg = lookup_config_next(config_tree, cfg)) {
get_config_string(cfg, &name); get_config_string(cfg, &name);
if(check_id(name)) { if(!check_id(name)) {
logger(LOG_ERR, logger(LOG_ERR,
_("Invalid name for outgoing connection in %s line %d"), _("Invalid name for outgoing connection in %s line %d"),
cfg->file, cfg->line); cfg->file, cfg->line);

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -33,7 +33,6 @@
#define DEVICE_TYPE_TUNTAP 1 #define DEVICE_TYPE_TUNTAP 1
int device_fd = -1; int device_fd = -1;
int device_type;
char *device; char *device;
char *iface; char *iface;
char *device_info; char *device_info;
@ -41,7 +40,7 @@ char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
cp(); cp();
@ -52,14 +51,14 @@ int setup_device(void)
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
device_info = _("NetBSD tun device"); device_info = _("NetBSD tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -69,7 +68,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -78,7 +77,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->data[12] = 0x08; packet->data[12] = 0x08;
@ -92,10 +91,10 @@ int read_packet(vpn_packet_t *packet)
device_info); device_info);
} }
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -105,10 +104,12 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -28,7 +28,7 @@
#include "utils.h" #include "utils.h"
#include "xalloc.h" #include "xalloc.h"
int hostnames = 0; bool hostnames = false;
/* /*
Turn a string into a struct addrinfo. Turn a string into a struct addrinfo.
@ -244,7 +244,7 @@ void maskcpy(void *va, void *vb, int masklen, int len)
a[i] = 0; a[i] = 0;
} }
int maskcheck(void *va, int masklen, int len) bool maskcheck(void *va, int masklen, int len)
{ {
int i; int i;
char *a = va; char *a = va;
@ -255,11 +255,11 @@ int maskcheck(void *va, int masklen, int len)
masklen %= 8; masklen %= 8;
if(masklen && a[i++] & (0xff >> masklen)) if(masklen && a[i++] & (0xff >> masklen))
return -1; return false;
for(; i < len; i++) for(; i < len; i++)
if(a[i] != 0) if(a[i] != 0)
return -2; return false;
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_NETUTL_H__
@ -25,7 +25,7 @@
#include "net.h" #include "net.h"
extern int hostnames; extern bool hostnames;
extern struct addrinfo *str2addrinfo(char *, char *, int); extern struct addrinfo *str2addrinfo(char *, char *, int);
extern sockaddr_t str2sockaddr(char *, char *); extern sockaddr_t str2sockaddr(char *, char *);
@ -36,6 +36,6 @@ extern void sockaddrunmap(sockaddr_t *);
extern int maskcmp(void *, void *, int, int); extern int maskcmp(void *, void *, int, int);
extern void maskcpy(void *, void *, int, int); extern void maskcpy(void *, void *, int, int);
extern void mask(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__ */ #endif /* __TINC_NETUTL_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_NODE_H__
@ -45,7 +45,7 @@ typedef struct node_t {
sockaddr_t address; /* his real (internet) ip to send UDP packets to */ sockaddr_t address; /* his real (internet) ip to send UDP packets to */
char *hostname; /* the hostname of its real ip */ 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 */ const EVP_CIPHER *cipher; /* Cipher type for UDP packets */
char *key; /* Cipher key and iv */ char *key; /* Cipher key and iv */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -33,7 +33,6 @@
#define DEVICE_TYPE_TUNTAP 1 #define DEVICE_TYPE_TUNTAP 1
int device_fd = -1; int device_fd = -1;
int device_type;
char *device; char *device;
char *iface; char *iface;
char *device_info; char *device_info;
@ -41,7 +40,7 @@ char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
cp(); cp();
@ -52,14 +51,14 @@ int setup_device(void)
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
device_info = _("OpenBSD tun device"); device_info = _("OpenBSD tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -69,7 +68,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
u_int32_t type; u_int32_t type;
@ -80,7 +79,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = readv(device_fd, vector, 2)) <= 0) { if((lenin = readv(device_fd, vector, 2)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
switch (ntohl(type)) { switch (ntohl(type)) {
@ -98,7 +97,7 @@ int read_packet(vpn_packet_t *packet)
ifdebug(TRAFFIC) logger(LOG_ERR, ifdebug(TRAFFIC) logger(LOG_ERR,
_ ("Unknown address family %d while reading packet from %s %s"), _ ("Unknown address family %d while reading packet from %s %s"),
ntohl(type), device_info, device); ntohl(type), device_info, device);
return -1; return false;
} }
packet->len = lenin + 10; packet->len = lenin + 10;
@ -109,10 +108,10 @@ int read_packet(vpn_packet_t *packet)
device_info); device_info);
} }
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
u_int32_t type; u_int32_t type;
struct iovec vector[2]; struct iovec vector[2];
@ -136,7 +135,7 @@ int write_packet(vpn_packet_t *packet)
ifdebug(TRAFFIC) logger(LOG_ERR, ifdebug(TRAFFIC) logger(LOG_ERR,
_("Unknown address family %d while writing packet to %s %s"), _("Unknown address family %d while writing packet to %s %s"),
af, device_info, device); af, device_info, device);
return -1; return false;
} }
vector[0].iov_base = &type; vector[0].iov_base = &type;
@ -147,10 +146,12 @@ int write_packet(vpn_packet_t *packet)
if(writev(device_fd, vector, 2) < 0) { if(writev(device_fd, vector, 2) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -35,21 +35,19 @@
#include "xalloc.h" #include "xalloc.h"
/* If zero, don't detach from the terminal. */ /* 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 *identname;
extern char *pidfilename; extern char *pidfilename;
extern char **g_argv; extern char **g_argv;
extern int use_logfile; extern bool use_logfile;
sigset_t emptysigset; sigset_t emptysigset;
static int saved_debug_level = -1; static int saved_debug_level = -1;
extern int sighup;
extern int sigalrm;
extern int do_purge;
static void memory_full(int size) static void memory_full(int size)
{ {
logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), 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 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; int pid;
@ -107,20 +105,20 @@ static int write_pidfile(void)
netname, pid); netname, pid);
else else
fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid); 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 it's locked, write-protected, or whatever */
if(!write_pid(pidfilename)) if(!write_pid(pidfilename))
return 1; return false;
return 0; return true;
} }
/* /*
kill older tincd for this net kill older tincd for this net
*/ */
int kill_other(int signal) bool kill_other(int signal)
{ {
int pid; int pid;
@ -134,7 +132,7 @@ int kill_other(int signal)
netname); netname);
else else
fprintf(stderr, _("No other tincd is running.\n")); fprintf(stderr, _("No other tincd is running.\n"));
return 1; return false;
} }
errno = 0; /* No error, sometimes errno is only changed on error */ errno = 0; /* No error, sometimes errno is only changed on error */
@ -151,13 +149,13 @@ int kill_other(int signal)
remove_pid(pidfilename); remove_pid(pidfilename);
} }
return 0; return true;
} }
/* /*
Detach from current terminal, write pidfile, kill parent Detach from current terminal, write pidfile, kill parent
*/ */
int detach(void) bool detach(void)
{ {
cp(); cp();
@ -165,24 +163,24 @@ int detach(void)
/* First check if we can open a fresh new pidfile */ /* First check if we can open a fresh new pidfile */
if(write_pidfile()) if(!write_pidfile())
return -1; return false;
/* If we succeeded in doing that, detach */ /* If we succeeded in doing that, detach */
closelogger(); closelogger();
if(do_detach) { if(do_detach) {
if(daemon(0, 0) < 0) { if(daemon(0, 0)) {
fprintf(stderr, _("Couldn't detach from terminal: %s"), fprintf(stderr, _("Couldn't detach from terminal: %s"),
strerror(errno)); strerror(errno));
return -1; return false;
} }
/* Now UPDATE the pid in the pidfile, because we changed it... */ /* Now UPDATE the pid in the pidfile, because we changed it... */
if(!write_pid(pidfilename)) if(!write_pid(pidfilename))
return -1; return false;
} }
openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR)); openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
@ -192,7 +190,7 @@ int detach(void)
xalloc_fail_func = memory_full; 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. 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; pid_t pid;
int status; int status;
@ -244,14 +242,14 @@ int execute_script(const char *name, char **envp)
/* First check if there is a script */ /* First check if there is a script */
if(stat(scriptname, &s)) if(stat(scriptname, &s))
return 0; return true;
pid = fork(); pid = fork();
if(pid < 0) { if(pid < 0) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "fork", logger(LOG_ERR, _("System call `%s' failed: %s"), "fork",
strerror(errno)); strerror(errno));
return -1; return false;
} }
if(pid) { if(pid) {
@ -264,26 +262,26 @@ int execute_script(const char *name, char **envp)
if(WEXITSTATUS(status)) { if(WEXITSTATUS(status)) {
logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"),
pid, name, WEXITSTATUS(status)); pid, name, WEXITSTATUS(status));
return -1; return false;
} else } else
return 0; return true;
} else if(WIFSIGNALED(status)) { /* Child was killed by a signal */ } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid, logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid,
name, WTERMSIG(status), strsignal(WTERMSIG(status))); name, WTERMSIG(status), strsignal(WTERMSIG(status)));
return -1; return false;
} else { /* Something strange happened */ } else { /* Something strange happened */
logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid,
name); name);
return -1; return false;
} }
} else if (errno != EINTR) { } else if (errno != EINTR) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid", logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid",
strerror(errno)); strerror(errno));
return -1; return false;
} }
/* Why do we get EINTR? */ /* Why do we get EINTR? */
return 0; return true;
} }
/* Child here */ /* Child here */
@ -344,7 +342,7 @@ static RETSIGTYPE fatal_signal_handler(int a)
static RETSIGTYPE sighup_handler(int a) static RETSIGTYPE sighup_handler(int a)
{ {
logger(LOG_NOTICE, _("Got HUP signal")); logger(LOG_NOTICE, _("Got HUP signal"));
sighup = 1; sighup = true;
} }
static RETSIGTYPE sigint_handler(int a) static RETSIGTYPE sigint_handler(int a)
@ -366,7 +364,7 @@ static RETSIGTYPE sigint_handler(int a)
static RETSIGTYPE sigalrm_handler(int a) static RETSIGTYPE sigalrm_handler(int a)
{ {
logger(LOG_NOTICE, _("Got ALRM signal")); logger(LOG_NOTICE, _("Got ALRM signal"));
sigalrm = 1; sigalrm = true;
} }
static RETSIGTYPE sigusr1_handler(int a) static RETSIGTYPE sigusr1_handler(int a)
@ -384,8 +382,7 @@ static RETSIGTYPE sigusr2_handler(int a)
static RETSIGTYPE sigwinch_handler(int a) static RETSIGTYPE sigwinch_handler(int a)
{ {
extern int do_purge; do_purge = true;
do_purge = 1;
} }
static RETSIGTYPE unexpected_signal_handler(int a) static RETSIGTYPE unexpected_signal_handler(int a)
@ -403,21 +400,20 @@ static struct {
int signal; int signal;
void (*handler)(int); void (*handler)(int);
} sighandlers[] = { } sighandlers[] = {
{ {SIGHUP, sighup_handler},
SIGHUP, sighup_handler}, { {SIGTERM, sigterm_handler},
SIGTERM, sigterm_handler}, { {SIGQUIT, sigquit_handler},
SIGQUIT, sigquit_handler}, { {SIGSEGV, fatal_signal_handler},
SIGSEGV, fatal_signal_handler}, { {SIGBUS, fatal_signal_handler},
SIGBUS, fatal_signal_handler}, { {SIGILL, fatal_signal_handler},
SIGILL, fatal_signal_handler}, { {SIGPIPE, ignore_signal_handler},
SIGPIPE, ignore_signal_handler}, { {SIGINT, sigint_handler},
SIGINT, sigint_handler}, { {SIGUSR1, sigusr1_handler},
SIGUSR1, sigusr1_handler}, { {SIGUSR2, sigusr2_handler},
SIGUSR2, sigusr2_handler}, { {SIGCHLD, ignore_signal_handler},
SIGCHLD, ignore_signal_handler}, { {SIGALRM, sigalrm_handler},
SIGALRM, sigalrm_handler}, { {SIGWINCH, sigwinch_handler},
SIGWINCH, sigwinch_handler}, { {0, NULL}
0, NULL}
}; };
void setup_signals(void) void setup_signals(void)

View file

@ -17,18 +17,20 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_PROCESS_H__
#define __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 void setup_signals(void);
extern int execute_script(const char *, char **); extern bool execute_script(const char *, char **);
extern int detach(void); extern bool detach(void);
extern int kill_other(int); extern bool kill_other(int);
extern void cleanup_and_exit(int) __attribute__ ((noreturn)); extern void cleanup_and_exit(int) __attribute__ ((noreturn));
#endif /* __TINC_PROCESS_H__ */ #endif /* __TINC_PROCESS_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -32,7 +32,7 @@
/* Jumptable for the request handlers */ /* 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, id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
status_h, error_h, termreq_h, status_h, error_h, termreq_h,
ping_h, pong_h, ping_h, pong_h,
@ -53,21 +53,21 @@ static char (*request_name[]) = {
static avl_tree_t *past_request_tree; static avl_tree_t *past_request_tree;
int check_id(char *id) bool check_id(char *id)
{ {
int i; int i;
for(i = 0; i < strlen(id); i++) for(i = 0; i < strlen(id); i++)
if(!isalnum(id[i]) && 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 /* Generic request routines - takes care of logging and error
detection as well */ detection as well */
int send_request(connection_t *c, const char *format, ...) bool send_request(connection_t *c, const char *format, ...)
{ {
va_list args; va_list args;
char buffer[MAXBUFSIZE]; char buffer[MAXBUFSIZE];
@ -86,7 +86,7 @@ int send_request(connection_t *c, const char *format, ...)
if(len < 0 || len > MAXBUFSIZE - 1) { if(len < 0 || len > MAXBUFSIZE - 1) {
logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"), logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
ifdebug(PROTOCOL) { ifdebug(PROTOCOL) {
@ -101,16 +101,16 @@ int send_request(connection_t *c, const char *format, ...)
buffer[len++] = '\n'; buffer[len++] = '\n';
if(c == broadcast) if(c == broadcast) {
return broadcast_meta(NULL, buffer, len); broadcast_meta(NULL, buffer, len);
else return true;
} else
return send_meta(c, buffer, len); return send_meta(c, buffer, len);
} }
int forward_request(connection_t *from) void forward_request(connection_t *from)
{ {
int request; int request;
cp();
cp(); cp();
@ -127,10 +127,10 @@ int forward_request(connection_t *from)
from->buffer[from->reqlen - 1] = '\n'; 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; int request;
@ -145,7 +145,7 @@ int receive_request(connection_t *c)
logger(LOG_ERR, _("Unknown request from %s (%s)"), logger(LOG_ERR, _("Unknown request from %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} else { } else {
ifdebug(PROTOCOL) { ifdebug(PROTOCOL) {
ifdebug(META) ifdebug(META)
@ -161,23 +161,23 @@ int receive_request(connection_t *c)
if((c->allow_request != ALL) && (c->allow_request != request)) { if((c->allow_request != ALL) && (c->allow_request != request)) {
logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
c->hostname); c->hostname);
return -1; return false;
} }
if(request_handlers[request] (c)) if(!request_handlers[request](c)) {
/* Something went wrong. Probably scriptkiddies. Terminate. */ /* Something went wrong. Probably scriptkiddies. Terminate. */
{
logger(LOG_ERR, _("Error while processing %s from %s (%s)"), logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
request_name[request], c->name, c->hostname); request_name[request], c->name, c->hostname);
return -1; return false;
} }
} else { } else {
logger(LOG_ERR, _("Bogus data received from %s (%s)"), logger(LOG_ERR, _("Bogus data received from %s (%s)"),
c->name, c->hostname); 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) 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); avl_delete_tree(past_request_tree);
} }
int seen_request(char *request) bool seen_request(char *request)
{ {
past_request_t p, *new; past_request_t p, *new;
@ -219,13 +219,13 @@ int seen_request(char *request)
if(avl_search(past_request_tree, &p)) { if(avl_search(past_request_tree, &p)) {
ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request")); ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
return 1; return true;
} else { } else {
new = (past_request_t *) xmalloc(sizeof(*new)); new = (past_request_t *) xmalloc(sizeof(*new));
new->request = xstrdup(request); new->request = xstrdup(request);
new->firstseen = now; new->firstseen = now;
avl_insert(past_request_tree, new); avl_insert(past_request_tree, new);
return 0; return false;
} }
} }

View file

@ -17,17 +17,12 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_PROTOCOL_H__
#define __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, /* Protocol version. Different versions are incompatible,
incompatible version have different protocols. incompatible version have different protocols.
*/ */
@ -36,7 +31,7 @@
/* Request numbers */ /* Request numbers */
enum { typedef enum request_t {
ALL = -1, /* Guardian for allow_request */ ALL = -1, /* Guardian for allow_request */
ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK, ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK,
STATUS, ERROR, TERMREQ, STATUS, ERROR, TERMREQ,
@ -46,7 +41,7 @@ enum {
KEY_CHANGED, REQ_KEY, ANS_KEY, KEY_CHANGED, REQ_KEY, ANS_KEY,
PACKET, PACKET,
LAST /* Guardian for the highest request number */ LAST /* Guardian for the highest request number */
}; } request_t;
typedef struct past_request_t { typedef struct past_request_t {
char *request; char *request;
@ -58,58 +53,63 @@ typedef struct past_request_t {
#define MAX_STRING_SIZE 2048 #define MAX_STRING_SIZE 2048
#define MAX_STRING "%2048s" #define MAX_STRING "%2048s"
#include "edge.h"
#include "net.h"
#include "node.h"
#include "subnet.h"
/* Basic functions */ /* Basic functions */
extern int send_request(connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3))); extern bool send_request(struct connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3)));
extern int forward_request(connection_t *); extern void forward_request(struct connection_t *);
extern int receive_request(connection_t *); extern bool receive_request(struct connection_t *);
extern int check_id(char *); extern bool check_id(char *);
extern void init_requests(void); extern void init_requests(void);
extern void exit_requests(void); extern void exit_requests(void);
extern int seen_request(char *); extern bool seen_request(char *);
extern void age_past_requests(void); extern void age_past_requests(void);
/* Requests */ /* Requests */
extern int send_id(connection_t *); extern bool send_id(struct connection_t *);
extern int send_metakey(connection_t *); extern bool send_metakey(struct connection_t *);
extern int send_challenge(connection_t *); extern bool send_challenge(struct connection_t *);
extern int send_chal_reply(connection_t *); extern bool send_chal_reply(struct connection_t *);
extern int send_ack(connection_t *); extern bool send_ack(struct connection_t *);
extern int send_status(connection_t *, int, char *); extern bool send_status(struct connection_t *, int, char *);
extern int send_error(connection_t *, int, char *); extern bool send_error(struct connection_t *, int, char *);
extern int send_termreq(connection_t *); extern bool send_termreq(struct connection_t *);
extern int send_ping(connection_t *); extern bool send_ping(struct connection_t *);
extern int send_pong(connection_t *); extern bool send_pong(struct connection_t *);
extern int send_add_subnet(connection_t *, subnet_t *); extern bool send_add_subnet(struct connection_t *, struct subnet_t *);
extern int send_del_subnet(connection_t *, subnet_t *); extern bool send_del_subnet(struct connection_t *, struct subnet_t *);
extern int send_add_edge(connection_t *, edge_t *); extern bool send_add_edge(struct connection_t *, struct edge_t *);
extern int send_del_edge(connection_t *, edge_t *); extern bool send_del_edge(struct connection_t *, struct edge_t *);
extern int send_key_changed(connection_t *, node_t *); extern bool send_key_changed(struct connection_t *, struct node_t *);
extern int send_req_key(connection_t *, node_t *, node_t *); extern bool send_req_key(struct connection_t *, struct node_t *, struct node_t *);
extern int send_ans_key(connection_t *, node_t *, node_t *); extern bool send_ans_key(struct connection_t *, struct node_t *, struct node_t *);
extern int send_tcppacket(connection_t *, vpn_packet_t *); extern bool send_tcppacket(struct connection_t *, struct vpn_packet_t *);
/* Request handlers */ /* Request handlers */
extern int id_h(connection_t *); extern bool id_h(struct connection_t *);
extern int metakey_h(connection_t *); extern bool metakey_h(struct connection_t *);
extern int challenge_h(connection_t *); extern bool challenge_h(struct connection_t *);
extern int chal_reply_h(connection_t *); extern bool chal_reply_h(struct connection_t *);
extern int ack_h(connection_t *); extern bool ack_h(struct connection_t *);
extern int status_h(connection_t *); extern bool status_h(struct connection_t *);
extern int error_h(connection_t *); extern bool error_h(struct connection_t *);
extern int termreq_h(connection_t *); extern bool termreq_h(struct connection_t *);
extern int ping_h(connection_t *); extern bool ping_h(struct connection_t *);
extern int pong_h(connection_t *); extern bool pong_h(struct connection_t *);
extern int add_subnet_h(connection_t *); extern bool add_subnet_h(struct connection_t *);
extern int del_subnet_h(connection_t *); extern bool del_subnet_h(struct connection_t *);
extern int add_edge_h(connection_t *); extern bool add_edge_h(struct connection_t *);
extern int del_edge_h(connection_t *); extern bool del_edge_h(struct connection_t *);
extern int key_changed_h(connection_t *); extern bool key_changed_h(struct connection_t *);
extern int req_key_h(connection_t *); extern bool req_key_h(struct connection_t *);
extern int ans_key_h(connection_t *); extern bool ans_key_h(struct connection_t *);
extern int tcppacket_h(connection_t *); extern bool tcppacket_h(struct connection_t *);
#endif /* __TINC_PROTOCOL_H__ */ #endif /* __TINC_PROTOCOL_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -39,7 +39,7 @@
#include "utils.h" #include "utils.h"
#include "xalloc.h" #include "xalloc.h"
int send_id(connection_t *c) bool send_id(connection_t *c)
{ {
cp(); cp();
@ -47,25 +47,25 @@ int send_id(connection_t *c)
myself->connection->protocol_version); myself->connection->protocol_version);
} }
int id_h(connection_t *c) bool id_h(connection_t *c)
{ {
char name[MAX_STRING_SIZE]; char name[MAX_STRING_SIZE];
int bla; bool choice;
cp(); cp();
if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if identity is a valid name */ /* 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
c->hostname, "invalid 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 */ /* 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)) { if(strcmp(c->name, name)) {
logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
c->name); c->name);
return -1; return false;
} }
} else } else
c->name = xstrdup(name); c->name = xstrdup(name);
@ -84,7 +84,7 @@ int id_h(connection_t *c)
if(c->protocol_version != myself->connection->protocol_version) { if(c->protocol_version != myself->connection->protocol_version) {
logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"), logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
c->name, c->hostname, c->protocol_version); c->name, c->hostname, c->protocol_version);
return -1; return false;
} }
if(bypass_security) { if(bypass_security) {
@ -97,25 +97,23 @@ int id_h(connection_t *c)
if(!c->config_tree) { if(!c->config_tree) {
init_configuration(&c->config_tree); init_configuration(&c->config_tree);
bla = read_connection_config(c); if(!read_connection_config(c)) {
if(bla) {
logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
c->name); c->name);
return -1; return false;
} }
} }
if(read_rsa_public_key(c)) { if(!read_rsa_public_key(c)) {
return -1; return false;
} }
/* Check some options */ /* 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; 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->options |= OPTION_TCPONLY | OPTION_INDIRECT;
c->allow_request = METAKEY; c->allow_request = METAKEY;
@ -123,10 +121,11 @@ int id_h(connection_t *c)
return send_metakey(c); return send_metakey(c);
} }
int send_metakey(connection_t *c) bool send_metakey(connection_t *c)
{ {
char buffer[MAX_STRING_SIZE]; char buffer[MAX_STRING_SIZE];
int len, x; int len;
bool x;
cp(); 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) { 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)"), logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
/* Convert the encrypted random data to a hexadecimal formatted string */ /* 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->outkey + len - c->outcipher->key_len -
c->outcipher->iv_len); c->outcipher->iv_len);
c->status.encryptout = 1; c->status.encryptout = true;
} }
return x; return x;
} }
int metakey_h(connection_t *c) bool metakey_h(connection_t *c)
{ {
char buffer[MAX_STRING_SIZE]; char buffer[MAX_STRING_SIZE];
int cipher, digest, maclength, compression; 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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
len = RSA_size(myself->connection->rsa_key); len = RSA_size(myself->connection->rsa_key);
@ -222,7 +221,7 @@ int metakey_h(connection_t *c)
if(strlen(buffer) != len * 2) { if(strlen(buffer) != len * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength"); logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
return -1; return false;
} }
/* Allocate buffers for the meta key */ /* 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() */ 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)"), logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
ifdebug(SCARY_THINGS) { ifdebug(SCARY_THINGS) {
@ -260,7 +259,7 @@ int metakey_h(connection_t *c)
if(!c->incipher) { if(!c->incipher) {
logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname); logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
return -1; return false;
} }
EVP_DecryptInit(c->inctx, c->incipher, EVP_DecryptInit(c->inctx, c->incipher,
@ -268,7 +267,7 @@ int metakey_h(connection_t *c)
c->inkey + len - c->incipher->key_len - c->inkey + len - c->incipher->key_len -
c->incipher->iv_len); c->incipher->iv_len);
c->status.decryptin = 1; c->status.decryptin = true;
} else { } else {
c->incipher = NULL; c->incipher = NULL;
} }
@ -280,12 +279,12 @@ int metakey_h(connection_t *c)
if(!c->indigest) { if(!c->indigest) {
logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname); 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) { if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0) {
logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname); logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
return -1; return false;
} }
} else { } else {
c->indigest = NULL; c->indigest = NULL;
@ -298,10 +297,10 @@ int metakey_h(connection_t *c)
return send_challenge(c); return send_challenge(c);
} }
int send_challenge(connection_t *c) bool send_challenge(connection_t *c)
{ {
char buffer[MAX_STRING_SIZE]; char buffer[MAX_STRING_SIZE];
int len, x; int len;
cp(); cp();
@ -325,12 +324,10 @@ int send_challenge(connection_t *c)
/* Send the challenge */ /* Send the challenge */
x = send_request(c, "%d %s", CHALLENGE, buffer); return send_request(c, "%d %s", CHALLENGE, buffer);
return x;
} }
int challenge_h(connection_t *c) bool challenge_h(connection_t *c)
{ {
char buffer[MAX_STRING_SIZE]; char buffer[MAX_STRING_SIZE];
int len; int len;
@ -340,7 +337,7 @@ int challenge_h(connection_t *c)
if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) { if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
len = RSA_size(myself->connection->rsa_key); len = RSA_size(myself->connection->rsa_key);
@ -350,7 +347,7 @@ int challenge_h(connection_t *c)
if(strlen(buffer) != len * 2) { if(strlen(buffer) != len * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, "wrong challenge length"); c->hostname, "wrong challenge length");
return -1; return false;
} }
/* Allocate buffers for the challenge */ /* Allocate buffers for the challenge */
@ -369,7 +366,7 @@ int challenge_h(connection_t *c)
return send_chal_reply(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]; char hash[EVP_MAX_MD_SIZE * 2 + 1];
EVP_MD_CTX ctx; EVP_MD_CTX ctx;
@ -393,7 +390,7 @@ int send_chal_reply(connection_t *c)
return send_request(c, "%d %s", CHAL_REPLY, hash); 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 hishash[MAX_STRING_SIZE];
char myhash[EVP_MAX_MD_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) { if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if the length of the hash is all right */ /* 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) { if(strlen(hishash) != c->outdigest->md_size * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, _("wrong challenge reply length")); c->hostname, _("wrong challenge reply length"));
return -1; return false;
} }
/* Convert the hash to binary format */ /* 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); logger(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
} }
return -1; return false;
} }
/* Identity has now been positively verified. /* Identity has now been positively verified.
@ -449,12 +446,11 @@ int chal_reply_h(connection_t *c)
return send_ack(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 /* ACK message contains rest of the information the other end needs
to create node_t and edge_t structures. */ to create node_t and edge_t structures. */
int x;
struct timeval now; struct timeval now;
cp(); cp();
@ -462,13 +458,9 @@ int send_ack(connection_t *c)
/* Estimate weight */ /* Estimate weight */
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
c->estimated_weight = c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
(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);
return x; return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options);
} }
static void send_everything(connection_t *c) 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 hisport[MAX_STRING_SIZE];
char *hisaddress, *dummy; 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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if we already have a node_t for him */ /* 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. */ /* 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"), ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
n->name, n->hostname); 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 */ /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
graph(); graph();
} }
@ -537,7 +529,7 @@ int ack_h(connection_t *c)
/* Activate this connection */ /* Activate this connection */
c->allow_request = ALL; c->allow_request = ALL;
c->status.active = 1; c->status.active = true;
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
c->hostname); c->hostname);
@ -570,5 +562,5 @@ int ack_h(connection_t *c)
graph(); graph();
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -36,9 +36,9 @@
#include "utils.h" #include "utils.h"
#include "xalloc.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; char *address, *port;
cp(); cp();
@ -54,7 +54,7 @@ int send_add_edge(connection_t *c, edge_t *e)
return x; return x;
} }
int add_edge_h(connection_t *c) bool add_edge_h(connection_t *c)
{ {
edge_t *e; edge_t *e;
node_t *from, *to; 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) { from_name, to_name, to_address, to_port, &options, &weight) != 6) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if names are valid */ /* 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
c->hostname, _("invalid 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
c->hostname, _("invalid name")); c->hostname, _("invalid name"));
return -1; return false;
} }
if(seen_request(c->buffer)) if(seen_request(c->buffer))
return 0; return true;
/* Lookup nodes */ /* 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"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
"ADD_EDGE", c->name, c->hostname); "ADD_EDGE", c->name, c->hostname);
send_add_edge(c, e); send_add_edge(c, e);
return 0; return true;
} else { } else {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
"ADD_EDGE", c->name, c->hostname); "ADD_EDGE", c->name, c->hostname);
@ -132,7 +132,7 @@ int add_edge_h(connection_t *c)
graph(); graph();
} }
} else } else
return 0; return true;
} else if(from == myself) { } else if(from == myself) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
"ADD_EDGE", c->name, c->hostname); "ADD_EDGE", c->name, c->hostname);
@ -141,7 +141,7 @@ int add_edge_h(connection_t *c)
e->to = to; e->to = to;
send_del_edge(c, e); send_del_edge(c, e);
free_edge(e); free_edge(e);
return 0; return true;
} }
e = new_edge(); e = new_edge();
@ -160,10 +160,10 @@ int add_edge_h(connection_t *c)
graph(); 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(); cp();
@ -171,7 +171,7 @@ int send_del_edge(connection_t *c, edge_t *e)
e->from->name, e->to->name); e->from->name, e->to->name);
} }
int del_edge_h(connection_t *c) bool del_edge_h(connection_t *c)
{ {
edge_t *e; edge_t *e;
char from_name[MAX_STRING_SIZE]; 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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if names are valid */ /* 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
c->hostname, _("invalid 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
c->hostname, _("invalid name")); c->hostname, _("invalid name"));
return -1; return false;
} }
if(seen_request(c->buffer)) if(seen_request(c->buffer))
return 0; return true;
/* Lookup nodes */ /* Lookup nodes */
@ -210,7 +210,7 @@ int del_edge_h(connection_t *c)
if(!from) { if(!from) {
ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname); "DEL_EDGE", c->name, c->hostname);
return 0; return true;
} }
to = lookup_node(to_name); to = lookup_node(to_name);
@ -218,7 +218,7 @@ int del_edge_h(connection_t *c)
if(!to) { if(!to) {
ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname); "DEL_EDGE", c->name, c->hostname);
return 0; return true;
} }
/* Check if edge exists */ /* Check if edge exists */
@ -228,14 +228,14 @@ int del_edge_h(connection_t *c)
if(!e) { if(!e) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname); "DEL_EDGE", c->name, c->hostname);
return 0; return true;
} }
if(e->from == myself) { if(e->from == myself) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
"DEL_EDGE", c->name, c->hostname); "DEL_EDGE", c->name, c->hostname);
send_add_edge(c, e); /* Send back a correction */ send_add_edge(c, e); /* Send back a correction */
return 0; return true;
} }
/* Tell the rest about the deleted edge */ /* Tell the rest about the deleted edge */
@ -250,5 +250,5 @@ int del_edge_h(connection_t *c)
graph(); graph();
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -32,9 +32,9 @@
#include "utils.h" #include "utils.h"
#include "xalloc.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(); cp();
@ -43,12 +43,12 @@ int send_key_changed(connection_t *c, node_t *n)
*/ */
if(n == myself && !mykeyused) if(n == myself && !mykeyused)
return 0; return true;
return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name); 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]; char name[MAX_STRING_SIZE];
node_t *n; node_t *n;
@ -58,38 +58,38 @@ int key_changed_h(connection_t *c)
if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) { if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED", logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
if(seen_request(c->buffer)) if(seen_request(c->buffer))
return 0; return true;
n = lookup_node(name); n = lookup_node(name);
if(!n) { if(!n) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
"KEY_CHANGED", c->name, c->hostname, name); "KEY_CHANGED", c->name, c->hostname, name);
return -1; return false;
} }
n->status.validkey = 0; n->status.validkey = false;
n->status.waitingforkey = 0; n->status.waitingforkey = false;
/* Tell the others */ /* Tell the others */
forward_request(c); 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(); cp();
return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name); 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 from_name[MAX_STRING_SIZE];
char to_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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
from = lookup_node(from_name); from = lookup_node(from_name);
@ -108,7 +108,7 @@ int req_key_h(connection_t *c)
if(!from) { if(!from) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), 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); "REQ_KEY", c->name, c->hostname, from_name);
return -1; return false;
} }
to = lookup_node(to_name); to = lookup_node(to_name);
@ -116,13 +116,13 @@ int req_key_h(connection_t *c)
if(!to) { if(!to) {
logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), 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); "REQ_KEY", c->name, c->hostname, to_name);
return -1; return false;
} }
/* Check if this key request is for us */ /* Check if this key request is for us */
if(to == myself) { /* Yes, send our own key back */ if(to == myself) { /* Yes, send our own key back */
mykeyused = 1; mykeyused = true;
from->received_seqno = 0; from->received_seqno = 0;
memset(from->late, 0, sizeof(from->late)); memset(from->late, 0, sizeof(from->late));
send_ans_key(c, myself, from); 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); 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]; char key[MAX_STRING_SIZE];
@ -149,7 +149,7 @@ int send_ans_key(connection_t *c, node_t *from, node_t *to)
from->compression); from->compression);
} }
int ans_key_h(connection_t *c) bool ans_key_h(connection_t *c)
{ {
char from_name[MAX_STRING_SIZE]; char from_name[MAX_STRING_SIZE];
char to_name[MAX_STRING_SIZE]; char to_name[MAX_STRING_SIZE];
@ -164,7 +164,7 @@ int ans_key_h(connection_t *c)
&compression) != 7) { &compression) != 7) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
from = lookup_node(from_name); from = lookup_node(from_name);
@ -172,7 +172,7 @@ int ans_key_h(connection_t *c)
if(!from) { if(!from) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), 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); "ANS_KEY", c->name, c->hostname, from_name);
return -1; return false;
} }
to = lookup_node(to_name); to = lookup_node(to_name);
@ -180,7 +180,7 @@ int ans_key_h(connection_t *c)
if(!to) { if(!to) {
logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), 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); "ANS_KEY", c->name, c->hostname, to_name);
return -1; return false;
} }
/* Forward it if necessary */ /* Forward it if necessary */
@ -199,8 +199,8 @@ int ans_key_h(connection_t *c)
hex2bin(from->key, from->key, from->keylength); hex2bin(from->key, from->key, from->keylength);
from->key[from->keylength] = '\0'; from->key[from->keylength] = '\0';
from->status.validkey = 1; from->status.validkey = true;
from->status.waitingforkey = 0; from->status.waitingforkey = false;
from->sent_seqno = 0; from->sent_seqno = 0;
/* Check and lookup cipher and digest algorithms */ /* Check and lookup cipher and digest algorithms */
@ -211,13 +211,13 @@ int ans_key_h(connection_t *c)
if(!from->cipher) { if(!from->cipher) {
logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
from->hostname); from->hostname);
return -1; return false;
} }
if(from->keylength != from->cipher->key_len + from->cipher->iv_len) { if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
from->hostname); from->hostname);
return -1; return false;
} }
} else { } else {
from->cipher = NULL; from->cipher = NULL;
@ -231,13 +231,13 @@ int ans_key_h(connection_t *c)
if(!from->digest) { if(!from->digest) {
logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
from->hostname); from->hostname);
return -1; return false;
} }
if(from->maclength > from->digest->md_size || from->maclength < 0) { if(from->maclength > from->digest->md_size || from->maclength < 0) {
logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
from->name, from->hostname); from->name, from->hostname);
return -1; return false;
} }
} else { } else {
from->digest = NULL; from->digest = NULL;
@ -245,7 +245,7 @@ int ans_key_h(connection_t *c)
if(compression < 0 || compression > 11) { if(compression < 0 || compression > 11) {
logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname); logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
return -1; return false;
} }
from->compression = compression; from->compression = compression;
@ -254,5 +254,5 @@ int ans_key_h(connection_t *c)
flush_queue(from); flush_queue(from);
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -33,7 +33,7 @@
/* Status and error notification routines */ /* 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(); 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); return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
} }
int status_h(connection_t *c) bool status_h(connection_t *c)
{ {
int statusno; int statusno;
char statusstring[MAX_STRING_SIZE]; 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) { if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS", logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"), ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"),
c->name, c->hostname, statusno, statusstring); 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(); 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); return send_request(c, "%d %d %s", ERROR, err, errstring);
} }
int error_h(connection_t *c) bool error_h(connection_t *c)
{ {
int err; int err;
char errorstring[MAX_STRING_SIZE]; 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) { if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR", logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
c->name, c->hostname); c->name, c->hostname);
return -1; return false;
} }
ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"), 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); terminate_connection(c, c->status.active);
return 0; return true;
} }
int send_termreq(connection_t *c) bool send_termreq(connection_t *c)
{ {
cp(); cp();
return send_request(c, "%d", TERMREQ); return send_request(c, "%d", TERMREQ);
} }
int termreq_h(connection_t *c) bool termreq_h(connection_t *c)
{ {
cp(); cp();
terminate_connection(c, c->status.active); terminate_connection(c, c->status.active);
return 0; return true;
} }
int send_ping(connection_t *c) bool send_ping(connection_t *c)
{ {
cp(); cp();
c->status.pinged = 1; c->status.pinged = true;
c->last_ping_time = now; c->last_ping_time = now;
return send_request(c, "%d", PING); return send_request(c, "%d", PING);
} }
int ping_h(connection_t *c) bool ping_h(connection_t *c)
{ {
cp(); cp();
return send_pong(c); return send_pong(c);
} }
int send_pong(connection_t *c) bool send_pong(connection_t *c)
{ {
cp(); cp();
return send_request(c, "%d", PONG); return send_request(c, "%d", PONG);
} }
int pong_h(connection_t *c) bool pong_h(connection_t *c)
{ {
cp(); cp();
c->status.pinged = 0; c->status.pinged = false;
/* Succesful connection, reset timeout if this is an outgoing connection. */ /* Succesful connection, reset timeout if this is an outgoing connection. */
if(c->outgoing) if(c->outgoing)
c->outgoing->timeout = 0; c->outgoing->timeout = 0;
return 0; return true;
} }
/* Sending and receiving packets via TCP */ /* 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(); cp();
/* Evil hack. */ /* Evil hack. */
x = send_request(c, "%d %hd", PACKET, packet->len); if(!send_request(c, "%d %hd", PACKET, packet->len))
return false;
if(x)
return x;
return send_meta(c, packet->data, packet->len); return send_meta(c, packet->data, packet->len);
} }
int tcppacket_h(connection_t *c) bool tcppacket_h(connection_t *c)
{ {
short int len; short int len;
@ -174,12 +170,12 @@ int tcppacket_h(connection_t *c)
if(sscanf(c->buffer, "%*d %hd", &len) != 1) { if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */ /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
c->tcplen = len; c->tcplen = len;
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -33,9 +33,9 @@
#include "utils.h" #include "utils.h"
#include "xalloc.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; char *netstr;
cp(); cp();
@ -48,7 +48,7 @@ int send_add_subnet(connection_t *c, subnet_t *subnet)
return x; return x;
} }
int add_subnet_h(connection_t *c) bool add_subnet_h(connection_t *c)
{ {
char subnetstr[MAX_STRING_SIZE]; char subnetstr[MAX_STRING_SIZE];
char name[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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if owner name is a valid */ /* 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
c->hostname, _("invalid name")); c->hostname, _("invalid name"));
return -1; return false;
} }
/* Check if subnet string is valid */ /* Check if subnet string is valid */
@ -78,11 +78,11 @@ int add_subnet_h(connection_t *c)
if(!s) { if(!s) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
c->hostname, _("invalid subnet string")); c->hostname, _("invalid subnet string"));
return -1; return false;
} }
if(seen_request(c->buffer)) if(seen_request(c->buffer))
return 0; return true;
/* Check if the owner of the new subnet is in the connection list */ /* 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)) { if(lookup_subnet(owner, s)) {
free_subnet(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 */ /* 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); "ADD_SUBNET", c->name, c->hostname);
s->owner = myself; s->owner = myself;
send_del_subnet(c, s); send_del_subnet(c, s);
return 0; return true;
} }
/* If everything is correct, add the subnet to the list of the owner */ /* 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); 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; char *netstr;
cp(); cp();
@ -138,7 +138,7 @@ int send_del_subnet(connection_t *c, subnet_t *s)
return x; return x;
} }
int del_subnet_h(connection_t *c) bool del_subnet_h(connection_t *c)
{ {
char subnetstr[MAX_STRING_SIZE]; char subnetstr[MAX_STRING_SIZE];
char name[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) { 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, logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
c->hostname); c->hostname);
return -1; return false;
} }
/* Check if owner name is a valid */ /* 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, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
c->hostname, _("invalid name")); c->hostname, _("invalid name"));
return -1; return false;
} }
/* Check if the owner of the new subnet is in the connection list */ /* 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) { if(!owner) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"), 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); "DEL_SUBNET", c->name, c->hostname, name);
return 0; return true;
} }
/* Check if subnet string is valid */ /* Check if subnet string is valid */
@ -178,11 +178,11 @@ int del_subnet_h(connection_t *c)
if(!s) { if(!s) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
c->hostname, _("invalid subnet string")); c->hostname, _("invalid subnet string"));
return -1; return false;
} }
if(seen_request(c->buffer)) if(seen_request(c->buffer))
return 0; return true;
/* If everything is correct, delete the subnet from the list of the owner */ /* 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) { if(!find) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"), 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); "DEL_SUBNET", c->name, c->hostname, name);
return 0; return true;
} }
/* If we are the owner of this subnet, retaliate with an ADD_SUBNET */ /* 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"), ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
"DEL_SUBNET", c->name, c->hostname); "DEL_SUBNET", c->name, c->hostname);
send_add_subnet(c, find); send_add_subnet(c, find);
return 0; return true;
} }
/* Tell the rest */ /* Tell the rest */
@ -215,5 +215,5 @@ int del_subnet_h(connection_t *c)
subnet_del(owner, find); subnet_del(owner, find);
return 0; return true;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "config.h"
@ -43,7 +43,6 @@
#include "system.h" #include "system.h"
int device_fd = -1; int device_fd = -1;
int device_type;
char *device; char *device;
char *interface; char *interface;
char ifrname[IFNAMSIZ]; char ifrname[IFNAMSIZ];
@ -52,7 +51,7 @@ char *device_info;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
struct ifreq ifr; struct ifreq ifr;
struct sockaddr_ll sa; 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) { if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device_info, logger(LOG_ERR, _("Could not open %s: %s"), device_info,
strerror(errno)); strerror(errno));
return -1; return false;
} }
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
@ -80,7 +79,7 @@ int setup_device(void)
close(device_fd); close(device_fd);
logger(LOG_ERR, _("Can't find interface %s: %s"), interface, logger(LOG_ERR, _("Can't find interface %s: %s"), interface,
strerror(errno)); strerror(errno));
return -1; return false;
} }
memset(&sa, '0', sizeof(sa)); memset(&sa, '0', sizeof(sa));
@ -90,12 +89,12 @@ int setup_device(void)
if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) { if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) {
logger(LOG_ERR, _("Could not bind to %s: %s"), device, strerror(errno)); 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); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -105,7 +104,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -114,7 +113,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(device_fd, packet->data, MTU)) <= 0) { if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->len = lenin; packet->len = lenin;
@ -125,10 +124,10 @@ int read_packet(vpn_packet_t *packet)
device_info); device_info);
} }
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -138,12 +137,12 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data, packet->len) < 0) { if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return 0; return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -54,10 +54,10 @@
#include "subnet.h" #include "subnet.h"
#include "utils.h" #include "utils.h"
int routing_mode = RMODE_ROUTER; rmode_t routing_mode = RMODE_ROUTER;
int priorityinheritance = 0; bool priorityinheritance = false;
int macexpire = 600; int macexpire = 600;
int overwrite_mac = 0; bool overwrite_mac = false;
mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}}; mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
/* RFC 1071 */ /* RFC 1071 */
@ -81,14 +81,14 @@ static uint16_t inet_checksum(void *data, int len, uint16_t prevsum)
return ~checksum; return ~checksum;
} }
static int ratelimit(void) { static bool ratelimit(void) {
static time_t lasttime = 0; static time_t lasttime = 0;
if(lasttime == now) if(lasttime == now)
return 1; return true;
lasttime = now; lasttime = now;
return 0; return false;
} }
static void learn_mac(mac_t *address) static void learn_mac(mac_t *address)

View file

@ -17,27 +17,30 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_ROUTE_H__
#define __TINC_ROUTE_H__ #define __TINC_ROUTE_H__
enum { #include "net.h"
#include "node.h"
typedef enum rmode_t {
RMODE_HUB = 0, RMODE_HUB = 0,
RMODE_SWITCH, RMODE_SWITCH,
RMODE_ROUTER, RMODE_ROUTER,
}; } rmode_t;
extern int routing_mode; extern rmode_t routing_mode;
extern int overwrite_mac; extern bool overwrite_mac;
extern int priorityinheritance; extern bool priorityinheritance;
extern int macexpire; extern int macexpire;
extern mac_t mymac; extern mac_t mymac;
extern void age_mac(void); extern void age_mac(void);
extern void route_incoming(node_t *, vpn_packet_t *); extern void route_incoming(struct node_t *, struct vpn_packet_t *);
extern void route_outgoing(vpn_packet_t *); extern void route_outgoing(struct vpn_packet_t *);
#endif /* __TINC_ROUTE_H__ */ #endif /* __TINC_ROUTE_H__ */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #define DEFAULT_DEVICE "/dev/tun"
int device_fd = -1; int device_fd = -1;
int device_type;
char *device = NULL; char *device = NULL;
char *iface = NULL; char *iface = NULL;
char ifrname[IFNAMSIZ]; char ifrname[IFNAMSIZ];
@ -44,7 +43,7 @@ char *device_info = NULL;
int device_total_in = 0; int device_total_in = 0;
int device_total_out = 0; int device_total_out = 0;
int setup_device(void) bool setup_device(void)
{ {
int ip_fd = -1, if_fd = -1; int ip_fd = -1, if_fd = -1;
int ppa; int ppa;
@ -57,7 +56,7 @@ int setup_device(void)
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
return -1; return false;
} }
ppa = 0; ppa = 0;
@ -69,35 +68,35 @@ int setup_device(void)
if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) { if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
logger(LOG_ERR, _("Could not open /dev/ip: %s"), strerror(errno)); logger(LOG_ERR, _("Could not open /dev/ip: %s"), strerror(errno));
return -1; return false;
} }
/* Assign a new PPA and get its unit number. */ /* Assign a new PPA and get its unit number. */
if((ppa = ioctl(device_fd, TUNNEWPPA, ppa)) < 0) { if((ppa = ioctl(device_fd, TUNNEWPPA, ppa)) < 0) {
logger(LOG_ERR, _("Can't assign new interface: %s"), strerror(errno)); logger(LOG_ERR, _("Can't assign new interface: %s"), strerror(errno));
return -1; return false;
} }
if((if_fd = open(device, O_RDWR, 0)) < 0) { if((if_fd = open(device, O_RDWR, 0)) < 0) {
logger(LOG_ERR, _("Could not open %s twice: %s"), device, logger(LOG_ERR, _("Could not open %s twice: %s"), device,
strerror(errno)); strerror(errno));
return -1; return false;
} }
if(ioctl(if_fd, I_PUSH, "ip") < 0) { if(ioctl(if_fd, I_PUSH, "ip") < 0) {
logger(LOG_ERR, _("Can't push IP module: %s"), strerror(errno)); 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 */ /* Assign ppa according to the unit number returned by tun device */
if(ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0) { if(ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0) {
logger(LOG_ERR, _("Can't set PPA %d: %s"), ppa, strerror(errno)); 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) { if(ioctl(ip_fd, I_LINK, if_fd) < 0) {
logger(LOG_ERR, _("Can't link TUN device to IP: %s"), strerror(errno)); 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)) 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); logger(LOG_INFO, _("%s is a %s"), device, device_info);
return 0; return true;
} }
void close_device(void) void close_device(void)
@ -117,7 +116,7 @@ void close_device(void)
close(device_fd); close(device_fd);
} }
int read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)
{ {
int lenin; int lenin;
@ -126,7 +125,7 @@ int read_packet(vpn_packet_t *packet)
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) { if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info, logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno)); device, strerror(errno));
return -1; return false;
} }
packet->data[12] = 0x08; 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, ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info); device_info);
return 0; return true;
} }
int write_packet(vpn_packet_t *packet) bool write_packet(vpn_packet_t *packet)
{ {
cp(); cp();
@ -152,12 +151,12 @@ int write_packet(vpn_packet_t *packet)
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) { 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, logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, packet->len,
strerror(errno)); strerror(errno));
return -1; return false;
} }
device_total_out += packet->len; device_total_out += packet->len;
return 0; return true;
} }
void dump_device_stats(void) void dump_device_stats(void)

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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__ #ifndef __TINC_SUBNET_H__
@ -25,12 +25,12 @@
#include "net.h" #include "net.h"
enum { typedef enum subnet_type_t {
SUBNET_MAC = 0, SUBNET_MAC = 0,
SUBNET_IPV4, SUBNET_IPV4,
SUBNET_IPV6, SUBNET_IPV6,
SUBNET_TYPES /* Guardian */ SUBNET_TYPES /* Guardian */
}; } subnet_type_t;
typedef struct subnet_mac_t { typedef struct subnet_mac_t {
mac_t address; mac_t address;
@ -53,7 +53,7 @@ typedef struct subnet_t {
struct node_t *owner; /* the owner of this subnet */ struct node_t *owner; /* the owner of this subnet */
struct node_t *uplink; /* the uplink which we should send packets to for 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: */ /* And now for the actual subnet: */

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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" #include "system.h"
@ -51,10 +51,10 @@
char *program_name = NULL; char *program_name = NULL;
/* If nonzero, display usage information and exit. */ /* 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. */ /* 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. */ /* If nonzero, it will attempt to kill a running tincd and exit. */
int kill_tincd = 0; int kill_tincd = 0;
@ -63,40 +63,39 @@ int kill_tincd = 0;
int generate_keys = 0; int generate_keys = 0;
/* If nonzero, use null ciphers and skip all key exchanges. */ /* 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. */ /* If nonzero, disable swapping for this process. */
int do_mlock = 0; bool do_mlock = false;
/* If nonzero, write log entries to a separate file. */ /* 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 *identname = NULL; /* program name for syslog */
char *pidfilename = NULL; /* pid file location */ char *pidfilename = NULL; /* pid file location */
char *logfilename = NULL; /* log file location */ char *logfilename = NULL; /* log file location */
char **g_argv; /* a copy of the cmdline arguments */ char **g_argv; /* a copy of the cmdline arguments */
char **environment; /* A pointer to the environment on char **environment; /* A pointer to the environment on startup */
startup */
static struct option const long_options[] = { static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'}, {"config", required_argument, NULL, 'c'},
{"kill", optional_argument, NULL, 'k'}, {"kill", optional_argument, NULL, 'k'},
{"net", required_argument, NULL, 'n'}, {"net", required_argument, NULL, 'n'},
{"help", no_argument, &show_help, 1}, {"help", no_argument, NULL, 1},
{"version", no_argument, &show_version, 1}, {"version", no_argument, NULL, 2},
{"no-detach", no_argument, &do_detach, 0}, {"no-detach", no_argument, NULL, 'D'},
{"generate-keys", optional_argument, NULL, 'K'}, {"generate-keys", optional_argument, NULL, 'K'},
{"debug", optional_argument, NULL, 'd'}, {"debug", optional_argument, NULL, 'd'},
{"bypass-security", no_argument, &bypass_security, 1}, {"bypass-security", no_argument, NULL, 3},
{"mlock", no_argument, &do_mlock, 1}, {"mlock", no_argument, NULL, 'L'},
{"logfile", optional_argument, NULL, 'F'}, {"logfile", optional_argument, NULL, 4},
{"pidfile", required_argument, NULL, 'P'}, {"pidfile", required_argument, NULL, 5},
{NULL, 0, NULL, 0} {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"), fprintf(stderr, _("Try `%s --help\' for more information.\n"),
program_name); program_name);
else { else {
@ -133,11 +132,11 @@ static void parse_options(int argc, char **argv, char **envp)
break; break;
case 'D': /* no detach */ case 'D': /* no detach */
do_detach = 0; do_detach = false;
break; break;
case 'L': /* no detach */ case 'L': /* no detach */
do_mlock = 1; do_mlock = true;
break; break;
case 'd': /* inc debug level */ case 'd': /* inc debug level */
@ -171,7 +170,7 @@ static void parse_options(int argc, char **argv, char **envp)
if(!kill_tincd) { 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"), fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
optarg); optarg);
usage(1); usage(true);
} }
} }
} else } else
@ -189,7 +188,7 @@ static void parse_options(int argc, char **argv, char **envp)
if(generate_keys < 512) { if(generate_keys < 512) {
fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"), fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
optarg); optarg);
usage(1); usage(true);
} }
generate_keys &= ~7; /* Round it to bytes */ generate_keys &= ~7; /* Round it to bytes */
@ -197,18 +196,30 @@ static void parse_options(int argc, char **argv, char **envp)
generate_keys = 1024; generate_keys = 1024;
break; break;
case 'F': /* write log entries to a file */ case 1: /* show help */
use_logfile = 1; 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) if(optarg)
logfilename = xstrdup(optarg); logfilename = xstrdup(optarg);
break; break;
case 'P': /* write PID to a file */ case 5: /* write PID to a file */
pidfilename = xstrdup(optarg); pidfilename = xstrdup(optarg);
break; break;
case '?': case '?':
usage(1); usage(true);
default: default:
break; 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 Generate a public/private RSA keypair, and ask for a file to store
them in. them in.
*/ */
static int keygen(int bits) static bool keygen(int bits)
{ {
RSA *rsa_key; RSA *rsa_key;
FILE *f; FILE *f;
@ -269,34 +280,15 @@ static int keygen(int bits)
if(!rsa_key) { if(!rsa_key) {
fprintf(stderr, _("Error during key generation!\n")); fprintf(stderr, _("Error during key generation!\n"));
return -1; return false;
} else } else
fprintf(stderr, _("Done.\n")); 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");
if(!f)
return -1;
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);
fclose(f);
free(filename);
asprintf(&filename, "%s/rsa_key.priv", confbase); asprintf(&filename, "%s/rsa_key.priv", confbase);
f = ask_and_safe_open(filename, _("private RSA key"), "a"); f = ask_and_safe_open(filename, _("private RSA key"), true, "a");
if(!f) if(!f)
return -1; return false;
if(ftell(f)) if(ftell(f))
fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n")); fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
@ -305,7 +297,26 @@ static int keygen(int bits)
fclose(f); fclose(f);
free(filename); free(filename);
return 0; 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 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);
fclose(f);
free(filename);
return true;
} }
/* /*
@ -360,10 +371,10 @@ int main(int argc, char **argv, char **envp)
} }
if(show_help) if(show_help)
usage(0); usage(false);
if(kill_tincd) if(kill_tincd)
exit(kill_other(kill_tincd)); exit(!kill_other(kill_tincd));
openlogger("tinc", LOGMODE_STDERR); openlogger("tinc", LOGMODE_STDERR);
@ -393,10 +404,10 @@ int main(int argc, char **argv, char **envp)
if(generate_keys) { if(generate_keys) {
read_server_config(); read_server_config();
exit(keygen(generate_keys)); exit(!keygen(generate_keys));
} }
if(read_server_config()) if(!read_server_config())
exit(1); exit(1);
if(lzo_init() != LZO_E_OK) { if(lzo_init() != LZO_E_OK) {
@ -404,11 +415,11 @@ int main(int argc, char **argv, char **envp)
exit(1); exit(1);
} }
if(detach()) if(!detach())
exit(0); exit(1);
for(;;) { for(;;) {
if(!setup_network_connections()) { if(setup_network_connections()) {
main_loop(); main_loop();
cleanup_and_exit(1); cleanup_and_exit(1);
} }