Coverage Report

Created: 2026-11-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
45
{
108
45
  int error = 0;
109
45
  int status = 0;
110
45
  WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
111
112
45
  if (!buf)
113
0
    return 0;
114
115
45
  BIO_clear_flags(bio, BIO_FLAGS_WRITE);
116
45
  status = _send(ptr->socket, buf, size, 0);
117
118
45
  if (status <= 0)
119
45
  {
120
45
    error = WSAGetLastError();
121
122
45
    if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
123
45
        (error == WSAEALREADY))
124
0
    {
125
0
      BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
126
0
    }
127
45
    else
128
45
    {
129
45
      BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
130
45
    }
131
45
  }
132
133
45
  return status;
134
45
}
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
21.6k
{
188
21.6k
  int status = -1;
189
21.6k
  WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
190
191
21.6k
  switch (cmd)
192
21.6k
  {
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
7.20k
    case BIO_C_SET_NONBLOCK:
210
7.20k
    {
211
7.20k
#ifndef _WIN32
212
7.20k
      int flags = 0;
213
7.20k
      flags = fcntl((int)ptr->socket, F_GETFL);
214
215
7.20k
      if (flags == -1)
216
0
        return 0;
217
218
7.20k
      if (arg1)
219
7.20k
        (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
7.20k
      return 1;
227
7.20k
    }
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, nullptr, nullptr, timeout ? &tv : nullptr);
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, nullptr, &rset, nullptr, timeout ? &tv : nullptr);
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
7.20k
    case BIO_C_SET_FD:
307
7.20k
      if (arg2)
308
7.20k
      {
309
7.20k
        transport_bio_simple_uninit(bio);
310
7.20k
        transport_bio_simple_init(bio, (SOCKET) * ((int*)arg2), (int)arg1);
311
7.20k
        status = 1;
312
7.20k
      }
313
314
7.20k
      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
7.20k
    default:
342
7.20k
      status = 0;
343
7.20k
      break;
344
21.6k
  }
345
346
14.4k
  return status;
347
21.6k
}
348
349
static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown)
350
7.20k
{
351
7.20k
  WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
352
7.20k
  ptr->socket = socket;
353
7.20k
  BIO_set_shutdown(bio, shutdown);
354
7.20k
  BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
355
7.20k
  BIO_set_init(bio, 1);
356
7.20k
  ptr->hEvent = WSACreateEvent();
357
358
7.20k
  if (!ptr->hEvent)
359
0
    return 0;
360
361
  /* WSAEventSelect automatically sets the socket in non-blocking mode */
362
7.20k
  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
7.20k
  return 1;
370
7.20k
}
371
372
static int transport_bio_simple_uninit(BIO* bio)
373
14.4k
{
374
14.4k
  WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
375
376
14.4k
  if (BIO_get_shutdown(bio))
377
14.4k
  {
378
14.4k
    if (BIO_get_init(bio) && ptr)
379
7.20k
    {
380
7.20k
      _shutdown(ptr->socket, SD_BOTH);
381
7.20k
      closesocket(ptr->socket);
382
7.20k
      ptr->socket = 0;
383
7.20k
    }
384
14.4k
  }
385
386
14.4k
  if (ptr && ptr->hEvent)
387
7.20k
  {
388
7.20k
    (void)CloseHandle(ptr->hEvent);
389
7.20k
    ptr->hEvent = nullptr;
390
7.20k
  }
391
392
14.4k
  BIO_set_init(bio, 0);
393
14.4k
  BIO_set_flags(bio, 0);
394
14.4k
  return 1;
395
14.4k
}
396
397
static int transport_bio_simple_new(BIO* bio)
398
7.20k
{
399
7.20k
  WINPR_BIO_SIMPLE_SOCKET* ptr = nullptr;
400
7.20k
  BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
401
7.20k
  ptr = (WINPR_BIO_SIMPLE_SOCKET*)calloc(1, sizeof(WINPR_BIO_SIMPLE_SOCKET));
402
403
7.20k
  if (!ptr)
404
0
    return 0;
405
406
7.20k
  BIO_set_data(bio, ptr);
407
7.20k
  return 1;
408
7.20k
}
409
410
static int transport_bio_simple_free(BIO* bio)
411
7.20k
{
412
7.20k
  WINPR_BIO_SIMPLE_SOCKET* ptr = nullptr;
413
414
7.20k
  if (!bio)
415
0
    return 0;
416
417
7.20k
  transport_bio_simple_uninit(bio);
418
7.20k
  ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
419
420
7.20k
  if (ptr)
421
7.20k
  {
422
7.20k
    BIO_set_data(bio, nullptr);
423
7.20k
    free(ptr);
424
7.20k
  }
425
426
7.20k
  return 1;
427
7.20k
}
428
429
BIO_METHOD* BIO_s_simple_socket(void)
430
7.20k
{
431
7.20k
  static BIO_METHOD* bio_methods = nullptr;
432
433
7.20k
  if (bio_methods == nullptr)
434
2
  {
435
2
    if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE, "SimpleSocket")))
436
0
      return nullptr;
437
438
2
    BIO_meth_set_write(bio_methods, transport_bio_simple_write);
439
2
    BIO_meth_set_read(bio_methods, transport_bio_simple_read);
440
2
    BIO_meth_set_puts(bio_methods, transport_bio_simple_puts);
441
2
    BIO_meth_set_gets(bio_methods, transport_bio_simple_gets);
