-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_id(name)))
- {
- syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
- name, cl->name, cl->hostname);
- free(name);
- return -1;
- }
-
- /* If everything is correct, add the subnet to the list of the owner */
-
- subnet_add(owner, subnet);
-
- /* Tell the rest */
-
- for(p = conn_list; p; p = p->next)
- if(p->status.meta && p->status.active && p!= cl)
- send_add_subnet(p, subnet);
-cp
- return 0;
-}
-
-int send_del_subnet(conn_list_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(conn_list_t *cl)
-{
- char *subnetstr;
- char *name;
- conn_list_t *owner, *p;
- subnet_t *subnet;
-cp
- if(sscanf(cl->buffer, "%*d %as %as", &name, &subnetstr) != 3)
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s)"), cl->name, cl->hostname);
- free(name); free(subnetstr);
- 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);
- free(name); free(subnetstr);
- 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);
- free(name); free(subnetstr);
- 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);
- free(name);
- 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);
- free(name);
- return -1;
- }
-
- /* If everything is correct, delete the subnet from the list of the owner */
-
- subnet_del(subnet);
-
- /* Tell the rest */
-
- for(p = conn_list; p; p = p->next)
- 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(conn_list_t *cl, conn_list_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(conn_list_t *cl)
-{
- conn_list_t *old, *new;
- conn_list_t *p;
-
-cp
- new = new_conn_list();
-
- if(sscanf(cl->buffer, "%*d %as %lx:%d %lx", &new->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(new->name))
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
- free_conn_list(new);
- return -1;
- }
-
- /* Check if somebody tries to add ourself */
-
- if(!strcmp(new->name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->name, cl->hostname);
- sighup = 1;
- free_conn_list(new);
- return 0;
- }
-
- /* Fill in more of the new conn_list structure */
-
- new->hostname = hostlookup(htonl(new->address));
-
- /* Check if the new host already exists in the connnection list */
-
- if((old = lookup_id(new->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, new->name, new->hostname);
- free_conn_list(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 conn_list */
-
- conn_list_add(new);
-
- /* Tell the rest about the new host */
-
- for(p = conn_list; p; p = p->next)
- if(p->status.meta && p->status.active && p!=cl)
- send_add_host(p, new);
-
- /* Fill in rest of conn_list 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(conn_list_t *cl, conn_list_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(conn_list_t *cl)
-{
- char *name;
- ip_t address;
- port_t port;
- long int options;
- conn_list_t *old, *p;
-cp
- if(sscanf(cl->buffer, "%*d %as %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);
- free(name);
- 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);
- free(name);
- 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);
- free(name);
- 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(p = conn_list; p; p = p->next)
- 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(conn_list_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(conn_list_t *cl)
-{
- int statusno;
- char *statusstring;
-cp
- if(sscanf(cl->buffer, "%*d %d %as", &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
- free(statusstring);
- return 0;
-}
-
-int send_error(conn_list_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(conn_list_t *cl)
-{
- int errno;
- char *errorstring;
-cp
- if(sscanf(cl->buffer, "%*d %d %as", &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);
- }
-
- free(errorstring);
- terminate_connection(cl);
-cp
- return 0;
-}
-
-int send_termreq(conn_list_t *cl)
-{
-cp
- return send_request(cl, "%d", TERMREQ);
-}
-
-int termreq_h(conn_list_t *cl)
-{
-cp
- terminate_connection(cl);
-cp
- return 0;
-}
-
-/* Keepalive routines - FIXME: needs a closer look */
-
-int send_ping(conn_list_t *cl)
-{
-cp
- cl->status.pinged = 1;
- cl->last_ping_time = time(NULL);
-cp
- return send_request(cl, "%d", PING);
-}
-
-int ping_h(conn_list_t *cl)
-{
-cp
- return send_pong(cl);
-}
-
-int send_pong(conn_list_t *cl)
-{
-cp
- return send_request(cl, "%d", PONG);
-}
-
-int pong_h(conn_list_t *cl)
-{
-cp
- cl->status.pinged = 0;
-cp
- return 0;
-}
-
-/* Key exchange */
-
-int send_key_changed(conn_list_t *from, conn_list_t *cl)
-{
- conn_list_t *p;
-cp
- for(p = conn_list; p != NULL; p = p->next)
- {
- 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(conn_list_t *cl)
-{
- char *from_id;
- conn_list_t *from;
-cp
- if(sscanf(cl->buffer, "%*d %as", &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);
- free(from_id);
- return -1;
- }
-
- free(from_id);
-
- from->status.validkey = 0;
- from->status.waitingforkey = 0;
-
- send_key_changed(from, cl);
-cp
- return 0;
-}
-
-int send_req_key(conn_list_t *from, conn_list_t *to)
-{
-cp
- return send_request(to->nexthop, "%d %s %s", REQ_KEY,
- from->name, to->name);