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"
48 #include "ed25519/sha512.h"
51 /* If nonzero, use null ciphers and skip all key exchanges. */
52 bool bypass_security = false;
54 int invitation_lifetime;
55 ecdsa_t *invitation_key = NULL;
57 static bool send_proxyrequest(connection_t *c) {
63 sockaddr2str(&c->address, &host, &port);
64 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
72 size_t reqlen = socks_req_len(proxytype, &c->address);
73 uint8_t *req = alloca(reqlen);
74 c->tcplen = create_socks_req(proxytype, req, &c->address);
75 return c->tcplen ? send_meta(c, req, reqlen) : false;
79 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
87 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
92 bool send_id(connection_t *c) {
93 gettimeofday(&c->start, NULL);
98 if(c->outgoing && !ecdsa_active(c->ecdsa) && !(c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name))) {
101 minor = myself->connection->protocol_minor;
105 if(proxytype && c->outgoing)
106 if(!send_proxyrequest(c)) {
110 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
113 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
116 if(strchr(data, '\n')) {
117 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
121 // Create a new host config file
122 char filename[PATH_MAX];
123 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
125 if(!access(filename, F_OK)) {
126 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
130 FILE *f = fopen(filename, "w");
133 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
137 fprintf(f, "Ed25519PublicKey = %s\n", data);
140 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key successfully received from %s (%s)", c->name, c->hostname);
142 // Call invitation-accepted script
144 char *address, *port;
146 environment_init(&env);
147 environment_add(&env, "NODE=%s", c->name);
148 sockaddr2str(&c->address, &address, &port);
149 environment_add(&env, "REMOTEADDRESS=%s", address);
150 environment_add(&env, "NAME=%s", myself->name);
155 execute_script("invitation-accepted", &env);
157 environment_exit(&env);
159 sptps_send_record(&c->sptps, 2, data, 0);
163 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
164 connection_t *c = handle;
170 if(type == 1 && c->status.invitation_used) {
171 return finalize_invitation(c, data, len);
174 if(type != 0 || len != 18 || c->status.invitation_used) {
178 // Recover the filename from the cookie and the key
179 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
180 const size_t hashbuflen = 18 + strlen(fingerprint);
181 char *hashbuf = alloca(hashbuflen);
183 memcpy(hashbuf, data, 18);
184 memcpy(hashbuf + 18, fingerprint, hashbuflen - 18);
185 sha512(hashbuf, hashbuflen, cookie);
186 b64encode_tinc_urlsafe(cookie, cookie, 18);
189 char filename[PATH_MAX], usedname[PATH_MAX];
190 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
191 snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
193 // Atomically rename the invitation file
194 if(rename(filename, usedname)) {
195 if(errno == ENOENT) {
196 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
204 // Check the timestamp of the invitation
207 if(stat(usedname, &st)) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
212 if(st.st_mtime + invitation_lifetime < now.tv_sec) {
213 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
217 // Open the renamed file
218 FILE *f = fopen(usedname, "r");
221 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
225 // Read the new node's Name from the file
228 if(!fgets(buf, sizeof(buf), f)) {
229 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
234 size_t buflen = strlen(buf);
236 // Strip whitespace at the end
237 while(buflen && strchr(" \t\r\n", buf[buflen - 1])) {
241 // Split the first line into variable and value
242 len = strcspn(buf, " \t=");
243 char *name = buf + len;
244 name += strspn(name, " \t");
248 name += strspn(name, " \t");
253 // Check that it is a valid Name
254 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name) || !strcmp(name, myself->name)) {
255 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
261 c->name = xstrdup(name);
263 // Send the node the contents of the invitation file
267 while((result = fread(buf, 1, sizeof(buf), f))) {
268 sptps_send_record(&c->sptps, 0, buf, result);
272 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
277 sptps_send_record(&c->sptps, 1, buf, 0);
281 c->status.invitation_used = true;
283 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s successfully sent to %s (%s)", cookie, c->name, c->hostname);
287 bool id_h(connection_t *c, const char *request) {
288 char name[MAX_STRING_SIZE];
290 if(sscanf(request, "%*d " MAX_STRING " %2d.%3d", name, &c->protocol_major, &c->protocol_minor) < 2) {
291 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
296 /* Check if this is a control connection */
298 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
299 c->status.control = true;
300 c->allow_request = CONTROL;
301 c->last_ping_time = now.tv_sec + 3600;
304 c->name = xstrdup("<control>");
310 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
314 if(!invitation_key) {
315 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
319 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
322 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
326 c->status.invitation = true;
327 char *mykey = ecdsa_get_base64_public_key(invitation_key);
337 if(!send_request(c, "%d %s", ACK, mykey)) {
343 c->protocol_minor = 2;
345 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
348 /* Check if identity is a valid name */
350 if(!check_id(name) || !strcmp(name, myself->name)) {
351 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
352 c->hostname, "invalid name");
356 /* If this is an outgoing connection, make sure we are connected to the right host */
359 if(strcmp(c->name, name)) {
360 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
366 c->name = xstrdup(name);
369 /* Check if version matches */
371 if(c->protocol_major != myself->connection->protocol_major) {
372 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
373 c->name, c->hostname, c->protocol_major, c->protocol_minor);
377 if(bypass_security) {
378 if(!c->config_tree) {
379 c->config_tree = create_configuration();
382 c->allow_request = ACK;
392 c->protocol_minor = 0;
395 if(!c->config_tree) {
396 c->config_tree = create_configuration();
398 if(!read_host_config(c->config_tree, c->name, false)) {
399 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
403 if(experimental && !ecdsa_active(c->ecdsa)) {
404 c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name);
407 /* Ignore failures if no key known yet */
410 if(c->protocol_minor && !ecdsa_active(c->ecdsa)) {
411 c->protocol_minor = 1;
414 /* Forbid version rollback for nodes whose Ed25519 key we know */
416 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
417 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
418 c->name, c->hostname, c->protocol_major, c->protocol_minor);
422 c->allow_request = METAKEY;
428 if(c->protocol_minor >= 2) {
429 c->allow_request = ACK;
431 const size_t labellen = 25 + strlen(myself->name) + strlen(c->name);
432 char *label = alloca(labellen);
435 snprintf(label, labellen, "tinc TCP key expansion %s %s", myself->name, c->name);
437 snprintf(label, labellen, "tinc TCP key expansion %s %s", c->name, myself->name);
440 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, labellen, send_meta_sptps, receive_meta_sptps);
442 return send_metakey(c);
446 #ifndef DISABLE_LEGACY
447 static const char *get_cipher_name(cipher_t *cipher) {
448 size_t keylen = cipher_keylength(cipher);
451 return "aes-128-cfb";
452 } else if(keylen <= 24) {
453 return "aes-192-cfb";
455 return "aes-256-cfb";
459 bool send_metakey(connection_t *c) {
460 if(!myself->connection->legacy) {
461 logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
465 rsa_t *rsa = read_rsa_public_key(c->config_tree, c->name);
471 legacy_ctx_t *ctx = new_legacy_ctx(rsa);
473 /* We need to use a stream mode for the meta protocol. Use AES for this,
474 but try to match the key size with the one from the cipher selected
478 const char *cipher_name = get_cipher_name(myself->incipher);
480 if(!init_crypto_by_name(&ctx->out, cipher_name, "sha256")) {
481 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest to %s (%s)", c->name, c->hostname);
482 free_legacy_ctx(ctx);
486 const size_t len = rsa_size(ctx->rsa);
487 const size_t hexkeylen = HEX_SIZE(len);
488 char *key = alloca(len);
489 char *enckey = alloca(len);
490 char *hexkey = alloca(hexkeylen);
492 /* Create a random key */
496 /* The message we send must be smaller than the modulus of the RSA key.
497 By definition, for a key of k bits, the following formula holds:
499 2^(k-1) <= modulus < 2^(k)
501 Where ^ means "to the power of", not "xor".
502 This means that to be sure, we must choose our message < 2^(k-1).
503 This can be done by setting the most significant bit to zero.
508 if(!cipher_set_key_from_rsa(&ctx->out.cipher, key, len, true)) {
509 free_legacy_ctx(ctx);
514 if(debug_level >= DEBUG_SCARY_THINGS) {
515 bin2hex(key, hexkey, len);
516 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
517 memzero(hexkey, hexkeylen);
520 /* Encrypt the random data
522 We do not use one of the PKCS padding schemes here.
523 This is allowed, because we encrypt a totally random string
524 with a length equal to that of the modulus of the RSA key.
527 bool encrypted = rsa_public_encrypt(ctx->rsa, key, len, enckey);
531 free_legacy_ctx(ctx);
532 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
536 free_legacy_ctx(c->legacy);
539 /* Convert the encrypted random data to a hexadecimal formatted string */
541 bin2hex(enckey, hexkey, len);
543 /* Send the meta key */
545 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
546 cipher_get_nid(&c->legacy->out.cipher),
547 digest_get_nid(&c->legacy->out.digest), c->outmaclength,
548 COMPRESS_NONE, hexkey);
550 c->status.encryptout = true;
554 bool metakey_h(connection_t *c, const char *request) {
555 if(!myself->connection->legacy || !c->legacy) {
559 char hexkey[MAX_STRING_SIZE];
561 const size_t len = rsa_size(myself->connection->legacy->rsa);
562 char *enckey = alloca(len);
563 char *key = alloca(len);
565 if(sscanf(request, "%*d %d %d %*d %*d " MAX_STRING, &cipher, &digest, hexkey) != 3) {
566 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
570 if(!cipher || !digest) {
571 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): cipher %d, digest %d", c->name, c->hostname, cipher, digest);
575 /* Convert the challenge from hexadecimal back to binary */
577 size_t inlen = hex2bin(hexkey, enckey, len);
579 /* Check if the length of the meta key is all right */
582 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
586 /* Decrypt the meta key */
588 if(!rsa_private_decrypt(myself->connection->legacy->rsa, enckey, len, key)) {
589 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
593 if(debug_level >= DEBUG_SCARY_THINGS) {
594 bin2hex(key, hexkey, len);
595 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
596 // Hopefully the user knew what he was doing leaking session keys into logs. We'll do the right thing here anyway.
597 memzero(hexkey, HEX_SIZE(len));
600 /* Check and lookup cipher and digest algorithms */
602 if(!init_crypto_by_nid(&c->legacy->in, cipher, digest)) {
604 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest from %s (%s)", c->name, c->hostname);
608 bool key_set = cipher_set_key_from_rsa(&c->legacy->in.cipher, key, len, false);
612 logger(DEBUG_ALWAYS, LOG_ERR, "Error setting RSA key for %s (%s)", c->name, c->hostname);
616 c->status.decryptin = true;
618 c->allow_request = CHALLENGE;
620 return send_challenge(c);
623 bool send_challenge(connection_t *c) {
624 const size_t len = rsa_size(c->legacy->rsa);
625 char *buffer = alloca(len * 2 + 1);
627 c->hischallenge = xrealloc(c->hischallenge, len);
629 /* Copy random data to the buffer */
631 randomize(c->hischallenge, len);
635 bin2hex(c->hischallenge, buffer, len);
637 /* Send the challenge */
639 return send_request(c, "%d %s", CHALLENGE, buffer);
642 bool challenge_h(connection_t *c, const char *request) {
643 if(!myself->connection->legacy) {
647 char buffer[MAX_STRING_SIZE];
648 const size_t len = rsa_size(myself->connection->legacy->rsa);
650 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
651 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
655 /* Check if the length of the challenge is all right */
657 if(strlen(buffer) != (size_t)len * 2) {
658 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
662 c->mychallenge = xrealloc(c->mychallenge, len);
664 /* Convert the challenge from hexadecimal back to binary */
666 hex2bin(buffer, c->mychallenge, len);
668 /* The rest is done by send_chal_reply() */
670 c->allow_request = CHAL_REPLY;
673 return send_chal_reply(c);
679 bool send_chal_reply(connection_t *c) {
680 const size_t len = rsa_size(myself->connection->legacy->rsa);
681 size_t digestlen = digest_length(&c->legacy->in.digest);
682 char *digest = alloca(digestlen * 2 + 1);
684 /* Calculate the hash from the challenge we received */
686 if(!digest_create(&c->legacy->in.digest, c->mychallenge, len, digest)) {
690 free(c->mychallenge);
691 c->mychallenge = NULL;
693 /* Convert the hash to a hexadecimal formatted string */
695 bin2hex(digest, digest, digestlen);
699 return send_request(c, "%d %s", CHAL_REPLY, digest);
702 bool chal_reply_h(connection_t *c, const char *request) {
703 char hishash[MAX_STRING_SIZE];
705 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
706 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
711 /* Convert the hash to binary format */
713 size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
715 /* Check if the length of the hash is all right */
717 if(inlen != digest_length(&c->legacy->out.digest)) {
718 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
723 /* Verify the hash */
725 if(!digest_verify(&c->legacy->out.digest, c->hischallenge, rsa_size(c->legacy->rsa), hishash)) {
726 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
730 /* Identity has now been positively verified.
731 Send an acknowledgement with the rest of the information needed.
734 free(c->hischallenge);
735 c->hischallenge = NULL;
736 c->allow_request = ACK;
745 static bool send_upgrade(connection_t *c) {
746 /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
747 * but doesn't know our key yet. So send it now. */
749 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
755 bool result = send_request(c, "%d %s", ACK, pubkey);
760 bool send_metakey(connection_t *c) {
765 bool metakey_h(connection_t *c, const char *request) {
771 bool send_challenge(connection_t *c) {
776 bool challenge_h(connection_t *c, const char *request) {
782 bool send_chal_reply(connection_t *c) {
787 bool chal_reply_h(connection_t *c, const char *request) {
793 static bool send_upgrade(connection_t *c) {
799 bool send_ack(connection_t *c) {
800 if(c->protocol_minor == 1) {
801 return send_upgrade(c);
804 /* ACK message contains rest of the information the other end needs
805 to create node_t and edge_t structures. */
810 /* Estimate weight */
812 gettimeofday(&now, NULL);
813 c->estimated_weight = (int)((now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000);
815 /* Check some options */
817 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) {
818 c->options |= OPTION_INDIRECT;
821 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) {
822 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
825 if(myself->options & OPTION_PMTU_DISCOVERY && !(c->options & OPTION_TCPONLY)) {
826 c->options |= OPTION_PMTU_DISCOVERY;
829 choice = myself->options & OPTION_CLAMP_MSS;
830 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
833 c->options |= OPTION_CLAMP_MSS;
836 if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) {
837 get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight);
840 return send_request(c, "%d %s %d %x", ACK, myport.udp, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
843 static void send_everything(connection_t *c) {
844 /* Send all known subnets and edges */
846 if(disablebuggypeers) {
849 char pad[MAXBUFSIZE - MAXSIZE];
852 memset(&zeropkt, 0, sizeof(zeropkt));
853 zeropkt.pkt.len = MAXBUFSIZE;
854 send_tcppacket(c, &zeropkt.pkt);
858 for splay_each(subnet_t, s, &myself->subnet_tree) {
859 send_add_subnet(c, s);
865 for splay_each(node_t, n, &node_tree) {
866 for splay_each(subnet_t, s, &n->subnet_tree) {
867 send_add_subnet(c, s);
870 for splay_each(edge_t, e, &n->edge_tree) {
876 static bool upgrade_h(connection_t *c, const char *request) {
877 char pubkey[MAX_STRING_SIZE];
879 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
880 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
884 if(ecdsa_active(c->ecdsa) || (c->ecdsa = read_ecdsa_public_key(&c->config_tree, c->name))) {
885 char *knownkey = ecdsa_get_base64_public_key(c->ecdsa);
886 bool different = strcmp(knownkey, pubkey);
890 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);
894 logger(DEBUG_ALWAYS, LOG_INFO, "Already have Ed25519 public key from %s (%s), ignoring.", c->name, c->hostname);
895 c->allow_request = TERMREQ;
896 return send_termreq(c);
899 c->ecdsa = ecdsa_set_base64_public_key(pubkey);
902 logger(DEBUG_ALWAYS, LOG_INFO, "Got bad Ed25519 public key from %s (%s), not upgrading.", c->name, c->hostname);
906 logger(DEBUG_ALWAYS, LOG_INFO, "Got Ed25519 public key from %s (%s), upgrading!", c->name, c->hostname);
907 append_config_file(c->name, "Ed25519PublicKey", pubkey);
908 c->allow_request = TERMREQ;
911 c->outgoing->timeout = 0;
914 return send_termreq(c);
917 bool ack_h(connection_t *c, const char *request) {
918 if(c->protocol_minor == 1) {
919 return upgrade_h(c, request);
922 char hisport[MAX_STRING_SIZE];
928 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
929 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
934 /* Check if we already have a node_t for him */
936 n = lookup_node(c->name);
940 n->name = xstrdup(c->name);
944 /* Oh dear, we already have a connection to this node. */
945 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
947 if(n->connection->outgoing) {
949 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
951 c->outgoing = n->connection->outgoing;
954 n->connection->outgoing = NULL;
957 terminate_connection(n->connection, false);
958 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
966 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
967 c->options &= ~OPTION_PMTU_DISCOVERY;
968 options &= ~OPTION_PMTU_DISCOVERY;
971 c->options |= options;
973 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
977 if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
981 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
983 c->options |= OPTION_CLAMP_MSS;
985 c->options &= ~OPTION_CLAMP_MSS;
989 /* Activate this connection */
991 c->allow_request = ALL;
993 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
996 /* Send him everything we know */
1000 /* Create an edge_t for this connection */
1002 c->edge = new_edge();
1003 c->edge->from = myself;
1005 sockaddrcpy(&c->edge->address, &c->address);
1006 sockaddr_setport(&c->edge->address, hisport);
1007 sockaddr_t local_sa;
1008 socklen_t local_salen = sizeof(local_sa);
1010 if(getsockname(c->socket, &local_sa.sa, &local_salen) < 0) {
1011 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name);
1013 sockaddr_setport(&local_sa, myport.udp);
1014 c->edge->local_address = local_sa;
1017 c->edge->weight = (weight + c->estimated_weight) / 2;
1018 c->edge->connection = c;
1019 c->edge->options = c->options;
1023 /* Notify everyone of the new edge */
1026 send_add_edge(c, c->edge);
1028 send_add_edge(everyone, c->edge);
1031 /* Run MST and SSSP algorithms */