/[rdesktop]/jpeg/rdesktop/trunk/tcp.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /jpeg/rdesktop/trunk/tcp.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1362 - (hide annotations)
Wed Jan 3 06:41:13 2007 UTC (17 years, 5 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/tcp.c
File MIME type: text/plain
File size: 7702 byte(s)
Indent fix

1 astrand 963 /* -*- c-basic-offset: 8 -*-
2 matty 3 rdesktop: A Remote Desktop Protocol client.
3     Protocol services - TCP layer
4 stargo 828 Copyright (C) Matthew Chapman 1999-2005
5 jsorg71 1349
6 matty 3 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 jsorg71 1349
11 matty 3 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 jsorg71 376
16 matty 3 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 jsorg71 1349 #ifndef _WIN32
22 matty 24 #include <unistd.h> /* select read write close */
23     #include <sys/socket.h> /* socket connect setsockopt */
24     #include <sys/time.h> /* timeval */
25     #include <netdb.h> /* gethostbyname */
26     #include <netinet/in.h> /* sockaddr_in */
27     #include <netinet/tcp.h> /* TCP_NODELAY */
28     #include <arpa/inet.h> /* inet_addr */
29     #include <errno.h> /* errno */
30 jsorg71 1349 #endif
31    
32 matty 10 #include "rdesktop.h"
33 matty 3
34 jsorg71 1349 #ifdef _WIN32
35     #define socklen_t int
36     #define TCP_CLOSE(_sck) closesocket(_sck)
37     #define TCP_STRERROR "tcp error"
38     #define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
39     #else
40     #define TCP_CLOSE(_sck) close(_sck)
41     #define TCP_STRERROR strerror(errno)
42     #define TCP_BLOCKS (errno == EWOULDBLOCK)
43     #endif
44    
45 matthewc 306 #ifndef INADDR_NONE
46     #define INADDR_NONE ((unsigned long) -1)
47     #endif
48    
49 jsorg71 1349 static int g_sock;
50     static struct stream g_in;
51 stargo 1309 #ifndef WITH_SCARD
52 jsorg71 1349 static struct stream g_out;
53 stargo 1309 #endif
54 jsorg71 710 int g_tcp_port_rdp = TCP_PORT_RDP;
55 matty 10
56 jsorg71 1349 /* wait till socket is ready to write or timeout */
57     static BOOL
58     tcp_can_send(int sck, int millis)
59     {
60     fd_set wfds;
61     struct timeval time;
62     int sel_count;
63    
64     time.tv_sec = millis / 1000;
65     time.tv_usec = (millis * 1000) % 1000000;
66     FD_ZERO(&wfds);
67     FD_SET(sck, &wfds);
68     sel_count = select(sck + 1, 0, &wfds, 0, &time);
69     if (sel_count > 0)
70     {
71     return True;
72     }
73     return False;
74     }
75    
76 matty 10 /* Initialise TCP transport data packet */
77 matty 25 STREAM
78 jsorg71 376 tcp_init(uint32 maxlen)
79 matty 3 {
80 stargo 1309 STREAM result = NULL;
81    
82     #ifdef WITH_SCARD
83 stargo 1328 scard_lock(SCARD_LOCK_TCP);
84 stargo 1309 result = scard_tcp_init();
85     #else
86 jsorg71 1349 result = &g_out;
87 stargo 1309 #endif
88    
89     if (maxlen > result->size)
90 matty 3 {
91 stargo 1309 result->data = (uint8 *) xrealloc(result->data, maxlen);
92     result->size = maxlen;
93 matty 3 }
94    
95 stargo 1309 result->p = result->data;
96     result->end = result->data + result->size;
97     #ifdef WITH_SCARD
98 stargo 1328 scard_unlock(SCARD_LOCK_TCP);
99 stargo 1309 #endif
100     return result;
101 matty 3 }
102    
103     /* Send TCP transport data packet */
104 matty 25 void
105     tcp_send(STREAM s)
106 matty 3 {
107 matty 24 int length = s->end - s->data;
108 matty 3 int sent, total = 0;
109    
110 stargo 1309 #ifdef WITH_SCARD
111 stargo 1328 scard_lock(SCARD_LOCK_TCP);
112 stargo 1309 #endif
113 matty 3 while (total < length)
114     {
115 jsorg71 1349 sent = send(g_sock, s->data + total, length - total, 0);
116 matty 3 if (sent <= 0)
117     {
118 jsorg71 1349 if (sent == -1 && TCP_BLOCKS)
119     {
120     tcp_can_send(g_sock, 100);
121     sent = 0;
122     }
123     else
124     {
125     error("send: %s\n", TCP_STRERROR);
126     return;
127     }
128 matty 3 }
129     total += sent;
130     }
131 stargo 1309 #ifdef WITH_SCARD
132 stargo 1328 scard_unlock(SCARD_LOCK_TCP);
133 stargo 1309 #endif
134 matty 3 }
135    
136     /* Receive a message on the TCP layer */
137 matty 25 STREAM
138 matthewc 423 tcp_recv(STREAM s, uint32 length)
139 matty 3 {
140 jsorg71 1349 uint32 new_length, end_offset, p_offset;
141 matty 33 int rcvd = 0;
142 matty 3
143 matthewc 423 if (s == NULL)
144 matty 10 {
145 matthewc 423 /* read into "new" stream */
146 jsorg71 1349 if (length > g_in.size)
147 matthewc 423 {
148 jsorg71 1349 g_in.data = (uint8 *) xrealloc(g_in.data, length);
149     g_in.size = length;
150 matthewc 423 }
151 jsorg71 1349 g_in.end = g_in.p = g_in.data;
152     s = &g_in;
153 matty 10 }
154 matthewc 423 else
155     {
156     /* append to existing stream */
157     new_length = (s->end - s->data) + length;
158     if (new_length > s->size)
159     {
160     p_offset = s->p - s->data;
161     end_offset = s->end - s->data;
162     s->data = (uint8 *) xrealloc(s->data, new_length);
163     s->size = new_length;
164     s->p = s->data + p_offset;
165     s->end = s->data + end_offset;
166     }
167     }
168 matty 3
169     while (length > 0)
170     {
171 jsorg71 1349 if (!ui_select(g_sock))
172 astrand 275 /* User quit */
173     return NULL;
174 matty 3
175 jsorg71 1349 rcvd = recv(g_sock, s->end, length, 0);
176 astrand 546 if (rcvd < 0)
177 matty 3 {
178 jsorg71 1349 if (rcvd == -1 && TCP_BLOCKS)
179     {
180     rcvd = 0;
181     }
182     else
183     {
184     error("recv: %s\n", TCP_STRERROR);
185     return NULL;
186     }
187 matty 33 }
188 astrand 546 else if (rcvd == 0)
189     {
190     error("Connection closed\n");
191     return NULL;
192     }
193 matty 9
194 matthewc 423 s->end += rcvd;
195 matty 33 length -= rcvd;
196 matty 3 }
197    
198 matthewc 423 return s;
199 matty 10 }
200    
201     /* Establish a connection on the TCP layer */
202 matty 25 BOOL
203     tcp_connect(char *server)
204 matty 10 {
205 jsorg71 1349 socklen_t option_len;
206     uint32 option_value;
207 astrand 440
208     #ifdef IPv6
209    
210     int n;
211     struct addrinfo hints, *res, *ressave;
212     char tcp_port_rdp_s[10];
213    
214 jsorg71 710 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
215 astrand 440
216     memset(&hints, 0, sizeof(struct addrinfo));
217     hints.ai_family = AF_UNSPEC;
218     hints.ai_socktype = SOCK_STREAM;
219    
220 stargo 728 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
221 astrand 440 {
222 astrand 443 error("getaddrinfo: %s\n", gai_strerror(n));
223 astrand 440 return False;
224     }
225    
226     ressave = res;
227 jsorg71 1349 g_sock = -1;
228 astrand 440 while (res)
229     {
230 jsorg71 1349 g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
231     if (!(g_sock < 0))
232 astrand 440 {
233 jsorg71 1349 if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
234 astrand 440 break;
235 jsorg71 1349 TCP_CLOSE(g_sock);
236     g_sock = -1;
237 astrand 440 }
238     res = res->ai_next;
239     }
240     freeaddrinfo(ressave);
241    
242 jsorg71 1349 if (g_sock == -1)
243 astrand 443 {
244     error("%s: unable to connect\n", server);
245     return False;
246     }
247    
248 astrand 440 #else /* no IPv6 support */
249    
250 matty 10 struct hostent *nslookup;
251     struct sockaddr_in servaddr;
252    
253     if ((nslookup = gethostbyname(server)) != NULL)
254     {
255 astrand 82 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
256 matty 10 }
257 matthewc 303 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
258 matty 10 {
259 matty 30 error("%s: unable to resolve host\n", server);
260 matty 10 return False;
261     }
262    
263 jsorg71 1349 if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
264 matty 10 {
265 jsorg71 1349 error("socket: %s\n", TCP_STRERROR);
266 matty 10 return False;
267     }
268    
269     servaddr.sin_family = AF_INET;
270 jsorg71 1349 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
271 matty 10
272 jsorg71 1349 if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
273 matty 10 {
274 jsorg71 1349 error("connect: %s\n", TCP_STRERROR);
275     TCP_CLOSE(g_sock);
276 matty 10 return False;
277     }
278    
279 astrand 440 #endif /* IPv6 */
280    
281 jsorg71 1349 option_value = 1;
282     option_len = sizeof(option_value);
283     setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
284     /* receive buffer must be a least 16 K */
285     if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
286     {
287     if (option_value < (1024 * 16))
288     {
289     option_value = 1024 * 16;
290     option_len = sizeof(option_value);
291 astrand 1362 setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
292     option_len);
293 jsorg71 1349 }
294     }
295 matty 10
296 jsorg71 1349 g_in.size = 4096;
297     g_in.data = (uint8 *) xmalloc(g_in.size);
298 matty 10
299 stargo 1309 #ifdef WITH_SCARD
300     scard_tcp_connect();
301     #else
302 jsorg71 1349 g_out.size = 4096;
303     g_out.data = (uint8 *) xmalloc(g_out.size);
304 stargo 1309 #endif
305 matty 10
306 matty 3 return True;
307     }
308 matty 10
309     /* Disconnect on the TCP layer */
310 matty 25 void
311 matthewc 192 tcp_disconnect(void)
312 matty 10 {
313 jsorg71 1349 TCP_CLOSE(g_sock);
314 matty 10 }
315 n-ki 625
316     char *
317     tcp_get_address()
318     {
319     static char ipaddr[32];
320     struct sockaddr_in sockaddr;
321 stargo 872 socklen_t len = sizeof(sockaddr);
322 jsorg71 1349 if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
323 n-ki 625 {
324 astrand 1362 uint8 *ip = (uint8 *) & sockaddr.sin_addr;
325 n-ki 625 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
326     }
327     else
328     strcpy(ipaddr, "127.0.0.1");
329     return ipaddr;
330     }
331 astrand 977
332     /* reset the state of the tcp layer */
333     /* Support for Session Directory */
334     void
335     tcp_reset_state(void)
336     {
337 jsorg71 1349 g_sock = -1; /* reset socket */
338 astrand 977
339     /* Clear the incoming stream */
340 jsorg71 1349 if (g_in.data != NULL)
341     xfree(g_in.data);
342     g_in.p = NULL;
343     g_in.end = NULL;
344     g_in.data = NULL;
345     g_in.size = 0;
346     g_in.iso_hdr = NULL;
347     g_in.mcs_hdr = NULL;
348     g_in.sec_hdr = NULL;
349     g_in.rdp_hdr = NULL;
350     g_in.channel_hdr = NULL;
351 astrand 977
352 stargo 1309 /* Clear the outgoing stream(s) */
353     #ifdef WITH_SCARD
354     scard_tcp_reset_state();
355     #else
356 jsorg71 1349 if (g_out.data != NULL)
357     xfree(g_out.data);
358     g_out.p = NULL;
359     g_out.end = NULL;
360     g_out.data = NULL;
361     g_out.size = 0;
362     g_out.iso_hdr = NULL;
363     g_out.mcs_hdr = NULL;
364     g_out.sec_hdr = NULL;
365     g_out.rdp_hdr = NULL;
366     g_out.channel_hdr = NULL;
367 stargo 1309 #endif
368 astrand 977 }

  ViewVC Help
Powered by ViewVC 1.1.26