Changeset b72caac for protocols/proxy.c
- Timestamp:
- 2006-06-21T16:34:33Z (18 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/proxy.c
r3af70b0 rb72caac 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 command25 , 2nd provide an easy way to add socks support */26 27 23 #define BITLBEE_CORE 28 24 #include <stdio.h> … … 46 42 #include "proxy.h" 47 43 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 52 44 char proxyhost[128] = ""; 53 45 int proxyport = 0; … … 57 49 58 50 struct PHB { 59 GaimInputFunctionfunc, proxy_func;51 b_event_handler func, proxy_func; 60 52 gpointer data, proxy_data; 61 53 char *host; … … 64 56 gint inpa; 65 57 }; 66 67 typedef struct _GaimIOClosure {68 GaimInputFunction function;69 guint result;70 gpointer data;71 } GaimIOClosure;72 58 73 59 … … 92 78 } 93 79 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) 80 static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond) 115 81 { 116 82 struct PHB *phb = data; … … 122 88 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { 123 89 closesocket(source); 124 gaim_input_remove(phb->inpa);90 b_event_remove(phb->inpa); 125 91 if( phb->proxy_func ) 126 92 phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); … … 129 95 g_free(phb); 130 96 } 131 return ;97 return FALSE; 132 98 } 133 99 #endif 134 100 sock_make_blocking(source); 135 gaim_input_remove(phb->inpa);101 b_event_remove(phb->inpa); 136 102 if( phb->proxy_func ) 137 103 phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); … … 140 106 g_free(phb); 141 107 } 108 109 return FALSE; 142 110 } 143 111 … … 158 126 159 127 sock_make_nonblocking(fd); 160 128 129 event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd); 130 161 131 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { 162 132 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); 164 134 phb->fd = fd; 165 135 } else { … … 179 149 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" 180 150 181 static void http_canread(gpointer data, gint source, GaimInputCondition cond)151 static gboolean http_canread(gpointer data, gint source, b_input_condition cond) 182 152 { 183 153 int nlc = 0; … … 186 156 char inputline[8192]; 187 157 188 gaim_input_remove(phb->inpa);158 b_event_remove(phb->inpa); 189 159 190 160 while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { … … 201 171 g_free(phb->host); 202 172 g_free(phb); 203 return ;173 return FALSE; 204 174 } 205 175 … … 208 178 g_free(phb->host); 209 179 g_free(phb); 210 return; 211 } 212 213 static void http_canwrite(gpointer data, gint source, GaimInputCondition cond) 180 181 return FALSE; 182 } 183 184 static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) 214 185 { 215 186 char cmd[384]; … … 218 189 int error = ETIMEDOUT; 219 190 if (phb->inpa > 0) 220 gaim_input_remove(phb->inpa);191 b_event_remove(phb->inpa); 221 192 len = sizeof(error); 222 193 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 225 196 g_free(phb->host); 226 197 g_free(phb); 227 return ;198 return FALSE; 228 199 } 229 200 sock_make_blocking(source); … … 236 207 g_free(phb->host); 237 208 g_free(phb); 238 return ;209 return FALSE; 239 210 } 240 211 … … 251 222 g_free(phb->host); 252 223 g_free(phb); 253 return ;224 return FALSE; 254 225 } 255 226 } … … 261 232 g_free(phb->host); 262 233 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; 267 240 } 268 241 … … 280 253 /* Connecting to SOCKS4 proxies */ 281 254 282 static void s4_canread(gpointer data, gint source, GaimInputCondition cond)255 static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) 283 256 { 284 257 unsigned char packet[12]; 285 258 struct PHB *phb = data; 286 259 287 gaim_input_remove(phb->inpa);260 b_event_remove(phb->inpa); 288 261 289 262 memset(packet, 0, sizeof(packet)); … … 292 265 g_free(phb->host); 293 266 g_free(phb); 294 return ;267 return FALSE; 295 268 } 296 269 … … 299 272 g_free(phb->host); 300 273 g_free(phb); 301 } 302 303 static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond) 274 275 return FALSE; 276 } 277 278 static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) 304 279 { 305 280 unsigned char packet[12]; … … 309 284 int error = ETIMEDOUT; 310 285 if (phb->inpa > 0) 311 gaim_input_remove(phb->inpa);286 b_event_remove(phb->inpa); 312 287 len = sizeof(error); 313 288 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 316 291 g_free(phb->host); 317 292 g_free(phb); 318 return ;293 return FALSE; 319 294 } 320 295 sock_make_blocking(source); … … 326 301 g_free(phb->host); 327 302 g_free(phb); 328 return ;303 return FALSE; 329 304 } 330 305 … … 343 318 g_free(phb->host); 344 319 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; 349 326 } 350 327 … … 362 339 /* Connecting to SOCKS5 proxies */ 363 340 364 static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)341 static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) 365 342 { 366 343 unsigned char buf[512]; 367 344 struct PHB *phb = data; 368 345 369 gaim_input_remove(phb->inpa);346 b_event_remove(phb->inpa); 370 347 371 348 if (read(source, buf, 10) < 10) { … … 374 351 g_free(phb->host); 375 352 g_free(phb); 376 return ;353 return FALSE; 377 354 } 378 355 if ((buf[0] != 0x05) || (buf[1] != 0x00)) { … … 381 358 g_free(phb->host); 382 359 g_free(phb); 383 return ;360 return FALSE; 384 361 } 385 362 … … 387 364 g_free(phb->host); 388 365 g_free(phb); 389 return; 366 367 return FALSE; 390 368 } 391 369 … … 395 373 struct PHB *phb = data; 396 374 int hlen = strlen(phb->host); 397 375 398 376 buf[0] = 0x05; 399 377 buf[1] = 0x01; /* CONNECT */ … … 413 391 } 414 392 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 396 static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) 419 397 { 420 398 unsigned char buf[512]; 421 399 struct PHB *phb = data; 422 400 423 gaim_input_remove(phb->inpa);401 b_event_remove(phb->inpa); 424 402 425 403 if (read(source, buf, 2) < 2) { … … 428 406 g_free(phb->host); 429 407 g_free(phb); 430 return ;408 return FALSE; 431 409 } 432 410 … … 436 414 g_free(phb->host); 437 415 g_free(phb); 438 return ;416 return FALSE; 439 417 } 440 418 441 419 s5_sendconnect(phb, source); 442 } 443 444 static void s5_canread(gpointer data, gint source, GaimInputCondition cond) 420 421 return FALSE; 422 } 423 424 static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) 445 425 { 446 426 unsigned char buf[512]; 447 427 struct PHB *phb = data; 448 428 449 gaim_input_remove(phb->inpa);429 b_event_remove(phb->inpa); 450 430 451 431 if (read(source, buf, 2) < 2) { … … 454 434 g_free(phb->host); 455 435 g_free(phb); 456 return ;436 return FALSE; 457 437 } 458 438 … … 462 442 g_free(phb->host); 463 443 g_free(phb); 464 return ;444 return FALSE; 465 445 } 466 446 … … 477 457 g_free(phb->host); 478 458 g_free(phb); 479 return ;459 return FALSE; 480 460 } 481 461 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); 483 463 } else { 484 464 s5_sendconnect(phb, source); 485 465 } 486 } 487 488 static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond) 466 467 return FALSE; 468 } 469 470 static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) 489 471 { 490 472 unsigned char buf[512]; … … 494 476 int error = ETIMEDOUT; 495 477 if (phb->inpa > 0) 496 gaim_input_remove(phb->inpa);478 b_event_remove(phb->inpa); 497 479 len = sizeof(error); 498 480 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 501 483 g_free(phb->host); 502 484 g_free(phb); 503 return ;485 return FALSE; 504 486 } 505 487 sock_make_blocking(source); … … 523 505 g_free(phb->host); 524 506 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; 529 513 } 530 514 … … 542 526 /* Export functions */ 543 527 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) 528 int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) 573 529 { 574 530 struct PHB *phb;
Note: See TracChangeset
for help on using the changeset viewer.