diff 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
line wrap: on
line diff
--- a/src/proxy.c	Thu Dec 20 21:01:59 2001 +0000
+++ b/src/proxy.c	Fri Dec 21 10:23:04 2001 +0000
@@ -33,6 +33,7 @@
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -118,6 +119,24 @@
 		g_source_remove(tag);
 }
 
+static struct sockaddr_in *gaim_gethostbyname(char *host)
+{
+	static struct sockaddr_in sin;
+
+	if (!inet_aton(host, &sin.sin_addr)) {
+		struct hostent *hp;
+		if (!(hp = gethostbyname(host))) {
+			return NULL;
+		}
+		memset(&sin, 0, sizeof(struct sockaddr_in));
+		memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
+		sin.sin_family = hp->h_addrtype;
+	} else
+		sin.sin_family = AF_INET;
+
+	return &sin;
+}
+
 static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
 {
 	struct PHB *phb = data;
@@ -151,31 +170,25 @@
 
 static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb)
 {
-	struct sockaddr_in sin;
-	struct hostent *hp;
+	struct sockaddr_in *sin;
 	int fd = -1;
 
 	debug_printf("connecting to %s:%d with no proxy\n", host, port);
 
-	if (!(hp = gethostbyname(host))) {
+	if (!(sin = gaim_gethostbyname(host))) {
 		debug_printf("gethostbyname failed\n");
 		g_free(phb);
 		return -1;
 	}
 
-	memset(&sin, 0, sizeof(struct sockaddr_in));
-	memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
-	sin.sin_family = hp->h_addrtype;
-	sin.sin_port = htons(port);
-
-	if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
+	if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
 		debug_printf("unable to create socket\n");
 		g_free(phb);
 		return -1;
 	}
 
 	fcntl(fd, F_SETFL, O_NONBLOCK);
-	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+	if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
 			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
@@ -302,23 +315,17 @@
 
 static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb)
 {
-	struct hostent *hp;
-	struct sockaddr_in sin;
+	struct sockaddr_in *sin;
 	int fd = -1;
 
 	debug_printf("connecting to %s:%d via %s:%d using HTTP\n", host, port, proxyhost, proxyport);
 
-	if (!(hp = gethostbyname(proxyhost))) {
+	if (!(sin = gaim_gethostbyname(proxyhost))) {
 		g_free(phb);
 		return -1;
 	}
 
-	memset(&sin, 0, sizeof(struct sockaddr_in));
-	memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
-	sin.sin_family = hp->h_addrtype;
-	sin.sin_port = htons(proxyport);
-
-	if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
+	if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
 		g_free(phb);
 		return -1;
 	}
@@ -327,7 +334,7 @@
 	phb->port = port;
 
 	fcntl(fd, F_SETFL, O_NONBLOCK);
-	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+	if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
 			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb);
@@ -427,23 +434,17 @@
 
 static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb)
 {
-	struct sockaddr_in sin;
-	struct hostent *hp;
+	struct sockaddr_in *sin;
 	int fd = -1;
 
 	debug_printf("connecting to %s:%d via %s:%d using SOCKS4\n", host, port, proxyhost, proxyport);
 
-	if (!(hp = gethostbyname(proxyhost))) {
+	if (!(sin = gaim_gethostbyname(proxyhost))) {
 		g_free(phb);
 		return -1;
 	}
 
-	memset(&sin, 0, sizeof(struct sockaddr_in));
-	memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
-	sin.sin_family = hp->h_addrtype;
-	sin.sin_port = htons(proxyport);
-
-	if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
+	if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
 		g_free(phb);
 		return -1;
 	}
@@ -452,7 +453,7 @@
 	phb->port = port;
 
 	fcntl(fd, F_SETFL, O_NONBLOCK);
-	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+	if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
 			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb);
@@ -656,23 +657,17 @@
 
 static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb)
 {
+	struct sockaddr_in *sin;
 	int fd = -1;
-	struct sockaddr_in sin;
-	struct hostent *hp;
 
 	debug_printf("connecting to %s:%d via %s:%d using SOCKS5\n", host, port, proxyhost, proxyport);
 
-	if (!(hp = gethostbyname(proxyhost))) {
+	if (!(sin = gaim_gethostbyname(proxyhost))) {
 		g_free(phb);
 		return -1;
 	}
 
-	memset(&sin, 0, sizeof(struct sockaddr_in));
-	memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
-	sin.sin_family = hp->h_addrtype;
-	sin.sin_port = htons(proxyport);
-
-	if ((fd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
+	if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
 		g_free(phb);
 		return -1;
 	}
@@ -681,7 +676,7 @@
 	phb->port = port;
 
 	fcntl(fd, F_SETFL, O_NONBLOCK);
-	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+	if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
 			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb);