- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.meta && p->status.active && p!= cl)
- send_add_subnet(p, subnet);
- }
-cp
- return 0;
-}
-
-int send_del_subnet(connection_t *cl, subnet_t *subnet)
-{
- int x;
- char *netstr;
-cp
- netstr = net2str(subnet);
- x = send_request(cl, "%d %s %s", DEL_SUBNET, subnet->owner->name, netstr);
- free(netstr);
-cp
- return x;
-}
-
-int del_subnet_h(connection_t *cl)
-{
- char subnetstr[MAX_STRING_SIZE];
- char name[MAX_STRING_SIZE];
- connection_t *owner, *p;
- subnet_t *subnet;
- avl_node_t *node;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if owner name is a valid */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid identity name"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if subnet string is valid */
-
- if(!(subnet = str2net(subnetstr)))
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid subnet string"), cl->name, cl->hostname);
- return -1;
- }
-
- free(subnetstr);
-
- /* Check if somebody tries to add a subnet of ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got DEL_SUBNET from %s (%s) for ourself, restarting"),
- cl->name, cl->hostname);
- sighup = 1;
- return 0;
- }
-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_id(name)))
- {
- syslog(LOG_ERR, _("Got DEL_SUBNET for %s from %s (%s) which is not in our connection list"),
- name, cl->name, cl->hostname);
- return -1;
- }
-
- /* If everything is correct, delete the subnet from the list of the owner */
-
- subnet_del(subnet);
-
- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.meta && p->status.active && p!= cl)
- send_del_subnet(p, subnet);
- }
-cp
- return 0;
-}
-
-/* New and closed connections notification */
-
-int send_add_host(connection_t *cl, connection_t *other)
-{
-cp
- return send_request(cl, "%d %s %lx:%d %lx", ADD_HOST,
- other->name, other->address, other->port, other->options);
-}
-
-int add_host_h(connection_t *cl)
-{
- connection_t *old, *new, *p;
- char name[MAX_STRING_SIZE];
- avl_node_t *node;
-cp
- new = new_connection();
-
- if(sscanf(cl->buffer, "%*d "MAX_STRING" %lx:%d %lx", name, &new->address, &new->port, &new->options) != 4)
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
- free_connection(new);
- return -1;
- }
-
- /* Check if somebody tries to add ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->name, cl->hostname);
- sighup = 1;
- free_connection(new);
- return 0;
- }
-
- /* Fill in more of the new connection structure */
-
- new->hostname = hostlookup(htonl(new->address));
-
- /* Check if the new host already exists in the connnection list */
-
- if((old = lookup_id(name)))
- {
- if((new->address == old->address) && (new->port == old->port))
- {
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
- old->name, old->hostname, name, new->hostname);
- free_connection(new);
- return 0;
- }
- else
- {
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Removing old entry for %s (%s) in favour of new connection"),
- old->name, old->hostname);
-
- terminate_connection(old);
- }
- }
-
- /* Hook it up into the connection */
-
- new->name = xstrdup(name);
- connection_add(new);
- id_add(new);
-
- /* Tell the rest about the new host */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.meta && p->status.active && p!=cl)
- send_add_host(p, new);
- }
-
- /* Fill in rest of connection structure */
-
- new->nexthop = cl;
- new->status.active = 1;
- new->cipher_pkttype = EVP_bf_cfb();
- new->cipher_pktkeylength = cl->cipher_pkttype->key_len + cl->cipher_pkttype->iv_len;
-cp
- return 0;
-}
-
-int send_del_host(connection_t *cl, connection_t *other)
-{
-cp
- return send_request(cl, "%d %s %lx:%d %lx", DEL_HOST,
- other->name, other->address, other->port, other->options);
-}
-
-int del_host_h(connection_t *cl)
-{
- char name[MAX_STRING_SIZE];
- ip_t address;
- port_t port;
- long int options;
- connection_t *old, *p;
- avl_node_t *node;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" %lx:%d %lx", name, &address, &port, &options) != 4)
- {
- syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if somebody tries to delete ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
- cl->name, cl->hostname);
- sighup = 1;
- return 0;
- }
-
- /* Check if the new host already exists in the connnection list */
-
- if(!(old = lookup_id(name)))
- {
- syslog(LOG_ERR, _("Got DEL_HOST from %s (%s) for %s which is not in our connection list"),
- name, cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if the rest matches */
-
- if(address!=old->address || port!=old->port || options!=old->options || cl!=old->nexthop)
- {
- syslog(LOG_WARNING, _("Got DEL_HOST from %s (%s) for %s which doesn't match"), cl->name, cl->hostname, old->name);
- return 0;
- }
-
- /* Ok, since EVERYTHING seems to check out all right, delete it */
-
- old->status.active = 0;
- terminate_connection(old);
-
- /* Tell the rest about the new host */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.meta && p->status.active && p!=cl)
- send_del_host(p, old);
- }
-cp
- return 0;
-}
-
-/* Status and error notification routines */
-
-int send_status(connection_t *cl, int statusno, char *statusstring)
-{
-cp
- if(!statusstring)
- statusstring = status_text[statusno];
-cp
- return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
-}
-
-int status_h(connection_t *cl)
-{
- int statusno;
- char statusstring[MAX_STRING_SIZE];
-cp
- if(sscanf(cl->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
- {
- syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_STATUS)
- {
- syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
- cl->name, cl->hostname, status_text[statusno], statusstring);
- }
-
-cp
- return 0;
-}
-
-int send_error(connection_t *cl, int errno, char *errstring)
-{
-cp
- if(!errstring)
- errstring = strerror(errno);
- return send_request(cl, "%d %d %s", ERROR, errno, errstring);
-}
-
-int error_h(connection_t *cl)
-{
- int errno;
- char errorstring[MAX_STRING_SIZE];
-cp
- if(sscanf(cl->buffer, "%*d %d "MAX_STRING, &errno, errorstring) != 2)
- {
- syslog(LOG_ERR, _("Got bad ERROR from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_ERROR)
- {
- syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
- cl->name, cl->hostname, strerror(errno), errorstring);
- }
-
- terminate_connection(cl);
-cp
- return 0;
-}
-
-int send_termreq(connection_t *cl)
-{
-cp
- return send_request(cl, "%d", TERMREQ);
-}
-
-int termreq_h(connection_t *cl)
-{
-cp
- terminate_connection(cl);
-cp
- return 0;
-}
-
-/* Keepalive routines - FIXME: needs a closer look */
-
-int send_ping(connection_t *cl)
-{
-cp
- cl->status.pinged = 1;
- cl->last_ping_time = time(NULL);
-cp
- return send_request(cl, "%d", PING);
-}
-
-int ping_h(connection_t *cl)
-{
-cp
- return send_pong(cl);
-}
-
-int send_pong(connection_t *cl)
-{
-cp
- return send_request(cl, "%d", PONG);
-}
-
-int pong_h(connection_t *cl)
-{
-cp
- cl->status.pinged = 0;
-cp
- return 0;
-}
-
-/* Key exchange */
-
-int send_key_changed(connection_t *from, connection_t *cl)
-{
- connection_t *p;
- avl_node_t *node;
-cp
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p != cl && p->status.meta && p->status.active)
- send_request(p, "%d %s", KEY_CHANGED, from->name);
- }
-cp
- return 0;
-}
-
-int key_changed_h(connection_t *cl)
-{
- char from_id[MAX_STRING_SIZE];
- connection_t *from;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING, from_id) != 1)
- {
- syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- if(!(from = lookup_id(from_id)))
- {
- syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"),
- cl->name, cl->hostname, from_id);
- return -1;
- }
-
- from->status.validkey = 0;
- from->status.waitingforkey = 0;
-
- send_key_changed(from, cl);
-cp
- return 0;
-}
-
-int send_req_key(connection_t *from, connection_t *to)
-{
-cp
- return send_request(to->nexthop, "%d %s %s", REQ_KEY,
- from->name, to->name);
-}
-
-int req_key_h(connection_t *cl)
-{
- char from_id[MAX_STRING_SIZE];
- char to_id[MAX_STRING_SIZE];
- connection_t *from, *to;
- char pktkey[129];
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" "MAX_STRING, from_id, to_id) != 2)
- {
- syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- if(!(from = lookup_id(from_id)))
- {
- syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"),
- cl->name, cl->hostname, from_id);
- return -1;
- }
-
- /* Check if this key request is for us */
-
- if(!strcmp(to_id, myself->name))
- {
- bin2hex(myself->cipher_pktkey, pktkey, myself->cipher_pktkeylength);
- pktkey[myself->cipher_pktkeylength*2] = '\0';
- send_ans_key(myself, from, pktkey);
- }
- else
- {
- if(!(to = lookup_id(to_id)))
- {
- syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"),
- cl->name, cl->hostname, to_id);
- return -1;
- }
-
- if(to->status.validkey) /* Proxy keys */
- {
- bin2hex(to->cipher_pktkey, pktkey, to->cipher_pktkeylength);
- pktkey[to->cipher_pktkeylength*2] = '\0';
- send_ans_key(to, from, pktkey);
- }
- else
- send_req_key(from, to);
- }
-
-cp
- return 0;
-}
-
-int send_ans_key(connection_t *from, connection_t *to, char *pktkey)
-{
-cp
- return send_request(to->nexthop, "%d %s %s %s", ANS_KEY,
- from->name, to->name, pktkey);
-}
-
-int ans_key_h(connection_t *cl)
-{
- char from_id[MAX_STRING_SIZE];
- char to_id[MAX_STRING_SIZE];
- char pktkey[MAX_STRING_SIZE];
- int keylength;
- connection_t *from, *to;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_id, to_id, pktkey) != 3)
- {
- syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- if(!(from = lookup_id(from_id)))
- {
- syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"),
- cl->name, cl->hostname, from_id);
- return -1;
- }
-
- /* Check correctness of packet key */
-
- keylength = strlen(pktkey);
-
- if(keylength != from->cipher_pktkeylength*2)
- {
- syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key length"),
- cl->name, cl->hostname, from->name);
- return -1;
- }
-
- /* Forward it if necessary */
-
- if(strcmp(to_id, myself->name))
- {
- if(!(to = lookup_id(to_id)))
- {
- syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"),
- cl->name, cl->hostname, to_id);
- return -1;
- }
- send_ans_key(from, to, pktkey);
- }
-
- /* Update our copy of the origin's packet key */
-
- if(from->cipher_pktkey)
- free(from->cipher_pktkey);
-
- from->cipher_pktkey = xstrdup(pktkey);
- keylength /= 2;
- hex2bin(from->cipher_pktkey, from->cipher_pktkey, keylength);
- from->cipher_pktkey[keylength] = '\0';
-
- from->status.validkey = 1;
- from->status.waitingforkey = 0;
-cp
- return 0;
-}
-
-/* Jumptable for the request handlers */
-
-int (*request_handlers[])(connection_t*) = {
- id_h, challenge_h, chal_reply_h, metakey_h, ack_h,
- status_h, error_h, termreq_h,
- ping_h, pong_h,
- add_host_h, del_host_h,
- add_subnet_h, del_subnet_h,
- key_changed_h, req_key_h, ans_key_h,
-};
-
-/* Request names */