Moving files, first attempt at gcrypt compatibility, more interface
abstraction
This commit is contained in:
parent
b0a676988a
commit
04d33be4bd
69 changed files with 10498 additions and 536 deletions
|
|
@ -1,15 +1,17 @@
|
|||
## Process this file with automake to produce Makefile.in
|
||||
# $Id: Makefile.am,v 1.6 2002/04/13 11:23:19 zarq Exp $
|
||||
# $Id: Makefile.am,v 1.7 2002/04/28 12:46:25 zarq Exp $
|
||||
|
||||
noinst_LIBRARIES = libtinc.a
|
||||
|
||||
INCLUDES = @INCLUDES@ -I. -I$(top_builddir) -I$(top_srcdir)/intl
|
||||
|
||||
libtinc_a_SOURCES = xmalloc.c pidfile.c utils.c getopt.c getopt1.c list.c avl_tree.c dropin.c
|
||||
libtinc_a_SOURCES = xmalloc.c pidfile.c utils.c getopt.c getopt1.c \
|
||||
list.c avl_tree.c hooks.c dropin.c edge.c conf.c netutl.c logging.c connection.c subnet.c node.c
|
||||
|
||||
libtinc_a_LIBADD = @LIBOBJS@ @ALLOCA@
|
||||
libtinc_a_DEPENDENCIES = $(libvpn_a_LIBADD)
|
||||
|
||||
noinst_HEADERS = xalloc.h pidfile.h utils.h getopt.h list.h avl_tree.h dropin.h
|
||||
noinst_HEADERS = xalloc.h pidfile.h utils.h getopt.h list.h avl_tree.h \
|
||||
hooks.h dropin.h edge.h net.h conf.h netutl.h logging.h connection.h subnet.h node.h
|
||||
|
||||
EXTRA_DIST = README
|
||||
|
|
|
|||
269
lib/conf.c
Normal file
269
lib/conf.c
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
conf.c -- configuration storage & retrieval code
|
||||
Copyright (C) 1998 Robert van der Meulen
|
||||
1998-2002 Ivo Timmermans <ivo@o2w.nl>
|
||||
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
||||
2000 Cris van Pelt <tribbel@arise.dhs.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <xalloc.h>
|
||||
#include <utils.h> /* for cp */
|
||||
#include <avl_tree.h>
|
||||
|
||||
#include "conf.h"
|
||||
#include "netutl.h" /* for str2address */
|
||||
#include "logging.h"
|
||||
|
||||
#include "system.h"
|
||||
|
||||
avl_tree_t *config_tree;
|
||||
|
||||
int pingtimeout = 0; /* seconds before timeout */
|
||||
char *confbase = NULL; /* directory in which all config files are */
|
||||
char *netname = NULL; /* name of the vpn network */
|
||||
|
||||
int config_compare(config_t *a, config_t *b)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = strcasecmp(a->variable, b->variable);
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
result = a->line - b->line;
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
else
|
||||
return strcmp(a->file, b->file);
|
||||
}
|
||||
|
||||
void init_configuration(avl_tree_t **config_tree)
|
||||
{
|
||||
cp
|
||||
*config_tree = avl_alloc_tree((avl_compare_t)config_compare, (avl_action_t)free_config);
|
||||
cp
|
||||
}
|
||||
|
||||
void exit_configuration(avl_tree_t **config_tree)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(*config_tree);
|
||||
*config_tree = NULL;
|
||||
cp
|
||||
}
|
||||
|
||||
config_t *new_config(void)
|
||||
{
|
||||
config_t *cfg;
|
||||
cp
|
||||
cfg = (config_t *)xmalloc_and_zero(sizeof(*cfg));
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
void free_config(config_t *cfg)
|
||||
{
|
||||
cp
|
||||
if(cfg->variable)
|
||||
free(cfg->variable);
|
||||
if(cfg->value)
|
||||
free(cfg->value);
|
||||
if(cfg->file)
|
||||
free(cfg->file);
|
||||
free(cfg);
|
||||
cp
|
||||
}
|
||||
|
||||
void config_add(avl_tree_t *config_tree, config_t *cfg)
|
||||
{
|
||||
cp
|
||||
avl_insert(config_tree, cfg);
|
||||
cp
|
||||
}
|
||||
|
||||
config_t *lookup_config(avl_tree_t *config_tree, char *variable)
|
||||
{
|
||||
config_t cfg, *found;
|
||||
cp
|
||||
cfg.variable = variable;
|
||||
cfg.file = "";
|
||||
cfg.line = 0;
|
||||
|
||||
found = avl_search_closest_greater(config_tree, &cfg);
|
||||
|
||||
if(!found)
|
||||
return NULL;
|
||||
|
||||
if(strcasecmp(found->variable, variable))
|
||||
return NULL;
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
config_t *lookup_config_next(avl_tree_t *config_tree, config_t *cfg)
|
||||
{
|
||||
avl_node_t *node;
|
||||
config_t *found;
|
||||
cp
|
||||
node = avl_search_node(config_tree, cfg);
|
||||
|
||||
if(node)
|
||||
{
|
||||
if(node->next)
|
||||
{
|
||||
found = (config_t *)node->next->data;
|
||||
if(!strcasecmp(found->variable, cfg->variable))
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int get_config_bool(config_t *cfg, int *result)
|
||||
{
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
if(!strcasecmp(cfg->value, "yes"))
|
||||
{
|
||||
*result = 1;
|
||||
return 1;
|
||||
}
|
||||
else if(!strcasecmp(cfg->value, "no"))
|
||||
{
|
||||
*result = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
syslog(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"),
|
||||
cfg->variable, cfg->file, cfg->line);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_config_int(config_t *cfg, int *result)
|
||||
{
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
if(sscanf(cfg->value, "%d", result) == 1)
|
||||
return 1;
|
||||
|
||||
syslog(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
|
||||
cfg->variable, cfg->file, cfg->line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_config_string(config_t *cfg, char **result)
|
||||
{
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
*result = xstrdup(cfg->value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int get_config_address(config_t *cfg, struct addrinfo **result)
|
||||
{
|
||||
struct addrinfo *ai;
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
ai = str2addrinfo(cfg->value, NULL, 0);
|
||||
|
||||
if(ai)
|
||||
{
|
||||
*result = ai;
|
||||
return 1;
|
||||
}
|
||||
|
||||
syslog(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
|
||||
cfg->variable, cfg->file, cfg->line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_config_port(config_t *cfg, port_t *result)
|
||||
{
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
if(sscanf(cfg->value, "%hu", result) == 1)
|
||||
{
|
||||
*result = htons(*result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
syslog(LOG_ERR, _("Port number expected for configuration variable %s in %s line %d"),
|
||||
cfg->variable, cfg->file, cfg->line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_config_subnet(config_t *cfg, subnet_t **result)
|
||||
{
|
||||
subnet_t *subnet;
|
||||
cp
|
||||
if(!cfg)
|
||||
return 0;
|
||||
|
||||
subnet = str2net(cfg->value);
|
||||
|
||||
if(!subnet)
|
||||
{
|
||||
syslog(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
|
||||
cfg->variable, cfg->file, cfg->line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Teach newbies what subnets are... */
|
||||
|
||||
if(((subnet->type == SUBNET_IPV4) && maskcheck((char *)&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t)))
|
||||
|| ((subnet->type == SUBNET_IPV6) && maskcheck((char *)&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t))))
|
||||
{
|
||||
syslog(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;
|
||||
}
|
||||
|
||||
*result = subnet;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
65
lib/conf.h
Normal file
65
lib/conf.h
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
conf.h -- header for conf.c
|
||||
Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_CONF_H__
|
||||
#define __TINC_CONF_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
#include "net.h"
|
||||
#include "subnet.h"
|
||||
|
||||
typedef struct config_t {
|
||||
char *variable;
|
||||
char *value;
|
||||
char *file;
|
||||
int line;
|
||||
} config_t;
|
||||
|
||||
extern avl_tree_t *config_tree;
|
||||
|
||||
extern int pingtimeout;
|
||||
extern int maxtimeout;
|
||||
extern int bypass_security;
|
||||
extern char *confbase;
|
||||
extern char *netname;
|
||||
|
||||
extern void init_configuration(avl_tree_t **);
|
||||
extern void exit_configuration(avl_tree_t **);
|
||||
extern config_t *new_config(void);
|
||||
extern void free_config(config_t *);
|
||||
extern void config_add(avl_tree_t *, config_t *);
|
||||
extern config_t *lookup_config(avl_tree_t *, char *);
|
||||
extern config_t *lookup_config_next(avl_tree_t *, config_t *);
|
||||
extern int get_config_bool(config_t *, int *);
|
||||
extern int get_config_int(config_t *, int *);
|
||||
extern int get_config_port(config_t *, port_t *);
|
||||
extern int get_config_string(config_t *, char **);
|
||||
extern int get_config_address(config_t *, struct addrinfo **);
|
||||
struct subnet_t; /* Needed for next line. */
|
||||
extern int get_config_subnet(config_t *, struct subnet_t **);
|
||||
|
||||
#endif /* __TINC_CONF_H__ */
|
||||
126
lib/connection.c
Normal file
126
lib/connection.c
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
connection.c -- connection list management
|
||||
Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
#include <list.h>
|
||||
|
||||
#include "net.h" /* Don't ask. */
|
||||
#include "netutl.h"
|
||||
#include "config.h"
|
||||
#include "conf.h"
|
||||
#include <utils.h>
|
||||
#include "subnet.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "xalloc.h"
|
||||
#include "system.h"
|
||||
|
||||
avl_tree_t *connection_tree; /* Meta connections */
|
||||
|
||||
int connection_compare(connection_t *a, connection_t *b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
void init_connections(void)
|
||||
{
|
||||
cp
|
||||
connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, NULL);
|
||||
cp
|
||||
}
|
||||
|
||||
void exit_connections(void)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(connection_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
connection_t *new_connection(void)
|
||||
{
|
||||
connection_t *c;
|
||||
cp
|
||||
c = (connection_t *)xmalloc_and_zero(sizeof(connection_t));
|
||||
|
||||
if(!c)
|
||||
return NULL;
|
||||
|
||||
gettimeofday(&c->start, NULL);
|
||||
cp
|
||||
return c;
|
||||
}
|
||||
|
||||
void free_connection(connection_t *c)
|
||||
{
|
||||
cp
|
||||
if(c->hostname)
|
||||
free(c->hostname);
|
||||
if(c->inkey)
|
||||
free(c->inkey);
|
||||
if(c->outkey)
|
||||
free(c->outkey);
|
||||
if(c->mychallenge)
|
||||
free(c->mychallenge);
|
||||
if(c->hischallenge)
|
||||
free(c->hischallenge);
|
||||
free(c);
|
||||
cp
|
||||
}
|
||||
|
||||
void connection_add(connection_t *c)
|
||||
{
|
||||
cp
|
||||
avl_insert(connection_tree, c);
|
||||
cp
|
||||
}
|
||||
|
||||
void connection_del(connection_t *c)
|
||||
{
|
||||
cp
|
||||
avl_delete(connection_tree, c);
|
||||
cp
|
||||
}
|
||||
|
||||
void dump_connections(void)
|
||||
{
|
||||
avl_node_t *node;
|
||||
connection_t *c;
|
||||
cp
|
||||
syslog(LOG_DEBUG, _("Connections:"));
|
||||
|
||||
for(node = connection_tree->head; node; node = node->next)
|
||||
{
|
||||
c = (connection_t *)node->data;
|
||||
syslog(LOG_DEBUG, _(" %s at %s options %lx socket %d status %04x"),
|
||||
c->name, c->hostname, c->options, c->socket, c->status);
|
||||
}
|
||||
|
||||
syslog(LOG_DEBUG, _("End of connections."));
|
||||
cp
|
||||
}
|
||||
141
lib/connection.h
Normal file
141
lib/connection.h
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
connection.h -- header for connection.c
|
||||
Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_CONNECTION_H__
|
||||
#define __TINC_CONNECTION_H__
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
#include <list.h>
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
# ifdef HAVE_OPENSSL_EVP_H
|
||||
# include <openssl/evp.h>
|
||||
# else
|
||||
# include <evp.h>
|
||||
# endif
|
||||
|
||||
# ifdef HAVE_OPENSSL_RSA_H
|
||||
# include <openssl/rsa.h>
|
||||
# else
|
||||
# include <rsa.h>
|
||||
# endif
|
||||
#endif /* USE_OPENSSL */
|
||||
|
||||
#ifdef USE_GCRYPT
|
||||
# include <gcrypt.h>
|
||||
#endif
|
||||
|
||||
#include "net.h"
|
||||
#include "conf.h"
|
||||
|
||||
#include "node.h"
|
||||
#include "edge.h"
|
||||
|
||||
#define OPTION_INDIRECT 0x0001
|
||||
#define OPTION_TCPONLY 0x0002
|
||||
|
||||
typedef struct connection_status_t {
|
||||
int pinged:1; /* sent ping */
|
||||
int active:1; /* 1 if active.. */
|
||||
int connecting:1; /* 1 if we are waiting for a non-blocking connect() to finish */
|
||||
int termreq:1; /* the termination of this connection was requested */
|
||||
int remove:1; /* Set to 1 if you want this connection removed */
|
||||
int timeout:1; /* 1 if gotten timeout */
|
||||
int encryptout:1; /* 1 if we can encrypt outgoing traffic */
|
||||
int decryptin:1; /* 1 if we have to decrypt incoming traffic */
|
||||
int mst:1; /* 1 if this connection is part of a minimum spanning tree */
|
||||
int unused:18;
|
||||
} connection_status_t;
|
||||
|
||||
typedef struct connection_t {
|
||||
char *name; /* name he claims to have */
|
||||
|
||||
sockaddr_t address; /* his real (internet) ip */
|
||||
char *hostname; /* the hostname of its real ip */
|
||||
int protocol_version; /* used protocol */
|
||||
|
||||
int socket; /* socket used for this connection */
|
||||
long int options; /* options for this connection */
|
||||
struct connection_status_t status; /* status info */
|
||||
int estimated_weight; /* estimation for the weight of the edge for this connection */
|
||||
struct timeval start; /* time this connection was started, used for above estimation */
|
||||
struct outgoing_t *outgoing; /* used to keep track of outgoing connections */
|
||||
|
||||
struct node_t *node; /* node associated with the other end */
|
||||
struct edge_t *edge; /* edge associated with this connection */
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
RSA *rsa_key; /* his public/private key */
|
||||
const EVP_CIPHER *incipher; /* Cipher he will use to send data to us */
|
||||
const EVP_CIPHER *outcipher; /* Cipher we will use to send data to him */
|
||||
EVP_CIPHER_CTX *inctx; /* Context of encrypted meta data that will come from him to us */
|
||||
EVP_CIPHER_CTX *outctx; /* Context of encrypted meta data that will be sent from us to him */
|
||||
const EVP_MD *indigest;
|
||||
const EVP_MD *outdigest;
|
||||
#endif
|
||||
|
||||
#ifdef USE_GCRYPT
|
||||
GCRY_SEXP rsa_key;
|
||||
GCRY_CIPHER_HD incipher;
|
||||
GCRY_CIPHER_HD outcipher;
|
||||
GCRY_CIPHER_HD inctx;
|
||||
GCRY_CIPHER_HD outctx;
|
||||
GCRY_MD_HD indigest;
|
||||
GCRY_MD_HD outdigest;
|
||||
/* FIXME */
|
||||
#endif
|
||||
|
||||
char *inkey; /* His symmetric meta key + iv */
|
||||
char *outkey; /* Our symmetric meta key + iv */
|
||||
int inkeylength; /* Length of his key + iv */
|
||||
int outkeylength; /* Length of our key + iv */
|
||||
int inmaclength;
|
||||
int outmaclength;
|
||||
int incompression;
|
||||
int outcompression;
|
||||
char *mychallenge; /* challenge we received from him */
|
||||
char *hischallenge; /* challenge we sent to him */
|
||||
|
||||
char buffer[MAXBUFSIZE]; /* metadata input buffer */
|
||||
int buflen; /* bytes read into buffer */
|
||||
int tcplen; /* length of incoming TCPpacket */
|
||||
int allow_request; /* defined if there's only one request possible */
|
||||
|
||||
time_t last_ping_time; /* last time we saw some activity from the other end */
|
||||
|
||||
avl_tree_t *config_tree; /* Pointer to configuration tree belonging to him */
|
||||
} connection_t;
|
||||
|
||||
extern avl_tree_t *connection_tree;
|
||||
|
||||
extern void init_connections(void);
|
||||
extern void exit_connections(void);
|
||||
extern connection_t *new_connection(void);
|
||||
extern void free_connection(connection_t *);
|
||||
extern void connection_add(connection_t *);
|
||||
extern void connection_del(connection_t *);
|
||||
extern void dump_connections(void);
|
||||
extern int read_connection_config(connection_t *);
|
||||
|
||||
#endif /* __TINC_CONNECTION_H__ */
|
||||
212
lib/edge.c
Normal file
212
lib/edge.c
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
edge.c -- edge tree management
|
||||
Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: edge.c,v 1.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <syslog.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
#include <list.h>
|
||||
|
||||
#include "net.h" /* Don't ask. */
|
||||
#include "netutl.h"
|
||||
#include "config.h"
|
||||
#include "conf.h"
|
||||
#include <utils.h>
|
||||
#include "subnet.h"
|
||||
|
||||
#include "xalloc.h"
|
||||
#include "system.h"
|
||||
|
||||
avl_tree_t *edge_tree; /* Tree with all known edges (replaces active_tree) */
|
||||
avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
|
||||
|
||||
int edge_compare(edge_t *a, edge_t *b)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = strcmp(a->from.node->name, b->from.node->name);
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
else
|
||||
return strcmp(a->to.node->name, b->to.node->name);
|
||||
}
|
||||
|
||||
/* Evil edge_compare() from a parallel universe ;)
|
||||
|
||||
int edge_compare(edge_t *a, edge_t *b)
|
||||
{
|
||||
int result;
|
||||
|
||||
return (result = strcmp(a->from.node->name, b->from.node->name)) || (result = strcmp(a->to.node->name, b->to.node->name)), result;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
int edge_name_compare(edge_t *a, edge_t *b)
|
||||
{
|
||||
int result;
|
||||
char *name_a1, *name_a2, *name_b1, *name_b2;
|
||||
|
||||
if(strcmp(a->from.node->name, a->to.node->name) < 0)
|
||||
name_a1 = a->from.node->name, name_a2 = a->to.node->name;
|
||||
else
|
||||
name_a1 = a->to.node->name, name_a2 = a->from.node->name;
|
||||
|
||||
if(strcmp(b->from.node->name, b->to.node->name) < 0)
|
||||
name_b1 = b->from.node->name, name_b2 = b->to.node->name;
|
||||
else
|
||||
name_b1 = b->to.node->name, name_b2 = b->from.node->name;
|
||||
|
||||
result = strcmp(name_a1, name_b1);
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
else
|
||||
return strcmp(name_a2, name_b2);
|
||||
}
|
||||
|
||||
int edge_weight_compare(edge_t *a, edge_t *b)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = a->weight - b->weight;
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
else
|
||||
return edge_name_compare(a, b);
|
||||
}
|
||||
|
||||
void init_edges(void)
|
||||
{
|
||||
cp
|
||||
edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
|
||||
edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
|
||||
cp
|
||||
}
|
||||
|
||||
avl_tree_t *new_edge_tree(void)
|
||||
{
|
||||
cp
|
||||
return avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
|
||||
cp
|
||||
}
|
||||
|
||||
void free_edge_tree(avl_tree_t *edge_tree)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(edge_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
void exit_edges(void)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(edge_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
/* Creation and deletion of connection elements */
|
||||
|
||||
edge_t *new_edge(void)
|
||||
{
|
||||
edge_t *e;
|
||||
cp
|
||||
e = (edge_t *)xmalloc_and_zero(sizeof(*e));
|
||||
cp
|
||||
return e;
|
||||
}
|
||||
|
||||
void free_edge(edge_t *e)
|
||||
{
|
||||
cp
|
||||
free(e);
|
||||
cp
|
||||
}
|
||||
|
||||
void edge_add(edge_t *e)
|
||||
{
|
||||
cp
|
||||
avl_insert(edge_tree, e);
|
||||
avl_insert(edge_weight_tree, e);
|
||||
avl_insert(e->from.node->edge_tree, e);
|
||||
avl_insert(e->to.node->edge_tree, e);
|
||||
cp
|
||||
}
|
||||
|
||||
void edge_del(edge_t *e)
|
||||
{
|
||||
cp
|
||||
avl_delete(edge_tree, e);
|
||||
avl_delete(edge_weight_tree, e);
|
||||
avl_delete(e->from.node->edge_tree, e);
|
||||
avl_delete(e->to.node->edge_tree, e);
|
||||
cp
|
||||
}
|
||||
|
||||
edge_t *lookup_edge(node_t *from, node_t *to)
|
||||
{
|
||||
edge_t v, *result;
|
||||
cp
|
||||
v.from.node = from;
|
||||
v.to.node = to;
|
||||
|
||||
result = avl_search(edge_tree, &v);
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
cp
|
||||
v.from.node = to;
|
||||
v.to.node = from;
|
||||
|
||||
return avl_search(edge_tree, &v);
|
||||
}
|
||||
|
||||
void dump_edges(void)
|
||||
{
|
||||
avl_node_t *node;
|
||||
edge_t *e;
|
||||
char *from_udp, *to_udp;
|
||||
cp
|
||||
syslog(LOG_DEBUG, _("Edges:"));
|
||||
|
||||
for(node = edge_tree->head; node; node = node->next)
|
||||
{
|
||||
e = (edge_t *)node->data;
|
||||
from_udp = sockaddr2hostname(&e->from.udpaddress);
|
||||
to_udp = sockaddr2hostname(&e->to.udpaddress);
|
||||
syslog(LOG_DEBUG, _(" %s at %s - %s at %s options %lx weight %d"),
|
||||
e->from.node->name, from_udp,
|
||||
e->to.node->name, to_udp,
|
||||
e->options, e->weight);
|
||||
free(from_udp);
|
||||
free(to_udp);
|
||||
}
|
||||
|
||||
syslog(LOG_DEBUG, _("End of edges."));
|
||||
cp
|
||||
}
|
||||
64
lib/edge.h
Normal file
64
lib/edge.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
edge.h -- header for edge.c
|
||||
Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: edge.h,v 1.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_EDGE_H__
|
||||
#define __TINC_EDGE_H__
|
||||
|
||||
#include <avl_tree.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "node.h"
|
||||
#include "connection.h"
|
||||
|
||||
typedef struct halfconnection_t {
|
||||
struct node_t *node; /* node associated with this end of the connection */
|
||||
/* sockaddr_t tcpaddress; */ /* real (internet) ip on this end of the meta connection */
|
||||
sockaddr_t udpaddress; /* real (internet) ip on this end of the vpn connection */
|
||||
} halfconnection_t;
|
||||
|
||||
typedef struct edge_t {
|
||||
struct halfconnection_t from;
|
||||
struct halfconnection_t to;
|
||||
|
||||
long int options; /* options turned on for this edge */
|
||||
int weight; /* weight of this edge */
|
||||
|
||||
struct connection_t *connection; /* connection associated with this edge, if available */
|
||||
|
||||
void *if_data; /* Interface data */
|
||||
} edge_t;
|
||||
|
||||
extern avl_tree_t *edge_tree; /* Tree with all known edges (replaces active_tree) */
|
||||
extern avl_tree_t *edge_weight_tree; /* Tree with all known edges sorted on weight */
|
||||
|
||||
extern void init_edges(void);
|
||||
extern void exit_edges(void);
|
||||
extern edge_t *new_edge(void);
|
||||
extern void free_edge(edge_t *);
|
||||
extern avl_tree_t *new_edge_tree(void);
|
||||
extern void free_edge_tree(avl_tree_t *);
|
||||
extern void edge_add(edge_t *);
|
||||
extern void edge_del(edge_t *);
|
||||
extern edge_t *lookup_edge(struct node_t *, struct node_t *);
|
||||
extern void dump_edges(void);
|
||||
|
||||
#endif /* __TINC_EDGE_H__ */
|
||||
106
lib/logging.c
Normal file
106
lib/logging.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
logging.c -- log messages to e.g. syslog
|
||||
Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: logging.c,v 1.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
avl_tree_t *log_hooks_tree = NULL;
|
||||
|
||||
int debug_lvl = 0;
|
||||
|
||||
int log_compare(const void *a, const void *b)
|
||||
{
|
||||
if(a < b)
|
||||
return -1;
|
||||
if(a > b)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void log(int level, int priority, char *fmt, ...)
|
||||
{
|
||||
avl_node_t *avlnode;
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
|
||||
{
|
||||
assert(avlnode->data);
|
||||
((log_function_t*)(avlnode->data))(level, priority, fmt, args);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void log_add_hook(log_function_t *fn)
|
||||
{
|
||||
if(!log_hooks_tree)
|
||||
log_hooks_tree = avl_alloc_tree(log_compare, NULL);
|
||||
|
||||
avl_insert(log_hooks_tree, (void*)fn);
|
||||
}
|
||||
|
||||
void log_del_hook(log_function_t *fn)
|
||||
{
|
||||
avl_delete(log_hooks_tree, (void*)fn);
|
||||
}
|
||||
|
||||
void log_default(int level, int priority, char *fmt, va_list ap)
|
||||
{
|
||||
if(debug_lvl >= level)
|
||||
{
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
void log_syslog(int level, int priority, char *fmt, va_list ap)
|
||||
{
|
||||
const int priorities[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_CRIT };
|
||||
|
||||
if(debug_lvl >= level)
|
||||
vsyslog(priorities[priority], fmt, ap);
|
||||
}
|
||||
|
||||
void tinc_syslog(int priority, char *fmt, ...)
|
||||
{
|
||||
/* Mapping syslog prio -> tinc prio */
|
||||
const int priorities[] = { TLOG_CRITICAL, TLOG_CRITICAL, TLOG_CRITICAL, TLOG_ERROR,
|
||||
TLOG_NOTICE, TLOG_NOTICE, TLOG_INFO, TLOG_DEBUG };
|
||||
avl_node_t *avlnode;
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
|
||||
{
|
||||
assert(avlnode->data);
|
||||
((log_function_t*)(avlnode->data))(0, priorities[priority], fmt, args);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
74
lib/logging.h
Normal file
74
lib/logging.h
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
logging.h -- header for logging.c
|
||||
Copyright (C) 2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2002 Ivo Timmermans <ivo@o2w.nl>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: logging.h,v 1.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_LOGGING_H__
|
||||
#define __TINC_LOGGING_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
enum {
|
||||
TLOG_DEBUG,
|
||||
TLOG_INFO,
|
||||
TLOG_NOTICE,
|
||||
TLOG_ERROR,
|
||||
TLOG_CRITICAL
|
||||
};
|
||||
|
||||
enum {
|
||||
DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */
|
||||
DEBUG_CONNECTIONS = 1, /* Show (dis)connects of other tinc daemons via TCP */
|
||||
DEBUG_ERROR = 2, /* Show error messages received from other hosts */
|
||||
DEBUG_STATUS = 2, /* Show status messages received from other hosts */
|
||||
DEBUG_PROTOCOL = 3, /* Show the requests that are sent/received */
|
||||
DEBUG_META = 4, /* Show contents of every request that is sent/received */
|
||||
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 */
|
||||
};
|
||||
|
||||
typedef void (log_function_t)(int,int,char*,va_list);
|
||||
|
||||
extern int debug_lvl;
|
||||
extern avl_tree_t *log_hooks_tree;
|
||||
|
||||
extern void log(int, int, char *, ...);
|
||||
extern void log_add_hook(log_function_t *);
|
||||
extern void log_del_hook(log_function_t *);
|
||||
extern log_function_t log_default;
|
||||
extern log_function_t log_syslog;
|
||||
extern void tinc_syslog(int, char *, ...);
|
||||
|
||||
#ifndef LOG_ERR /* Something from syslog.h */
|
||||
# define syslog tinc_syslog
|
||||
#define LOG_EMERG 0 /* system is unusable */
|
||||
#define LOG_ALERT 1 /* action must be taken immediately */
|
||||
#define LOG_CRIT 2 /* critical conditions */
|
||||
#define LOG_ERR 3 /* error conditions */
|
||||
#define LOG_WARNING 4 /* warning conditions */
|
||||
#define LOG_NOTICE 5 /* normal but significant condition */
|
||||
#define LOG_INFO 6 /* informational */
|
||||
#define LOG_DEBUG 7 /* debug-level messages */
|
||||
#else
|
||||
# warning dont include syslog!
|
||||
#endif
|
||||
|
||||
#endif /* __TINC_LOGGING_H__ */
|
||||
153
lib/net.h
Normal file
153
lib/net.h
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
net.h -- header for net.c
|
||||
Copyright (C) 1998-2002 Ivo Timmermans <zarq@iname.com>
|
||||
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_NET_H__
|
||||
#define __TINC_NET_H__
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef ENABLE_JUMBOGRAMS
|
||||
#define MTU 9014 /* 9000 bytes payload + 14 bytes ethernet header */
|
||||
#define MAXSIZE 9100 /* MTU + header (seqno) and trailer (CBC padding and HMAC) */
|
||||
#define MAXBUFSIZE 9100 /* Must support TCP packets of length 9000. */
|
||||
#else
|
||||
#define MTU 1514 /* 1500 bytes payload + 14 bytes ethernet header */
|
||||
#define MAXSIZE 1600 /* MTU + header (seqno) and trailer (CBC padding and HMAC) */
|
||||
#define MAXBUFSIZE 2100 /* Quite large but needed for support of keys up to 8192 bits. */
|
||||
#endif
|
||||
|
||||
#define MAXSOCKETS 128 /* Overkill... */
|
||||
|
||||
#define MAXQUEUELENGTH 8 /* Maximum number of packats in a single queue */
|
||||
|
||||
typedef struct mac_t
|
||||
{
|
||||
unsigned char x[6];
|
||||
} mac_t;
|
||||
|
||||
typedef struct ipv4_t
|
||||
{
|
||||
unsigned char x[4];
|
||||
} ipv4_t;
|
||||
|
||||
typedef struct ip_mask_t {
|
||||
ipv4_t address;
|
||||
ipv4_t mask;
|
||||
} ip_mask_t;
|
||||
|
||||
typedef struct ipv6_t
|
||||
{
|
||||
unsigned short x[8];
|
||||
} ipv6_t;
|
||||
|
||||
typedef unsigned short port_t;
|
||||
|
||||
typedef short length_t;
|
||||
|
||||
typedef union {
|
||||
struct sockaddr sa;
|
||||
struct sockaddr_in in;
|
||||
struct sockaddr_in6 in6;
|
||||
} sockaddr_t;
|
||||
|
||||
#ifdef SA_LEN
|
||||
#define SALEN(s) SA_LEN(&s)
|
||||
#else
|
||||
#define SALEN(s) (s.sa_family==AF_INET?sizeof(struct sockaddr_in):sizeof(struct sockaddr_in6))
|
||||
#endif
|
||||
|
||||
typedef struct vpn_packet_t {
|
||||
length_t len; /* the actual number of bytes in the `data' field */
|
||||
int priority; /* priority or TOS */
|
||||
unsigned int seqno; /* 32 bits sequence number (network byte order of course) */
|
||||
unsigned char data[MAXSIZE];
|
||||
} vpn_packet_t;
|
||||
|
||||
typedef struct queue_element_t {
|
||||
void *packet;
|
||||
struct queue_element_t *prev;
|
||||
struct queue_element_t *next;
|
||||
} queue_element_t;
|
||||
|
||||
typedef struct packet_queue_t {
|
||||
queue_element_t *head;
|
||||
queue_element_t *tail;
|
||||
} packet_queue_t;
|
||||
|
||||
typedef struct outgoing_t {
|
||||
char *name;
|
||||
int timeout;
|
||||
struct config_t *cfg;
|
||||
struct addrinfo *ai;
|
||||
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 char *request_name[];
|
||||
extern char *status_text[];
|
||||
|
||||
#include "connection.h" /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
|
||||
|
||||
extern listen_socket_t listen_socket[MAXSOCKETS];
|
||||
extern int listen_sockets;
|
||||
extern int keyexpires;
|
||||
extern int keylifetime;
|
||||
extern int do_prune;
|
||||
extern int do_purge;
|
||||
extern char *myport;
|
||||
extern time_t now;
|
||||
|
||||
extern void retry_outgoing(outgoing_t *);
|
||||
extern void handle_incoming_vpn_data(int);
|
||||
extern void finish_connecting(connection_t *);
|
||||
extern void do_outgoing_connection(connection_t *);
|
||||
extern int 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_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 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(connection_t *, int);
|
||||
extern void flush_queue(struct node_t *);
|
||||
extern int read_rsa_public_key(struct connection_t *);
|
||||
|
||||
#endif /* __TINC_NET_H__ */
|
||||
247
lib/netutl.c
Normal file
247
lib/netutl.c
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
netutl.c -- some supporting network utility code
|
||||
Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <utils.h>
|
||||
#include <xalloc.h>
|
||||
|
||||
#include "errno.h"
|
||||
#include "conf.h"
|
||||
#include "net.h"
|
||||
#include "netutl.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "system.h"
|
||||
|
||||
int hostnames = 0;
|
||||
|
||||
/*
|
||||
Turn a string into a struct addrinfo.
|
||||
Return NULL on failure.
|
||||
*/
|
||||
struct addrinfo *str2addrinfo(char *address, char *service, int socktype)
|
||||
{
|
||||
struct addrinfo hint, *ai;
|
||||
int err;
|
||||
cp
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
|
||||
hint.ai_family = addressfamily;
|
||||
hint.ai_socktype = socktype;
|
||||
|
||||
if((err = getaddrinfo(address, service, &hint, &ai)))
|
||||
{
|
||||
if(debug_lvl >= DEBUG_ERROR)
|
||||
syslog(LOG_WARNING, _("Error looking up %s port %s: %s\n"), address, service, gai_strerror(err));
|
||||
cp_trace();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cp
|
||||
return ai;
|
||||
}
|
||||
|
||||
sockaddr_t str2sockaddr(char *address, char *port)
|
||||
{
|
||||
struct addrinfo hint, *ai;
|
||||
sockaddr_t result;
|
||||
int err;
|
||||
cp
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
|
||||
hint.ai_family = AF_UNSPEC;
|
||||
hint.ai_flags = AI_NUMERICHOST;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if((err = getaddrinfo(address, port, &hint, &ai) || !ai))
|
||||
{
|
||||
syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err));
|
||||
cp_trace();
|
||||
raise(SIGFPE);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
result = *(sockaddr_t *)ai->ai_addr;
|
||||
freeaddrinfo(ai);
|
||||
cp
|
||||
return result;
|
||||
}
|
||||
|
||||
void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr)
|
||||
{
|
||||
char address[NI_MAXHOST];
|
||||
char port[NI_MAXSERV];
|
||||
char *scopeid;
|
||||
int err;
|
||||
cp
|
||||
if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
|
||||
{
|
||||
syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
|
||||
cp_trace();
|
||||
raise(SIGFPE);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#ifdef HAVE_LINUX
|
||||
if((scopeid = strchr(address, '%')))
|
||||
*scopeid = '\0'; /* Descope. */
|
||||
#endif
|
||||
|
||||
*addrstr = xstrdup(address);
|
||||
*portstr = xstrdup(port);
|
||||
cp
|
||||
}
|
||||
|
||||
char *sockaddr2hostname(sockaddr_t *sa)
|
||||
{
|
||||
char *str;
|
||||
char address[NI_MAXHOST] = "unknown";
|
||||
char port[NI_MAXSERV] = "unknown";
|
||||
int err;
|
||||
cp
|
||||
if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
|
||||
{
|
||||
syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
|
||||
}
|
||||
|
||||
asprintf(&str, _("%s port %s"), address, port);
|
||||
cp
|
||||
return str;
|
||||
}
|
||||
|
||||
int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
|
||||
{
|
||||
int result;
|
||||
cp
|
||||
result = a->sa.sa_family - b->sa.sa_family;
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
switch(a->sa.sa_family)
|
||||
{
|
||||
case AF_UNSPEC:
|
||||
return 0;
|
||||
case AF_INET:
|
||||
result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
|
||||
if(result)
|
||||
return result;
|
||||
return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
|
||||
case AF_INET6:
|
||||
result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
|
||||
if(result)
|
||||
return result;
|
||||
return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
|
||||
default:
|
||||
syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
|
||||
cp_trace();
|
||||
raise(SIGFPE);
|
||||
exit(0);
|
||||
}
|
||||
cp
|
||||
}
|
||||
|
||||
void sockaddrunmap(sockaddr_t *sa)
|
||||
{
|
||||
if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
|
||||
{
|
||||
sa->in.sin_addr.s_addr = ((uint32_t *)&sa->in6.sin6_addr)[3];
|
||||
sa->in.sin_family = AF_INET;
|
||||
}
|
||||
}
|
||||
|
||||
/* Subnet mask handling */
|
||||
|
||||
int maskcmp(char *a, char *b, int masklen, int len)
|
||||
{
|
||||
int i, m, result;
|
||||
cp
|
||||
for(m = masklen, i = 0; m >= 8; m -= 8, i++)
|
||||
if((result = a[i] - b[i]))
|
||||
return result;
|
||||
|
||||
if(m)
|
||||
return (a[i] & (0x100 - (1 << (8 - m)))) - (b[i] & (0x100 - (1 << (8 - m))));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mask(char *a, int masklen, int len)
|
||||
{
|
||||
int i;
|
||||
cp
|
||||
i = masklen / 8;
|
||||
masklen %= 8;
|
||||
|
||||
if(masklen)
|
||||
a[i++] &= (0x100 - (1 << masklen));
|
||||
|
||||
for(; i < len; i++)
|
||||
a[i] = 0;
|
||||
}
|
||||
|
||||
void maskcpy(char *a, char *b, int masklen, int len)
|
||||
{
|
||||
int i, m;
|
||||
cp
|
||||
for(m = masklen, i = 0; m >= 8; m -= 8, i++)
|
||||
a[i] = b[i];
|
||||
|
||||
if(m)
|
||||
{
|
||||
a[i] = b[i] & (0x100 - (1 << m));
|
||||
i++;
|
||||
}
|
||||
|
||||
for(; i < len; i++)
|
||||
a[i] = 0;
|
||||
}
|
||||
|
||||
int maskcheck(char *a, int masklen, int len)
|
||||
{
|
||||
int i;
|
||||
cp
|
||||
i = masklen / 8;
|
||||
masklen %= 8;
|
||||
|
||||
if(masklen)
|
||||
if(a[i++] & (char)~(0x100 - (1 << masklen)))
|
||||
return -1;
|
||||
|
||||
for(; i < len; i++)
|
||||
if(a[i] != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
46
lib/netutl.h
Normal file
46
lib/netutl.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
netutl.h -- header file for netutl.c
|
||||
Copyright (C) 1998-2002 Ivo Timmermans <zarq@iname.com>
|
||||
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_NETUTL_H__
|
||||
#define __TINC_NETUTL_H__
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include "net.h"
|
||||
|
||||
extern int hostnames;
|
||||
|
||||
extern char *hostlookup(unsigned long);
|
||||
extern struct addrinfo *str2addrinfo(char *, char *, int);
|
||||
extern sockaddr_t str2sockaddr(char *, char *);
|
||||
extern void sockaddr2str(sockaddr_t *, char **, char **);
|
||||
extern char *sockaddr2hostname(sockaddr_t *);
|
||||
extern int sockaddrcmp(sockaddr_t *, sockaddr_t *);
|
||||
extern void sockaddrunmap(sockaddr_t *);
|
||||
extern int maskcmp(char *, char *, int, int);
|
||||
extern void maskcpy(char *, char *, int, int);
|
||||
extern void mask(char *, int, int);
|
||||
extern int maskcheck(char *, int, int);
|
||||
|
||||
#endif /* __TINC_NETUTL_H__ */
|
||||
188
lib/node.c
Normal file
188
lib/node.c
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
node.c -- node tree management
|
||||
Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: node.c,v 1.1 2002/04/28 12:46:25 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
#include "node.h"
|
||||
#include "netutl.h"
|
||||
#include "net.h"
|
||||
#include <utils.h>
|
||||
#include <xalloc.h>
|
||||
#include <hooks.h>
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
#include "system.h"
|
||||
|
||||
avl_tree_t *node_tree; /* Known nodes, sorted by name */
|
||||
avl_tree_t *node_udp_tree; /* Known nodes, sorted by address and port */
|
||||
|
||||
node_t *myself;
|
||||
|
||||
int node_compare(node_t *a, node_t *b)
|
||||
{
|
||||
return strcmp(a->name, b->name);
|
||||
}
|
||||
|
||||
int node_udp_compare(node_t *a, node_t *b)
|
||||
{
|
||||
int result;
|
||||
cp
|
||||
result = sockaddrcmp(&a->address, &b->address);
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
return (a->name && b->name)?strcmp(a->name, b->name):0;
|
||||
}
|
||||
|
||||
void init_nodes(void)
|
||||
{
|
||||
cp
|
||||
node_tree = avl_alloc_tree((avl_compare_t)node_compare, NULL);
|
||||
node_udp_tree = avl_alloc_tree((avl_compare_t)node_udp_compare, NULL);
|
||||
cp
|
||||
}
|
||||
|
||||
void exit_nodes(void)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(node_tree);
|
||||
avl_delete_tree(node_udp_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
node_t *new_node(void)
|
||||
{
|
||||
node_t *n = (node_t *)xmalloc_and_zero(sizeof(*n));
|
||||
cp
|
||||
n->subnet_tree = new_subnet_tree();
|
||||
n->edge_tree = new_edge_tree();
|
||||
n->queue = list_alloc((list_action_t)free);
|
||||
cp
|
||||
return n;
|
||||
}
|
||||
|
||||
void free_node(node_t *n)
|
||||
{
|
||||
cp
|
||||
if(n->queue)
|
||||
list_delete_list(n->queue);
|
||||
if(n->name)
|
||||
free(n->name);
|
||||
if(n->hostname)
|
||||
free(n->hostname);
|
||||
if(n->key)
|
||||
free(n->key);
|
||||
if(n->subnet_tree)
|
||||
free_subnet_tree(n->subnet_tree);
|
||||
if(n->edge_tree)
|
||||
free_edge_tree(n->edge_tree);
|
||||
free(n);
|
||||
cp
|
||||
}
|
||||
|
||||
void node_add(node_t *n)
|
||||
{
|
||||
cp
|
||||
avl_insert(node_tree, n);
|
||||
avl_insert(node_udp_tree, n);
|
||||
|
||||
run_hooks("node-add", n);
|
||||
cp
|
||||
}
|
||||
|
||||
void node_del(node_t *n)
|
||||
{
|
||||
avl_node_t *node, *next;
|
||||
edge_t *e;
|
||||
subnet_t *s;
|
||||
cp
|
||||
for(node = n->subnet_tree->head; node; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
s = (subnet_t *)node->data;
|
||||
subnet_del(n, s);
|
||||
}
|
||||
|
||||
for(node = n->subnet_tree->head; node; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
e = (edge_t *)node->data;
|
||||
edge_del(e);
|
||||
}
|
||||
cp
|
||||
avl_delete(node_tree, n);
|
||||
avl_delete(node_udp_tree, n);
|
||||
|
||||
run_hooks("node-del", n);
|
||||
cp
|
||||
}
|
||||
|
||||
node_t *lookup_node(char *name)
|
||||
{
|
||||
node_t n;
|
||||
cp
|
||||
n.name = name;
|
||||
return avl_search(node_tree, &n);
|
||||
}
|
||||
|
||||
node_t *lookup_node_udp(sockaddr_t *sa)
|
||||
{
|
||||
node_t n;
|
||||
cp
|
||||
n.address = *sa;
|
||||
n.name = NULL;
|
||||
|
||||
return avl_search(node_udp_tree, &n);
|
||||
}
|
||||
|
||||
void dump_nodes(void)
|
||||
{
|
||||
avl_node_t *node;
|
||||
node_t *n;
|
||||
cp
|
||||
log(0, TLOG_DEBUG,
|
||||
_("Nodes:"));
|
||||
|
||||
for(node = node_tree->head; node; node = node->next)
|
||||
{
|
||||
n = (node_t *)node->data;
|
||||
#ifdef USE_OPENSSL
|
||||
syslog(LOG_DEBUG, _(" %s at %s cipher %d digest %d maclength %d compression %d options %lx status %04x nexthop %s via %s"),
|
||||
n->name, n->hostname, n->cipher?n->cipher->nid:0, n->digest?n->digest->type:0, n->maclength, n->compression, n->options,
|
||||
n->status, n->nexthop?n->nexthop->name:"-", n->via?n->via->name:"-");
|
||||
#endif
|
||||
#ifdef USE_GCRYPT
|
||||
syslog(LOG_DEBUG, _(" %s at %s cipher %d digest %d maclength %d compression %d options %lx status %04x nexthop %s via %s"),
|
||||
n->name, n->hostname, n->cipher?-1:0, n->digest?-1:0, n->maclength, n->compression, n->options,
|
||||
n->status, n->nexthop?n->nexthop->name:"-", n->via?n->via->name:"-");
|
||||
#endif
|
||||
}
|
||||
|
||||
syslog(LOG_DEBUG, _("End of nodes."));
|
||||
cp
|
||||
}
|
||||
104
lib/node.h
Normal file
104
lib/node.h
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
node.h -- header for node.c
|
||||
Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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 2002/04/28 12:46:26 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_NODE_H__
|
||||
#define __TINC_NODE_H__
|
||||
|
||||
#include <gcrypt.h>
|
||||
|
||||
#include <avl_tree.h>
|
||||
|
||||
#include "subnet.h"
|
||||
#include "connection.h"
|
||||
|
||||
typedef struct node_status_t {
|
||||
int active:1; /* 1 if active.. */
|
||||
int validkey:1; /* 1 if we currently have a valid key for him */
|
||||
int waitingforkey:1; /* 1 if we already sent out a request */
|
||||
int visited:1; /* 1 if this node has been visited by one of the graph algorithms */
|
||||
int reachable:1; /* 1 if this node is reachable in the graph */
|
||||
int indirect:1; /* 1 if this node is not directly reachable by us */
|
||||
int unused:26;
|
||||
} node_status_t;
|
||||
|
||||
typedef struct node_t {
|
||||
char *name; /* name of this node */
|
||||
long int options; /* options turned on for this node */
|
||||
|
||||
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;
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
const EVP_CIPHER *cipher; /* Cipher type for UDP packets */
|
||||
#endif
|
||||
#ifdef USE_GCRYPT
|
||||
GCRY_CIPHER_HD cipher; /* Cipher type for UDP packets */
|
||||
#endif
|
||||
|
||||
char *key; /* Cipher key and iv */
|
||||
int keylength; /* Cipher key and iv length*/
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
const EVP_MD *digest; /* Digest type for MAC */
|
||||
#endif
|
||||
#ifdef USE_GCRYPT
|
||||
GCRY_MD_HD digest; /* Digest type for MAC */
|
||||
#endif
|
||||
|
||||
int maclength; /* Length of MAC */
|
||||
|
||||
int compression; /* Compressionlevel, 0 = no compression */
|
||||
|
||||
list_t *queue; /* Queue for packets awaiting to be encrypted */
|
||||
|
||||
struct node_t *nexthop; /* nearest node from us to him */
|
||||
struct node_t *via; /* next hop for UDP packets */
|
||||
|
||||
avl_tree_t *subnet_tree; /* Pointer to a tree of subnets belonging to this node */
|
||||
|
||||
avl_tree_t *edge_tree; /* Edges with this node as one of the endpoints */
|
||||
|
||||
struct connection_t *connection; /* Connection associated with this node (if a direct connection exists) */
|
||||
|
||||
unsigned int sent_seqno; /* Sequence number last sent to this node */
|
||||
unsigned int received_seqno; /* Sequence number last received from this node */
|
||||
|
||||
void *data; /* Interface details */
|
||||
} node_t;
|
||||
|
||||
extern struct node_t *myself;
|
||||
extern avl_tree_t *node_tree;
|
||||
extern avl_tree_t *node_udp_tree;
|
||||
|
||||
extern void init_nodes(void);
|
||||
extern void exit_nodes(void);
|
||||
extern node_t *new_node(void);
|
||||
extern void free_node(node_t *);
|
||||
extern void node_add(node_t *);
|
||||
extern void node_del(node_t *);
|
||||
extern node_t *lookup_node(char *);
|
||||
extern node_t *lookup_node_udp(sockaddr_t *);
|
||||
extern void dump_nodes(void);
|
||||
|
||||
#endif /* __TINC_NODE_H__ */
|
||||
399
lib/subnet.c
Normal file
399
lib/subnet.c
Normal file
|
|
@ -0,0 +1,399 @@
|
|||
/*
|
||||
subnet.c -- handle subnet lookups and lists
|
||||
Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
$Id: subnet.c,v 1.1 2002/04/28 12:46:26 zarq Exp $
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <hooks.h>
|
||||
#include <utils.h>
|
||||
#include <xalloc.h>
|
||||
#include <avl_tree.h>
|
||||
|
||||
#include "conf.h"
|
||||
#include "net.h"
|
||||
#include "node.h"
|
||||
#include "subnet.h"
|
||||
#include "netutl.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "system.h"
|
||||
|
||||
/* lists type of subnet */
|
||||
|
||||
avl_tree_t *subnet_tree;
|
||||
|
||||
/* Subnet comparison */
|
||||
|
||||
int subnet_compare_mac(subnet_t *a, subnet_t *b)
|
||||
{
|
||||
cp
|
||||
return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
|
||||
}
|
||||
|
||||
int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
|
||||
{
|
||||
int result;
|
||||
cp
|
||||
result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
return a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
|
||||
}
|
||||
|
||||
int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
|
||||
{
|
||||
int result;
|
||||
cp
|
||||
result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
return a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
|
||||
}
|
||||
|
||||
int subnet_compare(subnet_t *a, subnet_t *b)
|
||||
{
|
||||
int result;
|
||||
cp
|
||||
result = a->type - b->type;
|
||||
|
||||
if(result)
|
||||
return result;
|
||||
|
||||
switch(a->type)
|
||||
{
|
||||
case SUBNET_MAC:
|
||||
return subnet_compare_mac(a, b);
|
||||
case SUBNET_IPV4:
|
||||
return subnet_compare_ipv4(a, b);
|
||||
case SUBNET_IPV6:
|
||||
return subnet_compare_ipv6(a, b);
|
||||
default:
|
||||
syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
|
||||
cp_trace();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Initialising trees */
|
||||
|
||||
void init_subnets(void)
|
||||
{
|
||||
cp
|
||||
subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
|
||||
cp
|
||||
}
|
||||
|
||||
void exit_subnets(void)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(subnet_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
avl_tree_t *new_subnet_tree(void)
|
||||
{
|
||||
cp
|
||||
return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
|
||||
cp
|
||||
}
|
||||
|
||||
void free_subnet_tree(avl_tree_t *subnet_tree)
|
||||
{
|
||||
cp
|
||||
avl_delete_tree(subnet_tree);
|
||||
cp
|
||||
}
|
||||
|
||||
/* Allocating and freeing space for subnets */
|
||||
|
||||
subnet_t *new_subnet(void)
|
||||
{
|
||||
cp
|
||||
return (subnet_t *)xmalloc(sizeof(subnet_t));
|
||||
}
|
||||
|
||||
void free_subnet(subnet_t *subnet)
|
||||
{
|
||||
cp
|
||||
free(subnet);
|
||||
}
|
||||
|
||||
/* Adding and removing subnets */
|
||||
|
||||
void subnet_add(node_t *n, subnet_t *subnet)
|
||||
{
|
||||
cp
|
||||
subnet->owner = n;
|
||||
|
||||
avl_insert(subnet_tree, subnet);
|
||||
cp
|
||||
avl_insert(n->subnet_tree, subnet);
|
||||
|
||||
run_hooks("subnet-add", subnet);
|
||||
cp
|
||||
}
|
||||
|
||||
void subnet_del(node_t *n, subnet_t *subnet)
|
||||
{
|
||||
cp
|
||||
avl_delete(n->subnet_tree, subnet);
|
||||
cp
|
||||
avl_delete(subnet_tree, subnet);
|
||||
|
||||
run_hooks("subnet-del", subnet);
|
||||
cp
|
||||
}
|
||||
|
||||
/* Ascii representation of subnets */
|
||||
|
||||
subnet_t *str2net(char *subnetstr)
|
||||
{
|
||||
int i, l;
|
||||
subnet_t *subnet;
|
||||
unsigned short int x[8];
|
||||
cp
|
||||
subnet = new_subnet();
|
||||
cp
|
||||
if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
|
||||
&x[0], &x[1], &x[2], &x[3],
|
||||
&l) == 5)
|
||||
{
|
||||
subnet->type = SUBNET_IPV4;
|
||||
subnet->net.ipv4.prefixlength = l;
|
||||
for(i = 0; i < 4; i++)
|
||||
subnet->net.ipv4.address.x[i] = x[i];
|
||||
return subnet;
|
||||
}
|
||||
|
||||
if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
|
||||
&x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
|
||||
&l) == 9)
|
||||
{
|
||||
subnet->type = SUBNET_IPV6;
|
||||
subnet->net.ipv6.prefixlength = l;
|
||||
for(i = 0; i < 8; i++)
|
||||
subnet->net.ipv6.address.x[i] = htons(x[i]);
|
||||
return subnet;
|
||||
}
|
||||
|
||||
if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
|
||||
&x[0], &x[1], &x[2], &x[3]) == 4)
|
||||
{
|
||||
subnet->type = SUBNET_IPV4;
|
||||
subnet->net.ipv4.prefixlength = 32;
|
||||
for(i = 0; i < 4; i++)
|
||||
subnet->net.ipv4.address.x[i] = x[i];
|
||||
return subnet;
|
||||
}
|
||||
|
||||
if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
|
||||
&x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
|
||||
{
|
||||
subnet->type = SUBNET_IPV6;
|
||||
subnet->net.ipv6.prefixlength = 128;
|
||||
for(i = 0; i < 8; i++)
|
||||
subnet->net.ipv6.address.x[i] = htons(x[i]);
|
||||
return subnet;
|
||||
}
|
||||
|
||||
if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
|
||||
&x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
|
||||
{
|
||||
subnet->type = SUBNET_MAC;
|
||||
for(i = 0; i < 6; i++)
|
||||
subnet->net.mac.address.x[i] = x[i];
|
||||
return subnet;
|
||||
}
|
||||
|
||||
free(subnet);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *net2str(subnet_t *subnet)
|
||||
{
|
||||
char *netstr;
|
||||
cp
|
||||
switch(subnet->type)
|
||||
{
|
||||
case SUBNET_MAC:
|
||||
asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
|
||||
subnet->net.mac.address.x[0],
|
||||
subnet->net.mac.address.x[1],
|
||||
subnet->net.mac.address.x[2],
|
||||
subnet->net.mac.address.x[3],
|
||||
subnet->net.mac.address.x[4],
|
||||
subnet->net.mac.address.x[5]);
|
||||
break;
|
||||
case SUBNET_IPV4:
|
||||
asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
|
||||
subnet->net.ipv4.address.x[0],
|
||||
subnet->net.ipv4.address.x[1],
|
||||
subnet->net.ipv4.address.x[2],
|
||||
subnet->net.ipv4.address.x[3],
|
||||
subnet->net.ipv4.prefixlength);
|
||||
break;
|
||||
case SUBNET_IPV6:
|
||||
asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
|
||||
ntohs(subnet->net.ipv6.address.x[0]),
|
||||
ntohs(subnet->net.ipv6.address.x[1]),
|
||||
ntohs(subnet->net.ipv6.address.x[2]),
|
||||
ntohs(subnet->net.ipv6.address.x[3]),
|
||||
ntohs(subnet->net.ipv6.address.x[4]),
|
||||
ntohs(subnet->net.ipv6.address.x[5]),
|
||||
ntohs(subnet->net.ipv6.address.x[6]),
|
||||
ntohs(subnet->net.ipv6.address.x[7]),
|
||||
subnet->net.ipv6.prefixlength);
|
||||
break;
|
||||
default:
|
||||
syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exitting!"), subnet->type);
|
||||
cp_trace();
|
||||
exit(0);
|
||||
}
|
||||
cp
|
||||
return netstr;
|
||||
}
|
||||
|
||||
/* Subnet lookup routines */
|
||||
|
||||
subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
|
||||
{
|
||||
cp
|
||||
return avl_search(owner->subnet_tree, subnet);
|
||||
}
|
||||
|
||||
subnet_t *lookup_subnet_mac(mac_t *address)
|
||||
{
|
||||
subnet_t subnet, *p;
|
||||
cp
|
||||
subnet.type = SUBNET_MAC;
|
||||
memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
|
||||
|
||||
p = (subnet_t *)avl_search(subnet_tree, &subnet);
|
||||
cp
|
||||
return p;
|
||||
}
|
||||
|
||||
subnet_t *lookup_subnet_ipv4(ipv4_t *address)
|
||||
{
|
||||
subnet_t subnet, *p;
|
||||
cp
|
||||
subnet.type = SUBNET_IPV4;
|
||||
memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
|
||||
subnet.net.ipv4.prefixlength = 32;
|
||||
|
||||
do
|
||||
{
|
||||
/* Go find subnet */
|
||||
|
||||
p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
|
||||
|
||||
/* Check if the found subnet REALLY matches */
|
||||
cp
|
||||
if(p)
|
||||
{
|
||||
if(p->type != SUBNET_IPV4)
|
||||
{
|
||||
p = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
|
||||
break;
|
||||
else
|
||||
{
|
||||
/* Otherwise, see if there is a bigger enclosing subnet */
|
||||
|
||||
subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
|
||||
maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
|
||||
}
|
||||
}
|
||||
} while (p);
|
||||
cp
|
||||
return p;
|
||||
}
|
||||
|
||||
subnet_t *lookup_subnet_ipv6(ipv6_t *address)
|
||||
{
|
||||
subnet_t subnet, *p;
|
||||
cp
|
||||
subnet.type = SUBNET_IPV6;
|
||||
memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
|
||||
subnet.net.ipv6.prefixlength = 128;
|
||||
|
||||
do
|
||||
{
|
||||
/* Go find subnet */
|
||||
|
||||
p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
|
||||
|
||||
/* Check if the found subnet REALLY matches */
|
||||
|
||||
cp
|
||||
if(p)
|
||||
{
|
||||
if(p->type != SUBNET_IPV6)
|
||||
return NULL;
|
||||
|
||||
if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
|
||||
break;
|
||||
else
|
||||
{
|
||||
/* Otherwise, see if there is a bigger enclosing subnet */
|
||||
|
||||
subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
|
||||
maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
|
||||
}
|
||||
}
|
||||
} while (p);
|
||||
cp
|
||||
return p;
|
||||
}
|
||||
|
||||
void dump_subnets(void)
|
||||
{
|
||||
char *netstr;
|
||||
subnet_t *subnet;
|
||||
avl_node_t *node;
|
||||
cp
|
||||
syslog(LOG_DEBUG, _("Subnet list:"));
|
||||
for(node = subnet_tree->head; node; node = node->next)
|
||||
{
|
||||
subnet = (subnet_t *)node->data;
|
||||
netstr = net2str(subnet);
|
||||
syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
|
||||
free(netstr);
|
||||
}
|
||||
syslog(LOG_DEBUG, _("End of subnet list."));
|
||||
cp
|
||||
}
|
||||
90
lib/subnet.h
Normal file
90
lib/subnet.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
subnet.h -- header for subnet.c
|
||||
Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
|
||||
2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
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 2002/04/28 12:46:26 zarq Exp $
|
||||
*/
|
||||
|
||||
#ifndef __TINC_SUBNET_H__
|
||||
#define __TINC_SUBNET_H__
|
||||
|
||||
#include "net.h"
|
||||
|
||||
enum
|
||||
{
|
||||
SUBNET_MAC = 0,
|
||||
SUBNET_IPV4,
|
||||
SUBNET_IPV6,
|
||||
SUBNET_TYPES /* Guardian */
|
||||
};
|
||||
|
||||
typedef struct subnet_mac_t
|
||||
{
|
||||
mac_t address;
|
||||
time_t lastseen;
|
||||
} subnet_mac_t;
|
||||
|
||||
typedef struct subnet_ipv4_t
|
||||
{
|
||||
ipv4_t address;
|
||||
int prefixlength;
|
||||
} subnet_ipv4_t;
|
||||
|
||||
typedef struct subnet_ipv6_t
|
||||
{
|
||||
ipv6_t address;
|
||||
int prefixlength;
|
||||
} subnet_ipv6_t;
|
||||
|
||||
#include "node.h"
|
||||
|
||||
typedef struct subnet_t {
|
||||
struct node_t *owner; /* the owner of this subnet */
|
||||
struct node_t *uplink; /* the uplink which we should send packets to for this subnet */
|
||||
|
||||
int type; /* subnet type (IPv4? IPv6? MAC? something even weirder?) */
|
||||
|
||||
/* And now for the actual subnet: */
|
||||
|
||||
union net
|
||||
{
|
||||
subnet_mac_t mac;
|
||||
subnet_ipv4_t ipv4;
|
||||
subnet_ipv6_t ipv6;
|
||||
} net;
|
||||
|
||||
void *data; /* Interface details */
|
||||
} subnet_t;
|
||||
|
||||
extern subnet_t *new_subnet(void);
|
||||
extern void free_subnet(subnet_t *);
|
||||
extern void init_subnets(void);
|
||||
extern void exit_subnets(void);
|
||||
extern avl_tree_t *new_subnet_tree(void);
|
||||
extern void free_subnet_tree(avl_tree_t *);
|
||||
extern void subnet_add(struct node_t *, subnet_t *);
|
||||
extern void subnet_del(struct node_t *, subnet_t *);
|
||||
extern char *net2str(subnet_t *);
|
||||
extern subnet_t *str2net(char *);
|
||||
extern subnet_t *lookup_subnet(struct node_t *, subnet_t *);
|
||||
extern subnet_t *lookup_subnet_mac(mac_t *);
|
||||
extern subnet_t *lookup_subnet_ipv4(ipv4_t *);
|
||||
extern subnet_t *lookup_subnet_ipv6(ipv6_t *);
|
||||
extern void dump_subnets(void);
|
||||
|
||||
#endif /* __TINC_SUBNET_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue