Added checkpoints to beginning and ending of every function.

This commit is contained in:
Guus Sliepen 2000-04-25 18:57:23 +00:00
parent b6bdb9079a
commit 3a33568652
4 changed files with 168 additions and 151 deletions

View file

@ -68,7 +68,7 @@ int char_hex_to_bin(int c)
int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
{
int i = 0, j = 0, l = strlen(hex);
cp
if(l&1)
{
i = j = 1;
@ -76,7 +76,7 @@ int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
}
for(; i < l; i+=2, j++)
bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
cp
return j&1?j+1:j;
}
@ -88,7 +88,7 @@ int read_passphrase(char *which, char **out)
int size;
extern char *confbase;
char *pp;
cp
if((cfg = get_config_val(passphrasesdir)) == NULL)
{
filename = xmalloc(strlen(confbase)+13+strlen(which));
@ -118,14 +118,16 @@ int read_passphrase(char *which, char **out)
fclose(f);
*out = xmalloc(size);
cp
return str_hex_to_bin(*out, pp);
}
int read_my_passphrase(void)
{
cp
if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
return -1;
cp
return 0;
}
@ -135,7 +137,7 @@ int generate_private_key(void)
int i;
char *s;
config_t const *cfg;
cp
if((cfg = get_config_val(keyexpire)) == NULL)
my_key_expiry = (time_t)(time(NULL) + 3600);
else
@ -157,20 +159,23 @@ int generate_private_key(void)
s[2 * PRIVATE_KEY_LENGTH] = '\0';
mpz_set_str(my_private_key, s, 16);
cp
return 0;
}
void calculate_public_key(void)
{
cp
mpz_powm(my_public_key, generator, my_private_key, shared_prime);
my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
cp
}
unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
int security_init(void)
{
cp
mpz_init(my_private_key);
mpz_init(my_public_key);
mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
@ -185,7 +190,7 @@ int security_init(void)
return -1;
calculate_public_key();
cp
return 0;
}
@ -194,7 +199,7 @@ void set_shared_key(char *almost_key)
char *tmp;
int len;
mpz_t ak, our_shared_key;
cp
mpz_init_set_str(ak, almost_key, 36);
mpz_init(our_shared_key);
mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
@ -212,6 +217,7 @@ void set_shared_key(char *almost_key)
free(tmp);
mpz_clear(ak);
mpz_clear(our_shared_key);
cp
}
@ -221,7 +227,7 @@ void encrypt_passphrase(passphrase_t *pp)
char tmp[1000];
int len;
BF_KEY bf_key;
cp
mpz_get_str(&tmp[0], 16, my_public_key);
len = str_hex_to_bin(key, tmp);
@ -232,6 +238,7 @@ void encrypt_passphrase(passphrase_t *pp)
if(key_inited)
cipher_set_key(&encryption_key, encryption_keylen, &text_key[0]);
cp
}
int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
@ -244,7 +251,7 @@ int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
BF_KEY bf_key;
char which[sizeof("123.123.123.123")+1];
char *meuk;
cp
mpz_init_set_str(pk, his_pubkey, 36);
mpz_get_str(&tmp[0], 16, pk);
len = str_hex_to_bin(key, tmp);
@ -261,7 +268,7 @@ int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
if(memcmp(meuk, out, len))
return -1;
cp
return 0;
}
@ -269,7 +276,7 @@ char *make_shared_key(char *pk)
{
mpz_t tmp, res;
char *r;
cp
mpz_init_set_str(tmp, pk, 36);
mpz_init(res);
mpz_powm(res, tmp, my_private_key, shared_prime);
@ -278,7 +285,7 @@ char *make_shared_key(char *pk)
mpz_clear(res);
mpz_clear(tmp);
cp
return r;
}
@ -287,6 +294,7 @@ char *make_shared_key(char *pk)
*/
void free_key(enc_key_t *k)
{
cp
if(!k)
return;
if(k->key)
@ -295,13 +303,14 @@ void free_key(enc_key_t *k)
free(k->key);
}
free(k);
cp
}
void recalculate_encryption_keys(void)
{
conn_list_t *p;
char *ek;
cp
for(p = conn_list; p != NULL; p = p->next)
{
if(!p->public_key || !p->public_key->key)
@ -315,12 +324,15 @@ void recalculate_encryption_keys(void)
p->key->key = xmalloc(strlen(ek) + 1);
strcpy(p->key->key, ek);
}
cp
}
void regenerate_keys(void)
{
cp
generate_private_key();
calculate_public_key();
send_key_changed2();
recalculate_encryption_keys();
cp
}

