comparison libpurple/protocols/msn/directconn.c @ 30440:264c76d29ab0

I don't like accessing memory by magic-constant.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Thu, 18 Mar 2010 04:30:27 +0000
parents bc720282a62d
children 24579d001831
comparison
equal deleted inserted replaced
30439:bc720282a62d 30440:264c76d29ab0
25 #include "directconn.h" 25 #include "directconn.h"
26 26
27 #include "slp.h" 27 #include "slp.h"
28 #include "slpmsg.h" 28 #include "slpmsg.h"
29 29
30 #define DC_SESSION_ID_OFFS 0 30 #pragma pack(push,1)
31 #define DC_SEQ_ID_OFFS 4 31 typedef struct {
32 #define DC_DATA_OFFSET_OFFS 8 32 guint32 session_id;
33 #define DC_TOTAL_DATA_SIZE_OFFS 16 33 guint32 seq_id;
34 #define DC_MESSAGE_LENGTH_OFFS 24 34 guint64 offset;
35 #define DC_FLAGS_OFFS 28 35 guint64 total_size;
36 #define DC_ACK_ID_OFFS 32 36 guint32 length;
37 #define DC_ACK_UID_OFFS 36 37 guint32 flags;
38 #define DC_ACK_DATA_SIZE_OFFS 40 38 guint32 ack_id;
39 #define DC_MESSAGE_BODY_OFFS 48 39 guint32 ack_uid;
40 40 guint64 ack_size;
41 #define DC_PACKET_HEADER_SIZE 48 41 /* guint8 body[1]; */
42 #define DC_MAX_BODY_SIZE 1352 42 } MsnDcContext;
43 #define DC_MAX_PACKET_SIZE (DC_PACKET_HEADER_SIZE + DC_MAX_BODY_SIZE) 43 #pragma pack(pop)
44
45 #define DC_PACKET_HEADER_SIZE sizeof(MsnDcContext)
46 #define DC_MAX_BODY_SIZE 1352
47 #define DC_MAX_PACKET_SIZE (DC_PACKET_HEADER_SIZE + DC_MAX_BODY_SIZE)
44 48
45 static void 49 static void
46 msn_dc_generate_nonce(MsnDirectConn *dc) 50 msn_dc_generate_nonce(MsnDirectConn *dc)
47 { 51 {
48 PurpleCipher *cipher = NULL; 52 PurpleCipher *cipher = NULL;
368 372
369 static void 373 static void
370 msn_dc_parse_binary_header(MsnDirectConn *dc) 374 msn_dc_parse_binary_header(MsnDirectConn *dc)
371 { 375 {
372 MsnSlpHeader *h; 376 MsnSlpHeader *h;
373 gchar *buffer; 377 MsnDcContext *context;
374 378
375 g_return_if_fail(dc != NULL); 379 g_return_if_fail(dc != NULL);
376 380
377 h = &dc->header; 381 h = &dc->header;
378 /* Skip packet size */ 382 /* Skip packet size */
379 buffer = dc->in_buffer + 4; 383 context = (MsnDcContext *)(dc->in_buffer + 4);
380 384
381 memcpy(&h->session_id, buffer + DC_SESSION_ID_OFFS, sizeof(h->session_id)); 385 h->session_id = GUINT32_FROM_LE(context->session_id);
382 h->session_id = GUINT32_FROM_LE(h->session_id); 386 h->id = GUINT32_FROM_LE(context->seq_id);
383 387 h->offset = GUINT64_FROM_LE(context->offset);
384 memcpy(&h->id, buffer + DC_SEQ_ID_OFFS, sizeof(h->id)); 388 h->total_size = GUINT64_FROM_LE(context->total_size);
385 h->id = GUINT32_FROM_LE(h->id); 389 h->length = GUINT32_FROM_LE(context->length);
386 390 h->flags = GUINT32_FROM_LE(context->flags);
387 memcpy(&h->offset, buffer + DC_DATA_OFFSET_OFFS, sizeof(h->offset)); 391 h->ack_id = GUINT32_FROM_LE(context->ack_id);
388 h->offset = GUINT64_FROM_LE(h->offset); 392 h->ack_sub_id = GUINT32_FROM_LE(context->ack_uid);
389 393 h->ack_size = GUINT64_FROM_LE(context->ack_size);
390 memcpy(&h->total_size, buffer + DC_TOTAL_DATA_SIZE_OFFS, sizeof(h->total_size));
391 h->total_size = GUINT64_FROM_LE(h->total_size);
392
393 memcpy(&h->length, buffer + DC_MESSAGE_LENGTH_OFFS, sizeof(h->length));
394 h->length = GUINT32_FROM_LE(h->length);
395
396 memcpy(&h->flags, buffer + DC_FLAGS_OFFS, sizeof(h->flags));
397 h->flags = GUINT32_FROM_LE(h->flags);
398
399 memcpy(&h->ack_id, buffer + DC_ACK_ID_OFFS, sizeof(h->ack_id));
400 h->ack_id = GUINT32_FROM_LE(h->ack_id);
401
402 memcpy(&h->ack_sub_id, buffer + DC_ACK_UID_OFFS, sizeof(h->ack_sub_id));
403 h->ack_sub_id = GUINT32_FROM_LE(h->ack_sub_id);
404
405 memcpy(&h->ack_size, buffer + DC_ACK_DATA_SIZE_OFFS, sizeof(h->ack_size));
406 h->ack_size = GUINT64_FROM_LE(h->ack_size);
407 } 394 }
408 395
409 static const gchar * 396 static const gchar *
410 msn_dc_serialize_binary_header(MsnDirectConn *dc) { 397 msn_dc_serialize_binary_header(MsnDirectConn *dc) {
411 MsnSlpHeader h; 398 MsnSlpHeader *h;
412 static gchar bin_header[DC_PACKET_HEADER_SIZE]; 399 static MsnDcContext bin_header;
413 400
414 g_return_val_if_fail(dc != NULL, NULL); 401 g_return_val_if_fail(dc != NULL, NULL);
415 402
416 memcpy(&h, &dc->header, sizeof(h)); 403 h = &dc->header;
417 404
418 h.session_id = GUINT32_TO_LE(h.session_id); 405 bin_header.session_id = GUINT32_TO_LE(h->session_id);
419 memcpy(bin_header + DC_SESSION_ID_OFFS, &h.session_id, sizeof(h.session_id)); 406 bin_header.seq_id = GUINT32_TO_LE(h->id);
420 407 bin_header.offset = GUINT64_TO_LE(h->offset);
421 h.id = GUINT32_TO_LE(h.id); 408 bin_header.total_size = GUINT64_TO_LE(h->total_size);
422 memcpy(bin_header + DC_SEQ_ID_OFFS, &h.id, sizeof(h.id)); 409 bin_header.length = GUINT32_TO_LE(h->length);
423 410 bin_header.flags = GUINT32_TO_LE(h->flags);
424 h.offset = GUINT64_TO_LE(h.offset); 411 bin_header.ack_id = GUINT32_TO_LE(h->ack_id);
425 memcpy(bin_header + DC_DATA_OFFSET_OFFS, &h.offset, sizeof(h.offset)); 412 bin_header.ack_uid = GUINT32_TO_LE(h->ack_sub_id);
426 413 bin_header.ack_size = GUINT64_TO_LE(h->ack_size);
427 h.total_size = GUINT64_TO_LE(h.total_size); 414
428 memcpy(bin_header + DC_TOTAL_DATA_SIZE_OFFS, &h.total_size, sizeof(h.total_size)); 415 return (const gchar *)&bin_header;
429
430 h.length = GUINT32_TO_LE(h.length);
431 memcpy(bin_header + DC_MESSAGE_LENGTH_OFFS, &h.length, sizeof(h.length));
432
433 h.flags = GUINT32_TO_LE(h.flags);
434 memcpy(bin_header + DC_FLAGS_OFFS, &h.flags, sizeof(h.flags));
435
436 h.ack_id = GUINT32_TO_LE(h.ack_id);
437 memcpy(bin_header + DC_ACK_ID_OFFS, &h.ack_id, sizeof(h.ack_id));
438
439 h.ack_sub_id = GUINT32_TO_LE(h.ack_sub_id);
440 memcpy(bin_header + DC_ACK_UID_OFFS, &h.ack_sub_id, sizeof(h.ack_sub_id));
441
442 h.ack_size = GUINT64_TO_LE(h.ack_size);
443 memcpy(bin_header + DC_ACK_DATA_SIZE_OFFS, &h.ack_size, sizeof(h.ack_size));
444
445 return bin_header;
446 } 416 }
447 417
448 /* 418 /*
449 static void 419 static void
450 msn_dc_send_bye(MsnDirectConn *dc) 420 msn_dc_send_bye(MsnDirectConn *dc)
691 dc->header.length = 0; 661 dc->header.length = 0;
692 dc->header.flags = 0x100; 662 dc->header.flags = 0x100;
693 663
694 h = msn_dc_serialize_binary_header(dc); 664 h = msn_dc_serialize_binary_header(dc);
695 memcpy(p->data + 4, h, DC_PACKET_HEADER_SIZE); 665 memcpy(p->data + 4, h, DC_PACKET_HEADER_SIZE);
696 memcpy(p->data + 4 + DC_ACK_ID_OFFS, dc->nonce, 16); 666 memcpy(p->data + 4 + offsetof(MsnDcContext, ack_id), dc->nonce, 16);
697 667
698 msn_dc_enqueue_packet(dc, p); 668 msn_dc_enqueue_packet(dc, p);
699 } 669 }
700 670
701 static void 671 static void
719 dc->header.id = dc->slpcall->slplink->slp_seq_id++; 689 dc->header.id = dc->slpcall->slplink->slp_seq_id++;
720 dc->header.length = 0; 690 dc->header.length = 0;
721 691
722 h = msn_dc_serialize_binary_header(dc); 692 h = msn_dc_serialize_binary_header(dc);
723 memcpy(p->data + 4, h, DC_PACKET_HEADER_SIZE); 693 memcpy(p->data + 4, h, DC_PACKET_HEADER_SIZE);
724 memcpy(p->data + 4 + DC_ACK_ID_OFFS, dc->nonce, 16); 694 memcpy(p->data + 4 + offsetof(MsnDcContext, ack_id), dc->nonce, 16);
725 695
726 msn_dc_enqueue_packet(dc, p); 696 msn_dc_enqueue_packet(dc, p);
727 } 697 }
728 698
729 static void 699 static void