comparison src/proxy.c @ 2906:538c58b43eff

[gaim-migrate @ 2919] save save me from this wandered around the town all the thousand things i might miss and you think we'll suffer much think we'll close our eyes just to see the light pass us by with tomorrow coming hope that i don't let you down again said i'm so glad to be here does it mean a thing if only i could breathe what you breathe if only i could see what you see sit we'll take our time watching the flowers grow all the friends we've known say goodbye and you did you suffer much did you close your eyes just to see the night rush on by gathered all around you hope that we don't let you down again i said i'm so glad to be here does it mean a thing if only i could breathe what you breathe if only i could see what you see i said i'm so glad to be here does it mean a thing if only i could breathe what you breathe if only i could see what you see if only i could just believe a thing --Moist, "Breathe" (as transcribed by http://www.veddma.com/veddma/moist.htm) Patches from: Ari Pollak Ben Miller Mark Doliner Sean Egan Vincas Ciziunas Thanks everyone. Somewhere in the middle of all of this it started to get really tedious and annoying. I think it's getting close to the point where I quit. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Fri, 21 Dec 2001 10:23:04 +0000
parents a090e98dbbb6
children 28cd1b9d08a1
comparison
equal deleted inserted replaced
2905:a090e98dbbb6 2906:538c58b43eff
31 #include <string.h> 31 #include <string.h>
32 #include <sys/types.h> 32 #include <sys/types.h>
33 #include <sys/socket.h> 33 #include <sys/socket.h>
34 #include <netdb.h> 34 #include <netdb.h>
35 #include <netinet/in.h> 35 #include <netinet/in.h>
36 #include <arpa/inet.h>
36 #include <unistd.h> 37 #include <unistd.h>
37 #include <fcntl.h> 38 #include <fcntl.h>
38 #include <errno.h> 39 #include <errno.h>
39 #include "gaim.h" 40 #include "gaim.h"
40 #include "proxy.h" 41 #include "proxy.h"
116 /* debug_printf("CLOSURE: removing input watcher %d\n", tag); */ 117 /* debug_printf("CLOSURE: removing input watcher %d\n", tag); */
117 if (tag > 0) 118 if (tag > 0)
118 g_source_remove(tag); 119 g_source_remove(tag);
119 } 120 }
120 121
122 static struct sockaddr_in *gaim_gethostbyname(char *host)
123 {
124 static struct sockaddr_in sin;
125
126 if (!inet_aton(host, &sin.sin_addr)) {
127 struct hostent *hp;
128 if (!(hp = gethostbyname(host))) {
129 return NULL;
130 }
131 memset(&sin, 0, sizeof(struct sockaddr_in));
132 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
133 sin.sin_family = hp->h_addrtype;
134 } else
135 sin.sin_family = AF_INET;
136
137 return &sin;
138 }
139
121 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond) 140 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
122 { 141 {
123 struct PHB *phb = data; 142 struct PHB *phb = data;
124 unsigned int len; 143 unsigned int len;
125 int error = ETIMEDOUT; 144 int error = ETIMEDOUT;
149 } 168 }
150 169
151 170
152 static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb) 171 static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb)
153 { 172 {
154 struct sockaddr_in sin; 173 struct sockaddr_in *sin;
155 struct hostent *hp;
156 int fd = -1; 174 int fd = -1;
157 175
158 debug_printf("connecting to %s:%d with no proxy\n", host, port); 176 debug_printf("connecting to %s:%d with no proxy\n", host, port);
159 177
160 if (!(hp = gethostbyname(host))) { 178 if (!(sin = gaim_gethostbyname(host))) {
161 debug_printf("gethostbyname failed\n"); 179 debug_printf("gethostbyname failed\n");
162 g_free(phb); 180 g_free(phb);
163 return -1; 181 return -1;
164 } 182 }
165 183
166 memset(&sin, 0, sizeof(struct sockaddr_in)); 184 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
167 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
168 sin.sin_family = hp->h_addrtype;
169 sin.sin_port = htons(port);
170
171 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
172 debug_printf("unable to create socket\n"); 185 debug_printf("unable to create socket\n");
173 g_free(phb); 186 g_free(phb);
174 return -1; 187 return -1;
175 } 188 }
176 189
177 fcntl(fd, F_SETFL, O_NONBLOCK); 190 fcntl(fd, F_SETFL, O_NONBLOCK);
178 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 191 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
179 if ((errno == EINPROGRESS) || (errno == EINTR)) { 192 if ((errno == EINPROGRESS) || (errno == EINTR)) {
180 debug_printf("Connect would have blocked\n"); 193 debug_printf("Connect would have blocked\n");
181 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb); 194 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
182 } else { 195 } else {
183 debug_printf("connect failed (errno %d)\n", errno); 196 debug_printf("connect failed (errno %d)\n", errno);
300 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); 313 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
301 } 314 }
302 315
303 static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb) 316 static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb)
304 { 317 {
305 struct hostent *hp; 318 struct sockaddr_in *sin;
306 struct sockaddr_in sin;
307 int fd = -1; 319 int fd = -1;
308 320
309 debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport); 321 debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport);
310 322
311 if (!(hp = gethostbyname(proxyhost))) { 323 if (!(sin = gaim_gethostbyname(proxyhost))) {
312 g_free(phb); 324 g_free(phb);
313 return -1; 325 return -1;
314 } 326 }
315 327
316 memset(&sin, 0, sizeof(struct sockaddr_in)); 328 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
317 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
318 sin.sin_family = hp->h_addrtype;
319 sin.sin_port = htons(proxyport);
320
321 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
322 g_free(phb); 329 g_free(phb);
323 return -1; 330 return -1;
324 } 331 }
325 332
326 phb->host = g_strdup(host); 333 phb->host = g_strdup(host);
327 phb->port = port; 334 phb->port = port;
328 335
329 fcntl(fd, F_SETFL, O_NONBLOCK); 336 fcntl(fd, F_SETFL, O_NONBLOCK);
330 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 337 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
331 if ((errno == EINPROGRESS) || (errno == EINTR)) { 338 if ((errno == EINPROGRESS) || (errno == EINTR)) {
332 debug_printf("Connect would have blocked\n"); 339 debug_printf("Connect would have blocked\n");
333 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb); 340 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb);
334 } else { 341 } else {
335 close(fd); 342 close(fd);
425 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); 432 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
426 } 433 }
427 434
428 static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb) 435 static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb)
429 { 436 {
430 struct sockaddr_in sin; 437 struct sockaddr_in *sin;
431 struct hostent *hp;
432 int fd = -1; 438 int fd = -1;
433 439
434 debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport); 440 debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport);
435 441
436 if (!(hp = gethostbyname(proxyhost))) { 442 if (!(sin = gaim_gethostbyname(proxyhost))) {
437 g_free(phb); 443 g_free(phb);
438 return -1; 444 return -1;
439 } 445 }
440 446
441 memset(&sin, 0, sizeof(struct sockaddr_in)); 447 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
442 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
443 sin.sin_family = hp->h_addrtype;
444 sin.sin_port = htons(proxyport);
445
446 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
447 g_free(phb); 448 g_free(phb);
448 return -1; 449 return -1;
449 } 450 }
450 451
451 phb->host = g_strdup(host); 452 phb->host = g_strdup(host);
452 phb->port = port; 453 phb->port = port;
453 454
454 fcntl(fd, F_SETFL, O_NONBLOCK); 455 fcntl(fd, F_SETFL, O_NONBLOCK);
455 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 456 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
456 if ((errno == EINPROGRESS) || (errno == EINTR)) { 457 if ((errno == EINPROGRESS) || (errno == EINTR)) {
457 debug_printf("Connect would have blocked\n"); 458 debug_printf("Connect would have blocked\n");
458 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb); 459 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb);
459 } else { 460 } else {
460 close(fd); 461 close(fd);
654 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); 655 phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
655 } 656 }
656 657
657 static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb) 658 static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb)
658 { 659 {
660 struct sockaddr_in *sin;
659 int fd = -1; 661 int fd = -1;
660 struct sockaddr_in sin;
661 struct hostent *hp;
662 662
663 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport); 663 debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport);
664 664
665 if (!(hp = gethostbyname(proxyhost))) { 665 if (!(sin = gaim_gethostbyname(proxyhost))) {
666 g_free(phb); 666 g_free(phb);
667 return -1; 667 return -1;
668 } 668 }
669 669
670 memset(&sin, 0, sizeof(struct sockaddr_in)); 670 if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
671 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
672 sin.sin_family = hp->h_addrtype;
673 sin.sin_port = htons(proxyport);
674
675 if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
676 g_free(phb); 671 g_free(phb);
677 return -1; 672 return -1;
678 } 673 }
679 674
680 phb->host = g_strdup(host); 675 phb->host = g_strdup(host);
681 phb->port = port; 676 phb->port = port;
682 677
683 fcntl(fd, F_SETFL, O_NONBLOCK); 678 fcntl(fd, F_SETFL, O_NONBLOCK);
684 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 679 if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
685 if ((errno == EINPROGRESS) || (errno == EINTR)) { 680 if ((errno == EINPROGRESS) || (errno == EINTR)) {
686 debug_printf("Connect would have blocked\n"); 681 debug_printf("Connect would have blocked\n");
687 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb); 682 phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb);
688 } else { 683 } else {
689 close(fd); 684 close(fd);