[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; |
---|
[cdca30b] | 41 | static int quitting = 0; |
---|
[13cc96c] | 42 | |
---|
[782d988] | 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 | |
---|
[13cc96c] | 50 | struct b_event_data |
---|
| 51 | { |
---|
| 52 | guint id; |
---|
| 53 | struct event evinfo; |
---|
[19ac9c5] | 54 | gint timeout; |
---|
[13cc96c] | 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 ); |
---|
[782d988] | 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 ); |
---|
[13cc96c] | 67 | } |
---|
| 68 | |
---|
| 69 | void b_main_run() |
---|
| 70 | { |
---|
| 71 | event_dispatch(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void b_main_quit() |
---|
| 75 | { |
---|
| 76 | struct timeval tv; |
---|
| 77 | |
---|
[cdca30b] | 78 | /* libevent sometimes generates events before really quitting, |
---|
| 79 | we want to stop them. */ |
---|
| 80 | quitting = 1; |
---|
| 81 | |
---|
[13cc96c] | 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; |
---|
[fc2ee0f] | 90 | int id; |
---|
[09f8cd1] | 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 | |
---|
[b642f381] | 100 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
| 101 | |
---|
[fc2ee0f] | 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 | |
---|
[cdca30b] | 106 | if( quitting ) |
---|
| 107 | { |
---|
| 108 | b_event_remove( id ); |
---|
| 109 | return; |
---|
| 110 | } |
---|
| 111 | |
---|
[13cc96c] | 112 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
[b642f381] | 113 | { |
---|
| 114 | event_debug( "Handler returned FALSE: " ); |
---|
[fc2ee0f] | 115 | b_event_remove( id ); |
---|
[b642f381] | 116 | } |
---|
[19ac9c5] | 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 | } |
---|
[13cc96c] | 126 | } |
---|
| 127 | |
---|
[b642f381] | 128 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
[13cc96c] | 129 | { |
---|
[782d988] | 130 | struct b_event_data *b_ev; |
---|
[13cc96c] | 131 | |
---|
[782d988] | 132 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data ); |
---|
[13cc96c] | 133 | |
---|
[782d988] | 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 | } |
---|
[b642f381] | 171 | |
---|
[13cc96c] | 172 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 173 | return b_ev->id; |
---|
| 174 | } |
---|
| 175 | |
---|
[09f8cd1] | 176 | /* TODO: Persistence for timers! */ |
---|
[13cc96c] | 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 | |
---|
[09f8cd1] | 182 | b_ev->id = id_next++; |
---|
[19ac9c5] | 183 | b_ev->timeout = timeout; |
---|
[13cc96c] | 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 ); |
---|
[09f8cd1] | 191 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
[13cc96c] | 192 | |
---|
[782d988] | 193 | event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
[b642f381] | 194 | |
---|
[13cc96c] | 195 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 196 | |
---|
| 197 | return b_ev->id; |
---|
| 198 | } |
---|
| 199 | |
---|
[b642f381] | 200 | void b_event_remove( gint id ) |
---|
[13cc96c] | 201 | { |
---|
[b642f381] | 202 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
[13cc96c] | 203 | |
---|
[b642f381] | 204 | event_debug( "b_event_remove( %d )\n", id ); |
---|
[13cc96c] | 205 | if( b_ev ) |
---|
| 206 | { |
---|
[09f8cd1] | 207 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
[782d988] | 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 ); |
---|
[13cc96c] | 217 | g_free( b_ev ); |
---|
| 218 | } |
---|
[b642f381] | 219 | else |
---|
| 220 | { |
---|
[782d988] | 221 | event_debug( "Already removed?\n" ); |
---|
[b642f381] | 222 | } |
---|
[13cc96c] | 223 | } |
---|
| 224 | |
---|
[3f199fc] | 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 | |
---|
[b8b8c6c] | 235 | if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) |
---|
[df70eafa] | 236 | { |
---|
[5330e3d] | 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 | } |
---|
[b8b8c6c] | 240 | if( ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) |
---|
[5330e3d] | 241 | { |
---|
| 242 | event_debug( "Warning: fd %d still had a write event handler when shutting down.\n", fd ); |
---|
[3f199fc] | 243 | b_event_remove( b_ev->id ); |
---|
[df70eafa] | 244 | } |
---|
[3f199fc] | 245 | |
---|
| 246 | close( fd ); |
---|
| 247 | } |
---|