changeset 6827:1cfbb731aa1f

[gaim-migrate @ 7372] Several fixes for attempted NULL pointer frees, and some MSNSLP fixes. Still not ready for use yet. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sun, 14 Sep 2003 01:59:09 +0000
parents 2adf21e70601
children cdcb43229e33
files src/protocols/msn/msg.c src/protocols/msn/msn.c src/protocols/msn/msn.h src/protocols/msn/msnslp.c src/protocols/msn/notification.c src/protocols/msn/servconn.c src/protocols/msn/session.c src/protocols/msn/switchboard.c
diffstat 8 files changed, 173 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/msn/msg.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/msg.c	Sun Sep 14 01:59:09 2003 +0000
@@ -36,6 +36,24 @@
 	if (*(tmp) != '\0') *(tmp)++ = '\0'; \
 	if (*(tmp) == '\n') (tmp)++
 
+
+#define msn_put16(buf, data) ( \
+		(*(buf) = (u_char)((data)>>8)&0xff), \
+		(*((buf)+1) = (u_char)(data)&0xff),  \
+		2)
+#define msn_get16(buf) ((((*(buf))<<8)&0xff00) + ((*((buf)+1)) & 0xff))
+#define msn_put32(buf, data) ( \
+		(*((buf)) = (u_char)((data)>>24)&0xff), \
+		(*((buf)+1) = (u_char)((data)>>16)&0xff), \
+		(*((buf)+2) = (u_char)((data)>>8)&0xff), \
+		(*((buf)+3) = (u_char)(data)&0xff), \
+		4)
+#define msn_get32(buf) ((((*(buf))<<24)&0xff000000) + \
+		(((*((buf)+1))<<16)&0x00ff0000) + \
+		(((*((buf)+2))<< 8)&0x0000ff00) + \
+		(((*((buf)+3)    )&0x000000ff)))
+
+
 /*
  * "MIME-Version: 1.0\r\n" == 19
  * "Content-Type: "        == 14
@@ -199,15 +217,15 @@
 		memcpy(footer, tmp, 4);
 
 		/* Import the header. */
-		memcpy(&msg->msnslp_header.session_id,      tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.id,              tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.offset,          tmp, 4); tmp += 8;
-		memcpy(&msg->msnslp_header.total_size,      tmp, 4); tmp += 8;
-		memcpy(&msg->msnslp_header.length,          tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.flags,           tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.ack_session_id,  tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.ack_unique_id,   tmp, 4); tmp += 4;
-		memcpy(&msg->msnslp_header.ack_length,      tmp, 4); tmp += 8;
+		msg->msnslp_header.session_id     = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.id             = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.offset         = msn_get32(tmp); tmp += 8;
+		msg->msnslp_header.total_size     = msn_get32(tmp); tmp += 8;
+		msg->msnslp_header.length         = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.flags          = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.ack_session_id = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.ack_unique_id  = msn_get32(tmp); tmp += 4;
+		msg->msnslp_header.ack_length     = msn_get32(tmp); tmp += 8;
 
 		/* Convert to the right endianness */
 		msg->msnslp_header.session_id = ntohl(msg->msnslp_header.session_id);