131
src/net.c
View file

@ -63,10 +63,11 @@ conn_list_t *myself = NULL;
void strip_mac_addresses(vpn_packet_t *p)
{
unsigned char tmp[MAXSIZE];
cp
memcpy(tmp, p->data, p->len);
p->len -= 12;
memcpy(p->data, &tmp[12], p->len);
cp
}
/*
@ -75,7 +76,7 @@ void strip_mac_addresses(vpn_packet_t *p)
void add_mac_addresses(vpn_packet_t *p)
{
unsigned char tmp[MAXSIZE];
cp
memcpy(&tmp[12], p->data, p->len);
p->len += 12;
tmp[0] = tmp[6] = 0xfe;
@ -83,13 +84,14 @@ void add_mac_addresses(vpn_packet_t *p)
*((ip_t*)(&tmp[2])) = (ip_t)(htonl(myself->vpn_ip));
*((ip_t*)(&tmp[8])) = *((ip_t*)(&tmp[26]));
memcpy(p->data, &tmp[0], p->len);
cp
}
int xsend(conn_list_t *cl, void *packet)
{
int r;
real_packet_t rp;
cp
do_encrypt((vpn_packet_t*)packet, &rp, cl->key);
rp.from = myself->vpn_ip;
@ -103,7 +105,7 @@ int xsend(conn_list_t *cl, void *packet)
}
total_socket_out += r;
cp
return 0;
}
@ -114,7 +116,7 @@ int xsend(conn_list_t *cl, void *packet)
int write_n(int fd, void *buf, size_t len)
{
int r, done = 0;
cp
do
{
if((r = write(fd, buf, len)) < 0)
@ -125,13 +127,14 @@ int write_n(int fd, void *buf, size_t len)
} while(len > 0);
return done;
cp
}
int xrecv(conn_list_t *cl, void *packet)
{
vpn_packet_t vp;
int lenin;
cp
do_decrypt((real_packet_t*)packet, &vp, cl->key);
add_mac_addresses(&vp);
@ -139,7 +142,7 @@ int xrecv(conn_list_t *cl, void *packet)
syslog(LOG_ERR, "Can't write to tap device: %m");
else
total_tap_out += lenin;
cp
return 0;
}
@ -150,7 +153,7 @@ int xrecv(conn_list_t *cl, void *packet)
void add_queue(packet_queue_t **q, void *packet, size_t s)
{
queue_element_t *e, *p;
cp
if(debug_lvl > 3)
syslog(LOG_DEBUG, "packet to queue: %d", s);
@ -172,6 +175,7 @@ void add_queue(packet_queue_t **q, void *packet, size_t s)
if((*q)->head == NULL)
(*q)->head = e;
cp
}
/*
@ -183,12 +187,11 @@ void flush_queue(conn_list_t *cl, packet_queue_t *pq,
int (*function)(conn_list_t*,void*))
{
queue_element_t *p, *prev = NULL, *next = NULL;
cp
for(p = pq->head; p != NULL; )
{
next = p->next;
cp
if(!function(cl, p->packet))
{
if(prev)
@ -196,21 +199,18 @@ cp
else
pq->head = next;
cp
free(p->packet);
cp
free(p);
cp
}
else
prev = p;
cp
p = next;
}
if(debug_lvl > 3)
syslog(LOG_DEBUG, "queue flushed");
cp
}
/*
@ -228,7 +228,6 @@ cp
IP_ADDR_V(cl->vpn_ip));
flush_queue(cl, cl->sq, xsend);
}
cp
if(cl->rq)
{
@ -246,7 +245,7 @@ cp
int send_packet(ip_t to, vpn_packet_t *packet)
{
conn_list_t *cl;
cp
if((cl = lookup_conn(to)) == NULL)
{
if(debug_lvl > 2)
@ -263,15 +262,12 @@ int send_packet(ip_t to, vpn_packet_t *packet)
}
}
cp
if(my_key_expiry <= time(NULL))
regenerate_keys();
cp
if(!cl->status.dataopen)
if(setup_vpn_connection(cl) < 0)
return -1;
cp
if(!cl->status.validkey)
{
@ -281,7 +277,6 @@ cp
return 0;
}
cp
if(!cl->status.active)
{
add_queue(&(cl->sq), packet, packet->len + 2);
@ -298,7 +293,7 @@ cp
int send_broadcast(conn_list_t *cl, vpn_packet_t *packet)
{
conn_list_t *p;
cp
for(p = cl; p != NULL; p = p->next)
if(send_packet(p->real_ip, packet) < 0)
{
@ -306,7 +301,7 @@ int send_broadcast(conn_list_t *cl, vpn_packet_t *packet)
p->vpn_ip, p->real_ip);
break; /* FIXME: should retry later, and send a ping over the metaconnection. */
}
cp
return 0;
}
@ -318,7 +313,7 @@ int setup_tap_fd(void)
int nfd;
const char *tapfname;
config_t const *cfg;
cp
if((cfg = get_config_val(tapdevice)) == NULL)
tapfname = "/dev/tap0";
else
@ -331,6 +326,7 @@ int setup_tap_fd(void)
}
tap_fd = nfd;
cp
return 0;
}
@ -343,7 +339,7 @@ int setup_listen_meta_socket(int port)
int nfd, flags;
struct sockaddr_in a;
const int one = 1;
cp
if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
syslog(LOG_ERR, "Creating metasocket failed: %m");
@ -379,7 +375,7 @@ int setup_listen_meta_socket(int port)
syslog(LOG_ERR, "listen: %m");
return -1;
}
cp
return nfd;
}
@ -392,7 +388,7 @@ int setup_vpn_in_socket(int port)
int nfd, flags;
struct sockaddr_in a;
const int one = 1;
cp
if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
syslog(LOG_ERR, "Creating socket failed: %m");
@ -422,7 +418,7 @@ int setup_vpn_in_socket(int port)
syslog(LOG_ERR, "Can't bind to port %hd/udp: %m", port);
return -1;
}
cp
return nfd;
}
@ -434,7 +430,7 @@ int setup_outgoing_meta_socket(conn_list_t *cl)
int flags;
struct sockaddr_in a;
config_t const *cfg;
cp
if((cfg = get_config_val(upstreamport)) == NULL)
cl->port = 655;
else
@ -467,7 +463,7 @@ int setup_outgoing_meta_socket(conn_list_t *cl)
cl->hostname = hostlookup(htonl(cl->real_ip));
syslog(LOG_INFO, "Connected to %s:%hd" , cl->hostname, cl->port);
cp
return 0;
}
@ -481,7 +477,7 @@ int setup_outgoing_meta_socket(conn_list_t *cl)
int setup_outgoing_connection(ip_t ip)
{
conn_list_t *ncn;
cp
ncn = new_conn_list();
ncn->real_ip = ip;
@ -496,7 +492,7 @@ int setup_outgoing_connection(ip_t ip)
ncn->status.outgoing = 1;
ncn->next = conn_list;
conn_list = ncn;
cp
return 0;
}
@ -506,7 +502,7 @@ int setup_outgoing_connection(ip_t ip)
int setup_myself(void)
{
config_t const *cfg;
cp
myself = new_conn_list();
if(!(cfg = get_config_val(myvpnip)))
@ -539,7 +535,7 @@ int setup_myself(void)
myself->status.active = 1;
syslog(LOG_NOTICE, "Ready: listening on port %d.", myself->port);
cp
return 0;
}
@ -549,7 +545,7 @@ int setup_myself(void)
int setup_network_connections(void)
{
config_t const *cfg;
cp
if((cfg = get_config_val(pingtimeout)) == NULL)
timeout = 10;
else
@ -567,7 +563,7 @@ int setup_network_connections(void)
if(setup_outgoing_connection(cfg->data.ip->ip))
return -1;
cp
return 0;
}
@ -576,7 +572,7 @@ sigalrm_handler(int a)
{
config_t const *cfg;
static int seconds_till_retry;
cp
cfg = get_config_val(upstreamip);
if(!setup_outgoing_connection(cfg->data.ip->ip))
@ -592,6 +588,7 @@ sigalrm_handler(int a)
syslog(LOG_ERR, "Still failed to connect to other. Will retry in %d seconds.",
seconds_till_retry);
}
cp
}
/*
@ -600,7 +597,7 @@ sigalrm_handler(int a)
void close_network_connections(void)
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
{
if(p->status.dataopen)
@ -627,6 +624,7 @@ void close_network_connections(void)
destroy_conn_list();
syslog(LOG_NOTICE, "Terminating.");
cp
return;
}
@ -637,7 +635,7 @@ int setup_vpn_connection(conn_list_t *cl)
{
int nfd, flags;
struct sockaddr_in a;
cp
if(debug_lvl > 1)
syslog(LOG_DEBUG, "Opening UDP socket to " IP_ADDR_S, IP_ADDR_V(cl->real_ip));
@ -668,7 +666,7 @@ int setup_vpn_connection(conn_list_t *cl)
cl->socket = nfd;
cl->status.dataopen = 1;
cp
return 0;
}
@ -681,7 +679,7 @@ conn_list_t *create_new_connection(int sfd)
conn_list_t *p;
struct sockaddr_in ci;
int len = sizeof(ci);
cp
p = new_conn_list();
if(getpeername(sfd, &ci, &len) < 0)
@ -702,7 +700,7 @@ conn_list_t *create_new_connection(int sfd)
free(p);
return NULL;
}
cp
return p;
}
@ -712,7 +710,7 @@ conn_list_t *create_new_connection(int sfd)
void build_fdset(fd_set *fs)
{
conn_list_t *p;
cp
FD_ZERO(fs);
for(p = conn_list; p != NULL; p = p->next)
@ -726,6 +724,7 @@ void build_fdset(fd_set *fs)
FD_SET(myself->meta_socket, fs);
FD_SET(myself->socket, fs);
FD_SET(tap_fd, fs);
cp
}
/*
@ -739,7 +738,7 @@ int handle_incoming_vpn_data(conn_list_t *cl)
int lenin;
int x, l = sizeof(x);
conn_list_t *f;
cp
if(getsockopt(cl->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
{
syslog(LOG_ERR, "This is a bug: %s:%d: %d:%m", __FILE__, __LINE__, cl->socket);
@ -784,7 +783,7 @@ int handle_incoming_vpn_data(conn_list_t *cl)
if(my_key_expiry <= time(NULL))
regenerate_keys();
}
cp
return 0;
}
@ -794,6 +793,7 @@ int handle_incoming_vpn_data(conn_list_t *cl)
*/
void terminate_connection(conn_list_t *cl)
{
cp
if(cl->status.remove)
return;
@ -817,6 +817,7 @@ void terminate_connection(conn_list_t *cl)
}
cl->status.remove = 1;
cp
}
/*
@ -826,7 +827,7 @@ void terminate_connection(conn_list_t *cl)
int send_broadcast_ping(void)
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
{
if(p->status.remove)
@ -844,7 +845,7 @@ int send_broadcast_ping(void)
}
last_ping_time = time(NULL);
cp
return 0;
}
@ -855,7 +856,7 @@ int send_broadcast_ping(void)
int check_dead_connections(void)
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
{
if(p->status.remove)
@ -868,7 +869,7 @@ int check_dead_connections(void)
terminate_connection(p);
}
}
cp
return 0;
}
@ -881,7 +882,7 @@ int handle_new_meta_connection(conn_list_t *cl)
conn_list_t *ncn;
struct sockaddr client;
int nfd, len = sizeof(struct sockaddr);
cp
if((nfd = accept(cl->meta_socket, &client, &len)) < 0)
{
syslog(LOG_ERR, "Accepting a new connection failed: %m");
@ -899,7 +900,7 @@ int handle_new_meta_connection(conn_list_t *cl)
ncn->status.meta = 1;
ncn->next = conn_list;
conn_list = ncn;
cp
return 0;
}
@ -911,7 +912,7 @@ int handle_incoming_meta_data(conn_list_t *cl)
int x, l = sizeof(x), lenin;
unsigned char tmp[1600];
int request;
cp
if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
{
syslog(LOG_ERR, "This is a bug: %s:%d: %d:%m", __FILE__, __LINE__, cl->meta_socket);
@ -939,7 +940,7 @@ int handle_incoming_meta_data(conn_list_t *cl)
else
if(request_handlers[request](cl, tmp, lenin) < 0)
return -1;
cp
return 0;
}
@ -951,12 +952,12 @@ void check_network_activity(fd_set *f)
{
conn_list_t *p;
int x, l = sizeof(x);
cp
for(p = conn_list; p != NULL; p = p->next)
{
if(p->status.remove)
continue;
cp
if(p->status.active)
if(FD_ISSET(p->socket, f))
{
@ -971,7 +972,7 @@ cp
terminate_connection(p);
return;
}
cp
if(p->status.meta)
if(FD_ISSET(p->meta_socket, f))
if(handle_incoming_meta_data(p) < 0)
@ -979,13 +980,11 @@ cp
terminate_connection(p);
return;
}
cp
}
cp
if(FD_ISSET(myself->socket, f))
handle_incoming_vpn_data(myself);
cp
if(FD_ISSET(myself->meta_socket, f))
handle_new_meta_connection(myself);
cp
@ -1000,7 +999,7 @@ void handle_tap_input(void)
vpn_packet_t vp;
ip_t from, to;
int ether_type, lenin;
cp
memset(&vp, 0, sizeof(vp));
if((lenin = read(tap_fd, &vp, MTU)) <= 0)
{
@ -1037,9 +1036,9 @@ void handle_tap_input(void)
MAC_ADDR_V(vp.data[0]), MAC_ADDR_V(vp.data[6]));
vp.len = (length_t)lenin - 2;
cp
strip_mac_addresses(&vp);
cp
send_packet(to, &vp);
cp
}
@ -1052,7 +1051,7 @@ void main_loop(void)
fd_set fset;
struct timeval tv;
int r;
cp
last_ping_time = time(NULL);
for(;;)
@ -1060,11 +1059,8 @@ void main_loop(void)
tv.tv_sec = timeout;
tv.tv_usec = 0;
cp
prune_conn_list();
cp
build_fdset(&fset);
cp
if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
{
@ -1073,7 +1069,6 @@ cp
syslog(LOG_ERR, "Error while waiting for input: %m");
return;
}
cp
if(r == 0 || last_ping_time + timeout < time(NULL))
/* Timeout... hm... something might be wrong. */
@ -1083,13 +1078,11 @@ cp
continue;
}
cp
check_network_activity(&fset);
cp
/* local tap data */
if(FD_ISSET(tap_fd, &fset))
handle_tap_input();
cp
}
cp
}

