Do not try to send REQ_KEY or ANS_KEY requests to unreachable nodes.
[tinc] / src / event.c
1 /*
2     event.c -- event queue
3     Copyright (C) 2002-2006 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, NULL);
51 }
52
53 void exit_events(void)
54 {
55         cp();
56
57         avl_delete_tree(event_tree);
58 }
59
60 void flush_events(void)
61 {
62         avl_tree_t *to_flush;
63         event_t *event;
64
65         /*
66          * Events can be inserted from event handlers, so only flush events
67          * already in the priority queue.
68          */
69
70         cp();
71
72         to_flush = event_tree;
73         init_events();
74         while (to_flush->head) {
75                 event = to_flush->head->data;
76                 event->handler(event->data);
77                 avl_delete(to_flush, event);
78         }
79         avl_delete_tree(to_flush);
80 }
81
82 event_t *new_event(void)
83 {
84         cp();
85
86         return xmalloc_and_zero(sizeof(event_t));
87 }
88
89 void free_event(event_t *event)
90 {
91         cp();
92
93         free(event);
94 }
95
96 void event_add(event_t *event)
97 {
98         cp();
99
100         event->id = ++id;
101         avl_insert(event_tree, event);
102 }
103
104 void event_del(event_t *event)
105 {
106         cp();
107
108         avl_delete(event_tree, event);
109 }
110
111 event_t *get_expired_event(void)
112 {
113         event_t *event;
114
115         cp();
116
117         if(event_tree->head) {
118                 event = event_tree->head->data;
119
120                 if(event->time < now) {
121                         event_del(event);
122                         return event;
123                 }
124         }
125
126         return NULL;
127 }