VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.3/tcp.c@ 58141

Last change on this file since 58141 was 55123, checked in by vboxsync, 10 years ago

rdesktop 1.8.3 modified for VBox

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - TCP layer
4 Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
5 Copyright 2005-2011 Peter Astrand <[email protected]> for Cendio AB
6 Copyright 2012-2013 Henrik Andersson <[email protected]> for Cendio AB
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/*
23 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
24 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
25 * the General Public License version 2 (GPLv2) at this time for any software where
26 * a choice of GPL license versions is made available with the language indicating
27 * that GPLv2 or any later version may be used, or where a choice of which version
28 * of the GPL is applied is otherwise unspecified.
29 */
30
31#ifndef _WIN32
32#include <unistd.h> /* select read write close */
33#include <sys/socket.h> /* socket connect setsockopt */
34#include <sys/time.h> /* timeval */
35#include <netdb.h> /* gethostbyname */
36#include <netinet/in.h> /* sockaddr_in */
37#include <netinet/tcp.h> /* TCP_NODELAY */
38#include <arpa/inet.h> /* inet_addr */
39#include <errno.h> /* errno */
40#endif
41
42#include <openssl/ssl.h>
43#include <openssl/x509.h>
44#include <openssl/err.h>
45
46#include "rdesktop.h"
47
48#ifdef _WIN32
49#define socklen_t int
50#define TCP_CLOSE(_sck) closesocket(_sck)
51#define TCP_STRERROR "tcp error"
52#define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
53#else
54#define TCP_CLOSE(_sck) close(_sck)
55#define TCP_STRERROR strerror(errno)
56#define TCP_BLOCKS (errno == EWOULDBLOCK)
57#endif
58
59#ifndef INADDR_NONE
60#define INADDR_NONE ((unsigned long) -1)
61#endif
62
63#ifdef WITH_SCARD
64#define STREAM_COUNT 8
65#else
66#define STREAM_COUNT 1
67#endif
68
69static RD_BOOL g_ssl_initialized = False;
70static SSL *g_ssl = NULL;
71static SSL_CTX *g_ssl_ctx = NULL;
72static int g_sock;
73static RD_BOOL g_run_ui = False;
74static struct stream g_in;
75static struct stream g_out[STREAM_COUNT];
76int g_tcp_port_rdp = TCP_PORT_RDP;
77extern RD_BOOL g_user_quit;
78extern RD_BOOL g_network_error;
79extern RD_BOOL g_reconnect_loop;
80
81/* wait till socket is ready to write or timeout */
82static RD_BOOL
83tcp_can_send(int sck, int millis)
84{
85 fd_set wfds;
86 struct timeval time;
87 int sel_count;
88
89 time.tv_sec = millis / 1000;
90 time.tv_usec = (millis * 1000) % 1000000;
91 FD_ZERO(&wfds);
92 FD_SET(sck, &wfds);
93 sel_count = select(sck + 1, 0, &wfds, 0, &time);
94 if (sel_count > 0)
95 {
96 return True;
97 }
98 return False;
99}
100
101/* Initialise TCP transport data packet */
102STREAM
103tcp_init(uint32 maxlen)
104{
105 static int cur_stream_id = 0;
106 STREAM result = NULL;
107
108#ifdef WITH_SCARD
109 scard_lock(SCARD_LOCK_TCP);
110#endif
111 result = &g_out[cur_stream_id];
112 cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
113
114 if (maxlen > result->size)
115 {
116 result->data = (uint8 *) xrealloc(result->data, maxlen);
117 result->size = maxlen;
118 }
119
120 result->p = result->data;
121 result->end = result->data + result->size;
122#ifdef WITH_SCARD
123 scard_unlock(SCARD_LOCK_TCP);
124#endif
125 return result;
126}
127
128/* Send TCP transport data packet */
129void
130tcp_send(STREAM s)
131{
132 int ssl_err;
133 int length = s->end - s->data;
134 int sent, total = 0;
135
136 if (g_network_error == True)
137 return;
138
139#ifdef WITH_SCARD
140 scard_lock(SCARD_LOCK_TCP);
141#endif
142 while (total < length)
143 {
144 if (g_ssl)
145 {
146 sent = SSL_write(g_ssl, s->data + total, length - total);
147 if (sent <= 0)
148 {
149 ssl_err = SSL_get_error(g_ssl, sent);
150 if (sent < 0 && (ssl_err == SSL_ERROR_WANT_READ ||
151 ssl_err == SSL_ERROR_WANT_WRITE))
152 {
153 tcp_can_send(g_sock, 100);
154 sent = 0;
155 }
156 else
157 {
158#ifdef WITH_SCARD
159 scard_unlock(SCARD_LOCK_TCP);
160#endif
161
162 error("SSL_write: %d (%s)\n", ssl_err, TCP_STRERROR);
163 g_network_error = True;
164 return;
165 }
166 }
167 }
168 else
169 {
170 sent = send(g_sock, s->data + total, length - total, 0);
171 if (sent <= 0)
172 {
173 if (sent == -1 && TCP_BLOCKS)
174 {
175 tcp_can_send(g_sock, 100);
176 sent = 0;
177 }
178 else
179 {
180#ifdef WITH_SCARD
181 scard_unlock(SCARD_LOCK_TCP);
182#endif
183
184 error("send: %s\n", TCP_STRERROR);
185 g_network_error = True;
186 return;
187 }
188 }
189 }
190 total += sent;
191 }
192#ifdef WITH_SCARD
193 scard_unlock(SCARD_LOCK_TCP);
194#endif
195}
196
197/* Receive a message on the TCP layer */
198STREAM
199tcp_recv(STREAM s, uint32 length)
200{
201 uint32 new_length, end_offset, p_offset;
202 int rcvd = 0, ssl_err;
203
204 if (g_network_error == True)
205 return NULL;
206
207 if (s == NULL)
208 {
209 /* read into "new" stream */
210 if (length > g_in.size)
211 {
212 g_in.data = (uint8 *) xrealloc(g_in.data, length);
213 g_in.size = length;
214 }
215 g_in.end = g_in.p = g_in.data;
216 s = &g_in;
217 }
218 else
219 {
220 /* append to existing stream */
221 new_length = (s->end - s->data) + length;
222 if (new_length > s->size)
223 {
224 p_offset = s->p - s->data;
225 end_offset = s->end - s->data;
226 s->data = (uint8 *) xrealloc(s->data, new_length);
227 s->size = new_length;
228 s->p = s->data + p_offset;
229 s->end = s->data + end_offset;
230 }
231 }
232
233 while (length > 0)
234 {
235 if ((!g_ssl || SSL_pending(g_ssl) <= 0) && g_run_ui)
236 {
237 if (!ui_select(g_sock))
238 {
239 /* User quit */
240 g_user_quit = True;
241 return NULL;
242 }
243 }
244
245 if (g_ssl)
246 {
247 rcvd = SSL_read(g_ssl, s->end, length);
248 ssl_err = SSL_get_error(g_ssl, rcvd);
249
250 if (ssl_err == SSL_ERROR_SSL)
251 {
252 if (SSL_get_shutdown(g_ssl) & SSL_RECEIVED_SHUTDOWN)
253 {
254 error("Remote peer initiated ssl shutdown.\n");
255 return NULL;
256 }
257
258 ERR_print_errors_fp(stdout);
259 g_network_error = True;
260 return NULL;
261 }
262
263 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE)
264 {
265 rcvd = 0;
266 }
267 else if (ssl_err != SSL_ERROR_NONE)
268 {
269 error("SSL_read: %d (%s)\n", ssl_err, TCP_STRERROR);
270 g_network_error = True;
271 return NULL;
272 }
273
274 }
275 else
276 {
277 rcvd = recv(g_sock, s->end, length, 0);
278 if (rcvd < 0)
279 {
280 if (rcvd == -1 && TCP_BLOCKS)
281 {
282 rcvd = 0;
283 }
284 else
285 {
286 error("recv: %s\n", TCP_STRERROR);
287 g_network_error = True;
288 return NULL;
289 }
290 }
291 else if (rcvd == 0)
292 {
293 error("Connection closed\n");
294 return NULL;
295 }
296 }
297
298 s->end += rcvd;
299 length -= rcvd;
300 }
301
302 return s;
303}
304
305/* Establish a SSL/TLS 1.0 connection */
306RD_BOOL
307tcp_tls_connect(void)
308{
309 int err;
310 long options;
311
312 if (!g_ssl_initialized)
313 {
314 SSL_load_error_strings();
315 SSL_library_init();
316 g_ssl_initialized = True;
317 }
318
319 /* create process context */
320 if (g_ssl_ctx == NULL)
321 {
322 g_ssl_ctx = SSL_CTX_new(TLSv1_client_method());
323 if (g_ssl_ctx == NULL)
324 {
325 error("tcp_tls_connect: SSL_CTX_new() failed to create TLS v1.0 context\n");
326 goto fail;
327 }
328
329 options = 0;
330#ifdef SSL_OP_NO_COMPRESSION
331 options |= SSL_OP_NO_COMPRESSION;
332#endif // __SSL_OP_NO_COMPRESSION
333 options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
334 SSL_CTX_set_options(g_ssl_ctx, options);
335 }
336
337 /* free old connection */
338 if (g_ssl)
339 SSL_free(g_ssl);
340
341 /* create new ssl connection */
342 g_ssl = SSL_new(g_ssl_ctx);
343 if (g_ssl == NULL)
344 {
345 error("tcp_tls_connect: SSL_new() failed\n");
346 goto fail;
347 }
348
349 if (SSL_set_fd(g_ssl, g_sock) < 1)
350 {
351 error("tcp_tls_connect: SSL_set_fd() failed\n");
352 goto fail;
353 }
354
355 do
356 {
357 err = SSL_connect(g_ssl);
358 }
359 while (SSL_get_error(g_ssl, err) == SSL_ERROR_WANT_READ);
360
361 if (err < 0)
362 {
363 ERR_print_errors_fp(stdout);
364 goto fail;
365 }
366
367 return True;
368
369 fail:
370 if (g_ssl)
371 SSL_free(g_ssl);
372 if (g_ssl_ctx)
373 SSL_CTX_free(g_ssl_ctx);
374
375 g_ssl = NULL;
376 g_ssl_ctx = NULL;
377 return False;
378}
379
380/* Get public key from server of TLS 1.0 connection */
381RD_BOOL
382tcp_tls_get_server_pubkey(STREAM s)
383{
384 X509 *cert = NULL;
385 EVP_PKEY *pkey = NULL;
386
387 s->data = s->p = NULL;
388 s->size = 0;
389
390 if (g_ssl == NULL)
391 goto out;
392
393 cert = SSL_get_peer_certificate(g_ssl);
394 if (cert == NULL)
395 {
396 error("tcp_tls_get_server_pubkey: SSL_get_peer_certificate() failed\n");
397 goto out;
398 }
399
400 pkey = X509_get_pubkey(cert);
401 if (pkey == NULL)
402 {
403 error("tcp_tls_get_server_pubkey: X509_get_pubkey() failed\n");
404 goto out;
405 }
406
407 s->size = i2d_PublicKey(pkey, NULL);
408 if (s->size < 1)
409 {
410 error("tcp_tls_get_server_pubkey: i2d_PublicKey() failed\n");
411 goto out;
412 }
413
414 s->data = s->p = xmalloc(s->size);
415 i2d_PublicKey(pkey, &s->p);
416 s->p = s->data;
417 s->end = s->p + s->size;
418
419 out:
420 if (cert)
421 X509_free(cert);
422 if (pkey)
423 EVP_PKEY_free(pkey);
424 return (s->size != 0);
425}
426
427/* Establish a connection on the TCP layer */
428RD_BOOL
429tcp_connect(char *server)
430{
431 socklen_t option_len;
432 uint32 option_value;
433 int i;
434
435#ifdef IPv6
436
437 int n;
438 struct addrinfo hints, *res, *ressave;
439 char tcp_port_rdp_s[10];
440
441 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
442
443 memset(&hints, 0, sizeof(struct addrinfo));
444 hints.ai_family = AF_UNSPEC;
445 hints.ai_socktype = SOCK_STREAM;
446
447 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
448 {
449 error("getaddrinfo: %s\n", gai_strerror(n));
450 return False;
451 }
452
453 ressave = res;
454 g_sock = -1;
455 while (res)
456 {
457 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
458 if (!(g_sock < 0))
459 {
460 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
461 break;
462 TCP_CLOSE(g_sock);
463 g_sock = -1;
464 }
465 res = res->ai_next;
466 }
467 freeaddrinfo(ressave);
468
469 if (g_sock == -1)
470 {
471 error("%s: unable to connect\n", server);
472 return False;
473 }
474
475#else /* no IPv6 support */
476
477 struct hostent *nslookup;
478 struct sockaddr_in servaddr;
479
480 if ((nslookup = gethostbyname(server)) != NULL)
481 {
482 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
483 }
484 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
485 {
486 error("%s: unable to resolve host\n", server);
487 return False;
488 }
489
490 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
491 {
492 error("socket: %s\n", TCP_STRERROR);
493 return False;
494 }
495
496 servaddr.sin_family = AF_INET;
497 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
498
499 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
500 {
501 if (!g_reconnect_loop)
502 error("connect: %s\n", TCP_STRERROR);
503
504 TCP_CLOSE(g_sock);
505 g_sock = -1;
506 return False;
507 }
508
509#endif /* IPv6 */
510
511 option_value = 1;
512 option_len = sizeof(option_value);
513 setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
514 /* receive buffer must be a least 16 K */
515 if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
516 {
517 if (option_value < (1024 * 16))
518 {
519 option_value = 1024 * 16;
520 option_len = sizeof(option_value);
521 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
522 option_len);
523 }
524 }
525
526 g_in.size = 4096;
527 g_in.data = (uint8 *) xmalloc(g_in.size);
528
529 for (i = 0; i < STREAM_COUNT; i++)
530 {
531 g_out[i].size = 4096;
532 g_out[i].data = (uint8 *) xmalloc(g_out[i].size);
533 }
534
535 return True;
536}
537
538/* Disconnect on the TCP layer */
539void
540tcp_disconnect(void)
541{
542 if (g_ssl)
543 {
544 if (!g_network_error)
545 (void) SSL_shutdown(g_ssl);
546 SSL_free(g_ssl);
547 g_ssl = NULL;
548 SSL_CTX_free(g_ssl_ctx);
549 g_ssl_ctx = NULL;
550 }
551
552 TCP_CLOSE(g_sock);
553 g_sock = -1;
554}
555
556char *
557tcp_get_address()
558{
559 static char ipaddr[32];
560 struct sockaddr_in sockaddr;
561 socklen_t len = sizeof(sockaddr);
562 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
563 {
564 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
565 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
566 }
567 else
568 strcpy(ipaddr, "127.0.0.1");
569 return ipaddr;
570}
571
572RD_BOOL
573tcp_is_connected()
574{
575 struct sockaddr_in sockaddr;
576 socklen_t len = sizeof(sockaddr);
577 if (getpeername(g_sock, (struct sockaddr *) &sockaddr, &len))
578 return True;
579 return False;
580}
581
582/* reset the state of the tcp layer */
583/* Support for Session Directory */
584void
585tcp_reset_state(void)
586{
587 int i;
588
589 /* Clear the incoming stream */
590 if (g_in.data != NULL)
591 xfree(g_in.data);
592 g_in.p = NULL;
593 g_in.end = NULL;
594 g_in.data = NULL;
595 g_in.size = 0;
596 g_in.iso_hdr = NULL;
597 g_in.mcs_hdr = NULL;
598 g_in.sec_hdr = NULL;
599 g_in.rdp_hdr = NULL;
600 g_in.channel_hdr = NULL;
601
602 /* Clear the outgoing stream(s) */
603 for (i = 0; i < STREAM_COUNT; i++)
604 {
605 if (g_out[i].data != NULL)
606 xfree(g_out[i].data);
607 g_out[i].p = NULL;
608 g_out[i].end = NULL;
609 g_out[i].data = NULL;
610 g_out[i].size = 0;
611 g_out[i].iso_hdr = NULL;
612 g_out[i].mcs_hdr = NULL;
613 g_out[i].sec_hdr = NULL;
614 g_out[i].rdp_hdr = NULL;
615 g_out[i].channel_hdr = NULL;
616 }
617}
618
619void
620tcp_run_ui(RD_BOOL run)
621{
622 g_run_ui = run;
623}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette