summaryrefslogtreecommitdiff
path: root/protocols/AimOscar/src/proxy.cpp
blob: 35076518aec4eb7ad6aec6aeb23e26a48d418396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Plugin of Miranda IM for communicating with users of the AIM protocol.
Copyright (c) 2008-2009 Boris Krasnovskiy
Copyright (C) 2005-2006 Aaron Myles Landwehr

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, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"

void __cdecl CAimProto::aim_proxy_helper(void* param)
{
	file_transfer *ft = (file_transfer*)param;

	if (ft->requester) 
	{
		if (proxy_initialize_send(ft->hConn, m_username, ft->icbm_cookie))
			return;//error
	}
	else
	{
		if (proxy_initialize_recv(ft->hConn, m_username, ft->icbm_cookie, ft->port))
			return;//error
	}

	//start listen for packets stuff
	NETLIBPACKETRECVER packetRecv = {0};
	packetRecv.cbSize = sizeof(packetRecv);
	packetRecv.dwTimeout = INFINITE;

	HANDLE hServerPacketRecver = (HANDLE) CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)ft->hConn, 2048 * 4);
	for (;;)
	{
		int recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)hServerPacketRecver, (LPARAM)&packetRecv);
		if (recvResult == 0) 
		{
			ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
			break;
		}
		if (recvResult == SOCKET_ERROR) 
		{
			ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
			break;
		}
		if (recvResult > 0) 
		{
			unsigned short length = _htons(*(unsigned short*)&packetRecv.buffer[0]);
			packetRecv.bytesUsed = length + 2;
			unsigned short type = _htons(*(unsigned short*)&packetRecv.buffer[4]);
			if (type == 0x0001)
			{
				unsigned short error = _htons(*(unsigned short*)&packetRecv.buffer[12]);
				switch (error)
				{
				case 0x000D:
					ShowPopup("Proxy Server File Transfer Error: Bad Request.", ERROR_POPUP);
					break;

				case 0x0010:
					ShowPopup("Proxy Server File Transfer Error: Initial Request Timed Out.", ERROR_POPUP);
					break;

				case 0x001A:
					ShowPopup("Proxy Server File Transfer Error: Accept Period Timed Out.", ERROR_POPUP);
					break;

				case 0x000e:
					ShowPopup("Proxy Server File Transfer Error: Incorrect command syntax.", ERROR_POPUP);
					break;

				case 0x0016:
					ShowPopup("Proxy Server File Transfer Error: Unknown command issued.", ERROR_POPUP);
					break;
				}

			}
			else if (type == 0x0003)
			{
				unsigned short port = _htons(*(unsigned short*)&packetRecv.buffer[12]);
				unsigned long  ip   = _htonl(*(unsigned long*)&packetRecv.buffer[14]);
				
				aim_send_file(m_hServerConn, m_seqno, ip, port, true, ft);
				debugLogA("Stage %d Proxy ft and we are not the sender.", ft->req_num);
			}
			else if (type == 0x0005) 
			{
				if (!ft->requester) 
				{
					aim_file_ad(m_hServerConn, m_seqno, ft->sn, ft->icbm_cookie, false, ft->max_ver);
					ft->accepted = true;
				}

				ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0);

				int i;
				for (i = 21; --i; )
				{
					if (Miranda_Terminated()) return;
					Sleep(100);
					if (ft->accepted) break;
				}
				if (i == 0) 
				{
					ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);
					break;
				}

				packetRecv.dwTimeout = 350000;

				int result;
				if (ft->sending)//we are sending
					result = sending_file(ft, hServerPacketRecver, packetRecv);
				else 
					result = receiving_file(ft, hServerPacketRecver, packetRecv);

				ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, result ? ACKRESULT_FAILED : ACKRESULT_SUCCESS, ft, 0);
				break;
			}
		}
	}
	Netlib_CloseHandle(hServerPacketRecver);
	Netlib_CloseHandle(ft->hConn);

	m_ft_list.remove_by_ft(ft);
}


int proxy_initialize_send(HANDLE connection, char* sn, char* cookie)
{
	const char sn_length = (char)mir_strlen(sn);
	const int len = sn_length + 21 + TLV_HEADER_SIZE + AIM_CAPS_LENGTH;

	char* buf= (char*)alloca(len);
	unsigned short offset=0;

	aim_writeshort(len-2, offset, buf);
	aim_writegeneric(10, "\x04\x4a\0\x02\0\0\0\0\0\0", offset, buf);
	aim_writechar((unsigned char)sn_length, offset, buf);               // screen name len
	aim_writegeneric(sn_length, sn, offset, buf);                       // screen name
	aim_writegeneric(8, cookie, offset, buf);                           // icbm cookie
	aim_writetlv(1, AIM_CAPS_LENGTH, AIM_CAP_FILE_TRANSFER, offset, buf);

	return Netlib_Send(connection, buf, offset, 0) >= 0 ? 0 : -1; 
}

int proxy_initialize_recv(HANDLE connection,char* sn, char* cookie,unsigned short port_check)
{
	const char sn_length = (char)mir_strlen(sn);
	const int len = sn_length + 23 + TLV_HEADER_SIZE + AIM_CAPS_LENGTH;

	char* buf= (char*)alloca(len);
	unsigned short offset=0;

	aim_writeshort(len-2, offset, buf);
	aim_writegeneric(10, "\x04\x4a\0\x04\0\0\0\0\0\0", offset, buf);
	aim_writechar((unsigned char)sn_length, offset, buf);               // screen name len
	aim_writegeneric(sn_length, sn, offset, buf);                       // screen name
	aim_writeshort(port_check, offset, buf);
	aim_writegeneric(8, cookie, offset, buf);                           // icbm cookie
	aim_writetlv(1, AIM_CAPS_LENGTH, AIM_CAP_FILE_TRANSFER, offset, buf);

	return Netlib_Send(connection, buf, offset, 0) >= 0 ? 0 : -1; 
}