Use a global list to track outgoing connections.
[tinc] / src / event.c
index 4d6431c..359fa42 100644 (file)
@@ -1,7 +1,7 @@
 /*
     event.c -- event queue
-    Copyright (C) 2002-2003 Guus Sliepen <guus@sliepen.eu.org>,
-                  2002-2003 Ivo Timmermans <ivo@o2w.nl>
+    Copyright (C) 2002-2007 Guus Sliepen <guus@tinc-vpn.org>,
+                  2002-2005 Ivo Timmermans
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: event.c,v 1.1.4.11 2003/08/28 21:05:10 guus Exp $
+    $Id$
 */
 
 #include "system.h"
@@ -47,7 +47,7 @@ void init_events(void)
 {
        cp();
 
-       event_tree = avl_alloc_tree((avl_compare_t) event_compare, NULL);
+       event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event);
 }
 
 void exit_events(void)
@@ -57,6 +57,34 @@ void exit_events(void)
        avl_delete_tree(event_tree);
 }
 
+void expire_events(void)
+{
+       avl_node_t *node;
+       event_t *event;
+       time_t diff;
+
+       /*
+        * Make all events appear expired by substracting the difference between
+         * the expiration time of the last event and the current time.
+        */
+
+       cp();
+
+       if(!event_tree->tail)
+               return;
+
+       event = event_tree->tail->data;
+       if(event->time < now)
+               return;
+
+       diff = 1 + event->time - now;
+       
+       for(node = event_tree->head; node; node = node->next) {
+               event = node->data;
+               event->time -= diff;
+       }
+}
+
 event_t *new_event(void)
 {
        cp();
@@ -96,7 +124,9 @@ event_t *get_expired_event(void)
                event = event_tree->head->data;
 
                if(event->time < now) {
-                       avl_delete(event_tree, event);
+                       avl_node_t *node = event_tree->head;
+                       avl_unlink_node(event_tree, node);
+                       free(node);
                        return event;
                }
        }