+cp
+ /* Generate packet encryption key */
+
+ myself->cipher_pkttype = EVP_bf_cbc();
+
+ myself->cipher_pktkeylength = myself->cipher_pkttype->key_len + myself->cipher_pkttype->iv_len;
+
+ myself->cipher_pktkey = (char *)xmalloc(myself->cipher_pktkeylength);
+ RAND_pseudo_bytes(myself->cipher_pktkey, myself->cipher_pktkeylength);
+
+ if(!(cfg = get_config_val(config, config_keyexpire)))
+ keylifetime = 3600;
+ else
+ keylifetime = cfg->data.val;
+
+ keyexpires = time(NULL) + keylifetime;
+cp
+ /* Done */
+
+ myself->status.active = 1;
+ id_add(myself);
+
+ syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
+cp
+ return 0;
+}
+
+RETSIGTYPE
+sigalrm_handler(int a)
+{
+ config_t const *cfg;
+cp
+ cfg = get_config_val(upstreamcfg, config_connectto);
+
+ if(!cfg)
+ {
+ if(upstreamcfg == config)
+ {
+ /* No upstream IP given, we're listen only. */
+ signal(SIGALRM, SIG_IGN);
+ return;
+ }
+ }
+ else
+ {
+ /* We previously tried all the ConnectTo lines. Now wrap back to the first. */
+ cfg = get_config_val(config, config_connectto);
+ }
+
+ while(cfg)
+ {
+ upstreamcfg = cfg->next;
+ if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
+ {
+ signal(SIGALRM, SIG_IGN);
+ return;
+ }
+ cfg = get_config_val(upstreamcfg, config_connectto); /* Or else we try the next ConnectTo line */
+ }
+
+ signal(SIGALRM, sigalrm_handler);
+ upstreamcfg = config;
+ seconds_till_retry += 5;
+ if(seconds_till_retry > MAXTIMEOUT) /* Don't wait more than MAXTIMEOUT seconds. */
+ seconds_till_retry = MAXTIMEOUT;
+ syslog(LOG_ERR, _("Still failed to connect to other, will retry in %d seconds"),
+ seconds_till_retry);
+ alarm(seconds_till_retry);
+cp
+}
+
+/*
+ setup all initial network connections
+*/
+int setup_network_connections(void)
+{
+ config_t const *cfg;
+cp
+ init_connections();
+ init_subnets();
+
+ if((cfg = get_config_val(config, config_pingtimeout)) == NULL)
+ timeout = 60;
+ else
+ {
+ timeout = cfg->data.val;
+ if(timeout < 1)
+ {
+ timeout = 86400;
+ }
+ }
+
+ if(setup_tap_fd() < 0)
+ return -1;
+
+ /* Run tinc-up script to further initialize the tap interface */
+ execute_script("tinc-up");
+
+ if(setup_myself() < 0)
+ return -1;
+
+ if(!(cfg = get_config_val(config, config_connectto)))
+ /* No upstream IP given, we're listen only. */
+ return 0;
+
+ while(cfg)
+ {
+ upstreamcfg = cfg->next;
+ if(!setup_outgoing_connection(cfg->data.ptr)) /* function returns 0 when there are no problems */
+ return 0;
+ cfg = get_config_val(upstreamcfg, config_connectto); /* Or else we try the next ConnectTo line */
+ }
+
+ if(do_detach)
+ {
+ signal(SIGALRM, sigalrm_handler);
+ upstreamcfg = config;
+ seconds_till_retry = MAXTIMEOUT;
+ syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), seconds_till_retry);
+ alarm(seconds_till_retry);
+ }
+ else
+ return -1;