442
2
    BIO_meth_set_ctrl(bio_methods, transport_bio_simple_ctrl);
443
2
    BIO_meth_set_create(bio_methods, transport_bio_simple_new);
444
2
    BIO_meth_set_destroy(bio_methods, transport_bio_simple_free);
445
2
  }
446
447
7.20k
  return bio_methods;
448
7.20k
}
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
45
{
462
45
  int ret = num;
463
45
  int nchunks = 0;
464
45
  size_t committedBytes = 0;
465
45
  DataChunk chunks[2] = WINPR_C_ARRAY_INIT;
466
45
  WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
467
45
  BIO* next_bio = nullptr;
468
469
45
  WINPR_ASSERT(bio);
470
45
  WINPR_ASSERT(ptr);
471
45
  if (num < 0)
472
0
    return num;
473
474
45
  ptr->writeBlocked = FALSE;
475
45
  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
45
  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
45
  nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer));
487
45
  next_bio = BIO_next(bio);
488
489
45
  for (int i = 0; i < nchunks; i++)
490
45
  {
491
45
    while (chunks[i].size)
492
45
    {
493
45
      ERR_clear_error();
494
495
45
      const size_t wr = MIN(INT32_MAX, chunks[i].size);
496
45
      const int status = BIO_write(next_bio, chunks[i].data, (int)wr);
497
498
45
      if (status <= 0)
499
45
      {
500
45
        if (!BIO_should_retry(next_bio))
501
45
        {
502
45
          BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
503
45
          ret = -1; /* fatal error */
504
45
          goto out;
505
45
        }
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
45
    }
521
45
  }
522
523
45
out:
524
45
  ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes);
525
45
  return ret;
526
45
}
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
14.4k
{
574
14.4k
  long status = -1;
575
14.4k
  WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
576
577
14.4k
  switch (cmd)
578
14.4k
  {
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, nullptr, 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
14.4k
    default:
604
14.4k
      status = BIO_ctrl(BIO_next(bio), cmd, arg1, arg2);
605
14.4k
      break;
606
14.4k
  }
607
608
14.4k
  return status;
609
14.4k
}
610
611
static int transport_bio_buffered_new(BIO* bio)
612
7.20k
{
613
7.20k
  WINPR_BIO_BUFFERED_SOCKET* ptr = nullptr;
614
7.20k
  BIO_set_init(bio, 1);
615
7.20k
  BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
616
7.20k
  ptr = (WINPR_BIO_BUFFERED_SOCKET*)calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET));
617
618
7.20k
  if (!ptr)
619
0
    return -1;
620
621
7.20k
  BIO_set_data(bio, (void*)ptr);
622
623
7.20k
  if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000))
624
0
    return -1;
625
626
7.20k
  return 1;
627
7.20k
}
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
7.20k
{
634
7.20k
  WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
635
636
7.20k
  if (!ptr)
637
0
    return 0;
638
639
7.20k
  ringbuffer_destroy(&ptr->xmitBuffer);
640
7.20k
  free(ptr);
641
7.20k
  return 1;
642
7.20k
}
643
644
BIO_METHOD* BIO_s_buffered_socket(void)
645
7.20k
{
646
7.20k
  static BIO_METHOD* bio_methods = nullptr;
647
648
7.20k
  if (bio_methods == nullptr)
649
2
  {
650
2
    if (!(bio_methods = BIO_meth_new(BIO_TYPE_BUFFERED, "BufferedSocket")))
651
0
      return nullptr;
652
653
2
    BIO_meth_set_write(bio_methods, transport_bio_buffered_write);
654
2
    BIO_meth_set_read(bio_methods, transport_bio_buffered_read);
655
2
    BIO_meth_set_puts(bio_methods, transport_bio_buffered_puts);
656
2
    BIO_meth_set_gets(bio_methods, transport_bio_buffered_gets);
657
2
    BIO_meth_set_ctrl(bio_methods, transport_bio_buffered_ctrl);
658
2
    BIO_meth_set_create(bio_methods, transport_bio_buffered_new);
659
2
    BIO_meth_set_destroy(bio_methods, transport_bio_buffered_free);
660
2
  }
661
662
7.20k
  return bio_methods;
663
7.20k
}
664
665
char* freerdp_tcp_address_to_string(const struct sockaddr_storage* addr, BOOL* pIPv6)
666
1.51k
{
667
1.51k
  char ipAddress[INET6_ADDRSTRLEN + 1] = WINPR_C_ARRAY_INIT;
668
1.51k
  const struct sockaddr_in6* sockaddr_ipv6 = (const struct sockaddr_in6*)addr;
669
1.51k
  const struct sockaddr_in* sockaddr_ipv4 = (const struct sockaddr_in*)addr;
670
671
1.51k
  if (addr == nullptr)
672
0
  {
673
0
    return nullptr;
674
0
  }
675
676
1.51k
  switch (sockaddr_ipv4->sin_family)
677
1.51k
  {
678
1.51k
    case AF_INET:
679
1.51k
      if (!inet_ntop(sockaddr_ipv4->sin_family, &sockaddr_ipv4->sin_addr, ipAddress,
680
1.51k
                     sizeof(ipAddress)))
681
0
        return nullptr;
682
683
1.51k
      break;
684
685
1.51k
    case AF_INET6:
686
0
      if (!inet_ntop(sockaddr_ipv6->sin6_family, &sockaddr_ipv6->sin6_addr, ipAddress,
687
0
                     sizeof(ipAddress)))
688
0
        return nullptr;
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 nullptr;
698
1.51k
  }
699
700
1.51k
  if (pIPv6 != nullptr)
701
0
  {
702
0
    *pIPv6 = (sockaddr_ipv4->sin_family == AF_INET6);
703
0
  }
704
705
1.51k
  return _strdup(ipAddress);
706
1.51k
}
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, nullptr))
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 != nullptr;
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 nullptr;
733
0
  }
