changeset 8231:f50c059b6384

[gaim-migrate @ 8954] This is Tim Ringenbach's patch to move some IP-related functions into the new gaim_network namespace, improve the local IP checking functionality by opening a socket, change some prefs, and add the ability to modify these prefs in the UI. Some ft.c bugs were fixed, and OSCAR, Jabber and Yahoo were updated to reflect the changes. The DCC SEND portion of this patch was not committed, as per his request (unless I misunderstood? :) committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Thu, 12 Feb 2004 00:36:55 +0000
parents 4e354776ae2a
children 1c6a9bc1ceea
files src/Makefile.am src/core.c src/ft.c src/ft.h src/gtkprefs.c src/network.c src/network.h src/protocols/jabber/oob.c src/protocols/jabber/si.c src/protocols/oscar/oscar.c src/protocols/yahoo/yahoo_filexfer.c src/proxy.c
diffstat 12 files changed, 495 insertions(+), 170 deletions(-) [+]
line wrap: on
line diff
--- a/src/Makefile.am	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/Makefile.am	Thu Feb 12 00:36:55 2004 +0000
@@ -79,6 +79,8 @@
 	md5.c \
 	md5.h \
 	multi.h \
+	network.c \
+	network.h \
 	notify.c \
 	notify.h \
 	plugin.c \
--- a/src/core.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/core.c	Thu Feb 12 00:36:55 2004 +0000
@@ -27,7 +27,7 @@
 #include "conversation.h"
 #include "core.h"
 #include "debug.h"
-#include "ft.h"
+#include "network.h"
 #include "plugin.h"
 #include "pounce.h"
 #include "prefs.h"
@@ -88,12 +88,12 @@
 	gaim_blist_init();
 	gaim_log_init();
 	gaim_buddy_icons_init();
+	gaim_network_init();
 	gaim_privacy_init();
 	gaim_pounces_init();
 	gaim_proxy_init();
 	gaim_sound_init();
 	gaim_ssl_init();
-	gaim_xfers_init();
 
 	if (ops != NULL && ops->ui_init != NULL)
 		ops->ui_init();
--- a/src/ft.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/ft.c	Thu Feb 12 00:36:55 2004 +0000
@@ -24,6 +24,7 @@
  */
 #include "internal.h"
 #include "ft.h"
+#include "network.h"
 #include "notify.h"
 #include "prefs.h"
 #include "proxy.h"
@@ -48,8 +49,6 @@
 	xfer->who     = g_strdup(who);
 	xfer->ui_ops  = gaim_xfers_get_ui_ops();
 
-	xfer->local_ip = g_strdup(gaim_xfers_get_ip_for_account(account));
-
 	ui_ops = gaim_xfer_get_ui_ops(xfer);
 
 	if (ui_ops != NULL && ui_ops->new_xfer != NULL)
@@ -77,7 +76,6 @@
 	g_free(xfer->filename);
 
 	if (xfer->remote_ip != NULL) g_free(xfer->remote_ip);
-	if (xfer->local_ip  != NULL) g_free(xfer->local_ip);
 
 	if (xfer->local_filename != NULL)
 		g_free(xfer->local_filename);
@@ -280,14 +278,6 @@
 			(double)gaim_xfer_get_size(xfer));
 }
 
-const char *
-gaim_xfer_get_local_ip(const GaimXfer *xfer)
-{
-	g_return_val_if_fail(xfer != NULL, NULL);
-
-	return xfer->local_ip;
-}
-
 unsigned int
 gaim_xfer_get_local_port(const GaimXfer *xfer)
 {
@@ -394,7 +384,7 @@
 }
 
 void
-gaim_xfer_set_read_fnc(GaimXfer *xfer, size_t (*fnc)(char **, GaimXfer *))
+gaim_xfer_set_read_fnc(GaimXfer *xfer, ssize_t (*fnc)(char **, GaimXfer *))
 {
 	g_return_if_fail(xfer != NULL);
 
@@ -403,7 +393,7 @@
 
 void
 gaim_xfer_set_write_fnc(GaimXfer *xfer,
-						size_t (*fnc)(const char *, size_t, GaimXfer *))
+						ssize_t (*fnc)(const char *, size_t, GaimXfer *))
 {
 	g_return_if_fail(xfer != NULL);
 
@@ -451,10 +441,10 @@
 	xfer->ops.cancel_recv = fnc;
 }
 
