1*c2c66affSColin Finck /* -*- c-basic-offset: 8 -*-
2*c2c66affSColin Finck    rdesktop: A Remote Desktop Protocol client.
3*c2c66affSColin Finck    Protocol services - TCP layer
4*c2c66affSColin Finck    Copyright (C) Matthew Chapman 1999-2005
5*c2c66affSColin Finck 
6*c2c66affSColin Finck    This program is free software; you can redistribute it and/or modify
7*c2c66affSColin Finck    it under the terms of the GNU General Public License as published by
8*c2c66affSColin Finck    the Free Software Foundation; either version 2 of the License, or
9*c2c66affSColin Finck    (at your option) any later version.
10*c2c66affSColin Finck 
11*c2c66affSColin Finck    This program is distributed in the hope that it will be useful,
12*c2c66affSColin Finck    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c2c66affSColin Finck    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c2c66affSColin Finck    GNU General Public License for more details.
15*c2c66affSColin Finck 
16*c2c66affSColin Finck    You should have received a copy of the GNU General Public License along
17*c2c66affSColin Finck    with this program; if not, write to the Free Software Foundation, Inc.,
18*c2c66affSColin Finck    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*c2c66affSColin Finck */
20*c2c66affSColin Finck 
21*c2c66affSColin Finck #if 0 // FIXME: header mess
22*c2c66affSColin Finck #include <unistd.h>		/* select read write close */
23*c2c66affSColin Finck #include <sys/socket.h>		/* socket connect setsockopt */
24*c2c66affSColin Finck #include <sys/time.h>		/* timeval */
25*c2c66affSColin Finck #include <netdb.h>		/* gethostbyname */
26*c2c66affSColin Finck #include <netinet/in.h>		/* sockaddr_in */
27*c2c66affSColin Finck #include <netinet/tcp.h>	/* TCP_NODELAY */
28*c2c66affSColin Finck #include <arpa/inet.h>		/* inet_addr */
29*c2c66affSColin Finck #include <errno.h>		/* errno */
30*c2c66affSColin Finck #endif
31*c2c66affSColin Finck 
32*c2c66affSColin Finck #include "rdesktop.h"
33*c2c66affSColin Finck #include <winsock2.h>
34*c2c66affSColin Finck 
35*c2c66affSColin Finck typedef int socklen_t;
36*c2c66affSColin Finck 
37*c2c66affSColin Finck #ifndef INADDR_NONE
38*c2c66affSColin Finck #define INADDR_NONE ((unsigned long) -1)
39*c2c66affSColin Finck #endif
40*c2c66affSColin Finck 
41*c2c66affSColin Finck /* Initialise TCP transport data packet */
42*c2c66affSColin Finck STREAM
tcp_init(RDPCLIENT * This,uint32 maxlen)43*c2c66affSColin Finck tcp_init(RDPCLIENT * This, uint32 maxlen)
44*c2c66affSColin Finck {
45*c2c66affSColin Finck 	if (maxlen > This->tcp.out.size)
46*c2c66affSColin Finck 	{
47*c2c66affSColin Finck 		void * p;
48*c2c66affSColin Finck 
49*c2c66affSColin Finck 		p = realloc(This->tcp.out.data, maxlen);
50*c2c66affSColin Finck 
51*c2c66affSColin Finck 		if (p == NULL)
52*c2c66affSColin Finck 		{
53*c2c66affSColin Finck 			This->disconnect_reason = 262;
54*c2c66affSColin Finck 			return NULL;
55*c2c66affSColin Finck 		}
56*c2c66affSColin Finck 
57*c2c66affSColin Finck 		This->tcp.out.data = (uint8 *)p;
58*c2c66affSColin Finck 		This->tcp.out.size = maxlen;
59*c2c66affSColin Finck 	}
60*c2c66affSColin Finck 
61*c2c66affSColin Finck 	This->tcp.out.p = This->tcp.out.data;
62*c2c66affSColin Finck 	This->tcp.out.end = This->tcp.out.data + This->tcp.out.size;
63*c2c66affSColin Finck 	return &This->tcp.out;
64*c2c66affSColin Finck }
65*c2c66affSColin Finck 
66*c2c66affSColin Finck /* Send TCP transport data packet */
67*c2c66affSColin Finck BOOL
tcp_send(RDPCLIENT * This,STREAM s)68*c2c66affSColin Finck tcp_send(RDPCLIENT * This, STREAM s)
69*c2c66affSColin Finck {
70*c2c66affSColin Finck 	int length = (int)(s->end - s->data);
71*c2c66affSColin Finck 	int total = 0;
72*c2c66affSColin Finck 	DWORD sent;
73*c2c66affSColin Finck 
74*c2c66affSColin Finck 	OVERLAPPED overlapped;
75*c2c66affSColin Finck 	memset(&overlapped, 0, sizeof(overlapped));
76*c2c66affSColin Finck 
77*c2c66affSColin Finck 	while (total < length)
78*c2c66affSColin Finck 	{
79*c2c66affSColin Finck 		WriteFile((HANDLE)This->tcp.sock, s->data + total, length - total, NULL, &overlapped);
80*c2c66affSColin Finck 
81*c2c66affSColin Finck 		switch(WaitForSingleObjectEx((HANDLE)This->tcp.sock, INFINITE, TRUE))
82*c2c66affSColin Finck 		{
83*c2c66affSColin Finck 			/* Success */
84*c2c66affSColin Finck 		case WAIT_OBJECT_0:
85*c2c66affSColin Finck 			break;
86*c2c66affSColin Finck 
87*c2c66affSColin Finck 			/* Timeout or error */
88*c2c66affSColin Finck 		case WAIT_TIMEOUT:
89*c2c66affSColin Finck 		default:
90*c2c66affSColin Finck 			This->disconnect_reason = 772;
91*c2c66affSColin Finck 
92*c2c66affSColin Finck 			/* Aborted, must disconnect ASAP */
93*c2c66affSColin Finck 		case WAIT_IO_COMPLETION:
94*c2c66affSColin Finck 			CancelIo((HANDLE)This->tcp.sock);
95*c2c66affSColin Finck 			break;
96*c2c66affSColin Finck 		}
97*c2c66affSColin Finck 
98*c2c66affSColin Finck 		/* Wait for completion. We could hang here, but we shouldn't */
99*c2c66affSColin Finck 		if(!GetOverlappedResult((HANDLE)This->tcp.sock, &overlapped, &sent, TRUE))
100*c2c66affSColin Finck 			return False;
101*c2c66affSColin Finck 
102*c2c66affSColin Finck 		total += sent;
103*c2c66affSColin Finck 	}
104*c2c66affSColin Finck 
105*c2c66affSColin Finck 	return True;
106*c2c66affSColin Finck }
107*c2c66affSColin Finck 
108*c2c66affSColin Finck /* Receive a message on the TCP layer */
109*c2c66affSColin Finck STREAM
tcp_recv(RDPCLIENT * This,STREAM s,uint32 length)110*c2c66affSColin Finck tcp_recv(RDPCLIENT * This, STREAM s, uint32 length)
111*c2c66affSColin Finck {
112*c2c66affSColin Finck 	unsigned int new_length, end_offset, p_offset;
113*c2c66affSColin Finck 	DWORD rcvd = 0;
114*c2c66affSColin Finck 
115*c2c66affSColin Finck 	if (s == NULL)
116*c2c66affSColin Finck 	{
117*c2c66affSColin Finck 		/* read into "new" stream */
118*c2c66affSColin Finck 		if (length > This->tcp.in.size)
119*c2c66affSColin Finck 		{
120*c2c66affSColin Finck 			void * p = realloc(This->tcp.in.data, length);
121*c2c66affSColin Finck 
122*c2c66affSColin Finck 			if(p == NULL)
123*c2c66affSColin Finck 			{
124*c2c66affSColin Finck 				This->disconnect_reason = 262;
125*c2c66affSColin Finck 				return NULL;
126*c2c66affSColin Finck 			}
127*c2c66affSColin Finck 
128*c2c66affSColin Finck 			This->tcp.in.data = (uint8 *) p;
129*c2c66affSColin Finck 			This->tcp.in.size = length;
130*c2c66affSColin Finck 		}
131*c2c66affSColin Finck 		This->tcp.in.end = This->tcp.in.p = This->tcp.in.data;
132*c2c66affSColin Finck 		s = &This->tcp.in;
133*c2c66affSColin Finck 	}
134*c2c66affSColin Finck 	else
135*c2c66affSColin Finck 	{
136*c2c66affSColin Finck 		/* append to existing stream */
137*c2c66affSColin Finck 		new_length = (unsigned int)(s->end - s->data) + length;
138*c2c66affSColin Finck 		if (new_length > s->size)
139*c2c66affSColin Finck 		{
140*c2c66affSColin Finck 			void * p = realloc(s->data, new_length);
141*c2c66affSColin Finck 
142*c2c66affSColin Finck 			if(p == NULL)
143*c2c66affSColin Finck 			{
144*c2c66affSColin Finck 				This->disconnect_reason = 262;
145*c2c66affSColin Finck 				return NULL;
146*c2c66affSColin Finck 			}
147*c2c66affSColin Finck 
148*c2c66affSColin Finck 			p_offset = (unsigned int)(s->p - s->data);
149*c2c66affSColin Finck 			end_offset = (unsigned int)(s->end - s->data);
150*c2c66affSColin Finck 			s->data = (uint8 *) p;
151*c2c66affSColin Finck 			s->size = new_length;
152*c2c66affSColin Finck 			s->p = s->data + p_offset;
153*c2c66affSColin Finck 			s->end = s->data + end_offset;
154*c2c66affSColin Finck 		}
155*c2c66affSColin Finck 	}
156*c2c66affSColin Finck 
157*c2c66affSColin Finck 	while (length > 0)
158*c2c66affSColin Finck 	{
159*c2c66affSColin Finck 		OVERLAPPED overlapped;
160*c2c66affSColin Finck 		memset(&overlapped, 0, sizeof(overlapped));
161*c2c66affSColin Finck 
162*c2c66affSColin Finck 		if (!ui_select(This, This->tcp.sock))
163*c2c66affSColin Finck 			/* User quit */
164*c2c66affSColin Finck 			return NULL;
165*c2c66affSColin Finck 
166*c2c66affSColin Finck 		ReadFile((HANDLE)This->tcp.sock, s->end, length, NULL, &overlapped);
167*c2c66affSColin Finck 
168*c2c66affSColin Finck 		switch(WaitForSingleObjectEx((HANDLE)This->tcp.sock, INFINITE, TRUE))
169*c2c66affSColin Finck 		{
170*c2c66affSColin Finck 			/* Success */
171*c2c66affSColin Finck 		case WAIT_OBJECT_0:
172*c2c66affSColin Finck 			break;
173*c2c66affSColin Finck 
174*c2c66affSColin Finck 			/* Timeout or error */
175*c2c66affSColin Finck 		case WAIT_TIMEOUT:
176*c2c66affSColin Finck 		default:
177*c2c66affSColin Finck 			This->disconnect_reason = 1028;
178*c2c66affSColin Finck 
179*c2c66affSColin Finck 			/* Aborted, must disconnect ASAP */
180*c2c66affSColin Finck 		case WAIT_IO_COMPLETION:
181*c2c66affSColin Finck 			CancelIo((HANDLE)This->tcp.sock);
182*c2c66affSColin Finck 			break;
183*c2c66affSColin Finck 		}
184*c2c66affSColin Finck 
185*c2c66affSColin Finck 		/* Wait for completion. We could hang here, but we shouldn't */
186*c2c66affSColin Finck 		if(!GetOverlappedResult((HANDLE)This->tcp.sock, &overlapped, &rcvd, TRUE))
187*c2c66affSColin Finck 			return False;
188*c2c66affSColin Finck 
189*c2c66affSColin Finck 		if (rcvd == 0)
190*c2c66affSColin Finck 		{
191*c2c66affSColin Finck 			error("Connection closed\n");
192*c2c66affSColin Finck 			This->disconnect_reason = 2308;
193*c2c66affSColin Finck 			return NULL;
194*c2c66affSColin Finck 		}
195*c2c66affSColin Finck 
196*c2c66affSColin Finck 		s->end += rcvd;
197*c2c66affSColin Finck 		length -= rcvd;
198*c2c66affSColin Finck 	}
199*c2c66affSColin Finck 
200*c2c66affSColin Finck 	return s;
201*c2c66affSColin Finck }
202*c2c66affSColin Finck 
203*c2c66affSColin Finck /* Establish a connection on the TCP layer */
204*c2c66affSColin Finck BOOL
tcp_connect(RDPCLIENT * This,char * server)205*c2c66affSColin Finck tcp_connect(RDPCLIENT * This, char *server)
206*c2c66affSColin Finck {
207*c2c66affSColin Finck 	int true_value = 1;
208*c2c66affSColin Finck 
209*c2c66affSColin Finck #ifdef IPv6
210*c2c66affSColin Finck 
211*c2c66affSColin Finck 	int n;
212*c2c66affSColin Finck 	struct addrinfo hints, *res, *ressave;
213*c2c66affSColin Finck 	char tcp_port_rdp_s[10];
214*c2c66affSColin Finck 
215*c2c66affSColin Finck 	snprintf(tcp_port_rdp_s, 10, "%d", This->tcp_port_rdp);
216*c2c66affSColin Finck 
217*c2c66affSColin Finck 	memset(&hints, 0, sizeof(struct addrinfo));
218*c2c66affSColin Finck 	hints.ai_family = AF_UNSPEC;
219*c2c66affSColin Finck 	hints.ai_socktype = SOCK_STREAM;
220*c2c66affSColin Finck 
221*c2c66affSColin Finck 	if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
222*c2c66affSColin Finck 	{
223*c2c66affSColin Finck 		error("getaddrinfo: %s\n", gai_strerror(n));
224*c2c66affSColin Finck 		return False;
225*c2c66affSColin Finck 	}
226*c2c66affSColin Finck 
227*c2c66affSColin Finck 	ressave = res;
228*c2c66affSColin Finck 	This->tcp.sock = -1;
229*c2c66affSColin Finck 	while (res)
230*c2c66affSColin Finck 	{
231*c2c66affSColin Finck 		This->tcp.sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
232*c2c66affSColin Finck 		if (!(This->tcp.sock < 0))
233*c2c66affSColin Finck 		{
234*c2c66affSColin Finck 			if (connect(This->tcp.sock, res->ai_addr, res->ai_addrlen) == 0)
235*c2c66affSColin Finck 				break;
236*c2c66affSColin Finck 			close(This->tcp.sock);
237*c2c66affSColin Finck 			This->tcp.sock = -1;
238*c2c66affSColin Finck 		}
239*c2c66affSColin Finck 		res = res->ai_next;
240*c2c66affSColin Finck 	}
241*c2c66affSColin Finck 	freeaddrinfo(ressave);
242*c2c66affSColin Finck 
243*c2c66affSColin Finck 	if (This->tcp.sock == -1)
244*c2c66affSColin Finck 	{
245*c2c66affSColin Finck 		error("%s: unable to connect\n", server);
246*c2c66affSColin Finck 		return False;
247*c2c66affSColin Finck 	}
248*c2c66affSColin Finck 
249*c2c66affSColin Finck #else /* no IPv6 support */
250*c2c66affSColin Finck 
251*c2c66affSColin Finck 	struct hostent *nslookup;
252*c2c66affSColin Finck 	struct sockaddr_in servaddr;
253*c2c66affSColin Finck 
254*c2c66affSColin Finck 	if ((nslookup = gethostbyname(server)) != NULL)
255*c2c66affSColin Finck 	{
256*c2c66affSColin Finck 		memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
257*c2c66affSColin Finck 	}
258*c2c66affSColin Finck 	else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
259*c2c66affSColin Finck 	{
260*c2c66affSColin Finck 		error("%s: unable to resolve host\n", server);
261*c2c66affSColin Finck 		This->disconnect_reason = 260;
262*c2c66affSColin Finck 		return False;
263*c2c66affSColin Finck 	}
264*c2c66affSColin Finck 
265*c2c66affSColin Finck 	if ((This->tcp.sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
266*c2c66affSColin Finck 	{
267*c2c66affSColin Finck 		// error("socket: %s\n", strerror(errno)); // EOF
268*c2c66affSColin Finck 		return False;
269*c2c66affSColin Finck 	}
270*c2c66affSColin Finck 
271*c2c66affSColin Finck 	servaddr.sin_family = AF_INET;
272*c2c66affSColin Finck 	servaddr.sin_port = htons(This->tcp_port_rdp);
273*c2c66affSColin Finck 
274*c2c66affSColin Finck 	// TODO: apply connection timeout here
275*c2c66affSColin Finck 
276*c2c66affSColin Finck 	if (connect(This->tcp.sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
277*c2c66affSColin Finck 	{
278*c2c66affSColin Finck 		// error("connect: %s\n", strerror(errno)); // EOF
279*c2c66affSColin Finck 		This->disconnect_reason = 516;
280*c2c66affSColin Finck 		closesocket(This->tcp.sock);
281*c2c66affSColin Finck 		return False;
282*c2c66affSColin Finck 	}
283*c2c66affSColin Finck 
284*c2c66affSColin Finck #endif /* IPv6 */
285*c2c66affSColin Finck 
286*c2c66affSColin Finck 	setsockopt(This->tcp.sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value));
287*c2c66affSColin Finck 
288*c2c66affSColin Finck 	This->tcp.in.size = 4096;
289*c2c66affSColin Finck 	This->tcp.in.data = (uint8 *) malloc(This->tcp.in.size);
290*c2c66affSColin Finck 
291*c2c66affSColin Finck 	if(This->tcp.in.data == NULL)
292*c2c66affSColin Finck 	{
293*c2c66affSColin Finck 		This->disconnect_reason = 262;
294*c2c66affSColin Finck 		return False;
295*c2c66affSColin Finck 	}
296*c2c66affSColin Finck 
297*c2c66affSColin Finck 	This->tcp.out.size = 4096;
298*c2c66affSColin Finck 	This->tcp.out.data = (uint8 *) malloc(This->tcp.out.size);
299*c2c66affSColin Finck 
300*c2c66affSColin Finck 	if(This->tcp.out.data == NULL)
301*c2c66affSColin Finck 	{
302*c2c66affSColin Finck 		This->disconnect_reason = 262;
303*c2c66affSColin Finck 		return False;
304*c2c66affSColin Finck 	}
305*c2c66affSColin Finck 
306*c2c66affSColin Finck 	return True;
307*c2c66affSColin Finck }
308*c2c66affSColin Finck 
309*c2c66affSColin Finck /* Disconnect on the TCP layer */
310*c2c66affSColin Finck BOOL
tcp_disconnect(RDPCLIENT * This)311*c2c66affSColin Finck tcp_disconnect(RDPCLIENT * This)
312*c2c66affSColin Finck {
313*c2c66affSColin Finck 	closesocket(This->tcp.sock);
314*c2c66affSColin Finck 	return True;
315*c2c66affSColin Finck }
316*c2c66affSColin Finck 
317*c2c66affSColin Finck wchar_t *
tcp_get_address(RDPCLIENT * This)318*c2c66affSColin Finck tcp_get_address(RDPCLIENT * This)
319*c2c66affSColin Finck {
320*c2c66affSColin Finck #if 0
321*c2c66affSColin Finck 	static char ipaddr[32];
322*c2c66affSColin Finck 	struct sockaddr_in sockaddr;
323*c2c66affSColin Finck 	socklen_t len = sizeof(sockaddr);
324*c2c66affSColin Finck 	if (getsockname(This->tcp.sock, (struct sockaddr *) &sockaddr, &len) == 0)
325*c2c66affSColin Finck 	{
326*c2c66affSColin Finck 		unsigned char *ip = (unsigned char *) &sockaddr.sin_addr;
327*c2c66affSColin Finck 		sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
328*c2c66affSColin Finck 	}
329*c2c66affSColin Finck 	else
330*c2c66affSColin Finck 		strcpy(ipaddr, "127.0.0.1");
331*c2c66affSColin Finck 	return ipaddr;
332*c2c66affSColin Finck #endif
333*c2c66affSColin Finck 	return NULL; // TODO
334*c2c66affSColin Finck }
335*c2c66affSColin Finck 
336*c2c66affSColin Finck /* reset the state of the tcp layer */
337*c2c66affSColin Finck /* Support for Session Directory */
338*c2c66affSColin Finck void
tcp_reset_state(RDPCLIENT * This)339*c2c66affSColin Finck tcp_reset_state(RDPCLIENT * This)
340*c2c66affSColin Finck {
341*c2c66affSColin Finck 	This->tcp.sock = -1;		/* reset socket */
342*c2c66affSColin Finck 
343*c2c66affSColin Finck 	/* Clear the incoming stream */
344*c2c66affSColin Finck 	if (This->tcp.in.data != NULL)
345*c2c66affSColin Finck 		free(This->tcp.in.data);
346*c2c66affSColin Finck 	This->tcp.in.p = NULL;
347*c2c66affSColin Finck 	This->tcp.in.end = NULL;
348*c2c66affSColin Finck 	This->tcp.in.data = NULL;
349*c2c66affSColin Finck 	This->tcp.in.size = 0;
350*c2c66affSColin Finck 	This->tcp.in.iso_hdr = NULL;
351*c2c66affSColin Finck 	This->tcp.in.mcs_hdr = NULL;
352*c2c66affSColin Finck 	This->tcp.in.sec_hdr = NULL;
353*c2c66affSColin Finck 	This->tcp.in.rdp_hdr = NULL;
354*c2c66affSColin Finck 	This->tcp.in.channel_hdr = NULL;
355*c2c66affSColin Finck 
356*c2c66affSColin Finck 	/* Clear the outgoing stream */
357*c2c66affSColin Finck 	if (This->tcp.out.data != NULL)
358*c2c66affSColin Finck 		free(This->tcp.out.data);
359*c2c66affSColin Finck 	This->tcp.out.p = NULL;
360*c2c66affSColin Finck 	This->tcp.out.end = NULL;
361*c2c66affSColin Finck 	This->tcp.out.data = NULL;
362*c2c66affSColin Finck 	This->tcp.out.size = 0;
363*c2c66affSColin Finck 	This->tcp.out.iso_hdr = NULL;
364*c2c66affSColin Finck 	This->tcp.out.mcs_hdr = NULL;
365*c2c66affSColin Finck 	This->tcp.out.sec_hdr = NULL;
366*c2c66affSColin Finck 	This->tcp.out.rdp_hdr = NULL;
367*c2c66affSColin Finck 	This->tcp.out.channel_hdr = NULL;
368*c2c66affSColin Finck }
369