[13cc96c] | 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> |
---|
[3f199fc] | 32 | #include <unistd.h> |
---|
[13cc96c] | 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 | |
---|
[782d988] | 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 | |
---|
[13cc96c] | 49 | struct b_event_data |
---|
| 50 | { |
---|
| 51 | guint id; |
---|
| 52 | struct event evinfo; |
---|
[19ac9c5] | 53 | gint timeout; |
---|
[13cc96c] | 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 ); |
---|
[782d988] | 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 ); |
---|
[13cc96c] | 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; |
---|
[fc2ee0f] | 85 | int id; |
---|
[09f8cd1] | 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 | |
---|
[b642f381] | 95 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
| 96 | |
---|
[fc2ee0f] | 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 | |
---|
[13cc96c] | 101 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
[b642f381] | 102 | { |
---|
| 103 | event_debug( "Handler returned FALSE: " ); |
---|
[fc2ee0f] | 104 | b_event_remove( id ); |
---|
[b642f381] | 105 | } |
---|
[19ac9c5] | 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 | } |
---|
[13cc96c] | 115 | } |
---|
| 116 | |
---|
[b642f381] | 117 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
[13cc96c] | 118 | { |
---|
[782d988] | 119 | struct b_event_data *b_ev; |
---|
[13cc96c] | 120 | |
---|
[782d988] | 121 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data ); |
---|
[13cc96c] | 122 | |
---|
[782d988] | 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 | } |
---|
[b642f381] | 160 | |
---|
[13cc96c] | 161 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 162 | return b_ev->id; |
---|
| 163 | } |
---|
| 164 | |
---|
[09f8cd1] | 165 | /* TODO: Persistence for timers! */ |
---|
[13cc96c] | 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 | |
---|
[09f8cd1] | 171 | b_ev->id = id_next++; |
---|
[19ac9c5] | 172 | b_ev->timeout = timeout; |
---|
[13cc96c] | 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 ); |
---|
[09f8cd1] | 180 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
[13cc96c] | 181 | |
---|
[782d988] | 182 | event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
[b642f381] | 183 | |
---|
[13cc96c] | 184 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 185 | |
---|
| 186 | return b_ev->id; |
---|
| 187 | } |
---|
| 188 | |
---|
[b642f381] | 189 | void b_event_remove( gint id ) |
---|
[13cc96c] | 190 | { |
---|
[b642f381] | 191 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
[13cc96c] | 192 | |
---|
[b642f381] | 193 | event_debug( "b_event_remove( %d )\n", id ); |
---|
[13cc96c] | 194 | if( b_ev ) |
---|
| 195 | { |
---|
[09f8cd1] | 196 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
[782d988] | 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 ); |
---|
[13cc96c] | 206 | g_free( b_ev ); |
---|
| 207 | } |
---|
[b642f381] | 208 | else |
---|
| 209 | { |
---|
[782d988] | 210 | event_debug( "Already removed?\n" ); |
---|
[b642f381] | 211 | } |
---|
[13cc96c] | 212 | } |
---|
| 213 | |
---|
[3f199fc] | 214 | void closesocket( int fd ) |
---|
| 215 | { |
---|
| 216 | struct b_event_data *b_ev; |
---|
| 217 | |
---|
| 218 | /* Since epoll() (the main reason we use libevent) automatically removes sockets from |
---|
| 219 | the epoll() list when a socket gets closed and some modules have a habit of |
---|
| 220 | closing sockets before removing event handlers, our and libevent's administration |
---|
| 221 | get a little bit messed up. So this little function will remove the handlers |
---|
| 222 | properly before closing a socket. */ |
---|
| 223 | |
---|
[b8b8c6c] | 224 | if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) |
---|
[df70eafa] | 225 | { |
---|
[5330e3d] | 226 | event_debug( "Warning: fd %d still had a read event handler when shutting down.\n", fd ); |
---|
| 227 | b_event_remove( b_ev->id ); |
---|
| 228 | } |
---|
[b8b8c6c] | 229 | if( ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) |
---|
[5330e3d] | 230 | { |
---|
| 231 | event_debug( "Warning: fd %d still had a write event handler when shutting down.\n", fd ); |
---|
[3f199fc] | 232 | b_event_remove( b_ev->id ); |
---|
[df70eafa] | 233 | } |
---|
[3f199fc] | 234 | |
---|
| 235 | close( fd ); |
---|
| 236 | } |
---|