-size_t
+ssize_t
 gaim_xfer_read(GaimXfer *xfer, char **buffer)
 {
-	size_t s, r;
+	ssize_t s, r;
 
 	g_return_val_if_fail(xfer   != NULL, 0);
 	g_return_val_if_fail(buffer != NULL, 0);
@@ -478,10 +468,10 @@
 	return r;
 }
 
-size_t
+ssize_t
 gaim_xfer_write(GaimXfer *xfer, const char *buffer, size_t size)
 {
-	size_t r, s;
+	ssize_t r, s;
 
 	g_return_val_if_fail(xfer   != NULL, 0);
 	g_return_val_if_fail(buffer != NULL, 0);
@@ -489,10 +479,13 @@
 
 	s = MIN(gaim_xfer_get_bytes_remaining(xfer), size);
 
-	if (xfer->ops.write != NULL)
+	if (xfer->ops.write != NULL) {
 		r = (xfer->ops.write)(buffer, s, xfer);
-	else
+	} else {
 		r = write(xfer->fd, buffer, s);
+		if ((gaim_xfer_get_bytes_sent(xfer)+r) >= gaim_xfer_get_size(xfer))
+			gaim_xfer_set_completed(xfer, TRUE);
+	}
 
 	return r;
 }
@@ -507,8 +500,12 @@
 
 	if (condition & GAIM_INPUT_READ) {
 		r = gaim_xfer_read(xfer, &buffer);
-		if (r > 0)
+		if (r > 0) {
 			fwrite(buffer, 1, r, xfer->dest_fp);
+		} else {
+			gaim_xfer_cancel_remote(xfer);
+			return;
+		}
 	}
 	else {
 		size_t s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096);
@@ -520,7 +517,11 @@
 		/* Write as much as we're allowed to. */
 		r = gaim_xfer_write(xfer, buffer, s);
 
-		if (r < s) {
+		if (r == -1) {
+			gaim_xfer_cancel_remote(xfer);
+			g_free(buffer);
+			return;
+		} else if (r < s) {
 			/* We have to seek back in the file now. */
 			fseek(xfer->dest_fp, r - s, SEEK_CUR);
 		}
@@ -765,72 +766,6 @@
 /**************************************************************************
  * File Transfer Subsystem API
  **************************************************************************/
-void
-gaim_xfers_set_local_ip(const char *ip)
-{
-	g_return_if_fail(ip != NULL);
-
-	gaim_prefs_set_string("/core/ft/public_ip", ip);
-}
-
-const char *
-gaim_xfers_get_local_ip(void)
-{
-	const char *ip;
-
-	ip = gaim_prefs_get_string("/core/ft/public_ip");
-
-	if (ip == NULL || *ip == '\0')
-		return NULL;
-
-	return ip;
-}
-
-const char *
-gaim_xfers_get_local_system_ip(void)
-{
-	struct hostent *host;
-	char localhost[129];
-	long unsigned add;
-	static char ip[46];
-
-	if (gethostname(localhost, 128) < 0)
-		return NULL;
-
-	if ((host = gethostbyname(localhost)) == NULL)
-		return NULL;
-
-	memcpy(&add, host->h_addr_list[0], 4);
-	add = htonl(add);
-
-	g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
-			   ((add >> 24) & 255),
-			   ((add >> 16) & 255),
-			   ((add >>  8) & 255),
-			   add & 255);
-
-	return ip;
-}
-
-const char *
-gaim_xfers_get_ip_for_account(const GaimAccount *account)
-{
-	g_return_val_if_fail(account != NULL, NULL);
-
-	if (gaim_account_get_public_ip(account) != NULL)
-		return gaim_account_get_public_ip(account);
-	else if (gaim_xfers_get_local_ip() != NULL)
-		return gaim_xfers_get_local_ip();
-	else
-		return gaim_xfers_get_local_system_ip();
-}
-
-void
-gaim_xfers_init(void)
-{
-	gaim_prefs_add_none("/core/ft");
-	gaim_prefs_add_string("/core/ft/public_ip", "");
-}
 
 void
 gaim_xfers_set_ui_ops(GaimXferUiOps *ops)
