2000-03-26 00:33:07 +00:00
|
|
|
/*
|
|
|
|
protocol.c -- handle the meta-protocol
|
2001-01-07 17:09:07 +00:00
|
|
|
Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
|
|
|
|
2000,2001 Guus Sliepen <guus@sliepen.warande.net>
|
2000-03-26 00:33:07 +00:00
|
|
|
|
|
|
|
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.
|
2000-05-29 21:01:26 +00:00
|
|
|
|
2001-11-04 23:48:27 +00:00
|
|
|
$Id: protocol.c,v 1.28.4.120 2001/11/04 23:48:27 guus Exp $
|
2000-03-26 00:33:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2000-05-04 23:17:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-03-26 00:33:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/socket.h>
|
2000-04-26 17:42:55 +00:00
|
|
|
#include <unistd.h>
|
2000-05-01 18:07:12 +00:00
|
|
|
#include <stdio.h>
|
2000-11-15 13:33:27 +00:00
|
|
|
#include <stdarg.h>
|
2001-02-25 11:09:29 +00:00
|
|
|
#include <errno.h>
|
2000-03-26 00:33:07 +00:00
|
|
|
|
|
|
|
#include <utils.h>
|
|
|
|
#include <xalloc.h>
|
2001-01-05 23:53:53 +00:00
|
|
|
#include <avl_tree.h>
|
2001-01-07 15:25:49 +00:00
|
|
|
#include <list.h>
|
2000-03-26 00:33:07 +00:00
|
|
|
|
2000-08-07 16:27:29 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2001-07-04 08:41:36 +00:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/evp.h>
|
2000-11-15 01:06:13 +00:00
|
|
|
|
2001-07-04 08:41:36 +00:00
|
|
|
#ifndef HAVE_RAND_PSEUDO_BYTES
|
|
|
|
#define RAND_pseudo_bytes RAND_bytes
|
2000-11-15 01:06:13 +00:00
|
|
|
#endif
|
|
|
|
|
2000-03-26 00:33:07 +00:00
|
|
|
#include "conf.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "netutl.h"
|
|
|
|
#include "protocol.h"
|
2000-09-26 14:06:11 +00:00
|
|
|
#include "meta.h"
|
2000-11-20 19:12:17 +00:00
|
|
|
#include "connection.h"
|
2001-10-27 12:13:17 +00:00
|
|
|
#include "node.h"
|
2001-10-28 08:41:19 +00:00
|
|
|
#include "edge.h"
|
2001-10-30 12:59:12 +00:00
|
|
|
#include "graph.h"
|
2000-03-26 00:33:07 +00:00
|
|
|
|
2000-05-29 21:01:26 +00:00
|
|
|
#include "system.h"
|
|
|
|
|
2001-05-24 21:52:26 +00:00
|
|
|
int mykeyused = 0;
|
|
|
|
|
2000-09-15 12:58:40 +00:00
|
|
|
int check_id(char *id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < strlen(id); i++)
|
2000-10-15 00:59:37 +00:00
|
|
|
if(!isalnum(id[i]) && id[i] != '_')
|
|
|
|
return -1;
|
2000-12-05 08:59:30 +00:00
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
return 0;
|
2000-09-15 12:58:40 +00:00
|
|
|
}
|
|
|
|
|
2000-12-05 08:59:30 +00:00
|
|
|
/* Generic request routines - takes care of logging and error
|
|
|
|
detection as well */
|
2000-05-01 18:07:12 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_request(connection_t *c, const char *format, ...)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2000-09-10 15:18:03 +00:00
|
|
|
va_list args;
|
2000-10-15 00:59:37 +00:00
|
|
|
char buffer[MAXBUFSIZE];
|
|
|
|
int len, request;
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2000-12-05 08:59:30 +00:00
|
|
|
/* Use vsnprintf instead of vasprintf: faster, no memory
|
|
|
|
fragmentation, cleanup is automatic, and there is a limit on the
|
|
|
|
input buffer anyway */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
va_start(args, format);
|
|
|
|
len = vsnprintf(buffer, MAXBUFSIZE, format, args);
|
|
|
|
request = va_arg(args, int);
|
2000-09-10 15:18:03 +00:00
|
|
|
va_end(args);
|
2000-03-26 00:33:07 +00:00
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
if(len < 0 || len > MAXBUFSIZE-1)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-09-22 16:20:07 +00:00
|
|
|
if(debug_lvl >= DEBUG_PROTOCOL)
|
2001-05-25 11:54:28 +00:00
|
|
|
{
|
|
|
|
if(debug_lvl >= DEBUG_META)
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
|
2001-05-25 11:54:28 +00:00
|
|
|
else
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
|
2001-05-25 11:54:28 +00:00
|
|
|
}
|
2000-10-15 00:59:37 +00:00
|
|
|
|
2001-05-25 11:54:28 +00:00
|
|
|
buffer[len++] = '\n';
|
2000-09-22 16:20:07 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_meta(c, buffer, len);
|
2000-09-22 16:20:07 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int receive_request(connection_t *c)
|
2000-09-22 16:20:07 +00:00
|
|
|
{
|
2000-09-26 14:06:11 +00:00
|
|
|
int request;
|
2001-05-25 11:54:28 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%d", &request) == 1)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-03-04 13:59:32 +00:00
|
|
|
if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
|
2000-09-26 14:06:11 +00:00
|
|
|
{
|
2001-05-25 11:54:28 +00:00
|
|
|
if(debug_lvl >= DEBUG_META)
|
|
|
|
syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, c->buffer);
|
2001-05-25 11:54:28 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_ERR, _("Unknown request from %s (%s)"),
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2001-05-25 11:54:28 +00:00
|
|
|
|
2000-09-26 14:06:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-21 11:52:08 +00:00
|
|
|
if(debug_lvl >= DEBUG_PROTOCOL)
|
2001-05-25 11:54:28 +00:00
|
|
|
{
|
|
|
|
if(debug_lvl >= DEBUG_META)
|
|
|
|
syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
|
2001-10-27 12:13:17 +00:00
|
|
|
request_name[request], c->name, c->hostname, c->buffer);
|
2001-05-25 11:54:28 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
|
2001-10-27 12:13:17 +00:00
|
|
|
request_name[request], c->name, c->hostname);
|
2001-05-25 11:54:28 +00:00
|
|
|
}
|
2000-09-26 14:06:11 +00:00
|
|
|
}
|
2000-10-29 00:02:20 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if((c->allow_request != ALL) && (c->allow_request != request))
|
2000-10-29 01:08:09 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
|
2000-10-29 01:08:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(request_handlers[request](c))
|
2000-09-26 14:06:11 +00:00
|
|
|
/* Something went wrong. Probably scriptkiddies. Terminate. */
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
|
2001-10-27 12:13:17 +00:00
|
|
|
request_name[request], c->name, c->hostname);
|
2000-09-26 14:06:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
2000-09-26 14:06:11 +00:00
|
|
|
else
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2000-09-26 14:06:11 +00:00
|
|
|
syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-03-26 00:33:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-10-29 00:02:20 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-05-25 11:54:28 +00:00
|
|
|
/* The authentication protocol is described in detail in doc/SECURITY2,
|
|
|
|
the rest will be described in doc/PROTOCOL. */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_id(connection_t *c)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int id_h(connection_t *c)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2000-11-22 19:55:53 +00:00
|
|
|
char name[MAX_STRING_SIZE];
|
2001-10-28 08:41:19 +00:00
|
|
|
int bla;
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Check if identity is a valid name */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-11-22 19:55:53 +00:00
|
|
|
if(check_id(name))
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-10-27 12:13:17 +00:00
|
|
|
|
|
|
|
/* If we set c->name in advance, make sure we are connected to the right host */
|
2000-11-22 19:55:53 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(c->name)
|
|
|
|
{
|
|
|
|
if(strcmp(c->name, name))
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c->name = xstrdup(name);
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check if version matches */
|
2000-11-22 18:54:08 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(c->protocol_version != myself->connection->protocol_version)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
|
|
|
|
c->name, c->hostname, c->protocol_version);
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-10-27 12:13:17 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
if(bypass_security)
|
|
|
|
{
|
|
|
|
if(!c->config_tree)
|
|
|
|
init_configuration(&c->config_tree);
|
|
|
|
c->allow_request = ACK;
|
|
|
|
return send_ack(c);
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!c->config_tree)
|
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
init_configuration(&c->config_tree);
|
|
|
|
|
|
|
|
if((bla = read_connection_config(c)))
|
2001-10-27 12:13:17 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-10-31 20:02:06 +00:00
|
|
|
}
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-31 20:02:06 +00:00
|
|
|
if(read_rsa_public_key(c))
|
|
|
|
{
|
|
|
|
return -1;
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
2000-12-22 21:34:24 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->allow_request = METAKEY;
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_metakey(c);
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_metakey(connection_t *c)
|
2001-02-27 16:37:31 +00:00
|
|
|
{
|
2001-11-03 21:22:02 +00:00
|
|
|
char buffer[MAX_STRING_SIZE];
|
2001-10-27 12:13:17 +00:00
|
|
|
int len, x;
|
2001-02-27 16:37:31 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
len = RSA_size(c->rsa_key);
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Allocate buffers for the meta key */
|
2001-10-08 15:37:14 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!c->outkey)
|
|
|
|
c->outkey = xmalloc(len);
|
2001-07-15 18:07:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!c->outctx)
|
|
|
|
c->outctx = xmalloc(sizeof(*c->outctx));
|
|
|
|
cp
|
|
|
|
/* Copy random data to the buffer */
|
|
|
|
|
|
|
|
RAND_bytes(c->outkey, len);
|
|
|
|
|
|
|
|
/* The message we send must be smaller than the modulus of the RSA key.
|
|
|
|
By definition, for a key of k bits, the following formula holds:
|
|
|
|
|
|
|
|
2^(k-1) <= modulus < 2^(k)
|
|
|
|
|
|
|
|
Where ^ means "to the power of", not "xor".
|
|
|
|
This means that to be sure, we must choose our message < 2^(k-1).
|
|
|
|
This can be done by setting the most significant bit to zero.
|
|
|
|
*/
|
2001-07-15 18:07:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->outkey[0] &= 0x7F;
|
|
|
|
|
|
|
|
if(debug_lvl >= DEBUG_SCARY_THINGS)
|
|
|
|
{
|
|
|
|
bin2hex(c->outkey, buffer, len);
|
|
|
|
buffer[len*2] = '\0';
|
|
|
|
syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
|
|
|
|
}
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Encrypt the random data
|
|
|
|
|
|
|
|
We do not use one of the PKCS padding schemes here.
|
|
|
|
This is allowed, because we encrypt a totally random string
|
|
|
|
with a length equal to that of the modulus of the RSA key.
|
|
|
|
*/
|
2001-11-03 21:22:02 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp
|
|
|
|
/* Convert the encrypted random data to a hexadecimal formatted string */
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
bin2hex(buffer, buffer, len);
|
|
|
|
buffer[len*2] = '\0';
|
2001-07-15 18:07:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Send the meta key */
|
|
|
|
|
|
|
|
x = send_request(c, "%d %s", METAKEY, buffer);
|
|
|
|
|
|
|
|
/* Further outgoing requests are encrypted with the key we just generated */
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
|
|
|
|
c->outkey + len - EVP_bf_cfb()->key_len,
|
|
|
|
c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
|
|
|
|
|
|
|
|
c->status.encryptout = 1;
|
2001-02-27 16:37:31 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int metakey_h(connection_t *c)
|
|
|
|
{
|
|
|
|
char buffer[MAX_STRING_SIZE];
|
|
|
|
int len;
|
|
|
|
cp
|
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
|
2001-02-27 16:37:31 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
|
2001-10-27 12:13:17 +00:00
|
|
|
return -1;
|
2001-02-27 16:37:31 +00:00
|
|
|
}
|
2001-11-03 21:22:02 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
len = RSA_size(myself->connection->rsa_key);
|
|
|
|
|
|
|
|
/* Check if the length of the meta key is all right */
|
|
|
|
|
|
|
|
if(strlen(buffer) != len*2)
|
2001-02-27 16:37:31 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
|
2001-10-27 12:13:17 +00:00
|
|
|
return -1;
|
2001-02-27 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Allocate buffers for the meta key */
|
2001-11-03 21:22:02 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!c->inkey)
|
|
|
|
c->inkey = xmalloc(len);
|
|
|
|
|
|
|
|
if(!c->inctx)
|
|
|
|
c->inctx = xmalloc(sizeof(*c->inctx));
|
|
|
|
|
|
|
|
/* Convert the challenge from hexadecimal back to binary */
|
2001-11-03 21:22:02 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
hex2bin(buffer,buffer,len);
|
|
|
|
|
|
|
|
/* Decrypt the meta key */
|
2001-11-03 21:22:02 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
|
2001-02-27 16:37:31 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
|
|
|
|
return -1;
|
2001-02-27 16:37:31 +00:00
|
|
|
}
|
2001-03-13 21:33:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(debug_lvl >= DEBUG_SCARY_THINGS)
|
2001-02-27 16:37:31 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
bin2hex(c->inkey, buffer, len);
|
|
|
|
buffer[len*2] = '\0';
|
|
|
|
syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
|
|
|
|
}
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* All incoming requests will now be encrypted. */
|
2001-11-03 21:22:02 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
|
|
|
|
c->inkey + len - EVP_bf_cfb()->key_len,
|
|
|
|
c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
|
|
|
|
|
|
|
|
c->status.decryptin = 1;
|
2001-02-27 16:37:31 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->allow_request = CHALLENGE;
|
2001-02-27 16:37:31 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_challenge(c);
|
2001-02-27 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_challenge(connection_t *c)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-11-03 21:22:02 +00:00
|
|
|
char buffer[MAX_STRING_SIZE];
|
2000-10-20 15:34:38 +00:00
|
|
|
int len, x;
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-02-25 19:09:45 +00:00
|
|
|
/* CHECKME: what is most reasonable value for len? */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
len = RSA_size(c->rsa_key);
|
2000-10-20 15:34:38 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Allocate buffers for the challenge */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
if(!c->hischallenge)
|
|
|
|
c->hischallenge = xmalloc(len);
|
2000-10-15 00:59:37 +00:00
|
|
|
cp
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Copy random data to the buffer */
|
2000-09-14 11:54:51 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
RAND_bytes(c->hischallenge, len);
|
2000-10-20 15:34:38 +00:00
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
cp
|
2001-02-25 19:09:45 +00:00
|
|
|
/* Convert to hex */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
bin2hex(c->hischallenge, buffer, len);
|
2000-10-20 15:34:38 +00:00
|
|
|
buffer[len*2] = '\0';
|
2001-02-25 19:09:45 +00:00
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
cp
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Send the challenge */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
x = send_request(c, "%d %s", CHALLENGE, buffer);
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2000-10-20 15:34:38 +00:00
|
|
|
return x;
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int challenge_h(connection_t *c)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2000-11-22 19:55:53 +00:00
|
|
|
char buffer[MAX_STRING_SIZE];
|
2000-10-20 15:34:38 +00:00
|
|
|
int len;
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
len = RSA_size(myself->connection->rsa_key);
|
2000-10-20 15:34:38 +00:00
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Check if the length of the challenge is all right */
|
|
|
|
|
2000-10-20 15:34:38 +00:00
|
|
|
if(strlen(buffer) != len*2)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Allocate buffers for the challenge */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!c->mychallenge)
|
|
|
|
c->mychallenge = xmalloc(len);
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Convert the challenge from hexadecimal back to binary */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
hex2bin(buffer,c->mychallenge,len);
|
2000-10-20 15:34:38 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->allow_request = CHAL_REPLY;
|
2000-10-21 11:52:08 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Rest is done by send_chal_reply() */
|
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_chal_reply(c);
|
2000-09-22 15:06:28 +00:00
|
|
|
}
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_chal_reply(connection_t *c)
|
2000-09-22 15:06:28 +00:00
|
|
|
{
|
|
|
|
char hash[SHA_DIGEST_LENGTH*2+1];
|
|
|
|
cp
|
|
|
|
/* Calculate the hash from the challenge we received */
|
2000-09-14 11:54:51 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
|
2000-09-10 15:18:03 +00:00
|
|
|
|
|
|
|
/* Convert the hash to a hexadecimal formatted string */
|
|
|
|
|
2000-09-14 21:51:21 +00:00
|
|
|
bin2hex(hash,hash,SHA_DIGEST_LENGTH);
|
|
|
|
hash[SHA_DIGEST_LENGTH*2] = '\0';
|
2000-09-10 15:18:03 +00:00
|
|
|
|
|
|
|
/* Send the reply */
|
|
|
|
|
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s", CHAL_REPLY, hash);
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int chal_reply_h(connection_t *c)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2000-11-22 19:55:53 +00:00
|
|
|
char hishash[MAX_STRING_SIZE];
|
2000-09-22 15:06:28 +00:00
|
|
|
char myhash[SHA_DIGEST_LENGTH];
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the length of the hash is all right */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Convert the hash to binary format */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
|
|
|
|
|
|
|
|
/* Calculate the hash from the challenge we sent */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Verify the incoming hash with the calculated hash */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-10-16 16:33:30 +00:00
|
|
|
if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
|
2000-10-21 11:52:08 +00:00
|
|
|
if(debug_lvl >= DEBUG_SCARY_THINGS)
|
|
|
|
{
|
|
|
|
bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
|
|
|
|
hishash[SHA_DIGEST_LENGTH*2] = '\0';
|
|
|
|
syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
|
|
|
|
}
|
2000-09-10 15:18:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Identity has now been positively verified.
|
2001-10-27 12:13:17 +00:00
|
|
|
Send an acknowledgement with the rest of the information needed.
|
2000-09-10 15:18:03 +00:00
|
|
|
*/
|
2001-10-27 12:13:17 +00:00
|
|
|
|
|
|
|
c->allow_request = ACK;
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_ack(c);
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_ack(connection_t *c)
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
/* ACK message contains rest of the information the other end needs
|
2001-10-28 08:41:19 +00:00
|
|
|
to create node_t and edge_t structures. */
|
2001-10-28 22:42:49 +00:00
|
|
|
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
/* Estimate weight */
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
|
2000-10-29 22:55:15 +00:00
|
|
|
cp
|
2001-10-28 22:42:49 +00:00
|
|
|
return send_request(c, "%d %hd %d", ACK, myself->port, c->estimated_weight);
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int ack_h(connection_t *c)
|
|
|
|
{
|
|
|
|
port_t port;
|
2001-10-28 22:42:49 +00:00
|
|
|
int weight;
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *n;
|
|
|
|
subnet_t *s;
|
2001-10-30 12:59:12 +00:00
|
|
|
edge_t *e;
|
|
|
|
connection_t *other;
|
2001-10-27 12:13:17 +00:00
|
|
|
avl_node_t *node, *node2;
|
2000-10-29 22:55:15 +00:00
|
|
|
cp
|
2001-10-28 22:42:49 +00:00
|
|
|
if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
|
2001-10-27 12:13:17 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
|
2001-10-27 12:13:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check if we already have a node_t for him */
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n = lookup_node(c->name);
|
2001-03-02 11:25:56 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!n)
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
n = new_node();
|
|
|
|
n->name = xstrdup(c->name);
|
2001-10-30 12:59:12 +00:00
|
|
|
n->address = c->address;
|
2001-10-27 12:13:17 +00:00
|
|
|
n->hostname = xstrdup(c->hostname);
|
|
|
|
n->port = port;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
|
|
|
|
|
|
|
|
node_add(n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(n->connection)
|
|
|
|
{
|
|
|
|
/* Oh dear, we already have a connection to this node. */
|
|
|
|
syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
|
|
|
|
terminate_connection(n->connection, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: check if information in existing node matches that of the other end of this connection */
|
|
|
|
}
|
2001-03-02 11:25:56 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n->connection = c;
|
|
|
|
c->node = n;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check some options
|
|
|
|
|
|
|
|
if((cfg = get_config_val(c->config, config_indirectdata)))
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
if(cfg->data.val == stupid_true)
|
|
|
|
c->options |= OPTION_INDIRECT;
|
2000-10-29 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if((cfg = get_config_val(c->config, config_tcponly)))
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
if(cfg->data.val == stupid_true)
|
|
|
|
c->options |= OPTION_TCPONLY;
|
2000-10-29 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if((myself->options | c->options) & OPTION_INDIRECT)
|
|
|
|
c->via = myself;
|
|
|
|
else
|
|
|
|
c->via = c;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
*/
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-30 16:34:32 +00:00
|
|
|
/* Create an edge_t for this connection */
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
c->edge = new_edge();
|
2001-10-27 12:13:17 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
c->edge->from = myself;
|
|
|
|
c->edge->to = n;
|
2001-10-28 22:42:49 +00:00
|
|
|
c->edge->weight = (weight + c->estimated_weight) / 2;
|
2001-10-28 08:41:19 +00:00
|
|
|
c->edge->connection = c;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
edge_add(c->edge);
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Activate this connection */
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->allow_request = ALL;
|
2001-10-28 08:41:19 +00:00
|
|
|
c->status.active = 1;
|
2001-10-30 12:59:12 +00:00
|
|
|
c->node->cipher = EVP_bf_cbc();
|
|
|
|
c->node->keylength = c->node->cipher->key_len + c->node->cipher->iv_len;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(debug_lvl >= DEBUG_CONNECTIONS)
|
|
|
|
syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
cp
|
|
|
|
/* Send him our subnets */
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
for(node = myself->subnet_tree->head; node; node = node->next)
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
s = (subnet_t *)node->data;
|
|
|
|
send_add_subnet(c, s);
|
2000-10-29 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* And send him all known nodes and their subnets */
|
|
|
|
|
|
|
|
for(node = node_tree->head; node; node = node->next)
|
2000-10-29 22:55:15 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
n = (node_t *)node->data;
|
2000-10-29 22:55:15 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
if(n == c->node || n == myself)
|
|
|
|
continue;
|
2001-02-25 19:09:45 +00:00
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
send_add_node(c, n);
|
|
|
|
|
|
|
|
for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
|
|
|
|
{
|
|
|
|
s = (subnet_t *)node2->data;
|
|
|
|
send_add_subnet(c, s);
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-30 12:59:12 +00:00
|
|
|
|
|
|
|
/* Send all known edges */
|
|
|
|
|
|
|
|
for(node = edge_tree->head; node; node = node->next)
|
|
|
|
{
|
|
|
|
e = (edge_t *)node->data;
|
|
|
|
|
|
|
|
if(e == c->edge)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
send_add_edge(c, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify others of this connection */
|
|
|
|
|
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
|
|
|
{
|
|
|
|
other = (connection_t *)node->data;
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
if(other->status.active && other != c)
|
|
|
|
{
|
|
|
|
send_add_node(other, c->node);
|
|
|
|
send_add_edge(other, c->edge);
|
|
|
|
}
|
2001-10-30 12:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run MST and SSSP algorithms */
|
|
|
|
|
|
|
|
mst_kruskal();
|
2001-10-31 12:50:24 +00:00
|
|
|
sssp_bfs(0);
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return 0;
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
|
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Address and subnet information exchange */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_add_subnet(connection_t *c, subnet_t *subnet)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-22 15:06:28 +00:00
|
|
|
int x;
|
|
|
|
char *netstr;
|
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
x = send_request(c, "%d %s %s", ADD_SUBNET,
|
|
|
|
subnet->owner->name, netstr = net2str(subnet));
|
2000-09-22 15:06:28 +00:00
|
|
|
free(netstr);
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2000-09-22 15:06:28 +00:00
|
|
|
return x;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int add_subnet_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-11-22 19:55:53 +00:00
|
|
|
char subnetstr[MAX_STRING_SIZE];
|
|
|
|
char name[MAX_STRING_SIZE];
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *owner;
|
|
|
|
connection_t *other;
|
|
|
|
subnet_t *s;
|
2001-01-05 23:53:53 +00:00
|
|
|
avl_node_t *node;
|
2000-09-17 21:42:05 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
|
2000-09-22 15:06:28 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if owner name is a valid */
|
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
if(check_id(name))
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
|
2000-09-17 21:42:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if subnet string is valid */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!(s = str2net(subnetstr)))
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
|
2000-09-17 21:42:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-22 15:06:28 +00:00
|
|
|
|
2000-09-17 21:42:05 +00:00
|
|
|
/* Check if the owner of the new subnet is in the connection list */
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
owner = lookup_node(name);
|
|
|
|
|
2001-11-04 23:48:27 +00:00
|
|
|
if(!owner)
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
|
2001-11-03 21:22:02 +00:00
|
|
|
c->name, c->hostname, name);
|
2000-09-22 15:06:28 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
/* Check if we already know this subnet */
|
|
|
|
|
|
|
|
if(lookup_subnet(owner, s))
|
|
|
|
{
|
|
|
|
free_subnet(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* If everything is correct, add the subnet to the list of the owner */
|
2000-10-11 22:01:02 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
subnet_add(owner, s);
|
2000-10-29 00:02:20 +00:00
|
|
|
|
|
|
|
/* Tell the rest */
|
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2000-11-20 19:12:17 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other != c)
|
|
|
|
send_add_subnet(other, s);
|
2000-11-20 19:12:17 +00:00
|
|
|
}
|
2000-09-22 15:06:28 +00:00
|
|
|
cp
|
2000-10-11 22:01:02 +00:00
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_del_subnet(connection_t *c, subnet_t *s)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-10-28 21:05:20 +00:00
|
|
|
int x;
|
|
|
|
char *netstr;
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
|
2000-10-28 21:05:20 +00:00
|
|
|
free(netstr);
|
|
|
|
cp
|
|
|
|
return x;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int del_subnet_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-11-22 19:55:53 +00:00
|
|
|
char subnetstr[MAX_STRING_SIZE];
|
|
|
|
char name[MAX_STRING_SIZE];
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *owner;
|
|
|
|
connection_t *other;
|
|
|
|
subnet_t *s, *find;
|
2001-01-05 23:53:53 +00:00
|
|
|
avl_node_t *node;
|
2000-09-17 21:42:05 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
|
2000-09-22 15:06:28 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if owner name is a valid */
|
|
|
|
|
2000-10-15 00:59:37 +00:00
|
|
|
if(check_id(name))
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
|
2000-09-17 21:42:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
/* Check if the owner of the new subnet is in the connection list */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
if(!(owner = lookup_node(name)))
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-11-03 21:22:02 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
|
|
|
|
"DEL_SUBNET", c->name, c->hostname, name);
|
2000-09-17 21:42:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
/* Check if subnet string is valid */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
if(!(s = str2net(subnetstr)))
|
2000-09-17 21:42:05 +00:00
|
|
|
{
|
2001-11-03 21:22:02 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
|
2000-09-22 15:06:28 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-09-22 15:06:28 +00:00
|
|
|
|
2000-10-11 22:01:02 +00:00
|
|
|
/* If everything is correct, delete the subnet from the list of the owner */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
find = lookup_subnet(owner, s);
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
free_subnet(s);
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(!find)
|
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
|
|
|
|
"DEL_SUBNET", c->name, c->hostname, name);
|
2001-10-27 12:13:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-03 21:22:02 +00:00
|
|
|
subnet_del(owner, find);
|
2000-10-29 00:02:20 +00:00
|
|
|
|
|
|
|
/* Tell the rest */
|
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2000-11-20 19:12:17 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other != c)
|
|
|
|
send_del_subnet(other, s);
|
2000-11-20 19:12:17 +00:00
|
|
|
}
|
2000-09-22 15:06:28 +00:00
|
|
|
cp
|
2000-10-11 22:01:02 +00:00
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* New and closed connections notification */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_add_node(connection_t *c, node_t *n)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s %lx:%d", ADD_NODE,
|
|
|
|
n->name, n->address, n->port);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int add_node_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
connection_t *other;
|
|
|
|
node_t *n;
|
|
|
|
char name[MAX_STRING_SIZE];
|
|
|
|
ipv4_t address;
|
|
|
|
port_t port;
|
2001-01-05 23:53:53 +00:00
|
|
|
avl_node_t *node;
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
|
2000-09-10 22:49:46 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
|
|
|
|
/* Check if identity is a valid name */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-11-22 19:55:53 +00:00
|
|
|
if(check_id(name))
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
|
2001-09-24 14:12:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check if node already exists */
|
2001-09-24 14:12:00 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n = lookup_node(name);
|
|
|
|
|
|
|
|
if(n)
|
2001-09-24 14:12:00 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check if it matches */
|
2001-10-30 16:34:32 +00:00
|
|
|
|
|
|
|
if(n->address != address || n->port != port)
|
|
|
|
syslog(LOG_DEBUG, _("Got %s from %s (%s) for %s which does not match existing entry"), "ADD_NODE", c->name, c->hostname, n->name);
|
|
|
|
|
|
|
|
return 0;
|
2001-09-24 14:12:00 +00:00
|
|
|
}
|
2001-10-27 12:13:17 +00:00
|
|
|
else
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
n = new_node();
|
|
|
|
n->name = xstrdup(name);
|
|
|
|
n->address = address;
|
|
|
|
n->port = port;
|
|
|
|
node_add(n);
|
2000-09-10 22:49:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Tell the rest about the new node */
|
2000-10-29 00:02:20 +00:00
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2000-11-20 19:12:17 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other !=c)
|
|
|
|
send_add_node(other, n);
|
2000-11-20 19:12:17 +00:00
|
|
|
}
|
2000-10-29 00:02:20 +00:00
|
|
|
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_del_node(connection_t *c, node_t *n)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-22 15:06:28 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s %lx:%d", DEL_NODE,
|
|
|
|
n->name, n->address, n->port);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int del_node_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *n;
|
|
|
|
char name[MAX_STRING_SIZE];
|
2001-05-07 19:08:46 +00:00
|
|
|
ipv4_t address;
|
2000-09-10 22:49:46 +00:00
|
|
|
port_t port;
|
2001-10-27 12:13:17 +00:00
|
|
|
connection_t *other;
|
2001-10-31 12:50:24 +00:00
|
|
|
avl_node_t *node;
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-14 21:51:21 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
/* Check if identity is a valid name */
|
|
|
|
|
2000-10-29 00:02:20 +00:00
|
|
|
if(check_id(name))
|
2000-09-22 15:06:28 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
|
2001-09-24 14:12:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-09-10 22:49:46 +00:00
|
|
|
/* Check if somebody tries to delete ourself */
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2000-09-22 15:06:28 +00:00
|
|
|
if(!strcmp(name, myself->name))
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2001-09-24 14:12:00 +00:00
|
|
|
return -1;
|
2000-09-22 15:06:28 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Check if the deleted host exists */
|
2000-09-22 15:06:28 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n = lookup_node(name);
|
|
|
|
|
|
|
|
if(!n)
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
|
2001-10-27 12:13:17 +00:00
|
|
|
return 0;
|
2000-09-10 22:49:46 +00:00
|
|
|
}
|
2000-09-22 15:06:28 +00:00
|
|
|
|
|
|
|
/* Check if the rest matches */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(address != n->address || port != n->port)
|
2000-09-22 15:06:28 +00:00
|
|
|
{
|
2001-10-30 16:34:32 +00:00
|
|
|
syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not match existing entry"), "DEL_NODE", c->name, c->hostname, n->name);
|
2000-09-22 15:06:28 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
/* Tell the rest about the deleted node */
|
2000-10-29 00:02:20 +00:00
|
|
|
|
2001-01-05 23:53:53 +00:00
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2000-11-20 19:12:17 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other != c)
|
|
|
|
send_del_node(other, n);
|
2000-11-20 19:12:17 +00:00
|
|
|
}
|
2001-10-27 12:13:17 +00:00
|
|
|
|
|
|
|
/* Delete the node */
|
|
|
|
|
|
|
|
node_del(n);
|
2001-10-30 16:34:32 +00:00
|
|
|
|
|
|
|
mst_kruskal();
|
2001-10-31 12:50:24 +00:00
|
|
|
sssp_bfs(0);
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
/* Edges */
|
2001-10-28 08:41:19 +00:00
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
int send_add_edge(connection_t *c, edge_t *e)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
|
|
|
cp
|
2001-10-31 20:37:54 +00:00
|
|
|
return send_request(c, "%d %s %s %lx %d", ADD_EDGE,
|
2001-10-30 12:59:12 +00:00
|
|
|
e->from->name, e->to->name, e->options, e->weight);
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int add_edge_h(connection_t *c)
|
|
|
|
{
|
|
|
|
connection_t *other;
|
2001-10-28 10:16:18 +00:00
|
|
|
edge_t *e;
|
2001-10-28 08:41:19 +00:00
|
|
|
node_t *from, *to;
|
|
|
|
char from_name[MAX_STRING_SIZE];
|
|
|
|
char to_name[MAX_STRING_SIZE];
|
|
|
|
long int options;
|
2001-10-30 12:59:12 +00:00
|
|
|
int weight;
|
2001-10-28 08:41:19 +00:00
|
|
|
avl_node_t *node;
|
|
|
|
cp
|
2001-10-30 12:59:12 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if names are valid */
|
|
|
|
|
|
|
|
if(check_id(from_name))
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_id(to_name))
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup nodes */
|
|
|
|
|
|
|
|
from = lookup_node(from_name);
|
|
|
|
|
|
|
|
if(!from)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
to = lookup_node(to_name);
|
|
|
|
|
|
|
|
if(!to)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-30 16:34:32 +00:00
|
|
|
/* Check if edge already exists */
|
2001-10-28 08:41:19 +00:00
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
e = lookup_edge(from, to);
|
2001-10-28 08:41:19 +00:00
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
if(e)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
2001-10-30 16:34:32 +00:00
|
|
|
if(e->weight != weight || e->options != options)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-28 10:16:18 +00:00
|
|
|
e = new_edge();
|
|
|
|
e->from = from;
|
|
|
|
e->to = to;
|
|
|
|
e->options = options;
|
2001-10-30 12:59:12 +00:00
|
|
|
e->weight = weight;
|
2001-10-28 10:16:18 +00:00
|
|
|
edge_add(e);
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the rest about the new edge */
|
|
|
|
|
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
|
|
|
{
|
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other != c)
|
2001-10-28 10:16:18 +00:00
|
|
|
send_add_edge(other, e);
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
2001-10-30 12:59:12 +00:00
|
|
|
/* Run MST before or after we tell the rest? */
|
|
|
|
|
|
|
|
mst_kruskal();
|
2001-10-31 12:50:24 +00:00
|
|
|
sssp_bfs(0);
|
2001-10-28 08:41:19 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
int send_del_edge(connection_t *c, edge_t *e)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
|
|
|
cp
|
2001-10-30 16:34:32 +00:00
|
|
|
return send_request(c, "%d %s %s %lx %d", DEL_EDGE,
|
|
|
|
e->from->name, e->to->name, e->options, e->weight);
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int del_edge_h(connection_t *c)
|
|
|
|
{
|
2001-10-28 10:16:18 +00:00
|
|
|
edge_t *e;
|
2001-10-28 08:41:19 +00:00
|
|
|
char from_name[MAX_STRING_SIZE];
|
|
|
|
char to_name[MAX_STRING_SIZE];
|
|
|
|
node_t *from, *to;
|
|
|
|
long int options;
|
2001-10-30 16:34:32 +00:00
|
|
|
int weight;
|
2001-10-28 08:41:19 +00:00
|
|
|
connection_t *other;
|
|
|
|
avl_node_t *node;
|
|
|
|
cp
|
2001-10-30 16:34:32 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
|
|
|
|
c->name, c->hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if names are valid */
|
|
|
|
|
|
|
|
if(check_id(from_name))
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_id(to_name))
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup nodes */
|
|
|
|
|
|
|
|
from = lookup_node(from_name);
|
|
|
|
|
|
|
|
if(!from)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
|
2001-11-04 23:29:50 +00:00
|
|
|
return 0;
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to = lookup_node(to_name);
|
|
|
|
|
|
|
|
if(!to)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
|
2001-11-04 23:29:50 +00:00
|
|
|
return 0;
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if edge exists */
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
e = lookup_edge(from, to);
|
2001-10-28 08:41:19 +00:00
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
if(e)
|
2001-10-28 08:41:19 +00:00
|
|
|
{
|
2001-10-30 16:34:32 +00:00
|
|
|
if(e->weight != weight || e->options != options)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
|
2001-11-04 23:29:50 +00:00
|
|
|
return 0;
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the rest about the deleted edge */
|
|
|
|
|
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
|
|
|
{
|
|
|
|
other = (connection_t *)node->data;
|
|
|
|
if(other->status.active && other != c)
|
2001-10-28 10:16:18 +00:00
|
|
|
send_del_edge(other, e);
|
2001-10-28 08:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the edge */
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
edge_del(e);
|
2001-10-30 12:59:12 +00:00
|
|
|
|
|
|
|
/* Run MST before or after we tell the rest? */
|
|
|
|
|
|
|
|
mst_kruskal();
|
2001-10-31 12:50:24 +00:00
|
|
|
sssp_bfs(1);
|
2001-10-28 08:41:19 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-10 15:18:03 +00:00
|
|
|
/* Status and error notification routines */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_status(connection_t *c, int statusno, char *statusstring)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
|
|
|
if(!statusstring)
|
|
|
|
statusstring = status_text[statusno];
|
2000-09-10 15:18:03 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int status_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
|
|
|
int statusno;
|
2000-11-22 19:55:53 +00:00
|
|
|
char statusstring[MAX_STRING_SIZE];
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-10 16:15:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-10-21 11:52:08 +00:00
|
|
|
if(debug_lvl >= DEBUG_STATUS)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-14 21:51:21 +00:00
|
|
|
syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, status_text[statusno], statusstring);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cp
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_error(connection_t *c, int err, char *errstring)
|
2000-09-10 15:18:03 +00:00
|
|
|
{
|
|
|
|
cp
|
2000-09-14 21:51:21 +00:00
|
|
|
if(!errstring)
|
2001-02-06 10:12:51 +00:00
|
|
|
errstring = strerror(err);
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %d %s", ERROR, err, errstring);
|
2000-09-10 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int error_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-02-06 10:42:27 +00:00
|
|
|
int err;
|
2000-11-22 19:55:53 +00:00
|
|
|
char errorstring[MAX_STRING_SIZE];
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-10 16:15:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-10-21 11:52:08 +00:00
|
|
|
if(debug_lvl >= DEBUG_ERROR)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-14 21:51:21 +00:00
|
|
|
syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, strerror(err), errorstring);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
terminate_connection(c, c->status.active);
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2000-09-10 16:15:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_termreq(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d", TERMREQ);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int termreq_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
terminate_connection(c, c->status.active);
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_ping(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-05-24 21:29:09 +00:00
|
|
|
char salt[SALTLEN*2+1];
|
2000-11-04 20:44:28 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
c->status.pinged = 1;
|
|
|
|
c->last_ping_time = time(NULL);
|
2001-07-04 08:41:36 +00:00
|
|
|
RAND_pseudo_bytes(salt, SALTLEN);
|
2001-05-24 21:29:09 +00:00
|
|
|
bin2hex(salt, salt, SALTLEN);
|
2001-05-25 08:36:11 +00:00
|
|
|
salt[SALTLEN*2] = '\0';
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s", PING, salt);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int ping_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_pong(c);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_pong(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
2001-05-24 21:29:09 +00:00
|
|
|
char salt[SALTLEN*2+1];
|
2000-09-10 16:15:35 +00:00
|
|
|
cp
|
2001-07-04 08:41:36 +00:00
|
|
|
RAND_pseudo_bytes(salt, SALTLEN);
|
2001-05-24 21:29:09 +00:00
|
|
|
bin2hex(salt, salt, SALTLEN);
|
2001-05-25 08:36:11 +00:00
|
|
|
salt[SALTLEN*2] = '\0';
|
2001-05-24 21:29:09 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s", PONG, salt);
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int pong_h(connection_t *c)
|
2000-09-10 16:15:35 +00:00
|
|
|
{
|
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
c->status.pinged = 0;
|
2000-09-10 22:49:46 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2000-09-10 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
2000-09-10 22:49:46 +00:00
|
|
|
/* Key exchange */
|
2000-09-10 16:15:35 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_key_changed(connection_t *c, node_t *n)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
connection_t *other;
|
2001-01-05 23:53:53 +00:00
|
|
|
avl_node_t *node;
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-05-24 21:52:26 +00:00
|
|
|
/* Only send this message if some other daemon requested our key previously.
|
|
|
|
This reduces unnecessary key_changed broadcasts.
|
|
|
|
*/
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(n == myself && !mykeyused)
|
2001-06-09 10:00:34 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for(node = connection_tree->head; node; node = node->next)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
other = (connection_t *)node->data;
|
2001-11-03 21:22:02 +00:00
|
|
|
if(other->status.active && other != c)
|
2001-10-27 12:13:17 +00:00
|
|
|
send_request(other, "%d %s", KEY_CHANGED, n->name);
|
2001-06-09 10:00:34 +00:00
|
|
|
}
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2000-03-26 00:33:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int key_changed_h(connection_t *c)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
char name[MAX_STRING_SIZE];
|
|
|
|
node_t *n;
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-14 21:51:21 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-05-01 18:07:12 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n = lookup_node(name);
|
|
|
|
|
|
|
|
if(!n)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, name);
|
2000-03-26 00:33:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-10 23:11:37 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
n->status.validkey = 0;
|
|
|
|
n->status.waitingforkey = 0;
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
send_key_changed(c, n);
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2000-03-26 00:33:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_req_key(connection_t *c, node_t *from, node_t *to)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s %s", REQ_KEY,
|
2000-09-17 21:42:05 +00:00
|
|
|
from->name, to->name);
|
2000-03-26 00:33:07 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int req_key_h(connection_t *c)
|
2000-08-07 14:52:16 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
char from_name[MAX_STRING_SIZE];
|
|
|
|
char to_name[MAX_STRING_SIZE];
|
|
|
|
node_t *from, *to;
|
|
|
|
char key[MAX_STRING_SIZE];
|
2000-08-07 14:52:16 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
|
2000-08-07 14:52:16 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-10 22:49:46 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
from = lookup_node(from_name);
|
|
|
|
|
|
|
|
if(!from)
|
2000-08-07 14:52:16 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, from_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
to = lookup_node(to_name);
|
|
|
|
|
|
|
|
if(!to)
|
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, to_name);
|
2000-08-07 14:52:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-03-26 00:33:07 +00:00
|
|
|
|
2000-09-10 22:49:46 +00:00
|
|
|
/* Check if this key request is for us */
|
2000-05-01 18:07:12 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(to == myself) /* Yes, send our own key back */
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
bin2hex(myself->key, key, myself->keylength);
|
|
|
|
key[myself->keylength * 2] = '\0';
|
|
|
|
send_ans_key(c, myself, from, key);
|
2001-05-24 21:52:26 +00:00
|
|
|
mykeyused = 1;
|
2000-03-26 00:33:07 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
else
|
|
|
|
{
|
2000-10-29 22:10:44 +00:00
|
|
|
if(to->status.validkey) /* Proxy keys */
|
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
bin2hex(to->key, key, to->keylength);
|
|
|
|
key[to->keylength * 2] = '\0';
|
|
|
|
send_ans_key(c, to, from, key);
|
2000-10-29 22:10:44 +00:00
|
|
|
}
|
|
|
|
else
|
2001-10-27 12:13:17 +00:00
|
|
|
send_req_key(to->nexthop->connection, from, to);
|
2000-09-10 22:49:46 +00:00
|
|
|
}
|
|
|
|
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2000-03-26 00:33:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_request(c, "%d %s %s %s", ANS_KEY,
|
|
|
|
from->name, to->name, key);
|
2000-03-26 00:33:07 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int ans_key_h(connection_t *c)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
char from_name[MAX_STRING_SIZE];
|
|
|
|
char to_name[MAX_STRING_SIZE];
|
|
|
|
char key[MAX_STRING_SIZE];
|
2000-09-10 23:11:37 +00:00
|
|
|
int keylength;
|
2001-10-27 12:13:17 +00:00
|
|
|
node_t *from, *to;
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
|
2000-06-23 19:27:03 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname);
|
2000-09-10 22:49:46 +00:00
|
|
|
return -1;
|
2000-09-17 21:42:05 +00:00
|
|
|
}
|
2000-05-01 18:07:12 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
from = lookup_node(from_name);
|
|
|
|
|
|
|
|
if(!from)
|
2000-03-26 00:33:07 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, from_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
to = lookup_node(to_name);
|
|
|
|
|
|
|
|
if(!to)
|
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
|
2001-10-27 12:13:17 +00:00
|
|
|
c->name, c->hostname, to_name);
|
2000-03-26 00:33:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-11-04 16:54:21 +00:00
|
|
|
/* Check correctness of packet key */
|
2000-05-01 18:07:12 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
keylength = strlen(key);
|
2000-09-17 21:42:05 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(keylength != from->keylength * 2)
|
2000-10-29 10:39:08 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
|
|
|
|
c->name, c->hostname, from->name, _("invalid key length"));
|
2000-10-29 10:39:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-10-29 09:19:27 +00:00
|
|
|
|
2000-11-04 16:54:21 +00:00
|
|
|
/* Forward it if necessary */
|
2000-10-29 09:19:27 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(to != myself)
|
2000-09-10 22:49:46 +00:00
|
|
|
{
|
2001-10-27 12:13:17 +00:00
|
|
|
send_ans_key(to->nexthop->connection, from, to, key);
|
2000-03-26 00:33:07 +00:00
|
|
|
}
|
2000-09-10 22:49:46 +00:00
|
|
|
|
2000-11-04 16:54:21 +00:00
|
|
|
/* Update our copy of the origin's packet key */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
if(from->key)
|
|
|
|
free(from->key);
|
2000-11-04 16:54:21 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
from->key = xstrdup(key);
|
2000-11-04 16:54:21 +00:00
|
|
|
keylength /= 2;
|
2001-10-27 12:13:17 +00:00
|
|
|
hex2bin(from->key, from->key, keylength);
|
|
|
|
from->key[keylength] = '\0';
|
2000-11-04 16:54:21 +00:00
|
|
|
|
|
|
|
from->status.validkey = 1;
|
|
|
|
from->status.waitingforkey = 0;
|
2001-01-07 15:25:49 +00:00
|
|
|
|
|
|
|
flush_queue(from);
|
2000-04-25 18:57:23 +00:00
|
|
|
cp
|
2000-03-26 00:33:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int send_tcppacket(connection_t *c, vpn_packet_t *packet)
|
2001-01-07 20:19:35 +00:00
|
|
|
{
|
|
|
|
int x;
|
2001-03-04 13:59:32 +00:00
|
|
|
cp
|
2001-05-25 11:54:28 +00:00
|
|
|
/* Evil hack. */
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
x = send_request(c, "%d %hd", PACKET, packet->len);
|
2001-01-07 20:19:35 +00:00
|
|
|
|
|
|
|
if(x)
|
|
|
|
return x;
|
2001-05-25 11:54:28 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
return send_meta(c, packet->data, packet->len);
|
2001-01-07 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
int tcppacket_h(connection_t *c)
|
2001-01-07 20:19:35 +00:00
|
|
|
{
|
2001-05-25 11:54:28 +00:00
|
|
|
short int len;
|
2001-03-04 13:59:32 +00:00
|
|
|
cp
|
2001-10-27 12:13:17 +00:00
|
|
|
if(sscanf(c->buffer, "%*d %hd", &len) != 1)
|
2001-01-07 20:19:35 +00:00
|
|
|
{
|
2001-10-28 08:41:19 +00:00
|
|
|
syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
|
2001-01-07 20:19:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-05-25 11:54:28 +00:00
|
|
|
/* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
|
2001-01-07 20:19:35 +00:00
|
|
|
|
2001-10-27 12:13:17 +00:00
|
|
|
c->tcplen = len;
|
2001-03-04 13:59:32 +00:00
|
|
|
cp
|
|
|
|
return 0;
|
2001-01-07 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
2000-09-22 16:20:07 +00:00
|
|
|
/* Jumptable for the request handlers */
|
2000-09-10 15:18:03 +00:00
|
|
|
|
2000-11-20 19:12:17 +00:00
|
|
|
int (*request_handlers[])(connection_t*) = {
|
2001-10-27 12:13:17 +00:00
|
|
|
id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
|
2000-09-10 16:15:35 +00:00
|
|
|
status_h, error_h, termreq_h,
|
|
|
|
ping_h, pong_h,
|
2001-10-27 12:13:17 +00:00
|
|
|
add_node_h, del_node_h,
|
2000-09-10 22:49:46 +00:00
|
|
|
add_subnet_h, del_subnet_h,
|
2001-10-28 08:41:19 +00:00
|
|
|
add_edge_h, del_edge_h,
|
2000-09-10 22:49:46 +00:00
|
|
|
key_changed_h, req_key_h, ans_key_h,
|
2001-01-07 20:19:35 +00:00
|
|
|
tcppacket_h,
|
2000-09-10 15:18:03 +00:00
|
|
|
};
|
|
|
|
|
2000-09-22 16:20:07 +00:00
|
|
|
/* Request names */
|
|
|
|
|
2000-09-10 16:15:35 +00:00
|
|
|
char (*request_name[]) = {
|
2001-10-27 12:13:17 +00:00
|
|
|
"ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
|
2000-09-10 16:15:35 +00:00
|
|
|
"STATUS", "ERROR", "TERMREQ",
|
|
|
|
"PING", "PONG",
|
2001-10-27 12:13:17 +00:00
|
|
|
"ADD_NODE", "DEL_NODE",
|
2000-09-10 22:49:46 +00:00
|
|
|
"ADD_SUBNET", "DEL_SUBNET",
|
2001-10-28 08:41:19 +00:00
|
|
|
"ADD_EDGE", "DEL_EDGE",
|
2000-09-10 22:49:46 +00:00
|
|
|
"KEY_CHANGED", "REQ_KEY", "ANS_KEY",
|
2001-01-07 20:19:35 +00:00
|
|
|
"PACKET",
|
2000-03-26 00:33:07 +00:00
|
|
|
};
|
2000-10-14 17:04:16 +00:00
|
|
|
|
|
|
|
/* Status strings */
|
|
|
|
|
|
|
|
char (*status_text[]) = {
|
2000-10-16 16:33:30 +00:00
|
|
|
"Warning",
|
2000-10-14 17:04:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Error strings */
|
|
|
|
|
|
|
|
char (*error_text[]) = {
|
2000-10-16 16:33:30 +00:00
|
|
|
"Error",
|
2000-10-14 17:04:16 +00:00
|
|
|
};
|