summaryrefslogtreecommitdiff
path: root/protocols/ICQCorp/src/socket.cpp
blob: 5a88d9a201a906afe55ee510b806d3c00c7e6a43 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
    ICQ Corporate protocol plugin for Miranda IM.
    Copyright (C) 2003-2005 Eugene Tarasenko <zlyden13@inbox.ru>

    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.
*/

#include "corp.h"

///////////////////////////////////////////////////////////////////////////////
//  Socket
///////////////////////////////////////////////////////////////////////////////

Socket::Socket()
{
	handleVal = INVALID_SOCKET;
	remoteIPVal = 0;
	remotePortVal = 0;
	localIPVal = 0;
	localPortVal = 0;
	messageVal = 0;
}

///////////////////////////////////////////////////////////////////////////////

Socket::~Socket()
{
	if (connected()) closeConnection();
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::connected()
{
	return handleVal != INVALID_SOCKET;
};

///////////////////////////////////////////////////////////////////////////////

bool Socket::completed(int result)
{
	if (result != SOCKET_ERROR || WSAGetLastError() != WSAEWOULDBLOCK)
		return true;

	Sleep(1);
	return false;
};

///////////////////////////////////////////////////////////////////////////////

void Socket::openSocket()
{
	if (connected())
		WSAAsyncSelect(handleVal, icq.hWnd, messageVal, FD_READ | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
}

///////////////////////////////////////////////////////////////////////////////

void Socket::closeSocket()
{
	if (connected())
		WSAAsyncSelect(handleVal, icq.hWnd, 0, 0);
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::setDestination(unsigned long rIp, unsigned short rPort)
{
	if (rIp == 0)
		return false;

	char localName[128];
	gethostname(localName, 128);
	localIPVal = lookup(localName);

	// set up remote connection
	remoteIPVal = rIp;
	remotePortVal = rPort;
	remote.sin_port = htons(rPort);
	remote.sin_addr.s_addr = rIp;
	return true;
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::setDestination(char *rHost, unsigned short rPort)
{
	return setDestination(lookup(rHost), rPort);
};

///////////////////////////////////////////////////////////////////////////////

unsigned long Socket::lookup(char *h)
{
	// check if the hostname is in dot and number notation
	unsigned long ina = inet_addr(h);
	if (ina != INADDR_NONE)
		return ina;

	// try and resolve hostname
	struct hostent *host = gethostbyname(h);
	if (host == nullptr) // Couldn't resolve hostname/ip
		return 0;

	// return the ip
	return *((unsigned long *)host->h_addr);
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::openConnection(int Type)
{
	handleVal = socket(AF_INET, Type, 0);
	if (handleVal == INVALID_SOCKET)
		return false;

	struct sockaddr_in local;
	memset(&local.sin_zero, 0, 8);
	local.sin_family = AF_INET;
	local.sin_port = htons(localPortVal);
	local.sin_addr.s_addr = htonl(INADDR_ANY);
	// local.sin_addr.s_addr = htonl(localIPVal);

	memset(&remote.sin_zero, 0, 8);
	remote.sin_family = AF_INET;

	bind(handleVal, (sockaddr*)&local, sizeof(sockaddr));

	int sizeofSockaddr = sizeof(sockaddr);
	getsockname(handleVal, (sockaddr*)&local, &sizeofSockaddr);
	localPortVal = ntohs(local.sin_port);
	if (local.sin_addr.s_addr)
		localIPVal = ntohl(local.sin_addr.s_addr);

	return true;
}

///////////////////////////////////////////////////////////////////////////////

void Socket::closeConnection()
{
	if (connected()) {
		closeSocket();
		shutdown(handleVal, SD_BOTH);
		closesocket(handleVal);
		handleVal = INVALID_SOCKET;
	}
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::sendBuffer(char *buffer, unsigned int bufferSize)
{
	int result;

	while (bufferSize > 0) {
		while (!completed(result = send(handleVal, buffer, bufferSize, 0)));
		if (result <= 0) {
			closeConnection();
			return false;
		}
		buffer += result;
		bufferSize -= result;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////

bool Socket::receiveBuffer(char *buffer, unsigned int bufferSize)
{
	int result;

	while (bufferSize > 0) {
		while (!completed(result = recv(handleVal, buffer, bufferSize, 0)));
		if (result <= 0) {
			closeConnection();
			return false;
		}
		buffer += result;
		bufferSize -= result;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////
//
//  TCPSocket
//
///////////////////////////////////////////////////////////////////////////////

TCPSocket::TCPSocket(unsigned int msg)
{
	messageVal = msg;
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::openConnection()
{
	if (remoteIPVal == 0)
		return false;
	if (!Socket::openConnection(SOCK_STREAM))
		return false;

	int result = connect(handleVal, (sockaddr*)&remote, sizeof(sockaddr));
	if (result == SOCKET_ERROR) {
		closeConnection();
		return false;
	}
	openSocket();
	return true;
}

///////////////////////////////////////////////////////////////////////////////

void TCPSocket::receiveConnection(TCPSocket &newSocket)
{
	int sizeofSockaddr = sizeof(sockaddr);
	newSocket.handleVal = accept(handleVal, (sockaddr*)&remote, &sizeofSockaddr);
	newSocket.openSocket();

	sizeofSockaddr = sizeof(sockaddr);
	getpeername(newSocket.handleVal, (sockaddr*)&newSocket.remote, &sizeofSockaddr);

	newSocket.remoteIPVal = newSocket.remote.sin_addr.s_addr;
	newSocket.remotePortVal = ntohs(newSocket.remote.sin_port);

	newSocket.localIPVal = localIPVal;
	newSocket.localPortVal = localPortVal;
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::startServer()
{
	if (!Socket::openConnection(SOCK_STREAM)) return false;
	listen(handleVal, 10);
	openSocket();
	return true;
}

///////////////////////////////////////////////////////////////////////////////

void TCPSocket::transferConnectionFrom(TCPSocket &from)
{
	handleVal = from.handleVal;
	remoteIPVal = from.remoteIPVal;
	remotePortVal = from.remotePortVal;
	localIPVal = from.localIPVal;
	localPortVal = from.localPortVal;

	from.closeSocket();
	from.handleVal = INVALID_SOCKET;
	openSocket();
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::sendPacket(Packet &p)
{
	return sendPacket(p, true);
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::receivePacket(Packet &p)
{
	return receivePacket(p, true);
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::sendPacket(Packet &p, bool sendSize)
{
	if (!connected())
		return false;
	
	if (sendSize) {
		char pSize[2];

		pSize[0] = (p.sizeVal) & 0xFF;
		pSize[1] = (p.sizeVal >> 8) & 0xFF;

		if (!sendBuffer(pSize, 2))
			return false;
	}
	return sendBuffer(p.buff, p.sizeVal);
}

///////////////////////////////////////////////////////////////////////////////

bool TCPSocket::receivePacket(Packet &p, bool recvSize)
{
	if (!connected())
		return false;
	
	WSAAsyncSelect(handleVal, icq.hWnd, messageVal, FD_ACCEPT | FD_CONNECT | FD_CLOSE);

	p.clearPacket();

	unsigned short sizeVal;
	if (recvSize) {
		if (!receiveBuffer((char*)&sizeVal, 2) || sizeVal > p.maxSize)
			return false;
	}
	else sizeVal = 1;

	if (!receiveBuffer(p.buff, sizeVal))
		return false;
	
	p.sizeVal = sizeVal;
	WSAAsyncSelect(handleVal, icq.hWnd, messageVal, FD_READ | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
	return true;
}

///////////////////////////////////////////////////////////////////////////////
//
//  UDPSocket
//
///////////////////////////////////////////////////////////////////////////////

UDPSocket::UDPSocket(unsigned int msg)
{
	messageVal = msg;
}

///////////////////////////////////////////////////////////////////////////////

bool UDPSocket::openConnection()
{
	if (remoteIPVal == 0)
		return false;
	if (!Socket::openConnection(SOCK_DGRAM))
		return false;
	
	openSocket();
	return true;
}

///////////////////////////////////////////////////////////////////////////////

bool UDPSocket::startServer()
{
	if (!Socket::openConnection(SOCK_DGRAM))
		return false;
	
	openSocket();
	return true;
}

///////////////////////////////////////////////////////////////////////////////

bool UDPSocket::sendPacket(Packet &p)
{
	if (!connected())
		return false;

	unsigned int bytesSent = 0;
	bytesSent = sendto(handleVal, p.buff, p.sizeVal, 0, (sockaddr*)&remote, sizeof(sockaddr));

	// if unable to send anything
	if (bytesSent != p.sizeVal) {
		closeConnection();
		return false;
	}

	return true;
}

///////////////////////////////////////////////////////////////////////////////

bool UDPSocket::receivePacket(Packet &p)
{
	if (!connected())
		return false;

	int sizeofSockaddr = sizeof(sockaddr);
	p.clearPacket();
	p.sizeVal = recvfrom(handleVal, p.buff, p.maxSize, 0, (sockaddr*)&remote, &sizeofSockaddr);

	// it didn't manage to receive anything, there was an error, close the socket and return false
	if (int(p.sizeVal) <= 0) {
		closeConnection();
		return false;
	}

	// make sure the size won't overflow the buffer
	if (p.sizeVal > p.maxSize) p.sizeVal = p.maxSize - sizeof(unsigned long);

	return true;
}