1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - TCP layer |
Protocol services - TCP layer |
4 |
Copyright (C) Matthew Chapman 1999-2002 |
Copyright (C) Matthew Chapman 1999-2005 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
34 |
|
|
35 |
static int sock; |
static int sock; |
36 |
static struct stream in; |
static struct stream in; |
37 |
|
#ifndef WITH_SCARD |
38 |
static struct stream out; |
static struct stream out; |
39 |
extern int tcp_port_rdp; |
#endif |
40 |
|
int g_tcp_port_rdp = TCP_PORT_RDP; |
41 |
|
|
42 |
/* Initialise TCP transport data packet */ |
/* Initialise TCP transport data packet */ |
43 |
STREAM |
STREAM |
44 |
tcp_init(uint32 maxlen) |
tcp_init(uint32 maxlen) |
45 |
{ |
{ |
46 |
if (maxlen > out.size) |
STREAM result = NULL; |
47 |
|
|
48 |
|
#ifdef WITH_SCARD |
49 |
|
scard_lock(SCARD_LOCK_TCP); |
50 |
|
result = scard_tcp_init(); |
51 |
|
#else |
52 |
|
result = &out; |
53 |
|
#endif |
54 |
|
|
55 |
|
if (maxlen > result->size) |
56 |
{ |
{ |
57 |
out.data = (uint8 *) xrealloc(out.data, maxlen); |
result->data = (uint8 *) xrealloc(result->data, maxlen); |
58 |
out.size = maxlen; |
result->size = maxlen; |
59 |
} |
} |
60 |
|
|
61 |
out.p = out.data; |
result->p = result->data; |
62 |
out.end = out.data + out.size; |
result->end = result->data + result->size; |
63 |
return &out; |
#ifdef WITH_SCARD |
64 |
|
scard_unlock(SCARD_LOCK_TCP); |
65 |
|
#endif |
66 |
|
return result; |
67 |
} |
} |
68 |
|
|
69 |
/* Send TCP transport data packet */ |
/* Send TCP transport data packet */ |
73 |
int length = s->end - s->data; |
int length = s->end - s->data; |
74 |
int sent, total = 0; |
int sent, total = 0; |
75 |
|
|
76 |
|
#ifdef WITH_SCARD |
77 |
|
scard_lock(SCARD_LOCK_TCP); |
78 |
|
#endif |
79 |
while (total < length) |
while (total < length) |
80 |
{ |
{ |
81 |
sent = send(sock, s->data + total, length - total, 0); |
sent = send(sock, s->data + total, length - total, 0); |
87 |
|
|
88 |
total += sent; |
total += sent; |
89 |
} |
} |
90 |
|
#ifdef WITH_SCARD |
91 |
|
scard_unlock(SCARD_LOCK_TCP); |
92 |
|
#endif |
93 |
} |
} |
94 |
|
|
95 |
/* Receive a message on the TCP layer */ |
/* Receive a message on the TCP layer */ |
162 |
struct addrinfo hints, *res, *ressave; |
struct addrinfo hints, *res, *ressave; |
163 |
char tcp_port_rdp_s[10]; |
char tcp_port_rdp_s[10]; |
164 |
|
|
165 |
snprintf(tcp_port_rdp_s, 10, "%d", tcp_port_rdp); |
snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp); |
166 |
|
|
167 |
memset(&hints, 0, sizeof(struct addrinfo)); |
memset(&hints, 0, sizeof(struct addrinfo)); |
168 |
hints.ai_family = AF_UNSPEC; |
hints.ai_family = AF_UNSPEC; |
169 |
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
170 |
|
|
171 |
n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res); |
if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res))) |
|
|
|
|
if (n < 0) |
|
172 |
{ |
{ |
173 |
error("getaddrinfo: %s\n", gai_strerror(n)); |
error("getaddrinfo: %s\n", gai_strerror(n)); |
174 |
return False; |
return False; |
218 |
} |
} |
219 |
|
|
220 |
servaddr.sin_family = AF_INET; |
servaddr.sin_family = AF_INET; |
221 |
servaddr.sin_port = htons(tcp_port_rdp); |
servaddr.sin_port = htons(g_tcp_port_rdp); |
222 |
|
|
223 |
if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) |
if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) |
224 |
{ |
{ |
234 |
in.size = 4096; |
in.size = 4096; |
235 |
in.data = (uint8 *) xmalloc(in.size); |
in.data = (uint8 *) xmalloc(in.size); |
236 |
|
|
237 |
|
#ifdef WITH_SCARD |
238 |
|
scard_tcp_connect(); |
239 |
|
#else |
240 |
out.size = 4096; |
out.size = 4096; |
241 |
out.data = (uint8 *) xmalloc(out.size); |
out.data = (uint8 *) xmalloc(out.size); |
242 |
|
#endif |
243 |
|
|
244 |
return True; |
return True; |
245 |
} |
} |
250 |
{ |
{ |
251 |
close(sock); |
close(sock); |
252 |
} |
} |
253 |
|
|
254 |
|
char * |
255 |
|
tcp_get_address() |
256 |
|
{ |
257 |
|
static char ipaddr[32]; |
258 |
|
struct sockaddr_in sockaddr; |
259 |
|
socklen_t len = sizeof(sockaddr); |
260 |
|
if (getsockname(sock, (struct sockaddr *) &sockaddr, &len) == 0) |
261 |
|
{ |
262 |
|
unsigned char *ip = (unsigned char *) &sockaddr.sin_addr; |
263 |
|
sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); |
264 |
|
} |
265 |
|
else |
266 |
|
strcpy(ipaddr, "127.0.0.1"); |
267 |
|
return ipaddr; |
268 |
|
} |
269 |
|
|
270 |
|
/* reset the state of the tcp layer */ |
271 |
|
/* Support for Session Directory */ |
272 |
|
void |
273 |
|
tcp_reset_state(void) |
274 |
|
{ |
275 |
|
sock = -1; /* reset socket */ |
276 |
|
|
277 |
|
/* Clear the incoming stream */ |
278 |
|
if (in.data != NULL) |
279 |
|
xfree(in.data); |
280 |
|
in.p = NULL; |
281 |
|
in.end = NULL; |
282 |
|
in.data = NULL; |
283 |
|
in.size = 0; |
284 |
|
in.iso_hdr = NULL; |
285 |
|
in.mcs_hdr = NULL; |
286 |
|
in.sec_hdr = NULL; |
287 |
|
in.rdp_hdr = NULL; |
288 |
|
in.channel_hdr = NULL; |
289 |
|
|
290 |
|
/* Clear the outgoing stream(s) */ |
291 |
|
#ifdef WITH_SCARD |
292 |
|
scard_tcp_reset_state(); |
293 |
|
#else |
294 |
|
if (out.data != NULL) |
295 |
|
xfree(out.data); |
296 |
|
out.p = NULL; |
297 |
|
out.end = NULL; |
298 |
|
out.data = NULL; |
299 |
|
out.size = 0; |
300 |
|
out.iso_hdr = NULL; |
301 |
|
out.mcs_hdr = NULL; |
302 |
|
out.sec_hdr = NULL; |
303 |
|
out.rdp_hdr = NULL; |
304 |
|
out.channel_hdr = NULL; |
305 |
|
#endif |
306 |
|
} |