734
735
0
  return freerdp_tcp_address_to_string(&saddr, nullptr);
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
1.51k
{
771
1.51k
  char* service = nullptr;
772
1.51k
  char port_str[16] = WINPR_C_ARRAY_INIT;
773
1.51k
  int status = 0;
774
775
1.51k
  struct addrinfo* result = nullptr;
776
1.51k
  struct addrinfo hints = { .ai_family = AF_UNSPEC,
777
1.51k
                          .ai_socktype = SOCK_STREAM,
778
1.51k
                          .ai_flags = ai_flags };
779
780
1.51k
  if (port >= 0)
781
1.51k
  {
782
1.51k
    (void)sprintf_s(port_str, sizeof(port_str) - 1, "%d", port);
783
1.51k
    service = port_str;
784
1.51k
  }
785
786
1.51k
  status = getaddrinfo(hostname, service, &hints, &result);
787
788
1.51k
  if (status)
789
0
  {
790
0
    WLog_WARN(TAG, "getaddrinfo(%s) failed with %s", hostname, gai_strerror(status));
791
0
    return nullptr;
792
0
  }
793
794
1.51k
  return result;
795
1.51k
}
796
797
static BOOL freerdp_tcp_is_hostname_resolvable(rdpContext* context, const char* hostname)
798
0
{
799
0
  struct addrinfo* result = freerdp_tcp_resolve_host(hostname, -1, 0);
800
801
0
  if (!result)
802
0
  {
803
0
    freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
804
805
0
    return FALSE;
806
0
  }
807
808
0
  freerdp_set_last_error_log(context, 0);
809
0
  freeaddrinfo(result);
810
0
  return TRUE;
811
0
}
812
813
static BOOL freerdp_tcp_connect_timeout(rdpContext* context, int sockfd, struct sockaddr* addr,
814
                                        size_t addrlen, UINT32 timeout)
815
0
{
816
0
  BOOL rc = FALSE;
817
0
  HANDLE handles[2] = WINPR_C_ARRAY_INIT;
818
0
  DWORD count = 0;
819
0
  u_long arg = 0;
820
0
  DWORD tout = (timeout > 0) ? timeout : INFINITE;
821
822
0
  handles[count] = CreateEvent(nullptr, TRUE, FALSE, nullptr);
823
824
0
  if (!handles[count])
825
0
    return FALSE;
826
827
0
  const int wsastatus = WSAEventSelect((SOCKET)sockfd, handles[count++],
828
0
                                       FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
829
830
0
  if (wsastatus < 0)
831
0
  {
832
0
    WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError());
833
0
    goto fail;
834
0
  }
835
836
0
  handles[count++] = utils_get_abort_event(context->rdp);
837
838
0
  {
839
0
    const int constatus =
840
0
        _connect((SOCKET)sockfd, addr, WINPR_ASSERTING_INT_CAST(int, addrlen));
841
0
    if (constatus < 0)
842
0
    {
843
0
      const int estatus = WSAGetLastError();
844
845
0
      switch (estatus)
846
0
      {
847
0
        case WSAEINPROGRESS:
848
0
        case WSAEWOULDBLOCK:
849
0
          break;
850
851
0
        default:
852
0
          goto fail;
853
0
      }
854
0
    }
855
0
  }
856
857
0
  {
858
0
    const DWORD wstatus = WaitForMultipleObjects(count, handles, FALSE, tout);
859
0
    if (WAIT_OBJECT_0 != wstatus)
860
0
      goto fail;
861
0
  }
862
863
0
  {
864
0
    const int status = WSAEventSelect((SOCKET)sockfd, handles[0], 0);
865
0
    if (status < 0)
866
0
    {
867
0
      WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError());
868
0
      goto fail;
869
0
    }
870
0
  }
871
872
0
  if (_ioctlsocket((SOCKET)sockfd, FIONBIO, &arg) != 0)
873
0
    goto fail;
874
875
0
  rc = TRUE;
876
0
fail:
877
0
{
878
0
  INT32 optval = 0;
879
0
  socklen_t optlen = sizeof(optval);
880
0
  if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&optval, &optlen) >= 0)
881
0
  {
882
0
    if (optval != 0)
883
0
    {
884
0
      char ebuffer[256] = WINPR_C_ARRAY_INIT;
885
0
      char hostname[512] = WINPR_C_ARRAY_INIT;
886
0
      char serv[512] = WINPR_C_ARRAY_INIT;
887
888
0
      getnameinfo(addr, WINPR_ASSERTING_INT_CAST(socklen_t, addrlen), hostname,
889
0
                  WINPR_ASSERTING_INT_CAST(socklen_t, sizeof(hostname)), serv,
890
0
                  WINPR_ASSERTING_INT_CAST(socklen_t, sizeof(serv)),
891
0
                  NI_NUMERICHOST | NI_NUMERICSERV);
892
0
      WLog_WARN(TAG, "connect to %s:%s failed with error: %s [%" PRId32 "]", hostname, serv,
893
0
                winpr_strerror(optval, ebuffer, sizeof(ebuffer)), optval);
894
0
      rc = FALSE;
895
0
    }
896
0
  }
