Changeset b72caac for protocols/proxy.c


Ignore:
Timestamp:
2006-06-21T16:34:33Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
59f5c42a
Parents:
3af70b0 (diff), df417ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merging libevent branch: Events can now be handles by both glib and libevent.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/proxy.c

    r3af70b0 rb72caac  
    2121 */
    2222
    23 /* this is a little piece of code to handle proxy connection */
    24 /* it is intended to : 1st handle http proxy, using the CONNECT command
    25  , 2nd provide an easy way to add socks support */
    26 
    2723#define BITLBEE_CORE
    2824#include <stdio.h>
     
    4642#include "proxy.h"
    4743
    48 #define GAIM_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
    49 #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
    50 #define GAIM_ERR_COND   (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
    51 
    5244char proxyhost[128] = "";
    5345int proxyport = 0;
     
    5749
    5850struct PHB {
    59         GaimInputFunction func, proxy_func;
     51        b_event_handler func, proxy_func;
    6052        gpointer data, proxy_data;
    6153        char *host;
     
    6456        gint inpa;
    6557};
    66 
    67 typedef struct _GaimIOClosure {
    68         GaimInputFunction function;
    69         guint result;
    70         gpointer data;
    71 } GaimIOClosure;
    7258
    7359
     
    9278}
    9379
    94 static void gaim_io_destroy(gpointer data)
    95 {
    96         g_free(data);
    97 }
    98 
    99 static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
    100 {
    101         GaimIOClosure *closure = data;
    102         GaimInputCondition gaim_cond = 0;
    103 
    104         if (condition & GAIM_READ_COND)
    105                 gaim_cond |= GAIM_INPUT_READ;
    106         if (condition & GAIM_WRITE_COND)
    107                 gaim_cond |= GAIM_INPUT_WRITE;
    108 
    109         closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
    110 
    111         return TRUE;
    112 }
    113 
    114 static void gaim_io_connected(gpointer data, gint source, GaimInputCondition cond)
     80static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond)
    11581{
    11682        struct PHB *phb = data;
     
    12288        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
    12389                closesocket(source);
    124                 gaim_input_remove(phb->inpa);
     90                b_event_remove(phb->inpa);
    12591                if( phb->proxy_func )
    12692                        phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ);
     
    12995                        g_free(phb);
    13096                }
    131                 return;
     97                return FALSE;
    13298        }
    13399#endif
    134100        sock_make_blocking(source);
    135         gaim_input_remove(phb->inpa);
     101        b_event_remove(phb->inpa);
    136102        if( phb->proxy_func )
    137103                phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ);
     
    140106                g_free(phb);
    141107        }
     108       
     109        return FALSE;
    142110}
    143111
     
    158126
    159127        sock_make_nonblocking(fd);
    160 
     128       
     129        event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd);
     130       
    161131        if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
    162132                if (sockerr_again()) {
    163                         phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb);
     133                        phb->inpa = b_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb);
    164134                        phb->fd = fd;
    165135                } else {
     
    179149#define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
    180150
    181 static void http_canread(gpointer data, gint source, GaimInputCondition cond)
     151static gboolean http_canread(gpointer data, gint source, b_input_condition cond)
    182152{
    183153        int nlc = 0;
     
    186156        char inputline[8192];
    187157
    188         gaim_input_remove(phb->inpa);
     158        b_event_remove(phb->inpa);
    189159
    190160        while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
     
    201171                g_free(phb->host);
    202172                g_free(phb);
    203                 return;
     173                return FALSE;
    204174        }
    205175
     
    208178        g_free(phb->host);
    209179        g_free(phb);
    210         return;
    211 }
    212 
    213 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
     180       
     181        return FALSE;
     182}
     183
     184static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond)
    214185{
    215186        char cmd[384];
     
    218189        int error = ETIMEDOUT;
    219190        if (phb->inpa > 0)
    220                 gaim_input_remove(phb->inpa);
     191                b_event_remove(phb->inpa);
    221192        len = sizeof(error);
    222193        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
     
    225196                g_free(phb->host);
    226197                g_free(phb);
    227                 return;
     198                return FALSE;
    228199        }
    229200        sock_make_blocking(source);
     
    236207                g_free(phb->host);
    237208                g_free(phb);
    238                 return;
     209                return FALSE;
    239210        }
    240211
     
    251222                        g_free(phb->host);
    252223                        g_free(phb);
    253                         return;
     224                        return FALSE;
    254225                }
    255226        }
     
    261232                g_free(phb->host);
    262233                g_free(phb);
    263                 return;
    264         }
    265 
    266         phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
     234                return FALSE;
     235        }
     236
     237        phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb);
     238       
     239        return FALSE;
    267240}
    268241
     
    280253/* Connecting to SOCKS4 proxies */
    281254
    282 static void s4_canread(gpointer data, gint source, GaimInputCondition cond)
     255static gboolean s4_canread(gpointer data, gint source, b_input_condition cond)
    283256{
    284257        unsigned char packet[12];
    285258        struct PHB *phb = data;
    286259
    287         gaim_input_remove(phb->inpa);
     260        b_event_remove(phb->inpa);
    288261
    289262        memset(packet, 0, sizeof(packet));
     
    292265                g_free(phb->host);
    293266                g_free(phb);
    294                 return;
     267                return FALSE;
    295268        }
    296269
     
    299272        g_free(phb->host);
    300273        g_free(phb);
    301 }
    302 
    303 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
     274       
     275        return FALSE;
     276}
     277
     278static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond)
    304279{
    305280        unsigned char packet[12];
     
    309284        int error = ETIMEDOUT;
    310285        if (phb->inpa > 0)
    311                 gaim_input_remove(phb->inpa);
     286                b_event_remove(phb->inpa);
    312287        len = sizeof(error);
    313288        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
     
    316291                g_free(phb->host);
    317292                g_free(phb);
    318                 return;
     293                return FALSE;
    319294        }
    320295        sock_make_blocking(source);
     
    326301                g_free(phb->host);
    327302                g_free(phb);
    328                 return;
     303                return FALSE;
    329304        }
    330305
     
    343318                g_free(phb->host);
    344319                g_free(phb);
    345                 return;
    346         }
    347 
    348         phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
     320                return FALSE;
     321        }
     322
     323        phb->inpa = b_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
     324       
     325        return FALSE;
    349326}
    350327
     
    362339/* Connecting to SOCKS5 proxies */
    363340
    364 static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
     341static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond)
    365342{
    366343        unsigned char buf[512];
    367344        struct PHB *phb = data;
    368345
    369         gaim_input_remove(phb->inpa);
     346        b_event_remove(phb->inpa);
    370347
    371348        if (read(source, buf, 10) < 10) {
     
    374351                g_free(phb->host);
    375352                g_free(phb);
    376                 return;
     353                return FALSE;
    377354        }
    378355        if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
     
    381358                g_free(phb->host);
    382359                g_free(phb);
    383                 return;
     360                return FALSE;
    384361        }
    385362
     
    387364        g_free(phb->host);
    388365        g_free(phb);
    389         return;
     366       
     367        return FALSE;
    390368}
    391369
     
    395373        struct PHB *phb = data;
    396374        int hlen = strlen(phb->host);
    397 
     375       
    398376        buf[0] = 0x05;
    399377        buf[1] = 0x01;          /* CONNECT */
     
    413391        }
    414392
    415         phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
    416 }
    417 
    418 static void s5_readauth(gpointer data, gint source, GaimInputCondition cond)
     393        phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
     394}
     395
     396static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond)
    419397{
    420398        unsigned char buf[512];
    421399        struct PHB *phb = data;
    422400
    423         gaim_input_remove(phb->inpa);
     401        b_event_remove(phb->inpa);
    424402
    425403        if (read(source, buf, 2) < 2) {
     
    428406                g_free(phb->host);
    429407                g_free(phb);
    430                 return;
     408                return FALSE;
    431409        }
    432410
     
    436414                g_free(phb->host);
    437415                g_free(phb);
    438                 return;
     416                return FALSE;
    439417        }
    440418
    441419        s5_sendconnect(phb, source);
    442 }
    443 
    444 static void s5_canread(gpointer data, gint source, GaimInputCondition cond)
     420       
     421        return FALSE;
     422}
     423
     424static gboolean s5_canread(gpointer data, gint source, b_input_condition cond)
    445425{
    446426        unsigned char buf[512];
    447427        struct PHB *phb = data;
    448428
    449         gaim_input_remove(phb->inpa);
     429        b_event_remove(phb->inpa);
    450430
    451431        if (read(source, buf, 2) < 2) {
     
    454434                g_free(phb->host);
    455435                g_free(phb);
    456                 return;
     436                return FALSE;
    457437        }
    458438
     
    462442                g_free(phb->host);
    463443                g_free(phb);
    464                 return;
     444                return FALSE;
    465445        }
    466446
     
    477457                        g_free(phb->host);
    478458                        g_free(phb);
    479                         return;
     459                        return FALSE;
    480460                }
    481461
    482                 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
     462                phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
    483463        } else {
    484464                s5_sendconnect(phb, source);
    485465        }
    486 }
    487 
    488 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
     466       
     467        return FALSE;
     468}
     469
     470static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond)
    489471{
    490472        unsigned char buf[512];
     
    494476        int error = ETIMEDOUT;
    495477        if (phb->inpa > 0)
    496                 gaim_input_remove(phb->inpa);
     478                b_event_remove(phb->inpa);
    497479        len = sizeof(error);
    498480        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
     
    501483                g_free(phb->host);
    502484                g_free(phb);
    503                 return;
     485                return FALSE;
    504486        }
    505487        sock_make_blocking(source);
     
    523505                g_free(phb->host);
    524506                g_free(phb);
    525                 return;
    526         }
    527 
    528         phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
     507                return FALSE;
     508        }
     509
     510        phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
     511       
     512        return FALSE;
    529513}
    530514
     
    542526/* Export functions */
    543527
    544 gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data)
    545 {
    546         GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
    547         GIOChannel *channel;
    548         GIOCondition cond = 0;
    549        
    550         closure->function = function;
    551         closure->data = data;
    552        
    553         if (condition & GAIM_INPUT_READ)
    554                 cond |= GAIM_READ_COND;
    555         if (condition & GAIM_INPUT_WRITE)
    556                 cond |= GAIM_WRITE_COND;
    557        
    558         channel = g_io_channel_unix_new(source);
    559         closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
    560                                               gaim_io_invoke, closure, gaim_io_destroy);
    561        
    562         g_io_channel_unref(channel);
    563         return closure->result;
    564 }
    565 
    566 void gaim_input_remove(gint tag)
    567 {
    568         if (tag > 0)
    569                 g_source_remove(tag);
    570 }
    571 
    572 int proxy_connect(const char *host, int port, GaimInputFunction func, gpointer data)
     528int proxy_connect(const char *host, int port, b_event_handler func, gpointer data)
    573529{
    574530        struct PHB *phb;
Note: See TracChangeset for help on using the changeset viewer.