- /* Check if subnet string is valid */
-
- if(!(s = str2net(subnetstr)))
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
- return -1;
- }
-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_node(name)))
- {
- syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
- name, c->name, c->hostname);
- return -1;
- }
-
- /* If everything is correct, add the subnet to the list of the owner */
-
- subnet_add(owner, s);
-
- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- other = (connection_t *)node->data;
- if(other->status.active && other != c)
- send_add_subnet(other, s);
- }
-cp
- return 0;
-}
-
-int send_del_subnet(connection_t *c, subnet_t *s)
-{
- int x;
- char *netstr;
-cp
- x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
- free(netstr);
-cp
- return x;
-}
-
-int del_subnet_h(connection_t *c)
-{
- char subnetstr[MAX_STRING_SIZE];
- char name[MAX_STRING_SIZE];
- node_t *owner;
- connection_t *other;
- subnet_t *s, *find;
- avl_node_t *node;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
- return -1;
- }
-
- /* Check if owner name is a valid */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
- return -1;
- }
-
- /* Check if subnet string is valid */
-
- if(!(s = str2net(subnetstr)))
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
- return -1;
- }
-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_node(name)))
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
- "DEL_SUBNET", c->name, c->hostname, name);
- return -1;
- }
-
- /* If everything is correct, delete the subnet from the list of the owner */
-
- find = lookup_subnet(owner, s);
-
- if(!find)
- {
- 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);
- return -1;
- }
-
- subnet_del(owner, s);
-
- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- other = (connection_t *)node->data;
- if(other->status.active && other != c)
- send_del_subnet(other, s);
- }
-cp
- return 0;
-}
-
-/* New and closed connections notification */
-
-int send_add_node(connection_t *c, node_t *n)
-{
-cp
- return send_request(c, "%d %s %lx:%d", ADD_NODE,
- n->name, n->address, n->port);
-}
-
-int add_node_h(connection_t *c)
-{
- connection_t *other;
- node_t *n;
- char name[MAX_STRING_SIZE];
- ipv4_t address;
- port_t port;
- avl_node_t *node;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
- return -1;
- }
-
- /* Check if node already exists */
-
- n = lookup_node(name);
-
- if(n)
- {
- /* Check if it matches */
- }
- else
- {
- n = new_node();
- n->name = xstrdup(name);
- n->address = address;
- n->port = port;
- node_add(n);
- }
-
- /* Tell the rest about the new node */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- other = (connection_t *)node->data;
- if(other->status.active && other !=c)
- send_add_node(other, n);
- }
-
-cp
- return 0;
-}
-
-int send_del_node(connection_t *c, node_t *n)
-{
-cp
- return send_request(c, "%d %s %lx:%d", DEL_NODE,
- n->name, n->address, n->port);
-}
-
-int del_node_h(connection_t *c)
-{
- node_t *n;
- char name[MAX_STRING_SIZE];
- ipv4_t address;
- port_t port;
- connection_t *other;
- avl_node_t *node;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
- c->name, c->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
- return -1;
- }
-
- /* Check if somebody tries to delete ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
- c->name, c->hostname);
- return -1;
- }
-
- /* Check if the deleted host exists */
-
- n = lookup_node(name);
-
- if(!n)
- {
- syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
- return 0;
- }
-
- /* Check if the rest matches */
-
- if(address != n->address || port != n->port)
- {
- syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which doesn't match"), "DEL_NODE", c->name, c->hostname, n->name);
- return 0;
- }
-
- /* Tell the rest about the deleted node */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- other = (connection_t *)node->data;
- if(other->status.active && other != c)
- send_del_node(other, n);
- }
-
- /* Delete the node */
-
- node_del(n);
-cp
- return 0;
-}
-
-/* Edges */
-
-int send_add_edge(connection_t *c, edge_t *e)
-{
-cp
- return send_request(c, "%d %s %s %lx %d", ADD_NODE,
- e->from->name, e->to->name, e->options, e->weight);
-}
-
-int add_edge_h(connection_t *c)
-{
- connection_t *other;
- edge_t *e;
- node_t *from, *to;
- char from_name[MAX_STRING_SIZE];
- char to_name[MAX_STRING_SIZE];
- long int options;
- int weight;
- avl_node_t *node;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
- {
- 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;
- }
-
- /* Check if node already exists */
-
- e = lookup_edge(from, to);
-
- if(e)
- {
- /* Check if it matches */
- }
- else
- {
- e = new_edge();
- e->from = from;
- e->to = to;
- e->options = options;
- e->weight = weight;
- edge_add(e);
- }
-
- /* 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)
- send_add_edge(other, e);
- }
-
- /* Run MST before or after we tell the rest? */
-
- mst_kruskal();
- sssp_bfs();
-cp
- return 0;
-}
-
-int send_del_edge(connection_t *c, edge_t *e)
-{
-cp
- return send_request(c, "%d %s %s %lx", DEL_EDGE,
- e->from->name, e->to->name, e->options);
-}
-
-int del_edge_h(connection_t *c)
-{
- edge_t *e;
- char from_name[MAX_STRING_SIZE];
- char to_name[MAX_STRING_SIZE];
- node_t *from, *to;
- long int options;
- connection_t *other;
- avl_node_t *node;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
- {
- 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"));
- return -1;
- }
-
- 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"));
- return -1;
- }
-
- /* Check if edge exists */
-
- e = lookup_edge(from, to);
-
- if(e)
- {
- /* Check if it matches */
- }
- else
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
- return -1;
- }
-
- /* 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)
- send_del_edge(other, e);
- }
-
- /* Delete the edge */
-
- edge_del(e);
-
- /* Run MST before or after we tell the rest? */
-
- mst_kruskal();
- sssp_bfs();
-cp
- return 0;
-}
-
-
-/* Status and error notification routines */
-
-int send_status(connection_t *c, int statusno, char *statusstring)
-{
-cp
- if(!statusstring)
- statusstring = status_text[statusno];
-cp
- return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
-}
-
-int status_h(connection_t *c)
-{
- int statusno;
- char statusstring[MAX_STRING_SIZE];
-cp
- if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
- c->name, c->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_STATUS)
- {
- syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
- c->name, c->hostname, status_text[statusno], statusstring);
- }
-
-cp
- return 0;
-}
-
-int send_error(connection_t *c, int err, char *errstring)
-{
-cp
- if(!errstring)
- errstring = strerror(err);
- return send_request(c, "%d %d %s", ERROR, err, errstring);
-}
-
-int error_h(connection_t *c)
-{
- int err;
- char errorstring[MAX_STRING_SIZE];
-cp
- if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
- c->name, c->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_ERROR)
- {
- syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
- c->name, c->hostname, strerror(err), errorstring);
- }
-
- terminate_connection(c, c->status.active);
-cp
- return 0;
-}
-
-int send_termreq(connection_t *c)
-{
-cp
- return send_request(c, "%d", TERMREQ);
-}
-
-int termreq_h(connection_t *c)
-{
-cp
- terminate_connection(c, c->status.active);
-cp
- return 0;
-}
-
-int send_ping(connection_t *c)
-{
- char salt[SALTLEN*2+1];
-cp
- c->status.pinged = 1;
- c->last_ping_time = time(NULL);
- RAND_pseudo_bytes(salt, SALTLEN);
- bin2hex(salt, salt, SALTLEN);
- salt[SALTLEN*2] = '\0';
-cp
- return send_request(c, "%d %s", PING, salt);
-}
-
-int ping_h(connection_t *c)
-{
-cp
- return send_pong(c);
-}
-
-int send_pong(connection_t *c)
-{
- char salt[SALTLEN*2+1];
-cp
- RAND_pseudo_bytes(salt, SALTLEN);
- bin2hex(salt, salt, SALTLEN);
- salt[SALTLEN*2] = '\0';
-cp
- return send_request(c, "%d %s", PONG, salt);
-}
-
-int pong_h(connection_t *c)
-{
-cp
- c->status.pinged = 0;
-cp
- return 0;
-}
-
-/* Key exchange */
-
-int send_key_changed(connection_t *c, node_t *n)
-{
- connection_t *other;
- avl_node_t *node;
-cp
- /* Only send this message if some other daemon requested our key previously.
- This reduces unnecessary key_changed broadcasts.
- */
-
- if(n == myself && !mykeyused)
- return 0;
-
- for(node = connection_tree->head; node; node = node->next)
- {
- other = (connection_t *)node->data;
- if(other != c && other->status.active)
- send_request(other, "%d %s", KEY_CHANGED, n->name);
- }
-cp
- return 0;
-}
-
-int key_changed_h(connection_t *c)
-{
- char name[MAX_STRING_SIZE];
- node_t *n;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
- c->name, c->hostname);
- return -1;
- }
-
- n = lookup_node(name);
-
- if(!n)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
- c->name, c->hostname, name);
- return -1;
- }
-
- n->status.validkey = 0;
- n->status.waitingforkey = 0;
-
- send_key_changed(c, n);
-cp
- return 0;
-}
-
-int send_req_key(connection_t *c, node_t *from, node_t *to)
-{
-cp
- return send_request(c, "%d %s %s", REQ_KEY,
- from->name, to->name);
-}
-
-int req_key_h(connection_t *c)
-{
- char from_name[MAX_STRING_SIZE];
- char to_name[MAX_STRING_SIZE];
- node_t *from, *to;
- char key[MAX_STRING_SIZE];
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
- c->name, c->hostname);
- return -1;
- }
-
- from = lookup_node(from_name);
-
- if(!from)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
- c->name, c->hostname, from_name);
- return -1;
- }
-
- to = lookup_node(to_name);
-
- if(!to)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
- c->name, c->hostname, to_name);
- return -1;
- }
-
- /* Check if this key request is for us */
-
- if(to == myself) /* Yes, send our own key back */
- {
- bin2hex(myself->key, key, myself->keylength);
- key[myself->keylength * 2] = '\0';
- send_ans_key(c, myself, from, key);
- mykeyused = 1;
- }
- else
- {
- if(to->status.validkey) /* Proxy keys */
- {
- bin2hex(to->key, key, to->keylength);
- key[to->keylength * 2] = '\0';
- send_ans_key(c, to, from, key);
- }
- else
- send_req_key(to->nexthop->connection, from, to);
- }
-
-cp
- return 0;
-}
-
-int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
-{
-cp
- return send_request(c, "%d %s %s %s", ANS_KEY,
- from->name, to->name, key);
-}
-
-int ans_key_h(connection_t *c)
-{
- char from_name[MAX_STRING_SIZE];
- char to_name[MAX_STRING_SIZE];
- char key[MAX_STRING_SIZE];
- int keylength;
- node_t *from, *to;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
- c->name, c->hostname);
- return -1;
- }
-
- from = lookup_node(from_name);
-
- if(!from)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
- c->name, c->hostname, from_name);
- return -1;
- }
-
- to = lookup_node(to_name);
-
- if(!to)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
- c->name, c->hostname, to_name);
- return -1;
- }
-
- /* Check correctness of packet key */
-
- keylength = strlen(key);
-
- if(keylength != from->keylength * 2)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
- c->name, c->hostname, from->name, _("invalid key length"));
- return -1;
- }
-
- /* Forward it if necessary */
-
- if(to != myself)
- {
- send_ans_key(to->nexthop->connection, from, to, key);
- }
-
- /* Update our copy of the origin's packet key */
-
- if(from->key)
- free(from->key);
-
- from->key = xstrdup(key);
- keylength /= 2;
- hex2bin(from->key, from->key, keylength);
- from->key[keylength] = '\0';
-
- from->status.validkey = 1;
- from->status.waitingforkey = 0;
-
- flush_queue(from);
-cp
- return 0;
-}
-
-int send_tcppacket(connection_t *c, vpn_packet_t *packet)
-{
- int x;
-cp
- /* Evil hack. */
-
- x = send_request(c, "%d %hd", PACKET, packet->len);
-
- if(x)
- return x;
-cp
- return send_meta(c, packet->data, packet->len);
-}
-
-int tcppacket_h(connection_t *c)
-{
- short int len;
-cp
- if(sscanf(c->buffer, "%*d %hd", &len) != 1)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
- return -1;
- }
-
- /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
-
- c->tcplen = len;
-cp
- return 0;
-}
-
-/* Jumptable for the request handlers */
-
-int (*request_handlers[])(connection_t*) = {
- id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
- status_h, error_h, termreq_h,
- ping_h, pong_h,
- add_node_h, del_node_h,
- add_subnet_h, del_subnet_h,
- add_edge_h, del_edge_h,
- key_changed_h, req_key_h, ans_key_h,
- tcppacket_h,
-};
-
-/* Request names */