along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: conf.c,v 1.9.4.67 2003/07/18 14:10:27 guus Exp $
+ $Id: conf.c,v 1.9.4.68 2003/07/22 20:55:19 guus Exp $
*/
#include "system.h"
return NULL;
}
-int get_config_bool(config_t *cfg, int *result)
+bool get_config_bool(config_t *cfg, bool *result)
{
cp();
if(!cfg)
- return 0;
+ return false;
if(!strcasecmp(cfg->value, "yes")) {
- *result = 1;
- return 1;
+ *result = true;
+ return true;
} else if(!strcasecmp(cfg->value, "no")) {
- *result = 0;
- return 1;
+ *result = false;
+ return true;
}
logger(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line);
- return 0;
+ return false;
}
-int get_config_int(config_t *cfg, int *result)
+bool get_config_int(config_t *cfg, int *result)
{
cp();
if(!cfg)
- return 0;
+ return false;
if(sscanf(cfg->value, "%d", result) == 1)
- return 1;
+ return true;
logger(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line);
- return 0;
+ return false;
}
-int get_config_string(config_t *cfg, char **result)
+bool get_config_string(config_t *cfg, char **result)
{
cp();
if(!cfg)
- return 0;
+ return false;
*result = xstrdup(cfg->value);
- return 1;
+ return true;
}
-int get_config_address(config_t *cfg, struct addrinfo **result)
+bool get_config_address(config_t *cfg, struct addrinfo **result)
{
struct addrinfo *ai;
cp();
if(!cfg)
- return 0;
+ return false;
ai = str2addrinfo(cfg->value, NULL, 0);
if(ai) {
*result = ai;
- return 1;
+ return true;
}
logger(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line);
- return 0;
+ return false;
}
-int get_config_subnet(config_t *cfg, subnet_t ** result)
+bool get_config_subnet(config_t *cfg, subnet_t ** result)
{
subnet_t *subnet;
cp();
if(!cfg)
- return 0;
+ return false;
subnet = str2net(cfg->value);
if(!subnet) {
logger(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line);
- return 0;
+ return false;
}
/* Teach newbies what subnets are... */
if(((subnet->type == SUBNET_IPV4)
- && maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t)))
+ && !maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t)))
|| ((subnet->type == SUBNET_IPV6)
- && maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) {
+ && !maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) {
logger(LOG_ERR, _ ("Network address and prefix length do not match for configuration variable %s in %s line %d"),
cfg->variable, cfg->file, cfg->line);
free(subnet);
- return 0;
+ return false;
}
*result = subnet;
- return 1;
+ return true;
}
/*
FILE *fp;
char *buffer, *line;
char *variable, *value;
- int lineno = 0, ignore = 0;
+ int lineno = 0;
+ bool ignore = false;
config_t *cfg;
size_t bufsize;
continue; /* comment: ignore */
if(!strcmp(variable, "-----BEGIN"))
- ignore = 1;
+ ignore = true;
if(!ignore) {
value = strtok(NULL, "\t\n\r =");
}
if(!strcmp(variable, "-----END"))
- ignore = 0;
+ ignore = false;
}
free(buffer);
return err;
}
-int read_server_config()
+bool read_server_config()
{
char *fname;
int x;
free(fname);
- return x;
+ return x == 0;
}
-int is_safe_path(const char *file)
+bool is_safe_path(const char *file)
{
#if !(defined(HAVE_CYGWIN) || defined(HAVE_MINGW))
char *p;
if(*file != '/') {
logger(LOG_ERR, _("`%s' is not an absolute path"), file);
- return 0;
+ return false;
}
p = strrchr(file, '/');
check1:
if(lstat(f, &s) < 0) {
logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
- return 0;
+ return false;
}
if(s.st_uid != geteuid()) {
logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"),
f, s.st_uid, geteuid());
- return 0;
+ return false;
}
if(S_ISLNK(s.st_mode)) {
if(readlink(f, l, MAXBUFSIZE) < 0) {
logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f,
strerror(errno));
- return 0;
+ return false;
}
f = l;
check2:
if(lstat(f, &s) < 0 && errno != ENOENT) {
logger(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
- return 0;
+ return false;
}
if(errno == ENOENT)
- return 1;
+ return true;
if(s.st_uid != geteuid()) {
logger(LOG_ERR, _("`%s' is owned by UID %d instead of %d"),
f, s.st_uid, geteuid());
- return 0;
+ return false;
}
if(S_ISLNK(s.st_mode)) {
if(readlink(f, l, MAXBUFSIZE) < 0) {
logger(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f,
strerror(errno));
- return 0;
+ return false;
}
f = l;
if(s.st_mode & 0007) {
/* Accessible by others */
logger(LOG_ERR, _("`%s' has unsecure permissions"), f);
- return 0;
+ return false;
}
#endif
- return 1;
+ return true;
}
-FILE *ask_and_safe_open(const char *filename, const char *what,
- const char *mode)
+FILE *ask_and_safe_open(const char *filename, const char *what, bool safe, const char *mode)
{
FILE *r;
char *directory;
}
/* Then check the file for nasty attacks */
- if(!is_safe_path(fn)) { /* Do not permit any directories that are readable or writeable by other users. */
- fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n"
- "I will not create or overwrite this file.\n"), fn);
- fclose(r);
- free(fn);
- return NULL;
+ if(safe) {
+ if(!is_safe_path(fn)) { /* Do not permit any directories that are readable or writeable by other users. */
+ fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n"
+ "I will not create or overwrite this file.\n"), fn);
+ fclose(r);
+ free(fn);
+ return NULL;
+ }
}
free(fn);
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: conf.h,v 1.6.4.39 2003/07/17 15:06:26 guus Exp $
+ $Id: conf.h,v 1.6.4.40 2003/07/22 20:55:19 guus Exp $
*/
#ifndef __TINC_CONF_H__
#define __TINC_CONF_H__
#include "avl_tree.h"
-#include "subnet.h"
typedef struct config_t {
char *variable;
int line;
} config_t;
+#include "subnet.h"
+
extern avl_tree_t *config_tree;
extern int pingtimeout;
extern int maxtimeout;
-extern int bypass_security;
+extern bool bypass_security;
extern char *confbase;
extern char *netname;
extern void config_add(avl_tree_t *, config_t *);
extern config_t *lookup_config(avl_tree_t *, char *);
extern config_t *lookup_config_next(avl_tree_t *, config_t *);
-extern int get_config_bool(config_t *, int *);
-extern int get_config_int(config_t *, int *);
-extern int get_config_string(config_t *, char **);
-extern int get_config_address(config_t *, struct addrinfo **);
-extern int get_config_subnet(config_t *, struct subnet_t **);
+extern bool get_config_bool(config_t *, bool *);
+extern bool get_config_int(config_t *, int *);
+extern bool get_config_string(config_t *, char **);
+extern bool get_config_address(config_t *, struct addrinfo **);
+extern bool get_config_subnet(config_t *, struct subnet_t **);
extern int read_config_file(avl_tree_t *, const char *);
-extern int read_server_config(void);
-extern FILE *ask_and_safe_open(const char *, const char *, const char *);
-extern int is_safe_path(const char *);
+extern bool read_server_config(void);
+extern FILE *ask_and_safe_open(const char *, const char *, bool, const char *);
+extern bool is_safe_path(const char *);
#endif /* __TINC_CONF_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: connection.c,v 1.1.2.40 2003/07/17 15:06:26 guus Exp $
+ $Id: connection.c,v 1.1.2.41 2003/07/22 20:55:19 guus Exp $
*/
#include "system.h"
logger(LOG_DEBUG, _("End of connections."));
}
-int read_connection_config(connection_t *c)
+bool read_connection_config(connection_t *c)
{
char *fname;
int x;
x = read_config_file(c->config_tree, fname);
free(fname);
- return x;
+ return x == 0;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: connection.h,v 1.1.2.34 2003/07/17 15:06:26 guus Exp $
+ $Id: connection.h,v 1.1.2.35 2003/07/22 20:55:19 guus Exp $
*/
#ifndef __TINC_CONNECTION_H__
#include <openssl/evp.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_TCPONLY 0x0002
int unused:18;
} connection_status_t;
+#include "edge.h"
+#include "list.h"
+#include "net.h"
+#include "node.h"
+
typedef struct connection_t {
char *name; /* name he claims to have */
- sockaddr_t address; /* his real (internet) ip */
+ union sockaddr_t address; /* his real (internet) ip */
char *hostname; /* the hostname of its real ip */
int protocol_version; /* used protocol */
extern void connection_add(connection_t *);
extern void connection_del(connection_t *);
extern void dump_connections(void);
-extern int read_connection_config(connection_t *);
+extern bool read_connection_config(connection_t *);
#endif /* __TINC_CONNECTION_H__ */
along with this program; if not, write to the Free Software\r
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
\r
- $Id: device.c,v 1.1.2.9 2003/07/18 13:41:35 guus Exp $\r
+ $Id: device.c,v 1.1.2.10 2003/07/22 20:55:20 guus Exp $\r
*/\r
\r
#include "system.h"\r
pid_t reader_pid;\r
int sp[2];\r
\r
-int setup_device(void)\r
+bool setup_device(void)\r
{\r
HKEY key, key2, adapterkey;\r
int i;\r
\r
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, (OSTYPE > 4 ? NETCARD_REG_KEY_2000 : NETCARD_REG_KEY), 0, KEY_READ, &key)) {\r
logger(LOG_ERR, _("Unable to read registry"));\r
- return -1;\r
+ return false;\r
}\r
\r
for (i = 0; ; i++) {\r
\r
if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) {\r
logger(LOG_ERR, _("Unable to read registry"));\r
- return -1;\r
+ return false;\r
}\r
\r
len = sizeof(productname);\r
\r
if(!found) {\r
logger(LOG_ERR, _("No CIPE adapters found!"));\r
- return -1;\r
+ return false;\r
}\r
\r
/* Get adapter name */\r
\r
if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) {\r
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
reader_pid = fork();\r
\r
if(reader_pid == -1) {\r
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
if(!reader_pid) {\r
\r
if(handle == INVALID_HANDLE_VALUE) {\r
logger(LOG_ERR, _("Could not open CIPE tap device for writing!"));\r
- return -1;\r
+ return false;\r
}\r
\r
device_fd = sp[0];\r
read(device_fd, &gelukt, 1);\r
if(gelukt != 1) {\r
logger(LOG_DEBUG, "Tap reader failed!");\r
- return -1;\r
+ return false;\r
}\r
\r
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))\r
\r
logger(LOG_INFO, _("%s is a %s"), device, device_info);\r
\r
- return 0;\r
+ return false;\r
}\r
\r
void close_device(void)\r
kill(reader_pid, SIGKILL);\r
}\r
\r
-int read_packet(vpn_packet_t *packet)\r
+bool read_packet(vpn_packet_t *packet)\r
{\r
int lenin;\r
\r
if((lenin = read(sp[0], packet->data, MTU)) <= 0) {\r
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,\r
device, strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
packet->len = lenin;\r
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,\r
device_info);\r
\r
- return 0;\r
+ return true;\r
}\r
\r
-int write_packet(vpn_packet_t *packet)\r
+bool write_packet(vpn_packet_t *packet)\r
{\r
int lenout;\r
\r
\r
if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) {\r
logger(LOG_ERR, "Error while writing to %s %s", device_info, device);\r
- return -1;\r
+ return false;\r
}\r
\r
device_total_out += packet->len;\r
\r
- return 0;\r
+ return true;\r
}\r
\r
void dump_device_stats(void)\r
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.9 2003/07/18 13:41:36 guus Exp $
+ $Id: device.c,v 1.1.2.10 2003/07/22 20:55:21 guus Exp $
*/
#include "system.h"
#define DEFAULT_DEVICE "/dev/tun0"
int device_fd = -1;
-int device_type;
char *device;
char *iface;
char *device_info;
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
cp();
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
device_info = _("MacOS/X tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
packet->data[12] = 0x08;
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
packet->len, device_info);
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
+
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.h,v 1.1.2.10 2003/07/18 13:45:06 guus Exp $
+ $Id: device.h,v 1.1.2.11 2003/07/22 20:55:19 guus Exp $
*/
#ifndef __TINC_DEVICE_H__
#define __TINC_DEVICE_H__
+#include "net.h"
+
extern int device_fd;
extern char *device;
extern char *iface;
-extern int setup_device(void);
+extern bool setup_device(void);
extern void close_device(void);
-extern int read_packet(vpn_packet_t *);
-extern int write_packet(vpn_packet_t *);
+extern bool read_packet(struct vpn_packet_t *);
+extern bool write_packet(struct vpn_packet_t *);
extern void dump_device_stats(void);
#endif /* __TINC_DEVICE_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.12 2003/07/18 13:41:36 guus Exp $
+ $Id: device.c,v 1.1.2.13 2003/07/22 20:55:21 guus Exp $
*/
#include "system.h"
#define DEFAULT_DEVICE "/dev/tap0"
int device_fd = -1;
-int device_type;
char *device;
char *iface;
char *device_info;
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
cp();
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
device_info = _("FreeBSD tap device");
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
packet->len = lenin;
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
packet->len, device_info);
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
+
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: graph.c,v 1.1.2.26 2003/07/18 13:45:06 guus Exp $
+ $Id: graph.c,v 1.1.2.27 2003/07/22 20:55:19 guus Exp $
*/
/* We need to generate two trees from the graph:
connection_t *c;
int nodes = 0;
int safe_edges = 0;
- int skipped;
+ bool skipped;
cp();
for(node = connection_tree->head; node; node = node->next) {
c = (connection_t *) node->data;
- c->status.mst = 0;
+ c->status.mst = false;
}
/* Do we have something to do at all? */
for(node = node_tree->head; node; node = node->next) {
n = (node_t *) node->data;
- n->status.visited = 0;
+ n->status.visited = false;
nodes++;
}
/* Starting point */
- ((edge_t *) edge_weight_tree->head->data)->from->status.visited = 1;
+ ((edge_t *) edge_weight_tree->head->data)->from->status.visited = true;
/* Add safe edges */
- for(skipped = 0, node = edge_weight_tree->head; node; node = next) {
+ for(skipped = false, node = edge_weight_tree->head; node; node = next) {
next = node->next;
e = (edge_t *) node->data;
if(!e->reverse || e->from->status.visited == e->to->status.visited) {
- skipped = 1;
+ skipped = true;
continue;
}
- e->from->status.visited = 1;
- e->to->status.visited = 1;
+ e->from->status.visited = true;
+ e->to->status.visited = true;
if(e->connection)
- e->connection->status.mst = 1;
+ e->connection->status.mst = true;
if(e->reverse->connection)
- e->reverse->connection->status.mst = 1;
+ e->reverse->connection->status.mst = true;
safe_edges++;
e->to->name, e->weight);
if(skipped) {
- skipped = 0;
+ skipped = false;
next = edge_weight_tree->head;
continue;
}
edge_t *e;
node_t *n;
avl_tree_t *todo_tree;
- int indirect;
+ bool indirect;
char *name;
char *address, *port;
char *envp[7];
for(node = node_tree->head; node; node = node->next) {
n = (node_t *) node->data;
- n->status.visited = 0;
- n->status.indirect = 1;
+ n->status.visited = false;
+ n->status.indirect = true;
}
/* Begin with myself */
- myself->status.visited = 1;
- myself->status.indirect = 0;
+ myself->status.visited = true;
+ myself->status.indirect = false;
myself->nexthop = myself;
myself->via = myself;
node = avl_alloc_node();
&& (!e->to->status.indirect || indirect))
continue;
- e->to->status.visited = 1;
+ e->to->status.visited = true;
e->to->status.indirect = indirect;
e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
e->to->via = indirect ? n->via : e->to;
n->name, n->hostname);
}
- n->status.validkey = 0;
- n->status.waitingforkey = 0;
+ n->status.validkey = false;
+ n->status.waitingforkey = false;
asprintf(&envp[0], "NETNAME=%s", netname ? : "");
asprintf(&envp[1], "DEVICE=%s", device ? : "");
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.19 2003/07/18 13:41:36 guus Exp $
+ $Id: device.c,v 1.1.2.20 2003/07/22 20:55:21 guus Exp $
*/
#include "system.h"
#include "route.h"
#include "utils.h"
-enum {
+typedef enum device_type_t {
DEVICE_TYPE_ETHERTAP,
DEVICE_TYPE_TUN,
DEVICE_TYPE_TAP,
-};
+} device_type_t;
int device_fd = -1;
-int device_type;
+device_type_t device_type;
char *device;
char *iface;
char ifrname[IFNAMSIZ];
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
struct ifreq ifr;
if(device_fd < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
#ifdef HAVE_TUNTAP
#endif
{
if(routing_mode == RMODE_ROUTER)
- overwrite_mac = 1;
+ overwrite_mac = true;
device_info = _("Linux ethertap device");
device_type = DEVICE_TYPE_ETHERTAP;
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno));
- return -1;
+ return false;
}
packet->len = lenin + 10;
if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno));
- return -1;
+ return false;
}
packet->len = lenin;
if(lenin <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"),
device_info, device, strerror(errno));
- return -1;
+ return false;
}
packet->len = lenin - 2;
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info);
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
break;
case DEVICE_TYPE_TAP:
if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
break;
case DEVICE_TYPE_ETHERTAP:
if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
break;
}
device_total_out += packet->len;
- return 0;
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: logger.c,v 1.1.2.4 2003/07/17 15:06:26 guus Exp $
+ $Id: logger.c,v 1.1.2.5 2003/07/22 20:55:19 guus Exp $
*/
#include "system.h"
#include "conf.h"
#include "logger.h"
-int debug_level = DEBUG_NOTHING;
-static int logmode = LOGMODE_STDERR;
+debug_t debug_level = DEBUG_NOTHING;
+static logmode_t logmode = LOGMODE_STDERR;
static pid_t logpid;
extern char *logfilename;
static FILE *logfile = NULL;
static const char *logident = NULL;
-void openlogger(const char *ident, int mode) {
+void openlogger(const char *ident, logmode_t mode) {
logident = ident;
logmode = mode;
switch(mode) {
+ case LOGMODE_NULL:
+ break;
case LOGMODE_STDERR:
logpid = getpid();
break;
va_start(ap, format);
switch(logmode) {
+ case LOGMODE_NULL:
+ break;
case LOGMODE_STDERR:
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
void closelogger(void) {
switch(logmode) {
+ case LOGMODE_NULL:
+ case LOGMODE_STDERR:
+ break;
case LOGMODE_FILE:
fclose(logfile);
break;
#ifndef __TINC_LOGGER_H__
-enum {
+typedef enum debug_t {
DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */
DEBUG_ALWAYS = 0,
DEBUG_CONNECTIONS = 1, /* Show (dis)connects of other tinc daemons via TCP */
DEBUG_TRAFFIC = 5, /* Show network traffic information */
DEBUG_PACKET = 6, /* Show contents of each packet that is being sent/received */
DEBUG_SCARY_THINGS = 10 /* You have been warned */
-};
+} debug_t;
-enum {
+typedef enum logmode_t {
LOGMODE_NULL,
LOGMODE_STDERR,
LOGMODE_FILE,
LOGMODE_SYSLOG
-};
+} logmode_t;
-extern int debug_level;
-extern void openlogger(const char *, int);
+extern debug_t debug_level;
+extern void openlogger(const char *, logmode_t);
extern void logger(int, const char *, ...) __attribute__ ((format(printf, 2, 3)));
extern void closelogger(void);
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: meta.c,v 1.1.2.36 2003/07/17 15:06:26 guus Exp $
+ $Id: meta.c,v 1.1.2.37 2003/07/22 20:55:19 guus Exp $
*/
#include "system.h"
#include "system.h"
#include "utils.h"
-int send_meta(connection_t *c, char *buffer, int length)
+bool send_meta(connection_t *c, char *buffer, int length)
{
char *bufp;
int outlen;
continue;
logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
c->hostname, strerror(errno));
- return -1;
+ return false;
}
bufp += result;
length -= result;
}
- return 0;
+ return true;
}
void broadcast_meta(connection_t *from, char *buffer, int length)
}
}
-int receive_meta(connection_t *c)
+bool receive_meta(connection_t *c)
{
int x;
socklen_t l = sizeof(x);
int oldlen, i;
int lenin, reqlen;
- int decrypted = 0;
+ bool decrypted = false;
char inbuf[MAXBUFSIZE];
cp();
if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) {
logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__,
__LINE__, c->socket, strerror(errno), c->name, c->hostname);
- return -1;
+ return false;
}
if(x) {
logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
c->name, c->hostname, strerror(x));
- return -1;
+ return false;
}
/* Strategy:
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
c->name, c->hostname);
} else if(errno == EINTR)
- return 0;
+ return true;
else
logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
c->name, c->hostname, strerror(errno));
- return -1;
+ return false;
}
oldlen = c->buflen;
if(c->status.decryptin && !decrypted) {
EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
memcpy(c->buffer + oldlen, inbuf, lenin);
- decrypted = 1;
+ decrypted = true;
}
/* Are we receiving a TCPpacket? */
if(reqlen) {
c->reqlen = reqlen;
- if(receive_request(c))
- return -1;
+ if(!receive_request(c))
+ return false;
c->buflen -= reqlen;
lenin -= reqlen;
if(c->buflen >= MAXBUFSIZE) {
logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
c->last_ping_time = now;
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: meta.h,v 1.1.2.9 2003/07/12 17:41:45 guus Exp $
+ $Id: meta.h,v 1.1.2.10 2003/07/22 20:55:19 guus Exp $
*/
#ifndef __TINC_META_H__
#include "connection.h"
-extern int send_meta(connection_t *, const char *, int);
-extern int broadcast_meta(connection_t *, const char *, int);
-extern int receive_meta(connection_t *);
+extern bool send_meta(struct connection_t *, const char *, int);
+extern bool broadcast_meta(struct connection_t *, const char *, int);
+extern bool receive_meta(struct connection_t *);
#endif /* __TINC_META_H__ */
along with this program; if not, write to the Free Software\r
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
\r
- $Id: device.c,v 1.1.2.1 2003/07/21 15:51:00 guus Exp $\r
+ $Id: device.c,v 1.1.2.2 2003/07/22 20:55:21 guus Exp $\r
*/\r
\r
#error "Device driver for MinGW environment not written yet!"\r
pid_t reader_pid;\r
int sp[2];\r
\r
-int setup_device(void)\r
+bool setup_device(void)\r
{\r
HKEY key, key2, adapterkey;\r
int i;\r
\r
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, (OSTYPE > 4 ? NETCARD_REG_KEY_2000 : NETCARD_REG_KEY), 0, KEY_READ, &key)) {\r
logger(LOG_ERR, _("Unable to read registry"));\r
- return -1;\r
+ return false;\r
}\r
\r
for (i = 0; ; i++) {\r
\r
if(RegOpenKeyEx (key, adapterid, 0, KEY_READ, &adapterkey)) {\r
logger(LOG_ERR, _("Unable to read registry"));\r
- return -1;\r
+ return false;\r
}\r
\r
len = sizeof(productname);\r
\r
if(!found) {\r
logger(LOG_ERR, _("No CIPE adapters found!"));\r
- return -1;\r
+ return false;\r
}\r
\r
/* Get adapter name */\r
\r
if(socketpair(AF_UNIX, SOCK_DGRAM, PF_UNIX, sp)) {\r
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "socketpair", strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
reader_pid = fork();\r
\r
if(reader_pid == -1) {\r
logger(LOG_DEBUG, _("System call `%s' failed: %s"), "fork", strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
if(!reader_pid) {\r
\r
if(handle == INVALID_HANDLE_VALUE) {\r
logger(LOG_ERR, _("Could not open CIPE tap device for writing!"));\r
- return -1;\r
+ return false;\r
}\r
\r
device_fd = sp[0];\r
read(device_fd, &gelukt, 1);\r
if(gelukt != 1) {\r
logger(LOG_DEBUG, "Tap reader failed!");\r
- return -1;\r
+ return false;\r
}\r
\r
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))\r
\r
logger(LOG_INFO, _("%s is a %s"), device, device_info);\r
\r
- return 0;\r
+ return true;\r
}\r
\r
void close_device(void)\r
kill(reader_pid, SIGKILL);\r
}\r
\r
-int read_packet(vpn_packet_t *packet)\r
+bool read_packet(vpn_packet_t *packet)\r
{\r
int lenin;\r
\r
if((lenin = read(sp[0], packet->data, MTU)) <= 0) {\r
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,\r
device, strerror(errno));\r
- return -1;\r
+ return false;\r
}\r
\r
packet->len = lenin;\r
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,\r
device_info);\r
\r
- return 0;\r
+ return true;\r
}\r
\r
-int write_packet(vpn_packet_t *packet)\r
+bool write_packet(vpn_packet_t *packet)\r
{\r
int lenout;\r
\r
\r
if(!WriteFile (handle, packet->data, packet->len, &lenout, NULL)) {\r
logger(LOG_ERR, "Error while writing to %s %s", device_info, device);\r
- return -1;\r
+ return false;\r
}\r
\r
device_total_out += packet->len;\r
\r
- return 0;\r
+ return true;\r
}\r
\r
void dump_device_stats(void)\r
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: net.c,v 1.35.4.191 2003/07/17 15:06:26 guus Exp $
+ $Id: net.c,v 1.35.4.192 2003/07/22 20:55:19 guus Exp $
*/
#include "system.h"
#include "route.h"
#include "subnet.h"
-int do_purge = 0;
-int sighup = 0;
-int sigalrm = 0;
+bool do_purge = false;
time_t now = 0;
/*
Terminate a connection:
- Close the socket
- - Remove associated edge and tell other connections about it if report = 1
+ - Remove associated edge and tell other connections about it if report = true
- Check if we need to retry making an outgoing connection
- Deactivate the host
*/
-void terminate_connection(connection_t *c, int report)
+void terminate_connection(connection_t *c, bool report)
{
cp();
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
c->name, c->hostname);
- c->status.remove = 1;
- c->status.active = 0;
+ c->status.remove = true;
+ c->status.active = false;
if(c->node)
c->node->connection = NULL;
if(c->status.pinged) {
ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
c->name, c->hostname);
- c->status.timeout = 1;
- terminate_connection(c, 1);
+ c->status.timeout = true;
+ terminate_connection(c, true);
} else {
send_ping(c);
}
}
ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
c->name, c->hostname);
- terminate_connection(c, 0);
+ terminate_connection(c, false);
}
}
}
cp();
if(FD_ISSET(device_fd, f)) {
- if(!read_packet(&packet))
+ if(read_packet(&packet))
route_outgoing(&packet);
}
if(FD_ISSET(c->socket, f)) {
if(c->status.connecting) {
- c->status.connecting = 0;
+ c->status.connecting = false;
getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
if(!result)
}
}
- if(receive_meta(c) < 0) {
+ if(!receive_meta(c)) {
terminate_connection(c, c->status.active);
continue;
}
if(do_purge) {
purge();
- do_purge = 0;
+ do_purge = false;
}
/* Let's check if everybody is still alive */
event->handler(event->data);
event_del(event);
}
- sigalrm = 0;
+ sigalrm = false;
}
if(sighup) {
char *fname;
struct stat s;
- sighup = 0;
+ sighup = false;
/* Reread our own configuration file */
exit_configuration(&config_tree);
init_configuration(&config_tree);
- if(read_server_config()) {
+ if(!read_server_config()) {
logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
exit(1);
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: net.h,v 1.9.4.65 2003/07/18 12:16:24 guus Exp $
+ $Id: net.h,v 1.9.4.66 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_NET_H__
typedef short length_t;
-typedef union {
+typedef union sockaddr_t {
struct sockaddr sa;
struct sockaddr_in in;
struct sockaddr_in6 in6;
queue_element_t *tail;
} packet_queue_t;
+typedef struct listen_socket_t {
+ int tcp;
+ int udp;
+ sockaddr_t sa;
+} listen_socket_t;
+
#include "conf.h"
typedef struct outgoing_t {
struct addrinfo *aip;
} outgoing_t;
-typedef struct listen_socket_t {
- int tcp;
- int udp;
- sockaddr_t sa;
-} listen_socket_t;
-
extern int maxtimeout;
extern int seconds_till_retry;
extern int addressfamily;
extern int listen_sockets;
extern int keyexpires;
extern int keylifetime;
-extern int do_prune;
-extern int do_purge;
+extern bool do_prune;
+extern bool do_purge;
extern char *myport;
extern time_t now;
extern EVP_CIPHER_CTX packet_ctx;
extern void handle_incoming_vpn_data(int);
extern void finish_connecting(struct connection_t *);
extern void do_outgoing_connection(struct connection_t *);
-extern int handle_new_meta_connection(int);
+extern bool handle_new_meta_connection(int);
extern int setup_listen_socket(sockaddr_t *);
extern int setup_vpn_in_socket(sockaddr_t *);
extern void send_packet(struct node_t *, vpn_packet_t *);
extern void receive_tcppacket(struct connection_t *, char *, int);
extern void broadcast_packet(struct node_t *, vpn_packet_t *);
-extern int setup_network_connections(void);
+extern bool setup_network_connections(void);
extern void setup_outgoing_connection(struct outgoing_t *);
extern void try_outgoing_connections(void);
extern void close_network_connections(void);
extern void main_loop(void);
-extern void terminate_connection(struct connection_t *, int);
+extern void terminate_connection(struct connection_t *, bool);
extern void flush_queue(struct node_t *);
-extern int read_rsa_public_key(struct connection_t *);
+extern bool read_rsa_public_key(struct connection_t *);
#endif /* __TINC_NET_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: net_packet.c,v 1.1.2.34 2003/07/17 15:06:26 guus Exp $
+ $Id: net_packet.c,v 1.1.2.35 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
if(!n->status.waitingforkey)
send_req_key(n->nexthop->connection, myself, n);
- n->status.waitingforkey = 1;
+ n->status.waitingforkey = true;
return;
}
n->name, via->name, n->via->hostname);
if((myself->options | via->options) & OPTION_TCPONLY) {
- if(send_tcppacket(via->connection, packet))
- terminate_connection(via->connection, 1);
+ if(!send_tcppacket(via->connection, packet))
+ terminate_connection(via->connection, true);
} else
send_udppacket(via, packet);
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: net_setup.c,v 1.1.2.36 2003/07/18 13:45:06 guus Exp $
+ $Id: net_setup.c,v 1.1.2.37 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
char *myport;
-int read_rsa_public_key(connection_t *c)
+bool read_rsa_public_key(connection_t *c)
{
FILE *fp;
char *fname;
BN_hex2bn(&c->rsa_key->n, key);
BN_hex2bn(&c->rsa_key->e, "FFFF");
free(key);
- return 0;
+ return true;
}
/* Else, check for PublicKeyFile statement and read it */
- if(get_config_string
- (lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
- if(is_safe_path(fname)) {
- fp = fopen(fname, "r");
-
- if(!fp) {
- logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
- fname, strerror(errno));
- free(fname);
- return -1;
- }
+ if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
+ fp = fopen(fname, "r");
+ if(!fp) {
+ logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
+ fname, strerror(errno));
free(fname);
- c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
- fclose(fp);
+ return false;
+ }
- if(c->rsa_key)
- return 0; /* Woohoo. */
+ free(fname);
+ c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
+ fclose(fp);
- /* If it fails, try PEM_read_RSA_PUBKEY. */
- fp = fopen(fname, "r");
+ if(c->rsa_key)
+ return true; /* Woohoo. */
- if(!fp) {
- logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
- fname, strerror(errno));
- free(fname);
- return -1;
- }
+ /* If it fails, try PEM_read_RSA_PUBKEY. */
+ fp = fopen(fname, "r");
+ if(!fp) {
+ logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
+ fname, strerror(errno));
free(fname);
- c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
- fclose(fp);
+ return false;
+ }
- if(c->rsa_key) {
-// RSA_blinding_on(c->rsa_key, NULL);
- return 0;
- }
+ free(fname);
+ c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
+ fclose(fp);
- logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
- fname, strerror(errno));
- return -1;
- } else {
- free(fname);
- return -1;
+ if(c->rsa_key) {
+// RSA_blinding_on(c->rsa_key, NULL);
+ return true;
}
+
+ logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
+ fname, strerror(errno));
+ return false;
}
/* Else, check if a harnessed public key is in the config file */
free(fname);
if(c->rsa_key)
- return 0;
+ return true;
/* Try again with PEM_read_RSA_PUBKEY. */
free(fname);
if(c->rsa_key)
- return 0;
+ return true;
logger(LOG_ERR, _("No public key for %s specified!"), c->name);
- return -1;
+ return false;
}
-int read_rsa_private_key(void)
+bool read_rsa_private_key(void)
{
FILE *fp;
char *fname, *key;
BN_hex2bn(&myself->connection->rsa_key->d, key);
BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
free(key);
- return 0;
+ return true;
}
if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
fname, strerror(errno));
free(fname);
- return -1;
+ return false;
}
free(fname);
if(!myself->connection->rsa_key) {
logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
fname, strerror(errno));
- return -1;
+ return false;
}
- return 0;
+ return true;
}
free(fname);
- return -1;
+ return false;
}
/*
Configure node_t myself and set up the local sockets (listen only)
*/
-int setup_myself(void)
+bool setup_myself(void)
{
config_t *cfg;
subnet_t *subnet;
char *name, *hostname, *mode, *afname, *cipher, *digest;
char *address = NULL;
struct addrinfo hint, *ai, *aip;
- int choice, err;
+ bool choice;
+ int err;
cp();
if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
logger(LOG_ERR, _("Name for tinc daemon required!"));
- return -1;
+ return false;
}
- if(check_id(name)) {
+ if(!check_id(name)) {
logger(LOG_ERR, _("Invalid name for myself!"));
free(name);
- return -1;
+ return false;
}
myself->name = name;
myself->connection->name = xstrdup(name);
- if(read_rsa_private_key())
- return -1;
+ if(!read_rsa_private_key())
+ return false;
- if(read_connection_config(myself->connection)) {
+ if(!read_connection_config(myself->connection)) {
logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
- return -1;
+ return false;
}
- if(read_rsa_public_key(myself->connection))
- return -1;
+ if(!read_rsa_public_key(myself->connection))
+ return false;
if(!get_config_string
(lookup_config(myself->connection->config_tree, "Port"), &myport))
while(cfg) {
if(!get_config_subnet(cfg, &subnet))
- return -1;
+ return false;
subnet_add(myself, subnet);
if(choice)
myself->options |= OPTION_TCPONLY;
- if(get_config_bool
- (lookup_config(myself->connection->config_tree, "IndirectData"),
- &choice))
+ if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
if(choice)
myself->options |= OPTION_INDIRECT;
- if(get_config_bool
- (lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
+ if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
if(choice)
myself->options |= OPTION_TCPONLY;
routing_mode = RMODE_HUB;
else {
logger(LOG_ERR, _("Invalid routing mode!"));
- return -1;
+ return false;
}
free(mode);
} else
routing_mode = RMODE_ROUTER;
- get_config_bool(lookup_config(config_tree, "PriorityInheritance"),
- &priorityinheritance);
+ get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
+
#if !defined(SOL_IP) || !defined(IP_TOS)
if(priorityinheritance)
logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
macexpire = 600;
- if(get_config_int
- (lookup_config(myself->connection->config_tree, "MaxTimeout"),
- &maxtimeout)) {
+ if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) {
if(maxtimeout <= 0) {
logger(LOG_ERR, _("Bogus maximum timeout!"));
- return -1;
+ return false;
}
} else
maxtimeout = 900;
addressfamily = AF_UNSPEC;
else {
logger(LOG_ERR, _("Invalid address family!"));
- return -1;
+ return false;
}
free(afname);
}
if(!myself->cipher) {
logger(LOG_ERR, _("Unrecognized cipher type!"));
- return -1;
+ return false;
}
}
} else
if(!myself->digest) {
logger(LOG_ERR, _("Unrecognized digest type!"));
- return -1;
+ return false;
}
}
} else
if(myself->digest) {
if(myself->maclength > myself->digest->md_size) {
logger(LOG_ERR, _("MAC length exceeds size of digest!"));
- return -1;
+ return false;
} else if(myself->maclength < 0) {
logger(LOG_ERR, _("Bogus MAC length!"));
- return -1;
+ return false;
}
}
} else
&myself->compression)) {
if(myself->compression < 0 || myself->compression > 11) {
logger(LOG_ERR, _("Bogus compression level!"));
- return -1;
+ return false;
}
} else
myself->compression = 0;
myself->nexthop = myself;
myself->via = myself;
- myself->status.active = 1;
- myself->status.reachable = 1;
+ myself->status.active = true;
+ myself->status.reachable = true;
node_add(myself);
graph();
if(err || !ai) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
gai_strerror(err));
- return -1;
+ return false;
}
listen_sockets = 0;
logger(LOG_NOTICE, _("Ready"));
else {
logger(LOG_ERR, _("Unable to create any listening socket!"));
- return -1;
+ return false;
}
- return 0;
+ return true;
}
/*
setup all initial network connections
*/
-int setup_network_connections(void)
+bool setup_network_connections(void)
{
char *envp[5];
int i;
} else
pingtimeout = 60;
- if(setup_device() < 0)
- return -1;
+ if(!setup_device())
+ return false;
- if(setup_myself() < 0)
- return -1;
+ if(!setup_myself())
+ return false;
/* Run tinc-up script to further initialize the tap interface */
asprintf(&envp[0], "NETNAME=%s", netname ? : "");
try_outgoing_connections();
- return 0;
+ return true;
}
/*
if(c->outgoing)
free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
- terminate_connection(c, 0);
+ terminate_connection(c, false);
}
if(myself && myself->connection)
- terminate_connection(myself->connection, 0);
+ terminate_connection(myself->connection, false);
for(i = 0; i < listen_sockets; i++) {
close(listen_socket[i].tcp);
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: net_socket.c,v 1.1.2.29 2003/07/18 13:45:06 guus Exp $
+ $Id: net_socket.c,v 1.1.2.30 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
if(!c->outgoing->cfg) {
ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
c->name);
- c->status.remove = 1;
+ c->status.remove = true;
retry_outgoing(c->outgoing);
return;
}
if(result == -1) {
if(errno == EINPROGRESS) {
- c->status.connecting = 1;
+ c->status.connecting = true;
return;
}
accept a new tcp connect and create a
new connection
*/
-int handle_new_meta_connection(int sock)
+bool handle_new_meta_connection(int sock)
{
connection_t *c;
sockaddr_t sa;
if(fd < 0) {
logger(LOG_ERR, _("Accepting a new connection failed: %s"),
strerror(errno));
- return -1;
+ return false;
}
sockaddrunmap(&sa);
c->allow_request = ID;
send_id(c);
- return 0;
+ return true;
}
void try_outgoing_connections(void)
cfg = lookup_config_next(config_tree, cfg)) {
get_config_string(cfg, &name);
- if(check_id(name)) {
+ if(!check_id(name)) {
logger(LOG_ERR,
_("Invalid name for outgoing connection in %s line %d"),
cfg->file, cfg->line);
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.11 2003/07/18 13:41:36 guus Exp $
+ $Id: device.c,v 1.1.2.12 2003/07/22 20:55:21 guus Exp $
*/
#include "system.h"
#define DEVICE_TYPE_TUNTAP 1
int device_fd = -1;
-int device_type;
char *device;
char *iface;
char *device_info;
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
cp();
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
device_info = _("NetBSD tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
packet->data[12] = 0x08;
device_info);
}
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
+
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: netutl.c,v 1.12.4.47 2003/07/17 15:06:26 guus Exp $
+ $Id: netutl.c,v 1.12.4.48 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
-int hostnames = 0;
+bool hostnames = false;
/*
Turn a string into a struct addrinfo.
a[i] = 0;
}
-int maskcheck(void *va, int masklen, int len)
+bool maskcheck(void *va, int masklen, int len)
{
int i;
char *a = va;
masklen %= 8;
if(masklen && a[i++] & (0xff >> masklen))
- return -1;
+ return false;
for(; i < len; i++)
if(a[i] != 0)
- return -2;
+ return false;
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: netutl.h,v 1.2.4.16 2003/07/17 15:06:26 guus Exp $
+ $Id: netutl.h,v 1.2.4.17 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_NETUTL_H__
#include "net.h"
-extern int hostnames;
+extern bool hostnames;
extern struct addrinfo *str2addrinfo(char *, char *, int);
extern sockaddr_t str2sockaddr(char *, char *);
extern int maskcmp(void *, void *, int, int);
extern void maskcpy(void *, void *, int, int);
extern void mask(void *, int, int);
-extern int maskcheck(void *, int, int);
+extern bool maskcheck(void *, int, int);
#endif /* __TINC_NETUTL_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: node.h,v 1.1.2.25 2003/07/17 15:06:26 guus Exp $
+ $Id: node.h,v 1.1.2.26 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_NODE_H__
sockaddr_t address; /* his real (internet) ip to send UDP packets to */
char *hostname; /* the hostname of its real ip */
- struct node_status_t status;
+ node_status_t status;
const EVP_CIPHER *cipher; /* Cipher type for UDP packets */
char *key; /* Cipher key and iv */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.16 2003/07/18 13:41:36 guus Exp $
+ $Id: device.c,v 1.1.2.17 2003/07/22 20:55:21 guus Exp $
*/
#include "system.h"
#define DEVICE_TYPE_TUNTAP 1
int device_fd = -1;
-int device_type;
char *device;
char *iface;
char *device_info;
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
cp();
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
device_info = _("OpenBSD tun device");
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
u_int32_t type;
if((lenin = readv(device_fd, vector, 2)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
switch (ntohl(type)) {
ifdebug(TRAFFIC) logger(LOG_ERR,
_ ("Unknown address family %d while reading packet from %s %s"),
ntohl(type), device_info, device);
- return -1;
+ return false;
}
packet->len = lenin + 10;
device_info);
}
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
u_int32_t type;
struct iovec vector[2];
ifdebug(TRAFFIC) logger(LOG_ERR,
_("Unknown address family %d while writing packet to %s %s"),
af, device_info, device);
- return -1;
+ return false;
}
vector[0].iov_base = &type;
if(writev(device_fd, vector, 2) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
+
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: process.c,v 1.1.2.56 2003/07/21 14:47:43 guus Exp $
+ $Id: process.c,v 1.1.2.57 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "xalloc.h"
/* If zero, don't detach from the terminal. */
-int do_detach = 1;
+bool do_detach = true;
+bool sighup = false;
+bool sigalrm = false;
extern char *identname;
extern char *pidfilename;
extern char **g_argv;
-extern int use_logfile;
+extern bool use_logfile;
sigset_t emptysigset;
static int saved_debug_level = -1;
-extern int sighup;
-extern int sigalrm;
-extern int do_purge;
-
static void memory_full(int size)
{
logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
/*
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;
netname, pid);
else
fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
- return 1;
+ return false;
}
/* if it's locked, write-protected, or whatever */
if(!write_pid(pidfilename))
- return 1;
+ return false;
- return 0;
+ return true;
}
/*
kill older tincd for this net
*/
-int kill_other(int signal)
+bool kill_other(int signal)
{
int pid;
netname);
else
fprintf(stderr, _("No other tincd is running.\n"));
- return 1;
+ return false;
}
errno = 0; /* No error, sometimes errno is only changed on error */
remove_pid(pidfilename);
}
- return 0;
+ return true;
}
/*
Detach from current terminal, write pidfile, kill parent
*/
-int detach(void)
+bool detach(void)
{
cp();
/* First check if we can open a fresh new pidfile */
- if(write_pidfile())
- return -1;
+ if(!write_pidfile())
+ return false;
/* If we succeeded in doing that, detach */
closelogger();
if(do_detach) {
- if(daemon(0, 0) < 0) {
+ if(daemon(0, 0)) {
fprintf(stderr, _("Couldn't detach from terminal: %s"),
strerror(errno));
- return -1;
+ return false;
}
/* Now UPDATE the pid in the pidfile, because we changed it... */
if(!write_pid(pidfilename))
- return -1;
+ return false;
}
openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
xalloc_fail_func = memory_full;
- return 0;
+ return true;
}
/*
/*
Fork and execute the program pointed to by name.
*/
-int execute_script(const char *name, char **envp)
+bool execute_script(const char *name, char **envp)
{
pid_t pid;
int status;
/* First check if there is a script */
if(stat(scriptname, &s))
- return 0;
+ return true;
pid = fork();
if(pid < 0) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "fork",
strerror(errno));
- return -1;
+ return false;
}
if(pid) {
if(WEXITSTATUS(status)) {
logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"),
pid, name, WEXITSTATUS(status));
- return -1;
+ return false;
} else
- return 0;
+ return true;
} else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid,
name, WTERMSIG(status), strsignal(WTERMSIG(status)));
- return -1;
+ return false;
} else { /* Something strange happened */
logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid,
name);
- return -1;
+ return false;
}
} else if (errno != EINTR) {
logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid",
strerror(errno));
- return -1;
+ return false;
}
/* Why do we get EINTR? */
- return 0;
+ return true;
}
/* Child here */
static RETSIGTYPE sighup_handler(int a)
{
logger(LOG_NOTICE, _("Got HUP signal"));
- sighup = 1;
+ sighup = true;
}
static RETSIGTYPE sigint_handler(int a)
static RETSIGTYPE sigalrm_handler(int a)
{
logger(LOG_NOTICE, _("Got ALRM signal"));
- sigalrm = 1;
+ sigalrm = true;
}
static RETSIGTYPE sigusr1_handler(int a)
static RETSIGTYPE sigwinch_handler(int a)
{
- extern int do_purge;
- do_purge = 1;
+ do_purge = true;
}
static RETSIGTYPE unexpected_signal_handler(int a)
int signal;
void (*handler)(int);
} sighandlers[] = {
- {
- SIGHUP, sighup_handler}, {
- SIGTERM, sigterm_handler}, {
- SIGQUIT, sigquit_handler}, {
- SIGSEGV, fatal_signal_handler}, {
- SIGBUS, fatal_signal_handler}, {
- SIGILL, fatal_signal_handler}, {
- SIGPIPE, ignore_signal_handler}, {
- SIGINT, sigint_handler}, {
- SIGUSR1, sigusr1_handler}, {
- SIGUSR2, sigusr2_handler}, {
- SIGCHLD, ignore_signal_handler}, {
- SIGALRM, sigalrm_handler}, {
- SIGWINCH, sigwinch_handler}, {
- 0, NULL}
+ {SIGHUP, sighup_handler},
+ {SIGTERM, sigterm_handler},
+ {SIGQUIT, sigquit_handler},
+ {SIGSEGV, fatal_signal_handler},
+ {SIGBUS, fatal_signal_handler},
+ {SIGILL, fatal_signal_handler},
+ {SIGPIPE, ignore_signal_handler},
+ {SIGINT, sigint_handler},
+ {SIGUSR1, sigusr1_handler},
+ {SIGUSR2, sigusr2_handler},
+ {SIGCHLD, ignore_signal_handler},
+ {SIGALRM, sigalrm_handler},
+ {SIGWINCH, sigwinch_handler},
+ {0, NULL}
};
void setup_signals(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: process.h,v 1.1.2.16 2003/07/17 15:06:26 guus Exp $
+ $Id: process.h,v 1.1.2.17 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_PROCESS_H__
#define __TINC_PROCESS_H__
-extern int do_detach;
+extern bool do_detach;
+extern bool sighup;
+extern bool sigalrm;
extern void setup_signals(void);
-extern int execute_script(const char *, char **);
-extern int detach(void);
-extern int kill_other(int);
+extern bool execute_script(const char *, char **);
+extern bool detach(void);
+extern bool kill_other(int);
extern void cleanup_and_exit(int) __attribute__ ((noreturn));
#endif /* __TINC_PROCESS_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol.c,v 1.28.4.141 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol.c,v 1.28.4.142 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
/* Jumptable for the request handlers */
-static int (*request_handlers[])(connection_t *) = {
+static bool (*request_handlers[])(connection_t *) = {
id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
status_h, error_h, termreq_h,
ping_h, pong_h,
static avl_tree_t *past_request_tree;
-int check_id(char *id)
+bool check_id(char *id)
{
int i;
for(i = 0; i < strlen(id); i++)
if(!isalnum(id[i]) && id[i] != '_')
- return -1;
+ return false;
- return 0;
+ return true;
}
/* Generic request routines - takes care of logging and error
detection as well */
-int send_request(connection_t *c, const char *format, ...)
+bool send_request(connection_t *c, const char *format, ...)
{
va_list args;
char buffer[MAXBUFSIZE];
if(len < 0 || len > MAXBUFSIZE - 1) {
logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
ifdebug(PROTOCOL) {
buffer[len++] = '\n';
- if(c == broadcast)
- return broadcast_meta(NULL, buffer, len);
- else
+ if(c == broadcast) {
+ broadcast_meta(NULL, buffer, len);
+ return true;
+ } else
return send_meta(c, buffer, len);
}
-int forward_request(connection_t *from)
+void forward_request(connection_t *from)
{
int request;
- cp();
cp();
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;
logger(LOG_ERR, _("Unknown request from %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
} else {
ifdebug(PROTOCOL) {
ifdebug(META)
if((c->allow_request != ALL) && (c->allow_request != request)) {
logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
c->hostname);
- return -1;
+ return false;
}
- if(request_handlers[request] (c))
+ if(!request_handlers[request](c)) {
/* Something went wrong. Probably scriptkiddies. Terminate. */
- {
+
logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
request_name[request], c->name, c->hostname);
- return -1;
+ return false;
}
} else {
logger(LOG_ERR, _("Bogus data received from %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
- return 0;
+ return true;
}
static int past_request_compare(past_request_t *a, past_request_t *b)
avl_delete_tree(past_request_tree);
}
-int seen_request(char *request)
+bool seen_request(char *request)
{
past_request_t p, *new;
if(avl_search(past_request_tree, &p)) {
ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
- return 1;
+ return true;
} else {
new = (past_request_t *) xmalloc(sizeof(*new));
new->request = xstrdup(request);
new->firstseen = now;
avl_insert(past_request_tree, new);
- return 0;
+ return false;
}
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol.h,v 1.5.4.39 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol.h,v 1.5.4.40 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_PROTOCOL_H__
#define __TINC_PROTOCOL_H__
-#include "edge.h"
-#include "net.h"
-#include "node.h"
-#include "subnet.h"
-
/* Protocol version. Different versions are incompatible,
incompatible version have different protocols.
*/
/* Request numbers */
-enum {
+typedef enum request_t {
ALL = -1, /* Guardian for allow_request */
ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK,
STATUS, ERROR, TERMREQ,
KEY_CHANGED, REQ_KEY, ANS_KEY,
PACKET,
LAST /* Guardian for the highest request number */
-};
+} request_t;
typedef struct past_request_t {
char *request;
#define MAX_STRING_SIZE 2048
#define MAX_STRING "%2048s"
+#include "edge.h"
+#include "net.h"
+#include "node.h"
+#include "subnet.h"
+
/* Basic functions */
-extern int send_request(connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3)));
-extern int forward_request(connection_t *);
-extern int receive_request(connection_t *);
-extern int check_id(char *);
+extern bool send_request(struct connection_t *, const char *, ...) __attribute__ ((format(printf, 2, 3)));
+extern void forward_request(struct connection_t *);
+extern bool receive_request(struct connection_t *);
+extern bool check_id(char *);
extern void init_requests(void);
extern void exit_requests(void);
-extern int seen_request(char *);
+extern bool seen_request(char *);
extern void age_past_requests(void);
/* Requests */
-extern int send_id(connection_t *);
-extern int send_metakey(connection_t *);
-extern int send_challenge(connection_t *);
-extern int send_chal_reply(connection_t *);
-extern int send_ack(connection_t *);
-extern int send_status(connection_t *, int, char *);
-extern int send_error(connection_t *, int, char *);
-extern int send_termreq(connection_t *);
-extern int send_ping(connection_t *);
-extern int send_pong(connection_t *);
-extern int send_add_subnet(connection_t *, subnet_t *);
-extern int send_del_subnet(connection_t *, subnet_t *);
-extern int send_add_edge(connection_t *, edge_t *);
-extern int send_del_edge(connection_t *, edge_t *);
-extern int send_key_changed(connection_t *, node_t *);
-extern int send_req_key(connection_t *, node_t *, node_t *);
-extern int send_ans_key(connection_t *, node_t *, node_t *);
-extern int send_tcppacket(connection_t *, vpn_packet_t *);
+extern bool send_id(struct connection_t *);
+extern bool send_metakey(struct connection_t *);
+extern bool send_challenge(struct connection_t *);
+extern bool send_chal_reply(struct connection_t *);
+extern bool send_ack(struct connection_t *);
+extern bool send_status(struct connection_t *, int, char *);
+extern bool send_error(struct connection_t *, int, char *);
+extern bool send_termreq(struct connection_t *);
+extern bool send_ping(struct connection_t *);
+extern bool send_pong(struct connection_t *);
+extern bool send_add_subnet(struct connection_t *, struct subnet_t *);
+extern bool send_del_subnet(struct connection_t *, struct subnet_t *);
+extern bool send_add_edge(struct connection_t *, struct edge_t *);
+extern bool send_del_edge(struct connection_t *, struct edge_t *);
+extern bool send_key_changed(struct connection_t *, struct node_t *);
+extern bool send_req_key(struct connection_t *, struct node_t *, struct node_t *);
+extern bool send_ans_key(struct connection_t *, struct node_t *, struct node_t *);
+extern bool send_tcppacket(struct connection_t *, struct vpn_packet_t *);
/* Request handlers */
-extern int id_h(connection_t *);
-extern int metakey_h(connection_t *);
-extern int challenge_h(connection_t *);
-extern int chal_reply_h(connection_t *);
-extern int ack_h(connection_t *);
-extern int status_h(connection_t *);
-extern int error_h(connection_t *);
-extern int termreq_h(connection_t *);
-extern int ping_h(connection_t *);
-extern int pong_h(connection_t *);
-extern int add_subnet_h(connection_t *);
-extern int del_subnet_h(connection_t *);
-extern int add_edge_h(connection_t *);
-extern int del_edge_h(connection_t *);
-extern int key_changed_h(connection_t *);
-extern int req_key_h(connection_t *);
-extern int ans_key_h(connection_t *);
-extern int tcppacket_h(connection_t *);
+extern bool id_h(struct connection_t *);
+extern bool metakey_h(struct connection_t *);
+extern bool challenge_h(struct connection_t *);
+extern bool chal_reply_h(struct connection_t *);
+extern bool ack_h(struct connection_t *);
+extern bool status_h(struct connection_t *);
+extern bool error_h(struct connection_t *);
+extern bool termreq_h(struct connection_t *);
+extern bool ping_h(struct connection_t *);
+extern bool pong_h(struct connection_t *);
+extern bool add_subnet_h(struct connection_t *);
+extern bool del_subnet_h(struct connection_t *);
+extern bool add_edge_h(struct connection_t *);
+extern bool del_edge_h(struct connection_t *);
+extern bool key_changed_h(struct connection_t *);
+extern bool req_key_h(struct connection_t *);
+extern bool ans_key_h(struct connection_t *);
+extern bool tcppacket_h(struct connection_t *);
#endif /* __TINC_PROTOCOL_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol_auth.c,v 1.1.4.24 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol_auth.c,v 1.1.4.25 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
-int send_id(connection_t *c)
+bool send_id(connection_t *c)
{
cp();
myself->connection->protocol_version);
}
-int id_h(connection_t *c)
+bool id_h(connection_t *c)
{
char name[MAX_STRING_SIZE];
- int bla;
+ bool choice;
cp();
if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if identity is a valid name */
- if(check_id(name)) {
+ if(!check_id(name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
c->hostname, "invalid name");
- return -1;
+ return false;
}
/* If we set c->name in advance, make sure we are connected to the right host */
if(strcmp(c->name, name)) {
logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
c->name);
- return -1;
+ return false;
}
} else
c->name = xstrdup(name);
if(c->protocol_version != myself->connection->protocol_version) {
logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
c->name, c->hostname, c->protocol_version);
- return -1;
+ return false;
}
if(bypass_security) {
if(!c->config_tree) {
init_configuration(&c->config_tree);
- bla = read_connection_config(c);
-
- if(bla) {
+ if(!read_connection_config(c)) {
logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
c->name);
- return -1;
+ return false;
}
}
- if(read_rsa_public_key(c)) {
- return -1;
+ if(!read_rsa_public_key(c)) {
+ return false;
}
/* Check some options */
- if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &bla) && bla) || myself->options & OPTION_INDIRECT)
+ if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
c->options |= OPTION_INDIRECT;
- if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &bla) && bla) || myself->options & OPTION_TCPONLY)
+ if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
c->allow_request = METAKEY;
return send_metakey(c);
}
-int send_metakey(connection_t *c)
+bool send_metakey(connection_t *c)
{
char buffer[MAX_STRING_SIZE];
- int len, x;
+ int len;
+ bool x;
cp();
if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len) {
logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
/* Convert the encrypted random data to a hexadecimal formatted string */
c->outkey + len - c->outcipher->key_len -
c->outcipher->iv_len);
- c->status.encryptout = 1;
+ c->status.encryptout = true;
}
return x;
}
-int metakey_h(connection_t *c)
+bool metakey_h(connection_t *c)
{
char buffer[MAX_STRING_SIZE];
int cipher, digest, maclength, compression;
if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
c->hostname);
- return -1;
+ return false;
}
len = RSA_size(myself->connection->rsa_key);
if(strlen(buffer) != len * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
- return -1;
+ return false;
}
/* Allocate buffers for the meta key */
if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) { /* See challenge() */
logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
ifdebug(SCARY_THINGS) {
if(!c->incipher) {
logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
- return -1;
+ return false;
}
EVP_DecryptInit(c->inctx, c->incipher,
c->inkey + len - c->incipher->key_len -
c->incipher->iv_len);
- c->status.decryptin = 1;
+ c->status.decryptin = true;
} else {
c->incipher = NULL;
}
if(!c->indigest) {
logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
- return -1;
+ return false;
}
if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0) {
logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
- return -1;
+ return false;
}
} else {
c->indigest = NULL;
return send_challenge(c);
}
-int send_challenge(connection_t *c)
+bool send_challenge(connection_t *c)
{
char buffer[MAX_STRING_SIZE];
- int len, x;
+ int len;
cp();
/* Send the challenge */
- x = send_request(c, "%d %s", CHALLENGE, buffer);
-
- return x;
+ return send_request(c, "%d %s", CHALLENGE, buffer);
}
-int challenge_h(connection_t *c)
+bool challenge_h(connection_t *c)
{
char buffer[MAX_STRING_SIZE];
int len;
if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
c->hostname);
- return -1;
+ return false;
}
len = RSA_size(myself->connection->rsa_key);
if(strlen(buffer) != len * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, "wrong challenge length");
- return -1;
+ return false;
}
/* Allocate buffers for the challenge */
return send_chal_reply(c);
}
-int send_chal_reply(connection_t *c)
+bool send_chal_reply(connection_t *c)
{
char hash[EVP_MAX_MD_SIZE * 2 + 1];
EVP_MD_CTX ctx;
return send_request(c, "%d %s", CHAL_REPLY, hash);
}
-int chal_reply_h(connection_t *c)
+bool chal_reply_h(connection_t *c)
{
char hishash[MAX_STRING_SIZE];
char myhash[EVP_MAX_MD_SIZE];
if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if the length of the hash is all right */
if(strlen(hishash) != c->outdigest->md_size * 2) {
logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, _("wrong challenge reply length"));
- return -1;
+ return false;
}
/* Convert the hash to binary format */
logger(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
}
- return -1;
+ return false;
}
/* Identity has now been positively verified.
return send_ack(c);
}
-int send_ack(connection_t *c)
+bool send_ack(connection_t *c)
{
/* ACK message contains rest of the information the other end needs
to create node_t and edge_t structures. */
- int x;
struct timeval now;
cp();
/* Estimate weight */
gettimeofday(&now, NULL);
- c->estimated_weight =
- (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec -
- c->start.tv_usec) / 1000;
- x = send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight,
- c->options);
+ c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
- return x;
+ return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options);
}
static void send_everything(connection_t *c)
}
}
-int ack_h(connection_t *c)
+bool ack_h(connection_t *c)
{
char hisport[MAX_STRING_SIZE];
char *hisaddress, *dummy;
if(sscanf(c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if we already have a node_t for him */
/* Oh dear, we already have a connection to this node. */
ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
n->name, n->hostname);
- terminate_connection(n->connection, 0);
+ terminate_connection(n->connection, false);
/* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
graph();
}
/* Activate this connection */
c->allow_request = ALL;
- c->status.active = 1;
+ c->status.active = true;
ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
c->hostname);
graph();
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol_edge.c,v 1.1.4.18 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol_edge.c,v 1.1.4.19 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
-int send_add_edge(connection_t *c, edge_t *e)
+bool send_add_edge(connection_t *c, edge_t *e)
{
- int x;
+ bool x;
char *address, *port;
cp();
return x;
}
-int add_edge_h(connection_t *c)
+bool add_edge_h(connection_t *c)
{
edge_t *e;
node_t *from, *to;
from_name, to_name, to_address, to_port, &options, &weight) != 6) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if names are valid */
- if(check_id(from_name)) {
+ if(!check_id(from_name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
- if(check_id(to_name)) {
+ if(!check_id(to_name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
if(seen_request(c->buffer))
- return 0;
+ return true;
/* Lookup nodes */
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
"ADD_EDGE", c->name, c->hostname);
send_add_edge(c, e);
- return 0;
+ return true;
} else {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
"ADD_EDGE", c->name, c->hostname);
graph();
}
} else
- return 0;
+ return true;
} else if(from == myself) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
"ADD_EDGE", c->name, c->hostname);
e->to = to;
send_del_edge(c, e);
free_edge(e);
- return 0;
+ return true;
}
e = new_edge();
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();
e->from->name, e->to->name);
}
-int del_edge_h(connection_t *c)
+bool del_edge_h(connection_t *c)
{
edge_t *e;
char from_name[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if names are valid */
- if(check_id(from_name)) {
+ if(!check_id(from_name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
- if(check_id(to_name)) {
+ if(!check_id(to_name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
if(seen_request(c->buffer))
- return 0;
+ return true;
/* Lookup nodes */
if(!from) {
ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname);
- return 0;
+ return true;
}
to = lookup_node(to_name);
if(!to) {
ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname);
- return 0;
+ return true;
}
/* Check if edge exists */
if(!e) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
"DEL_EDGE", c->name, c->hostname);
- return 0;
+ return true;
}
if(e->from == myself) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
"DEL_EDGE", c->name, c->hostname);
send_add_edge(c, e); /* Send back a correction */
- return 0;
+ return true;
}
/* Tell the rest about the deleted edge */
graph();
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol_key.c,v 1.1.4.19 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol_key.c,v 1.1.4.20 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
-int mykeyused = 0;
+bool mykeyused = false;
-int send_key_changed(connection_t *c, node_t *n)
+bool send_key_changed(connection_t *c, node_t *n)
{
cp();
*/
if(n == myself && !mykeyused)
- return 0;
+ return true;
return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
}
-int key_changed_h(connection_t *c)
+bool key_changed_h(connection_t *c)
{
char name[MAX_STRING_SIZE];
node_t *n;
if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
c->name, c->hostname);
- return -1;
+ return false;
}
if(seen_request(c->buffer))
- return 0;
+ return true;
n = lookup_node(name);
if(!n) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
"KEY_CHANGED", c->name, c->hostname, name);
- return -1;
+ return false;
}
- n->status.validkey = 0;
- n->status.waitingforkey = 0;
+ n->status.validkey = false;
+ n->status.waitingforkey = false;
/* Tell the others */
forward_request(c);
- return 0;
+ return true;
}
-int send_req_key(connection_t *c, node_t *from, node_t *to)
+bool send_req_key(connection_t *c, node_t *from, node_t *to)
{
cp();
return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
}
-int req_key_h(connection_t *c)
+bool req_key_h(connection_t *c)
{
char from_name[MAX_STRING_SIZE];
char to_name[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
c->hostname);
- return -1;
+ return false;
}
from = lookup_node(from_name);
if(!from) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
"REQ_KEY", c->name, c->hostname, from_name);
- return -1;
+ return false;
}
to = lookup_node(to_name);
if(!to) {
logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
"REQ_KEY", c->name, c->hostname, to_name);
- return -1;
+ return false;
}
/* Check if this key request is for us */
if(to == myself) { /* Yes, send our own key back */
- mykeyused = 1;
+ mykeyused = true;
from->received_seqno = 0;
memset(from->late, 0, sizeof(from->late));
send_ans_key(c, myself, from);
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];
from->compression);
}
-int ans_key_h(connection_t *c)
+bool ans_key_h(connection_t *c)
{
char from_name[MAX_STRING_SIZE];
char to_name[MAX_STRING_SIZE];
&compression) != 7) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
c->hostname);
- return -1;
+ return false;
}
from = lookup_node(from_name);
if(!from) {
logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
"ANS_KEY", c->name, c->hostname, from_name);
- return -1;
+ return false;
}
to = lookup_node(to_name);
if(!to) {
logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
"ANS_KEY", c->name, c->hostname, to_name);
- return -1;
+ return false;
}
/* Forward it if necessary */
hex2bin(from->key, from->key, from->keylength);
from->key[from->keylength] = '\0';
- from->status.validkey = 1;
- from->status.waitingforkey = 0;
+ from->status.validkey = true;
+ from->status.waitingforkey = false;
from->sent_seqno = 0;
/* Check and lookup cipher and digest algorithms */
if(!from->cipher) {
logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
from->hostname);
- return -1;
+ return false;
}
if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
from->hostname);
- return -1;
+ return false;
}
} else {
from->cipher = NULL;
if(!from->digest) {
logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
from->hostname);
- return -1;
+ return false;
}
if(from->maclength > from->digest->md_size || from->maclength < 0) {
logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
from->name, from->hostname);
- return -1;
+ return false;
}
} else {
from->digest = NULL;
if(compression < 0 || compression > 11) {
logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
- return -1;
+ return false;
}
from->compression = compression;
flush_queue(from);
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol_misc.c,v 1.1.4.11 2003/07/17 15:06:26 guus Exp $
+ $Id: protocol_misc.c,v 1.1.4.12 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
/* 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();
return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
}
-int status_h(connection_t *c)
+bool status_h(connection_t *c)
{
int statusno;
char statusstring[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
c->name, c->hostname);
- return -1;
+ return false;
}
ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"),
c->name, c->hostname, statusno, statusstring);
- return 0;
+ return true;
}
-int send_error(connection_t *c, int err, char *errstring)
+bool send_error(connection_t *c, int err, char *errstring)
{
cp();
return send_request(c, "%d %d %s", ERROR, err, errstring);
}
-int error_h(connection_t *c)
+bool error_h(connection_t *c)
{
int err;
char errorstring[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
c->name, c->hostname);
- return -1;
+ return false;
}
ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"),
terminate_connection(c, c->status.active);
- return 0;
+ return true;
}
-int send_termreq(connection_t *c)
+bool send_termreq(connection_t *c)
{
cp();
return send_request(c, "%d", TERMREQ);
}
-int termreq_h(connection_t *c)
+bool termreq_h(connection_t *c)
{
cp();
terminate_connection(c, c->status.active);
- return 0;
+ return true;
}
-int send_ping(connection_t *c)
+bool send_ping(connection_t *c)
{
cp();
- c->status.pinged = 1;
+ c->status.pinged = true;
c->last_ping_time = now;
return send_request(c, "%d", PING);
}
-int ping_h(connection_t *c)
+bool ping_h(connection_t *c)
{
cp();
return send_pong(c);
}
-int send_pong(connection_t *c)
+bool send_pong(connection_t *c)
{
cp();
return send_request(c, "%d", PONG);
}
-int pong_h(connection_t *c)
+bool pong_h(connection_t *c)
{
cp();
- c->status.pinged = 0;
+ c->status.pinged = false;
/* Succesful connection, reset timeout if this is an outgoing connection. */
if(c->outgoing)
c->outgoing->timeout = 0;
- return 0;
+ return true;
}
/* Sending and receiving packets via TCP */
-int send_tcppacket(connection_t *c, vpn_packet_t *packet)
+bool send_tcppacket(connection_t *c, vpn_packet_t *packet)
{
- int x;
-
cp();
/* Evil hack. */
- x = send_request(c, "%d %hd", PACKET, packet->len);
-
- if(x)
- return x;
+ if(!send_request(c, "%d %hd", PACKET, packet->len))
+ return false;
return send_meta(c, packet->data, packet->len);
}
-int tcppacket_h(connection_t *c)
+bool tcppacket_h(connection_t *c)
{
short int len;
if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
c->tcplen = len;
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol_subnet.c,v 1.1.4.13 2003/07/21 14:47:43 guus Exp $
+ $Id: protocol_subnet.c,v 1.1.4.14 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
-int send_add_subnet(connection_t *c, subnet_t *subnet)
+bool send_add_subnet(connection_t *c, subnet_t *subnet)
{
- int x;
+ bool x;
char *netstr;
cp();
return x;
}
-int add_subnet_h(connection_t *c)
+bool add_subnet_h(connection_t *c)
{
char subnetstr[MAX_STRING_SIZE];
char name[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if owner name is a valid */
- if(check_id(name)) {
+ if(!check_id(name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
/* Check if subnet string is valid */
if(!s) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
c->hostname, _("invalid subnet string"));
- return -1;
+ return false;
}
if(seen_request(c->buffer))
- return 0;
+ return true;
/* Check if the owner of the new subnet is in the connection list */
if(lookup_subnet(owner, s)) {
free_subnet(s);
- return 0;
+ return true;
}
/* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
"ADD_SUBNET", c->name, c->hostname);
s->owner = myself;
send_del_subnet(c, s);
- return 0;
+ return true;
}
/* If everything is correct, add the subnet to the list of the owner */
forward_request(c);
- return 0;
+ return true;
}
-int send_del_subnet(connection_t *c, subnet_t *s)
+bool send_del_subnet(connection_t *c, subnet_t *s)
{
- int x;
+ bool x;
char *netstr;
cp();
return x;
}
-int del_subnet_h(connection_t *c)
+bool del_subnet_h(connection_t *c)
{
char subnetstr[MAX_STRING_SIZE];
char name[MAX_STRING_SIZE];
if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
c->hostname);
- return -1;
+ return false;
}
/* Check if owner name is a valid */
- if(check_id(name)) {
+ if(!check_id(name)) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
c->hostname, _("invalid name"));
- return -1;
+ return false;
}
/* Check if the owner of the new subnet is in the connection list */
if(!owner) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
"DEL_SUBNET", c->name, c->hostname, name);
- return 0;
+ return true;
}
/* Check if subnet string is valid */
if(!s) {
logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
c->hostname, _("invalid subnet string"));
- return -1;
+ return false;
}
if(seen_request(c->buffer))
- return 0;
+ return true;
/* If everything is correct, delete the subnet from the list of the owner */
if(!find) {
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
"DEL_SUBNET", c->name, c->hostname, name);
- return 0;
+ return true;
}
/* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
"DEL_SUBNET", c->name, c->hostname);
send_add_subnet(c, find);
- return 0;
+ return true;
}
/* Tell the rest */
subnet_del(owner, find);
- return 0;
+ return true;
}
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.7 2003/07/12 17:41:48 guus Exp $
+ $Id: device.c,v 1.1.2.8 2003/07/22 20:55:21 guus Exp $
*/
#include "config.h"
#include "system.h"
int device_fd = -1;
-int device_type;
char *device;
char *interface;
char ifrname[IFNAMSIZ];
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
struct ifreq ifr;
struct sockaddr_ll sa;
if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device_info,
strerror(errno));
- return -1;
+ return false;
}
memset(&ifr, 0, sizeof(ifr));
close(device_fd);
logger(LOG_ERR, _("Can't find interface %s: %s"), interface,
strerror(errno));
- return -1;
+ return false;
}
memset(&sa, '0', 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));
- return -1;
+ return false;
}
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
packet->len = lenin;
device_info);
}
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data, packet->len) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
- return 0;
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: route.c,v 1.1.2.61 2003/07/18 12:21:03 guus Exp $
+ $Id: route.c,v 1.1.2.62 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
#include "subnet.h"
#include "utils.h"
-int routing_mode = RMODE_ROUTER;
-int priorityinheritance = 0;
+rmode_t routing_mode = RMODE_ROUTER;
+bool priorityinheritance = false;
int macexpire = 600;
-int overwrite_mac = 0;
+bool overwrite_mac = false;
mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
/* RFC 1071 */
return ~checksum;
}
-static int ratelimit(void) {
+static bool ratelimit(void) {
static time_t lasttime = 0;
if(lasttime == now)
- return 1;
+ return true;
lasttime = now;
- return 0;
+ return false;
}
static void learn_mac(mac_t *address)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: route.h,v 1.1.2.12 2003/07/15 16:26:18 guus Exp $
+ $Id: route.h,v 1.1.2.13 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_ROUTE_H__
#define __TINC_ROUTE_H__
-enum {
+#include "net.h"
+#include "node.h"
+
+typedef enum rmode_t {
RMODE_HUB = 0,
RMODE_SWITCH,
RMODE_ROUTER,
-};
+} rmode_t;
-extern int routing_mode;
-extern int overwrite_mac;
-extern int priorityinheritance;
+extern rmode_t routing_mode;
+extern bool overwrite_mac;
+extern bool priorityinheritance;
extern int macexpire;
extern mac_t mymac;
extern void age_mac(void);
-extern void route_incoming(node_t *, vpn_packet_t *);
-extern void route_outgoing(vpn_packet_t *);
+extern void route_incoming(struct node_t *, struct vpn_packet_t *);
+extern void route_outgoing(struct vpn_packet_t *);
#endif /* __TINC_ROUTE_H__ */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: device.c,v 1.1.2.15 2003/07/18 13:41:37 guus Exp $
+ $Id: device.c,v 1.1.2.16 2003/07/22 20:55:21 guus Exp $
*/
#define DEFAULT_DEVICE "/dev/tun"
int device_fd = -1;
-int device_type;
char *device = NULL;
char *iface = NULL;
char ifrname[IFNAMSIZ];
int device_total_in = 0;
int device_total_out = 0;
-int setup_device(void)
+bool setup_device(void)
{
int ip_fd = -1, if_fd = -1;
int ppa;
if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
- return -1;
+ return false;
}
ppa = 0;
if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
logger(LOG_ERR, _("Could not open /dev/ip: %s"), strerror(errno));
- return -1;
+ return false;
}
/* Assign a new PPA and get its unit number. */
if((ppa = ioctl(device_fd, TUNNEWPPA, ppa)) < 0) {
logger(LOG_ERR, _("Can't assign new interface: %s"), strerror(errno));
- return -1;
+ return false;
}
if((if_fd = open(device, O_RDWR, 0)) < 0) {
logger(LOG_ERR, _("Could not open %s twice: %s"), device,
strerror(errno));
- return -1;
+ return false;
}
if(ioctl(if_fd, I_PUSH, "ip") < 0) {
logger(LOG_ERR, _("Can't push IP module: %s"), strerror(errno));
- return -1;
+ return false;
}
/* Assign ppa according to the unit number returned by tun device */
if(ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0) {
logger(LOG_ERR, _("Can't set PPA %d: %s"), ppa, strerror(errno));
- return -1;
+ return false;
}
if(ioctl(ip_fd, I_LINK, if_fd) < 0) {
logger(LOG_ERR, _("Can't link TUN device to IP: %s"), strerror(errno));
- return -1;
+ return false;
}
if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
logger(LOG_INFO, _("%s is a %s"), device, device_info);
- return 0;
+ return true;
}
void close_device(void)
close(device_fd);
}
-int read_packet(vpn_packet_t *packet)
+bool read_packet(vpn_packet_t *packet)
{
int lenin;
if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
device, strerror(errno));
- return -1;
+ return false;
}
packet->data[12] = 0x08;
ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
device_info);
- return 0;
+ return true;
}
-int write_packet(vpn_packet_t *packet)
+bool write_packet(vpn_packet_t *packet)
{
cp();
if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, packet->len,
strerror(errno));
- return -1;
+ return false;
}
device_total_out += packet->len;
- return 0;
+ return true;
}
void dump_device_stats(void)
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: subnet.h,v 1.1.2.21 2003/07/06 23:16:29 guus Exp $
+ $Id: subnet.h,v 1.1.2.22 2003/07/22 20:55:20 guus Exp $
*/
#ifndef __TINC_SUBNET_H__
#include "net.h"
-enum {
+typedef enum subnet_type_t {
SUBNET_MAC = 0,
SUBNET_IPV4,
SUBNET_IPV6,
SUBNET_TYPES /* Guardian */
-};
+} subnet_type_t;
typedef struct subnet_mac_t {
mac_t address;
struct node_t *owner; /* the owner of this subnet */
struct node_t *uplink; /* the uplink which we should send packets to for this subnet */
- int type; /* subnet type (IPv4? IPv6? MAC? something even weirder?) */
+ subnet_type_t type; /* subnet type (IPv4? IPv6? MAC? something even weirder?) */
/* And now for the actual subnet: */
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: tincd.c,v 1.10.4.75 2003/07/22 12:58:34 guus Exp $
+ $Id: tincd.c,v 1.10.4.76 2003/07/22 20:55:20 guus Exp $
*/
#include "system.h"
char *program_name = NULL;
/* If nonzero, display usage information and exit. */
-int show_help = 0;
+bool show_help = false;
/* If nonzero, print the version on standard output and exit. */
-int show_version = 0;
+bool show_version = false;
/* If nonzero, it will attempt to kill a running tincd and exit. */
int kill_tincd = 0;
int generate_keys = 0;
/* If nonzero, use null ciphers and skip all key exchanges. */
-int bypass_security = 0;
+bool bypass_security = false;
/* If nonzero, disable swapping for this process. */
-int do_mlock = 0;
+bool do_mlock = false;
/* If nonzero, write log entries to a separate file. */
-int use_logfile = 0;
+bool use_logfile = false;
char *identname = NULL; /* program name for syslog */
char *pidfilename = NULL; /* pid file location */
char *logfilename = NULL; /* log file location */
char **g_argv; /* a copy of the cmdline arguments */
-char **environment; /* A pointer to the environment on
- startup */
+char **environment; /* A pointer to the environment on startup */
static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'},
{"kill", optional_argument, NULL, 'k'},
{"net", required_argument, NULL, 'n'},
- {"help", no_argument, &show_help, 1},
- {"version", no_argument, &show_version, 1},
- {"no-detach", no_argument, &do_detach, 0},
+ {"help", no_argument, NULL, 1},
+ {"version", no_argument, NULL, 2},
+ {"no-detach", no_argument, NULL, 'D'},
{"generate-keys", optional_argument, NULL, 'K'},
{"debug", optional_argument, NULL, 'd'},
- {"bypass-security", no_argument, &bypass_security, 1},
- {"mlock", no_argument, &do_mlock, 1},
- {"logfile", optional_argument, NULL, 'F'},
- {"pidfile", required_argument, NULL, 'P'},
+ {"bypass-security", no_argument, NULL, 3},
+ {"mlock", no_argument, NULL, 'L'},
+ {"logfile", optional_argument, NULL, 4},
+ {"pidfile", required_argument, NULL, 5},
{NULL, 0, NULL, 0}
};
-static void usage(int status)
+static void usage(bool status)
{
- if(status != 0)
+ if(status)
fprintf(stderr, _("Try `%s --help\' for more information.\n"),
program_name);
else {
break;
case 'D': /* no detach */
- do_detach = 0;
+ do_detach = false;
break;
case 'L': /* no detach */
- do_mlock = 1;
+ do_mlock = true;
break;
case 'd': /* inc debug level */
if(!kill_tincd) {
fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
optarg);
- usage(1);
+ usage(true);
}
}
} else
if(generate_keys < 512) {
fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
optarg);
- usage(1);
+ usage(true);
}
generate_keys &= ~7; /* Round it to bytes */
generate_keys = 1024;
break;
- case 'F': /* write log entries to a file */
- use_logfile = 1;
+ case 1: /* show help */
+ show_help = true;
+ break;
+
+ case 2: /* show version */
+ show_version = true;
+ break;
+
+ case 3: /* bypass security */
+ bypass_security = true;
+ break;
+
+ case 4: /* write log entries to a file */
+ use_logfile = true;
if(optarg)
logfilename = xstrdup(optarg);
break;
- case 'P': /* write PID to a file */
+ case 5: /* write PID to a file */
pidfilename = xstrdup(optarg);
break;
case '?':
- usage(1);
+ usage(true);
default:
break;
Generate a public/private RSA keypair, and ask for a file to store
them in.
*/
-static int keygen(int bits)
+static bool keygen(int bits)
{
RSA *rsa_key;
FILE *f;
if(!rsa_key) {
fprintf(stderr, _("Error during key generation!\n"));
- return -1;
+ return false;
} else
fprintf(stderr, _("Done.\n"));
- get_config_string(lookup_config(config_tree, "Name"), &name);
-
- if(name)
- asprintf(&filename, "%s/hosts/%s", confbase, name);
- else
- asprintf(&filename, "%s/rsa_key.pub", confbase);
-
- f = ask_and_safe_open(filename, _("public RSA key"), "a");
+ asprintf(&filename, "%s/rsa_key.priv", confbase);
+ f = ask_and_safe_open(filename, _("private RSA key"), true, "a");
if(!f)
- return -1;
+ return false;
if(ftell(f))
fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
- PEM_write_RSAPublicKey(f, rsa_key);
+ PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
fclose(f);
free(filename);
- asprintf(&filename, "%s/rsa_key.priv", confbase);
- f = ask_and_safe_open(filename, _("private RSA key"), "a");
+ get_config_string(lookup_config(config_tree, "Name"), &name);
+
+ if(name)
+ asprintf(&filename, "%s/hosts/%s", confbase, name);
+ else
+ asprintf(&filename, "%s/rsa_key.pub", confbase);
+
+ f = ask_and_safe_open(filename, _("public RSA key"), false, "a");
if(!f)
- return -1;
+ return false;
if(ftell(f))
fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
- PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
+ PEM_write_RSAPublicKey(f, rsa_key);
fclose(f);
free(filename);
- return 0;
+ return true;
}
/*
}
if(show_help)
- usage(0);
+ usage(false);
if(kill_tincd)
- exit(kill_other(kill_tincd));
+ exit(!kill_other(kill_tincd));
openlogger("tinc", LOGMODE_STDERR);
if(generate_keys) {
read_server_config();
- exit(keygen(generate_keys));
+ exit(!keygen(generate_keys));
}
- if(read_server_config())
+ if(!read_server_config())
exit(1);
if(lzo_init() != LZO_E_OK) {
exit(1);
}
- if(detach())
- exit(0);
+ if(!detach())
+ exit(1);
for(;;) {
- if(!setup_network_connections()) {
+ if(setup_network_connections()) {
main_loop();
cleanup_and_exit(1);
}