Changes in protocols/proxy.c [19ac9c5:701acdd4]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/proxy.c
r19ac9c5 r701acdd4 21 21 */ 22 22 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 23 27 #define BITLBEE_CORE 24 28 #include <stdio.h> … … 42 46 #include "proxy.h" 43 47 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 44 52 char proxyhost[128] = ""; 45 53 int proxyport = 0; … … 49 57 50 58 struct PHB { 51 b_event_handlerfunc, proxy_func;59 GaimInputFunction func, proxy_func; 52 60 gpointer data, proxy_data; 53 61 char *host; … … 56 64 gint inpa; 57 65 }; 66 67 typedef struct _GaimIOClosure { 68 GaimInputFunction function; 69 guint result; 70 gpointer data; 71 } GaimIOClosure; 58 72 59 73 … … 78 92 } 79 93 80 static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond) 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 // if (condition & GAIM_ERR_COND) 109 // fprintf( stderr, "ERROR! fd=%d\n", g_io_channel_unix_get_fd( source ) ); 110 111 closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); 112 113 return TRUE; 114 } 115 116 static void gaim_io_connected(gpointer data, gint source, GaimInputCondition cond) 81 117 { 82 118 struct PHB *phb = data; … … 88 124 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { 89 125 closesocket(source); 90 b_event_remove(phb->inpa);126 gaim_input_remove(phb->inpa); 91 127 if( phb->proxy_func ) 92 128 phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); … … 95 131 g_free(phb); 96 132 } 97 return FALSE;133 return; 98 134 } 99 135 #endif 100 136 sock_make_blocking(source); 101 b_event_remove(phb->inpa);137 gaim_input_remove(phb->inpa); 102 138 if( phb->proxy_func ) 103 139 phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); … … 106 142 g_free(phb); 107 143 } 108 109 return FALSE;110 144 } 111 145 … … 126 160 127 161 sock_make_nonblocking(fd); 128 129 event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd); 130 162 131 163 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { 132 164 if (sockerr_again()) { 133 phb->inpa = b_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb);165 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb); 134 166 phb->fd = fd; 135 167 } else { … … 149 181 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" 150 182 151 static gboolean http_canread(gpointer data, gint source, b_input_condition cond)183 static void http_canread(gpointer data, gint source, GaimInputCondition cond) 152 184 { 153 185 int nlc = 0; … … 156 188 char inputline[8192]; 157 189 158 b_event_remove(phb->inpa);190 gaim_input_remove(phb->inpa); 159 191 160 192 while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { … … 171 203 g_free(phb->host); 172 204 g_free(phb); 173 return FALSE;205 return; 174 206 } 175 207 … … 178 210 g_free(phb->host); 179 211 g_free(phb); 180 181 return FALSE; 182 } 183 184 static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) 212 return; 213 } 214 215 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond) 185 216 { 186 217 char cmd[384]; … … 189 220 int error = ETIMEDOUT; 190 221 if (phb->inpa > 0) 191 b_event_remove(phb->inpa);222 gaim_input_remove(phb->inpa); 192 223 len = sizeof(error); 193 224 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 196 227 g_free(phb->host); 197 228 g_free(phb); 198 return FALSE;229 return; 199 230 } 200 231 sock_make_blocking(source); … … 207 238 g_free(phb->host); 208 239 g_free(phb); 209 return FALSE;240 return; 210 241 } 211 242 … … 222 253 g_free(phb->host); 223 254 g_free(phb); 224 return FALSE;255 return; 225 256 } 226 257 } … … 232 263 g_free(phb->host); 233 264 g_free(phb); 234 return FALSE; 235 } 236 237 phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb); 238 239 return FALSE; 265 return; 266 } 267 268 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); 240 269 } 241 270 … … 253 282 /* Connecting to SOCKS4 proxies */ 254 283 255 static gboolean s4_canread(gpointer data, gint source, b_input_condition cond)284 static void s4_canread(gpointer data, gint source, GaimInputCondition cond) 256 285 { 257 286 unsigned char packet[12]; 258 287 struct PHB *phb = data; 259 288 260 b_event_remove(phb->inpa);289 gaim_input_remove(phb->inpa); 261 290 262 291 memset(packet, 0, sizeof(packet)); … … 265 294 g_free(phb->host); 266 295 g_free(phb); 267 return FALSE;296 return; 268 297 } 269 298 … … 272 301 g_free(phb->host); 273 302 g_free(phb); 274 275 return FALSE; 276 } 277 278 static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) 303 } 304 305 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond) 279 306 { 280 307 unsigned char packet[12]; … … 284 311 int error = ETIMEDOUT; 285 312 if (phb->inpa > 0) 286 b_event_remove(phb->inpa);313 gaim_input_remove(phb->inpa); 287 314 len = sizeof(error); 288 315 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 291 318 g_free(phb->host); 292 319 g_free(phb); 293 return FALSE;320 return; 294 321 } 295 322 sock_make_blocking(source); … … 301 328 g_free(phb->host); 302 329 g_free(phb); 303 return FALSE;330 return; 304 331 } 305 332 … … 318 345 g_free(phb->host); 319 346 g_free(phb); 320 return FALSE; 321 } 322 323 phb->inpa = b_input_add(source, GAIM_INPUT_READ, s4_canread, phb); 324 325 return FALSE; 347 return; 348 } 349 350 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); 326 351 } 327 352 … … 339 364 /* Connecting to SOCKS5 proxies */ 340 365 341 static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond)366 static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond) 342 367 { 343 368 unsigned char buf[512]; 344 369 struct PHB *phb = data; 345 370 346 b_event_remove(phb->inpa);371 gaim_input_remove(phb->inpa); 347 372 348 373 if (read(source, buf, 10) < 10) { … … 351 376 g_free(phb->host); 352 377 g_free(phb); 353 return FALSE;378 return; 354 379 } 355 380 if ((buf[0] != 0x05) || (buf[1] != 0x00)) { … … 358 383 g_free(phb->host); 359 384 g_free(phb); 360 return FALSE;385 return; 361 386 } 362 387 … … 364 389 g_free(phb->host); 365 390 g_free(phb); 366 367 return FALSE; 391 return; 368 392 } 369 393 … … 373 397 struct PHB *phb = data; 374 398 int hlen = strlen(phb->host); 375 399 376 400 buf[0] = 0x05; 377 401 buf[1] = 0x01; /* CONNECT */ … … 391 415 } 392 416 393 phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);394 } 395 396 static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond)417 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); 418 } 419 420 static void s5_readauth(gpointer data, gint source, GaimInputCondition cond) 397 421 { 398 422 unsigned char buf[512]; 399 423 struct PHB *phb = data; 400 424 401 b_event_remove(phb->inpa);425 gaim_input_remove(phb->inpa); 402 426 403 427 if (read(source, buf, 2) < 2) { … … 406 430 g_free(phb->host); 407 431 g_free(phb); 408 return FALSE;432 return; 409 433 } 410 434 … … 414 438 g_free(phb->host); 415 439 g_free(phb); 416 return FALSE;440 return; 417 441 } 418 442 419 443 s5_sendconnect(phb, source); 420 421 return FALSE; 422 } 423 424 static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) 444 } 445 446 static void s5_canread(gpointer data, gint source, GaimInputCondition cond) 425 447 { 426 448 unsigned char buf[512]; 427 449 struct PHB *phb = data; 428 450 429 b_event_remove(phb->inpa);451 gaim_input_remove(phb->inpa); 430 452 431 453 if (read(source, buf, 2) < 2) { … … 434 456 g_free(phb->host); 435 457 g_free(phb); 436 return FALSE;458 return; 437 459 } 438 460 … … 442 464 g_free(phb->host); 443 465 g_free(phb); 444 return FALSE;466 return; 445 467 } 446 468 … … 457 479 g_free(phb->host); 458 480 g_free(phb); 459 return FALSE;481 return; 460 482 } 461 483 462 phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);484 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); 463 485 } else { 464 486 s5_sendconnect(phb, source); 465 487 } 466 467 return FALSE; 468 } 469 470 static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) 488 } 489 490 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond) 471 491 { 472 492 unsigned char buf[512]; … … 476 496 int error = ETIMEDOUT; 477 497 if (phb->inpa > 0) 478 b_event_remove(phb->inpa);498 gaim_input_remove(phb->inpa); 479 499 len = sizeof(error); 480 500 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 483 503 g_free(phb->host); 484 504 g_free(phb); 485 return FALSE;505 return; 486 506 } 487 507 sock_make_blocking(source); … … 505 525 g_free(phb->host); 506 526 g_free(phb); 507 return FALSE; 508 } 509 510 phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread, phb); 511 512 return FALSE; 527 return; 528 } 529 530 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); 513 531 } 514 532 … … 526 544 /* Export functions */ 527 545 528 int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) 546 gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data) 547 { 548 GaimIOClosure *closure = g_new0(GaimIOClosure, 1); 549 GIOChannel *channel; 550 GIOCondition cond = 0; 551 552 closure->function = function; 553 closure->data = data; 554 555 if (condition & GAIM_INPUT_READ) 556 cond |= GAIM_READ_COND; 557 if (condition & GAIM_INPUT_WRITE) 558 cond |= GAIM_WRITE_COND; 559 560 channel = g_io_channel_unix_new(source); 561 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, 562 gaim_io_invoke, closure, gaim_io_destroy); 563 564 g_io_channel_unref(channel); 565 return closure->result; 566 } 567 568 void gaim_input_remove(gint tag) 569 { 570 if (tag > 0) 571 g_source_remove(tag); 572 } 573 574 int proxy_connect(const char *host, int port, GaimInputFunction func, gpointer data) 529 575 { 530 576 struct PHB *phb;
Note: See TracChangeset
for help on using the changeset viewer.