changeset 31044:7b771e6f1142

Change SLP P2P functions to use the new buffer access functions instead of mapping to a struct. I don't mind this since it's limited to just one pair of functions. This means that we don't need to use any packing there. Refs #12856.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 17 Dec 2010 07:49:47 +0000
parents 5981b4116b52
children f91501dc406e
files libpurple/protocols/msn/directconn.c libpurple/protocols/msn/p2p.c libpurple/protocols/msn/p2p.h libpurple/protocols/msn/slpmsg.c libpurple/protocols/msn/slpmsg_part.c
diffstat 5 files changed, 64 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/directconn.c	Fri Dec 17 07:12:21 2010 +0000
+++ b/libpurple/protocols/msn/directconn.c	Fri Dec 17 07:49:47 2010 +0000
@@ -447,11 +447,11 @@
 {
 	const gchar *h;
 
-	h = (gchar*)  msn_p2p_header_to_wire(&dc->header);
+	h = msn_p2p_header_to_wire(&dc->header);
 
 	memcpy(p->data, h, P2P_PACKET_HEADER_SIZE);
 
-	memcpy(p->data + offsetof(MsnP2PHeader, ack_id), dc->nonce, 16);
+	memcpy(p->data + P2P_HEADER_ACK_ID_OFFSET, dc->nonce, 16);
 
 	msn_dc_enqueue_packet(dc, p);
 }
@@ -495,7 +495,7 @@
 	if (packet_length != P2P_PACKET_HEADER_SIZE)
 		return FALSE;
 
