Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tcp.c « libfreerdp-core - github.com/FreeRDP/FreeRDP-old.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5bfbb27239e561723cb371ba3a554b09d3d3a41b (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
/*
   FreeRDP: A Remote Desktop Protocol client.
   TCP Layer

   Copyright (C) Matthew Chapman 1999-2008

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#ifndef _WIN32
#include <unistd.h>		/* select read write close */
#include <sys/socket.h>		/* socket connect setsockopt */
#include <sys/time.h>		/* timeval */
#include <netdb.h>		/* gethostbyname */
#include <netinet/in.h>		/* sockaddr_in */
#include <netinet/tcp.h>	/* TCP_NODELAY */
#include <arpa/inet.h>		/* inet_addr */
#include <errno.h>		/* errno */
#include <fcntl.h>		/* fcntl F_GETFL F_SETFL O_NONBLOCK */
#endif

#include "frdp.h"
#include "iso.h"
#include "mcs.h"
#include "rdp.h"
#include <freerdp/utils/memory.h>
#include <freerdp/utils/hexdump.h>

#include "tcp.h"

#ifdef _WIN32
#define socklen_t int
#define TCP_CLOSE(_sck) closesocket(_sck)
#define TCP_STRERROR "tcp error"
#define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
#define MSG_NOSIGNAL 0
#else
#define TCP_CLOSE(_sck) close(_sck)
#define TCP_STRERROR strerror(errno)
#define TCP_BLOCKS (errno == EWOULDBLOCK)
#endif

#ifdef __APPLE__
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif

#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned long) -1)
#endif

/*****************************************************************************/
/* returns boolean */
static RD_BOOL
tcp_socket_ok(int sck)
{
#if defined(_WIN32)
	int opt;
	int opt_len;
#else
	int opt;
	unsigned int opt_len;
#endif

	opt_len = sizeof(opt);
	if (getsockopt(sck, SOL_SOCKET, SO_ERROR, (char *) (&opt), &opt_len) == 0)
	{
		if (opt == 0)
		{
			return True;
		}
	}

	return False;
}

/* wait till socket is ready to write or timeout */
RD_BOOL
tcp_can_send(int sck, int millis)
{
	fd_set wfds;
	struct timeval time;
	int sel_count;

	time.tv_sec = millis / 1000;
	time.tv_usec = (millis * 1000) % 1000000;

	FD_ZERO(&wfds);
	FD_SET(sck, &wfds);
	sel_count = select(sck + 1, 0, &wfds, 0, &time);

	if (sel_count > 0)
	{
		return tcp_socket_ok(sck);
	}

	return False;
}

/* wait till socket is ready to read or timeout */
RD_BOOL
tcp_can_recv(int sck, int millis)
{
	fd_set rfds;
	struct timeval time;
	int sel_count;

	time.tv_sec = millis / 1000;
	time.tv_usec = (millis * 1000) % 1000000;

	FD_ZERO(&rfds);
	FD_SET(sck, &rfds);
	sel_count = select(sck + 1, &rfds, 0, 0, &time);

	if (sel_count > 0)
	{
		return tcp_socket_ok(sck);
	}

	return False;
}

void
tcp_write(rdpTcp * tcp, char* b, int length)
{
	int sent = 0;
	int total = 0;

	while (total < length)
	{
		while (total < length)
		{
			sent = send(tcp->sockfd, b + total, length - total, MSG_NOSIGNAL);
			if (sent <= 0)
			{
				if (sent == -1 && TCP_BLOCKS)
				{
					tcp_can_send(tcp->sockfd, 100);
					sent = 0;
				}
				else
				{
					ui_error(tcp->net->rdp->inst, "send: %s\n", TCP_STRERROR);
					return;
				}
			}
			total += sent;
		}
	}
}

int
tcp_read(rdpTcp * tcp, char* b, int length)
{
	int rcvd = 0;

	if (!ui_select(tcp->net->sec->rdp->inst, tcp->sockfd))
		return -1; /* user quit */

	rcvd = recv(tcp->sockfd, b, length, 0);

	if (rcvd < 0)
	{
		if (rcvd == -1 && TCP_BLOCKS)
		{
			tcp_can_recv(tcp->sockfd, 1);
			rcvd = 0;
		}
		else
		{
			ui_error(tcp->net->rdp->inst, "recv: %s\n", TCP_STRERROR);
			return -1;
		}
	}
	else if (rcvd == 0)
	{
		ui_error(tcp->net->rdp->inst, "Connection closed\n");
			return -1;
	}

	return rcvd;
}

/* Establish a connection on the TCP layer */
RD_BOOL
tcp_connect(rdpTcp * tcp, char * server, int port)
{
	int sockfd;
	uint32 option_value;
	socklen_t option_len;

#ifdef IPv6

	int n;
	struct addrinfo hints, *res, *ressave;
	char tcp_port_rdp_s[10];

	printf("connecting to %s:%d\n", server, port);

	snprintf(tcp_port_rdp_s, 10, "%d", port);

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
	{
		ui_error(tcp->net->rdp->inst, "getaddrinfo: %s\n", gai_strerror(n));
		return False;
	}

	ressave = res;
	sockfd = -1;
	while (res)
	{
		sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (!(sockfd < 0))
		{
			if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
				break;
			TCP_CLOSE(sockfd);
			sockfd = -1;
		}
		res = res->ai_next;
	}
	freeaddrinfo(ressave);

	if (sockfd == -1)
	{
		ui_error(tcp->net->rdp->inst, "%s: unable to connect\n", server);
		return False;
	}

#else /* no IPv6 support */

	struct hostent *nslookup;
	struct sockaddr_in servaddr;

	printf("connecting to %s:%d\n", server, port);

	if ((nslookup = gethostbyname(server)) != NULL)
	{
		memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
	}
	else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
	{
		ui_error(tcp->net->rdp->inst, "%s: unable to resolve host\n", server);
		return False;
	}

	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		ui_error(tcp->net->rdp->inst, "socket: %s\n", TCP_STRERROR);
		return False;
	}

	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons((uint16) port);

	if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
	{
		ui_error(tcp->net->rdp->inst, "connect: %s\n", TCP_STRERROR);
		TCP_CLOSE(sock);
		return False;
	}

#endif /* IPv6 */

	tcp->sockfd = sockfd;

	/* set socket as non blocking */
#ifdef _WIN32
	{
		u_long arg = 1;
		ioctlsocket(tcp->sockfd, FIONBIO, &arg);
		tcp->wsa_event = WSACreateEvent();
		WSAEventSelect(tcp->sockfd, tcp->wsa_event, FD_READ);
	}
#else
	option_value = fcntl(tcp->sockfd, F_GETFL);
	option_value = option_value | O_NONBLOCK;
	fcntl(tcp->sockfd, F_SETFL, option_value);
#endif

	option_value = 1;
	option_len = sizeof(option_value);
	setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
	/* receive buffer must be a least 16 K */
	if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
	{
		if (option_value < (1024 * 16))
		{
			option_value = 1024 * 16;
			option_len = sizeof(option_value);
			setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
				   option_len);
		}
	}

	return True;
}

