Mercurial > pidgin
diff src/proxy.c @ 1252:46c09828e929
[gaim-migrate @ 1262]
still need to do buddy.c, conversation.c, dialogs.c, prefs.c.
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Wed, 13 Dec 2000 22:12:02 +0000 |
| parents | 56c7ceb986a8 |
| children | 153eb28ea454 |
line wrap: on
line diff
--- a/src/proxy.c Wed Dec 13 20:23:06 2000 +0000 +++ b/src/proxy.c Wed Dec 13 22:12:02 2000 +0000 @@ -39,50 +39,47 @@ #include "proxy.h" /* this code is borrowed from cvs 1.10 */ -static int proxy_recv_line (int sock, char **resultp) +static int proxy_recv_line(int sock, char **resultp) { - int c; - char *result; - size_t input_index = 0; - size_t result_size = 80; + int c; + char *result; + size_t input_index = 0; + size_t result_size = 80; - result = g_malloc (result_size); + result = g_malloc(result_size); + + while (1) { + char ch; + if (recv(sock, &ch, 1, 0) < 0) + debug_printf("recv() error from proxy server\n"); + c = ch; - while (1) - { - char ch; - if (recv (sock, &ch, 1, 0) < 0) - fprintf (stderr, "recv() error from proxy server\n"); - c = ch; + if (c == EOF) { + g_free(result); + + /* It's end of file. */ + debug_printf("end of file from server\n"); + } - if (c == EOF) - { - g_free (result); + if (c == '\012') + break; - /* It's end of file. */ - fprintf(stderr, "end of file from server\n"); + result[input_index++] = c; + while (input_index + 1 >= result_size) { + result_size *= 2; + result = (char *)g_realloc(result, result_size); + } } - if (c == '\012') - break; + if (resultp) + *resultp = result; - result[input_index++] = c; - while (input_index + 1 >= result_size) - { - result_size *= 2; - result = (char *) g_realloc (result, result_size); - } - } + /* Terminate it just for kicks, but we *can* deal with embedded NULs. */ + result[input_index] = '\0'; - if (resultp) - *resultp = result; - - /* Terminate it just for kicks, but we *can* deal with embedded NULs. */ - result[input_index] = '\0'; - - if (resultp == NULL) - g_free (result); - return input_index; + if (resultp == NULL) + g_free(result); + return input_index; } static int proxy_connect_none(char *host, unsigned short port) @@ -93,7 +90,7 @@ debug_printf("connecting to %s:%d with no proxy\n", host, port); - if (!(hp = gethostbyname(host))) + if (!(hp = gethostbyname(host))) return -1; memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -115,8 +112,7 @@ #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" -static int proxy_connect_http(char *host, unsigned short port, - char *proxyhost, unsigned short proxyport) +static int proxy_connect_http(char *host, unsigned short port, char *proxyhost, unsigned short proxyport) { struct hostent *hp; struct sockaddr_in sin; @@ -126,7 +122,7 @@ debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport); - if (!(hp = gethostbyname(proxyhost))) + if (!(hp = gethostbyname(proxyhost))) return -1; memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -144,13 +140,13 @@ snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\n\r\n\r", host, port); - if (send(fd, cmd, strlen(cmd),0) < 0) + if (send(fd, cmd, strlen(cmd), 0) < 0) return -1; if (proxy_recv_line(fd, &inputline) < 0) return -1; if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING) == 0) || - (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2) == 0)))) { + (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2) == 0)))) { while (strlen(inputline) > 1) { free(inputline); if (proxy_recv_line(fd, &inputline) < 0) @@ -168,7 +164,7 @@ } static int proxy_connect_socks4(char *host, unsigned short port, - char *proxyhost, unsigned short proxyport) + char *proxyhost, unsigned short proxyport) { struct sockaddr_in sin; unsigned char packet[12]; @@ -177,7 +173,7 @@ debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport); - if (!(hp = gethostbyname(proxyhost))) + if (!(hp = gethostbyname(proxyhost))) return -1; memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -199,12 +195,12 @@ packet[0] = 4; packet[1] = 1; - packet[2] = (((unsigned short) htons(port)) >> 8); - packet[3] = (((unsigned short) htons(port)) & 0xff); - packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; - packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; - packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; - packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; + packet[2] = (((unsigned short)htons(port)) >> 8); + packet[3] = (((unsigned short)htons(port)) & 0xff); + packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; + packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; + packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; + packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; packet[8] = 0; if (write(fd, packet, 9) == 9) { memset(packet, 0, sizeof(packet)); @@ -217,7 +213,7 @@ } static int proxy_connect_socks5(char *host, unsigned short port, - char *proxyhost, unsigned short proxyport) + char *proxyhost, unsigned short proxyport) { int i, fd = -1; unsigned char buf[512]; @@ -226,7 +222,7 @@ debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport); - if (!(hp = gethostbyname(proxyhost))) + if (!(hp = gethostbyname(proxyhost))) return -1; memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -243,7 +239,7 @@ } i = 0; - buf[0] = 0x05; /* SOCKS version 5 */ + buf[0] = 0x05; /* SOCKS version 5 */ buf[1] = 0x01; buf[2] = 0x00; i = 3; @@ -264,15 +260,15 @@ } buf[0] = 0x05; - buf[1] = 0x01; /* CONNECT */ - buf[2] = 0x00; /* reserved */ - buf[3] = 0x03; /* address type -- host name */ + buf[1] = 0x01; /* CONNECT */ + buf[2] = 0x00; /* reserved */ + buf[3] = 0x03; /* address type -- host name */ buf[4] = strlen(host); - memcpy(buf+5, host, strlen(host)); - buf[5+strlen(host)] = htons(port) >> 8; - buf[5+strlen(host)+1] = htons(port) & 0xff; + memcpy(buf + 5, host, strlen(host)); + buf[5 + strlen(host)] = htons(port) >> 8; + buf[5 + strlen(host) + 1] = htons(port) & 0xff; - if (write(fd, buf, (5+strlen(host)+2)) < (5+strlen(host)+2)) { + if (write(fd, buf, (5 + strlen(host) + 2)) < (5 + strlen(host) + 2)) { close(fd); return -1; } @@ -292,10 +288,9 @@ { if (!host || !port || (port == -1)) return -1; - else if ((proxytype == PROXY_NONE) || - !proxyhost || !proxyhost[0] || - !proxyport || (proxyport == -1)) - return proxy_connect_none(host, port); + else if ((proxytype == PROXY_NONE) || + !proxyhost || !proxyhost[0] || + !proxyport || (proxyport == -1)) return proxy_connect_none(host, port); else if (proxytype == PROXY_HTTP) return proxy_connect_http(host, port, proxyhost, proxyport); else if (proxytype == PROXY_SOCKS4)