--- a/src/ft.h	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/ft.h	Thu Feb 12 00:36:55 2004 +0000
@@ -7,7 +7,7 @@
  * Gaim is the legal property of its developers, whose names are too numerous
  * to list here.  Please refer to the COPYRIGHT file distributed with this
  * source distribution.
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -94,7 +94,6 @@
 
 	FILE *dest_fp;                /**< The destination file pointer.       */
 
-	char *local_ip;               /**< The local IP address.               */
 	char *remote_ip;              /**< The remote IP address.              */
 	int local_port;               /**< The local port.                     */
 	int remote_port;              /**< The remote port.                    */
@@ -116,8 +115,8 @@
 		void (*end)(GaimXfer *xfer);
 		void (*cancel_send)(GaimXfer *xfer);
 		void (*cancel_recv)(GaimXfer *xfer);
-		size_t (*read)(char **buffer, GaimXfer *xfer);
-		size_t (*write)(const char *buffer, size_t size, GaimXfer *xfer);
+		ssize_t (*read)(char **buffer, GaimXfer *xfer);
+		ssize_t (*write)(const char *buffer, size_t size, GaimXfer *xfer);
 		void (*ack)(GaimXfer *xfer, const char *buffer, size_t size);
 
 	} ops;
@@ -296,15 +295,6 @@
 double gaim_xfer_get_progress(const GaimXfer *xfer);
 
 /**
- * Returns the local IP address in the file transfer.
- *
- * @param xfer The file transfer.
- *
- * @return The IP address on this end.
- */
-const char *gaim_xfer_get_local_ip(const GaimXfer *xfer);
-
-/**
  * Returns the local port number in the file transfer.
  *
  * @param xfer The file transfer.
@@ -379,7 +369,7 @@
  * @param fnc  The read function.
  */
 void gaim_xfer_set_read_fnc(GaimXfer *xfer,
-		size_t (*fnc)(char **, GaimXfer *));
+		ssize_t (*fnc)(char **, GaimXfer *));
 
 /**
  * Sets the write function for the file transfer.
@@ -388,7 +378,7 @@
  * @param fnc  The write function.
  */
 void gaim_xfer_set_write_fnc(GaimXfer *xfer,
-		size_t (*fnc)(const char *, size_t, GaimXfer *));
+		ssize_t (*fnc)(const char *, size_t, GaimXfer *));
 
 /**
  * Sets the acknowledge function for the file transfer.
@@ -457,9 +447,9 @@
  * @param xfer   The file transfer.
  * @param buffer The buffer that will be created to contain the data.
  *
- * @return The number of bytes read.
+ * @return The number of bytes read, or -1.
  */
-size_t gaim_xfer_read(GaimXfer *xfer, char **buffer);
+ssize_t gaim_xfer_read(GaimXfer *xfer, char **buffer);
 
 /**
  * Writes data to a file transfer stream.
@@ -468,9 +458,9 @@
  * @param buffer The buffer to read the data from.
  * @param size   The number of bytes to write.
  *
- * @return The number of bytes written.
+ * @return The number of bytes written, or -1.
  */
-size_t gaim_xfer_write(GaimXfer *xfer, const char *buffer, size_t size);
+ssize_t gaim_xfer_write(GaimXfer *xfer, const char *buffer, size_t size);
 
 /**
  * Starts a file transfer.
@@ -524,62 +514,6 @@
 /*@}*/
 
 /**************************************************************************/
