VirtualBox

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

Last change on this file since 15061 was 11982, checked in by vboxsync, 16 years ago

All: license header changes for 2.0 (OSE headers, add Sun GPL/LGPL disclaimer)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - TCP layer
4 Copyright (C) Matthew Chapman 1999-2007
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
22 * Sun 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, Sun 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;
68
69/* wait till socket is ready to write or timeout */
70static RD_BOOL
71tcp_can_send(int sck, int millis)
72{
73 fd_set wfds;
74 struct timeval time;
75 int sel_count;
76
77 time.tv_sec = millis / 1000;
78 time.tv_usec = (millis * 1000) % 1000000;
79 FD_ZERO(&wfds);
80 FD_SET(sck, &wfds);
81 sel_count = select(sck + 1, 0, &wfds, 0, &time);
82 if (sel_count > 0)
83 {
84 return True;
85 }
86 return False;
87}
88
89/* Initialise TCP transport data packet */
90STREAM
91tcp_init(uint32 maxlen)
92{
93 static int cur_stream_id = 0;
94 STREAM result = NULL;
95
96#ifdef WITH_SCARD
97 scard_lock(SCARD_LOCK_TCP);
98#endif
99 result = &g_out[cur_stream_id];
100 cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
101
102 if (maxlen > result->size)
103 {
104 result->data = (uint8 *) xrealloc(result->data, maxlen);
105 result->size = maxlen;
106 }
107
108 result->p = result->data;
109 result->end = result->data + result->size;
110#ifdef WITH_SCARD
111 scard_unlock(SCARD_LOCK_TCP);
112#endif
113 return result;
114}
115
116/* Send TCP transport data packet */
117void
118tcp_send(STREAM s)
119{
120 int length = s->end - s->data;
121 int sent, total = 0;
122
123#ifdef WITH_SCARD
124 scard_lock(SCARD_LOCK_TCP);
125#endif
126 while (total < length)
127 {
128 sent = send(g_sock, s->data + total, length - total, 0);
129 if (sent <= 0)
130 {
131 if (sent == -1 && TCP_BLOCKS)
132 {
133 tcp_can_send(g_sock, 100);
134 sent = 0;
135 }
136 else
137 {
138 error("send: %s\n", TCP_STRERROR);
139 return;
140 }
141 }
142 total += sent;
143 }
144#ifdef WITH_SCARD
145 scard_unlock(SCARD_LOCK_TCP);
146#endif
147}
148
149/* Receive a message on the TCP layer */
150STREAM
151tcp_recv(STREAM s, uint32 length)
152{
153 uint32 new_length, end_offset, p_offset;
154 int rcvd = 0;
155
156 if (s == NULL)
157 {
158 /* read into "new" stream */
159 if (length > g_in.size)
160 {
161 g_in.data = (uint8 *) xrealloc(g_in.data, length);
162 g_in.size = length;
163 }
164 g_in.end = g_in.p = g_in.data;
165 s = &g_in;
166 }
167 else
168 {
169 /* append to existing stream */
170 new_length = (s->end - s->data) + length;
171 if (new_length > s->size)
172 {
173 p_offset = s->p - s->data;
174 end_offset = s->end - s->data;
175 s->data = (uint8 *) xrealloc(s->data, new_length);
176 s->size = new_length;
177 s->p = s->data + p_offset;
178 s->end = s->data + end_offset;
179 }
180 }
181
182 while (length > 0)
183 {
184 if (!ui_select(g_sock))
185 /* User quit */
186 return NULL;
187
188 rcvd = recv(g_sock, s->end, length, 0);
189 if (rcvd < 0)
190 {
191 if (rcvd == -1 && TCP_BLOCKS)
192 {
193 rcvd = 0;
194 }
195 else
196 {
197 error("recv: %s\n", TCP_STRERROR);
198 return NULL;
199 }
200 }
201 else if (rcvd == 0)
202 {
203 error("Connection closed\n");
204 return NULL;
205 }
206
207 s->end += rcvd;
208 length -= rcvd;
209 }
210
211 return s;
212}
213
214/* Establish a connection on the TCP layer */
215RD_BOOL
216tcp_connect(char *server)
217{
218 socklen_t option_len;
219 uint32 option_value;
220 int i;
221
222#ifdef IPv6
223
224 int n;
225 struct addrinfo hints, *res, *ressave;
226 char tcp_port_rdp_s[10];
227
228 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
229
230 memset(&hints, 0, sizeof(struct addrinfo));
231 hints.ai_family = AF_UNSPEC;
232 hints.ai_socktype = SOCK_STREAM;
233
234 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
235 {
236 error("getaddrinfo: %s\n", gai_strerror(n));
237 return False;
238 }
239
240 ressave = res;
241 g_sock = -1;
242 while (res)
243 {
244 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
245 if (!(g_sock < 0))
246 {
247 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
248 break;
249 TCP_CLOSE(g_sock);
250 g_sock = -1;
251 }
252 res = res->ai_next;
253 }
254 freeaddrinfo(ressave);
255
256 if (g_sock == -1)
257 {
258 error("%s: unable to connect\n", server);
259 return False;
260 }
261
262#else /* no IPv6 support */
263
264 struct hostent *nslookup;
265 struct sockaddr_in servaddr;
266
267 if ((nslookup = gethostbyname(server)) != NULL)
268 {
269 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
270 }
271 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
272 {
273 error("%s: unable to resolve host\n", server);
274 return False;
275 }
276
277 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
278 {
279 error("socket: %s\n", TCP_STRERROR);
280 return False;
281 }
282
283 servaddr.sin_family = AF_INET;
284 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
285
286 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
287 {
288 error("connect: %s\n", TCP_STRERROR);
289 TCP_CLOSE(g_sock);
290 return False;
291 }
292
293#endif /* IPv6 */
294
295 option_value = 1;
296 option_len = sizeof(option_value);
297 setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
298 /* receive buffer must be a least 16 K */
299 if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
300 {
301 if (option_value < (1024 * 16))
302 {
303 option_value = 1024 * 16;
304 option_len = sizeof(option_value);
305 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
306 option_len);
307 }
308 }
309
310 g_in.size = 4096;
311 g_in.data = (uint8 *) xmalloc(g_in.size);
312
313 for (i = 0; i < STREAM_COUNT; i++)
314 {
315 g_out[i].size = 4096;
316 g_out[i].data = (uint8 *) xmalloc(g_out[i].size);
317 }
318
319 return True;
320}
321
322/* Disconnect on the TCP layer */
323void
324tcp_disconnect(void)
325{
326 TCP_CLOSE(g_sock);
327}
328
329char *
330tcp_get_address()
331{
332 static char ipaddr[32];
333 struct sockaddr_in sockaddr;
334 socklen_t len = sizeof(sockaddr);
335 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
336 {
337 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
338 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
339 }
340 else
341 strcpy(ipaddr, "127.0.0.1");
342 return ipaddr;
343}
344
345/* reset the state of the tcp layer */
346/* Support for Session Directory */
347void
348tcp_reset_state(void)
349{
350 int i;
351
352 g_sock = -1; /* reset socket */
353
354 /* Clear the incoming stream */
355 if (g_in.data != NULL)
356 xfree(g_in.data);
357 g_in.p = NULL;
358 g_in.end = NULL;
359 g_in.data = NULL;
360 g_in.size = 0;
361 g_in.iso_hdr = NULL;
362 g_in.mcs_hdr = NULL;
363 g_in.sec_hdr = NULL;
364 g_in.rdp_hdr = NULL;
365 g_in.channel_hdr = NULL;
366
367 /* Clear the outgoing stream(s) */
368 for (i = 0; i < STREAM_COUNT; i++)
369 {
370 if (g_out[i].data != NULL)
371 xfree(g_out[i].data);
372 g_out[i].p = NULL;
373 g_out[i].end = NULL;
374 g_out[i].data = NULL;
375 g_out[i].size = 0;
376 g_out[i].iso_hdr = NULL;
377 g_out[i].mcs_hdr = NULL;
378 g_out[i].sec_hdr = NULL;
379 g_out[i].rdp_hdr = NULL;
380 g_out[i].channel_hdr = NULL;
381 }
382}
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