diff src/proxy.c @ 2090:b66aca8e8dce

[gaim-migrate @ 2100] change ../config.h to <config.h> because that's better. change from GdkInput functions to GaimInput for reasons mentioned elsewhere. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Tue, 31 Jul 2001 23:23:40 +0000
parents 3366311426df
children 39b4413ce4b3
line wrap: on
line diff
--- a/src/proxy.c	Tue Jul 31 18:02:40 2001 +0000
+++ b/src/proxy.c	Tue Jul 31 23:23:40 2001 +0000
@@ -24,7 +24,7 @@
  , 2nd provide an easy way to add socks support */
 
 #ifdef HAVE_CONFIG_H
-#include "../config.h"
+#include "config.h"
 #endif
 #include <stdio.h>
 #include <stdlib.h>
@@ -36,10 +36,12 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <gtk/gtk.h>
 #include "gaim.h"
 #include "proxy.h"
 
+#define GAIM_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
+#define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR)
+
 char proxyhost[128] = { 0 };
 int  proxyport = 0;
 int  proxytype = 0;
@@ -47,14 +49,61 @@
 char proxypass[128] = { 0 };
 
 struct PHB {
-	GdkInputFunction func;
+	GaimInputFunction func;
 	gpointer data;
 	char *host;
 	int port;
 	gint inpa;
 };
 
-static void no_one_calls(gpointer data, gint source, GdkInputCondition cond)
+typedef struct _GaimIOClosure {
+	GaimInputFunction function;
+	gpointer data;
+} GaimIOClosure;
+
+static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
+{
+	GaimIOClosure *closure = data;
+	GaimInputCondition gaim_cond = 0;
+
+	if (condition & GAIM_READ_COND)
+		gaim_cond |= GAIM_INPUT_READ;
+	if (condition & GAIM_READ_COND)
+		gaim_cond |= GAIM_INPUT_WRITE;
+
+	closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
+
+	return TRUE;
+}
+
+gint gaim_input_add(gint source, GaimInputCondition condition,
+		GaimInputFunction function, gpointer data)
+{
+	guint result;
+	GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
+	GIOChannel *channel;
+	GIOCondition cond = 0;
+
+	closure->function = function;
+	closure->data = data;
+
+	if (condition & GAIM_INPUT_READ)
+		cond |= GAIM_READ_COND;
+	if (condition & GAIM_INPUT_WRITE)
+		cond |= GAIM_WRITE_COND;
+
+	channel = g_io_channel_unix_new(source);
+	result = g_io_add_watch(channel, cond, gaim_io_invoke, closure);
+	g_io_channel_unref(channel);
+	return result;
+}
+
+void gaim_input_remove(gint tag)
+{
+	g_source_remove(tag);
+}
+
+static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
 {
 	struct PHB *phb = data;
 	unsigned int len;
@@ -63,14 +112,14 @@
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		gdk_input_remove(phb->inpa);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		gaim_input_remove(phb->inpa);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb);
 		return;
 	}
 	fcntl(source, F_SETFL, 0);
-	gdk_input_remove(phb->inpa);
-	phb->func(phb->data, source, GDK_INPUT_READ);
+	gaim_input_remove(phb->inpa);
+	phb->func(phb->data, source, GAIM_INPUT_READ);
 	g_free(phb);
 }
 
@@ -78,7 +127,7 @@
 {
 	struct PHB *phb = data;
 
-	phb->func(phb->data, phb->port, GDK_INPUT_READ);
+	phb->func(phb->data, phb->port, GAIM_INPUT_READ);
 	g_free(phb);
 
 	return FALSE;
@@ -112,7 +161,7 @@
 	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
-			phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, no_one_calls, phb);
+			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
 		} else {
 			close(fd);
 			g_free(phb);
@@ -130,9 +179,9 @@
 		}
 		fcntl(fd, F_SETFL, 0);
 		phb->port = fd; /* bleh */
-		gtk_timeout_add(50, clean_connect, phb); /* we do this because we never
-							    want to call our callback
-							    before we return. */
+		g_timeout_add(50, clean_connect, phb); /* we do this because we never
+							  want to call our callback
+							  before we return. */
 	}
 
 	return fd;
@@ -141,14 +190,14 @@
 #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established"
 #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
 
