Changeset 5ebff60 for lib/proxy.c
- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/proxy.c
raf359b4 r5ebff60 68 68 socklen_t len; 69 69 int error = ETIMEDOUT; 70 70 71 len = sizeof(error); 71 72 72 73 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { 73 74 if ((phb->gai_cur = phb->gai_cur->ai_next)) { … … 87 88 b_event_remove(phb->inpa); 88 89 phb->inpa = 0; 89 if ( phb->proxy_func )90 if (phb->proxy_func) { 90 91 phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ); 91 else {92 } else { 92 93 phb->func(phb->data, -1, B_EV_IO_READ); 93 94 g_free(phb); … … 99 100 b_event_remove(phb->inpa); 100 101 phb->inpa = 0; 101 if ( phb->proxy_func )102 if (phb->proxy_func) { 102 103 phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ); 103 else {104 } else { 104 105 phb->func(phb->data, source, B_EV_IO_READ); 105 106 g_free(phb); 106 107 } 107 108 108 109 return FALSE; 109 110 } … … 113 114 struct sockaddr_in me; 114 115 int fd = -1; 115 116 if (phb->gai_cur == NULL) 117 { 116 117 if (phb->gai_cur == NULL) { 118 118 int ret; 119 119 char port[6]; 120 120 struct addrinfo hints; 121 121 122 122 g_snprintf(port, sizeof(port), "%d", port_); 123 123 124 124 memset(&hints, 0, sizeof(struct addrinfo)); 125 125 hints.ai_family = AF_UNSPEC; 126 126 hints.ai_socktype = SOCK_STREAM; 127 127 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; 128 129 if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) 128 129 if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) { 130 130 phb->gai_cur = phb->gai; 131 else131 } else { 132 132 event_debug("gai(): %s\n", gai_strerror(ret)); 133 }134 135 for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) 136 {133 } 134 } 135 136 for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) { 137 137 if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) { 138 event_debug( 138 event_debug("socket failed: %d\n", errno); 139 139 continue; 140 140 } … … 142 142 sock_make_nonblocking(fd); 143 143 144 if (global.conf->iface_out) 145 { 144 if (global.conf->iface_out) { 146 145 me.sin_family = AF_INET; 147 146 me.sin_port = 0; 148 me.sin_addr.s_addr = inet_addr( global.conf->iface_out);149 150 if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) 147 me.sin_addr.s_addr = inet_addr(global.conf->iface_out); 148 149 if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) { 151 150 event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out); 151 } 152 152 } 153 153 154 154 event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port_, fd); 155 155 156 156 if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) { 157 event_debug( 157 event_debug("connect failed: %s\n", strerror(errno)); 158 158 closesocket(fd); 159 159 fd = -1; … … 162 162 phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb); 163 163 phb->fd = fd; 164 164 165 165 break; 166 166 } 167 167 } 168 169 if(fd < 0 && host) 170 g_free(phb); 168 169 if (fd < 0 && host) { 170 g_free(phb); 171 } 171 172 172 173 return fd; … … 188 189 b_event_remove(phb->inpa); 189 190 190 while ((pos < sizeof(inputline) -1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {191 if (inputline[pos - 1] == '\n') 191 while ((pos < sizeof(inputline) - 1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { 192 if (inputline[pos - 1] == '\n') { 192 193 nlc++; 193 else if (inputline[pos - 1] != '\r')194 } else if (inputline[pos - 1] != '\r') { 194 195 nlc = 0; 196 } 195 197 } 196 198 inputline[pos] = '\0'; … … 208 210 g_free(phb->host); 209 211 g_free(phb); 210 212 211 213 return FALSE; 212 214 } … … 218 220 socklen_t len; 219 221 int error = ETIMEDOUT; 220 if (phb->inpa > 0) 222 223 if (phb->inpa > 0) { 221 224 b_event_remove(phb->inpa); 225 } 222 226 len = sizeof(error); 223 227 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 231 235 232 236 g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, 233 237 phb->host, phb->port); 234 238 if (send(source, cmd, strlen(cmd), 0) < 0) { 235 239 close(source); … … 266 270 267 271 phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb); 268 272 269 273 return FALSE; 270 274 } … … 276 280 phb->proxy_func = http_canwrite; 277 281 phb->proxy_data = phb; 278 279 return( proxy_connect_none( proxyhost, proxyport, phb ));282 283 return(proxy_connect_none(proxyhost, proxyport, phb)); 280 284 } 281 285 … … 302 306 g_free(phb->host); 303 307 g_free(phb); 304 308 305 309 return FALSE; 306 310 } … … 313 317 socklen_t len; 314 318 int error = ETIMEDOUT; 315 if (phb->inpa > 0) 319 320 if (phb->inpa > 0) { 316 321 b_event_remove(phb->inpa); 322 } 317 323 len = sizeof(error); 318 324 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 338 344 packet[2] = phb->port >> 8; 339 345 packet[3] = phb->port & 0xff; 340 packet[4] = (unsigned char) (hp->h_addr_list[0])[0];341 packet[5] = (unsigned char) (hp->h_addr_list[0])[1];342 packet[6] = (unsigned char) (hp->h_addr_list[0])[2];343 packet[7] = (unsigned char) (hp->h_addr_list[0])[3];346 packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; 347 packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; 348 packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; 349 packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; 344 350 packet[8] = 0; 345 351 if (write(source, packet, 9) != 9) { … … 352 358 353 359 phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); 354 360 355 361 return FALSE; 356 362 } … … 362 368 phb->proxy_func = s4_canwrite; 363 369 phb->proxy_data = phb; 364 365 return( proxy_connect_none( proxyhost, proxyport, phb ));370 371 return(proxy_connect_none(proxyhost, proxyport, phb)); 366 372 } 367 373 … … 394 400 g_free(phb->host); 395 401 g_free(phb); 396 402 397 403 return FALSE; 398 404 } … … 403 409 struct PHB *phb = data; 404 410 int hlen = strlen(phb->host); 405 411 406 412 buf[0] = 0x05; 407 buf[1] = 0x01; 408 buf[2] = 0x00; 409 buf[3] = 0x03; 413 buf[1] = 0x01; /* CONNECT */ 414 buf[2] = 0x00; /* reserved */ 415 buf[3] = 0x03; /* address type -- host name */ 410 416 buf[4] = hlen; 411 417 memcpy(buf + 5, phb->host, hlen); … … 448 454 449 455 s5_sendconnect(phb, source); 450 456 451 457 return FALSE; 452 458 } … … 477 483 if (buf[1] == 0x02) { 478 484 unsigned int i = strlen(proxyuser), j = strlen(proxypass); 479 buf[0] = 0x01; 485 buf[0] = 0x01; /* version 1 */ 480 486 buf[1] = i; 481 487 memcpy(buf + 2, proxyuser, i); … … 494 500 s5_sendconnect(phb, source); 495 501 } 496 502 497 503 return FALSE; 498 504 } … … 505 511 socklen_t len; 506 512 int error = ETIMEDOUT; 507 if (phb->inpa > 0) 513 514 if (phb->inpa > 0) { 508 515 b_event_remove(phb->inpa); 516 } 509 517 len = sizeof(error); 510 518 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { … … 518 526 519 527 i = 0; 520 buf[0] = 0x05; 528 buf[0] = 0x05; /* SOCKS version 5 */ 521 529 if (proxyuser[0]) { 522 buf[1] = 0x02; 523 buf[2] = 0x00; 524 buf[3] = 0x02; 530 buf[1] = 0x02; /* two methods */ 531 buf[2] = 0x00; /* no authentication */ 532 buf[3] = 0x02; /* username/password authentication */ 525 533 i = 4; 526 534 } else { … … 539 547 540 548 phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); 541 549 542 550 return FALSE; 543 551 } … … 549 557 phb->proxy_func = s5_canwrite; 550 558 phb->proxy_data = phb; 551 552 return( proxy_connect_none( proxyhost, proxyport, phb ));559 560 return(proxy_connect_none(proxyhost, proxyport, phb)); 553 561 } 554 562 … … 559 567 { 560 568 struct PHB *phb; 561 569 562 570 if (!host || port <= 0 || !func || strlen(host) > 128) { 563 571 return -1; 564 572 } 565 573 566 574 phb = g_new0(struct PHB, 1); 567 575 phb->func = func; 568 576 phb->data = data; 569 570 if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) 577 578 if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) { 571 579 return proxy_connect_none(host, port, phb); 572 else if (proxytype == PROXY_HTTP)580 } else if (proxytype == PROXY_HTTP) { 573 581 return proxy_connect_http(host, port, phb); 574 else if (proxytype == PROXY_SOCKS4)582 } else if (proxytype == PROXY_SOCKS4) { 575 583 return proxy_connect_socks4(host, port, phb); 576 else if (proxytype == PROXY_SOCKS5)584 } else if (proxytype == PROXY_SOCKS5) { 577 585 return proxy_connect_socks5(host, port, phb); 578 586 } 587 579 588 g_free(phb); 580 589 return -1;
Note: See TracChangeset
for help on using the changeset viewer.