view libpurple/protocols/qq/packet_parse.c @ 23638:1c50f12b1c52

2008.08.02 - csyfek <csyfek(at)gmail.com> * Commit to Pidgin * Tickets: Fixes #1861 Fixes #1902 References #5112 2008.08.02 - ccpaging <ecc_hy(at)hotmail.com> * Store all keys and md5 values of qq_data in char[QQ_KEY_LENGTH] * Use random value in inikey * TEA header padding in crypt.c * Rewrite login part of qq_process 2008.07.31 - ccpaging <ecc_hy(at)hotmail.com> * Fixed: send reply when get duplicate server command. The server may not get our reply before. * Tag custom picture as text "(Broken)" 2008.07.30 - ccpaging <ecc_hy(at)hotmail.com>, csyfek <csyfek(at)gmail.com> * Change some debug message * Modify buddy status flag according to eva for QQ2006 * Modify buddy status parse and correspond to eva2 * Add getIP/putIP functions to packet_parse.c, and replace some gen_ip_str * Replace guint32 *ip with struct in_addr, and reduce g_new/g_free operation * Source file changed: Merge buddy_status into buddy_list Change login_logout to qq_base Merge keep_alive into qq_base New qq_process extract from qq_network * Fixed: Byte alignment bug in crypt.c, tested in ARM PDA * Fixed: group chat message may get in before getting group info, and so group info is empty * Add qq_send_cmd_group_get_group_info when joined a group chat in group_im.c * Add some new group command identify according eva but further program * Add some new QQ client version identify * Fixed: Identify buddy's client version by IM packet, and not by status * Add some new info in buddy's tooltip text * Add video falg to buddy's emblem. But those flag in buddy status may not prasing correctly * Use new timeout function to handle send keep_alive, resend packet, update buddy status * Add new advanced options: The end user may change interval of keep_alive, resend packet, update buddy status to feed their need. For example, saving network flow when use mobile phone. Keep alive packet must be sent in 60-120 seconds whatever client rcved data of not. The intervals of keep alive and update status should be multiple of resend's interval, Since we use counter not time() in a single timeout function for efficiency. * Rewrite qq_trans.c, and use one g_list to manage: Store server packet before login, and prase all of them when get login Store client send packet for resend scanning, confirm server reply, filter duplicate server reply Store server packet for filter out duplicate * Add QQ_MSG_SYS_NOTICE = 0x06 in sys_msg.c * Rewrite qq_proc_cmd_reply and qq_proc_cmd_server: In QQ protocol, one packet reply may need a new packet send later. We may call it packet trigger. The triggers always is hided in every qq_process_reply. Now we try to extract those triggers and put into a single function, and then every trigger should be obviously and easy to manage.
author SHiNE CsyFeK <csyfek@gmail.com>
date Sat, 02 Aug 2008 15:00:46 +0000
parents 55f986ccbb6a
children 967344bc404d
line wrap: on
line source

/**
 * @file packet_parse.c
 *
 * purple
 *
 * Purple 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., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include <string.h>

#include "packet_parse.h"
#include "debug.h"

/*------------------------------------------------PUT------------------------------------------------*/

/* note:
 * 1, in these functions, 'b' stands for byte, 'w' stands for word, 'dw' stands for double word.
 * 2, we use '*cursor' and 'buf' as two addresses to calculate the length.
 * 3, change '0' to '1', if want to get more info about the packet parsing. */

#if 0
#define PARSER_DEBUG
#endif

/* read one byte from buf, 
 * return the number of bytes read if succeeds, otherwise return -1 */
gint qq_get8(guint8 *b, guint8 *buf)
{
	guint8 b_dest;
	memcpy(&b_dest, buf, sizeof(b_dest));
	*b = b_dest;
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get8] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get8] b_dest 0x%2x, *b 0x%02x\n", b_dest, *b);
#endif
	return sizeof(b_dest);
}


