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 | |
---|
42 | /* Since libevent doesn't handle two event handlers for one fd-condition |
---|
43 | very well (which happens sometimes when BitlBee changes event handlers |
---|
44 | for a combination), let's buid some indexes so we can delete them here |
---|
45 | already, just in time. */ |
---|
46 | static GHashTable *read_hash; |
---|
47 | static GHashTable *write_hash; |
---|
48 | |
---|
49 | struct b_event_data |
---|
50 | { |
---|
51 | guint id; |
---|
52 | struct event evinfo; |
---|
53 | gint timeout; |
---|
54 | b_event_handler function; |
---|
55 | void *data; |
---|
56 | }; |
---|
57 | |
---|
58 | void b_main_init() |
---|
59 | { |
---|
60 | event_init(); |
---|
61 | |
---|
62 | id_next = 1; |
---|
63 | id_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
64 | read_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
65 | write_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
66 | } |
---|
67 | |
---|
68 | void b_main_run() |
---|
69 | { |
---|
70 | event_dispatch(); |
---|
71 | } |
---|
72 | |
---|
73 | void b_main_quit() |
---|
74 | { |
---|
75 | struct timeval tv; |
---|
76 | |
---|
77 | memset( &tv, 0, sizeof( struct timeval ) ); |
---|
78 | event_loopexit( &tv ); |
---|
79 | } |
---|
80 | |
---|
81 | static void b_event_passthrough( int fd, short event, void *data ) |
---|
82 | { |
---|
83 | struct b_event_data *b_ev = data; |
---|
84 | b_input_condition cond = 0; |
---|
85 | int id; |
---|
86 | |
---|
87 | if( fd >= 0 ) |
---|
88 | { |
---|
89 | if( event & EV_READ ) |
---|
90 | cond |= GAIM_INPUT_READ; |
---|
91 | if( event & EV_WRITE ) |
---|
92 | cond |= GAIM_INPUT_WRITE; |
---|
93 | } |
---|
94 | |
---|
95 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
96 | |
---|
97 | /* Since the called function might cancel this handler already |
---|
98 | (which free()s b_ev, we have to remember the ID here. */ |
---|
99 | id = b_ev->id; |
---|
100 | |
---|
101 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
102 | { |
---|
103 | event_debug( "Handler returned FALSE: " ); |
---|
104 | b_event_remove( id ); |
---|
105 | } |
---|
106 | else if( fd == -1 ) |
---|
107 | { |
---|
108 | struct timeval tv; |
---|
109 | |
---|
110 | tv.tv_sec = b_ev->timeout / 1000; |
---|
111 | tv.tv_usec = ( b_ev->timeout % 1000 ) * 1000; |
---|
112 | |
---|
113 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
118 | { |
---|
119 | struct b_event_data *b_ev; |
---|
120 | |
---|
121 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data ); |
---|
122 | |
---|
123 | if( ( condition & GAIM_INPUT_READ && ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) || |
---|
124 | ( condition & GAIM_INPUT_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) ) |
---|
125 | { |
---|
126 | /* We'll stick with this libevent entry, but give it a new BitlBee id. */ |
---|
127 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
128 | |
---|
129 | event_debug( "(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next ); |
---|
130 | |
---|
131 | b_ev->id = id_next++; |
---|
132 | b_ev->function = function; |
---|
133 | b_ev->data = data; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | GIOCondition out_cond; |
---|
138 | |
---|
139 | event_debug( "(new) = %d\n", id_next ); |
---|
140 | |
---|
141 | b_ev = g_new0( struct b_event_data, 1 ); |
---|
142 | b_ev->id = id_next++; |
---|
143 | b_ev->function = function; |
---|
144 | b_ev->data = data; |
---|
145 | |
---|
146 | out_cond = EV_PERSIST; |
---|
147 | if( condition & GAIM_INPUT_READ ) |
---|
148 | out_cond |= EV_READ; |
---|
149 | if( condition & GAIM_INPUT_WRITE ) |
---|
150 | out_cond |= EV_WRITE; |
---|
151 | |
---|
152 | event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev ); |
---|
153 | event_add( &b_ev->evinfo, NULL ); |
---|
154 | |
---|
155 | if( out_cond & EV_READ ) |
---|
156 | g_hash_table_insert( read_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
157 | if( out_cond & EV_WRITE ) |
---|
158 | g_hash_table_insert( write_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
159 | } |
---|
160 | |
---|
161 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
162 | return b_ev->id; |
---|
163 | } |
---|
164 | |
---|
165 | /* TODO: Persistence for timers! */ |
---|
166 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
167 | { |
---|
168 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
169 | struct timeval tv; |
---|
170 | |
---|
171 | b_ev->id = id_next++; |
---|
172 | b_ev->timeout = timeout; |
---|
173 | b_ev->function = function; |
---|
174 | b_ev->data = data; |
---|
175 | |
---|
176 | tv.tv_sec = timeout / 1000; |
---|
177 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
178 | |
---|
179 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
180 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
181 | |
---|
182 | event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
183 | |
---|
184 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
185 | |
---|
186 | return b_ev->id; |
---|
187 | } |
---|
188 | |
---|
189 | void b_event_remove( gint id ) |
---|
190 | { |
---|
191 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
192 | |
---|
193 | event_debug( "b_event_remove( %d )\n", id ); |
---|
194 | if( b_ev ) |
---|
195 | { |
---|
196 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
197 | if( b_ev->evinfo.ev_fd >= 0 ) |
---|
198 | { |
---|
199 | if( b_ev->evinfo.ev_events & EV_READ ) |
---|
200 | g_hash_table_remove( read_hash, &b_ev->evinfo.ev_fd ); |
---|
201 | if( b_ev->evinfo.ev_events & EV_WRITE ) |
---|
202 | g_hash_table_remove( write_hash, &b_ev->evinfo.ev_fd ); |
---|
203 | } |
---|
204 | |
---|
205 | event_del( &b_ev->evinfo ); |
---|
206 | g_free( b_ev ); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | event_debug( "Already removed?\n" ); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | gboolean b_event_remove_by_data( gpointer data ) |
---|
215 | { |
---|
216 | /* FIXME! */ |
---|
217 | event_debug( "FALSE!\n" ); |
---|
218 | return FALSE; |
---|
219 | } |
---|
220 | |
---|
221 | void closesocket( int fd ) |
---|
222 | { |
---|
223 | struct b_event_data *b_ev; |
---|
224 | |
---|
225 | /* Since epoll() (the main reason we use libevent) automatically removes sockets from |
---|
226 | the epoll() list when a socket gets closed and some modules have a habit of |
---|
227 | closing sockets before removing event handlers, our and libevent's administration |
---|
228 | get a little bit messed up. So this little function will remove the handlers |
---|
229 | properly before closing a socket. */ |
---|
230 | |
---|
231 | if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) || ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) |
---|
232 | b_event_remove( b_ev->id ); |
---|
233 | |
---|
234 | close( fd ); |
---|
235 | } |
---|