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

📄 oft.c

📁 Linux下的多协议即时通讯程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Purple's oscar protocol plugin * This file is the legal property of its developers. * Please see the AUTHORS file distributed alongside this file. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*//* * I feel like this is a good place to explain OFT, so I'm going to * do just that.  Each OFT packet has a header type.  I guess this * is pretty similar to the subtype of a SNAC packet.  The type * basically tells the other client the meaning of the OFT packet. * There are two distinct types of file transfer, which I usually * call "sendfile" and "getfile."  Sendfile is when you send a file * to another AIM user.  Getfile is when you share a group of files, * and other users request that you send them the files. * * A typical sendfile file transfer goes like this: *   1) Sender sends a channel 2 ICBM telling the other user that *      we want to send them a file.  At the same time, we open a *      listener socket (this should be done before sending the *      ICBM) on some port, and wait for them to connect to us. *      The ICBM we sent should contain our IP address and the port *      number that we're listening on. *   2) The receiver connects to the sender on the given IP address *      and port.  After the connection is established, the receiver *      sends an ICBM signifying that we are ready and waiting. *   3) The sender sends an OFT PROMPT message over the OFT *      connection. *   4) The receiver of the file sends back an exact copy of this *      OFT packet, except the cookie is filled in with the cookie *      from the ICBM.  I think this might be an attempt to verify *      that the user that is connected is actually the guy that *      we sent the ICBM to.  Oh, I've been calling this the ACK. *   5) The sender starts sending raw data across the connection *      until the entire file has been sent. *   6) The receiver knows the file is finished because the sender *      sent the file size in an earlier OFT packet.  So then the *      receiver sends the DONE thingy (after filling in the *      "received" checksum and size) and closes the connection. */#include "oscar.h"#include "peer.h"#include "util.h"#define CHECKSUM_BUFFER_SIZE 256 * 1024struct _ChecksumData{	PeerConnection *conn;	PurpleXfer *xfer;	GSourceFunc callback;	size_t size;	guint32 checksum;	size_t total;	FILE *file;	guint8 buffer[CHECKSUM_BUFFER_SIZE];	guint timer;};voidpeer_oft_checksum_destroy(ChecksumData *checksum_data){	checksum_data->conn->checksum_data = NULL;	fclose(checksum_data->file);	if (checksum_data->timer > 0)		purple_timeout_remove(checksum_data->timer);	g_free(checksum_data);}/** * Calculate oft checksum of buffer * * Prevcheck should be 0xFFFF0000 when starting a checksum of a file.  The * checksum is kind of a rolling checksum thing, so each time you get bytes * of a file you just call this puppy and it updates the checksum.  You can * calculate the checksum of an entire file by calling this in a while or a * for loop, or something. * * Thanks to Graham Booker for providing this improved checksum routine, * which is simpler and should be more accurate than Josh Myer's original * code. -- wtm * * This algorithm works every time I have tried it.  The other fails * sometimes.  So, AOL who thought this up?  It has got to be the weirdest * checksum I have ever seen. * * @param buffer Buffer of data to checksum.  Man I'd like to buff her... * @param bufsize Size of buffer. * @param prevchecksum Previous checksum. * @param odd Whether an odd number of bytes have been processed before this call */static guint32peer_oft_checksum_chunk(const guint8 *buffer, int bufferlen, guint32 prevchecksum, int odd){	guint32 checksum, oldchecksum;	int i = 0;	unsigned short val;	checksum = (prevchecksum >> 16) & 0xffff;	if (odd)	{		/*		 * This is one hell of a hack, but it should always work.		 * Essentially, I am reindexing the array so that index 1		 * is the first element.  Since the odd and even bytes are		 * detected by the index number.		 */		i = 1;		bufferlen++;		buffer--;	}	for (; i < bufferlen; i++)	{		oldchecksum = checksum;		if (i & 1)			val = buffer[i];		else			val = buffer[i] << 8;		checksum -= val;		/*		 * The following appears to be necessary.... It happens		 * every once in a while and the checksum doesn't fail.		 */		if (checksum > oldchecksum)			checksum--;	}	checksum = ((checksum & 0x0000ffff) + (checksum >> 16));	checksum = ((checksum & 0x0000ffff) + (checksum >> 16));	return checksum << 16;}static gbooleanpeer_oft_checksum_file_piece(gpointer data){	ChecksumData *checksum_data;	gboolean repeat;	checksum_data = data;	repeat = FALSE;	if (checksum_data->total < checksum_data->size)	{		size_t bytes = MIN(CHECKSUM_BUFFER_SIZE,				checksum_data->size - checksum_data->total);		bytes = fread(checksum_data->buffer, 1, bytes, checksum_data->file);		if (bytes != 0)		{			checksum_data->checksum = peer_oft_checksum_chunk(checksum_data->buffer, bytes, checksum_data->checksum, checksum_data->total & 1);			checksum_data->total += bytes;			repeat = TRUE;		}	}	if (!repeat)	{		purple_debug_info("oscar", "Checksum of %s calculated\n",				purple_xfer_get_local_filename(checksum_data->xfer));		if (checksum_data->callback != NULL)			checksum_data->callback(checksum_data);		peer_oft_checksum_destroy(checksum_data);	}	return repeat;}/** * Calculate oft checksum of a file in a series of calls to * peer_oft_checksum_file_piece().  We do it this way because * calculating the checksum on large files can take a long time, * and we want to return control to the UI so that the application * doesn't appear completely frozen. * * @param conn The connection used for this file transfer. * @param xfer The file transfer needing this checksum. * @param callback The function to call upon calculation of the checksum. * @param size The maximum size to check. */static voidpeer_oft_checksum_file(PeerConnection *conn, PurpleXfer *xfer, GSourceFunc callback, size_t size){	ChecksumData *checksum_data;	purple_debug_info("oscar", "Calculating checksum of %s\n",			purple_xfer_get_local_filename(xfer));	checksum_data = g_malloc0(sizeof(ChecksumData));	checksum_data->conn = conn;	checksum_data->xfer = xfer;	checksum_data->callback = callback;	checksum_data->size = size;	checksum_data->checksum = 0xffff0000;	checksum_data->file = fopen(purple_xfer_get_local_filename(xfer), "rb");	if (checksum_data->file == NULL)	{		purple_debug_error("oscar", "Unable to open %s for checksumming: %s\n",				purple_xfer_get_local_filename(xfer), strerror(errno));		callback(checksum_data);		g_free(checksum_data);	}	else	{		checksum_data->timer = purple_timeout_add(10,				peer_oft_checksum_file_piece, checksum_data);		conn->checksum_data = checksum_data;	}}static voidpeer_oft_copy_xfer_data(PeerConnection *conn, OftFrame *frame){	g_free(conn->xferdata.name);	memcpy(&(conn->xferdata), frame, sizeof(OftFrame));	conn->xferdata.name = g_memdup(frame->name, frame->name_length);}/** * Free any OFT related data. */voidpeer_oft_close(PeerConnection *conn){	/*	 * If canceled by local user, and we're receiving a file, and	 * we're not connected/ready then send an ICBM cancel message.	 */	if ((purple_xfer_get_status(conn->xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL) &&		!conn->ready)	{		aim_im_sendch2_cancel(conn);	}	if (conn->sending_data_timer != 0)	{		purple_timeout_remove(conn->sending_data_timer);		conn->sending_data_timer = 0;	}}/** * Write the given OftFrame to a ByteStream and send it out * on the established PeerConnection. */static voidpeer_oft_send(PeerConnection *conn, OftFrame *frame){	size_t length;	ByteStream bs;	length = 192 + MAX(64, frame->name_length + 1);	byte_stream_new(&bs, length);	byte_stream_putraw(&bs, conn->magic, 4);	byte_stream_put16(&bs, length);	byte_stream_put16(&bs, frame->type);	byte_stream_putraw(&bs, frame->cookie, 8);	byte_stream_put16(&bs, frame->encrypt);	byte_stream_put16(&bs, frame->compress);	byte_stream_put16(&bs, frame->totfiles);	byte_stream_put16(&bs, frame->filesleft);	byte_stream_put16(&bs, frame->totparts);	byte_stream_put16(&bs, frame->partsleft);	byte_stream_put32(&bs, frame->totsize);	byte_stream_put32(&bs, frame->size);	byte_stream_put32(&bs, frame->modtime);	byte_stream_put32(&bs, frame->checksum);	byte_stream_put32(&bs, frame->rfrcsum);	byte_stream_put32(&bs, frame->rfsize);	byte_stream_put32(&bs, frame->cretime);	byte_stream_put32(&bs, frame->rfcsum);	byte_stream_put32(&bs, frame->nrecvd);	byte_stream_put32(&bs, frame->recvcsum);	byte_stream_putraw(&bs, frame->idstring, 32);	byte_stream_put8(&bs, frame->flags);	byte_stream_put8(&bs, frame->lnameoffset);	byte_stream_put8(&bs, frame->lsizeoffset);	byte_stream_putraw(&bs, frame->dummy, 69);	byte_stream_putraw(&bs, frame->macfileinfo, 16);	byte_stream_put16(&bs, frame->nencode);	byte_stream_put16(&bs, frame->nlanguage);	/*	 * The name can be more than 64 characters, but if it is less than	 * 64 characters it is padded with NULLs.	 */	byte_stream_putraw(&bs, frame->name, MAX(64, frame->name_length + 1));	peer_connection_send(conn, &bs);	g_free(bs.data);}voidpeer_oft_send_prompt(PeerConnection *conn){	conn->xferdata.type = PEER_TYPE_PROMPT;	peer_oft_send(conn, &conn->xferdata);}static voidpeer_oft_send_ack(PeerConnection *conn){	conn->xferdata.type = PEER_TYPE_ACK;	/* Fill in the cookie */	memcpy(conn->xferdata.cookie, conn->cookie, 8);	peer_oft_send(conn, &conn->xferdata);}static voidpeer_oft_send_resume_accept(PeerConnection *conn){	conn->xferdata.type = PEER_TYPE_RESUMEACCEPT;	/* Fill in the cookie */	memcpy(conn->xferdata.cookie, conn->cookie, 8);	peer_oft_send(conn, &conn->xferdata);}static voidpeer_oft_send_done(PeerConnection *conn){	conn->xferdata.type = PEER_TYPE_DONE;	conn->xferdata.filesleft = 0;	conn->xferdata.partsleft = 0;	conn->xferdata.nrecvd = purple_xfer_get_bytes_sent(conn->xfer);	peer_oft_send(conn, &conn->xferdata);}/** * This function exists so that we don't remove the outgoing * data watcher while we're still sending data.  In most cases * any data we're sending will be instantly wisked away to a TCP * buffer maintained by our operating system... but we want to * make sure the core doesn't start sending file data while * we're still sending OFT frame data.  That would be bad. */static gbooleanstart_transfer_when_done_sending_data(gpointer data){	PeerConnection *conn;	conn = data;	if (purple_circ_buffer_get_max_read(conn->buffer_outgoing) == 0)	{		conn->sending_data_timer = 0;		conn->xfer->fd = conn->fd;		conn->fd = -1;		purple_xfer_start(conn->xfer, conn->xfer->fd, NULL, 0);		return FALSE;	}	return TRUE;}/** * This function is similar to the above function, except instead * of starting the xfer it will destroy the connection.  This is * used when you want to send one final message across the peer * connection, and then close everything. */static gbooleandestroy_connection_when_done_sending_data(gpointer data){	PeerConnection *conn;	conn = data;	if (purple_circ_buffer_get_max_read(conn->buffer_outgoing) == 0)	{		conn->sending_data_timer = 0;		peer_connection_destroy(conn, conn->disconnect_reason, NULL);		return FALSE;	}	return TRUE;}/* * This is called when a buddy sends us some file info.  This happens when they * are sending a file to you, and you have just established a connection to them. * You should send them the exact same info except use the real cookie.  We also * get like totally ready to like, receive the file, kay? */static voidpeer_oft_recv_frame_prompt(PeerConnection *conn, OftFrame *frame){	/* Record the file information and send an ack */

⌨️ 快捷键说明

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