-static void http_canread(gpointer data, gint source, GdkInputCondition cond)
+static void http_canread(gpointer data, gint source, GaimInputCondition cond)
 {
 	int nlc = 0;
 	int pos = 0;
 	struct PHB *phb = data;
 	char inputline[8192];
 
-	gdk_input_remove(phb->inpa);
+	gaim_input_remove(phb->inpa);
 
 	while ((nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
 		if (inputline[pos-1] == '\n')
@@ -162,20 +211,20 @@
 
 	if ((memcmp(HTTP_GOODSTRING , inputline, strlen(HTTP_GOODSTRING )) == 0) ||
 	    (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
-		phb->func(phb->data, source, GDK_INPUT_READ);
+		phb->func(phb->data, source, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
 	close(source);
-	phb->func(phb->data, -1, GDK_INPUT_READ);
+	phb->func(phb->data, -1, GAIM_INPUT_READ);
 	g_free(phb->host);
 	g_free(phb);
 	return;
 }
 
-static void http_canwrite(gpointer data, gint source, GdkInputCondition cond)
+static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
 {
 	char cmd[384];
 	struct PHB *phb = data;
@@ -183,11 +232,11 @@
 	int error = ETIMEDOUT;
 	debug_printf("Connected\n");
 	if (phb->inpa > 0)
-		gdk_input_remove(phb->inpa);
+		gaim_input_remove(phb->inpa);
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -197,7 +246,7 @@
 	g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\n", phb->host, phb->port);
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -212,7 +261,7 @@
 		g_free(t2);
 		if (send(source, cmd, strlen(cmd), 0) < 0) {
 			close(source);
-			phb->func(phb->data, -1, GDK_INPUT_READ);
+			phb->func(phb->data, -1, GAIM_INPUT_READ);
 			g_free(phb->host);
 			g_free(phb);
 			return;
@@ -222,13 +271,13 @@
 	g_snprintf(cmd, sizeof(cmd), "\r\n");
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
-	phb->inpa = gdk_input_add(source, GDK_INPUT_READ, http_canread, phb);
+	phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
 }
 
 static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb)
@@ -261,7 +310,7 @@
 	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
-			phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, http_canwrite, phb);
+			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, http_canwrite, phb);
 		} else {
 			close(fd);
 			g_free(phb->host);
@@ -280,34 +329,34 @@
 			return -1;
 		}
 		fcntl(fd, F_SETFL, 0);
-		http_canwrite(phb, fd, GDK_INPUT_WRITE);
+		http_canwrite(phb, fd, GAIM_INPUT_WRITE);
 	}
 
 	return fd;
 }
 
-static void s4_canread(gpointer data, gint source, GdkInputCondition cond)
+static void s4_canread(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char packet[12];
 	struct PHB *phb = data;
 
-	gdk_input_remove(phb->inpa);
+	gaim_input_remove(phb->inpa);
 
 	memset(packet, 0, sizeof(packet));
 	if (read(source, packet, 9) >= 4 && packet[1] == 90) {
-		phb->func(phb->data, source, GDK_INPUT_READ);
+		phb->func(phb->data, source, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
 	close(source);
-	phb->func(phb->data, -1, GDK_INPUT_READ);
+	phb->func(phb->data, -1, GAIM_INPUT_READ);
 	g_free(phb->host);
 	g_free(phb);
 }
 
-static void s4_canwrite(gpointer data, gint source, GdkInputCondition cond)
+static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char packet[12];
 	struct hostent *hp;
@@ -316,11 +365,11 @@
 	int error = ETIMEDOUT;
 	debug_printf("Connected\n");
 	if (phb->inpa > 0)
-		gdk_input_remove(phb->inpa);
+		gaim_input_remove(phb->inpa);
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -330,7 +379,7 @@
 	/* XXX does socks4 not support host name lookups by the proxy? */
 	if (!(hp = gethostbyname(phb->host))) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -347,13 +396,13 @@
 	packet[8] = 0;
 	if (write(source, packet, 9) != 9) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
-	phb->inpa = gdk_input_add(source, GDK_INPUT_READ, s4_canread, phb);
+	phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
 }
 
 static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb)
@@ -386,7 +435,7 @@
 	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
-			phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, s4_canwrite, phb);
+			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s4_canwrite, phb);
 		} else {
 			close(fd);
 			g_free(phb->host);
@@ -405,24 +454,24 @@
 			return -1;
 		}
 		fcntl(fd, F_SETFL, 0);
