2000-10-11 10:35:17 +00:00
|
|
|
/*
|
|
|
|
connlist.c -- connection list management
|
|
|
|
Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
|
|
|
|
2000 Ivo Timmermans <itimmermans@bigfoot.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2000-10-20 15:34:38 +00:00
|
|
|
$Id: connlist.c,v 1.1.2.7 2000/10/20 15:34:34 guus Exp $
|
2000-10-11 10:35:17 +00:00
|
|
|
*/
|
|
|
|
|
2000-10-11 22:01:02 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
2000-10-14 17:04:16 +00:00
|
|
|
#include "net.h" /* Don't ask. */
|
2000-10-11 10:35:17 +00:00
|
|
|
#include "config.h"
|
2000-10-14 17:04:16 +00:00
|
|
|
#include "conf.h"
|
2000-10-11 10:35:17 +00:00
|
|
|
#include <utils.h>
|
|
|
|
|
2000-10-14 17:04:16 +00:00
|
|
|
#include "system.h"
|
2000-10-11 10:35:17 +00:00
|
|
|
|
|
|
|
/* Root of the connection list */
|
|
|
|
|
|
|
|
conn_list_t *conn_list = NULL;
|
|
|
|
conn_list_t *myself = NULL;
|
|
|
|
|
|
|
|
/* Creation and deletion of conn_list elements */
|
|
|
|
|
|
|
|
conn_list_t *new_conn_list(void)
|
|
|
|
{
|
2000-10-11 22:01:02 +00:00
|
|
|
conn_list_t *p = (conn_list_t *)xmalloc(sizeof(*p));
|
2000-10-11 10:35:17 +00:00
|
|
|
cp
|
|
|
|
/* initialise all those stupid pointers at once */
|
|
|
|
memset(p, '\0', sizeof(*p));
|
|
|
|
cp
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_conn_list(conn_list_t *p)
|
|
|
|
{
|
|
|
|
cp
|
|
|
|
if(p->sq)
|
|
|
|
destroy_queue(p->sq);
|
|
|
|
if(p->rq)
|
|
|
|
destroy_queue(p->rq);
|
2000-10-15 00:59:37 +00:00
|
|
|
if(p->name && p->name!=unknown)
|
2000-10-11 10:35:17 +00:00
|
|
|
free(p->name);
|
|
|
|
if(p->hostname)
|
|
|
|
free(p->hostname);
|
2000-10-20 15:34:38 +00:00
|
|
|
if(p->rsa_key)
|
|
|
|
RSA_free(p->rsa_key);
|
2000-10-14 17:04:16 +00:00
|
|
|
if(p->cipher_pktkey)
|
|
|
|
free(p->cipher_pktkey);
|
2000-10-15 00:59:37 +00:00
|
|
|
if(p->buffer)
|
|
|
|
free(p->buffer);
|
2000-10-11 10:35:17 +00:00
|
|
|
free(p);
|
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
remove all marked connections
|
|
|
|
*/
|
|
|
|
void prune_conn_list(void)
|
|
|
|
{
|
|
|
|
conn_list_t *p, *prev = NULL, *next = NULL;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; )
|
|
|
|
{
|
|
|
|
next = p->next;
|
|
|
|
|
|
|
|
if(p->status.remove)
|
|
|
|
{
|
|
|
|
if(prev)
|
|
|
|
prev->next = next;
|
|
|
|
else
|
|
|
|
conn_list = next;
|
|
|
|
|
2000-10-14 17:04:16 +00:00
|
|
|
free_conn_list(p);
|
2000-10-11 10:35:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
prev = p;
|
|
|
|
|
|
|
|
p = next;
|
|
|
|
}
|
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
free all elements of conn_list
|
|
|
|
*/
|
|
|
|
void destroy_conn_list(void)
|
|
|
|
{
|
|
|
|
conn_list_t *p, *next;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; )
|
|
|
|
{
|
|
|
|
next = p->next;
|
2000-10-14 17:04:16 +00:00
|
|
|
free_conn_list(p);
|
2000-10-11 10:35:17 +00:00
|
|
|
p = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_list = NULL;
|
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Linked list management */
|
|
|
|
|
|
|
|
void conn_list_add(conn_list_t *cl)
|
|
|
|
{
|
|
|
|
cp
|
2000-10-11 22:01:02 +00:00
|
|
|
cl->next = conn_list;
|
2000-10-11 10:35:17 +00:00
|
|
|
cl->prev = NULL;
|
2000-10-16 16:33:30 +00:00
|
|
|
if(cl->next)
|
|
|
|
cl->next->prev = cl;
|
2000-10-11 22:01:02 +00:00
|
|
|
conn_list = cl;
|
2000-10-11 10:35:17 +00:00
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
|
|
|
void conn_list_del(conn_list_t *cl)
|
|
|
|
{
|
|
|
|
cp
|
|
|
|
if(cl->prev)
|
|
|
|
cl->prev->next = cl->next;
|
|
|
|
else
|
2000-10-11 22:01:02 +00:00
|
|
|
conn_list = cl->next;
|
2000-10-11 10:35:17 +00:00
|
|
|
|
|
|
|
cl->next->prev = cl->prev;
|
|
|
|
free_conn_list(cl);
|
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup functions */
|
|
|
|
|
2000-10-11 22:01:02 +00:00
|
|
|
conn_list_t *lookup_id(char *name)
|
|
|
|
{
|
|
|
|
conn_list_t *p;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; p = p->next)
|
2000-10-16 19:04:47 +00:00
|
|
|
if(p->status.active)
|
|
|
|
if(strcmp(name, p->name) == 0)
|
|
|
|
break;
|
2000-10-11 22:01:02 +00:00
|
|
|
cp
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2000-10-11 10:35:17 +00:00
|
|
|
conn_list_t *lookup_conn_list_mac(mac_t address)
|
|
|
|
{
|
|
|
|
conn_list_t *p;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; p = p->next)
|
2000-10-11 22:01:02 +00:00
|
|
|
if(lookup_subnet_mac(p->subnets, address))
|
2000-10-11 10:35:17 +00:00
|
|
|
break;
|
|
|
|
cp
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_list_t *lookup_conn_list_ipv4(ipv4_t address)
|
|
|
|
{
|
|
|
|
conn_list_t *p;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; p = p->next)
|
2000-10-11 22:01:02 +00:00
|
|
|
if(lookup_subnet_ipv4(p->subnets, address))
|
2000-10-11 10:35:17 +00:00
|
|
|
break;
|
|
|
|
cp
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_list_t *lookup_conn_list_ipv6(ipv6_t address)
|
|
|
|
{
|
|
|
|
conn_list_t *p;
|
|
|
|
cp
|
|
|
|
for(p = conn_list; p != NULL; p = p->next)
|
2000-10-11 22:01:02 +00:00
|
|
|
if(lookup_subnet_ipv6(p->subnets, address))
|
2000-10-11 10:35:17 +00:00
|
|
|
break;
|
|
|
|
cp
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Debugging */
|
|
|
|
|
|
|
|
void dump_conn_list(void)
|
|
|
|
{
|
|
|
|
conn_list_t *p;
|
2000-10-11 22:01:02 +00:00
|
|
|
subnet_t *s;
|
|
|
|
char *netstr;
|
2000-10-11 10:35:17 +00:00
|
|
|
cp
|
|
|
|
syslog(LOG_DEBUG, _("Connection list:"));
|
|
|
|
|
|
|
|
for(p = conn_list; p != NULL; p = p->next)
|
|
|
|
{
|
2000-10-11 22:01:02 +00:00
|
|
|
syslog(LOG_DEBUG, _("%s at %s port %hd flags %d sockets %d, %d status %04x"),
|
|
|
|
p->name, p->hostname, p->port, p->flags,
|
2000-10-11 10:35:17 +00:00
|
|
|
p->socket, p->meta_socket, p->status);
|
2000-10-11 22:01:02 +00:00
|
|
|
for(s = p->subnets; s != NULL; s = s->next)
|
|
|
|
{
|
|
|
|
netstr = net2str(s);
|
|
|
|
syslog(LOG_DEBUG, ": %s", netstr);
|
|
|
|
free(netstr);
|
|
|
|
}
|
2000-10-11 10:35:17 +00:00
|
|
|
}
|
2000-10-11 22:01:02 +00:00
|
|
|
|
|
|
|
syslog(LOG_DEBUG, _("End of connection list."));
|
2000-10-11 10:35:17 +00:00
|
|
|
cp
|
|
|
|
}
|
2000-10-14 17:04:16 +00:00
|
|
|
|
|
|
|
int read_host_config(conn_list_t *cl)
|
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
int x;
|
|
|
|
cp
|
2000-10-15 00:59:37 +00:00
|
|
|
asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
|
2000-10-14 17:04:16 +00:00
|
|
|
x = read_config_file(&cl->config, fname);
|
|
|
|
free(fname);
|
|
|
|
cp
|
|
|
|
return x;
|
|
|
|
}
|