897
0
}
898
899
0
  (void)CloseHandle(handles[0]);
900
0
  return rc;
901
0
}
902
903
typedef struct
904
{
905
  SOCKET s;
906
  struct addrinfo* addr;
907
  struct addrinfo* result;
908
} t_peer;
909
910
static void peer_free(t_peer* peer)
911
0
{
912
0
  if (peer->s != INVALID_SOCKET)
913
0
    closesocket(peer->s);
914
915
0
  freeaddrinfo(peer->addr);
916
0
  memset(peer, 0, sizeof(t_peer));
917
0
  peer->s = INVALID_SOCKET;
918
0
}
919
920
static int freerdp_tcp_connect_multi(rdpContext* context, char** hostnames, const UINT32* ports,
921
                                     UINT32 count, UINT16 port, WINPR_ATTR_UNUSED UINT32 timeout)
922
0
{
923
0
  UINT32 sindex = count;
924
0
  SOCKET sockfd = INVALID_SOCKET;
925
0
  struct addrinfo* addr = nullptr;
926
0
  struct addrinfo* result = nullptr;
927
928
0
  HANDLE* events = (HANDLE*)calloc(count + 1, sizeof(HANDLE));
929
0
  t_peer* peers = (t_peer*)calloc(count, sizeof(t_peer));
930
931
0
  if (!peers || !events || (count < 1))
932
0
  {
933
0
    free(peers);
934
0
    free((void*)events);
935
0
    return -1;
936
0
  }
937
938
0
  for (UINT32 index = 0; index < count; index++)
939
0
  {
940
0
    int curPort = port;
941
942
0
    if (ports)
943
0
      curPort = WINPR_ASSERTING_INT_CAST(int, ports[index]);
944
945
0
    result = freerdp_tcp_resolve_host(hostnames[index], curPort, 0);
946
947
0
    if (!result)
948
0
      continue;
949
950
0
    addr = result;
951
952
0
    if ((addr->ai_family == AF_INET6) && (addr->ai_next != nullptr))
953
0
    {
954
0
      while ((addr = addr->ai_next))
955
0
      {
956
0
        if (addr->ai_family == AF_INET)
957
0
          break;
958
0
      }
959
960
0
      if (!addr)
961
0
        addr = result;
962
0
    }
963
964
0
    peers[index].s = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
965
966
0
    if (peers[index].s == INVALID_SOCKET)
967
0
    {
968
0
      freeaddrinfo(result);
969
0
      continue;
970
0
    }
971
972
0
    peers[index].addr = addr;
973
0
    peers[index].result = result;
974
0
  }
975
976
0
  for (UINT32 index = 0; index < count; index++)
977
0
  {
978
0
    sockfd = peers[index].s;
979
0
    addr = peers[index].addr;
980
981
0
    if ((sockfd == INVALID_SOCKET) || (!addr))
982
0
      continue;
983
984
    /* blocking tcp connect */
985
0
    const int rc =
986
0
        _connect(sockfd, addr->ai_addr, WINPR_ASSERTING_INT_CAST(int, addr->ai_addrlen));
987
988
0
    if (rc >= 0)
989
0
    {
990
      /* connection success */
991
0
      sindex = index;
992
0
      break;
993
0
    }
994
0
  }
995
996
0
  if (sindex < count)
997
0
  {
998
0
    sockfd = peers[sindex].s;
999
0
    peers[sindex].s = INVALID_SOCKET;
1000
0
  }
1001
0
  else
1002
0
    freerdp_set_last_error_log(context, FREERDP_ERROR_CONNECT_CANCELLED);
1003
1004
0
  for (UINT32 index = 0; index < count; index++)
1005
0
    peer_free(&peers[index]);
1006
1007
0
  free(peers);
1008
0
  free((void*)events);
1009
0
  return (int)sockfd;
1010
0
}
1011
1012
BOOL freerdp_tcp_set_keep_alive_mode(const rdpSettings* settings, int sockfd)
1013
7.20k
{
1014
7.20k
  const BOOL keepalive = (freerdp_settings_get_bool(settings, FreeRDP_TcpKeepAlive));
1015
7.20k
  UINT32 optval = 0;
1016
7.20k
  socklen_t optlen = 0;
1017
7.20k
  BOOL isTcp = FALSE;
1018
1019
7.20k
  int xoptval = 0;
1020
7.20k
  socklen_t xoptlen = sizeof(xoptval);
1021
7.20k
  const int rc = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void*)&xoptval, &xoptlen);
1022
1023
7.20k
  if (rc == 0)
1024
24
    isTcp = xoptval == SOCK_STREAM;
1025
1026
7.20k
  optval = keepalive ? 1 : 0;
1027
7.20k
  optlen = sizeof(optval);
1028
1029
7.20k
  if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, optlen) < 0)
1030
7.18k
  {
1031
7.18k
    WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_KEEPALIVE");
1032
7.18k
  }
1033
1034
7.20k
  if (!isTcp)
1035
7.18k
    return TRUE;
1036
1037
24
#ifndef _WIN32
1038
24
#ifdef TCP_KEEPIDLE
1039
24
  optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveDelay) : 0;
1040
24
  optlen = sizeof(optval);
1041
1042
24
  if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*)&optval, optlen) < 0)
1043
24
  {
1044
24
    WLog_WARN(TAG, "setsockopt() IPPROTO_TCP, TCP_KEEPIDLE");
1045
24
  }