-	memcpy(nonce, dc->in_buffer + 4 + offsetof(MsnP2PHeader, ack_id), 16);
+	memcpy(nonce, dc->in_buffer + 4 + P2P_HEADER_ACK_ID_OFFSET, 16);
 
 	if (dc->nonce_type == DC_NONCE_PLAIN) {
 		if (memcmp(dc->nonce, nonce, 16) == 0) {
@@ -676,14 +676,11 @@
 
 		if (dc->state != DC_STATE_FOO) {
 			MsnP2PHeader *context;
-			MsnP2PHeader *h;
 			
 			/* Skip packet size */
-			context = (MsnP2PHeader *)(dc->in_buffer + 4);
-
-			h = msn_p2p_header_from_wire(context);
-			memcpy(&dc->header, h, P2P_PACKET_HEADER_SIZE);
-			g_free(h);
+			context = msn_p2p_header_from_wire(dc->in_buffer + 4);
+			memcpy(&dc->header, context, P2P_PACKET_HEADER_SIZE);
+			g_free(context);
 		}
 
 		switch (msn_dc_process_packet(dc, packet_length)) {
--- a/libpurple/protocols/msn/p2p.c	Fri Dec 17 07:12:21 2010 +0000
+++ b/libpurple/protocols/msn/p2p.c	Fri Dec 17 07:49:47 2010 +0000
@@ -25,68 +25,71 @@
 #include "internal.h"
 
 #include "p2p.h"
+#include "msnutils.h"
 
 MsnP2PHeader *
-msn_p2p_header_from_wire(MsnP2PHeader *wire)
+msn_p2p_header_from_wire(const char *wire)
 {
 	MsnP2PHeader *header;
 
 	header = g_new(MsnP2PHeader, 1);
 
-	header->session_id = GUINT32_FROM_LE(wire->session_id);
-	header->id         = GUINT32_FROM_LE(wire->id);
-	header->offset     = GUINT64_FROM_LE(wire->offset);
-	header->total_size = GUINT64_FROM_LE(wire->total_size);
-	header->length     = GUINT32_FROM_LE(wire->length);
-	header->flags      = GUINT32_FROM_LE(wire->flags);
-	header->ack_id     = GUINT32_FROM_LE(wire->ack_id);
-	header->ack_sub_id = GUINT32_FROM_LE(wire->ack_sub_id);
-	header->ack_size   = GUINT64_FROM_LE(wire->ack_size);
+	header->session_id = msn_pop32le(wire);
+	header->id         = msn_pop32le(wire);
+	header->offset     = msn_pop64le(wire);
+	header->total_size = msn_pop64le(wire);
+	header->length     = msn_pop32le(wire);
+	header->flags      = msn_pop32le(wire);
+	header->ack_id     = msn_pop32le(wire);
+	header->ack_sub_id = msn_pop32le(wire);
+	header->ack_size   = msn_pop64le(wire);
 
 	return header;
 }
 
-MsnP2PHeader *
+char *
 msn_p2p_header_to_wire(MsnP2PHeader *header)
 {
-	MsnP2PHeader *wire;
+	char *wire;
+	char *tmp;
 	
-	wire = g_new(MsnP2PHeader, 1);
+	tmp = wire = g_new(char, P2P_PACKET_HEADER_SIZE);
 
-	wire->session_id = GUINT32_TO_LE(header->session_id);
-	wire->id         = GUINT32_TO_LE(header->id);
-	wire->offset     = GUINT64_TO_LE(header->offset);
-	wire->total_size = GUINT64_TO_LE(header->total_size);
-	wire->length     = GUINT32_TO_LE(header->length);
-	wire->flags      = GUINT32_TO_LE(header->flags);
-	wire->ack_id     = GUINT32_TO_LE(header->ack_id);
-	wire->ack_sub_id = GUINT32_TO_LE(header->ack_sub_id);
-	wire->ack_size   = GUINT64_TO_LE(header->ack_size);
+	msn_push32le(tmp, header->session_id);
+	msn_push32le(tmp, header->id);
+	msn_push64le(tmp, header->offset);
+	msn_push64le(tmp, header->total_size);
+	msn_push32le(tmp, header->length);
+	msn_push32le(tmp, header->flags);
+	msn_push32le(tmp, header->ack_id);
+	msn_push32le(tmp, header->ack_sub_id);
+	msn_push64le(tmp, header->ack_size);
 
 	return wire;
 
 }
 
 MsnP2PFooter *
-msn_p2p_footer_from_wire(MsnP2PFooter *wire)
+msn_p2p_footer_from_wire(const char *wire)
 {
 	MsnP2PFooter *footer;
 
 	footer = g_new(MsnP2PFooter, 1);
 
-	footer->value = GUINT32_FROM_BE(wire->value);
+	footer->value = msn_pop32be(wire);
 
 	return footer;
 }
 
-MsnP2PFooter *
+char *
 msn_p2p_footer_to_wire(MsnP2PFooter *footer)
 {
-	MsnP2PFooter *wire;
+	char *wire;
+	char *tmp;
 
-	wire = g_new(MsnP2PFooter, 1);
+	tmp = wire = g_new(char, P2P_PACKET_FOOTER_SIZE);
 
-	wire->value = GUINT32_TO_BE(footer->value);
+	msn_push32be(tmp, footer->value);
 
 	return wire;
 }
@@ -98,3 +101,4 @@
 	        flags == (P2P_WLM2009_COMP | P2P_MSN_OBJ_DATA) ||
 	        flags == P2P_FILE_DATA);
 }
+
--- a/libpurple/protocols/msn/p2p.h	Fri Dec 17 07:12:21 2010 +0000
+++ b/libpurple/protocols/msn/p2p.h	Fri Dec 17 07:49:47 2010 +0000
@@ -25,8 +25,6 @@
 #ifndef MSN_P2P_H
 #define MSN_P2P_H
 
-
-#pragma pack(push,1)
 typedef struct {
 	guint32 session_id;
 	guint32 id;
@@ -45,21 +43,23 @@
 	guint64 ack_size;
 /*	guint8  body[1]; */
 } MsnP2PHeader;
-#pragma pack(pop)
+#define P2P_PACKET_HEADER_SIZE (6 * 4 + 3 * 8)
 
-#pragma pack(push,1)
+/* Used for DCs to store nonces */
+#define P2P_HEADER_ACK_ID_OFFSET (2*4 + 2*8 + 2*4)
+
 typedef struct {
 	guint8  header_len;
 	guint8  opcode;
 	guint16 message_len;
 	guint32 base_id;
 } MsnP2Pv2Header;
-#pragma pack(pop)
 
 typedef struct
 {
 	guint32 value;
 } MsnP2PFooter;
+#define P2P_PACKET_FOOTER_SIZE (1 * 4)
 
 typedef enum
 {
@@ -91,19 +91,16 @@
 	P2P_APPID_DISPLAY   = 0xC         /**< Display Image */
 } MsnP2PAppId;
 
-#define P2P_PACKET_HEADER_SIZE sizeof(MsnP2PHeader)
-#define P2P_PACKET_FOOTER_SIZE sizeof(MsnP2PFooter)
+MsnP2PHeader *
+msn_p2p_header_from_wire(const char *wire);
 
-MsnP2PHeader *
-msn_p2p_header_from_wire(MsnP2PHeader *wire);
-
-MsnP2PHeader *
+char *
 msn_p2p_header_to_wire(MsnP2PHeader *header);
 
 MsnP2PFooter *
-msn_p2p_footer_from_wire(MsnP2PFooter *wire);
+msn_p2p_footer_from_wire(const char *wire);
 
-MsnP2PFooter *
+char *
 msn_p2p_footer_to_wire(MsnP2PFooter *footer);
 
 gboolean
--- a/libpurple/protocols/msn/slpmsg.c	Fri Dec 17 07:12:21 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg.c	Fri Dec 17 07:49:47 2010 +0000
@@ -263,21 +263,21 @@
 
 char *msn_slpmsg_serialize(MsnSlpMessage *slpmsg, size_t *ret_size)
 {
-	MsnP2PHeader *header;
-	MsnP2PFooter *footer;
+	char *header;
+	char *footer;
 	char *base;
 	char *tmp;
 	size_t siz;
 
-	base = g_malloc(P2P_PACKET_HEADER_SIZE + slpmsg->size + sizeof(MsnP2PFooter));
+	base = g_malloc(P2P_PACKET_HEADER_SIZE + slpmsg->size + P2P_PACKET_FOOTER_SIZE);
 	tmp = base;
 
 	header = msn_p2p_header_to_wire(slpmsg->header);
 	footer = msn_p2p_footer_to_wire(slpmsg->footer);
 
-	siz = sizeof(MsnP2PHeader);
+	siz = P2P_PACKET_HEADER_SIZE;
 	/* Copy header */
-	memcpy(tmp, (char*)header, siz);
+	memcpy(tmp, header, siz);
 	tmp += siz;
 
 	/* Copy body */
@@ -285,8 +285,8 @@
 	tmp += slpmsg->size;
 
 	/* Copy footer */
-	siz = sizeof(MsnP2PFooter);
-	memcpy(tmp, (char*)footer, siz);
+	siz = P2P_PACKET_FOOTER_SIZE;
+	memcpy(tmp, footer, siz);
 	tmp += siz;
 
 	*ret_size = tmp - base;
--- a/libpurple/protocols/msn/slpmsg_part.c	Fri Dec 17 07:12:21 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg_part.c	Fri Dec 17 07:49:47 2010 +0000
@@ -60,7 +60,7 @@
 	tmp = data;
 
 	/* Extract the binary SLP header */
-	part->header = msn_p2p_header_from_wire((MsnP2PHeader*)tmp);
+	part->header = msn_p2p_header_from_wire(tmp);
 	tmp += P2P_PACKET_HEADER_SIZE;
 
 	/* Extract the body */
@@ -76,7 +76,7 @@
 
 	/* Extract the footer */
 	if (body_len >= 0) 
-		part->footer = msn_p2p_footer_from_wire((MsnP2PFooter*)tmp);
+		part->footer = msn_p2p_footer_from_wire(tmp);
 
 	return part;
 }
@@ -136,21 +136,21 @@
 
 char *msn_slpmsgpart_serialize(MsnSlpMessagePart *part, size_t *ret_size)
 {
-	MsnP2PHeader *header;
-	MsnP2PFooter *footer;
+	char *header;
+	char *footer;
 	char *base;
 	char *tmp;
 	size_t siz;
 
-	base = g_malloc(P2P_PACKET_HEADER_SIZE + part->size + sizeof(MsnP2PFooter));
+	base = g_malloc(P2P_PACKET_HEADER_SIZE + part->size + P2P_PACKET_FOOTER_SIZE);
 	tmp = base;
 
 	header = msn_p2p_header_to_wire(part->header);
 	footer = msn_p2p_footer_to_wire(part->footer);
 
-	siz = sizeof(MsnP2PHeader);
+	siz = P2P_PACKET_HEADER_SIZE;
 	/* Copy header */
-	memcpy(tmp, (char*)header, siz);
+	memcpy(tmp, header, siz);
 	tmp += siz;
 
 	/* Copy body */
@@ -158,8 +158,8 @@
 	tmp += part->size;
 
 	/* Copy footer */
-	siz = sizeof(MsnP2PFooter);
-	memcpy(tmp, (char*)footer, siz);
+	siz = P2P_PACKET_FOOTER_SIZE;
+	memcpy(tmp, footer, siz);
 	tmp += siz;
 
 	*ret_size = tmp - base;