-/** @name File Transfer Subsystem API                                     */
-/**************************************************************************/
-/*@{*/
-
-/**
- * Sets the IP address of the local system in preferences.
- *
- * @param ip The local IP address.
- */
-void gaim_xfers_set_local_ip(const char *ip);
-
-/**
- * Returns the IP address of the local system set in preferences.
- *
- * @return The local IP address set in preferences.
- */
-const char *gaim_xfers_get_local_ip(void);
-
-/**
- * Returns the IP address of the local system.
- *
- * @note The returned string is a pointer to a static buffer. If this
- *       function is called twice, it may be important to make a copy
- *       of the returned string.
- *
- * @return The local IP address.
- */
-const char *gaim_xfers_get_local_system_ip(void);
-
-/**
- * Returns the IP address that should be used for the specified account.
- *
- * First, the IP associated with @a account is tried, via a call to
- * gaim_account_get_local_ip().
- *
- * If that IP is not set, the IP set in preferences is tried.
- *
- * If that IP is not set, the system's local IP is tried, via a call to
- * gaim_xfers_get_local_ip().
- *
- * @note The returned string is a pointer to a static buffer. If this
- *       function is called twice, it may be important to make a copy
- *       of the returned string.
- *
- * @return The local IP address to be used.
- */
-const char *gaim_xfers_get_ip_for_account(const GaimAccount *account);
-
-/**
- * Initializes the file transfer subsystem.
- */
-void gaim_xfers_init(void);
-
-/*@}*/
-
-/**************************************************************************/
 /** @name UI Registration Functions                                       */
 /**************************************************************************/
 /*@{*/
--- a/src/gtkprefs.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/gtkprefs.c	Thu Feb 12 00:36:55 2004 +0000
@@ -33,6 +33,7 @@
 #include "sound.h"
 #include "util.h"
 #include "multi.h"
+#include "network.h"
 
 #include "gtkblist.h"
 #include "gtkconv.h"
@@ -1050,6 +1051,81 @@
 	return ret;
 }
 
+static void network_ip_changed(GtkEntry *entry, gpointer data)
+{
+	gaim_prefs_set_string("/core/network/public_ip", gtk_entry_get_text(entry));
+}
+
+GtkWidget *network_page() {
+	GtkWidget *ret;
+	GtkWidget *vbox, *entry;
+	GtkWidget *table, *label, *auto_ip_checkbox, *ports_checkbox, *spin_button;
+	GtkSizeGroup *sg;
+
+	ret = gtk_vbox_new(FALSE, 18);
+	gtk_container_set_border_width (GTK_CONTAINER (ret), 12);
+
+	vbox = gaim_gtk_make_frame (ret, _("IP Address"));
+
+	auto_ip_checkbox = gaim_gtk_prefs_checkbox(_("_Autodetect IP Address"),
+			"/core/network/auto_ip", vbox);
+
+	table = gtk_table_new(2, 1, FALSE);
+	gtk_container_set_border_width(GTK_CONTAINER(table), 5);
+	gtk_table_set_col_spacings(GTK_TABLE(table), 5);
+	gtk_table_set_row_spacings(GTK_TABLE(table), 10);
+	gtk_container_add(GTK_CONTAINER(vbox), table);
+
+	label = gtk_label_new_with_mnemonic(_("Public _IP:"));
+	gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
+	gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
+
+	entry = gtk_entry_new();
+	gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
+	gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
+	g_signal_connect(G_OBJECT(entry), "changed",
+					 G_CALLBACK(network_ip_changed), NULL);
+
+	if (gaim_network_get_local_ip() != NULL)
+		gtk_entry_set_text(GTK_ENTRY(entry),
+		                   gaim_network_get_local_ip());
+
+	gaim_set_accessible_label (entry, label);
+
+
+	if (gaim_prefs_get_bool("/core/network/auto_ip")) {
+		gtk_widget_set_sensitive(GTK_WIDGET(table), FALSE);
+	}
+
+	g_signal_connect(G_OBJECT(auto_ip_checkbox), "clicked",
+					 G_CALLBACK(gaim_gtk_toggle_sensitive), table);
+
+
+
+	vbox = gaim_gtk_make_frame (ret, _("Ports"));
+	sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
+
+	ports_checkbox = gaim_gtk_prefs_checkbox(_("_Manually specify range of ports to listen on"),
+			"/core/network/ports_range_use", vbox);
+
+	spin_button = gaim_gtk_prefs_labeled_spin_button(vbox, _("_Start Port:"),
+			"/core/network/ports_range_start", 0, 65535, sg);
+	if (!gaim_prefs_get_bool("/core/network/ports_range_use"))
+		gtk_widget_set_sensitive(GTK_WIDGET(spin_button), FALSE);
+	g_signal_connect(G_OBJECT(ports_checkbox), "clicked",
+					 G_CALLBACK(gaim_gtk_toggle_sensitive), spin_button);
+
+	spin_button = gaim_gtk_prefs_labeled_spin_button(vbox, _("_End Port:"),
+			"/core/network/ports_range_end", 0, 65535, sg);
+	if (!gaim_prefs_get_bool("/core/network/ports_range_use"))
+		gtk_widget_set_sensitive(GTK_WIDGET(spin_button), FALSE);
+	g_signal_connect(G_OBJECT(ports_checkbox), "clicked",
+					 G_CALLBACK(gaim_gtk_toggle_sensitive), spin_button);
+
+	gtk_widget_show_all(ret);
+	return ret;
+}
+
 static void
 proxy_changed_cb(const char *name, GaimPrefType type, gpointer value,
 		gpointer data)
