Merge my current work done on the kd++ branch:
[reactos.git] / reactos / base / applications / mstsc / tcp.c
1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - TCP layer
4 Copyright (C) Matthew Chapman 1999-2005
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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "precomp.h"
22
23 #ifdef _WIN32
24 #define socklen_t int
25 #define TCP_CLOSE(_sck) closesocket(_sck)
26 #define TCP_STRERROR "tcp error"
27 #define TCP_SLEEP(_n) Sleep(_n)
28 #define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
29 #else /* _WIN32 */
30 #define TCP_CLOSE(_sck) close(_sck)
31 #define TCP_STRERROR strerror(errno)
32 #define TCP_SLEEP(_n) sleep(_n)
33 #define TCP_BLOCKS (errno == EWOULDBLOCK)
34 #endif /* _WIN32 */
35
36 #ifndef INADDR_NONE
37 #define INADDR_NONE ((unsigned long) -1)
38 #endif
39
40 static int sock;
41 static struct stream in;
42 static struct stream out;
43 int g_tcp_port_rdp = TCP_PORT_RDP;
44
45 /* Initialise TCP transport data packet */
46 STREAM
47 tcp_init(uint32 maxlen)
48 {
49 if (maxlen > out.size)
50 {
51 out.data = (uint8 *) xrealloc(out.data, maxlen);
52 out.size = maxlen;
53 }
54
55 out.p = out.data;
56 out.end = out.data + out.size;
57 return &out;
58 }
59
60 /* Send TCP transport data packet */
61 void
62 tcp_send(STREAM s)
63 {
64 int length = s->end - s->data;
65 int sent, total = 0;
66
67 while (total < length)
68 {
69 sent = send(sock, (char *)s->data + total, length - total, 0);
70 if (sent <= 0)
71 {
72 if (sent == -1 && TCP_BLOCKS)
73 {
74 TCP_SLEEP(0);
75 sent = 0;
76 }
77 else
78 {
79 error("send: %s\n", TCP_STRERROR);
80 return;
81 }
82 }
83 total += sent;
84 }
85 }
86
87 /* Receive a message on the TCP layer */
88 STREAM
89 tcp_recv(STREAM s, uint32 length)
90 {
91 unsigned int new_length, end_offset, p_offset;
92 int rcvd = 0;
93
94 if (s == NULL)
95 {
96 /* read into "new" stream */
97 if (length > in.size)
98 {
99 in.data = (uint8 *) xrealloc(in.data, length);
100 in.size = length;
101 }
102 in.end = in.p = in.data;
103 s = &in;
104 }
105 else
106 {
107 /* append to existing stream */
108 new_length = (s->end - s->data) + length;
109 if (new_length > s->size)
110 {
111 p_offset = s->p - s->data;
112 end_offset = s->end - s->data;
113 s->data = (uint8 *) xrealloc(s->data, new_length);
114 s->size = new_length;
115 s->p = s->data + p_offset;
116 s->end = s->data + end_offset;
117 }
118 }
119
120 while (length > 0)
121 {
122 if (!ui_select(sock))
123 /* User quit */
124 return NULL;
125
126 rcvd = recv(sock, (char *)s->end, length, 0);
127 if (rcvd < 0)
128 {
129 if (rcvd == -1 && TCP_BLOCKS)
130 {
131 TCP_SLEEP(0);
132 rcvd = 0;
133 }
134 else
135 {
136 error("recv: %s\n", TCP_STRERROR);
137 return NULL;
138 }
139 }
140 else if (rcvd == 0)
141 {
142 error("Connection closed\n");
143 return NULL;
144 }
145
146 s->end += rcvd;
147 length -= rcvd;
148 }
149
150 return s;
151 }
152
153 /* Establish a connection on the TCP layer */
154 BOOL
155 tcp_connect(char *server)
156 {
157 int true_value = 1;
158
159 #ifdef IPv6
160
161 int n;
162 struct addrinfo hints, *res, *ressave;
163 char tcp_port_rdp_s[10];
164
165 snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
166
167 memset(&hints, 0, sizeof(struct addrinfo));
168 hints.ai_family = AF_UNSPEC;
169 hints.ai_socktype = SOCK_STREAM;
170
171 if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
172 {
173 error("getaddrinfo: %s\n", gai_strerror(n));
174 return False;
175 }
176
177 ressave = res;
178 sock = -1;
179 while (res)
180 {
181 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
182 if (!(sock < 0))
183 {
184 if (connect(sock, res->ai_addr, res->ai_addrlen) == 0)
185 break;
186 TCP_CLOSE(sock);
187 sock = -1;
188 }
189 res = res->ai_next;
190 }
191 freeaddrinfo(ressave);
192
193 if (sock == -1)
194 {
195 error("%s: unable to connect\n", server);
196 return False;
197 }
198
199 #else /* no IPv6 support */
200
201 struct hostent *nslookup;
202 struct sockaddr_in servaddr;
203
204 if ((nslookup = gethostbyname(server)) != NULL)
205 {
206 memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
207 }
208 else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
209 {
210 error("%s: unable to resolve host\n", server);
211 return False;
212 }
213
214 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
215 {
216 error("socket: %s\n", TCP_STRERROR);
217 return False;
218 }
219
220 servaddr.sin_family = AF_INET;
221 servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
222
223 if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
224 {
225 error("connect: %s\n", TCP_STRERROR);
226 TCP_CLOSE(sock);
227 return False;
228 }
229
230 #endif /* IPv6 */
231
232 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value));
233
234 in.size = 4096;
235 in.data = (uint8 *) xmalloc(in.size);
236
237 out.size = 4096;
238 out.data = (uint8 *) xmalloc(out.size);
239
240 return True;
241 }
242
243 /* Disconnect on the TCP layer */
244 void
245 tcp_disconnect(void)
246 {
247 TCP_CLOSE(sock);
248 }
249
250 char *
251 tcp_get_address()
252 {
253 static char ipaddr[32];
254 struct sockaddr_in sockaddr;
255 socklen_t len = sizeof(sockaddr);
256 if (getsockname(sock, (struct sockaddr *) &sockaddr, &len) == 0)
257 {
258 unsigned char *ip = (unsigned char *) &sockaddr.sin_addr;
259 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
260 }
261 else
262 strcpy(ipaddr, "127.0.0.1");
263 return ipaddr;
264 }
265
266 /* reset the state of the tcp layer */
267 /* Support for Session Directory */
268 void
269 tcp_reset_state(void)
270 {
271 sock = -1; /* reset socket */
272
273 /* Clear the incoming stream */
274 if (in.data != NULL)
275 xfree(in.data);
276 in.p = NULL;
277 in.end = NULL;
278 in.data = NULL;
279 in.size = 0;
280 in.iso_hdr = NULL;
281 in.mcs_hdr = NULL;
282 in.sec_hdr = NULL;
283 in.rdp_hdr = NULL;
284 in.channel_hdr = NULL;
285
286 /* Clear the outgoing stream */
287 if (out.data != NULL)
288 xfree(out.data);
289 out.p = NULL;
290 out.end = NULL;
291 out.data = NULL;
292 out.size = 0;
293 out.iso_hdr = NULL;
294 out.mcs_hdr = NULL;
295 out.sec_hdr = NULL;
296 out.rdp_hdr = NULL;
297 out.channel_hdr = NULL;
298 }