-int setup_outgoing_connection(char *name)
-{
- connection_t *ncn;
- struct hostent *h;
- config_t const *cfg;
-cp
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Invalid name for outgoing connection"));
- return -1;
- }
-
- ncn = new_connection();
- asprintf(&ncn->name, "%s", name);
-
- if(read_host_config(ncn))
- {
- syslog(LOG_ERR, _("Error reading host configuration file for %s"), ncn->name);
- free_connection(ncn);
- return -1;
- }
-
- if(!(cfg = get_config_val(ncn->config, config_address)))
- {
- syslog(LOG_ERR, _("No address specified for %s"), ncn->name);
- free_connection(ncn);
- return -1;
- }
-
- if(!(h = gethostbyname(cfg->data.ptr)))
- {
- syslog(LOG_ERR, _("Error looking up `%s': %m"), cfg->data.ptr);
- free_connection(ncn);
- return -1;
- }
-
- ncn->address = ntohl(*((ip_t*)(h->h_addr_list[0])));
- ncn->hostname = hostlookup(htonl(ncn->address));
-
- if(setup_outgoing_meta_socket(ncn) < 0)
- {
- syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
- ncn->hostname);
- free_connection(ncn);
- return -1;
- }
-
- ncn->status.outgoing = 1;
- ncn->buffer = xmalloc(MAXBUFSIZE);
- ncn->buflen = 0;
- ncn->last_ping_time = time(NULL);
-
- connection_add(ncn);
-
- send_id(ncn);
-cp
- return 0;
-}
-
-int read_rsa_public_key(connection_t *cl)
-{
- config_t const *cfg;
- FILE *fp;
- char *fname;
- void *result;
-cp
- if(!cl->rsa_key)
- cl->rsa_key = RSA_new();
-
- /* First, check for simple PublicKey statement */
-
- if((cfg = get_config_val(cl->config, config_publickey)))
- {
- BN_hex2bn(&cl->rsa_key->n, cfg->data.ptr);
- BN_hex2bn(&cl->rsa_key->e, "FFFF");
- return 0;
- }
-
- /* Else, check for PublicKeyFile statement and read it */
-
- if((cfg = get_config_val(cl->config, config_publickeyfile)))
- {
- if(is_safe_path(cfg->data.ptr))
- {
- if((fp = fopen(cfg->data.ptr, "r")) == NULL)
- {
- syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
- cfg->data.ptr);
- return -1;
- }
- result = PEM_read_RSAPublicKey(fp, &cl->rsa_key, NULL, NULL);
- fclose(fp);
- if(!result)
- {
- syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
- cfg->data.ptr);
- return -1;
- }
- return 0;
- }
- else
- return -1;
- }
-
- /* Else, check if a harnessed public key is in the config file */
-
- asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
- if((fp = fopen(fname, "r")))
- {
- result = PEM_read_RSAPublicKey(fp, &cl->rsa_key, NULL, NULL);
- fclose(fp);
- free(fname);
- if(result)
- return 0;
- }
-
- free(fname);
-
- /* Nothing worked. */
-
- syslog(LOG_ERR, _("No public key for %s specified!"), cl->name);
-cp
- return -1;
-}
-
-int read_rsa_private_key(void)
-{
- config_t const *cfg;
- FILE *fp;
- void *result;
-cp
- if(!myself->rsa_key)
- myself->rsa_key = RSA_new();
-
- if((cfg = get_config_val(config, config_privatekey)))
- {
- BN_hex2bn(&myself->rsa_key->d, cfg->data.ptr);
- BN_hex2bn(&myself->rsa_key->e, "FFFF");
- }
- else if((cfg = get_config_val(config, config_privatekeyfile)))
- {
- if((fp = fopen(cfg->data.ptr, "r")) == NULL)
- {
- syslog(LOG_ERR, _("Error reading RSA private key file `%s': %m"),
- cfg->data.ptr);
- return -1;
- }
- result = PEM_read_RSAPrivateKey(fp, &myself->rsa_key, NULL, NULL);
- fclose(fp);
- if(!result)
- {
- syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %m"),
- cfg->data.ptr);
- return -1;
- }
- }
- else
- {
- syslog(LOG_ERR, _("No private key for tinc daemon specified!"));
- return -1;
- }
-cp
- return 0;
-}
+static void timeout_handler(int fd, short events, void *event) {
+ splay_node_t *node, *next;
+ connection_t *c;
+ time_t now = time(NULL);
+
+ for(node = connection_tree->head; node; node = next) {
+ next = node->next;
+ c = node->data;
+
+ if(c->status.control)
+ continue;
+
+ if(c->last_ping_time + pingtimeout <= now) {
+ if(c->status.active) {
+ if(c->status.pinged) {
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
+ c->name, c->hostname, (long)now - c->last_ping_time);
+ terminate_connection(c, true);
+ continue;
+ } else if(c->last_ping_time + pinginterval <= now) {
+ send_ping(c);
+ }
+ } else {
+ if(c->status.connecting) {
+ logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
+ c->status.connecting = false;
+ closesocket(c->socket);
+ do_outgoing_connection(c);
+ } else {
+ logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
+ terminate_connection(c, false);
+ continue;
+ }
+ }
+ }
+ }