@@ -2373,6 +2449,7 @@
 	prefs_notebook_add_page(_("Conversations"), NULL, conv_page(), &p2, NULL, notebook_page++);
 	prefs_notebook_add_page(_("IMs"), NULL, im_page(), &c, &p2, notebook_page++);
 	prefs_notebook_add_page(_("Chats"), NULL, chat_page(), &c, &p2, notebook_page++);
+	/* XXX */prefs_notebook_add_page(_("Network"), NULL, network_page(), &p, NULL, notebook_page++);
 	prefs_notebook_add_page(_("Proxy"), NULL, proxy_page(), &p, NULL, notebook_page++);
 #ifndef _WIN32
 	/* We use the registered default browser in windows */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network.c	Thu Feb 12 00:36:55 2004 +0000
@@ -0,0 +1,244 @@
+/**
+ * @file network.c Network Implementation
+ * @ingroup core
+ *
+ * gaim
+ *
+ * Gaim is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/stat.h>
+#include <arpa/inet.h>
+
+#include "debug.h"
+#include "account.h"
+#include "network.h"
+#include "prefs.h"
+
+void
+gaim_network_set_local_ip(const char *ip)
+{
+	g_return_if_fail(ip != NULL);
+
+	gaim_prefs_set_string("/core/network/public_ip", ip);
+}
+
+const char *
+gaim_network_get_local_ip(void)
+{
+	const char *ip;
+
+	if (gaim_prefs_get_bool("/core/network/auto_ip"))
+		return NULL;
+
+	ip = gaim_prefs_get_string("/core/network/public_ip");
+
+	if (ip == NULL || *ip == '\0')
+		return NULL;
+
+	return ip;
+}
+
+static const char *
+gaim_network_get_local_ip_from_fd(int fd)
+{
+	struct sockaddr_in addr;
+	socklen_t len;
+	static char ip[16];
+	const char *tmp;
+
+	g_return_val_if_fail(fd > 0, NULL);
+
+	len = sizeof(addr);
+	if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
+		gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
+		return NULL;
+	}
+
+	tmp = inet_ntoa(addr.sin_addr);
+	strncpy(ip, tmp, sizeof(ip));
+	return ip;
+}
+
+const char *
+gaim_network_get_local_system_ip(int fd)
+{
+	struct hostent *host;
+	char localhost[129];
+	long unsigned add;
+	static char ip[46];
+	const char *tmp = NULL;
+
+	if (fd != -1)
+		tmp = gaim_network_get_local_ip_from_fd(fd);
+
+	if (tmp)
+		return tmp;
+
+	if (gethostname(localhost, 128) < 0)
+		return NULL;
+
+	if ((host = gethostbyname(localhost)) == NULL)
+		return NULL;
+
+	memcpy(&add, host->h_addr_list[0], 4);
+	add = htonl(add);
+
+	g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
+			   ((add >> 24) & 255),
+			   ((add >> 16) & 255),
+			   ((add >>  8) & 255),
+			   add & 255);
+
+	return ip;
+}
+
+const char *
+gaim_network_get_ip_for_account(const GaimAccount *account, int fd)
+{
+	if (account && (gaim_account_get_public_ip(account) != NULL))
+		return gaim_account_get_public_ip(account);
+	else if (gaim_network_get_local_ip() != NULL)
+		return gaim_network_get_local_ip();
+	else
+		return gaim_network_get_local_system_ip(fd);
+}
+
+static int gaim_network_do_listen(short portnum)
+{
+#if HAVE_GETADDRINFO
+	int listenfd;
+	const int on = 1;
+	struct addrinfo hints, *res, *ressave;
+	char serv[5];
+
+	snprintf(serv, sizeof(serv), "%d", portnum);
+	memset(&hints, 0, sizeof(struct addrinfo));
+	hints.ai_flags = AI_PASSIVE;
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	if (getaddrinfo(NULL /* any IP */, serv, &hints, &res) != 0) {
+		gaim_debug_warning("network", "getaddrinfo: %s\n", strerror(errno));
+		return -1;
+	}
+	ressave = res;
+	do {
+		listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+		if (listenfd < 0)
+			continue;
+		setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+		if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
+			break; /* success */
+		close(listenfd);
+	} while ( (res = res->ai_next) );
+
+	if (!res)
+		return -1;
+
+	freeaddrinfo(ressave);
+#else
+	int listenfd;
+	const int on = 1;
+	struct sockaddr_in sockin;
+
+	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+		gaim_debug_warning("network", "socket: %s\n", strerror(errno));
+		return -1;
+	}
+
+	if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) {
+		gaim_debug_warning("network", "setsockopt: %s\n", strerror(errno));
+		close(listenfd);
+		return -1;
+	}
+
+	memset(&sockin, 0, sizeof(struct sockaddr_in));
+	sockin.sin_family = AF_INET;
+	sockin.sin_port = htons(portnum);
+
+	if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
+		gaim_debug_warning("network", "bind: %s\n", strerror(errno));
+		close(listenfd);
+		return -1;
+	}
+#endif
+
+	if (listen(listenfd, 4) != 0) {
+		gaim_debug_warning("network", "listen: %s\n", strerror(errno));
+		close(listenfd);
+		return -1;
+	}
+	fcntl(listenfd, F_SETFL, O_NONBLOCK);
+
+	gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd));
+	return listenfd;
+}
+
+int gaim_network_listen(short portnum)
+{
+	int ret = 0, start, end;
+
+	if (!gaim_prefs_get_bool("/core/network/ports_range_use") || portnum)
+		return gaim_network_do_listen(portnum);
+
+	start = gaim_prefs_get_int("/core/network/ports_range_start");
+	end = gaim_prefs_get_int("/core/network/ports_range_end");
+
+	for (; start <= end; start++) {
+		ret = gaim_network_do_listen(start);
+		if (ret >= 0)
+			break;
+	}
+
+	return ret;
+}
+
+short gaim_network_get_port_from_fd(int fd)
+{
+	struct sockaddr_in addr;
+	socklen_t len;
+
+	g_return_val_if_fail(fd > 0, 0);
+
+	len = sizeof(addr);
+	if (getsockname(fd, (struct sockaddr *) &addr, &len) == -1) {
+		gaim_debug_warning("network", "getsockname: %s\n", strerror(errno));
+		return 0;
+	}
+
+	return ntohs(addr.sin_port);
+}
+
+void
+gaim_network_init(void)
+{
+	gaim_prefs_add_none  ("/core/network");
+	gaim_prefs_add_bool  ("/core/network/auto_ip", TRUE);
+	gaim_prefs_add_string("/core/network/public_ip", "");
+	gaim_prefs_add_bool  ("/core/network/ports_range_use", FALSE);
+	gaim_prefs_add_int   ("/core/network/ports_range_start", 1024);
+	gaim_prefs_add_int   ("/core/network/ports_range_end", 2048);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network.h	Thu Feb 12 00:36:55 2004 +0000
@@ -0,0 +1,128 @@
+/**
+ * @file network.h Network API
+ * @ingroup core
+ *
+ * gaim
+ *
+ * Gaim is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#ifndef _GAIM_NETWORK_H_
+#define _GAIM_NETWORK_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**************************************************************************/
+/** @name Network API                                     */
+/**************************************************************************/
+/*@{*/
+
+/**
+ * Sets the IP address of the local system in preferences.
+ *
+ * @param ip The local IP address.
+ */
+void gaim_network_set_local_ip(const char *ip);
+
+/**
+ * Returns the IP address of the local system set in preferences.
+ *
+ * This returns the value set via gaim_network_set_local_ip().
+ * You probably want to use gaim_network_get_ip_for_account() instead.
+ *
+ * @return The local IP address set in preferences.
+ */
+const char *gaim_network_get_local_ip(void);
+
+/**
+ * Returns the IP address of the local system.
+ *
+ * You probably want to use gaim_network_get_ip_for_account() instead.
+ *
+ * @note The returned string is a pointer to a static buffer. If this
+ *       function is called twice, it may be important to make a copy
+ *       of the returned string.
+ *
+ * @param fd The fd to use to help figure out the IP, or else -1.
+ * @return The local IP address.
+ */
+const char *gaim_network_get_local_system_ip(int fd);
+
+/**
+ * Returns the IP address that should be used for the specified account.
+ *
+ * First, if @a account is not @c NULL, the IP associated with @a account
+ * is tried, via a call to gaim_account_get_local_ip().
+ *
+ * If that IP is not set, the IP set in preferences is tried.
+ *
+ * If that IP is not set, the system's local IP is tried, via a call to
+ * gaim_network_get_local_ip().
+ *
+ * @note The returned string is a pointer to a static buffer. If this
+ *       function is called twice, it may be important to make a copy
+ *       of the returned string.
+ *
+ * @param account The account to use. This may be @c NULL, and if so
+ *                the first step listed above is skipped.
+ * @param fd The fd to use to help figure out the IP, or -1.
+ * @return The local IP address to be used.
+ */
+const char *gaim_network_get_ip_for_account(const GaimAccount *account, int fd);
+
+/**
+ * Opens a listening port.
+ *
+ * This opens a listening port. The caller will want to set up a watcher
+ * of type GAIM_INPUT_READ on the returned fd. It will probably call
+ * accept in the callback, and then possibly remove the watcher and close
+ * the listening socket, and add a new watcher on the new socket accept
+ * returned.
+ *
+ * @param portnum The port number to bind to, or 0, to let the core decide.
+ *                By default, the core will let the kernel pick one at random,
+ *                but users are allowed to specify a range.
+ *
+ * @return The file descriptor of the listening socket.
+ */
+int gaim_network_listen(short portnum);
+
+/**
+ * Gets a port number from a file descriptor.
+ *
+ * @param fd The file descriptor. This should be a tcp socket. The current
+ *           implementation probably dies on anything but IPv4. Perhaps this
+ *           possible bug will inspire new and valuable contributors to Gaim.
+ * @return The port number, in host byte order.
+ */
+short gaim_network_get_port_from_fd(int fd);
+
+/**
+ * Initializes the network subsystem.
+ */
+void gaim_network_init(void);
+
+/*@}*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _GAIM_NETWORK_H_ */
--- a/src/protocols/jabber/oob.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/protocols/jabber/oob.c	Thu Feb 12 00:36:55 2004 +0000
@@ -85,7 +85,7 @@
 	g_free(buf);
 }
 
