+ return send_challenge(cl);
+}
+
+int send_challenge(conn_list_t *cl)
+{
+ char *buffer;
+ int keylength;
+ int x;
+cp
+ if(cl->chal_hash)
+ free(cl->chal_hash);
+
+ /* Allocate buffers for the challenge and the hash */
+
+ cl->chal_hash = xmalloc(SHA_DIGEST_LEN);
+ keylength = BN_num_bytes(cl->metakey.n);
+ buffer = xmalloc(keylength*2);
+
+ /* Copy random data and the public key to the buffer */
+
+ RAND_bytes(buffer, keylength);
+ BN_bn2bin(cl->metakey.n, buffer+keylength);
+
+ /* Calculate the hash from that */
+
+ SHA1(buffer, keylength*2, cl->chal_hash);
+
+ /* Convert the random data to a hexadecimal formatted string */
+
+ bin2hex(buffer,buffer,keylength);
+ buffer[keylength*2] = '\0';
+
+ /* Send the challenge */
+
+ cl->allow_request = CHAL_REPLY;
+ x = send_request(cl, "%d %s", CHALLENGE, buffer);
+ free(buffer);
+cp
+ return x;
+}
+
+int challenge_h(conn_list_t *cl)
+{
+ char *challenge;
+cp
+ if(sscanf(cl->buffer, "%*d %as", &cl->id, &challenge) != 1)
+ {
+ syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->id, cl->hostname);
+ return -1;
+ }
+
+ /* Rest is done by send_chal_reply() */
+
+ x = send_chal_reply(cl, challenge);
+ free(challenge);
+cp
+ return x;
+}
+
+int send_chal_reply(conn_list_t *cl, char *challenge)
+{
+ char *buffer;
+ int keylength;
+ char *hash;
+ int x;
+cp
+ keylength = BN_num_bytes(myself->meyakey.n);
+
+ /* Check if the length of the challenge is all right */
+
+ if(strlen(challenge) != keylength*2)
+ {
+ syslog(LOG_ERROR, _("Intruder: wrong challenge length from %s (%s)"), cl->id, cl->hostname);
+ return -1;
+ }
+
+ /* Allocate buffers for the challenge and the hash */
+
+ buffer = xmalloc(keylength*2);
+ hash = xmalloc(SHA_DIGEST_LEN*2+1);
+
+ /* Copy the incoming random data and our public key to the buffer */
+
+ hex2bin(challenge, buffer, keylength);
+ BN_bn2bin(myself->metakey.n, buffer+keylength);
+
+ /* Calculate the hash from that */
+
+ SHA1(buffer, keylength*2, hash);
+ free(buffer);
+
+ /* Convert the hash to a hexadecimal formatted string */
+
+ bin2hex(hash,hash,SHA_DIGEST_LEN);
+ hash[SHA_DIGEST_LEN*2] = '\0';
+
+ /* Send the reply */
+
+ if(cl->status.outgoing)
+ cl->allow_resuest = ID;
+ else
+ cl->allow_request = ACK;
+
+ x = send_request(cl, "%d %s", CHAL_REPLY, hash);
+ free(hash);
+cp
+ return x;
+}
+
+int chal_reply_h(conn_list_t *cl)
+{
+ char *hash;
+cp
+ if(sscanf(cl->buffer, "%*d %as", &cl->id, &hash) != 2)
+ {
+ syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->id, cl->hostname);
+ return -1;
+ }
+
+ /* Check if the length of the hash is all right */
+
+ if(strlen(hash) != SHA_DIGEST_LEN*2)
+ {
+ syslog(LOG_ERROR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->id, cl->hostname);
+ return -1;
+ }
+
+ /* Convert the hash to binary format */
+
+ hex2bin(hash, hash, SHA_DIGEST_LEN);
+
+ /* Verify the incoming hash with the calculated hash */
+
+ if{!memcmp(hash, cl->chal_hash, SHA_DIGEST_LEN)}
+ {
+ syslog(LOG_ERROR, _("Intruder: wrong challenge reply from %s (%s)"), cl->id, cl->hostname);
+ return -1;
+ }
+
+ /* Identity has now been positively verified.
+ If we are accepting this new connection, then send our identity,
+ if we are making this connecting, acknowledge.
+ */
+
+ free(hash);
+ free(cl->chal_hash);
+
+cp
+ if(cl->status.outgoing)
+ {
+ cl->allow_request = ACK;
+ return send_ack(cl);
+ }
+ else
+ {
+ cl->allow_request = CHALLENGE;
+ return send_id(cl);
+ }
+}
+
+int send_ack(conn_list_t *cl)
+{
+cp
+ return send_request(cl, "%d", ACK);
+}
+
+int ack_h(conn_list_t *cl)
+{
+ conn_list_t old;
+cp
+ /* Okay, before we active the connection, we check if there is another entry
+ in the connection list with the same vpn_ip. If so, it presumably is an
+ old connection that has timed out but we don't know it yet.
+ */
+
+ while((old = lookup_id(cl->id)))
+ {
+ if(debug_lvl > DEBUG_CONNECTIONS)
+ syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
+ cl->id, old->hostname, cl->hostname);
+ old->status.active = 0;
+ terminate_connection(old);
+ }
+
+ /* Activate this connection */
+
+ cl->allow_request = ALL;
+ cl->status.active = 1;
+
+ if(debug_lvl > DEBUG_CONNECTIONS)
+ syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->id, cl->hostname);
+
+ /* Exchange information about other tinc daemons */
+
+ notify_others(cl, NULL, send_add_host);
+ notify_one(cl);
+
+ upstreamindex = 0;
+
+cp
+ if(cl->status.outgoing)
+ return 0;
+ else
+ return send_ack(cl);
+}
+
+/* Address and subnet information exchange */
+
+int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
+{
+cp
+ return send_meta(cl, "%d %s %d %s", ADD_SUBNET, other->id, subnet->type, net2str(subnet));
+}
+
+int add_subnet_h(conn_list_t *cl)
+{
+}
+
+int send_del_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
+{
+cp
+ return send_meta(cl, "%d %s %d %s", DEL_SUBNET, other->id, subnet->type, net2str(subnet));
+}
+
+int del_subnet_h(conn_list_t *cl)
+{
+}
+
+/* New and closed connections notification */
+
+int send_add_host(conn_list_t *cl, conn_list_t *other)
+{
+cp
+ return send_meta(cl, "%d %lx:%d", ADD_HOST, other->id, other->address, other->port);
+}
+
+int add_host_h(conn_list_t *cl)
+{
+}
+
+int send_del_host(conn_list_t *cl, conn_list_t *other)
+{
+cp
+ return send_meta(cl, "%d %lx:%d", DEL_HOST, other->id, other->address, other->port);
+}
+
+int del_host_h(conn_list_t *cl)
+{
+}
+
+/* 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->id, cl->hostname);
+ return -1;
+ }
+
+ if(debug_lvl > DEBUG_STATUS)
+ {
+ syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"), cl->id, cl->hostname, status_text[statusno], statusstring);
+ }
+
+cp
+ free(statusstring);