VirtualBox

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

最後變更 在這個檔案從55121是 55121,由 vboxsync 提交於 10 年 前

rdesktop 1.8.3 unmodified

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

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