/* read two bytes as "guint16" from buf, 
 * return the number of bytes read if succeeds, otherwise return -1 */
gint qq_get16(guint16 *w, guint8 *buf)
{
	guint16 w_dest;
	memcpy(&w_dest, buf, sizeof(w_dest));
	*w = g_ntohs(w_dest);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get16] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get16] w_dest 0x%04x, *w 0x%04x\n", w_dest, *w);
#endif
	return sizeof(w_dest);
}

/* read four bytes as "guint32" from buf, 
 * return the number of bytes read if succeeds, otherwise return -1 */
gint qq_get32(guint32 *dw, guint8 *buf)
{
	guint32 dw_dest;
	memcpy(&dw_dest, buf, sizeof(dw_dest));
	*dw = g_ntohl(dw_dest);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get32] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][get32] dw_dest 0x%08x, *dw 0x%08x\n", dw_dest, *dw);
#endif
	return sizeof(dw_dest);
}

gint qq_getIP(struct in_addr *ip, guint8 *buf)
{
	memcpy(ip, buf, sizeof(struct in_addr));
	return sizeof(struct in_addr);
}

/* read datalen bytes from buf, 
 * return the number of bytes read if succeeds, otherwise return -1 */
gint qq_getdata(guint8 *data, gint datalen, guint8 *buf)
{
    memcpy(data, buf, datalen);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][getdata] buf %p\n", (void *)buf);
#endif
    return datalen;
}


/* read four bytes as "time_t" from buf,
 * return the number of bytes read if succeeds, otherwise return -1
 * This function is a wrapper around read_packet_dw() to avoid casting. */
gint qq_getime(time_t *t, guint8 *buf)
{
	guint32 dw_dest;
	memcpy(&dw_dest, buf, sizeof(dw_dest));
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][getime] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][getime] dw_dest before 0x%08x\n", dw_dest);
#endif
	dw_dest = g_ntohl(dw_dest);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][getime] dw_dest after 0x%08x\n", dw_dest);
#endif
	memcpy(t, &dw_dest, sizeof(dw_dest));
	return sizeof(dw_dest);
}

/*------------------------------------------------PUT------------------------------------------------*/
/* pack one byte into buf
 * return the number of bytes packed, otherwise return -1 */
gint qq_put8(guint8 *buf, guint8 b)
{
    memcpy(buf, &b, sizeof(b));
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put8] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put8] b 0x%02x\n", b);
#endif
    return sizeof(b);
}


/* pack two bytes as "guint16" into buf
 * return the number of bytes packed, otherwise return -1 */
gint qq_put16(guint8 *buf, guint16 w)
{
    guint16 w_porter;
    w_porter = g_htons(w);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put16] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put16] w 0x%04x, w_porter 0x%04x\n", w, w_porter);
#endif
    memcpy(buf, &w_porter, sizeof(w_porter));
    return sizeof(w_porter);
}


/* pack four bytes as "guint32" into buf
 * return the number of bytes packed, otherwise return -1 */
gint qq_put32(guint8 *buf, guint32 dw)
{
    guint32 dw_porter;
    dw_porter = g_htonl(dw);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put32] buf %p\n", (void *)buf);
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter);
#endif
    memcpy(buf, &dw_porter, sizeof(dw_porter));
    return sizeof(dw_porter);
}

gint qq_putIP(guint8* buf, struct in_addr *ip)
{
    memcpy(buf, ip, sizeof(struct in_addr));
    return sizeof(struct in_addr);
}

/* pack datalen bytes into buf
 * return the number of bytes packed, otherwise return -1 */
gint qq_putdata(guint8 *buf, const guint8 *data, const int datalen)
{
    memcpy(buf, data, datalen);
#ifdef PARSER_DEBUG
	purple_debug(PURPLE_DEBUG_INFO, "QQ", "[DBG][putdata] buf %p\n", (void *)buf);
#endif
    return datalen;
}