1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * Event handling (using libevent) |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | This program is free software; you can redistribute it and/or modify |
---|
13 | it under the terms of the GNU General Public License as published by |
---|
14 | the Free Software Foundation; either version 2 of the License, or |
---|
15 | (at your option) any later version. |
---|
16 | |
---|
17 | This program is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | GNU General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License with |
---|
23 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
24 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
25 | Suite 330, Boston, MA 02111-1307 USA |
---|
26 | */ |
---|
27 | |
---|
28 | #define BITLBEE_CORE |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <sys/types.h> |
---|
34 | #include "proxy.h" |
---|
35 | |
---|
36 | #include <sys/time.h> |
---|
37 | #include <event.h> |
---|
38 | |
---|
39 | static guint id_next; |
---|
40 | static GHashTable *id_hash; |
---|
41 | static int quitting = 0; |
---|
42 | |
---|
43 | /* Since libevent doesn't handle two event handlers for one fd-condition |
---|
44 | very well (which happens sometimes when BitlBee changes event handlers |
---|
45 | for a combination), let's buid some indexes so we can delete them here |
---|
46 | already, just in time. */ |
---|
47 | static GHashTable *read_hash; |
---|
48 | static GHashTable *write_hash; |
---|
49 | |
---|
50 | struct b_event_data |
---|
51 | { |
---|
52 | guint id; |
---|
53 | struct event evinfo; |
---|
54 | gint timeout; |
---|
55 | b_event_handler function; |
---|
56 | void *data; |
---|
57 | }; |
---|
58 | |
---|
59 | void b_main_init() |
---|
60 | { |
---|
61 | event_init(); |
---|
62 | |
---|
63 | id_next = 1; |
---|
64 | id_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
65 | read_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
66 | write_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
67 | } |
---|
68 | |
---|
69 | void b_main_run() |
---|
70 | { |
---|
71 | event_dispatch(); |
---|
72 | } |
---|
73 | |
---|
74 | void b_main_quit() |
---|
75 | { |
---|
76 | struct timeval tv; |
---|
77 | |
---|
78 | /* libevent sometimes generates events before really quitting, |
---|
79 | we want to stop them. */ |
---|
80 | quitting = 1; |
---|
81 | |
---|
82 | memset( &tv, 0, sizeof( struct timeval ) ); |
---|
83 | event_loopexit( &tv ); |
---|
84 | } |
---|
85 | |
---|
86 | static void b_event_passthrough( int fd, short event, void *data ) |
---|
87 | { |
---|
88 | struct b_event_data *b_ev = data; |
---|
89 | b_input_condition cond = 0; |
---|
90 | int id; |
---|
91 | |
---|
92 | if( fd >= 0 ) |
---|
93 | { |
---|
94 | if( event & EV_READ ) |
---|
95 | cond |= GAIM_INPUT_READ; |
---|
96 | if( event & EV_WRITE ) |
---|
97 | cond |= GAIM_INPUT_WRITE; |
---|
98 | } |
---|
99 | |
---|
100 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
101 | |
---|
102 | /* Since the called function might cancel this handler already |
---|
103 | (which free()s b_ev, we have to remember the ID here. */ |
---|
104 | id = b_ev->id; |
---|
105 | |
---|
106 | if( quitting ) |
---|
107 | { |
---|
108 | b_event_remove( id ); |
---|
109 | return; |
---|
110 | } |
---|
111 | |
---|
112 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
113 | { |
---|
114 | event_debug( "Handler returned FALSE: " ); |
---|
115 | b_event_remove( id ); |
---|
116 | } |
---|
117 | else if( fd == -1 ) |
---|
118 | { |
---|
119 | struct timeval tv; |
---|
120 | |
---|
121 | tv.tv_sec = b_ev->timeout / 1000; |
---|
122 | tv.tv_usec = ( b_ev->timeout % 1000 ) * 1000; |
---|
123 | |
---|
124 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
129 | { |
---|
130 | struct b_event_data *b_ev; |
---|
131 | |
---|
132 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data ); |
---|
133 | |
---|
134 | if( ( condition & GAIM_INPUT_READ && ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) || |
---|
135 | ( condition & GAIM_INPUT_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) ) |
---|
136 | { |
---|
137 | /* We'll stick with this libevent entry, but give it a new BitlBee id. */ |
---|
138 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
139 | |
---|
140 | event_debug( "(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next ); |
---|
141 | |
---|
142 | b_ev->id = id_next++; |
---|
143 | b_ev->function = function; |
---|
144 | b_ev->data = data; |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | GIOCondition out_cond; |
---|
149 | |
---|
150 | event_debug( "(new) = %d\n", id_next ); |
---|
151 | |
---|
152 | b_ev = g_new0( struct b_event_data, 1 ); |
---|
153 | b_ev->id = id_next++; |
---|
154 | b_ev->function = function; |
---|
155 | b_ev->data = data; |
---|
156 | |
---|
157 | out_cond = EV_PERSIST; |
---|
158 | if( condition & GAIM_INPUT_READ ) |
---|
159 | out_cond |= EV_READ; |
---|
160 | if( condition & GAIM_INPUT_WRITE ) |
---|
161 | out_cond |= EV_WRITE; |
---|
162 | |
---|
163 | event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev ); |
---|
164 | event_add( &b_ev->evinfo, NULL ); |
---|
165 | |
---|
166 | if( out_cond & EV_READ ) |
---|
167 | g_hash_table_insert( read_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
168 | if( out_cond & EV_WRITE ) |
---|
169 | g_hash_table_insert( write_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
170 | } |
---|
171 | |
---|
172 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
173 | return b_ev->id; |
---|
174 | } |
---|
175 | |
---|
176 | /* TODO: Persistence for timers! */ |
---|
177 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
178 | { |
---|
179 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
180 | struct timeval tv; |
---|
181 | |
---|
182 | b_ev->id = id_next++; |
---|
183 | b_ev->timeout = timeout; |
---|
184 | b_ev->function = function; |
---|
185 | b_ev->data = data; |
---|
186 | |
---|
187 | tv.tv_sec = timeout / 1000; |
---|
188 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
189 | |
---|
190 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
191 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
192 | |
---|
193 | event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
194 | |
---|
195 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
196 | |
---|
197 | return b_ev->id; |
---|
198 | } |
---|
199 | |
---|
200 | void b_event_remove( gint id ) |
---|
201 | { |
---|
202 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
203 | |
---|
204 | event_debug( "b_event_remove( %d )\n", id ); |
---|
205 | if( b_ev ) |
---|
206 | { |
---|
207 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
208 | if( b_ev->evinfo.ev_fd >= 0 ) |
---|
209 | { |
---|
210 | if( b_ev->evinfo.ev_events & EV_READ ) |
---|
211 | g_hash_table_remove( read_hash, &b_ev->evinfo.ev_fd ); |
---|
212 | if( b_ev->evinfo.ev_events & EV_WRITE ) |
---|
213 | g_hash_table_remove( write_hash, &b_ev->evinfo.ev_fd ); |
---|
214 | } |
---|
215 | |
---|
216 | event_del( &b_ev->evinfo ); |
---|
217 | g_free( b_ev ); |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | event_debug( "Already removed?\n" ); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | void closesocket( int fd ) |
---|
226 | { |
---|
227 | struct b_event_data *b_ev; |
---|
228 | |
---|
229 | /* Since epoll() (the main reason we use libevent) automatically removes sockets from |
---|
230 | the epoll() list when a socket gets closed and some modules have a habit of |
---|
231 | closing sockets before removing event handlers, our and libevent's administration |
---|
232 | get a little bit messed up. So this little function will remove the handlers |
---|
233 | properly before closing a socket. */ |
---|
234 | |
---|
235 | if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) |
---|
236 | { |
---|
237 | event_debug( "Warning: fd %d still had a read event handler when shutting down.\n", fd ); |
---|
238 | b_event_remove( b_ev->id ); |
---|
239 | } |
---|
240 | if( ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) |
---|
241 | { |
---|
242 | event_debug( "Warning: fd %d still had a write event handler when shutting down.\n", fd ); |
---|
243 | b_event_remove( b_ev->id ); |
---|
244 | } |
---|
245 | |
---|
246 | close( fd ); |
---|
247 | } |
---|