- fd_set fset;
- struct timeval tv;
- int r;
- time_t last_ping_check;
- int t;
- event_t *event;
-cp
- last_ping_check = time(NULL);
-
- srand(time(NULL));
-
- for(;;)
- {
- tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
- tv.tv_usec = 0;
-
- build_fdset(&fset);
-
- if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
- {
- if(errno != EINTR) /* because of a signal */
- {
- syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
- return;
- }
- }
-
- if(r > 0)
- check_network_activity(&fset);
-
- if(do_prune)
- {
- prune_connections();
- do_prune = 0;
- }
-
- if(do_purge)
- {
- purge();
- do_purge = 0;
- }
-
- t = time(NULL);
-
- /* Let's check if everybody is still alive */
-
- if(last_ping_check + pingtimeout < t)
- {
- check_dead_connections();
- last_ping_check = time(NULL);
-
- /* Should we regenerate our key? */
-
- if(keyexpires < t)
- {
- if(debug_lvl >= DEBUG_STATUS)
- syslog(LOG_INFO, _("Regenerating symmetric key"));
-
- RAND_pseudo_bytes(myself->key, myself->keylength);
- send_key_changed(myself->connection, myself);
- keyexpires = time(NULL) + keylifetime;
- }
- }
-
-
- while((event = get_expired_event()))
- {
- event->handler(event->data);
- free(event);
- }
-
- if(sigalrm)
- {
- syslog(LOG_INFO, _("Flushing event queue"));
-
- while(event_tree->head)
- {
- event = (event_t *)event_tree->head->data;
- event->handler(event->data);
- event_del(event);
- }
- sigalrm = 0;
- }
-
- if(sighup)
- {
- syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
- sighup = 0;
- close_network_connections();
- exit_configuration(&config_tree);
-
- if(read_server_config())
- {
- syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
- exit(1);
- }
-
- sleep(5);
-
- if(setup_network_connections())
- return;
-
- continue;
- }
- }
-cp
+ fd_set fset;
+ struct timeval tv;
+ int r, maxfd;
+ time_t last_ping_check, last_config_check;
+ event_t *event;
+
+ cp();
+
+ last_ping_check = now;
+ last_config_check = now;
+ srand(now);
+
+ for(;;) {
+ now = time(NULL);
+
+ tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
+ tv.tv_usec = 0;
+
+ maxfd = build_fdset(&fset);
+
+ r = select(maxfd + 1, &fset, NULL, NULL, &tv);
+
+ if(r < 0) {
+ if(errno != EINTR && errno != EAGAIN) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Error while waiting for input: %s"),
+ strerror(errno));
+ cp_trace();
+ dump_connections();
+ return;
+ }
+
+ continue;
+ }
+
+ check_network_activity(&fset);
+
+ if(do_purge) {
+ purge();
+ do_purge = 0;
+ }
+
+ /* Let's check if everybody is still alive */
+
+ if(last_ping_check + pingtimeout < now) {
+ check_dead_connections();
+ last_ping_check = now;
+
+ if(routing_mode == RMODE_SWITCH)
+ age_mac();
+
+ age_past_requests();
+
+ /* Should we regenerate our key? */
+
+ if(keyexpires < now) {
+ logger(DEBUG_STATUS, LOG_INFO, _("Regenerating symmetric key"));
+
+ RAND_pseudo_bytes(myself->key, myself->keylength);
+ EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
+ send_key_changed(broadcast, myself);
+ keyexpires = now + keylifetime;
+ }
+ }
+
+
+ while((event = get_expired_event())) {
+ event->handler(event->data);
+ free(event);
+ }
+
+ if(sigalrm) {
+ logger(DEBUG_ALWAYS, LOG_INFO, _("Flushing event queue"));
+
+ while(event_tree->head) {
+ event = (event_t *) event_tree->head->data;
+ event->handler(event->data);
+ event_del(event);
+ }
+ sigalrm = 0;
+ }
+
+ if(sighup) {
+ connection_t *c;
+ avl_node_t *node;
+ char *fname;
+ struct stat s;
+
+ sighup = 0;
+
+ /* Reread our own configuration file */
+
+ exit_configuration(&config_tree);
+ init_configuration(&config_tree);
+
+ if(read_server_config()) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Unable to reread configuration file, exitting."));
+ exit(1);
+ }
+
+ /* Close connections to hosts that have a changed or deleted host config file */
+
+ for(node = connection_tree->head; node; node = node->next) {
+ c = (connection_t *) node->data;
+
+ if(c->outgoing) {
+ free(c->outgoing->name);
+ freeaddrinfo(c->outgoing->ai);
+ free(c->outgoing);
+ c->outgoing = NULL;
+ }
+
+ asprintf(&fname, "%s/hosts/%s", confbase, c->name);
+ if(stat(fname, &s) || s.st_mtime > last_config_check)
+ terminate_connection(c, c->status.active);
+ free(fname);
+ }
+
+ last_config_check = now;
+
+ /* Try to make outgoing connections */
+
+ try_outgoing_connections();
+
+ continue;
+ }
+ }