@@ -301,6 +319,7 @@
 msn_message_to_string(const MsnMessage *msg, size_t *ret_size)
 {
 	GList *l;
+	const char *body;
 	char *msg_start;
 	char *str;
 	char buf[MSN_BUF_LEN];
@@ -317,6 +336,8 @@
 	 */
 	g_return_val_if_fail(msg != NULL, NULL);
 
+	body = msn_message_get_body(msg);
+
 	if (msn_message_is_incoming(msg)) {
 		MsnUser *sender = msn_message_get_sender(msg);
 
@@ -335,8 +356,10 @@
 	str = g_new0(char, len + 1);
 
 	g_strlcpy(str, buf, len);
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
 	msg_start = str + strlen(str);
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
 	/* Standard header. */
 	if (msg->charset == NULL) {
@@ -351,8 +374,10 @@
 				   "Content-Type: %s; charset=%s\r\n",
 				   msg->content_type, msg->charset);
 	}
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
 	g_strlcat(str, buf, len);
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
 	for (l = msg->attr_list; l != NULL; l = l->next) {
 		const char *key = (char *)l->data;
@@ -365,18 +390,18 @@
 		g_strlcat(str, buf, len);
 	}
 
+	g_strlcat(str, "\r\n", len);
+
 	if (msg->msnslp_message)
 	{
 		char *c;
-		char blank[4];
 		long session_id, id, offset, total_size, length, flags;
 		long ack_session_id, ack_unique_id, ack_length;
 
-		blank[0] = blank[1] = blank[2] = blank[3] = 0;
+		c = str + strlen(str);
 
-		g_strlcat(str, "\r\n", 3);
-
-		c = str + strlen(str);
+		gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
 		session_id      = htonl(msg->msnslp_header.session_id);
 		id              = htonl(msg->msnslp_header.id);
@@ -388,44 +413,59 @@
 		ack_unique_id   = htonl(msg->msnslp_header.ack_unique_id);
 		ack_length      = htonl(msg->msnslp_header.ack_length);
 
-		memcpy(c, &session_id,      4); c += 4;
-		memcpy(c, &id,              4); c += 4;
-		memcpy(c, &offset,          4); c += 4;
-		memcpy(c, blank,            4); c += 4;
-		memcpy(c, &total_size,      4); c += 4;
-		memcpy(c, blank,            4); c += 4;
-		memcpy(c, &length,          4); c += 4;
-		memcpy(c, &flags,           4); c += 4;
-		memcpy(c, &ack_session_id,  4); c += 4;
-		memcpy(c, &ack_unique_id,   4); c += 4;
-		memcpy(c, &ack_length,      4); c += 4;
-		memcpy(c, blank,            4); c += 4;
+		gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
-		strncpy(c, msn_message_get_body(msg), len);
-
-		c += strlen(msn_message_get_body(msg));
+		c += msn_put32(c, session_id);     gaim_debug_misc("msn", "1\n");
+		c += msn_put32(c, id);             gaim_debug_misc("msn", "2\n");
+		c += msn_put32(c, offset);         gaim_debug_misc("msn", "3\n");
+		c += msn_put32(c, 0);              gaim_debug_misc("msn", "4\n");
+		c += msn_put32(c, total_size);     gaim_debug_misc("msn", "5\n");
+		c += msn_put32(c, 0);              gaim_debug_misc("msn", "6\n");
+		c += msn_put32(c, length);         gaim_debug_misc("msn", "7\n");
+		c += msn_put32(c, flags);          gaim_debug_misc("msn", "8\n");
+		c += msn_put32(c, ack_session_id); gaim_debug_misc("msn", "9\n");
+		c += msn_put32(c, ack_unique_id);  gaim_debug_misc("msn", "10\n");
+		c += msn_put32(c, ack_length);     gaim_debug_misc("msn", "11\n");
+		c += msn_put32(c, 0);              gaim_debug_misc("msn", "12\n");
 
-		if (strlen(msn_message_get_body(msg)) > 0)
-			*c++ = '\0';
-
-		memcpy(c, &msg->msnslp_footer.app_id, 4); c += 4;
-		*c = '\0';
-
-		gaim_debug_misc("msn", "cur size = %d\n", (c - str));
+		gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
 		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
 
-		if (msg->size != (c - str))
+		if (body != NULL)
+		{
+			strncpy(c, body, len);
+
+			gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+			gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
+
+			c += strlen(body);
+
+			gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+			gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
+
+			if (strlen(body) > 0)
+				*c++ = '\0';
+
+			gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+			gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
+		}
+
+		c += msn_put32(c, msg->msnslp_footer.app_id);
+
+		gaim_debug_misc("msn", "cur size = %d\n", (c - msg_start));
+		gaim_debug_misc("msn", "msg->size = %d\n", msg->size);
+
+		if (msg->size != (c - msg_start))
 		{
 			gaim_debug(GAIM_DEBUG_ERROR, "msn",
-					   "Outgoing message size (%d) and string length (%d) "
-					   "do not match!\n", msg->size, (c - str));
+					   "Outgoing message size (%d) and data length (%d) "
+					   "do not match!\n", msg->size, (c - msg_start));
 		}
 	}
 	else
 	{
-		g_snprintf(buf, sizeof(buf), "\r\n%s", msn_message_get_body(msg));
-
-		g_strlcat(str, buf, len);
+		g_strlcat(str, body, len);
 
 		if (msg->size != strlen(msg_start)) {
 			gaim_debug(GAIM_DEBUG_ERROR, "msn",
@@ -536,7 +576,7 @@
 	int newline_count = 0;
 	size_t new_len;
 
-	g_return_if_fail(msg != NULL);
+	g_return_if_fail(msg  != NULL);
 	g_return_if_fail(body != NULL);
 
 	if (msg->body != NULL) {
--- a/src/protocols/msn/msn.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/msn.c	Sun Sep 14 01:59:09 2003 +0000
@@ -19,6 +19,9 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+#include <glib.h>
+static void (*real_g_free)(gpointer) = g_free;
+
 #include "msn.h"
 #include "accountopt.h"
 #include "msg.h"
@@ -36,6 +39,17 @@
 
 static char *msn_normalize(const char *str);
 
+void
+msn_free(void *ptr, const char *filename, int line)
+{
+	gaim_debug_misc("msn", "Freeing pointer %p at %s:%d\n",
+					ptr, filename, line);
+	if (ptr == NULL)
+		abort();
+
+	real_g_free(ptr);
+}
+
 typedef struct
 {
 	GaimConnection *gc;
--- a/src/protocols/msn/msn.h	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/msn.h	Sun Sep 14 01:59:09 2003 +0000
@@ -50,6 +50,10 @@
 
 #define MSN_BUF_LEN 8192
 
+void msn_free(void *ptr, const char *filename, int line);
+
+#define g_free(x) msn_free((x), __FILE__, __LINE__)
+
 #define USEROPT_MSNSERVER 3
 #define MSN_SERVER "messenger.hotmail.com"
 #define USEROPT_MSNPORT 4
--- a/src/protocols/msn/msnslp.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/msnslp.c	Sun Sep 14 01:59:09 2003 +0000
@@ -119,6 +119,9 @@
 	msn_message_set_attr(msg, "P2P-Dest",
 			msn_user_get_passport(msn_message_get_receiver(msg)));
 
+	if (msg->msnslp_header.session_id == 0)
+		msg->msnslp_footer.app_id = 1;
+
 	msn_switchboard_send_msg(slpsession->swboard, msg);
 }
 
@@ -131,6 +134,8 @@
 	MsnMessage *invite_msg;
 	char *msnobj_data;
 	char *msnobj_base64;
+	char *branch;
+	char *call_id;
 	char *content;
 	char *body;
 	char *c;
@@ -147,8 +152,27 @@
 	if ((c = strchr(msnobj_base64, '=')) != NULL)
 		*c = '\0';
 
-	if (slpsession->session_id == 0)
-		slpsession->session_id = rand() % 0xFFFFFF00;
+	slpsession->session_id = rand() % 0xFFFFFF00 + 4;
+
+	branch = g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111,
+							 rand() % 0xAAFF + 0x1111);
+
+	call_id = g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111,
+							  rand() % 0xAAFF + 0x1111);
 
 	content = g_strdup_printf(
 		"EUF-GUID: {A4268EEC-FEC5-49E5-95C3-F126696BDBF6}\r\n"
@@ -164,9 +188,9 @@
 		"INVITE MSNMSGR:%s MSNSLP/1.0\r\n"
 		"To: <msnmsgr:%s>\r\n"
 		"From: <msnmsgr:%s>\r\n"
-		"Via: MSNSLP/1.0/TLP ;branch={33517CE4-02FC-4428-B6F4-39927229B722}\r\n"
+		"Via: MSNSLP/1.0/TLP ;branch={%s}\r\n"
 		"CSeq: 0\r\n"
-		"Call-ID: {9D79AE57-1BD5-444B-B14E-3FC9BB2B5D58}\r\n"
+		"Call-ID: {%s}\r\n"
 		"Max-Forwards: 0\r\n"
 		"Content-Type: application/x-msnmsgr-sessionreqbody\r\n"
 		"Content-Length: %d\r\n"
@@ -176,14 +200,19 @@
 		msn_user_get_passport(remote_user),
 		msn_user_get_passport(remote_user),
 		msn_user_get_passport(local_user),
+		branch,
+		call_id,
 		strlen(content) + 5,
 		content);
 
 	g_free(content);
+	g_free(branch);
+	g_free(call_id);
 
 	gaim_debug_misc("msn", "Message = {%s}\n", body);
 
-	invite_msg = msn_message_new_msnslp();
+	//invite_msg = msn_message_new_msnslp();
+	invite_msg = msn_message_new();
 
 	msn_message_set_sender(invite_msg, local_user);
 	msn_message_set_receiver(invite_msg, remote_user);
--- a/src/protocols/msn/notification.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/notification.c	Sun Sep 14 01:59:09 2003 +0000
@@ -134,9 +134,12 @@
 		char *new_buffer = g_new(char, size + s + 1);
 
 		if (buffer != NULL)
+		{
 			strncpy(new_buffer, buffer, size);
 
-		g_free(buffer);
+			g_free(buffer);
+		}
+
 		buffer = new_buffer;
 
 		strncpy(buffer + size, temp_buf, s);
@@ -516,8 +519,12 @@
 	}
 
 	g_free(request_str);
-	g_free(session->ssl_url);
-	session->ssl_url = NULL;
+
+	if (session->ssl_url != NULL)
+	{
+		g_free(session->ssl_url);
+		session->ssl_url = NULL;
+	}
 
 	/* Get the PassportURLs line. */
 	if ((s = msn_ssl_read(gsc, &buffer)) <= 0)
--- a/src/protocols/msn/servconn.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/servconn.c	Sun Sep 14 01:59:09 2003 +0000
@@ -243,7 +243,8 @@
 
 	close(servconn->fd);
 
-	g_free(servconn->rxqueue);
+	if (servconn->rxqueue != NULL)
+		g_free(servconn->rxqueue);
 
 	while (servconn->txqueue != NULL) {
 		g_free(servconn->txqueue->data);
@@ -336,8 +337,10 @@
 {
 	g_return_val_if_fail(servconn != NULL, 0);
 
+#if 0
 	gaim_debug(GAIM_DEBUG_MISC, "msn", "C: %s%s", buf,
 			   (*(buf + size - 1) == '\n' ? "" : "\n"));
+#endif
 
 	return write(servconn->fd, buf, size);
 }
--- a/src/protocols/msn/session.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/session.c	Sun Sep 14 01:59:09 2003 +0000
@@ -71,16 +71,26 @@
 	while (session->lists.forward)
 		msn_user_destroy(session->lists.forward->data);
 
-	g_slist_free(session->lists.allow);
-	g_slist_free(session->lists.block);
+	if (session->lists.allow != NULL)
+		g_slist_free(session->lists.allow);
+
+	if (session->lists.block != NULL)
+		g_slist_free(session->lists.block);
 
 	msn_groups_destroy(session->groups);
 	msn_users_destroy(session->users);
 
-	g_free(session->passport_info.kv);
-	g_free(session->passport_info.sid);
-	g_free(session->passport_info.mspauth);
-	g_free(session->passport_info.file);
+	if (session->passport_info.kv != NULL)
+		g_free(session->passport_info.kv);
+
+	if (session->passport_info.sid != NULL)
+		g_free(session->passport_info.sid);
+
+	if (session->passport_info.mspauth != NULL)
+		g_free(session->passport_info.mspauth);
+
+	if (session->passport_info.file != NULL)
+		g_free(session->passport_info.file);
 
 	g_free(session);
 }
--- a/src/protocols/msn/switchboard.c	Sat Sep 13 23:28:08 2003 +0000
+++ b/src/protocols/msn/switchboard.c	Sun Sep 14 01:59:09 2003 +0000
@@ -45,7 +45,8 @@
 	msn_message_set_attr(msg, "User-Agent", NULL);
 	msn_message_set_body(msg, MSN_CLIENTINFO);
 
-	if (!msn_switchboard_send_msg(swboard, msg)) {
+	if (!msn_switchboard_send_msg(swboard, msg))
+	{
 		gaim_debug_warning("msn",
 						   "Unable to send clientcaps. "
 						   "Disconnecting from switchboard.\n");
@@ -93,8 +94,7 @@
 
 	send_clientcaps(swboard);
 
-#if 0
-	if (session->protocol_ver >= 9)
+	if (0 && session->protocol_ver >= 9)
 	{
 		MsnUser *local_user, *remote_user;
 
@@ -111,7 +111,6 @@
 											 local_user, remote_user,
 											 msn_user_get_object(remote_user));
 	}
-#endif
 
 	return TRUE;
 }
@@ -639,6 +638,7 @@
 	char *buf;
 	size_t len;
 	int ret;
+	FILE *fp;
 
 	g_return_val_if_fail(swboard != NULL, FALSE);
 	g_return_val_if_fail(msg     != NULL, FALSE);
@@ -659,6 +659,10 @@
 
 	ret = msn_servconn_write(swboard->servconn, buf, len);
 
+	fp = fopen("/tmp/msn-msg", "wb");
+	fwrite(buf, 1, len, fp);
+	fclose(fp);
+
 	g_free(buf);
 
 	return (ret > 0);