View file

@ -42,7 +42,7 @@
conn_list_t *lookup_conn(ip_t ip)
{
conn_list_t *p = conn_list;
cp
/* Exact match suggested by James B. MacLean */
for(p = conn_list; p != NULL; p = p->next)
if(ip == p->vpn_ip)
@ -50,7 +50,7 @@ conn_list_t *lookup_conn(ip_t ip)
for(p = conn_list; p != NULL; p = p->next)
if((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask))
return p;
cp
return NULL;
}
@ -60,7 +60,7 @@ conn_list_t *lookup_conn(ip_t ip)
void destroy_queue(packet_queue_t *pq)
{
queue_element_t *p, *q;
cp
for(p = pq->head; p != NULL; p = q)
{
q = p->next;
@ -70,6 +70,7 @@ void destroy_queue(packet_queue_t *pq)
}
free(pq);
cp
}
/*
@ -77,6 +78,7 @@ void destroy_queue(packet_queue_t *pq)
*/
void free_conn_element(conn_list_t *p)
{
cp
if(p->hostname)
free(p->hostname);
if(p->pp)
@ -88,6 +90,7 @@ void free_conn_element(conn_list_t *p)
free_key(p->public_key);
free_key(p->key);
free(p);
cp
}
/*
@ -96,7 +99,7 @@ void free_conn_element(conn_list_t *p)
void prune_conn_list(void)
{
conn_list_t *p, *prev = NULL, *next = NULL;
cp
for(p = conn_list; p != NULL; )
{
next = p->next;
@ -115,6 +118,7 @@ void prune_conn_list(void)
p = next;
}
cp
}
/*
@ -123,10 +127,11 @@ void prune_conn_list(void)
conn_list_t *new_conn_list(void)
{
conn_list_t *p = xmalloc(sizeof(conn_list_t));
cp
/* initialise all those stupid pointers at once */
memset(p, '\0', sizeof(conn_list_t));
p->nexthop = p;
cp
return p;
}
@ -137,16 +142,15 @@ void destroy_conn_list(void)
{
conn_list_t *p, *next;
cp
for(p = conn_list; p != NULL; )
{
next = p->next;
free_conn_element(p);
p = next;
}
cp
conn_list = NULL;
cp
}
/*
@ -158,7 +162,7 @@ char *hostlookup(unsigned long addr)
char *name;
struct hostent *host = NULL;
struct in_addr in;
cp
in.s_addr = addr;
host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
@ -173,7 +177,7 @@ char *hostlookup(unsigned long addr)
name = xmalloc(20);
sprintf(name, "%s", inet_ntoa(in));
}
cp
return name;
}
@ -187,7 +191,7 @@ ip_mask_t *strtoip(char *str)
int masker;
char *q, *p;
struct hostent *h;
cp
p = str;
if((q = strchr(p, '/')))
{
@ -213,14 +217,14 @@ ip_mask_t *strtoip(char *str)
ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
cp
return ip;
}
void dump_conn_list(void)
{
conn_list_t *p;
cp
syslog(LOG_DEBUG, "Connection list:");
for(p = conn_list; p != NULL; p = p->next)
@ -229,4 +233,5 @@ void dump_conn_list(void)
IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->vpn_mask), p->status,
p->socket, p->meta_socket);
}
cp
}

View file

@ -36,7 +36,7 @@
int send_ack(conn_list_t *cl)
{
unsigned char tmp = ACK;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "Send ACK to %s", cl->hostname);
@ -46,14 +46,14 @@ int send_ack(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %d:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_termreq(conn_list_t *cl)
{
termreq_t tmp;
cp
tmp.type = TERMREQ;
tmp.vpn_ip = myself->vpn_ip;
@ -66,14 +66,14 @@ int send_termreq(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_timeout(conn_list_t *cl)
{
termreq_t tmp;
cp
tmp.type = PINGTIMEOUT;
tmp.vpn_ip = myself->vpn_ip;
@ -86,14 +86,14 @@ int send_timeout(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_del_host(conn_list_t *cl, conn_list_t *new_host)
{
del_host_t tmp;
cp
tmp.type = DEL_HOST;
tmp.vpn_ip = new_host->vpn_ip;
@ -106,14 +106,14 @@ int send_del_host(conn_list_t *cl, conn_list_t *new_host)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_ping(conn_list_t *cl)
{
unsigned char tmp = PING;
cp
if(debug_lvl > 3)
syslog(LOG_DEBUG, "pinging " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
@ -122,27 +122,27 @@ int send_ping(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_pong(conn_list_t *cl)
{
unsigned char tmp = PONG;
cp
if((send(cl->meta_socket, &tmp, sizeof(tmp), 0)) < 0)
{
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_add_host(conn_list_t *cl, conn_list_t *new_host)
{
add_host_t tmp;
cp
tmp.type = ADD_HOST;
tmp.real_ip = new_host->real_ip;
tmp.vpn_ip = new_host->vpn_ip;
@ -159,14 +159,14 @@ int send_add_host(conn_list_t *cl, conn_list_t *new_host)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_key_changed(conn_list_t *cl, conn_list_t *src)
{
key_changed_t tmp;
cp
tmp.type = KEY_CHANGED;
tmp.from = src->vpn_ip;
@ -179,23 +179,24 @@ int send_key_changed(conn_list_t *cl, conn_list_t *src)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
void send_key_changed2(void)
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
if(p->status.meta && p->protocol_version > PROT_3)
send_key_changed(p, myself);
cp
}
int send_basic_info(conn_list_t *cl)
{
basic_info_t tmp;
cp
tmp.type = BASIC_INFO;
tmp.protocol = PROT_CURRENT;
@ -213,14 +214,14 @@ int send_basic_info(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_passphrase(conn_list_t *cl)
{
passphrase_t tmp;
cp
tmp.type = PASSPHRASE;
encrypt_passphrase(&tmp);
@ -233,14 +234,14 @@ int send_passphrase(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_public_key(conn_list_t *cl)
{
public_key_t *tmp;
cp
tmp = (public_key_t*)xmalloc(strlen(my_public_key_base36)+sizeof(public_key_t));
tmp->type = PUBLIC_KEY;
tmp->len = strlen(my_public_key_base36);
@ -255,14 +256,14 @@ int send_public_key(conn_list_t *cl)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
int send_calculate(conn_list_t *cl, char *k)
{
calculate_t *tmp;
cp
tmp = xmalloc(strlen(k)+sizeof(calculate_t));
tmp->type = CALCULATE;
tmp->len = strlen(k);
@ -273,7 +274,7 @@ int send_calculate(conn_list_t *cl, char *k)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
@ -281,7 +282,7 @@ int send_key_request(ip_t to)
{
key_req_t *tmp;
conn_list_t *fw;
cp
tmp = xmalloc(sizeof(key_req_t));
tmp->type = REQ_KEY;
tmp->to = to;
@ -305,7 +306,7 @@ int send_key_request(ip_t to)
return -1;
}
fw->status.waitingforkey = 1;
cp
return 0;
}
@ -313,7 +314,7 @@ int send_key_answer(conn_list_t *cl, ip_t to)
{
key_req_t *tmp;
conn_list_t *fw;
cp
tmp = xmalloc(sizeof(key_req_t)+strlen(my_public_key_base36));
tmp->type = ANS_KEY;
tmp->to = to;
@ -339,7 +340,7 @@ int send_key_answer(conn_list_t *cl, ip_t to)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
@ -352,11 +353,11 @@ int notify_others(conn_list_t *new, conn_list_t *source,
int (*function)(conn_list_t*, conn_list_t*))
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
if(p != new && p != source && p->status.meta && p->protocol_version > PROT_3)
function(p, new);
cp
return 0;
}
@ -367,11 +368,11 @@ int notify_others(conn_list_t *new, conn_list_t *source,
int notify_one(conn_list_t *new)
{
conn_list_t *p;
cp
for(p = conn_list; p != NULL; p = p->next)
if(p != new && p->protocol_version > PROT_3)
send_add_host(new, p);
cp
return 0;
}
@ -382,7 +383,7 @@ int notify_one(conn_list_t *new)
int basic_info_h(conn_list_t *cl, unsigned char *d, int len)
{
basic_info_t *tmp = (basic_info_t*)d;
cp
cl->protocol_version = tmp->protocol;
cl->port = tmp->portnr;
cl->vpn_ip = tmp->vpn_ip;
@ -416,14 +417,14 @@ int basic_info_h(conn_list_t *cl, unsigned char *d, int len)
}
cl->status.active = 0;
cp
return 0;
}
int passphrase_h(conn_list_t *cl, unsigned char *d, int len)
{
passphrase_t *tmp = (passphrase_t*)d;
cp
cl->pp = xmalloc(tmp->len+3);
memcpy(cl->pp, tmp, tmp->len+3);
@ -434,7 +435,7 @@ int passphrase_h(conn_list_t *cl, unsigned char *d, int len)
send_passphrase(cl);
else
send_public_key(cl);
cp
return 0;
}
@ -442,7 +443,7 @@ int public_key_h(conn_list_t *cl, unsigned char *d, int len)
{
char *g_n;
public_key_t *tmp = (public_key_t*)d;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "got PUBLIC_KEY(%hd,%s)", tmp->len, &tmp->key);
@ -467,12 +468,13 @@ int public_key_h(conn_list_t *cl, unsigned char *d, int len)
cl->status.active = 1;
notify_others(cl, NULL, send_add_host);
notify_one(cl);
cp
return 0;
}
int ack_h(conn_list_t *cl, unsigned char *d, int len)
{
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "got ACK");
@ -480,6 +482,7 @@ int ack_h(conn_list_t *cl, unsigned char *d, int len)
syslog(LOG_NOTICE, "Connection with %s activated.", cl->hostname);
/*
=== FIXME ===
Now I'm going to cheat. The meta protocol is actually
a stream of requests, that may come in in the same TCP
packet. This is the only place that it will happen,
@ -494,27 +497,29 @@ int ack_h(conn_list_t *cl, unsigned char *d, int len)
if(request_handlers[d[1]](cl, d + 1, len - 1) < 0)
return -1;
}
cp
return 0;
}
int termreq_h(conn_list_t *cl, unsigned char *d, int len)
{
cp
syslog(LOG_NOTICE, IP_ADDR_S " wants to quit", IP_ADDR_V(cl->vpn_ip));
cl->status.termreq = 1;
terminate_connection(cl);
notify_others(cl, NULL, send_del_host);
cp
return 0;
}
int timeout_h(conn_list_t *cl, unsigned char *d, int len)
{
cp
syslog(LOG_NOTICE, IP_ADDR_S " says it's gotten a timeout from us", IP_ADDR_V(cl->vpn_ip));
cl->status.termreq = 1;
terminate_connection(cl);
cp
return 0;
}
@ -522,7 +527,7 @@ int del_host_h(conn_list_t *cl, unsigned char *d, int len)
{
del_host_t *tmp = (del_host_t*)d;
conn_list_t *fw;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "got DEL_HOST for " IP_ADDR_S,
IP_ADDR_V(tmp->vpn_ip));
@ -538,28 +543,30 @@ int del_host_h(conn_list_t *cl, unsigned char *d, int len)
fw->status.termreq = 1;
terminate_connection(fw);
cp
return 0;
}
int ping_h(conn_list_t *cl, unsigned char *d, int len)
{
cp
if(debug_lvl > 3)
syslog(LOG_DEBUG, "responding to ping from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
cl->status.pinged = 0;
cl->status.got_pong = 1;
send_pong(cl);
cp
return 0;
}
int pong_h(conn_list_t *cl, unsigned char *d, int len)
{
cp
if(debug_lvl > 3)
syslog(LOG_DEBUG, "ok, got pong from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
cl->status.got_pong = 1;
cp
return 0;
}
@ -567,7 +574,7 @@ int add_host_h(conn_list_t *cl, unsigned char *d, int len)
{
add_host_t *tmp = (add_host_t*)d;
conn_list_t *ncn, *fw;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "Add host request from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
if(debug_lvl > 3)
@ -606,7 +613,7 @@ int add_host_h(conn_list_t *cl, unsigned char *d, int len)
if(request_handlers[d[sizeof(add_host_t)]](cl, d + sizeof(add_host_t), len - sizeof(add_host_t)) < 0)
return -1;
}
cp
return 0;
}
@ -614,7 +621,7 @@ int req_key_h(conn_list_t *cl, unsigned char *d, int len)
{
key_req_t *tmp = (key_req_t*)d;
conn_list_t *fw;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "got REQ_KEY from " IP_ADDR_S " for " IP_ADDR_S,
IP_ADDR_V(tmp->from), IP_ADDR_V(tmp->to));
@ -642,14 +649,14 @@ int req_key_h(conn_list_t *cl, unsigned char *d, int len)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
void set_keys(conn_list_t *cl, key_req_t *k)
{
char *ek;
cp
if(!cl->public_key)
{
cl->public_key = xmalloc(sizeof(enc_key_t));
@ -674,13 +681,14 @@ void set_keys(conn_list_t *cl, key_req_t *k)
cl->key->expiry = k->expiry;
cl->key->key = xmalloc(strlen(ek) + 1);
strcpy(cl->key->key, ek);
cp
}
int ans_key_h(conn_list_t *cl, unsigned char *d, int len)
{
key_req_t *tmp = (key_req_t*)d;
conn_list_t *fw, *gk;
cp
if(debug_lvl > 3)
syslog(LOG_DEBUG, "got ANS_KEY from " IP_ADDR_S " for " IP_ADDR_S,
IP_ADDR_V(tmp->from), IP_ADDR_V(tmp->to));
@ -722,7 +730,7 @@ int ans_key_h(conn_list_t *cl, unsigned char *d, int len)
syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
return -1;
}
cp
return 0;
}
@ -730,7 +738,7 @@ int key_changed_h(conn_list_t *cl, unsigned char *d, int len)
{
key_changed_t *tmp = (key_changed_t*)d;
conn_list_t *ik;
cp
if(debug_lvl > 2)
syslog(LOG_DEBUG, "got KEY_CHANGED from " IP_ADDR_S,
IP_ADDR_V(tmp->from));
@ -751,7 +759,7 @@ int key_changed_h(conn_list_t *cl, unsigned char *d, int len)
syslog(LOG_DEBUG, "Forwarding key invalidation request");
notify_others(cl, ik, send_key_changed);
cp
return 0;
}
@ -777,4 +785,3 @@ int (*request_handlers[256])(conn_list_t*, unsigned char*, int) = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};