VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.4/tcp.c@ 88182

最後變更 在這個檔案從88182是 63869,由 vboxsync 提交於 8 年 前

typo

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.8 KB
 
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#ifdef __GNUC__
323# if (__GNUC__ << 8) + __GNUC_MINOR__ >= 0x406
324# pragma GCC diagnostic push
325# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
326# endif
327#endif
328 g_ssl_ctx = SSL_CTX_new(TLSv1_client_method());
329#ifdef __GNUC__
330# if (__GNUC__ << 8) + __GNUC_MINOR__ >= 0x406
331# pragma GCC diagnostic pop
332# endif
333#endif
334 if (g_ssl_ctx == NULL)
335 {
336 error("tcp_tls_connect: SSL_CTX_new() failed to create TLS v1.0 context\n");
337 goto fail;
338 }
339
340 options = 0;
341#ifdef SSL_OP_NO_COMPRESSION
342 options |= SSL_OP_NO_COMPRESSION;
343#endif // __SSL_OP_NO_COMPRESSION
344 options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
345 SSL_CTX_set_options(g_ssl_ctx, options);
346 }
347
348 /* free old connection */
349 if (g_ssl)
350 SSL_free(g_ssl);
351
352 /* create new ssl connection */
353 g_ssl = SSL_new(g_ssl_ctx);
354 if (g_ssl == NULL)
355 {
356 error("tcp_tls_connect: SSL_new() failed\n");
357 goto fail;
358 }
359
360 if (SSL_set_fd(g_ssl, g_sock) < 1)
361 {
362 error("tcp_tls_connect: SSL_set_fd() failed\n");
363 goto fail;
364 }
365
366 do
367 {
368 err = SSL_connect(g_ssl);
369 }
370 while (SSL_get_error(g_ssl, err) == SSL_ERROR_WANT_READ);
371
372 if (err < 0)
373 {
374 ERR_print_errors_fp(stdout);
375 goto fail;
376 }
377
378 return True;
379
380 fail:
381 if (g_ssl)
382 SSL_free(g_ssl);
383 if (g_ssl_ctx)
384 SSL_CTX_free(g_ssl_ctx);
385
386 g_ssl = NULL;
387 g_ssl_ctx = NULL;
388 return False;
389}
390
391/* Get public key from server of TLS 1.0 connection */
392RD_BOOL
393tcp_tls_get_server_pubkey(STREAM s)
394{
395 X509 *cert = NULL;
396 EVP_PKEY *pkey = NULL;
397
398 s->data = s->p = NULL;
399 s->size = 0;
400
401 if (g_ssl == NULL)
402 goto out;
403
404 cert = SSL_get_peer_certificate(g_ssl);
405 if (cert == NULL)
406 {
407 error("tcp_tls_get_server_pubkey: SSL_get_peer_certificate() failed\n");
408 goto out;
409 }
410
411 pkey = X509_get_pubkey(cert);
412 if (pkey == NULL)
413 {
414 error("tcp_tls_get_server_pubkey: X509_get_pubkey() failed\n");
415 goto out;
416 }
417
418 s->size = i2d_PublicKey(pkey, NULL);
419 if (s->size < 1)
420 {
421 error("tcp_tls_get_server_pubkey: i2d_PublicKey() failed\n");
422 goto out;
423 }
424
425 s->data = s->p = xmalloc(s->size);
426 i2d_PublicKey(pkey, &s->p);
427 s->p = s->data;
428 s->end = s->p + s->size;
429
430 out:
431 if (cert)
432 X509_free(cert);
433 if (pkey)
434 EVP_PKEY_free(pkey);
435 return (s->size != 0);
436}
437
438/* Establish a connection on the TCP layer */
439RD_BOOL
440tcp_connect(char *server)
441{
442 socklen_t option_len;
443 uint32 option_value;
444 int i;
445
446#ifdef IPv6
447
448 int n;
449 struct addrinfo hints, *res, *ressave;
450 char tcp_port_rdp_s[10];
451
452 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
453
454 memset(&hints, 0, sizeof(struct addrinfo));
455 hints.ai_family = AF_UNSPEC;
456 hints.ai_socktype = SOCK_STREAM;
457
458 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
459 {
460 error("getaddrinfo: %s\n", gai_strerror(n));
461 return False;
462 }
463
464 ressave = res;
465 g_sock = -1;
466 while (res)
467 {
468 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
469 if (!(g_sock < 0))
470 {
471 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
472 break;
473 TCP_CLOSE(g_sock);
474 g_sock = -1;
475 }
476 res = res->ai_next;
477 }
478 freeaddrinfo(ressave);
479
480 if (g_sock == -1)
481 {
482 error("%s: unable to connect\n", server);
483 return False;
484 }
485
486#else /* no IPv6 support */
487
488 struct hostent *nslookup;
489 struct sockaddr_in servaddr;
490
491 if ((nslookup = gethostbyname(server)) != NULL)
492 {
493 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
494 }
495 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
496 {
497 error("%s: unable to resolve host\n", server);
498 return False;
499 }
500
501 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
502 {
503 error("socket: %s\n", TCP_STRERROR);
504 return False;
505 }
506
507 servaddr.sin_family = AF_INET;
508 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
509
510 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
511 {
512 if (!g_reconnect_loop)
513 error("connect: %s\n", TCP_STRERROR);
514
515 TCP_CLOSE(g_sock);
516 g_sock = -1;
517 return False;
518 }
519
520#endif /* IPv6 */
521
522 option_value = 1;
523 option_len = sizeof(option_value);
524 setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
525 /* receive buffer must be a least 16 K */
526 if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
527 {
528 if (option_value < (1024 * 16))
529 {
530 option_value = 1024 * 16;
531 option_len = sizeof(option_value);
532 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
533 option_len);
534 }
535 }
536
537 g_in.size = 4096;
538 g_in.data = (uint8 *) xmalloc(g_in.size);
539
540 for (i = 0; i < STREAM_COUNT; i++)
541 {
542 g_out[i].size = 4096;
543 g_out[i].data = (uint8 *) xmalloc(g_out[i].size);
544 }
545
546 return True;
547}
548
549/* Disconnect on the TCP layer */
550void
551tcp_disconnect(void)
552{
553 if (g_ssl)
554 {
555 if (!g_network_error)
556 (void) SSL_shutdown(g_ssl);
557 SSL_free(g_ssl);
558 g_ssl = NULL;
559 SSL_CTX_free(g_ssl_ctx);
560 g_ssl_ctx = NULL;
561 }
562
563 TCP_CLOSE(g_sock);
564 g_sock = -1;
565}
566
567char *
568tcp_get_address()
569{
570 static char ipaddr[32];
571 struct sockaddr_in sockaddr;
572 socklen_t len = sizeof(sockaddr);
573 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
574 {
575 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
576 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
577 }
578 else
579 strcpy(ipaddr, "127.0.0.1");
580 return ipaddr;
581}
582
583RD_BOOL
584tcp_is_connected()
585{
586 struct sockaddr_in sockaddr;
587 socklen_t len = sizeof(sockaddr);
588 if (getpeername(g_sock, (struct sockaddr *) &sockaddr, &len))
589 return True;
590 return False;
591}
592
593/* reset the state of the tcp layer */
594/* Support for Session Directory */
595void
596tcp_reset_state(void)
597{
598 int i;
599
600 /* Clear the incoming stream */
601 if (g_in.data != NULL)
602 xfree(g_in.data);
603 g_in.p = NULL;
604 g_in.end = NULL;
605 g_in.data = NULL;
606 g_in.size = 0;
607 g_in.iso_hdr = NULL;
608 g_in.mcs_hdr = NULL;
609 g_in.sec_hdr = NULL;
610 g_in.rdp_hdr = NULL;
611 g_in.channel_hdr = NULL;
612
613 /* Clear the outgoing stream(s) */
614 for (i = 0; i < STREAM_COUNT; i++)
615 {
616 if (g_out[i].data != NULL)
617 xfree(g_out[i].data);
618 g_out[i].p = NULL;
619 g_out[i].end = NULL;
620 g_out[i].data = NULL;
621 g_out[i].size = 0;
622 g_out[i].iso_hdr = NULL;
623 g_out[i].mcs_hdr = NULL;
624 g_out[i].sec_hdr = NULL;
625 g_out[i].rdp_hdr = NULL;
626 g_out[i].channel_hdr = NULL;
627 }
628}
629
630void
631tcp_run_ui(RD_BOOL run)
632{
633 g_run_ui = run;
634}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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