1046
1047
24
#endif
1048
#ifndef SOL_TCP
1049
  /* "tcp" from /etc/protocols as getprotobyname(3C) */
1050
#define SOL_TCP 6
1051
#endif
1052
24
#ifdef TCP_KEEPCNT
1053
24
  optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveRetries) : 0;
1054
24
  optlen = sizeof(optval);
1055
1056
24
  if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void*)&optval, optlen) < 0)
1057
24
  {
1058
24
    WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPCNT");
1059
24
  }
1060
1061
24
#endif
1062
24
#ifdef TCP_KEEPINTVL
1063
24
  optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveInterval) : 0;
1064
24
  optlen = sizeof(optval);
1065
1066
24
  if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void*)&optval, optlen) < 0)
1067
24
  {
1068
24
    WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPINTVL");
1069
24
  }
1070
1071
24
#endif
1072
24
#endif
1073
#if defined(__MACOSX__) || defined(__IOS__)
1074
  optval = 1;
1075
  optlen = sizeof(optval);
1076
1077
  if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&optval, optlen) < 0)
1078
  {
1079
    WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_NOSIGPIPE");
1080
  }
1081
1082
#endif
1083
24
#ifdef TCP_USER_TIMEOUT
1084
24
  optval = freerdp_settings_get_uint32(settings, FreeRDP_TcpAckTimeout);
1085
24
  optlen = sizeof(optval);
1086
1087
24
  if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (void*)&optval, optlen) < 0)
1088
24
  {
1089
24
    WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_USER_TIMEOUT");
1090
24
  }
1091
1092
24
#endif
1093
24
  return TRUE;
1094
7.20k
}
1095
1096
int freerdp_tcp_connect(rdpContext* context, const char* hostname, int port, DWORD timeout)
1097
0
{
1098
0
  rdpTransport* transport = nullptr;
1099
0
  if (!context || !context->rdp)
1100
0
    return -1;
1101
0
  transport = context->rdp->transport;
1102
0
  if (!transport)
1103
0
    return -1;
1104
0
  return transport_tcp_connect(context->rdp->transport, hostname, port, timeout);
1105
0
}
1106
1107
static struct addrinfo* reorder_addrinfo_by_preference(rdpContext* context, struct addrinfo* addr)
1108
0
{
1109
0
  WINPR_ASSERT(context);
1110
0
  WINPR_ASSERT(addr);
1111
1112
0
  const BOOL preferIPv6 =
1113
0
      freerdp_settings_get_bool(context->settings, FreeRDP_PreferIPv6OverIPv4);
1114
0
  if (!preferIPv6)
1115
0
    return addr;
1116
1117
0
  struct addrinfo* ipv6Head = nullptr;
1118
0
  struct addrinfo* ipv6Tail = nullptr;
1119
0
  struct addrinfo* otherHead = nullptr;
1120
0
  struct addrinfo* otherTail = nullptr;
1121
1122
  /* Partition the list into IPv6 and other addresses */
1123
0
  while (addr)
1124
0
  {
1125
0
    struct addrinfo* next = addr->ai_next;
1126
0
    addr->ai_next = nullptr;
1127
1128
0
    if (addr->ai_family == AF_INET6)
1129
0
    {
1130
0
      if (!ipv6Head)
1131
0
        ipv6Head = addr;
1132
0
      else
1133
0
        ipv6Tail->ai_next = addr;
1134
0
      ipv6Tail = addr;
1135
0
    }
1136
0
    else
1137
0
    {
1138
0
      if (!otherHead)
1139
0
        otherHead = addr;
1140
0
      else
1141
0
        otherTail->ai_next = addr;
1142
0
      otherTail = addr;
1143
0
    }
1144
0
    addr = next;
1145
0
  }
1146
1147
  /* Concatenate the lists */
1148
0
  if (ipv6Tail)
1149
0
    ipv6Tail->ai_next = otherHead;
1150
1151
0
  return ipv6Head ? ipv6Head : otherHead;
1152
0
}
1153
1154
static int get_next_addrinfo(rdpContext* context, struct addrinfo* input, struct addrinfo** result,
1155
                             UINT32 errorCode)
1156
0
{
1157
0
  WINPR_ASSERT(context);
1158
0
  WINPR_ASSERT(result);
1159
1160
0
  struct addrinfo* addr = input;
1161
0
  if (!addr)
1162
0
    goto fail;
1163
1164
  /* We want to force IPvX, abort if not detected */
1165
0
  {
1166
0
    const UINT32 IPvX = freerdp_settings_get_uint32(context->settings, FreeRDP_ForceIPvX);
1167
0
    switch (IPvX)
1168
0
    {
1169
0
      case 4:
1170
0
      case 6:
1171
0
      {
1172
0
        const int family = (IPvX == 4) ? AF_INET : AF_INET6;
1173
0
        while (addr && (addr->ai_family != family))
1174
0
          addr = addr->ai_next;
1175
0
      }
1176
0
      break;
1177
0
      default:
1178
0
        break;
1179
0
    }
1180
0
  }
1181
1182
0
  if (!addr)
1183
0
    goto fail;
1184
1185
0
  *result = addr;
1186
0
  return 0;
1187
1188
0
fail:
1189
0
  freerdp_set_last_error_if_not(context, errorCode);
1190
0
  *result = nullptr;
1191
0
  return -1;
1192
0
}
1193
1194
static int freerdp_vsock_connect(rdpContext* context, const char* hostname, int port)
1195
0
{
1196
#if defined(HAVE_AF_VSOCK_H)
1197
  int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
1198
  if (sockfd < 0)
1199
  {
1200
    char buffer[256] = WINPR_C_ARRAY_INIT;
1201
    WLog_WARN(TAG, "socket(AF_VSOCK, SOCK_STREAM, 0) failed with %s",
1202
              winpr_strerror(errno, buffer, sizeof(buffer)));
1203
    freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1204
    return -1;
1205
  }
1206
1207
  struct sockaddr_vm addr = WINPR_C_ARRAY_INIT;
1208
1209
  addr.svm_family = AF_VSOCK;
1210
  addr.svm_port = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_port), port);
