- 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->cipher_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);
- 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 *pktkey)
-{
-cp
- return send_request(to->nexthop, "%d %s %s %s", ANS_KEY,
- from->name, to->name, pktkey);
-}
-
-int ans_key_h(conn_list_t *cl)
-{
- char *from_id, *to_id, *pktkey;
- int keylength;
- conn_list_t *from, *to;
-cp
- if(sscanf(cl->buffer, "%*d %as %as %as", &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);
- free(from_id); free(to_id); free(pktkey);
- 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(pktkey);
-
- 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(pktkey);
- return -1;
- }
- keylength /= 2;
- hex2bin(pktkey, pktkey, keylength);
- BF_set_key(cl->cipher_pktkey, keylength, pktkey);
- }
- 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(pktkey);
- return -1;
- }
- send_ans_key(from, to, pktkey);
- }
-
- free(from_id); free(to_id); free(pktkey);
-cp
- return 0;