VirtualBox

source: vbox/trunk/src/VBox/RDP/client/tcp.c@ 37910

最後變更 在這個檔案從37910是 37224,由 vboxsync 提交於 14 年 前

RDP/client: fix OSE

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.3 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
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21/*
22 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
24 * the General Public License version 2 (GPLv2) at this time for any software where
25 * a choice of GPL license versions is made available with the language indicating
26 * that GPLv2 or any later version may be used, or where a choice of which version
27 * of the GPL is applied is otherwise unspecified.
28 */
29
30#ifndef _WIN32
31#include <unistd.h> /* select read write close */
32#include <sys/socket.h> /* socket connect setsockopt */
33#include <sys/time.h> /* timeval */
34#include <netdb.h> /* gethostbyname */
35#include <netinet/in.h> /* sockaddr_in */
36#include <netinet/tcp.h> /* TCP_NODELAY */
37#include <arpa/inet.h> /* inet_addr */
38#include <errno.h> /* errno */
39#endif
40
41#include "rdesktop.h"
42
43#ifdef _WIN32
44#define socklen_t int
45#define TCP_CLOSE(_sck) closesocket(_sck)
46#define TCP_STRERROR "tcp error"
47#define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
48#else
49#define TCP_CLOSE(_sck) close(_sck)
50#define TCP_STRERROR strerror(errno)
51#define TCP_BLOCKS (errno == EWOULDBLOCK)
52#endif
53
54#ifndef INADDR_NONE
55#define INADDR_NONE ((unsigned long) -1)
56#endif
57
58#ifdef WITH_SCARD
59#define STREAM_COUNT 8
60#else
61#define STREAM_COUNT 1
62#endif
63
64static int g_sock;
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;
69
70/* wait till socket is ready to write or timeout */
71static RD_BOOL
72tcp_can_send(int sck, int millis)
73{
74 fd_set wfds;
75 struct timeval time;
76 int sel_count;
77
78 time.tv_sec = millis / 1000;
79 time.tv_usec = (millis * 1000) % 1000000;
80 FD_ZERO(&wfds);
81 FD_SET(sck, &wfds);
82 sel_count = select(sck + 1, 0, &wfds, 0, &time);
83 if (sel_count > 0)
84 {
85 return True;
86 }
87 return False;
88}
89
90/* Initialise TCP transport data packet */
91STREAM
92tcp_init(uint32 maxlen)
93{
94 static int cur_stream_id = 0;
95 STREAM result = NULL;
96
97#ifdef WITH_SCARD
98 scard_lock(SCARD_LOCK_TCP);
99#endif
100 result = &g_out[cur_stream_id];
101 cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
102
103 if (maxlen > result->size)
104 {
105 result->data = (uint8 *) xrealloc(result->data, maxlen);
106 result->size = maxlen;
107 }
108
109 result->p = result->data;
110 result->end = result->data + result->size;
111#ifdef WITH_SCARD
112 scard_unlock(SCARD_LOCK_TCP);
113#endif
114 return result;
115}
116
117/* Send TCP transport data packet */
118void
119tcp_send(STREAM s)
120{
121 int length = s->end - s->data;
122 int sent, total = 0;
123
124#ifdef WITH_SCARD
125 scard_lock(SCARD_LOCK_TCP);
126#endif
127 while (total < length)
128 {
129 sent = send(g_sock, s->data + total, length - total, 0);
130 if (sent <= 0)
131 {
132 if (sent == -1 && TCP_BLOCKS)
133 {
134 tcp_can_send(g_sock, 100);
135 sent = 0;
136 }
137 else
138 {
139 error("send: %s\n", TCP_STRERROR);
140 return;
141 }
142 }
143 total += sent;
144 }
145#ifdef WITH_SCARD
146 scard_unlock(SCARD_LOCK_TCP);
147#endif
148}
149
150/* Receive a message on the TCP layer */
151STREAM
152tcp_recv(STREAM s, uint32 length)
153{
154 uint32 new_length, end_offset, p_offset;
155 int rcvd = 0;
156
157 if (s == NULL)
158 {
159 /* read into "new" stream */
160 if (length > g_in.size)
161 {
162 g_in.data = (uint8 *) xrealloc(g_in.data, length);
163 g_in.size = length;
164 }
165 g_in.end = g_in.p = g_in.data;
166 s = &g_in;
167 }
168 else
169 {
170 /* append to existing stream */
171 new_length = (s->end - s->data) + length;
172 if (new_length > s->size)
173 {
174 p_offset = s->p - s->data;
175 end_offset = s->end - s->data;
176 s->data = (uint8 *) xrealloc(s->data, new_length);
177 s->size = new_length;
178 s->p = s->data + p_offset;
179 s->end = s->data + end_offset;
180 }
181 }
182
183 while (length > 0)
184 {
185 if (!ui_select(g_sock))
186 {
187 /* User quit */
188 g_user_quit = True;
189 return NULL;
190 }
191
192 rcvd = recv(g_sock, s->end, length, 0);
193 if (rcvd < 0)
194 {
195 if (rcvd == -1 && TCP_BLOCKS)
196 {
197 rcvd = 0;
198 }
199 else
200 {
201 error("recv: %s\n", TCP_STRERROR);
202 return NULL;
203 }
204 }
205 else if (rcvd == 0)
206 {
207 error("Connection closed\n");
208 return NULL;
209 }
210
211 s->end += rcvd;
212 length -= rcvd;
213 }
214
215 return s;
216}
217
218/* Establish a connection on the TCP layer */
219RD_BOOL
220tcp_connect(char *server)
221{
222 socklen_t option_len;
223 uint32 option_value;
224 int i;
225
226#ifdef IPv6
227
228 int n;
229 struct addrinfo hints, *res, *ressave;
230 char tcp_port_rdp_s[10];
231
232 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
233
234 memset(&hints, 0, sizeof(struct addrinfo));
235 hints.ai_family = AF_UNSPEC;
236 hints.ai_socktype = SOCK_STREAM;
237
238 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
239 {
240 error("getaddrinfo: %s\n", gai_strerror(n));
241 return False;
242 }
243
244 ressave = res;
245 g_sock = -1;
246 while (res)
247 {
248 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
249 if (!(g_sock < 0))
250 {
251 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
252 break;
253 TCP_CLOSE(g_sock);
254 g_sock = -1;
255 }
256 res = res->ai_next;
257 }
258 freeaddrinfo(ressave);
259
260 if (g_sock == -1)
261 {
262 error("%s: unable to connect\n", server);
263 return False;
264 }
265
266#else /* no IPv6 support */
267
268 struct hostent *nslookup;
269 struct sockaddr_in servaddr;
270
271 if ((nslookup = gethostbyname(server)) != NULL)
272 {
273 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
274 }
275 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
276 {
277 error("%s: unable to resolve host\n", server);
278 return False;
279 }
280
281 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
282 {
283 error("socket: %s\n", TCP_STRERROR);
284 return False;
285 }
286
287 servaddr.sin_family = AF_INET;
288 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
289
290 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
291 {
292 error("connect: %s\n", TCP_STRERROR);
293 TCP_CLOSE(g_sock);
294 return False;
295 }
296
297#endif /* IPv6 */
298
299 option_value = 1;
300 option_len = sizeof(option_value);
301 setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
302 /* receive buffer must be a least 16 K */
303 if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
304 {
305 if (option_value < (1024 * 16))
306 {
307 option_value = 1024 * 16;
308 option_len = sizeof(option_value);
309 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
310 option_len);
311 }
312 }
313
314 g_in.size = 4096;
315 g_in.data = (uint8 *) xmalloc(g_in.size);
316
317 for (i = 0; i < STREAM_COUNT; i++)
318 {
319 g_out[i].size = 4096;
320 g_out[i].data = (uint8 *) xmalloc(g_out[i].size);
321 }
322
323 return True;
324}
325
326/* Disconnect on the TCP layer */
327void
328tcp_disconnect(void)
329{
330 TCP_CLOSE(g_sock);
331}
332
333char *
334tcp_get_address()
335{
336 static char ipaddr[32];
337 struct sockaddr_in sockaddr;
338 socklen_t len = sizeof(sockaddr);
339 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
340 {
341 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
342 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
343 }
344 else
345 strcpy(ipaddr, "127.0.0.1");
346 return ipaddr;
347}
348
349/* reset the state of the tcp layer */
350/* Support for Session Directory */
351void
352tcp_reset_state(void)
353{
354 int i;
355
356 g_sock = -1; /* reset socket */
357
358 /* Clear the incoming stream */
359 if (g_in.data != NULL)
360 xfree(g_in.data);
361 g_in.p = NULL;
362 g_in.end = NULL;
363 g_in.data = NULL;
364 g_in.size = 0;
365 g_in.iso_hdr = NULL;
366 g_in.mcs_hdr = NULL;
367 g_in.sec_hdr = NULL;
368 g_in.rdp_hdr = NULL;
369 g_in.channel_hdr = NULL;
370
371 /* Clear the outgoing stream(s) */
372 for (i = 0; i < STREAM_COUNT; i++)
373 {
374 if (g_out[i].data != NULL)
375 xfree(g_out[i].data);
376 g_out[i].p = NULL;
377 g_out[i].end = NULL;
378 g_out[i].data = NULL;
379 g_out[i].size = 0;
380 g_out[i].iso_hdr = NULL;
381 g_out[i].mcs_hdr = NULL;
382 g_out[i].sec_hdr = NULL;
383 g_out[i].rdp_hdr = NULL;
384 g_out[i].channel_hdr = NULL;
385 }
386}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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