1211
1212
  errno = 0;
1213
  char* ptr = nullptr;
1214
  unsigned long val = strtoul(hostname, &ptr, 10);
1215
  if (errno || (val > UINT32_MAX))
1216
  {
1217
    char ebuffer[256] = WINPR_C_ARRAY_INIT;
1218
    WLog_ERR(TAG, "could not extract port from '%s', value=%lu, error=%s", hostname, val,
1219
             winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
1220
    close(sockfd);
1221
    return -1;
1222
  }
1223
  addr.svm_cid = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_cid), val);
1224
  if (addr.svm_cid == 2)
1225
  {
1226
    addr.svm_flags = VMADDR_FLAG_TO_HOST;
1227
  }
1228
  if ((connect(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_vm))) == -1)
1229
  {
1230
    WLog_ERR(TAG, "failed to connect to %s", hostname);
1231
    close(sockfd);
1232
    return -1;
1233
  }
1234
  return sockfd;
1235
#else
1236
0
  WLog_ERR(TAG, "Compiled without AF_VSOCK, '%s' not supported", hostname);
1237
0
  return -1;
1238
0
#endif
1239
0
}
1240
1241
static void log_connection_address(const char* hostname, struct addrinfo* addr)
1242
0
{
1243
0
  WINPR_ASSERT(addr);
1244
1245
0
  char* peerAddress = freerdp_tcp_address_to_string(
1246
0
      WINPR_PACKED_ALIGN_CAST(const struct sockaddr_storage*, addr->ai_addr), nullptr);
1247
0
  if (peerAddress)
1248
0
    WLog_DBG(TAG, "resolved %s: try to connect to %s", hostname, peerAddress);
1249
0
  free(peerAddress);
1250
0
}
1251
1252
static int freerdp_host_connect(rdpContext* context, const char* hostname, int port, DWORD timeout)
1253
0
{
1254
0
  int sockfd = -1;
1255
0
  struct addrinfo* addr = nullptr;
1256
0
  struct addrinfo* result = freerdp_tcp_resolve_host(hostname, port, 0);
1257
1258
0
  if (!result)
1259
0
  {
1260
0
    freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1261
0
    return -1;
1262
0
  }
1263
0
  freerdp_set_last_error_log(context, 0);
1264
1265
  /* By default we take the first returned entry.
1266
   * If PreferIPv6OverIPv4 = TRUE we reorder addresses by preference:
1267
   * IPv6 addresses come first, then other addresses.
1268
   */
1269
0
  result = reorder_addrinfo_by_preference(context, result);
1270
1271
0
  const int rc = get_next_addrinfo(context, result, &addr, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1272
0
  if (rc < 0)
1273
0
    goto fail;
1274
1275
0
  do
1276
0
  {
1277
0
    sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
1278
0
    if (sockfd >= 0)
1279
0
    {
1280
0
      log_connection_address(hostname, addr);
1281
1282
0
      if (!freerdp_tcp_connect_timeout(context, sockfd, addr->ai_addr, addr->ai_addrlen,
1283
0
                                       timeout))
1284
0
      {
1285
0
        close(sockfd);
1286
0
        sockfd = -1;
1287
0
      }
1288
0
    }
1289
1290
0
    if (sockfd < 0)
1291
0
    {
1292
0
      const int lrc =
1293
0
          get_next_addrinfo(context, addr->ai_next, &addr, FREERDP_ERROR_CONNECT_FAILED);
1294
0
      if (lrc < 0)
1295
0
        goto fail;
1296
0
    }
1297
0
  } while (sockfd < 0);
1298
1299
0
fail:
1300
0
  freeaddrinfo(result);
1301
0
  return sockfd;
1302
0
}
1303
1304
int freerdp_tcp_default_connect(rdpContext* context, rdpSettings* settings, const char* hostname,
1305
                                int port, DWORD timeout)
1306
0
{
1307
0
  int sockfd = -1;
1308
0
  BOOL ipcSocket = FALSE;
1309
0
  BOOL useExternalDefinedSocket = FALSE;
1310
1311
0
  if (!hostname)
1312
0
  {
1313
0
    freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1314
1315
0
    return -1;
1316
0
  }
1317
1318
0
  if (hostname[0] == '/')
1319
0
    ipcSocket = TRUE;
1320
1321
0
  if (hostname[0] == '|')
1322
0
    useExternalDefinedSocket = TRUE;
1323
1324
0
  const char* vsock = utils_is_vsock(hostname);
1325
0
  if (ipcSocket)
1326
0
  {
1327
0
    sockfd = freerdp_uds_connect(hostname);
1328
1329
0
    if (sockfd < 0)
1330
0
    {
1331
0
      freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1332
0
      return -1;
1333
0
    }
1334
0
  }
1335
0
  else if (useExternalDefinedSocket)
1336
0
    sockfd = port;
1337
0
  else if (vsock)
1338
0
    sockfd = freerdp_vsock_connect(context, vsock, port);
1339
0
  else
1340
0
  {
1341
0
    if (!settings->GatewayEnabled)
1342
0
    {
1343
0
      if (!freerdp_tcp_is_hostname_resolvable(context, hostname) ||
1344
0
          settings->RemoteAssistanceMode)
1345
0
      {
1346
0
        if (settings->TargetNetAddressCount > 0)
1347
0
        {
1348
0
          WINPR_ASSERT(port <= UINT16_MAX);
1349
0
          sockfd = freerdp_tcp_connect_multi(
1350
0
              context, settings->TargetNetAddresses, settings->TargetNetPorts,
1351
0
              settings->TargetNetAddressCount, (UINT16)port, timeout);
1352
0
        }
1353
0
      }
1354
0
    }
1355
1356
0
    if (sockfd <= 0)
1357
0
      sockfd = freerdp_host_connect(context, hostname, port, timeout);
1358
0
  }
1359
1360
0
  if (!vsock)
1361
0
  {
1362
0
    if (!freerdp_tcp_get_ip_address(settings, sockfd))
1363
0
    {
1364
0
      if (!useExternalDefinedSocket)
1365
0
        close(sockfd);
1366
1367
0
      freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1368
1369
0
      WLog_ERR(TAG, "Couldn't get socket ip address");
1370
0
      return -1;
1371
0
    }
1372
0
  }
1373
1374
0
  if (!ipcSocket && !useExternalDefinedSocket)
1375
0
  {
1376
0
    (void)freerdp_tcp_set_nodelay(WLog_Get(TAG), WLOG_ERROR, sockfd);
1377
0
  }
1378
1379
  /* receive buffer must be a least 32 K */
1380
0
  UINT32 optval = 1;
1381
0
  socklen_t optlen = sizeof(optval);
1382
0
  if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*)&optval, &optlen) == 0)
