⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_trans.c

📁 Linux下的多协议即时通讯程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * @file file_trans.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#ifdef _WIN32#define random rand#endif#include "debug.h"#include "ft.h"#include "cipher.h"#include "crypt.h"#include "file_trans.h"#include "header_info.h"#include "im.h"#include "packet_parse.h"#include "proxy.h"#include "send_core.h"#include "send_file.h"#include "utils.h"struct _qq_file_header {	guint8 tag;	guint16 client_ver;	guint8 file_key;	guint32 sender_uid;	guint32 receiver_uid;};typedef struct _qq_file_header qq_file_header;static guint32 _get_file_key(guint8 seed){	guint32 key;	key = seed | (seed << 8) | (seed << 16) | (seed << 24);	return key;}		static guint32 _gen_file_key(){	guint8 seed;		seed = random();	return _get_file_key(seed);}static guint32 _decrypt_qq_uid(guint32 uid, guint32 key){	return ~(uid ^ key);}static guint32 _encrypt_qq_uid(guint32 uid, guint32 key){	return (~uid) ^ key;}static void _fill_filename_md5(const gchar *filename, guint8 *md5){	PurpleCipher *cipher;	PurpleCipherContext *context;	g_return_if_fail(filename != NULL && md5 != NULL);	cipher = purple_ciphers_find_cipher("md5");	context = purple_cipher_context_new(cipher, NULL);	purple_cipher_context_append(context, (guint8 *) filename, strlen(filename));	purple_cipher_context_digest(context, 16, md5, NULL);	purple_cipher_context_destroy(context);}static void _fill_file_md5(const gchar *filename, gint filelen, guint8 *md5){	FILE *fp;	guint8 *buffer;	PurpleCipher *cipher;	PurpleCipherContext *context;	const gint QQ_MAX_FILE_MD5_LENGTH = 10002432;	g_return_if_fail(filename != NULL && md5 != NULL);	if (filelen > QQ_MAX_FILE_MD5_LENGTH) 		filelen = QQ_MAX_FILE_MD5_LENGTH;	fp = fopen(filename, "rb");	g_return_if_fail(fp != NULL);	buffer = g_newa(guint8, filelen);	g_return_if_fail(buffer != NULL);	fread(buffer, filelen, 1, fp);	cipher = purple_ciphers_find_cipher("md5");	context = purple_cipher_context_new(cipher, NULL);	purple_cipher_context_append(context, buffer, filelen);	purple_cipher_context_digest(context, 16, md5, NULL);	purple_cipher_context_destroy(context);	fclose(fp);}static void _qq_get_file_header(guint8 *buf, guint8 **cursor, gint buflen, qq_file_header *fh){	read_packet_b(buf, cursor, buflen, &(fh->tag));	read_packet_w(buf, cursor, buflen, &(fh->client_ver));	read_packet_b(buf, cursor, buflen, &fh->file_key);	read_packet_dw(buf, cursor, buflen, &(fh->sender_uid));	read_packet_dw(buf, cursor, buflen, &(fh->receiver_uid));	fh->sender_uid = _decrypt_qq_uid(fh->sender_uid, _get_file_key(fh->file_key));	fh->receiver_uid = _decrypt_qq_uid(fh->receiver_uid, _get_file_key(fh->file_key));}static const gchar *qq_get_file_cmd_desc(gint type){	switch (type) {		case QQ_FILE_CMD_SENDER_SAY_HELLO:			return "QQ_FILE_CMD_SENDER_SAY_HELLO";		case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:			return "QQ_FILE_CMD_SENDER_SAY_HELLO_ACK";		case QQ_FILE_CMD_RECEIVER_SAY_HELLO:			return "QQ_FILE_CMD_RECEIVER_SAY_HELLO";		case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:			return "QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK";		case QQ_FILE_CMD_NOTIFY_IP_ACK:			return "QQ_FILE_CMD_NOTIFY_IP_ACK";		case QQ_FILE_CMD_PING:			return "QQ_FILE_CMD_PING";		case QQ_FILE_CMD_PONG:			return "QQ_FILE_CMD_PONG";		case QQ_FILE_CMD_INITATIVE_CONNECT:			return "QQ_FILE_CMD_INITATIVE_CONNECT";		case QQ_FILE_CMD_FILE_OP:			return "QQ_FILE_CMD_FILE_OP";		case QQ_FILE_CMD_FILE_OP_ACK:			return "QQ_FILE_CMD_FILE_OP_ACK";		case QQ_FILE_BASIC_INFO:			return "QQ_FILE_BASIC_INFO";		case QQ_FILE_DATA_INFO:			return "QQ_FILE_DATA_INFO";		case QQ_FILE_EOF:			return "QQ_FILE_EOF";		default:			return "UNKNOWN_TYPE";	}}/* The memmap version has better performance for big files transfering * but it will spend plenty of memory, so do not use it in a low-memory host */#ifdef USE_MMAP#include <sys/mman.h>static int _qq_xfer_open_file(const gchar *filename, const gchar *method, PurpleXfer *xfer){	ft_info *info = xfer->data;	int fd;	if (method[0] == 'r') {		fd = open(purple_xfer_get_local_filename(xfer), O_RDONLY);		info->buffer = mmap(0, purple_xfer_get_size(xfer), PROT_READ, MAP_PRIVATE, fd, 0);	}	else 	{		fd = open(purple_xfer_get_local_filename(xfer), O_RDWR|O_CREAT, 0644);		info->buffer = mmap(0, purple_xfer_get_size(xfer), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE, fd, 0);	}			if (info->buffer == NULL) {		return - 1;	}	return 0;}static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, PurpleXfer *xfer){	ft_info *info = xfer->data;	gint readbytes;	buffer = info->buffer + len * index;	readbytes = purple_xfer_get_size(xfer) - (buffer - info->buffer);	if (readbytes > info->fragment_len) readbytes = info->fragment_len;	return readbytes;}static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, PurpleXfer *xfer){	ft_info *info = xfer->data;	memcpy(info->buffer + index * len, buffer, len);	return 0;}void qq_xfer_close_file(PurpleXfer *xfer){	ft_info *info = xfer->data;	if (info->buffer) munmap(info->buffer, purple_xfer_get_size(xfer));}#elsestatic int _qq_xfer_open_file(const gchar *filename, const gchar *method, PurpleXfer *xfer){	ft_info *info = xfer->data;	info->dest_fp = fopen(purple_xfer_get_local_filename(xfer), method);	if (info->dest_fp == NULL) {		return -1;	}	return 0;}static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, PurpleXfer *xfer){	ft_info *info = xfer->data;	fseek(info->dest_fp, index * len, SEEK_SET);	return fread(buffer, 1, len, info->dest_fp);}static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, PurpleXfer *xfer){	ft_info *info = xfer->data;	fseek(info->dest_fp, index * len, SEEK_SET);	return fwrite(buffer, 1, len, info->dest_fp);}void qq_xfer_close_file(PurpleXfer *xfer){	ft_info *info = xfer->data;	if (info->dest_fp) fclose(info->dest_fp);}#endifstatic gint _qq_send_file(PurpleConnection *gc, guint8 *data, gint len, guint16 packet_type, guint32 to_uid){	gint bytes;	guint8 *cursor, *buf;	guint32 file_key;	qq_data *qd;	ft_info *info;	qd = (qq_data *) gc->proto_data;	g_return_val_if_fail(qd->session_key != NULL, -1);	info = (ft_info *) qd->xfer->data;	bytes = 0;	buf = g_newa(guint8, MAX_PACKET_SIZE);	cursor = buf;	file_key = _gen_file_key();	bytes += create_packet_b(buf, &cursor, packet_type);	bytes += create_packet_w(buf, &cursor, QQ_CLIENT);	bytes += create_packet_b(buf, &cursor, file_key & 0xff);	bytes += create_packet_dw(buf, &cursor, _encrypt_qq_uid(qd->uid, file_key));	bytes += create_packet_dw(buf, &cursor, _encrypt_qq_uid(to_uid, file_key));	bytes += create_packet_data(buf, &cursor, data, len);	if (bytes == len + 12) {		_qq_xfer_write(buf, bytes, qd->xfer);	} else		purple_debug(PURPLE_DEBUG_INFO, "QQ", "send_file: want %d but got %d\n", len + 12, bytes);	return bytes;}/* send a file to udp channel with QQ_FILE_CONTROL_PACKET_TAG */void qq_send_file_ctl_packet(PurpleConnection *gc, guint16 packet_type, guint32 to_uid, guint8 hellobyte){	qq_data *qd;	gint bytes, bytes_expected, encrypted_len;	guint8 *raw_data, *cursor, *encrypted_data;	time_t now;	ft_info *info;		qd = (qq_data *) gc->proto_data;	info = (ft_info *) qd->xfer->data;	raw_data = g_new0 (guint8, 61);	cursor = raw_data;		bytes = 0;	now = time(NULL);	bytes += create_packet_data(raw_data, &cursor, qd->session_md5, 16);	bytes += create_packet_w(raw_data, &cursor, packet_type);	switch (packet_type) {		case QQ_FILE_CMD_SENDER_SAY_HELLO:		case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:		case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:		case QQ_FILE_CMD_NOTIFY_IP_ACK:		case QQ_FILE_CMD_RECEIVER_SAY_HELLO:			bytes += create_packet_w(raw_data, &cursor, info->send_seq);			break;		default:			bytes += create_packet_w(raw_data, &cursor, ++qd->send_seq);	}	bytes += create_packet_dw(raw_data, &cursor, (guint32) now);	bytes += create_packet_b(raw_data, &cursor, 0x00);	bytes += create_packet_b(raw_data, &cursor, qd->my_icon);	bytes += create_packet_dw(raw_data, &cursor, 0x00000000);	bytes += create_packet_dw(raw_data, &cursor, 0x00000000);	bytes += create_packet_dw(raw_data, &cursor, 0x00000000);	bytes += create_packet_dw(raw_data, &cursor, 0x00000000);	bytes += create_packet_w(raw_data, &cursor, 0x0000);	bytes += create_packet_b(raw_data, &cursor, 0x00);	/* 0x65: send a file, 0x6b: send a custom face */	bytes += create_packet_b(raw_data, &cursor, QQ_FILE_TRANSFER_FILE); /* FIXME temp by gfhuang */	switch (packet_type)	{		case QQ_FILE_CMD_SENDER_SAY_HELLO:		case QQ_FILE_CMD_RECEIVER_SAY_HELLO:		case QQ_FILE_CMD_SENDER_SAY_HELLO_ACK:		case QQ_FILE_CMD_RECEIVER_SAY_HELLO_ACK:			bytes += create_packet_b(raw_data, &cursor, 0x00);			bytes += create_packet_b(raw_data, &cursor, hellobyte);			bytes_expected = 48;			break;		case QQ_FILE_CMD_PING:		case QQ_FILE_CMD_PONG:		case QQ_FILE_CMD_NOTIFY_IP_ACK:			bytes += qq_fill_conn_info(raw_data, &cursor, info);			bytes_expected = 61;			break;		default:			purple_debug(PURPLE_DEBUG_INFO, "QQ", "qq_send_file_ctl_packet: Unknown packet type[%d]\n",					packet_type);			bytes_expected = 0;	}		if (bytes == bytes_expected) {		gchar *hex_dump = hex_dump_to_str(raw_data, bytes);		purple_debug(PURPLE_DEBUG_INFO, "QQ", "sending packet[%s]: \n%s", qq_get_file_cmd_desc(packet_type), hex_dump);		g_free(hex_dump);		encrypted_len = bytes + 16;		encrypted_data = g_newa(guint8, encrypted_len);		qq_crypt(ENCRYPT, raw_data, bytes, info->file_session_key, encrypted_data, &encrypted_len);		/*debug: try to decrypt it */		/*		if (QQ_DEBUG) {			guint8 *buf;			int buflen;			hex_dump = hex_dump_to_str(encrypted_data, encrypted_len);			purple_debug(PURPLE_DEBUG_INFO, "QQ", "encrypted packet: \n%s", hex_dump);			g_free(hex_dump);			buf = g_newa(guint8, MAX_PACKET_SIZE);			buflen = encrypted_len;			if (qq_crypt(DECRYPT, encrypted_data, encrypted_len, info->file_session_key, buf, &buflen)) {				purple_debug(PURPLE_DEBUG_INFO, "QQ", "decrypt success\n");				if (buflen == bytes && memcmp(raw_data, buf, buflen) == 0)					purple_debug(PURPLE_DEBUG_INFO, "QQ", "checksum ok\n");				hex_dump = hex_dump_to_str(buf, buflen);				purple_debug(PURPLE_DEBUG_INFO, "QQ", "decrypted packet: \n%s", hex_dump);				g_free(hex_dump);			} else {				purple_debug(PURPLE_DEBUG_INFO, "QQ", "decrypt fail\n");			}		}		*/		purple_debug(PURPLE_DEBUG_INFO, "QQ", "<== send %s packet\n", qq_get_file_cmd_desc(packet_type));		_qq_send_file(gc, encrypted_data, encrypted_len, QQ_FILE_CONTROL_PACKET_TAG, info->to_uid);	}	else		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "qq_send_file_ctl_packet: Expected to get %d bytes, but get %d",				bytes_expected, bytes);}/* send a file to udp channel with QQ_FILE_DATA_PACKET_TAG */static void _qq_send_file_data_packet(PurpleConnection *gc, guint16 packet_type, guint8 sub_type, 		guint32 fragment_index, guint16 seq, guint8 *data, gint len){	gint bytes;	guint8 *raw_data, *cursor, filename_md5[QQ_KEY_LENGTH], file_md5[QQ_KEY_LENGTH];	guint32 fragment_size = 1000;	gchar *filename;	gint filename_len, filesize;	qq_data *qd;	ft_info *info;	qd = (qq_data *) gc->proto_data;	info = (ft_info *) qd->xfer->data;	filename = (gchar *) purple_xfer_get_filename(qd->xfer);	filesize = purple_xfer_get_size(qd->xfer);	raw_data = g_newa(guint8, MAX_PACKET_SIZE);	cursor = raw_data;	bytes = 0;	bytes += create_packet_b(raw_data, &cursor, 0x00);	bytes += create_packet_w(raw_data, &cursor, packet_type);	switch (packet_type) {		case QQ_FILE_BASIC_INFO:		case QQ_FILE_DATA_INFO:		case QQ_FILE_EOF:			bytes += create_packet_w(raw_data, &cursor, 0x0000);			bytes += create_packet_b(raw_data, &cursor, 0x00);			break;		case QQ_FILE_CMD_FILE_OP:			switch(sub_type)			{				case QQ_FILE_BASIC_INFO:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -