Use a global list to track outgoing connections.
[oweals/tinc.git] / src / event.c
1 /*
2     event.c -- event queue
3     Copyright (C) 2002-2007 Guus Sliepen <guus@tinc-vpn.org>,
4                   2002-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include "avl_tree.h"
26 #include "event.h"
27 #include "utils.h"
28 #include "xalloc.h"
29
30 avl_tree_t *event_tree;
31 extern time_t now;
32
33 int id;
34
35 static int event_compare(const event_t *a, const event_t *b)
36 {
37         if(a->time > b->time)
38                 return 1;
39
40         if(a->time < b->time)
41                 return -1;
42
43         return a->id - b->id;
44 }
45
46 void init_events(void)
47 {
48         cp();
49
50         event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event);
51 }
52
53 void exit_events(void)
54 {
55         cp();
56
57         avl_delete_tree(event_tree);
58 }
59
60 void expire_events(void)
61 {
62         avl_node_t *node;
63         event_t *event;
64         time_t diff;
65
66         /*
67          * Make all events appear expired by substracting the difference between
68          * the expiration time of the last event and the current time.
69          */
70
71         cp();
72
73         if(!event_tree->tail)
74                 return;
75
76         event = event_tree->tail->data;
77         if(event->time < now)
78                 return;
79
80         diff = 1 + event->time - now;
81         
82         for(node = event_tree->head; node; node = node->next) {
83                 event = node->data;
84                 event->time -= diff;
85         }
86 }
87
88 event_t *new_event(void)
89 {
90         cp();
91
92         return xmalloc_and_zero(sizeof(event_t));
93 }
94
95 void free_event(event_t *event)
96 {
97         cp();
98
99         free(event);
100 }
101
102 void event_add(event_t *event)
103 {
104         cp();
105
106         event->id = ++id;
107         avl_insert(event_tree, event);
108 }
109
110 void event_del(event_t *event)
111 {
112         cp();
113
114         avl_delete(event_tree, event);
115 }
116
117 event_t *get_expired_event(void)
118 {
119         event_t *event;
120
121         cp();
122
123         if(event_tree->head) {
124                 event = event_tree->head->data;
125
126                 if(event->time < now) {
127                         avl_node_t *node = event_tree->head;
128                         avl_unlink_node(event_tree, node);
129                         free(node);
130                         return event;
131                 }
132         }
133
134         return NULL;
135 }