1383
0
  {
1384
0
    if (optval < (1024 * 32))
1385
0
    {
1386
0
      optval = 1024 * 32;
1387
0
      optlen = sizeof(optval);
1388
1389
0
      if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*)&optval, optlen) < 0)
1390
0
      {
1391
0
        close(sockfd);
1392
1393
0
        freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1394
1395
0
        WLog_ERR(TAG, "unable to set receive buffer len");
1396
0
        return -1;
1397
0
      }
1398
0
    }
1399
0
  }
1400
1401
0
  if (!ipcSocket && !useExternalDefinedSocket)
1402
0
  {
1403
0
    if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1404
0
    {
1405
0
      close(sockfd);
1406
1407
0
      freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1408
1409
0
      WLog_ERR(TAG, "Couldn't set keep alive mode.");
1410
0
      return -1;
1411
0
    }
1412
0
  }
1413
1414
0
  if (WaitForSingleObject(utils_get_abort_event(context->rdp), 0) == WAIT_OBJECT_0)
1415
0
  {
1416
0
    close(sockfd);
1417
0
    return -1;
1418
0
  }
1419
1420
0
  return sockfd;
1421
0
}
1422
1423
struct rdp_tcp_layer
1424
{
1425
  int sockfd;
1426
  HANDLE hEvent;
1427
};
1428
typedef struct rdp_tcp_layer rdpTcpLayer;
1429
1430
static int freerdp_tcp_layer_read(void* userContext, void* data, int bytes)
1431
0
{
1432
0
  if (!userContext)
1433
0
    return -1;
1434
0
  if (!data || !bytes)
1435
0
    return 0;
1436
1437
0
  rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1438
1439
0
  (void)WSAResetEvent(tcpLayer->hEvent);
1440
0
  const int status = _recv((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1441
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 int freerdp_tcp_layer_write(void* userContext, const void* data, int bytes)
1455
0
{
1456
0
  if (!userContext)
1457
0
    return -1;
1458
0
  if (!data || !bytes)
1459
0
    return 0;
1460
1461
0
  rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1462
1463
0
  const int status = _send((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1464
0
  if (status > 0)
1465
0
    return status;
1466
1467
0
  const int error = WSAGetLastError();
1468
1469
0
  if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1470
0
      (error == WSAEALREADY))
1471
0
    errno = EAGAIN;
1472
1473
0
  return status;
1474
0
}
1475
1476
static BOOL freerdp_tcp_layer_close(void* userContext)
1477
0
{
1478
0
  if (!userContext)
1479
0
    return FALSE;
1480
1481
0
  rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1482
1483
0
  if (tcpLayer->sockfd >= 0)
1484
0
    closesocket((SOCKET)tcpLayer->sockfd);
1485
0
  if (tcpLayer->hEvent)
1486
0
    (void)CloseHandle(tcpLayer->hEvent);
1487
1488
0
  return TRUE;
1489
0
}
1490
1491
static BOOL freerdp_tcp_layer_wait(void* userContext, BOOL waitWrite, DWORD timeout)
1492
0
{
1493
0
  if (!userContext)
1494
0
    return FALSE;
1495
1496
0
  rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1497
1498
0
  int status = -1;
1499
0
  int sockfd = tcpLayer->sockfd;
1500
0
#ifdef WINPR_HAVE_POLL_H
1501
0
  struct pollfd pollset = WINPR_C_ARRAY_INIT;
1502
0
  pollset.fd = sockfd;
1503
0
  pollset.events = waitWrite ? POLLOUT : POLLIN;
1504
1505
0
  do
1506
0
  {
1507
0
    status = poll(&pollset, 1, (int)timeout);
1508
0
  } while ((status < 0) && (errno == EINTR));
1509
1510
#else
1511
  fd_set rset = WINPR_C_ARRAY_INIT;
1512
  struct timeval tv = WINPR_C_ARRAY_INIT;
1513
  FD_ZERO(&rset);
1514
  FD_SET(sockfd, &rset);
1515
1516
  if (timeout)
1517
  {
1518
    tv.tv_sec = timeout / 1000;
1519
    tv.tv_usec = (timeout % 1000) * 1000;
1520
  }
1521
1522
  do
1523
  {
1524
    if (waitWrite)
1525
      status = select(sockfd + 1, nullptr, &rset, nullptr, timeout ? &tv : nullptr);
1526
    else
1527
      status = select(sockfd + 1, &rset, nullptr, nullptr, timeout ? &tv : nullptr);
1528
  } while ((status < 0) && (errno == EINTR));
1529
1530
#endif
1531
1532
0
  return status != 0;
1533
0
}
1534
1535
static HANDLE freerdp_tcp_layer_get_event(void* userContext)
1536
0
{
1537
0
  if (!userContext)
1538
0
    return nullptr;
1539
1540
0
  rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1541
1542
0
  return tcpLayer->hEvent;
1543
0
}
1544
1545
rdpTransportLayer* freerdp_tcp_connect_layer(rdpContext* context, const char* hostname, int port,
1546
                                             DWORD timeout)
1547
0
{
1548
0
  WINPR_ASSERT(context);
1549
1550
0
  const rdpSettings* settings = context->settings;
1551
0
  WINPR_ASSERT(settings);
1552
1553
0
  rdpTransportLayer* layer = nullptr;
1554
0
  rdpTcpLayer* tcpLayer = nullptr;
1555
1556
0
  int sockfd = freerdp_tcp_connect(context, hostname, port, timeout);
1557
0
  if (sockfd < 0)
1558
0
    goto fail;
1559
0
  if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1560
0
    goto fail;
1561
1562
0
  layer = transport_layer_new(freerdp_get_transport(context), sizeof(rdpTcpLayer));
1563
0
  if (!layer)
1564
0
    goto fail;
1565
1566
0
  layer->Read = freerdp_tcp_layer_read;
1567
0
  layer->Write = freerdp_tcp_layer_write;
1568
0
  layer->Close = freerdp_tcp_layer_close;
1569
0
  layer->Wait = freerdp_tcp_layer_wait;
1570
0
  layer->GetEvent = freerdp_tcp_layer_get_event;
1571
1572
0
  tcpLayer = (rdpTcpLayer*)layer->userContext;
1573
0
  WINPR_ASSERT(tcpLayer);
1574
1575
0
  tcpLayer->sockfd = -1;
1576
0
  tcpLayer->hEvent = WSACreateEvent();
1577
0
  if (!tcpLayer->hEvent)
1578
0
    goto fail;
1579
1580
  /* WSAEventSelect automatically sets the socket in non-blocking mode */
1581
0
  if (WSAEventSelect((SOCKET)sockfd, tcpLayer->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
1582
0
  {
1583
0
    WLog_ERR(TAG, "WSAEventSelect returned 0x%08x", (unsigned)WSAGetLastError());
1584
0
    goto fail;
1585
0
  }
1586
1587
0
  tcpLayer->sockfd = sockfd;
1588
1589
0
  return layer;
1590
1591
0
fail:
1592
0
  if (sockfd >= 0)
1593
0
    closesocket((SOCKET)sockfd);
1594
0
  transport_layer_free(layer);
1595
0
  return nullptr;
1596
0
}
1597
1598
BOOL freerdp_tcp_set_nodelay(wLog* log, DWORD level, int sockfd)
1599
24
{
1600
24
  WINPR_ASSERT(log);
1601
1602
24
  int type = -1;
1603
24
  socklen_t typelen = sizeof(type);
1604
24
  char* ptype = (char*)&type;
1605
24
  const int rc = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, ptype, &typelen);
1606
24
  if (rc < 0)
1607
0
  {
1608
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
1609
0
    WLog_Print(log, level, "can't get SOL_SOCKET|SO_TYPE (%s)",
1610
0
               winpr_strerror(errno, buffer, sizeof(buffer)));
1611
0
    return FALSE;
1612
0
  }
1613
24
  else if (type == SOCK_STREAM)
1614
24
  {
1615
24
    int option_value = -1;
1616
24
    const socklen_t option_len = sizeof(option_value);
1617
24
    const int sr =
1618
24
        setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*)&option_value, option_len);
1619
24
    if (sr < 0)
1620
24
    {
1621
      /* local unix sockets don't have the TCP_NODELAY implemented, so don't make this
1622
       * error fatal */
1623
24
      char buffer[128] = WINPR_C_ARRAY_INIT;
1624
24
      WLog_Print(log, level, "can't set TCP_NODELAY (%s)",
1625
24
                 winpr_strerror(errno, buffer, sizeof(buffer)));
1626
24
      return FALSE;
1627
24
    }
1628
24
  }
1629
0
  else
1630
0
  {
1631
0
    WLog_Print(log, level, "Socket SOL_SOCKET|SO_TYPE %d unsupported", type);
1632
0
    return FALSE;
1633
0
  }
1634
0
  return TRUE;
1635
24
}