- 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;
-}
-
-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)
- if(!(cl->options & OPTION_INDIRECT) || from == myself)
- 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;
-
- flush_queue(from);
-cp
- return 0;
-}
-
-int send_tcppacket(connection_t *cl, vpn_packet_t *packet)
-{
- int x;
-
- x = send_request(cl->nexthop, "%d %hd", PACKET, packet->len);
-
- if(x)
- return x;
-
- return send_meta(cl->nexthop, packet->data, packet->len);
-}
-
-int tcppacket_h(connection_t *cl)
-{
- vpn_packet_t packet;
- char *p;
- int todo, x;
-
- if(sscanf(cl->buffer, "%*d %hd", packet.len) != 1)
- {
- syslog(LOG_ERR, _("Got bad PACKET from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Evil hack. */
-
- p = packet.data;
- todo = packet.len;
-
- while(todo)
- {
- x = read(cl->meta_socket, p, todo);
- if(x<0)
- {
- syslog(LOG_ERR, _("Error during reception of PACKET from %s (%s): %m"), cl->name, cl->hostname);
- return -1;
- }
-
- todo -= x;
- p += x;
- }
-
- return receive_packet(cl, &packet);