2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2022 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
26 #include "control_common.h"
45 #include "compression.h"
47 #include "address_cache.h"
49 #include "ed25519/sha512.h"
52 /* If nonzero, use null ciphers and skip all key exchanges. */
53 bool bypass_security = false;
55 int invitation_lifetime;
56 ecdsa_t *invitation_key = NULL;
58 static bool send_proxyrequest(connection_t *c) {
64 sockaddr2str(&c->address, &host, &port);
65 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
73 size_t reqlen = socks_req_len(proxytype, &c->address);
74 uint8_t *req = alloca(reqlen);
75 c->tcplen = create_socks_req(proxytype, req, &c->address);
76 return c->tcplen ? send_meta(c, req, reqlen) : false;
80 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
88 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
93 bool send_id(connection_t *c) {
94 gettimeofday(&c->start, NULL);
99 if(c->outgoing && !ecdsa_active(c->ecdsa) && !(c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name))) {
102 minor = myself->connection->protocol_minor;
106 if(proxytype && c->outgoing)
107 if(!send_proxyrequest(c)) {
111 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
114 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
117 if(strchr(data, '\n')) {
118 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
122 // Create a new host config file
123 char filename[PATH_MAX];
124 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
126 if(!access(filename, F_OK)) {
127 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
131 FILE *f = fopen(filename, "w");
134 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
138 fprintf(f, "Ed25519PublicKey = %s\n", data);
141 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key successfully received from %s (%s)", c->name, c->hostname);
144 c->node = lookup_node(c->name);
148 c->node = new_node(c->name);
149 c->node->connection = c;
153 if(!c->node->address_cache) {
154 c->node->address_cache = open_address_cache(c->node);
157 add_recent_address(c->node->address_cache, &c->address);
159 // Call invitation-accepted script
161 char *address, *port;
163 environment_init(&env);
164 environment_add(&env, "NODE=%s", c->name);
165 sockaddr2str(&c->address, &address, &port);
166 environment_add(&env, "REMOTEADDRESS=%s", address);
167 environment_add(&env, "NAME=%s", myself->name);
172 execute_script("invitation-accepted", &env);
174 environment_exit(&env);
176 sptps_send_record(&c->sptps, 2, data, 0);
180 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
181 connection_t *c = handle;
187 if(type == 1 && c->status.invitation_used) {
188 return finalize_invitation(c, data, len);
191 if(type != 0 || len != 18 || c->status.invitation_used) {
195 // Recover the filename from the cookie and the key
196 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
197 const size_t hashbuflen = 18 + strlen(fingerprint);
198 char *hashbuf = alloca(hashbuflen);
200 memcpy(hashbuf, data, 18);
201 memcpy(hashbuf + 18, fingerprint, hashbuflen - 18);
202 sha512(hashbuf, hashbuflen, cookie);
203 b64encode_tinc_urlsafe(cookie, cookie, 18);
206 char filename[PATH_MAX], usedname[PATH_MAX];
207 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
208 snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
210 // Atomically rename the invitation file
211 if(rename(filename, usedname)) {
212 if(errno == ENOENT) {
213 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
215 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
221 // Check the timestamp of the invitation
224 if(stat(usedname, &st)) {
225 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
229 if(st.st_mtime + invitation_lifetime < now.tv_sec) {
230 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
234 // Open the renamed file
235 FILE *f = fopen(usedname, "r");
238 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
242 // Read the new node's Name from the file
245 if(!fgets(buf, sizeof(buf), f)) {
246 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
251 size_t buflen = strlen(buf);
253 // Strip whitespace at the end
254 while(buflen && strchr(" \t\r\n", buf[buflen - 1])) {
258 // Split the first line into variable and value
259 len = strcspn(buf, " \t=");
260 char *name = buf + len;
261 name += strspn(name, " \t");
265 name += strspn(name, " \t");
270 // Check that it is a valid Name
271 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name) || !strcmp(name, myself->name)) {
272 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
278 c->name = xstrdup(name);
280 // Send the node the contents of the invitation file
284 while((result = fread(buf, 1, sizeof(buf), f))) {
285 sptps_send_record(&c->sptps, 0, buf, result);
289 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
294 sptps_send_record(&c->sptps, 1, buf, 0);
298 c->status.invitation_used = true;
300 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s successfully sent to %s (%s)", cookie, c->name, c->hostname);
304 bool id_h(connection_t *c, const char *request) {
305 char name[MAX_STRING_SIZE];
307 if(sscanf(request, "%*d " MAX_STRING " %2d.%3d", name, &c->protocol_major, &c->protocol_minor) < 2) {
308 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
313 /* Check if this is a control connection */
315 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
316 c->status.control = true;
317 c->allow_request = CONTROL;
318 c->last_ping_time = now.tv_sec + 3600;
321 c->name = xstrdup("<control>");
327 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
331 if(!invitation_key) {
332 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
336 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
339 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
343 c->status.invitation = true;
344 char *mykey = ecdsa_get_base64_public_key(invitation_key);
354 if(!send_request(c, "%d %s", ACK, mykey)) {
360 c->protocol_minor = 2;
362 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
365 /* Check if identity is a valid name */
367 if(!check_id(name) || !strcmp(name, myself->name)) {
368 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
369 c->hostname, "invalid name");
373 /* If this is an outgoing connection, make sure we are connected to the right host */
376 if(strcmp(c->name, name)) {
377 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
383 c->name = xstrdup(name);
386 /* Check if version matches */
388 if(c->protocol_major != myself->connection->protocol_major) {
389 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
390 c->name, c->hostname, c->protocol_major, c->protocol_minor);
394 if(bypass_security) {
395 if(!c->config_tree) {
396 c->config_tree = create_configuration();
399 c->allow_request = ACK;
409 c->protocol_minor = 0;
412 if(!c->config_tree) {
413 c->config_tree = create_configuration();
415 if(!read_host_config(c->config_tree, c->name, false)) {
416 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
420 if(experimental && !ecdsa_active(c->ecdsa)) {
421 c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name);
424 /* Ignore failures if no key known yet */
427 if(c->protocol_minor && !ecdsa_active(c->ecdsa)) {
428 c->protocol_minor = 1;
431 /* Forbid version rollback for nodes whose Ed25519 key we know */
433 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
434 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
435 c->name, c->hostname, c->protocol_major, c->protocol_minor);
439 c->allow_request = METAKEY;
445 if(c->protocol_minor >= 2) {
446 c->allow_request = ACK;
448 const size_t labellen = 25 + strlen(myself->name) + strlen(c->name);
449 char *label = alloca(labellen);
452 snprintf(label, labellen, "tinc TCP key expansion %s %s", myself->name, c->name);
454 snprintf(label, labellen, "tinc TCP key expansion %s %s", c->name, myself->name);
457 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, labellen, send_meta_sptps, receive_meta_sptps);
459 return send_metakey(c);
463 #ifndef DISABLE_LEGACY
464 static const char *get_cipher_name(cipher_t *cipher) {
465 size_t keylen = cipher_keylength(cipher);
468 return "aes-128-cfb";
469 } else if(keylen <= 24) {
470 return "aes-192-cfb";
472 return "aes-256-cfb";
476 bool send_metakey(connection_t *c) {
477 if(!myself->connection->legacy) {
478 logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
482 rsa_t *rsa = read_rsa_public_key(c->config_tree, c->name);
488 legacy_ctx_t *ctx = new_legacy_ctx(rsa);
490 /* We need to use a stream mode for the meta protocol. Use AES for this,
491 but try to match the key size with the one from the cipher selected
495 const char *cipher_name = get_cipher_name(myself->incipher);
497 if(!init_crypto_by_name(&ctx->out, cipher_name, "sha256")) {
498 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest to %s (%s)", c->name, c->hostname);
499 free_legacy_ctx(ctx);
503 const size_t len = rsa_size(ctx->rsa);
504 const size_t hexkeylen = HEX_SIZE(len);
505 char *key = alloca(len);
506 char *enckey = alloca(len);
507 char *hexkey = alloca(hexkeylen);
509 /* Create a random key */
513 /* The message we send must be smaller than the modulus of the RSA key.
514 By definition, for a key of k bits, the following formula holds:
516 2^(k-1) <= modulus < 2^(k)
518 Where ^ means "to the power of", not "xor".
519 This means that to be sure, we must choose our message < 2^(k-1).
520 This can be done by setting the most significant bit to zero.
525 if(!cipher_set_key_from_rsa(&ctx->out.cipher, key, len, true)) {
526 free_legacy_ctx(ctx);
531 if(debug_level >= DEBUG_SCARY_THINGS) {
532 bin2hex(key, hexkey, len);
533 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
534 memzero(hexkey, hexkeylen);
537 /* Encrypt the random data
539 We do not use one of the PKCS padding schemes here.
540 This is allowed, because we encrypt a totally random string
541 with a length equal to that of the modulus of the RSA key.
544 bool encrypted = rsa_public_encrypt(ctx->rsa, key, len, enckey);
548 free_legacy_ctx(ctx);
549 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
553 free_legacy_ctx(c->legacy);
556 /* Convert the encrypted random data to a hexadecimal formatted string */
558 bin2hex(enckey, hexkey, len);
560 /* Send the meta key */
562 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
563 cipher_get_nid(&c->legacy->out.cipher),
564 digest_get_nid(&c->legacy->out.digest), c->outmaclength,
565 COMPRESS_NONE, hexkey);
567 c->status.encryptout = true;
571 bool metakey_h(connection_t *c, const char *request) {
572 if(!myself->connection->legacy || !c->legacy) {
576 char hexkey[MAX_STRING_SIZE];
578 const size_t len = rsa_size(myself->connection->legacy->rsa);
579 char *enckey = alloca(len);
580 char *key = alloca(len);
582 if(sscanf(request, "%*d %d %d %*d %*d " MAX_STRING, &cipher, &digest, hexkey) != 3) {
583 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
587 if(!cipher || !digest) {
588 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): cipher %d, digest %d", c->name, c->hostname, cipher, digest);
592 /* Convert the challenge from hexadecimal back to binary */
594 size_t inlen = hex2bin(hexkey, enckey, len);
596 /* Check if the length of the meta key is all right */
599 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
603 /* Decrypt the meta key */
605 if(!rsa_private_decrypt(myself->connection->legacy->rsa, enckey, len, key)) {
606 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
610 if(debug_level >= DEBUG_SCARY_THINGS) {
611 bin2hex(key, hexkey, len);
612 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
613 // Hopefully the user knew what he was doing leaking session keys into logs. We'll do the right thing here anyway.
614 memzero(hexkey, HEX_SIZE(len));
617 /* Check and lookup cipher and digest algorithms */
619 if(!init_crypto_by_nid(&c->legacy->in, cipher, digest)) {
621 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest from %s (%s)", c->name, c->hostname);
625 bool key_set = cipher_set_key_from_rsa(&c->legacy->in.cipher, key, len, false);
629 logger(DEBUG_ALWAYS, LOG_ERR, "Error setting RSA key for %s (%s)", c->name, c->hostname);
633 c->status.decryptin = true;
635 c->allow_request = CHALLENGE;
637 return send_challenge(c);
640 bool send_challenge(connection_t *c) {
641 const size_t len = rsa_size(c->legacy->rsa);
642 char *buffer = alloca(len * 2 + 1);
644 c->hischallenge = xrealloc(c->hischallenge, len);
646 /* Copy random data to the buffer */
648 randomize(c->hischallenge, len);
652 bin2hex(c->hischallenge, buffer, len);
654 /* Send the challenge */
656 return send_request(c, "%d %s", CHALLENGE, buffer);
659 bool challenge_h(connection_t *c, const char *request) {
660 if(!myself->connection->legacy) {
664 char buffer[MAX_STRING_SIZE];
665 const size_t len = rsa_size(myself->connection->legacy->rsa);
667 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
668 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
672 /* Check if the length of the challenge is all right */
674 if(strlen(buffer) != (size_t)len * 2) {
675 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
679 c->mychallenge = xrealloc(c->mychallenge, len);
681 /* Convert the challenge from hexadecimal back to binary */
683 hex2bin(buffer, c->mychallenge, len);
685 /* The rest is done by send_chal_reply() */
687 c->allow_request = CHAL_REPLY;
690 return send_chal_reply(c);
696 bool send_chal_reply(connection_t *c) {
697 const size_t len = rsa_size(myself->connection->legacy->rsa);
698 size_t digestlen = digest_length(&c->legacy->in.digest);
699 char *digest = alloca(digestlen * 2 + 1);
701 /* Calculate the hash from the challenge we received */
703 if(!digest_create(&c->legacy->in.digest, c->mychallenge, len, digest)) {
707 free(c->mychallenge);
708 c->mychallenge = NULL;
710 /* Convert the hash to a hexadecimal formatted string */
712 bin2hex(digest, digest, digestlen);
716 return send_request(c, "%d %s", CHAL_REPLY, digest);
719 bool chal_reply_h(connection_t *c, const char *request) {
720 char hishash[MAX_STRING_SIZE];
722 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
723 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
728 /* Convert the hash to binary format */
730 size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
732 /* Check if the length of the hash is all right */
734 if(inlen != digest_length(&c->legacy->out.digest)) {
735 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
740 /* Verify the hash */
742 if(!digest_verify(&c->legacy->out.digest, c->hischallenge, rsa_size(c->legacy->rsa), hishash)) {
743 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
747 /* Identity has now been positively verified.
748 Send an acknowledgement with the rest of the information needed.
751 free(c->hischallenge);
752 c->hischallenge = NULL;
753 c->allow_request = ACK;
762 static bool send_upgrade(connection_t *c) {
763 /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
764 * but doesn't know our key yet. So send it now. */
766 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
772 bool result = send_request(c, "%d %s", ACK, pubkey);
777 bool send_metakey(connection_t *c) {
782 bool metakey_h(connection_t *c, const char *request) {
788 bool send_challenge(connection_t *c) {
793 bool challenge_h(connection_t *c, const char *request) {
799 bool send_chal_reply(connection_t *c) {
804 bool chal_reply_h(connection_t *c, const char *request) {
810 static bool send_upgrade(connection_t *c) {
816 bool send_ack(connection_t *c) {
817 if(c->protocol_minor == 1) {
818 return send_upgrade(c);
821 /* ACK message contains rest of the information the other end needs
822 to create node_t and edge_t structures. */
827 /* Estimate weight */
829 gettimeofday(&now, NULL);
830 c->estimated_weight = (int)((now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000);
832 /* Check some options */
834 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) {
835 c->options |= OPTION_INDIRECT;
838 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) {
839 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
842 if(myself->options & OPTION_PMTU_DISCOVERY && !(c->options & OPTION_TCPONLY)) {
843 c->options |= OPTION_PMTU_DISCOVERY;
846 choice = myself->options & OPTION_CLAMP_MSS;
847 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
850 c->options |= OPTION_CLAMP_MSS;
853 if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) {
854 get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight);
857 return send_request(c, "%d %s %d %x", ACK, myport.udp, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
860 static void send_everything(connection_t *c) {
861 /* Send all known subnets and edges */
863 if(disablebuggypeers) {
866 char pad[MAXBUFSIZE - MAXSIZE];
869 memset(&zeropkt, 0, sizeof(zeropkt));
870 zeropkt.pkt.len = MAXBUFSIZE;
871 send_tcppacket(c, &zeropkt.pkt);
875 for splay_each(subnet_t, s, &myself->subnet_tree) {
876 send_add_subnet(c, s);
882 for splay_each(node_t, n, &node_tree) {
883 for splay_each(subnet_t, s, &n->subnet_tree) {
884 send_add_subnet(c, s);
887 for splay_each(edge_t, e, &n->edge_tree) {
893 static bool upgrade_h(connection_t *c, const char *request) {
894 char pubkey[MAX_STRING_SIZE];
896 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
897 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
901 if(ecdsa_active(c->ecdsa) || (c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name))) {
902 char *knownkey = ecdsa_get_base64_public_key(c->ecdsa);
903 bool different = strcmp(knownkey, pubkey);
907 logger(DEBUG_ALWAYS, LOG_ERR, "Already have an Ed25519 public key from %s (%s) which is different from the one presented now!", c->name, c->hostname);
911 logger(DEBUG_ALWAYS, LOG_INFO, "Already have Ed25519 public key from %s (%s), ignoring.", c->name, c->hostname);
912 c->allow_request = TERMREQ;
913 return send_termreq(c);
916 c->ecdsa = ecdsa_set_base64_public_key(pubkey);
919 logger(DEBUG_ALWAYS, LOG_INFO, "Got bad Ed25519 public key from %s (%s), not upgrading.", c->name, c->hostname);
923 logger(DEBUG_ALWAYS, LOG_INFO, "Got Ed25519 public key from %s (%s), upgrading!", c->name, c->hostname);
924 append_config_file(c->name, "Ed25519PublicKey", pubkey);
925 c->allow_request = TERMREQ;
928 c->outgoing->timeout = 0;
931 return send_termreq(c);
934 bool ack_h(connection_t *c, const char *request) {
935 if(c->protocol_minor == 1) {
936 return upgrade_h(c, request);
939 char hisport[MAX_STRING_SIZE];
945 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
946 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
951 /* Check if we already have a node_t for him */
953 n = lookup_node(c->name);
956 n = new_node(c->name);
960 /* Oh dear, we already have a connection to this node. */
961 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
963 if(n->connection->outgoing) {
965 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
967 c->outgoing = n->connection->outgoing;
970 n->connection->outgoing = NULL;
973 terminate_connection(n->connection, false);
974 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
982 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
983 c->options &= ~OPTION_PMTU_DISCOVERY;
984 options &= ~OPTION_PMTU_DISCOVERY;
987 c->options |= options;
989 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
993 if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
997 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
999 c->options |= OPTION_CLAMP_MSS;
1001 c->options &= ~OPTION_CLAMP_MSS;
1005 /* Activate this connection */
1007 c->allow_request = ALL;
1009 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
1012 /* Send him everything we know */
1016 /* Create an edge_t for this connection */
1018 c->edge = new_edge();
1019 c->edge->from = myself;
1021 sockaddrcpy(&c->edge->address, &c->address);
1022 sockaddr_setport(&c->edge->address, hisport);
1023 sockaddr_t local_sa;
1024 socklen_t local_salen = sizeof(local_sa);
1026 if(getsockname(c->socket, &local_sa.sa, &local_salen) < 0) {
1027 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name);
1029 sockaddr_setport(&local_sa, myport.udp);
1030 c->edge->local_address = local_sa;
1033 c->edge->weight = (weight + c->estimated_weight) / 2;
1034 c->edge->connection = c;
1035 c->edge->options = c->options;
1039 /* Notify everyone of the new edge */
1042 send_add_edge(c, c->edge);
1044 send_add_edge(everyone, c->edge);
1047 /* Run MST and SSSP algorithms */