changeset 13909:8264f52a1142

[gaim-migrate @ 16406] More of sf patch #1490646, from Jonty Wareing & Jono Cole "The screen name + hostname of the sending user is sent in the outgoing jabber message, fixing a sporadic problem with iChat. The port in use has been fixed to the one described in the Bonjour specification and can no longer be changed in the prpl preferences - modifiying this just stops the client from being able to start a chat. The option for a buddy icon has been removed for now as no code actually uses it yet - we plan to change this in the future. This update also introduces automatic local port retry for up to ten attempts if the port is in use (e.g. if multiple instances of gaim are running)." committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 03 Jul 2006 00:37:41 +0000
parents cab785a7c766
children 6c907830a45f
files src/protocols/bonjour/bonjour.c src/protocols/bonjour/bonjour.h src/protocols/bonjour/buddy.c src/protocols/bonjour/dns_sd.c src/protocols/bonjour/dns_sd.h src/protocols/bonjour/jabber.c src/protocols/bonjour/jabber.h
diffstat 7 files changed, 42 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/bonjour/bonjour.c	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/bonjour.c	Mon Jul 03 00:37:41 2006 +0000
@@ -103,7 +103,7 @@
 
 	/* Start waiting for jabber connections (iChat style) */
 	bd->jabber_data = g_new(BonjourJabber, 1);
-	bd->jabber_data->port = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT);
+	bd->jabber_data->port = BONJOUR_DEFAULT_PORT_INT;
 	bd->jabber_data->account = account;
 
 	if (bonjour_jabber_start(bd->jabber_data) == -1) {
@@ -123,7 +123,7 @@
 	bd->dns_sd_data->version = g_strdup("1");
 	bd->dns_sd_data->first = g_strdup(gaim_account_get_string(account, "first", default_firstname));
 	bd->dns_sd_data->last = g_strdup(gaim_account_get_string(account, "last", default_lastname));
-	bd->dns_sd_data->port_p2pj = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT);
+	bd->dns_sd_data->port_p2pj = bd->jabber_data->port;
 	bd->dns_sd_data->phsh = g_strdup("");
 	bd->dns_sd_data->email = g_strdup(gaim_account_get_string(account, "email", ""));
 	bd->dns_sd_data->vc = g_strdup("");
@@ -356,7 +356,8 @@
 	OPT_PROTO_NO_PASSWORD,
 	NULL,                                                    /* user_splits */
 	NULL,                                                    /* protocol_options */
-	{"png", 0, 0, 96, 96, GAIM_ICON_SCALE_DISPLAY},          /* icon_spec */
+	/* {"png", 0, 0, 96, 96, GAIM_ICON_SCALE_DISPLAY}, */    /* icon_spec */
+	NO_BUDDY_ICONS, /* not yet */                            /* icon_spec */
 	bonjour_list_icon,                                       /* list_icon */
 	bonjour_list_emblems,                                    /* list_emblems */
 	bonjour_status_text,                                     /* status_text */
@@ -576,9 +577,6 @@
 	prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
 
 	/* Creating the options for the protocol */
-	option = gaim_account_option_int_new(_("Port"), "port", 5298);
-	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
-
 	option = gaim_account_option_string_new(_("First name"), "first", default_firstname);
 	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
 
--- a/src/protocols/bonjour/bonjour.h	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/bonjour.h	Mon Jul 03 00:37:41 2006 +0000
@@ -40,6 +40,8 @@
 #define BONJOUR_STATUS_ID_AVAILABLE "available"
 #define BONJOUR_STATUS_ID_AWAY      "away"
 
+#define BONJOUR_DEFAULT_PORT_INT 5298
+
 typedef struct _BonjourData
 {
 	BonjourDnsSd *dns_sd_data;
--- a/src/protocols/bonjour/buddy.c	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/buddy.c	Mon Jul 03 00:37:41 2006 +0000
@@ -69,10 +69,6 @@
 		return FALSE;
 	}
 
-	if (buddy->port_p2pj == -1) {
-		return FALSE;
-	}
-
 	if (buddy->status == NULL) {
 		return FALSE;
 	}
