- fd_set fset;
- struct timeval tv;
- int r;
- time_t last_ping_check;
- int t;
- event_t *event;
-cp
- last_ping_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;
-
- 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;
- }
-
- /* 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_ROUTER)
- age_mac();
-
- /* Should we regenerate our key? */
-
- if(keyexpires < now)
- {
- 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 = now + 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)
- {
- sighup = 0;
- close_network_connections();
- exit_configuration(&config_tree);
-
- syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
- sleep(5);
-
- init_configuration(&config_tree);
-
- if(read_server_config())
- {
- syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
- exit(1);
- }
-
- if(setup_network_connections())
- return;
-
- continue;
- }
- }
-cp
+ fd_set readset, writeset;
+ struct timeval tv;
+ int r, maxfd;
+ time_t last_ping_check, last_config_check, last_graph_dump;
+ event_t *event;
+
+ cp();
+
+ last_ping_check = now;
+ last_config_check = now;
+ last_graph_dump = now;
+
+ srand(now);
+
+ running = true;
+
+ while(running) {
+ now = time(NULL);
+
+ // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ maxfd = build_fdset(&readset, &writeset);
+
+ r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
+
+ if(r < 0) {
+ if(errno != EINTR && errno != EAGAIN) {
+ logger(LOG_ERR, _("Error while waiting for input: %s"),
+ strerror(errno));
+ cp_trace();
+ dump_connections();
+ return 1;
+ }
+
+ continue;
+ }
+
+ check_network_activity(&readset, &writeset);
+
+ if(do_purge) {
+ purge();
+ do_purge = false;
+ }
+
+ /* 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_subnets();
+
+ age_past_requests();
+
+ /* Should we regenerate our key? */
+
+ if(keyexpires < now) {
+ ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
+
+ RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
+ if(myself->cipher)
+ EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)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(event);
+ }
+
+ if(sigalrm) {
+ logger(LOG_INFO, _("Flushing event queue"));
+ flush_events();
+ sigalrm = false;
+ }
+
+ if(sighup) {
+ connection_t *c;
+ avl_node_t *node;
+ char *fname;
+ struct stat s;
+
+ sighup = false;
+
+ /* Reread our own configuration file */
+
+ exit_configuration(&config_tree);
+ init_configuration(&config_tree);
+
+ if(!read_server_config()) {
+ logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
+ return 1;
+ }
+
+ /* Close connections to hosts that have a changed or deleted host config file */
+
+ for(node = connection_tree->head; node; node = node->next) {
+ c = node->data;
+
+ if(c->outgoing) {
+ free(c->outgoing->name);
+ if(c->outgoing->ai)
+ 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();
+ }
+
+ /* Dump graph if wanted every 60 seconds*/
+
+ if(last_graph_dump + 60 < now) {
+ dump_graph();
+ last_graph_dump = now;
+ }
+ }
+
+ return 0;