From ec0c16b9b63f361b11a757ee1641d562e4811f93 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 10 Oct 2001 08:49:47 +0000 Subject: [PATCH] Further implementation of doc/CONNECTIVITY. connection.[ch] is now split into a node, vertex and connection part. --- src/connection.h | 68 +++++++--------------- src/node.c | 109 ++++++++++++++++++++++++++++++++++++ src/node.h | 12 +++- src/vertex.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++ src/vertex.h | 33 ++++++----- 5 files changed, 304 insertions(+), 61 deletions(-) create mode 100644 src/node.c create mode 100644 src/vertex.c diff --git a/src/connection.h b/src/connection.h index a75769b..c60562c 100644 --- a/src/connection.h +++ b/src/connection.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: connection.h,v 1.1.2.13 2001/10/08 11:47:55 guus Exp $ + $Id: connection.h,v 1.1.2.14 2001/10/10 08:49:47 guus Exp $ */ #ifndef __TINC_CONNECTION_H__ @@ -43,6 +43,9 @@ #include "net.h" #include "conf.h" +#include "node.h" +#include "vertex.h" + typedef struct status_bits_t { int pinged:1; /* sent ping */ int meta:1; /* meta connection exists */ @@ -63,66 +66,37 @@ typedef struct status_bits_t { #define OPTION_TCPONLY 0x0002 typedef struct connection_t { - char *name; /* name of this connection */ ipv4_t address; /* his real (internet) ip */ - short unsigned int meta_port; /* port number of meta connection */ + short unsigned int port; /* port number of meta connection */ char *hostname; /* the hostname of its real ip */ int protocol_version; /* used protocol */ - short unsigned int port; /* port number for UDP traffic */ - long int options; /* options turned on for this connection */ - int socket; /* our udp vpn socket */ - int meta_socket; /* our tcp meta socket */ + int socket; /* socket used for this connection */ status_bits_t status; /* status info */ - RSA *rsa_key; /* the public/private key */ - EVP_CIPHER_CTX *cipher_inctx; /* Context of encrypted meta data that will come from him to us */ - EVP_CIPHER_CTX *cipher_outctx; /* Context of encrypted meta data that will be sent from us to him */ - char *cipher_inkey; /* His symmetric meta key */ - char *cipher_outkey; /* Our symmetric meta key */ - EVP_CIPHER *cipher_pkttype; /* Cipher type for encrypted vpn packets */ - char *cipher_pktkey; /* Cipher key and iv */ - int cipher_pktkeylength; /* Cipher key and iv length*/ + struct node_t *node; /* node associated with the other end */ + struct vertex_t *vertex; /* vertex associated with this connection */ + + RSA *rsa_key; /* his public/private key */ + EVP_CIPHER *incipher; /* Cipher he will use to send data to us */ + 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 */ + 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 */ + char *mychallenge; /* challenge we received from him */ + char *hischallenge; /* challenge we sent to him */ char *buffer; /* 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 */ - - list_t *queue; /* Queue for packets awaiting to be encrypted */ - - char *mychallenge; /* challenge we received from him */ - char *hischallenge; /* challenge we sent to him */ - - struct connection_t *nexthop; /* nearest meta-hop from us to him */ - struct connection_t *prevhop; /* nearest meta-hop from him to us */ - struct connection_t *via; /* next hop for UDP packets */ - - avl_tree_t *subnet_tree; /* Pointer to a tree of subnets belonging to this connection */ - - struct config_t *config; /* Pointer to configuration tree belonging to this host */ + time_t last_ping_time; /* last time we saw some activity from the other end */ } connection_t; extern avl_tree_t *connection_tree; -extern avl_tree_t *active_tree; -extern connection_t *myself; - -extern void init_connections(void); -extern connection_t *new_connection(void); -extern void free_connection(connection_t *); -extern void id_add(connection_t *); -extern void active_add(connection_t *); -extern void active_del(connection_t *); -extern void connection_add(connection_t *); -extern void connection_del(connection_t *); -extern void prune_add(connection_t *); -extern void prune_flush(void); -extern connection_t *lookup_id(char *); -extern connection_t *lookup_active(ipv4_t, short unsigned int); -extern void dump_connection_list(void); -extern int read_host_config(connection_t *); -extern void destroy_trees(void); #endif /* __TINC_CONNECTION_H__ */ diff --git a/src/node.c b/src/node.c new file mode 100644 index 0000000..d255362 --- /dev/null +++ b/src/node.c @@ -0,0 +1,109 @@ +/* + node.c -- node tree management + Copyright (C) 2001 Guus Sliepen , + 2001 Ivo Timmermans + + 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.2.1 2001/10/10 08:49:47 guus Exp $ +*/ + +avl_tree_t *node_tree; /* Known nodes, sorted by name */ + +int node_compare(connection_t *a, connection_t *b) +{ + return strcmp(a->name, b->name); +} + +void init_nodes(void) +{ +cp + node_tree = avl_alloc_tree((avl_compare_t)node_compare, NULL); +cp +} + +void exit_nodes(void) +{ +cp + avl_delete_tree(node_tree); +cp +} + +node_t *new_node(void) +{ + node_t *n = (node_t *)xmalloc_and_zero(sizeof(*n)); +cp + n->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL); + 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->config) + clear_config(&n->config); + free(n); +cp +} + +node_t *lookup_node(char *name) +{ + node_t n; +cp + n.name = name; + return avl_search(node_tree, &n); +} + + +int read_host_config(nodet *n) +{ + char *fname; + int x; +cp + asprintf(&fname, "%s/hosts/%s", confbase, n->name); + x = read_config_file(&n->config, fname); + free(fname); +cp + return x; +} + +void dump_nodes(void) +{ + avl_node_t *node; + node_t *n; +cp + syslog(LOG_DEBUG, _("Nodes:")); + + for(node = node_tree->head; node; node = node->next) + { + n = (connection_t *)node->data; + syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"), + n->name, n->hostname, n->port, n->options, + n->socket, n->meta_socket, n->status); + } + + syslog(LOG_DEBUG, _("End of nodes.")); +cp +} diff --git a/src/node.h b/src/node.h index f96c56e..e6b28d2 100644 --- a/src/node.h +++ b/src/node.h @@ -17,9 +17,14 @@ 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.2 2001/10/09 19:37:10 guus Exp $ + $Id: node.h,v 1.1.2.3 2001/10/10 08:49:47 guus Exp $ */ +#ifndef __TINC_NODE_H__ +#define __TINC_NODE_H__ + +#include + typedef struct node_t { char *name; /* name of this connection */ int protocol_version; /* used protocol */ @@ -45,3 +50,8 @@ typedef struct node_t { struct config_t *config; /* Pointer to configuration tree belonging to this node */ } node_t; + +struct node_t *myself; +extern avl_tree_t *node_tree; + +#endif /* __TINC_NODE_H__ */ diff --git a/src/vertex.c b/src/vertex.c new file mode 100644 index 0000000..54da872 --- /dev/null +++ b/src/vertex.c @@ -0,0 +1,143 @@ +/* + vertex.c -- vertex tree management + Copyright (C) 2000,2001 Guus Sliepen , + 2000,2001 Ivo Timmermans + + 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: vertex.c,v 1.1.2.1 2001/10/10 08:49:47 guus Exp $ +*/ + +#include "config.h" + +#include +#include +#include + +#include +#include + +#include "net.h" /* Don't ask. */ +#include "netutl.h" +#include "config.h" +#include "conf.h" +#include +#include "subnet.h" + +#include "xalloc.h" +#include "system.h" + +avl_tree_t *vertex_tree; /* Tree with all known vertices (replaces active_tree) */ +avl_tree_t *connection_tree; /* Tree with all meta connections with ourself */ + +int connection_compare(connection_t *a, connection_t *b) +{ + return a->meta_socket - b->meta_socket; +} + +int vertex_compare(vertex_t *a, vertex_t *b) +{ + int result; + + result = strcmp(a->from->name, b->from->name); + + if(result) + return result; + else + return strcmp(a->to->name, b->to->name); +} + +/* Evil vertex_compare() from a parallel universe ;) + +int vertex_compare(vertex_t *a, vertex_t *b) +{ + int result; + + return (result = strcmp(a->from->name, b->from->name)) || (result = strcmp(a->to->name, b->to->name)), result; +} + +*/ + +void init_vertices(void) +{ +cp + vertex_tree = avl_alloc_tree((avl_compare_t)vertex_compare, NULL); +cp +} + +void exit_vertices(void) +{ +cp + avl_delete_tree(vertex_tree); +cp +} + +/* Creation and deletion of connection elements */ + +vertex_t *new_vertex(void) +{ +cp + vertex_t *v = (vertex_t *)xmalloc_and_zero(sizeof(*v)); +cp + return v; +} + +void free_vertex(vertex_t *v) +{ +cp + if(v->from.hostname) + free(v->from.hostname) + if(v->to.hostname) + free(v->to.hostname) + + free(v); +cp +} + +vertex_t *lookup_vertex(node_t *from, node_t *to) +{ + vertex_t v, *result; +cp + v.from.node = from; + v.to.node = to; + + result = avl_search(vertex_tree, &v); + + if(result) + return result; +cp + v.from.node = to; + v.to.node = from; + + return avl_search(vertex_tree, &v); +} + +void dump_vertices(void) +{ + avl_node_t *node; + vertex_t *v; +cp + syslog(LOG_DEBUG, _("Vertices:")); + + for(node = vertex_tree->head; node; node = node->next) + { + v = (vertex_t *)node->data; + syslog(LOG_DEBUG, _(" %s - %s options %ld"), + v->from.node->name, v->to.node->name, v->options); + } + + syslog(LOG_DEBUG, _("End of vertices.")); +cp +} diff --git a/src/vertex.h b/src/vertex.h index 7425756..9490f48 100644 --- a/src/vertex.h +++ b/src/vertex.h @@ -17,28 +17,35 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: vertex.h,v 1.1.2.2 2001/10/09 19:37:10 guus Exp $ + $Id: vertex.h,v 1.1.2.3 2001/10/10 08:49:47 guus Exp $ */ +#ifndef __TINC_VERTEX_H__ +#define __TINC_VERTEX_H__ + +#include + +#include "node.h" +#include "connection.h" + typedef struct vertex_t { - struct halfconnection_t *from; - struct halfconnection_t *to; + struct halfconnection_t from; + struct halfconnection_t to; + long int options; /* options turned on for this connection */ + int metric; /* weight of this vertex */ + + struct connection_t *connection; /* connection associated with this vertex, if available */ } vertex_t; typedef struct halfconnection_t { - struct node_t *node; + struct node_t *node; /* node associated with this end of the connection */ ipv4_t address; /* real (internet) ip on this end of the meta connection */ short unsigned int port; /* port number of this end of the meta connection */ char *hostname; /* the hostname of real ip */ - - /* Following bits only used when this is a connection with ourself. */ - - RSA *rsa_key; /* RSA key used for authentication */ - EVP_CIPHER *cipher; /* Cipher type for meta protocol */ - EVP_CIPHER_CTX *ctx; /* Cipher state for meta protocol */ - char *key; /* Cipher key + iv */ - int keylength; /* Cipher keylength */ - char *challenge; /* Challenge sent to this end */ } halfconnection_t; + +extern avl_tree_t *vertex_tree; /* Tree with all known vertices (replaces active_tree) */ + +#endif /* __TINC_VERTEX_H__ */ -- 2.25.1