--- a/src/protocols/bonjour/dns_sd.c	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/dns_sd.c	Mon Jul 03 00:37:41 2006 +0000
@@ -21,27 +21,6 @@
 #include "buddy.h"
 #include "debug.h"
 
-/* Private data */
-
-typedef struct _dns_sd_packet
-{
-	gchar *name;
-	gchar *txtvers;
-	gchar *version;
-	gchar *first;
-	gchar *last;
-	gint port_p2pj;
-	gchar *phsh;
-	gchar *status;
-	gchar *message;
-	gchar *email;
-	gchar *vc;
-	gchar *jid;
-	gchar *AIM;
-} dns_sd_packet;
-
-/* End private data */
-
 /* Private functions */
 
 static sw_result HOWL_API
@@ -83,7 +62,6 @@
 	gchar *txtvers = NULL;
 	gchar *version = NULL;
 	gchar *first = NULL;
-	gint port_p2pj = -1;
 	gchar *phsh = NULL;
 	gchar *status = NULL;
 	gchar *email = NULL;
@@ -119,8 +97,6 @@
 				version = g_strdup(value);
 			} else if (strcmp(key, "1st") == 0) {
 				first = g_strdup(value);
-			} else if (strcmp(key, "port.p2pj") == 0) {
-				port_p2pj = atoi(value);
 			} else if (strcmp(key, "status") == 0) {
 				status = g_strdup(value);
 			} else if (strcmp(key, "email") == 0) {
@@ -143,7 +119,7 @@
 
 	/* Put the parameters of the text_record in a buddy and add the buddy to */
 	/* the buddy list */
-	buddy = bonjour_buddy_new(name, first, port_p2pj, phsh,
+	buddy = bonjour_buddy_new(name, first, port, phsh,
 							  status, email, last, jid, AIM, vc, ip, msg);
 
 	if (bonjour_buddy_check(buddy) == FALSE)
@@ -200,7 +176,7 @@
 			gaim_debug_warning("bonjour", "_browser_reply --> Remove domain\n");
 			break;
 		case SW_DISCOVERY_BROWSE_ADD_SERVICE:
-			/* A new peer has join the network and uses iChat bonjour */
+			/* A new peer has joined the network and uses iChat bonjour */
 			gaim_debug_info("bonjour", "_browser_reply --> Add service\n");
 			if (g_ascii_strcasecmp(name, account->username) != 0)
 			{
@@ -235,6 +211,7 @@
 {
 	sw_text_record dns_data;
 	sw_result publish_result = SW_OKAY;
+	char portstring[6];
 
 	/* Fill the data for the service */
 	if (sw_text_record_init(&dns_data) != SW_OKAY)
@@ -243,16 +220,20 @@
 		return -1;
 	}
 
+	/* Convert the port to a string */
+	snprintf(portstring, sizeof(portstring), "%d", data->port_p2pj);
+
+	/* Publish standard records */
 	sw_text_record_add_key_and_string_value(dns_data, "txtvers", data->txtvers);
 	sw_text_record_add_key_and_string_value(dns_data, "version", data->version);
 	sw_text_record_add_key_and_string_value(dns_data, "1st", data->first);
 	sw_text_record_add_key_and_string_value(dns_data, "last", data->last);
-	/* sw_text_record_add_key_and_string_value(dns_data, "port.p2pj", itoa(data->port_p2pj)); */
-	sw_text_record_add_key_and_string_value(dns_data, "port.p2pj", BONJOUR_DEFAULT_PORT);
+	sw_text_record_add_key_and_string_value(dns_data, "port.p2pj", portstring);
 	sw_text_record_add_key_and_string_value(dns_data, "phsh", data->phsh);
 	sw_text_record_add_key_and_string_value(dns_data, "status", data->status);
 	sw_text_record_add_key_and_string_value(dns_data, "vc", data->vc);
 
+	/* Publish extra records */
 	if ((data->email != NULL) && (*data->email != '\0'))
 		sw_text_record_add_key_and_string_value(dns_data, "email", data->email);
 
@@ -280,7 +261,7 @@
 	}
 	if (publish_result != SW_OKAY)
 	{
-		gaim_debug_error("bonjour", "Unable to publish or change the status of the _presence._tcp service.");
+		gaim_debug_error("bonjour", "Unable to publish or change the status of the _presence._tcp service.\n");
 		return -1;
 	}
 
--- a/src/protocols/bonjour/dns_sd.h	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/dns_sd.h	Mon Jul 03 00:37:41 2006 +0000
@@ -21,8 +21,6 @@
 #include <glib.h>
 #include "account.h"
 
-#define BONJOUR_DEFAULT_PORT "5298"
-#define BONJOUR_DEFAULT_PORT_INT 5298
 #define ICHAT_SERVICE "_presence._tcp."
 
 /**
--- a/src/protocols/bonjour/jabber.c	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/jabber.c	Mon Jul 03 00:37:41 2006 +0000
@@ -450,6 +450,8 @@
 {
 	struct sockaddr_in my_addr;
 	int yes = 1;
+	int i;
+	gboolean bind_successful;
 
 	/* Open a listening socket for incoming conversations */
 	if ((data->socket = socket(PF_INET, SOCK_STREAM, 0)) < 0)
@@ -469,19 +471,33 @@
 
 	memset(&my_addr, 0, sizeof(struct sockaddr_in));
 	my_addr.sin_family = PF_INET;
-	my_addr.sin_port = htons(data->port);
 
-	if (bind(data->socket, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) != 0)
+	/* Attempt to find a free port */
+	bind_successful = FALSE;
+	for (i = 0; i < 10; i++)
+	{
+		my_addr.sin_port = htons(data->port);
+		if (bind(data->socket, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == 0)
+		{
+			bind_successful = TRUE;
+			break;
+		}
+		data->port++;
+	}
+
+	/* On no!  We tried 10 ports and could not bind to ANY of them */
+	if (!bind_successful)
 	{
 		gaim_debug_error("bonjour", "Cannot bind socket: %s\n", strerror(errno));
-		gaim_connection_error(data->account->gc, _("Cannot bind socket to port"));
+		gaim_connection_error(data->account->gc, _("Could not bind socket to port"));
 		return -1;
 	}
 
+	/* Attempt to listen on the bound socket */
 	if (listen(data->socket, 10) != 0)
 	{
 		gaim_debug_error("bonjour", "Cannot listen on socket: %s\n", strerror(errno));
-		gaim_connection_error(data->account->gc, _("Cannot listen on socket"));
+		gaim_connection_error(data->account->gc, _("Could not listen on socket"));
 		return -1;
 	}
 
@@ -498,7 +514,7 @@
 	/* Open a watcher in the socket we have just opened */
 	data->watcher_id = gaim_input_add(data->socket, GAIM_INPUT_READ, _server_socket_handler, data);
 
-	return 0;
+	return data->port;
 }
 
 int
@@ -547,6 +563,7 @@
 
 	message_node = xmlnode_new("message");
 	xmlnode_set_attrib(message_node, "to", ((BonjourBuddy*)(gb->proto_data))->name);
+	xmlnode_set_attrib(message_node, "from", data->account->username);
 	xmlnode_set_attrib(message_node, "type", "chat");
 	xmlnode_insert_child(message_node, message_body_node);
 	xmlnode_insert_child(message_node, message_html_node);
--- a/src/protocols/bonjour/jabber.h	Sun Jul 02 23:56:08 2006 +0000
+++ b/src/protocols/bonjour/jabber.h	Mon Jul 03 00:37:41 2006 +0000
@@ -49,8 +49,10 @@
 } BonjourJabberConversation;
 
 /**
- * Start listening for jabber connections. Returns 0 if the connection could be
- * stablished, -1 if a problem appears.
+ * Start listening for jabber connections.
+ *
+ * @return -1 if there was a problem, else returns the listening
+ *         port number.
  */
 gint bonjour_jabber_start(BonjourJabber *data);