-		s4_canwrite(phb, fd, GDK_INPUT_WRITE);
+		s4_canwrite(phb, fd, GAIM_INPUT_WRITE);
 	}
 
 	return fd;
 }
 
-static void s5_canread_again(gpointer data, gint source, GdkInputCondition cond)
+static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char buf[512];
 	struct PHB *phb = data;
 
-	gdk_input_remove(phb->inpa);
+	gaim_input_remove(phb->inpa);
 	debug_printf("able to read again\n");
 
 	if (read(source, buf, 10) < 10) {
 		debug_printf("or not...\n");
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -430,13 +479,13 @@
 	if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
 		debug_printf("bad data\n");
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
-	phb->func(phb->data, source, GDK_INPUT_READ);
+	phb->func(phb->data, source, GAIM_INPUT_READ);
 	g_free(phb->host);
 	g_free(phb);
 	return;
@@ -459,26 +508,26 @@
 
 	if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
-	phb->inpa = gdk_input_add(source, GDK_INPUT_READ, s5_canread_again, phb);
+	phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
 }
 
-static void s5_readauth(gpointer data, gint source, GdkInputCondition cond)
+static void s5_readauth(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char buf[512];
 	struct PHB *phb = data;
 
-	gdk_input_remove(phb->inpa);
+	gaim_input_remove(phb->inpa);
 	debug_printf("got auth response\n");
 
 	if (read(source, buf, 2) < 2) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -486,7 +535,7 @@
 
 	if ((buf[0] != 0x01) || (buf[1] == 0x00)) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -495,17 +544,17 @@
 	s5_sendconnect(phb, source);
 }
 
-static void s5_canread(gpointer data, gint source, GdkInputCondition cond)
+static void s5_canread(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char buf[512];
 	struct PHB *phb = data;
 
-	gdk_input_remove(phb->inpa);
+	gaim_input_remove(phb->inpa);
 	debug_printf("able to read\n");
 
 	if (read(source, buf, 2) < 2) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -513,7 +562,7 @@
 
 	if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -528,19 +577,19 @@
 		memcpy(buf+2+i+1, proxypass, j);
 		if (write(source, buf, 3+i+j) < 3+i+j) {
 			close(source);
-			phb->func(phb->data, -1, GDK_INPUT_READ);
+			phb->func(phb->data, -1, GAIM_INPUT_READ);
 			g_free(phb->host);
 			g_free(phb);
 			return;
 		}
 
-		phb->inpa = gdk_input_add(source, GDK_INPUT_READ, s5_readauth, phb);
+		phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
 	} else {
 		s5_sendconnect(phb, source);
 	}
 }
 
-static void s5_canwrite(gpointer data, gint source, GdkInputCondition cond)
+static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
 {
 	unsigned char buf[512];
 	int i;
@@ -549,11 +598,11 @@
 	int error = ETIMEDOUT;
 	debug_printf("Connected\n");
 	if (phb->inpa > 0)
-		gdk_input_remove(phb->inpa);
+		gaim_input_remove(phb->inpa);
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
@@ -576,13 +625,13 @@
 	if (write(source, buf, i) < i) {
 		debug_printf("unable to write\n");
 		close(source);
-		phb->func(phb->data, -1, GDK_INPUT_READ);
+		phb->func(phb->data, -1, GAIM_INPUT_READ);
 		g_free(phb->host);
 		g_free(phb);
 		return;
 	}
 
-	phb->inpa = gdk_input_add(source, GDK_INPUT_READ, s5_canread, phb);
+	phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
 }
 
 static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb)
@@ -615,7 +664,7 @@
 	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
 		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			debug_printf("Connect would have blocked\n");
-			phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, s5_canwrite, phb);
+			phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, s5_canwrite, phb);
 		} else {
 			close(fd);
 			g_free(phb->host);
@@ -634,13 +683,13 @@
 			return -1;
 		}
 		fcntl(fd, F_SETFL, 0);
-		s5_canwrite(phb, fd, GDK_INPUT_WRITE);
+		s5_canwrite(phb, fd, GAIM_INPUT_WRITE);
 	}
 
 	return fd;
 }
 
-int proxy_connect(char *host, int port, GdkInputFunction func, gpointer data)
+int proxy_connect(char *host, int port, GaimInputFunction func, gpointer data)
 {
 	struct PHB *phb = g_new0(struct PHB, 1);
 	phb->func = func;