/* Disconnect on the TCP layer */
void
tcp_disconnect(rdpTcp * tcp)
{
	if (tcp->sockfd != -1)
	{
		TCP_CLOSE(tcp->sockfd);
		tcp->sockfd = -1;
	}
#ifdef _WIN32
	if (tcp->wsa_event)
	{
		WSACloseEvent(tcp->wsa_event);
		tcp->wsa_event = 0;
	}
#endif
}

/* Returns pointer to internal buffer with lifetime as tcp */
char *
tcp_get_address(rdpTcp * tcp)
{
	struct sockaddr_in sockaddr;
	socklen_t len = sizeof(sockaddr);
	if (getsockname(tcp->sockfd, (struct sockaddr *) &sockaddr, &len) == 0)
	{
		uint8 *ip = (uint8 *) & sockaddr.sin_addr;
		snprintf(tcp->ipaddr, sizeof(tcp->ipaddr), "%d.%d.%d.%d", ip[0], ip[1], ip[2],
			 ip[3]);
	}
	else
		strncpy(tcp->ipaddr, "127.0.0.1", sizeof(tcp->ipaddr));
	tcp->ipaddr[sizeof(tcp->ipaddr) - 1] = 0;
	return tcp->ipaddr;
}

rdpTcp *
tcp_new(struct rdp_network * net)
{
	rdpTcp * self;

	self = (rdpTcp *) xmalloc(sizeof(rdpTcp));

	if (self != NULL)
	{
		memset(self, 0, sizeof(rdpTcp));
		self->net = net;
		self->sockfd = -1;
	}

	return self;
}

void
tcp_free(rdpTcp * tcp)
{
	if (tcp != NULL)
	{
		xfree(tcp);
	}
}