/src/FreeRDP/libfreerdp/core/tcp.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Transmission Control Protocol (TCP) |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | |
21 | | #include <freerdp/config.h> |
22 | | |
23 | | #include "settings.h" |
24 | | |
25 | | #include <time.h> |
26 | | #include <errno.h> |
27 | | #include <fcntl.h> |
28 | | |
29 | | #include <winpr/crt.h> |
30 | | #include <winpr/platform.h> |
31 | | #include <winpr/winsock.h> |
32 | | |
33 | | #include "rdp.h" |
34 | | #include "utils.h" |
35 | | |
36 | | #if !defined(_WIN32) |
37 | | |
38 | | #include <netdb.h> |
39 | | #include <unistd.h> |
40 | | #include <sys/ioctl.h> |
41 | | #include <sys/socket.h> |
42 | | #include <netinet/in.h> |
43 | | #include <netinet/tcp.h> |
44 | | #include <net/if.h> |
45 | | #include <sys/types.h> |
46 | | #include <arpa/inet.h> |
47 | | |
48 | | #ifdef WINPR_HAVE_POLL_H |
49 | | #include <poll.h> |
50 | | #else |
51 | | #include <time.h> |
52 | | #include <sys/select.h> |
53 | | #endif |
54 | | |
55 | | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
56 | | #ifndef SOL_TCP |
57 | | #define SOL_TCP IPPROTO_TCP |
58 | | #endif |
59 | | #endif |
60 | | |
61 | | #ifdef __APPLE__ |
62 | | #ifndef SOL_TCP |
63 | | #define SOL_TCP IPPROTO_TCP |
64 | | #endif |
65 | | #ifndef TCP_KEEPIDLE |
66 | | #define TCP_KEEPIDLE TCP_KEEPALIVE |
67 | | #endif |
68 | | #endif |
69 | | |
70 | | #else |
71 | | |
72 | | #include <winpr/windows.h> |
73 | | |
74 | | #include <winpr/crt.h> |
75 | | |
76 | | #define SHUT_RDWR SD_BOTH |
77 | | #define close(_fd) closesocket(_fd) |
78 | | |
79 | | #endif |
80 | | |
81 | | #include <freerdp/log.h> |
82 | | |
83 | | #include <winpr/stream.h> |
84 | | |
85 | | #include "tcp.h" |
86 | | #include "../crypto/opensslcompat.h" |
87 | | |
88 | | #if defined(HAVE_AF_VSOCK_H) |
89 | | #include <ctype.h> |
90 | | #include <linux/vm_sockets.h> |
91 | | #endif |
92 | | |
93 | 0 | #define TAG FREERDP_TAG("core") |
94 | | |
95 | | /* Simple Socket BIO */ |
96 | | |
97 | | typedef struct |
98 | | { |
99 | | SOCKET socket; |
100 | | HANDLE hEvent; |
101 | | } WINPR_BIO_SIMPLE_SOCKET; |
102 | | |
103 | | static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown); |
104 | | static int transport_bio_simple_uninit(BIO* bio); |
105 | | |
106 | | static int transport_bio_simple_write(BIO* bio, const char* buf, int size) |
107 | 115 | { |
108 | 115 | int error = 0; |
109 | 115 | int status = 0; |
110 | 115 | WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
111 | | |
112 | 115 | if (!buf) |
113 | 0 | return 0; |
114 | | |
115 | 115 | BIO_clear_flags(bio, BIO_FLAGS_WRITE); |
116 | 115 | status = _send(ptr->socket, buf, size, 0); |
117 | | |
118 | 115 | if (status <= 0) |
119 | 115 | { |
120 | 115 | error = WSAGetLastError(); |
121 | | |
122 | 115 | if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || |
123 | 115 | (error == WSAEALREADY)) |
124 | 0 | { |
125 | 0 | BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY)); |
126 | 0 | } |
127 | 115 | else |
128 | 115 | { |
129 | 115 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
130 | 115 | } |
131 | 115 | } |
132 | | |
133 | 115 | return status; |
134 | 115 | } |
135 | | |
136 | | static int transport_bio_simple_read(BIO* bio, char* buf, int size) |
137 | 0 | { |
138 | 0 | int error = 0; |
139 | 0 | int status = 0; |
140 | 0 | WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
141 | |
|
142 | 0 | if (!buf) |
143 | 0 | return 0; |
144 | | |
145 | 0 | BIO_clear_flags(bio, BIO_FLAGS_READ); |
146 | 0 | (void)WSAResetEvent(ptr->hEvent); |
147 | 0 | status = _recv(ptr->socket, buf, size, 0); |
148 | |
|
149 | 0 | if (status > 0) |
150 | 0 | { |
151 | 0 | return status; |
152 | 0 | } |
153 | | |
154 | 0 | if (status == 0) |
155 | 0 | { |
156 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | 0 | error = WSAGetLastError(); |
161 | |
|
162 | 0 | if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || |
163 | 0 | (error == WSAEALREADY)) |
164 | 0 | { |
165 | 0 | BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY)); |
166 | 0 | } |
167 | 0 | else |
168 | 0 | { |
169 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
170 | 0 | } |
171 | |
|
172 | 0 | return -1; |
173 | 0 | } |
174 | | |
175 | | static int transport_bio_simple_puts(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED const char* str) |
176 | 0 | { |
177 | 0 | return -2; |
178 | 0 | } |
179 | | |
180 | | static int transport_bio_simple_gets(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED char* str, |
181 | | WINPR_ATTR_UNUSED int size) |
182 | 0 | { |
183 | 0 | return 1; |
184 | 0 | } |
185 | | |
186 | | static long transport_bio_simple_ctrl(BIO* bio, int cmd, long arg1, void* arg2) |
187 | 19.2k | { |
188 | 19.2k | int status = -1; |
189 | 19.2k | WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
190 | | |
191 | 19.2k | switch (cmd) |
192 | 19.2k | { |
193 | 0 | case BIO_C_SET_SOCKET: |
194 | 0 | transport_bio_simple_uninit(bio); |
195 | 0 | transport_bio_simple_init(bio, (SOCKET)arg2, (int)arg1); |
196 | 0 | return 1; |
197 | 0 | case BIO_C_GET_SOCKET: |
198 | 0 | if (!BIO_get_init(bio) || !arg2) |
199 | 0 | return 0; |
200 | | |
201 | 0 | *((SOCKET*)arg2) = ptr->socket; |
202 | 0 | return 1; |
203 | 0 | case BIO_C_GET_EVENT: |
204 | 0 | if (!BIO_get_init(bio) || !arg2) |
205 | 0 | return 0; |
206 | | |
207 | 0 | *((HANDLE*)arg2) = ptr->hEvent; |
208 | 0 | return 1; |
209 | 6.40k | case BIO_C_SET_NONBLOCK: |
210 | 6.40k | { |
211 | 6.40k | #ifndef _WIN32 |
212 | 6.40k | int flags = 0; |
213 | 6.40k | flags = fcntl((int)ptr->socket, F_GETFL); |
214 | | |
215 | 6.40k | if (flags == -1) |
216 | 0 | return 0; |
217 | | |
218 | 6.40k | if (arg1) |
219 | 6.40k | (void)fcntl((int)ptr->socket, F_SETFL, flags | O_NONBLOCK); |
220 | 0 | else |
221 | 0 | (void)fcntl((int)ptr->socket, F_SETFL, flags & ~(O_NONBLOCK)); |
222 | | |
223 | | #else |
224 | | /* the internal socket is always non-blocking */ |
225 | | #endif |
226 | 6.40k | return 1; |
227 | 6.40k | } |
228 | 0 | case BIO_C_WAIT_READ: |
229 | 0 | { |
230 | 0 | int timeout = (int)arg1; |
231 | 0 | int sockfd = (int)ptr->socket; |
232 | 0 | #ifdef WINPR_HAVE_POLL_H |
233 | 0 | struct pollfd pollset; |
234 | 0 | pollset.fd = sockfd; |
235 | 0 | pollset.events = POLLIN; |
236 | 0 | pollset.revents = 0; |
237 | |
|
238 | 0 | do |
239 | 0 | { |
240 | 0 | status = poll(&pollset, 1, timeout); |
241 | 0 | } while ((status < 0) && (errno == EINTR)); |
242 | |
|
243 | | #else |
244 | | fd_set rset = WINPR_C_ARRAY_INIT; |
245 | | struct timeval tv = WINPR_C_ARRAY_INIT; |
246 | | FD_ZERO(&rset); |
247 | | FD_SET(sockfd, &rset); |
248 | | |
249 | | if (timeout) |
250 | | { |
251 | | tv.tv_sec = timeout / 1000; |
252 | | tv.tv_usec = (timeout % 1000) * 1000; |
253 | | } |
254 | | |
255 | | do |
256 | | { |
257 | | status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL); |
258 | | } while ((status < 0) && (errno == EINTR)); |
259 | | |
260 | | #endif |
261 | | /* Convert timeout to error return */ |
262 | 0 | if (status == 0) |
263 | 0 | errno = ETIMEDOUT; |
264 | 0 | } |
265 | 0 | break; |
266 | | |
267 | 0 | case BIO_C_WAIT_WRITE: |
268 | 0 | { |
269 | 0 | int timeout = (int)arg1; |
270 | 0 | int sockfd = (int)ptr->socket; |
271 | 0 | #ifdef WINPR_HAVE_POLL_H |
272 | 0 | struct pollfd pollset; |
273 | 0 | pollset.fd = sockfd; |
274 | 0 | pollset.events = POLLOUT; |
275 | 0 | pollset.revents = 0; |
276 | |
|
277 | 0 | do |
278 | 0 | { |
279 | 0 | status = poll(&pollset, 1, timeout); |
280 | 0 | } while ((status < 0) && (errno == EINTR)); |
281 | |
|
282 | | #else |
283 | | fd_set rset = WINPR_C_ARRAY_INIT; |
284 | | struct timeval tv = WINPR_C_ARRAY_INIT; |
285 | | FD_ZERO(&rset); |
286 | | FD_SET(sockfd, &rset); |
287 | | |
288 | | if (timeout) |
289 | | { |
290 | | tv.tv_sec = timeout / 1000; |
291 | | tv.tv_usec = (timeout % 1000) * 1000; |
292 | | } |
293 | | |
294 | | do |
295 | | { |
296 | | status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL); |
297 | | } while ((status < 0) && (errno == EINTR)); |
298 | | |
299 | | #endif |
300 | | /* Convert timeout to error return */ |
301 | 0 | if (status == 0) |
302 | 0 | errno = ETIMEDOUT; |
303 | 0 | } |
304 | 0 | break; |
305 | | |
306 | 6.40k | case BIO_C_SET_FD: |
307 | 6.40k | if (arg2) |
308 | 6.40k | { |
309 | 6.40k | transport_bio_simple_uninit(bio); |
310 | 6.40k | transport_bio_simple_init(bio, (SOCKET) * ((int*)arg2), (int)arg1); |
311 | 6.40k | status = 1; |
312 | 6.40k | } |
313 | | |
314 | 6.40k | break; |
315 | | |
316 | 0 | case BIO_C_GET_FD: |
317 | 0 | if (BIO_get_init(bio)) |
318 | 0 | { |
319 | 0 | if (arg2) |
320 | 0 | *((int*)arg2) = (int)ptr->socket; |
321 | |
|
322 | 0 | status = (int)ptr->socket; |
323 | 0 | } |
324 | |
|
325 | 0 | break; |
326 | | |
327 | 0 | case BIO_CTRL_GET_CLOSE: |
328 | 0 | status = BIO_get_shutdown(bio); |
329 | 0 | break; |
330 | | |
331 | 0 | case BIO_CTRL_SET_CLOSE: |
332 | 0 | BIO_set_shutdown(bio, (int)arg1); |
333 | 0 | status = 1; |
334 | 0 | break; |
335 | | |
336 | 0 | case BIO_CTRL_FLUSH: |
337 | 0 | case BIO_CTRL_DUP: |
338 | 0 | status = 1; |
339 | 0 | break; |
340 | | |
341 | 6.40k | default: |
342 | 6.40k | status = 0; |
343 | 6.40k | break; |
344 | 19.2k | } |
345 | | |
346 | 12.8k | return status; |
347 | 19.2k | } |
348 | | |
349 | | static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown) |
350 | 6.40k | { |
351 | 6.40k | WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
352 | 6.40k | ptr->socket = socket; |
353 | 6.40k | BIO_set_shutdown(bio, shutdown); |
354 | 6.40k | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
355 | 6.40k | BIO_set_init(bio, 1); |
356 | 6.40k | ptr->hEvent = WSACreateEvent(); |
357 | | |
358 | 6.40k | if (!ptr->hEvent) |
359 | 0 | return 0; |
360 | | |
361 | | /* WSAEventSelect automatically sets the socket in non-blocking mode */ |
362 | 6.40k | if (WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE)) |
363 | 0 | { |
364 | 0 | WLog_ERR(TAG, "WSAEventSelect returned 0x%08x", |
365 | 0 | WINPR_CXX_COMPAT_CAST(unsigned, WSAGetLastError())); |
366 | 0 | return 0; |
367 | 0 | } |
368 | | |
369 | 6.40k | return 1; |
370 | 6.40k | } |
371 | | |
372 | | static int transport_bio_simple_uninit(BIO* bio) |
373 | 12.8k | { |
374 | 12.8k | WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
375 | | |
376 | 12.8k | if (BIO_get_shutdown(bio)) |
377 | 12.8k | { |
378 | 12.8k | if (BIO_get_init(bio) && ptr) |
379 | 6.40k | { |
380 | 6.40k | _shutdown(ptr->socket, SD_BOTH); |
381 | 6.40k | closesocket(ptr->socket); |
382 | 6.40k | ptr->socket = 0; |
383 | 6.40k | } |
384 | 12.8k | } |
385 | | |
386 | 12.8k | if (ptr && ptr->hEvent) |
387 | 6.40k | { |
388 | 6.40k | (void)CloseHandle(ptr->hEvent); |
389 | 6.40k | ptr->hEvent = NULL; |
390 | 6.40k | } |
391 | | |
392 | 12.8k | BIO_set_init(bio, 0); |
393 | 12.8k | BIO_set_flags(bio, 0); |
394 | 12.8k | return 1; |
395 | 12.8k | } |
396 | | |
397 | | static int transport_bio_simple_new(BIO* bio) |
398 | 6.40k | { |
399 | 6.40k | WINPR_BIO_SIMPLE_SOCKET* ptr = NULL; |
400 | 6.40k | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
401 | 6.40k | ptr = (WINPR_BIO_SIMPLE_SOCKET*)calloc(1, sizeof(WINPR_BIO_SIMPLE_SOCKET)); |
402 | | |
403 | 6.40k | if (!ptr) |
404 | 0 | return 0; |
405 | | |
406 | 6.40k | BIO_set_data(bio, ptr); |
407 | 6.40k | return 1; |
408 | 6.40k | } |
409 | | |
410 | | static int transport_bio_simple_free(BIO* bio) |
411 | 6.40k | { |
412 | 6.40k | WINPR_BIO_SIMPLE_SOCKET* ptr = NULL; |
413 | | |
414 | 6.40k | if (!bio) |
415 | 0 | return 0; |
416 | | |
417 | 6.40k | transport_bio_simple_uninit(bio); |
418 | 6.40k | ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio); |
419 | | |
420 | 6.40k | if (ptr) |
421 | 6.40k | { |
422 | 6.40k | BIO_set_data(bio, NULL); |
423 | 6.40k | free(ptr); |
424 | 6.40k | } |
425 | | |
426 | 6.40k | return 1; |
427 | 6.40k | } |
428 | | |
429 | | BIO_METHOD* BIO_s_simple_socket(void) |
430 | 6.40k | { |
431 | 6.40k | static BIO_METHOD* bio_methods = NULL; |
432 | | |
433 | 6.40k | if (bio_methods == NULL) |
434 | 1 | { |
435 | 1 | if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE, "SimpleSocket"))) |
436 | 0 | return NULL; |
437 | | |
438 | 1 | BIO_meth_set_write(bio_methods, transport_bio_simple_write); |
439 | 1 | BIO_meth_set_read(bio_methods, transport_bio_simple_read); |
440 | 1 | BIO_meth_set_puts(bio_methods, transport_bio_simple_puts); |
441 | 1 | BIO_meth_set_gets(bio_methods, transport_bio_simple_gets); |
442 | 1 | BIO_meth_set_ctrl(bio_methods, transport_bio_simple_ctrl); |
443 | 1 | BIO_meth_set_create(bio_methods, transport_bio_simple_new); |
444 | 1 | BIO_meth_set_destroy(bio_methods, transport_bio_simple_free); |
445 | 1 | } |
446 | | |
447 | 6.40k | return bio_methods; |
448 | 6.40k | } |
449 | | |
450 | | /* Buffered Socket BIO */ |
451 | | |
452 | | typedef struct |
453 | | { |
454 | | BIO* bufferedBio; |
455 | | BOOL readBlocked; |
456 | | BOOL writeBlocked; |
457 | | RingBuffer xmitBuffer; |
458 | | } WINPR_BIO_BUFFERED_SOCKET; |
459 | | |
460 | | static int transport_bio_buffered_write(BIO* bio, const char* buf, int num) |
461 | 115 | { |
462 | 115 | int ret = num; |
463 | 115 | int nchunks = 0; |
464 | 115 | size_t committedBytes = 0; |
465 | 115 | DataChunk chunks[2] = WINPR_C_ARRAY_INIT; |
466 | 115 | WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio); |
467 | 115 | BIO* next_bio = NULL; |
468 | | |
469 | 115 | WINPR_ASSERT(bio); |
470 | 115 | WINPR_ASSERT(ptr); |
471 | 115 | if (num < 0) |
472 | 0 | return num; |
473 | | |
474 | 115 | ptr->writeBlocked = FALSE; |
475 | 115 | BIO_clear_flags(bio, BIO_FLAGS_WRITE); |
476 | | |
477 | | /* we directly append extra bytes in the xmit buffer, this could be prevented |
478 | | * but for now it makes the code more simple. |
479 | | */ |
480 | 115 | if (buf && (num > 0) && !ringbuffer_write(&ptr->xmitBuffer, (const BYTE*)buf, (size_t)num)) |
481 | 0 | { |
482 | 0 | WLog_ERR(TAG, "an error occurred when writing (num: %d)", num); |
483 | 0 | return -1; |
484 | 0 | } |
485 | | |
486 | 115 | nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer)); |
487 | 115 | next_bio = BIO_next(bio); |
488 | | |
489 | 115 | for (int i = 0; i < nchunks; i++) |
490 | 115 | { |
491 | 115 | while (chunks[i].size) |
492 | 115 | { |
493 | 115 | ERR_clear_error(); |
494 | | |
495 | 115 | const size_t wr = MIN(INT32_MAX, chunks[i].size); |
496 | 115 | const int status = BIO_write(next_bio, chunks[i].data, (int)wr); |
497 | | |
498 | 115 | if (status <= 0) |
499 | 115 | { |
500 | 115 | if (!BIO_should_retry(next_bio)) |
501 | 115 | { |
502 | 115 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
503 | 115 | ret = -1; /* fatal error */ |
504 | 115 | goto out; |
505 | 115 | } |
506 | | |
507 | 0 | if (BIO_should_write(next_bio)) |
508 | 0 | { |
509 | 0 | BIO_set_flags(bio, BIO_FLAGS_WRITE); |
510 | 0 | ptr->writeBlocked = TRUE; |
511 | 0 | goto out; /* EWOULDBLOCK */ |
512 | 0 | } |
513 | 0 | } |
514 | 0 | else |
515 | 0 | { |
516 | 0 | committedBytes += (size_t)status; |
517 | 0 | chunks[i].size -= (size_t)status; |
518 | 0 | chunks[i].data += status; |
519 | 0 | } |
520 | 115 | } |
521 | 115 | } |
522 | | |
523 | 115 | out: |
524 | 115 | ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes); |
525 | 115 | return ret; |
526 | 115 | } |
527 | | |
528 | | static int transport_bio_buffered_read(BIO* bio, char* buf, int size) |
529 | 0 | { |
530 | 0 | int status = 0; |
531 | 0 | WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio); |
532 | 0 | BIO* next_bio = BIO_next(bio); |
533 | 0 | ptr->readBlocked = FALSE; |
534 | 0 | BIO_clear_flags(bio, BIO_FLAGS_READ); |
535 | 0 | ERR_clear_error(); |
536 | 0 | status = BIO_read(next_bio, buf, size); |
537 | |
|
538 | 0 | if (status <= 0) |
539 | 0 | { |
540 | 0 | if (!BIO_should_retry(next_bio)) |
541 | 0 | { |
542 | 0 | BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
543 | 0 | goto out; |
544 | 0 | } |
545 | | |
546 | 0 | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
547 | |
|
548 | 0 | if (BIO_should_read(next_bio)) |
549 | 0 | { |
550 | 0 | BIO_set_flags(bio, BIO_FLAGS_READ); |
551 | 0 | ptr->readBlocked = TRUE; |
552 | 0 | goto out; |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | 0 | out: |
557 | 0 | return status; |
558 | 0 | } |
559 | | |
560 | | static int transport_bio_buffered_puts(WINPR_ATTR_UNUSED BIO* bio, |
561 | | WINPR_ATTR_UNUSED const char* str) |
562 | 0 | { |
563 | 0 | return 1; |
564 | 0 | } |
565 | | |
566 | | static int transport_bio_buffered_gets(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED char* str, |
567 | | WINPR_ATTR_UNUSED int size) |
568 | 0 | { |
569 | 0 | return 1; |
570 | 0 | } |
571 | | |
572 | | static long transport_bio_buffered_ctrl(BIO* bio, int cmd, long arg1, void* arg2) |
573 | 12.8k | { |
574 | 12.8k | long status = -1; |
575 | 12.8k | WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio); |
576 | | |
577 | 12.8k | switch (cmd) |
578 | 12.8k | { |
579 | 0 | case BIO_CTRL_FLUSH: |
580 | 0 | if (!ringbuffer_used(&ptr->xmitBuffer)) |
581 | 0 | status = 1; |
582 | 0 | else |
583 | 0 | status = (transport_bio_buffered_write(bio, NULL, 0) >= 0) ? 1 : -1; |
584 | |
|
585 | 0 | break; |
586 | | |
587 | 0 | case BIO_CTRL_WPENDING: |
588 | 0 | status = WINPR_ASSERTING_INT_CAST(long, ringbuffer_used(&ptr->xmitBuffer)); |
589 | 0 | break; |
590 | | |
591 | 0 | case BIO_CTRL_PENDING: |
592 | 0 | status = 0; |
593 | 0 | break; |
594 | | |
595 | 0 | case BIO_C_READ_BLOCKED: |
596 | 0 | status = (int)ptr->readBlocked; |
597 | 0 | break; |
598 | | |
599 | 0 | case BIO_C_WRITE_BLOCKED: |
600 | 0 | status = (int)ptr->writeBlocked; |
601 | 0 | break; |
602 | | |
603 | 12.8k | default: |
604 | 12.8k | status = BIO_ctrl(BIO_next(bio), cmd, arg1, arg2); |
605 | 12.8k | break; |
606 | 12.8k | } |
607 | | |
608 | 12.8k | return status; |
609 | 12.8k | } |
610 | | |
611 | | static int transport_bio_buffered_new(BIO* bio) |
612 | 6.40k | { |
613 | 6.40k | WINPR_BIO_BUFFERED_SOCKET* ptr = NULL; |
614 | 6.40k | BIO_set_init(bio, 1); |
615 | 6.40k | BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
616 | 6.40k | ptr = (WINPR_BIO_BUFFERED_SOCKET*)calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET)); |
617 | | |
618 | 6.40k | if (!ptr) |
619 | 0 | return -1; |
620 | | |
621 | 6.40k | BIO_set_data(bio, (void*)ptr); |
622 | | |
623 | 6.40k | if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000)) |
624 | 0 | return -1; |
625 | | |
626 | 6.40k | return 1; |
627 | 6.40k | } |
628 | | |
629 | | /* Free the buffered BIO. |
630 | | * Do not free other elements in the BIO stack, |
631 | | * let BIO_free_all handle that. */ |
632 | | static int transport_bio_buffered_free(BIO* bio) |
633 | 6.40k | { |
634 | 6.40k | WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio); |
635 | | |
636 | 6.40k | if (!ptr) |
637 | 0 | return 0; |
638 | | |
639 | 6.40k | ringbuffer_destroy(&ptr->xmitBuffer); |
640 | 6.40k | free(ptr); |
641 | 6.40k | return 1; |
642 | 6.40k | } |
643 | | |
644 | | BIO_METHOD* BIO_s_buffered_socket(void) |
645 | 6.40k | { |
646 | 6.40k | static BIO_METHOD* bio_methods = NULL; |
647 | | |
648 | 6.40k | if (bio_methods == NULL) |
649 | 1 | { |
650 | 1 | if (!(bio_methods = BIO_meth_new(BIO_TYPE_BUFFERED, "BufferedSocket"))) |
651 | 0 | return NULL; |
652 | | |
653 | 1 | BIO_meth_set_write(bio_methods, transport_bio_buffered_write); |
654 | 1 | BIO_meth_set_read(bio_methods, transport_bio_buffered_read); |
655 | 1 | BIO_meth_set_puts(bio_methods, transport_bio_buffered_puts); |
656 | 1 | BIO_meth_set_gets(bio_methods, transport_bio_buffered_gets); |
657 | 1 | BIO_meth_set_ctrl(bio_methods, transport_bio_buffered_ctrl); |
658 | 1 | BIO_meth_set_create(bio_methods, transport_bio_buffered_new); |
659 | 1 | BIO_meth_set_destroy(bio_methods, transport_bio_buffered_free); |
660 | 1 | } |
661 | | |
662 | 6.40k | return bio_methods; |
663 | 6.40k | } |
664 | | |
665 | | char* freerdp_tcp_address_to_string(const struct sockaddr_storage* addr, BOOL* pIPv6) |
666 | 0 | { |
667 | 0 | char ipAddress[INET6_ADDRSTRLEN + 1] = WINPR_C_ARRAY_INIT; |
668 | 0 | const struct sockaddr_in6* sockaddr_ipv6 = (const struct sockaddr_in6*)addr; |
669 | 0 | const struct sockaddr_in* sockaddr_ipv4 = (const struct sockaddr_in*)addr; |
670 | |
|
671 | 0 | if (addr == NULL) |
672 | 0 | { |
673 | 0 | return NULL; |
674 | 0 | } |
675 | | |
676 | 0 | switch (sockaddr_ipv4->sin_family) |
677 | 0 | { |
678 | 0 | case AF_INET: |
679 | 0 | if (!inet_ntop(sockaddr_ipv4->sin_family, &sockaddr_ipv4->sin_addr, ipAddress, |
680 | 0 | sizeof(ipAddress))) |
681 | 0 | return NULL; |
682 | | |
683 | 0 | break; |
684 | | |
685 | 0 | case AF_INET6: |
686 | 0 | if (!inet_ntop(sockaddr_ipv6->sin6_family, &sockaddr_ipv6->sin6_addr, ipAddress, |
687 | 0 | sizeof(ipAddress))) |
688 | 0 | return NULL; |
689 | | |
690 | 0 | break; |
691 | | |
692 | 0 | case AF_UNIX: |
693 | 0 | (void)sprintf_s(ipAddress, ARRAYSIZE(ipAddress), "127.0.0.1"); |
694 | 0 | break; |
695 | | |
696 | 0 | default: |
697 | 0 | return NULL; |
698 | 0 | } |
699 | | |
700 | 0 | if (pIPv6 != NULL) |
701 | 0 | { |
702 | 0 | *pIPv6 = (sockaddr_ipv4->sin_family == AF_INET6); |
703 | 0 | } |
704 | |
|
705 | 0 | return _strdup(ipAddress); |
706 | 0 | } |
707 | | |
708 | | static bool freerdp_tcp_get_ip_address(rdpSettings* settings, int sockfd) |
709 | 0 | { |
710 | 0 | WINPR_ASSERT(settings); |
711 | |
|
712 | 0 | struct sockaddr_storage saddr = WINPR_C_ARRAY_INIT; |
713 | 0 | socklen_t length = sizeof(struct sockaddr_storage); |
714 | |
|
715 | 0 | if (!freerdp_settings_set_string(settings, FreeRDP_ClientAddress, NULL)) |
716 | 0 | return false; |
717 | 0 | if (sockfd < 0) |
718 | 0 | return false; |
719 | 0 | if (getsockname(sockfd, (struct sockaddr*)&saddr, &length) != 0) |
720 | 0 | return false; |
721 | 0 | settings->ClientAddress = freerdp_tcp_address_to_string(&saddr, &settings->IPv6Enabled); |
722 | 0 | return settings->ClientAddress != NULL; |
723 | 0 | } |
724 | | |
725 | | char* freerdp_tcp_get_peer_address(SOCKET sockfd) |
726 | 0 | { |
727 | 0 | struct sockaddr_storage saddr = WINPR_C_ARRAY_INIT; |
728 | 0 | socklen_t length = sizeof(struct sockaddr_storage); |
729 | |
|
730 | 0 | if (getpeername((int)sockfd, (struct sockaddr*)&saddr, &length) != 0) |
731 | 0 | { |
732 | 0 | return NULL; |
733 | 0 | } |
734 | | |
735 | 0 | return freerdp_tcp_address_to_string(&saddr, NULL); |
736 | 0 | } |
737 | | |
738 | | static int freerdp_uds_connect(const char* path) |
739 | 0 | { |
740 | 0 | #ifndef _WIN32 |
741 | 0 | int status = 0; |
742 | 0 | int sockfd = 0; |
743 | 0 | struct sockaddr_un addr = WINPR_C_ARRAY_INIT; |
744 | 0 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0); |
745 | |
|
746 | 0 | if (sockfd == -1) |
747 | 0 | { |
748 | 0 | WLog_ERR(TAG, "socket"); |
749 | 0 | return -1; |
750 | 0 | } |
751 | | |
752 | 0 | addr.sun_family = AF_UNIX; |
753 | 0 | strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); |
754 | 0 | status = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)); |
755 | |
|
756 | 0 | if (status < 0) |
757 | 0 | { |
758 | 0 | WLog_ERR(TAG, "connect"); |
759 | 0 | close(sockfd); |
760 | 0 | return -1; |
761 | 0 | } |
762 | | |
763 | 0 | return sockfd; |
764 | | #else /* ifndef _WIN32 */ |
765 | | return -1; |
766 | | #endif |
767 | 0 | } |
768 | | |
769 | | struct addrinfo* freerdp_tcp_resolve_host(const char* hostname, int port, int ai_flags) |
770 | 0 | { |
771 | 0 | char* service = NULL; |
772 | 0 | char port_str[16]; |
773 | 0 | int status = 0; |
774 | 0 | struct addrinfo hints = WINPR_C_ARRAY_INIT; |
775 | 0 | struct addrinfo* result = NULL; |
776 | 0 | hints.ai_family = AF_UNSPEC; |
777 | 0 | hints.ai_socktype = SOCK_STREAM; |
778 | 0 | hints.ai_flags = ai_flags; |
779 | |
|
780 | 0 | if (port >= 0) |
781 | 0 | { |
782 | 0 | (void)sprintf_s(port_str, sizeof(port_str) - 1, "%d", port); |
783 | 0 | service = port_str; |
784 | 0 | } |
785 | |
|
786 | 0 | status = getaddrinfo(hostname, service, &hints, &result); |
787 | |
|
788 | 0 | if (status) |
789 | 0 | return NULL; |
790 | | |
791 | 0 | return result; |
792 | 0 | } |
793 | | |
794 | | static BOOL freerdp_tcp_is_hostname_resolvable(rdpContext* context, const char* hostname) |
795 | 0 | { |
796 | 0 | struct addrinfo* result = freerdp_tcp_resolve_host(hostname, -1, 0); |
797 | |
|
798 | 0 | if (!result) |
799 | 0 | { |
800 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND); |
801 | |
|
802 | 0 | return FALSE; |
803 | 0 | } |
804 | | |
805 | 0 | freerdp_set_last_error_log(context, 0); |
806 | 0 | freeaddrinfo(result); |
807 | 0 | return TRUE; |
808 | 0 | } |
809 | | |
810 | | static BOOL freerdp_tcp_connect_timeout(rdpContext* context, int sockfd, struct sockaddr* addr, |
811 | | size_t addrlen, UINT32 timeout) |
812 | 0 | { |
813 | 0 | BOOL rc = FALSE; |
814 | 0 | HANDLE handles[2] = WINPR_C_ARRAY_INIT; |
815 | 0 | DWORD count = 0; |
816 | 0 | u_long arg = 0; |
817 | 0 | DWORD tout = (timeout > 0) ? timeout : INFINITE; |
818 | |
|
819 | 0 | handles[count] = CreateEvent(NULL, TRUE, FALSE, NULL); |
820 | |
|
821 | 0 | if (!handles[count]) |
822 | 0 | return FALSE; |
823 | | |
824 | 0 | const int wsastatus = WSAEventSelect((SOCKET)sockfd, handles[count++], |
825 | 0 | FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE); |
826 | |
|
827 | 0 | if (wsastatus < 0) |
828 | 0 | { |
829 | 0 | WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError()); |
830 | 0 | goto fail; |
831 | 0 | } |
832 | | |
833 | 0 | handles[count++] = utils_get_abort_event(context->rdp); |
834 | |
|
835 | 0 | { |
836 | 0 | const int constatus = |
837 | 0 | _connect((SOCKET)sockfd, addr, WINPR_ASSERTING_INT_CAST(int, addrlen)); |
838 | 0 | if (constatus < 0) |
839 | 0 | { |
840 | 0 | const int estatus = WSAGetLastError(); |
841 | |
|
842 | 0 | switch (estatus) |
843 | 0 | { |
844 | 0 | case WSAEINPROGRESS: |
845 | 0 | case WSAEWOULDBLOCK: |
846 | 0 | break; |
847 | | |
848 | 0 | default: |
849 | 0 | goto fail; |
850 | 0 | } |
851 | 0 | } |
852 | 0 | } |
853 | | |
854 | 0 | { |
855 | 0 | const DWORD wstatus = WaitForMultipleObjects(count, handles, FALSE, tout); |
856 | 0 | if (WAIT_OBJECT_0 != wstatus) |
857 | 0 | goto fail; |
858 | 0 | } |
859 | | |
860 | 0 | { |
861 | 0 | INT32 optval = 0; |
862 | 0 | socklen_t optlen = sizeof(optval); |
863 | 0 | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0) |
864 | 0 | goto fail; |
865 | | |
866 | 0 | if (optval != 0) |
867 | 0 | { |
868 | 0 | char ebuffer[256] = WINPR_C_ARRAY_INIT; |
869 | 0 | WLog_DBG(TAG, "connect failed with error: %s [%" PRId32 "]", |
870 | 0 | winpr_strerror(optval, ebuffer, sizeof(ebuffer)), optval); |
871 | 0 | goto fail; |
872 | 0 | } |
873 | 0 | } |
874 | | |
875 | 0 | { |
876 | 0 | const int status = WSAEventSelect((SOCKET)sockfd, handles[0], 0); |
877 | 0 | if (status < 0) |
878 | 0 | { |
879 | 0 | WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError()); |
880 | 0 | goto fail; |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | 0 | if (_ioctlsocket((SOCKET)sockfd, FIONBIO, &arg) != 0) |
885 | 0 | goto fail; |
886 | | |
887 | 0 | rc = TRUE; |
888 | 0 | fail: |
889 | 0 | (void)CloseHandle(handles[0]); |
890 | 0 | return rc; |
891 | 0 | } |
892 | | |
893 | | typedef struct |
894 | | { |
895 | | SOCKET s; |
896 | | struct addrinfo* addr; |
897 | | struct addrinfo* result; |
898 | | } t_peer; |
899 | | |
900 | | static void peer_free(t_peer* peer) |
901 | 0 | { |
902 | 0 | if (peer->s != INVALID_SOCKET) |
903 | 0 | closesocket(peer->s); |
904 | |
|
905 | 0 | freeaddrinfo(peer->addr); |
906 | 0 | memset(peer, 0, sizeof(t_peer)); |
907 | 0 | peer->s = INVALID_SOCKET; |
908 | 0 | } |
909 | | |
910 | | static int freerdp_tcp_connect_multi(rdpContext* context, char** hostnames, const UINT32* ports, |
911 | | UINT32 count, UINT16 port, WINPR_ATTR_UNUSED UINT32 timeout) |
912 | 0 | { |
913 | 0 | UINT32 sindex = count; |
914 | 0 | SOCKET sockfd = INVALID_SOCKET; |
915 | 0 | struct addrinfo* addr = NULL; |
916 | 0 | struct addrinfo* result = NULL; |
917 | |
|
918 | 0 | HANDLE* events = (HANDLE*)calloc(count + 1, sizeof(HANDLE)); |
919 | 0 | t_peer* peers = (t_peer*)calloc(count, sizeof(t_peer)); |
920 | |
|
921 | 0 | if (!peers || !events || (count < 1)) |
922 | 0 | { |
923 | 0 | free(peers); |
924 | 0 | free((void*)events); |
925 | 0 | return -1; |
926 | 0 | } |
927 | | |
928 | 0 | for (UINT32 index = 0; index < count; index++) |
929 | 0 | { |
930 | 0 | int curPort = port; |
931 | |
|
932 | 0 | if (ports) |
933 | 0 | curPort = WINPR_ASSERTING_INT_CAST(int, ports[index]); |
934 | |
|
935 | 0 | result = freerdp_tcp_resolve_host(hostnames[index], curPort, 0); |
936 | |
|
937 | 0 | if (!result) |
938 | 0 | continue; |
939 | | |
940 | 0 | addr = result; |
941 | |
|
942 | 0 | if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0)) |
943 | 0 | { |
944 | 0 | while ((addr = addr->ai_next)) |
945 | 0 | { |
946 | 0 | if (addr->ai_family == AF_INET) |
947 | 0 | break; |
948 | 0 | } |
949 | |
|
950 | 0 | if (!addr) |
951 | 0 | addr = result; |
952 | 0 | } |
953 | |
|
954 | 0 | peers[index].s = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
955 | |
|
956 | 0 | if (peers[index].s == INVALID_SOCKET) |
957 | 0 | { |
958 | 0 | freeaddrinfo(result); |
959 | 0 | continue; |
960 | 0 | } |
961 | | |
962 | 0 | peers[index].addr = addr; |
963 | 0 | peers[index].result = result; |
964 | 0 | } |
965 | |
|
966 | 0 | for (UINT32 index = 0; index < count; index++) |
967 | 0 | { |
968 | 0 | sockfd = peers[index].s; |
969 | 0 | addr = peers[index].addr; |
970 | |
|
971 | 0 | if ((sockfd == INVALID_SOCKET) || (!addr)) |
972 | 0 | continue; |
973 | | |
974 | | /* blocking tcp connect */ |
975 | 0 | const int rc = |
976 | 0 | _connect(sockfd, addr->ai_addr, WINPR_ASSERTING_INT_CAST(int, addr->ai_addrlen)); |
977 | |
|
978 | 0 | if (rc >= 0) |
979 | 0 | { |
980 | | /* connection success */ |
981 | 0 | sindex = index; |
982 | 0 | break; |
983 | 0 | } |
984 | 0 | } |
985 | |
|
986 | 0 | if (sindex < count) |
987 | 0 | { |
988 | 0 | sockfd = peers[sindex].s; |
989 | 0 | peers[sindex].s = INVALID_SOCKET; |
990 | 0 | } |
991 | 0 | else |
992 | 0 | freerdp_set_last_error_log(context, FREERDP_ERROR_CONNECT_CANCELLED); |
993 | |
|
994 | 0 | for (UINT32 index = 0; index < count; index++) |
995 | 0 | peer_free(&peers[index]); |
996 | |
|
997 | 0 | free(peers); |
998 | 0 | free((void*)events); |
999 | 0 | return (int)sockfd; |
1000 | 0 | } |
1001 | | |
1002 | | BOOL freerdp_tcp_set_keep_alive_mode(const rdpSettings* settings, int sockfd) |
1003 | 6.40k | { |
1004 | 6.40k | const BOOL keepalive = (freerdp_settings_get_bool(settings, FreeRDP_TcpKeepAlive)); |
1005 | 6.40k | UINT32 optval = 0; |
1006 | 6.40k | socklen_t optlen = 0; |
1007 | 6.40k | optval = keepalive ? 1 : 0; |
1008 | 6.40k | optlen = sizeof(optval); |
1009 | | |
1010 | 6.40k | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, optlen) < 0) |
1011 | 6.40k | { |
1012 | 6.40k | WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_KEEPALIVE"); |
1013 | 6.40k | } |
1014 | | |
1015 | 6.40k | #ifndef _WIN32 |
1016 | 6.40k | #ifdef TCP_KEEPIDLE |
1017 | 6.40k | optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveDelay) : 0; |
1018 | 6.40k | optlen = sizeof(optval); |
1019 | | |
1020 | 6.40k | if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*)&optval, optlen) < 0) |
1021 | 6.40k | { |
1022 | 6.40k | WLog_WARN(TAG, "setsockopt() IPPROTO_TCP, TCP_KEEPIDLE"); |
1023 | 6.40k | } |
1024 | | |
1025 | 6.40k | #endif |
1026 | | #ifndef SOL_TCP |
1027 | | /* "tcp" from /etc/protocols as getprotobyname(3C) */ |
1028 | | #define SOL_TCP 6 |
1029 | | #endif |
1030 | 6.40k | #ifdef TCP_KEEPCNT |
1031 | 6.40k | optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveRetries) : 0; |
1032 | 6.40k | optlen = sizeof(optval); |
1033 | | |
1034 | 6.40k | if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void*)&optval, optlen) < 0) |
1035 | 6.40k | { |
1036 | 6.40k | WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPCNT"); |
1037 | 6.40k | } |
1038 | | |
1039 | 6.40k | #endif |
1040 | 6.40k | #ifdef TCP_KEEPINTVL |
1041 | 6.40k | optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveInterval) : 0; |
1042 | 6.40k | optlen = sizeof(optval); |
1043 | | |
1044 | 6.40k | if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void*)&optval, optlen) < 0) |
1045 | 6.40k | { |
1046 | 6.40k | WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPINTVL"); |
1047 | 6.40k | } |
1048 | | |
1049 | 6.40k | #endif |
1050 | 6.40k | #endif |
1051 | | #if defined(__MACOSX__) || defined(__IOS__) |
1052 | | optval = 1; |
1053 | | optlen = sizeof(optval); |
1054 | | |
1055 | | if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&optval, optlen) < 0) |
1056 | | { |
1057 | | WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_NOSIGPIPE"); |
1058 | | } |
1059 | | |
1060 | | #endif |
1061 | 6.40k | #ifdef TCP_USER_TIMEOUT |
1062 | 6.40k | optval = freerdp_settings_get_uint32(settings, FreeRDP_TcpAckTimeout); |
1063 | 6.40k | optlen = sizeof(optval); |
1064 | | |
1065 | 6.40k | if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (void*)&optval, optlen) < 0) |
1066 | 6.40k | { |
1067 | 6.40k | WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_USER_TIMEOUT"); |
1068 | 6.40k | } |
1069 | | |
1070 | 6.40k | #endif |
1071 | 6.40k | return TRUE; |
1072 | 6.40k | } |
1073 | | |
1074 | | int freerdp_tcp_connect(rdpContext* context, const char* hostname, int port, DWORD timeout) |
1075 | 0 | { |
1076 | 0 | rdpTransport* transport = NULL; |
1077 | 0 | if (!context || !context->rdp) |
1078 | 0 | return -1; |
1079 | 0 | transport = context->rdp->transport; |
1080 | 0 | if (!transport) |
1081 | 0 | return -1; |
1082 | 0 | return transport_tcp_connect(context->rdp->transport, hostname, port, timeout); |
1083 | 0 | } |
1084 | | |
1085 | | static struct addrinfo* reorder_addrinfo_by_preference(rdpContext* context, struct addrinfo* addr) |
1086 | 0 | { |
1087 | 0 | WINPR_ASSERT(context); |
1088 | 0 | WINPR_ASSERT(addr); |
1089 | |
|
1090 | 0 | const BOOL preferIPv6 = |
1091 | 0 | freerdp_settings_get_bool(context->settings, FreeRDP_PreferIPv6OverIPv4); |
1092 | 0 | if (!preferIPv6) |
1093 | 0 | return addr; |
1094 | | |
1095 | 0 | struct addrinfo* ipv6Head = NULL; |
1096 | 0 | struct addrinfo* ipv6Tail = NULL; |
1097 | 0 | struct addrinfo* otherHead = NULL; |
1098 | 0 | struct addrinfo* otherTail = NULL; |
1099 | | |
1100 | | /* Partition the list into IPv6 and other addresses */ |
1101 | 0 | while (addr) |
1102 | 0 | { |
1103 | 0 | struct addrinfo* next = addr->ai_next; |
1104 | 0 | addr->ai_next = NULL; |
1105 | |
|
1106 | 0 | if (addr->ai_family == AF_INET6) |
1107 | 0 | { |
1108 | 0 | if (!ipv6Head) |
1109 | 0 | ipv6Head = addr; |
1110 | 0 | else |
1111 | 0 | ipv6Tail->ai_next = addr; |
1112 | 0 | ipv6Tail = addr; |
1113 | 0 | } |
1114 | 0 | else |
1115 | 0 | { |
1116 | 0 | if (!otherHead) |
1117 | 0 | otherHead = addr; |
1118 | 0 | else |
1119 | 0 | otherTail->ai_next = addr; |
1120 | 0 | otherTail = addr; |
1121 | 0 | } |
1122 | 0 | addr = next; |
1123 | 0 | } |
1124 | | |
1125 | | /* Concatenate the lists */ |
1126 | 0 | if (ipv6Tail) |
1127 | 0 | ipv6Tail->ai_next = otherHead; |
1128 | |
|
1129 | 0 | return ipv6Head ? ipv6Head : otherHead; |
1130 | 0 | } |
1131 | | |
1132 | | static int get_next_addrinfo(rdpContext* context, struct addrinfo* input, struct addrinfo** result, |
1133 | | UINT32 errorCode) |
1134 | 0 | { |
1135 | 0 | WINPR_ASSERT(context); |
1136 | 0 | WINPR_ASSERT(result); |
1137 | |
|
1138 | 0 | struct addrinfo* addr = input; |
1139 | 0 | if (!addr) |
1140 | 0 | goto fail; |
1141 | | |
1142 | | /* We want to force IPvX, abort if not detected */ |
1143 | 0 | { |
1144 | 0 | const UINT32 IPvX = freerdp_settings_get_uint32(context->settings, FreeRDP_ForceIPvX); |
1145 | 0 | switch (IPvX) |
1146 | 0 | { |
1147 | 0 | case 4: |
1148 | 0 | case 6: |
1149 | 0 | { |
1150 | 0 | const int family = (IPvX == 4) ? AF_INET : AF_INET6; |
1151 | 0 | while (addr && (addr->ai_family != family)) |
1152 | 0 | addr = addr->ai_next; |
1153 | 0 | } |
1154 | 0 | break; |
1155 | 0 | default: |
1156 | 0 | break; |
1157 | 0 | } |
1158 | 0 | } |
1159 | | |
1160 | 0 | if (!addr) |
1161 | 0 | goto fail; |
1162 | | |
1163 | 0 | *result = addr; |
1164 | 0 | return 0; |
1165 | | |
1166 | 0 | fail: |
1167 | 0 | freerdp_set_last_error_if_not(context, errorCode); |
1168 | 0 | *result = NULL; |
1169 | 0 | return -1; |
1170 | 0 | } |
1171 | | |
1172 | | static int freerdp_vsock_connect(rdpContext* context, const char* hostname, int port) |
1173 | 0 | { |
1174 | | #if defined(HAVE_AF_VSOCK_H) |
1175 | | int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0); |
1176 | | if (sockfd < 0) |
1177 | | { |
1178 | | char buffer[256] = WINPR_C_ARRAY_INIT; |
1179 | | WLog_WARN(TAG, "socket(AF_VSOCK, SOCK_STREAM, 0) failed with %s", |
1180 | | winpr_strerror(errno, buffer, sizeof(buffer))); |
1181 | | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1182 | | return -1; |
1183 | | } |
1184 | | |
1185 | | struct sockaddr_vm addr = WINPR_C_ARRAY_INIT; |
1186 | | |
1187 | | addr.svm_family = AF_VSOCK; |
1188 | | addr.svm_port = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_port), port); |
1189 | | |
1190 | | errno = 0; |
1191 | | char* ptr = NULL; |
1192 | | unsigned long val = strtoul(hostname, &ptr, 10); |
1193 | | if (errno || (val > UINT32_MAX)) |
1194 | | { |
1195 | | char ebuffer[256] = WINPR_C_ARRAY_INIT; |
1196 | | WLog_ERR(TAG, "could not extract port from '%s', value=%lu, error=%s", hostname, val, |
1197 | | winpr_strerror(errno, ebuffer, sizeof(ebuffer))); |
1198 | | close(sockfd); |
1199 | | return -1; |
1200 | | } |
1201 | | addr.svm_cid = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_cid), val); |
1202 | | if (addr.svm_cid == 2) |
1203 | | { |
1204 | | addr.svm_flags = VMADDR_FLAG_TO_HOST; |
1205 | | } |
1206 | | if ((connect(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_vm))) == -1) |
1207 | | { |
1208 | | WLog_ERR(TAG, "failed to connect to %s", hostname); |
1209 | | close(sockfd); |
1210 | | return -1; |
1211 | | } |
1212 | | return sockfd; |
1213 | | #else |
1214 | 0 | WLog_ERR(TAG, "Compiled without AF_VSOCK, '%s' not supported", hostname); |
1215 | 0 | return -1; |
1216 | 0 | #endif |
1217 | 0 | } |
1218 | | |
1219 | | static void log_connection_address(const char* hostname, struct addrinfo* addr) |
1220 | 0 | { |
1221 | 0 | WINPR_ASSERT(addr); |
1222 | |
|
1223 | 0 | char* peerAddress = |
1224 | 0 | freerdp_tcp_address_to_string((const struct sockaddr_storage*)addr->ai_addr, NULL); |
1225 | 0 | if (peerAddress) |
1226 | 0 | WLog_DBG(TAG, "resolved %s: try to connect to %s", hostname, peerAddress); |
1227 | 0 | free(peerAddress); |
1228 | 0 | } |
1229 | | |
1230 | | static int freerdp_host_connect(rdpContext* context, const char* hostname, int port, DWORD timeout) |
1231 | 0 | { |
1232 | 0 | int sockfd = -1; |
1233 | 0 | struct addrinfo* addr = NULL; |
1234 | 0 | struct addrinfo* result = freerdp_tcp_resolve_host(hostname, port, 0); |
1235 | |
|
1236 | 0 | if (!result) |
1237 | 0 | { |
1238 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND); |
1239 | 0 | return -1; |
1240 | 0 | } |
1241 | 0 | freerdp_set_last_error_log(context, 0); |
1242 | | |
1243 | | /* By default we take the first returned entry. |
1244 | | * If PreferIPv6OverIPv4 = TRUE we reorder addresses by preference: |
1245 | | * IPv6 addresses come first, then other addresses. |
1246 | | */ |
1247 | 0 | result = reorder_addrinfo_by_preference(context, result); |
1248 | |
|
1249 | 0 | const int rc = get_next_addrinfo(context, result, &addr, FREERDP_ERROR_DNS_NAME_NOT_FOUND); |
1250 | 0 | if (rc < 0) |
1251 | 0 | goto fail; |
1252 | | |
1253 | 0 | do |
1254 | 0 | { |
1255 | 0 | sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
1256 | 0 | if (sockfd >= 0) |
1257 | 0 | { |
1258 | 0 | log_connection_address(hostname, addr); |
1259 | |
|
1260 | 0 | if (!freerdp_tcp_connect_timeout(context, sockfd, addr->ai_addr, addr->ai_addrlen, |
1261 | 0 | timeout)) |
1262 | 0 | { |
1263 | 0 | close(sockfd); |
1264 | 0 | sockfd = -1; |
1265 | 0 | } |
1266 | 0 | } |
1267 | |
|
1268 | 0 | if (sockfd < 0) |
1269 | 0 | { |
1270 | 0 | const int lrc = |
1271 | 0 | get_next_addrinfo(context, addr->ai_next, &addr, FREERDP_ERROR_CONNECT_FAILED); |
1272 | 0 | if (lrc < 0) |
1273 | 0 | goto fail; |
1274 | 0 | } |
1275 | 0 | } while (sockfd < 0); |
1276 | | |
1277 | 0 | fail: |
1278 | 0 | freeaddrinfo(result); |
1279 | 0 | return sockfd; |
1280 | 0 | } |
1281 | | |
1282 | | int freerdp_tcp_default_connect(rdpContext* context, rdpSettings* settings, const char* hostname, |
1283 | | int port, DWORD timeout) |
1284 | 0 | { |
1285 | 0 | int sockfd = -1; |
1286 | 0 | BOOL ipcSocket = FALSE; |
1287 | 0 | BOOL useExternalDefinedSocket = FALSE; |
1288 | |
|
1289 | 0 | if (!hostname) |
1290 | 0 | { |
1291 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1292 | |
|
1293 | 0 | return -1; |
1294 | 0 | } |
1295 | | |
1296 | 0 | if (hostname[0] == '/') |
1297 | 0 | ipcSocket = TRUE; |
1298 | |
|
1299 | 0 | if (hostname[0] == '|') |
1300 | 0 | useExternalDefinedSocket = TRUE; |
1301 | |
|
1302 | 0 | const char* vsock = utils_is_vsock(hostname); |
1303 | 0 | if (ipcSocket) |
1304 | 0 | { |
1305 | 0 | sockfd = freerdp_uds_connect(hostname); |
1306 | |
|
1307 | 0 | if (sockfd < 0) |
1308 | 0 | { |
1309 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1310 | 0 | return -1; |
1311 | 0 | } |
1312 | 0 | } |
1313 | 0 | else if (useExternalDefinedSocket) |
1314 | 0 | sockfd = port; |
1315 | 0 | else if (vsock) |
1316 | 0 | sockfd = freerdp_vsock_connect(context, vsock, port); |
1317 | 0 | else |
1318 | 0 | { |
1319 | 0 | if (!settings->GatewayEnabled) |
1320 | 0 | { |
1321 | 0 | if (!freerdp_tcp_is_hostname_resolvable(context, hostname) || |
1322 | 0 | settings->RemoteAssistanceMode) |
1323 | 0 | { |
1324 | 0 | if (settings->TargetNetAddressCount > 0) |
1325 | 0 | { |
1326 | 0 | WINPR_ASSERT(port <= UINT16_MAX); |
1327 | 0 | sockfd = freerdp_tcp_connect_multi( |
1328 | 0 | context, settings->TargetNetAddresses, settings->TargetNetPorts, |
1329 | 0 | settings->TargetNetAddressCount, (UINT16)port, timeout); |
1330 | 0 | } |
1331 | 0 | } |
1332 | 0 | } |
1333 | |
|
1334 | 0 | if (sockfd <= 0) |
1335 | 0 | sockfd = freerdp_host_connect(context, hostname, port, timeout); |
1336 | 0 | } |
1337 | | |
1338 | 0 | if (!vsock) |
1339 | 0 | { |
1340 | 0 | if (!freerdp_tcp_get_ip_address(settings, sockfd)) |
1341 | 0 | { |
1342 | 0 | if (!useExternalDefinedSocket) |
1343 | 0 | close(sockfd); |
1344 | |
|
1345 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1346 | |
|
1347 | 0 | WLog_ERR(TAG, "Couldn't get socket ip address"); |
1348 | 0 | return -1; |
1349 | 0 | } |
1350 | 0 | } |
1351 | | |
1352 | 0 | if (!ipcSocket && !useExternalDefinedSocket) |
1353 | 0 | { |
1354 | 0 | (void)freerdp_tcp_set_nodelay(WLog_Get(TAG), WLOG_ERROR, sockfd); |
1355 | 0 | } |
1356 | | |
1357 | | /* receive buffer must be a least 32 K */ |
1358 | 0 | UINT32 optval = 1; |
1359 | 0 | socklen_t optlen = sizeof(optval); |
1360 | 0 | if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*)&optval, &optlen) == 0) |
1361 | 0 | { |
1362 | 0 | if (optval < (1024 * 32)) |
1363 | 0 | { |
1364 | 0 | optval = 1024 * 32; |
1365 | 0 | optlen = sizeof(optval); |
1366 | |
|
1367 | 0 | if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*)&optval, optlen) < 0) |
1368 | 0 | { |
1369 | 0 | close(sockfd); |
1370 | |
|
1371 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1372 | |
|
1373 | 0 | WLog_ERR(TAG, "unable to set receive buffer len"); |
1374 | 0 | return -1; |
1375 | 0 | } |
1376 | 0 | } |
1377 | 0 | } |
1378 | | |
1379 | 0 | if (!ipcSocket && !useExternalDefinedSocket) |
1380 | 0 | { |
1381 | 0 | if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd)) |
1382 | 0 | { |
1383 | 0 | close(sockfd); |
1384 | |
|
1385 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED); |
1386 | |
|
1387 | 0 | WLog_ERR(TAG, "Couldn't set keep alive mode."); |
1388 | 0 | return -1; |
1389 | 0 | } |
1390 | 0 | } |
1391 | | |
1392 | 0 | if (WaitForSingleObject(utils_get_abort_event(context->rdp), 0) == WAIT_OBJECT_0) |
1393 | 0 | { |
1394 | 0 | close(sockfd); |
1395 | 0 | return -1; |
1396 | 0 | } |
1397 | | |
1398 | 0 | return sockfd; |
1399 | 0 | } |
1400 | | |
1401 | | struct rdp_tcp_layer |
1402 | | { |
1403 | | int sockfd; |
1404 | | HANDLE hEvent; |
1405 | | }; |
1406 | | typedef struct rdp_tcp_layer rdpTcpLayer; |
1407 | | |
1408 | | static int freerdp_tcp_layer_read(void* userContext, void* data, int bytes) |
1409 | 0 | { |
1410 | 0 | if (!userContext) |
1411 | 0 | return -1; |
1412 | 0 | if (!data || !bytes) |
1413 | 0 | return 0; |
1414 | | |
1415 | 0 | rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext; |
1416 | |
|
1417 | 0 | (void)WSAResetEvent(tcpLayer->hEvent); |
1418 | 0 | const int status = _recv((SOCKET)tcpLayer->sockfd, data, bytes, 0); |
1419 | |
|
1420 | 0 | if (status > 0) |
1421 | 0 | return status; |
1422 | | |
1423 | 0 | const int error = WSAGetLastError(); |
1424 | |
|
1425 | 0 | if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || |
1426 | 0 | (error == WSAEALREADY)) |
1427 | 0 | errno = EAGAIN; |
1428 | |
|
1429 | 0 | return status; |
1430 | 0 | } |
1431 | | |
1432 | | static int freerdp_tcp_layer_write(void* userContext, const void* data, int bytes) |
1433 | 0 | { |
1434 | 0 | if (!userContext) |
1435 | 0 | return -1; |
1436 | 0 | if (!data || !bytes) |
1437 | 0 | return 0; |
1438 | | |
1439 | 0 | rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext; |
1440 | |
|
1441 | 0 | const int status = _send((SOCKET)tcpLayer->sockfd, data, bytes, 0); |
1442 | 0 | if (status > 0) |
1443 | 0 | return status; |
1444 | | |
1445 | 0 | const int error = WSAGetLastError(); |
1446 | |
|
1447 | 0 | if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || |
1448 | 0 | (error == WSAEALREADY)) |
1449 | 0 | errno = EAGAIN; |
1450 | |
|
1451 | 0 | return status; |
1452 | 0 | } |
1453 | | |
1454 | | static BOOL freerdp_tcp_layer_close(void* userContext) |
1455 | 0 | { |
1456 | 0 | if (!userContext) |
1457 | 0 | return FALSE; |
1458 | | |
1459 | 0 | rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext; |
1460 | |
|
1461 | 0 | if (tcpLayer->sockfd >= 0) |
1462 | 0 | closesocket((SOCKET)tcpLayer->sockfd); |
1463 | 0 | if (tcpLayer->hEvent) |
1464 | 0 | (void)CloseHandle(tcpLayer->hEvent); |
1465 | |
|
1466 | 0 | return TRUE; |
1467 | 0 | } |
1468 | | |
1469 | | static BOOL freerdp_tcp_layer_wait(void* userContext, BOOL waitWrite, DWORD timeout) |
1470 | 0 | { |
1471 | 0 | if (!userContext) |
1472 | 0 | return FALSE; |
1473 | | |
1474 | 0 | rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext; |
1475 | |
|
1476 | 0 | int status = -1; |
1477 | 0 | int sockfd = tcpLayer->sockfd; |
1478 | 0 | #ifdef WINPR_HAVE_POLL_H |
1479 | 0 | struct pollfd pollset = WINPR_C_ARRAY_INIT; |
1480 | 0 | pollset.fd = sockfd; |
1481 | 0 | pollset.events = waitWrite ? POLLOUT : POLLIN; |
1482 | |
|
1483 | 0 | do |
1484 | 0 | { |
1485 | 0 | status = poll(&pollset, 1, (int)timeout); |
1486 | 0 | } while ((status < 0) && (errno == EINTR)); |
1487 | |
|
1488 | | #else |
1489 | | fd_set rset = WINPR_C_ARRAY_INIT; |
1490 | | struct timeval tv = WINPR_C_ARRAY_INIT; |
1491 | | FD_ZERO(&rset); |
1492 | | FD_SET(sockfd, &rset); |
1493 | | |
1494 | | if (timeout) |
1495 | | { |
1496 | | tv.tv_sec = timeout / 1000; |
1497 | | tv.tv_usec = (timeout % 1000) * 1000; |
1498 | | } |
1499 | | |
1500 | | do |
1501 | | { |
1502 | | if (waitWrite) |
1503 | | status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL); |
1504 | | else |
1505 | | status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL); |
1506 | | } while ((status < 0) && (errno == EINTR)); |
1507 | | |
1508 | | #endif |
1509 | |
|
1510 | 0 | return status != 0; |
1511 | 0 | } |
1512 | | |
1513 | | static HANDLE freerdp_tcp_layer_get_event(void* userContext) |
1514 | 0 | { |
1515 | 0 | if (!userContext) |
1516 | 0 | return NULL; |
1517 | | |
1518 | 0 | rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext; |
1519 | |
|
1520 | 0 | return tcpLayer->hEvent; |
1521 | 0 | } |
1522 | | |
1523 | | rdpTransportLayer* freerdp_tcp_connect_layer(rdpContext* context, const char* hostname, int port, |
1524 | | DWORD timeout) |
1525 | 0 | { |
1526 | 0 | WINPR_ASSERT(context); |
1527 | |
|
1528 | 0 | const rdpSettings* settings = context->settings; |
1529 | 0 | WINPR_ASSERT(settings); |
1530 | |
|
1531 | 0 | rdpTransportLayer* layer = NULL; |
1532 | 0 | rdpTcpLayer* tcpLayer = NULL; |
1533 | |
|
1534 | 0 | int sockfd = freerdp_tcp_connect(context, hostname, port, timeout); |
1535 | 0 | if (sockfd < 0) |
1536 | 0 | goto fail; |
1537 | 0 | if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd)) |
1538 | 0 | goto fail; |
1539 | | |
1540 | 0 | layer = transport_layer_new(freerdp_get_transport(context), sizeof(rdpTcpLayer)); |
1541 | 0 | if (!layer) |
1542 | 0 | goto fail; |
1543 | | |
1544 | 0 | layer->Read = freerdp_tcp_layer_read; |
1545 | 0 | layer->Write = freerdp_tcp_layer_write; |
1546 | 0 | layer->Close = freerdp_tcp_layer_close; |
1547 | 0 | layer->Wait = freerdp_tcp_layer_wait; |
1548 | 0 | layer->GetEvent = freerdp_tcp_layer_get_event; |
1549 | |
|
1550 | 0 | tcpLayer = (rdpTcpLayer*)layer->userContext; |
1551 | 0 | WINPR_ASSERT(tcpLayer); |
1552 | |
|
1553 | 0 | tcpLayer->sockfd = -1; |
1554 | 0 | tcpLayer->hEvent = WSACreateEvent(); |
1555 | 0 | if (!tcpLayer->hEvent) |
1556 | 0 | goto fail; |
1557 | | |
1558 | | /* WSAEventSelect automatically sets the socket in non-blocking mode */ |
1559 | 0 | if (WSAEventSelect((SOCKET)sockfd, tcpLayer->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE)) |
1560 | 0 | { |
1561 | 0 | WLog_ERR(TAG, "WSAEventSelect returned 0x%08x", (unsigned)WSAGetLastError()); |
1562 | 0 | goto fail; |
1563 | 0 | } |
1564 | | |
1565 | 0 | tcpLayer->sockfd = sockfd; |
1566 | |
|
1567 | 0 | return layer; |
1568 | | |
1569 | 0 | fail: |
1570 | 0 | if (sockfd >= 0) |
1571 | 0 | closesocket((SOCKET)sockfd); |
1572 | 0 | transport_layer_free(layer); |
1573 | 0 | return NULL; |
1574 | 0 | } |
1575 | | |
1576 | | BOOL freerdp_tcp_set_nodelay(wLog* log, DWORD level, int sockfd) |
1577 | 0 | { |
1578 | 0 | WINPR_ASSERT(log); |
1579 | |
|
1580 | 0 | int type = -1; |
1581 | 0 | socklen_t typelen = sizeof(type); |
1582 | 0 | char* ptype = (char*)&type; |
1583 | 0 | const int rc = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, ptype, &typelen); |
1584 | 0 | if (rc < 0) |
1585 | 0 | { |
1586 | 0 | char buffer[128] = WINPR_C_ARRAY_INIT; |
1587 | 0 | WLog_Print(log, level, "can't get SOL_SOCKET|SO_TYPE (%s)", |
1588 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
1589 | 0 | return FALSE; |
1590 | 0 | } |
1591 | 0 | else if (type == SOCK_STREAM) |
1592 | 0 | { |
1593 | 0 | int option_value = -1; |
1594 | 0 | const socklen_t option_len = sizeof(option_value); |
1595 | 0 | const int sr = |
1596 | 0 | setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*)&option_value, option_len); |
1597 | 0 | if (sr < 0) |
1598 | 0 | { |
1599 | | /* local unix sockets don't have the TCP_NODELAY implemented, so don't make this |
1600 | | * error fatal */ |
1601 | 0 | char buffer[128] = WINPR_C_ARRAY_INIT; |
1602 | 0 | WLog_Print(log, level, "can't set TCP_NODELAY (%s)", |
1603 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
1604 | 0 | return FALSE; |
1605 | 0 | } |
1606 | 0 | } |
1607 | 0 | else |
1608 | 0 | { |
1609 | 0 | WLog_Print(log, level, "Socket SOL_SOCKET|SO_TYPE %d unsupported", type); |
1610 | 0 | return FALSE; |
1611 | 0 | } |
1612 | 0 | return TRUE; |
1613 | 0 | } |