-static size_t jabber_oob_xfer_read(char **buffer, GaimXfer *xfer) {
+static ssize_t jabber_oob_xfer_read(char **buffer, GaimXfer *xfer) {
 	JabberOOBXfer *jox = xfer->data;
 	char test;
 	int size;
--- a/src/protocols/jabber/si.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/protocols/jabber/si.c	Thu Feb 12 00:36:55 2004 +0000
@@ -21,6 +21,7 @@
 #include "internal.h"
 #include "debug.h"
 #include "ft.h"
+#include "network.h"
 #include "notify.h"
 #include "util.h"
 
@@ -69,6 +70,7 @@
 	GaimXfer *xfer;
 	JabberSIXfer *jsx;
 	xmlnode *si, *feature, *x, *field, *value;
+	GaimAccount *account = gaim_connection_get_account(js->gc);
 
 	si = xmlnode_get_child(packet, "si");
 
@@ -130,7 +132,7 @@
 		streamhost = xmlnode_new_child(query, "streamhost");
 		xmlnode_set_attrib(streamhost, "jid",
 				gaim_account_get_username(js->gc->account));
-		xmlnode_set_attrib(streamhost, "host", xfer->local_ip);
+		xmlnode_set_attrib(streamhost, "host", gaim_network_get_ip_for_account(account, js->fd));
 		buf = g_strdup_printf("%d", xfer->local_port);
 		xmlnode_set_attrib(streamhost, "port", buf);
 		g_free(buf);
--- a/src/protocols/oscar/oscar.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/protocols/oscar/oscar.c	Thu Feb 12 00:36:55 2004 +0000
@@ -35,6 +35,7 @@
 #include "ft.h"
 #include "imgstore.h"
 #include "multi.h"
+#include "network.h"
 #include "notify.h"
 #include "privacy.h"
 #include "prpl.h"
@@ -1032,8 +1033,10 @@
 
 static void oscar_ask_sendfile(GaimConnection *gc, const char *destsn) {
 	OscarData *od = (OscarData *)gc->proto_data;
+	GaimAccount *account = gaim_connection_get_account(gc);
 	GaimXfer *xfer;
 	struct aim_oft_info *oft_info;
+	const char *ip;
 
 	/* You want to send a file to someone else, you're so generous */
 
@@ -1042,7 +1045,8 @@
 	xfer->local_port = 5190;
 
 	/* Create the oscar-specific data */
-	oft_info = aim_oft_createinfo(od->sess, NULL, destsn, xfer->local_ip, xfer->local_port, 0, 0, NULL);
+	ip = gaim_network_get_ip_for_account(account, od->conn?od->conn->fd:-1);
+	oft_info = aim_oft_createinfo(od->sess, NULL, destsn, ip, xfer->local_port, 0, 0, NULL);
 	xfer->data = oft_info;
 
 	 /* Setup our I/O op functions */
--- a/src/protocols/yahoo/yahoo_filexfer.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/protocols/yahoo/yahoo_filexfer.c	Thu Feb 12 00:36:55 2004 +0000
@@ -247,7 +247,7 @@
 }
 
 
-size_t yahoo_xfer_read(char **buffer, GaimXfer *xfer)
+ssize_t yahoo_xfer_read(char **buffer, GaimXfer *xfer)
 {
 	gchar buf[4096];
 	ssize_t len;
@@ -308,7 +308,7 @@
 	return len;
 }
 
-size_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer)
+ssize_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer)
 {
 	ssize_t len;
 	struct yahoo_xfer_data *xd = xfer->data;
--- a/src/proxy.c	Wed Feb 11 22:34:55 2004 +0000
+++ b/src/proxy.c	Thu Feb 12 00:36:55 2004 +0000
@@ -1763,7 +1763,6 @@
 									connection_host_resolved, phb);
 }
 
-
 static void
 proxy_pref_cb(const char *name, GaimPrefType type, gpointer value,
 			  gpointer data)