- /* 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, add the subnet to the list of the owner */
-cp
- return subnet_del(owner, subnet);
-}
-
-/* New and closed connections notification */
-
-int send_add_host(conn_list_t *cl, conn_list_t *other)
-{
- char *optstr;
- int x;
-cp
- x = send_request(cl, "%d %s %s %lx:%d %s", ADD_HOST,
- myself->name, other->name, other->real_ip, other->port, optstr = opt2str(other->options));
- free(optstr);
-cp
- return x;
-}
-
-int add_host_h(conn_list_t *cl)
-{
- char *optstr;
- char *sender;
- conn_list_t *old, *new, *hisuplink;
-cp
- new = new_conn_list();
-
- if(sscanf(cl->buffer, "%*d %as %as %lx:%d %as", &sender, &new->name, &new->address, &new->port, &optstr) != 5)
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if option string is valid */
-
- if((new->options = str2opt(optstr)) == -1)
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid option string"), cl->name, cl->hostname);
- free(optstr); free(sender);
- return -1;
- }
-
- free(optstr);
-
- /* Check if identity is a valid name */
-
- if(!check_id(new->name) || !check_id(sender))
- {
- syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
- free(sender);
- 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(sender);
- return 0;
- }
-
- /* We got an ADD_HOST from ourself!? */
-
- if(!strcmp(sender, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) from ourself, restarting"), cl->name, cl->hostname);
- sighup = 1;
- free(sender);
- return 0;
- }
-
- /* Lookup his uplink */
-
- if(!(new->hisuplink = lookup_id(sender))
- {
- syslog(LOG_ERR, _("Got ADD_HOST from %s (%s) with origin %s which is not in our connection list"),
- sender, cl->name, cl->hostname);
- free(sender);
- return -1;
- }
-
- free(sender);
-
- /* Fill in more of the new conn_list structure */
-
- new->hostname = hostlookup(htonl(new->real_ip));
-
- /* Check if the new host already exists in the connnection list */
-
- if((old = lookup_id(new->name)))
- {
- if((new->real_ip == old->real_ip) && (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);
- return 0;
- }
- else
- {
- if(debug_lvl > DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
- old->name, old->hostname);
- old->status.active = 0;
- terminate_connection(old);
- }
- }
-
- /* Fill in rest of conn_list structure */
-
- new->myuplink = cl;
- new->status.active = 1;
-
- /* Hook it up into the conn_list */
-
- conn_list_add(conn_list, new);
-
- /* Tell the rest about the new host */
-
- notify_others(new, cl, send_add_host);
-
-cp
- return 0;
-}
-
-int send_del_host(conn_list_t *cl, conn_list_t *other)
-{
- char *optstr;
- int x;
-cp
- x = send_request(cl, "%d %s %s %lx:%d %s", DEL_HOST,
- myself->name, other->name, other->real_ip, other->port, optstr = opt2str(other->options));
- free(optstr);
-cp
- return x;
-}
-
-int del_host_h(conn_list_t *cl)
-{
- char *name;
- char *sender;
- char *opstr;
- ip_t address;
- port_t port;
- int options;
- conn_list_t *old, *hisuplink;
-
-cp
- if(sscanf(cl->buffer, "%*d %as %as %lx:%d %as", &sender, &name, &address, &port, &optstr) != 5)
- {
- syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
- cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if option string is valid */
-
- if((options = str2opt(optstr)) == -1)
- {
- syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s): invalid option string"), cl->name, cl->hostname);
- free(optstr); free(sender); free(name);
- return -1;
- }
-
- free(optstr);
-
- /* Check if identity is a valid name */
-
- if(!check_id(name) || !check_id(sender))
- {
- syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s): invalid identity name"), cl->name, cl->hostname);
- free(name); free(sender);
- 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); free(sender);
- sighup = 1;
- return 0;
- }
-
- /* We got an ADD_HOST from ourself!? */
-
- if(!strcmp(sender, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) from ourself, restarting"), cl->name, cl->hostname);
- sighup = 1;
- free(name); free(sender);
- return 0;
- }
-
- /* Lookup his uplink */
-
- if(!(hisuplink = lookup_id(sender))
- {
- syslog(LOG_ERR, _("Got DEL_HOST from %s (%s) with origin %s which is not in our connection list"),
- cl->name, cl->hostname, sender);
- free(name); free(sender);
- return -1;
- }
-
- free(sender);
-
- /* 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 || hisuplink!=old->hisuplink || cl!=old->myuplink)
- {
- 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.termreq = 1;
- old->status.active = 0;
-
- terminate_connection(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);
- cl->status.termreq = 1;
- 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
- cl->status.termreq = 1;
- terminate_connection(cl);
-cp
- return 0;
-}
-
-/* Keepalive routines - FIXME: needs a closer look */
-
-int send_ping(conn_list_t *cl)
-{
- cl->status.pinged = 1;
-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.got_pong = 1;
-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);
-}
-
-int req_key_h(conn_list_t *cl)
-{
- char *from_id, *to_id;
- conn_list_t *from, *to;
-cp
- if(sscanf(cl->buffer, "%*d %as %as", &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);
- free(from_id); free(to_id);
- return -1;
- }
-
- /* Check if this key request is for us */
-
- if(!strcmp(to_id, myself->name))
- {
- send_ans_key(myself, from, myself->datakey->key);
- }
- 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);
- free(from_id); free(to_id);
- return -1;
- }
- send_req_key(from, to);
- }
-
- free(from_id); free(to_id);
-cp
- return 0;
-}
-
-int send_ans_key(conn_list_t *from, conn_list_t *to, char *datakey)
-{
-cp
- return send_request(to->nexthop, "%d %s %s %s", ANS_KEY,
- from->name, to->name, datakey);
-}
-
-int ans_key_h(conn_list_t *cl)
-{
- char *from_id, *to_id, *datakey;
- int keylength;
- conn_list_t *from, *to;
-cp
- if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 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);
- free(from_id); free(to_id); free(datakey);
- return -1;
- }
-
- /* Check if this key request is for us */
-
- if(!strcmp(to_id, myself->name))
- {
- /* It is for us, convert it to binary and set the key with it. */
-
- keylength = strlen(datakey);
-
- if((keylength%2) || (keylength <= 0))
- {
- syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"),
- cl->name, cl->hostname, from->name);
- free(from_id); free(to_id); free(datakey);
- return -1;
- }
- keylength /= 2;
- hex2bin(datakey, datakey, keylength);
- BF_set_key(cl->datakey, keylength, datakey);
- }
- else
- {
- 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);
- free(from_id); free(to_id); free(datakey);
- return -1;
- }
- send_ans_key(from, to, datakey);
- }
-
- free(from_id); free(to_id); free(datakey);
-cp
- return 0;
-}