21#include <freerdp/config.h>
30#include <winpr/platform.h>
31#include <winpr/winsock.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <netinet/tcp.h>
48#ifdef WINPR_HAVE_POLL_H
52#include <sys/select.h>
55#if defined(__FreeBSD__) || defined(__OpenBSD__)
57#define SOL_TCP IPPROTO_TCP
63#define SOL_TCP IPPROTO_TCP
66#define TCP_KEEPIDLE TCP_KEEPALIVE
72#include <winpr/windows.h>
76#define SHUT_RDWR SD_BOTH
77#define close(_fd) closesocket(_fd)
81#include <freerdp/log.h>
83#include <winpr/stream.h>
86#include "../crypto/opensslcompat.h"
88#if defined(HAVE_AF_VSOCK_H)
90#include <linux/vm_sockets.h>
93#define TAG FREERDP_TAG("core")
101} WINPR_BIO_SIMPLE_SOCKET;
103static int transport_bio_simple_init(BIO* bio, SOCKET socket,
int shutdown);
104static int transport_bio_simple_uninit(BIO* bio);
106static int transport_bio_simple_write(BIO* bio,
const char* buf,
int size)
110 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
115 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
116 status = _send(ptr->socket, buf, size, 0);
120 error = WSAGetLastError();
122 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
123 (error == WSAEALREADY))
125 BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
129 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
136static int transport_bio_simple_read(BIO* bio,
char* buf,
int size)
140 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
145 BIO_clear_flags(bio, BIO_FLAGS_READ);
146 (void)WSAResetEvent(ptr->hEvent);
147 status = _recv(ptr->socket, buf, size, 0);
156 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
160 error = WSAGetLastError();
162 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
163 (error == WSAEALREADY))
165 BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
169 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
175static int transport_bio_simple_puts(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED
const char* str)
180static int transport_bio_simple_gets(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED
char* str,
181 WINPR_ATTR_UNUSED
int size)
186static long transport_bio_simple_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
189 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
193 case BIO_C_SET_SOCKET:
194 transport_bio_simple_uninit(bio);
195 transport_bio_simple_init(bio, (SOCKET)arg2, (
int)arg1);
197 case BIO_C_GET_SOCKET:
198 if (!BIO_get_init(bio) || !arg2)
201 *((SOCKET*)arg2) = ptr->socket;
203 case BIO_C_GET_EVENT:
204 if (!BIO_get_init(bio) || !arg2)
207 *((HANDLE*)arg2) = ptr->hEvent;
209 case BIO_C_SET_NONBLOCK:
213 flags = fcntl((
int)ptr->socket, F_GETFL);
219 (void)fcntl((
int)ptr->socket, F_SETFL, flags | O_NONBLOCK);
221 (
void)fcntl((
int)ptr->socket, F_SETFL, flags & ~(O_NONBLOCK));
228 case BIO_C_WAIT_READ:
230 int timeout = (int)arg1;
231 int sockfd = (int)ptr->socket;
232#ifdef WINPR_HAVE_POLL_H
233 struct pollfd pollset;
235 pollset.events = POLLIN;
240 status = poll(&pollset, 1, timeout);
241 }
while ((status < 0) && (errno == EINTR));
244 fd_set rset = WINPR_C_ARRAY_INIT;
245 struct timeval tv = WINPR_C_ARRAY_INIT;
247 FD_SET(sockfd, &rset);
251 tv.tv_sec = timeout / 1000;
252 tv.tv_usec = (timeout % 1000) * 1000;
257 status = select(sockfd + 1, &rset,
nullptr,
nullptr, timeout ? &tv : nullptr);
258 }
while ((status < 0) && (errno == EINTR));
267 case BIO_C_WAIT_WRITE:
269 int timeout = (int)arg1;
270 int sockfd = (int)ptr->socket;
271#ifdef WINPR_HAVE_POLL_H
272 struct pollfd pollset;
274 pollset.events = POLLOUT;
279 status = poll(&pollset, 1, timeout);
280 }
while ((status < 0) && (errno == EINTR));
283 fd_set rset = WINPR_C_ARRAY_INIT;
284 struct timeval tv = WINPR_C_ARRAY_INIT;
286 FD_SET(sockfd, &rset);
290 tv.tv_sec = timeout / 1000;
291 tv.tv_usec = (timeout % 1000) * 1000;
296 status = select(sockfd + 1,
nullptr, &rset,
nullptr, timeout ? &tv : nullptr);
297 }
while ((status < 0) && (errno == EINTR));
309 transport_bio_simple_uninit(bio);
310 transport_bio_simple_init(bio, (SOCKET) * ((
int*)arg2), (
int)arg1);
317 if (BIO_get_init(bio))
320 *((
int*)arg2) = (int)ptr->socket;
322 status = (int)ptr->socket;
327 case BIO_CTRL_GET_CLOSE:
328 status = BIO_get_shutdown(bio);
331 case BIO_CTRL_SET_CLOSE:
332 BIO_set_shutdown(bio, (
int)arg1);
349static int transport_bio_simple_init(BIO* bio, SOCKET socket,
int shutdown)
351 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
352 ptr->socket = socket;
353 BIO_set_shutdown(bio, shutdown);
354 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
355 BIO_set_init(bio, 1);
356 ptr->hEvent = WSACreateEvent();
362 if (WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
364 WLog_ERR(TAG,
"WSAEventSelect returned 0x%08x",
365 WINPR_CXX_COMPAT_CAST(
unsigned, WSAGetLastError()));
372static int transport_bio_simple_uninit(BIO* bio)
374 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
376 if (BIO_get_shutdown(bio))
378 if (BIO_get_init(bio) && ptr)
380 _shutdown(ptr->socket, SD_BOTH);
381 closesocket(ptr->socket);
386 if (ptr && ptr->hEvent)
388 (void)CloseHandle(ptr->hEvent);
389 ptr->hEvent =
nullptr;
392 BIO_set_init(bio, 0);
393 BIO_set_flags(bio, 0);
397static int transport_bio_simple_new(BIO* bio)
399 WINPR_BIO_SIMPLE_SOCKET* ptr =
nullptr;
400 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
401 ptr = (WINPR_BIO_SIMPLE_SOCKET*)calloc(1,
sizeof(WINPR_BIO_SIMPLE_SOCKET));
406 BIO_set_data(bio, ptr);
410static int transport_bio_simple_free(BIO* bio)
412 WINPR_BIO_SIMPLE_SOCKET* ptr =
nullptr;
417 transport_bio_simple_uninit(bio);
418 ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
422 BIO_set_data(bio,
nullptr);
429BIO_METHOD* BIO_s_simple_socket(
void)
431 static BIO_METHOD* bio_methods =
nullptr;
433 if (bio_methods ==
nullptr)
435 if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE,
"SimpleSocket")))
438 BIO_meth_set_write(bio_methods, transport_bio_simple_write);
439 BIO_meth_set_read(bio_methods, transport_bio_simple_read);
440 BIO_meth_set_puts(bio_methods, transport_bio_simple_puts);
441 BIO_meth_set_gets(bio_methods, transport_bio_simple_gets);
442 BIO_meth_set_ctrl(bio_methods, transport_bio_simple_ctrl);
443 BIO_meth_set_create(bio_methods, transport_bio_simple_new);
444 BIO_meth_set_destroy(bio_methods, transport_bio_simple_free);
458} WINPR_BIO_BUFFERED_SOCKET;
460static int transport_bio_buffered_write(BIO* bio,
const char* buf,
int num)
464 size_t committedBytes = 0;
465 DataChunk chunks[2] = WINPR_C_ARRAY_INIT;
466 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
467 BIO* next_bio =
nullptr;
474 ptr->writeBlocked = FALSE;
475 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
480 if (buf && (num > 0) && !ringbuffer_write(&ptr->xmitBuffer, (
const BYTE*)buf, (
size_t)num))
482 WLog_ERR(TAG,
"an error occurred when writing (num: %d)", num);
486 nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer));
487 next_bio = BIO_next(bio);
489 for (
int i = 0; i < nchunks; i++)
491 while (chunks[i].size)
495 const size_t wr = MIN(INT32_MAX, chunks[i].size);
496 const int status = BIO_write(next_bio, chunks[i].data, (
int)wr);
500 if (!BIO_should_retry(next_bio))
502 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
507 if (BIO_should_write(next_bio))
509 BIO_set_flags(bio, BIO_FLAGS_WRITE);
510 ptr->writeBlocked = TRUE;
516 committedBytes += (size_t)status;
517 chunks[i].size -= (size_t)status;
518 chunks[i].data += status;
524 ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes);
528static int transport_bio_buffered_read(BIO* bio,
char* buf,
int size)
531 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
532 BIO* next_bio = BIO_next(bio);
533 ptr->readBlocked = FALSE;
534 BIO_clear_flags(bio, BIO_FLAGS_READ);
536 status = BIO_read(next_bio, buf, size);
540 if (!BIO_should_retry(next_bio))
542 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
546 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
548 if (BIO_should_read(next_bio))
550 BIO_set_flags(bio, BIO_FLAGS_READ);
551 ptr->readBlocked = TRUE;
560static int transport_bio_buffered_puts(WINPR_ATTR_UNUSED BIO* bio,
561 WINPR_ATTR_UNUSED
const char* str)
566static int transport_bio_buffered_gets(WINPR_ATTR_UNUSED BIO* bio, WINPR_ATTR_UNUSED
char* str,
567 WINPR_ATTR_UNUSED
int size)
572static long transport_bio_buffered_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
575 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
580 if (!ringbuffer_used(&ptr->xmitBuffer))
583 status = (transport_bio_buffered_write(bio,
nullptr, 0) >= 0) ? 1 : -1;
587 case BIO_CTRL_WPENDING:
588 status = WINPR_ASSERTING_INT_CAST(
long, ringbuffer_used(&ptr->xmitBuffer));
591 case BIO_CTRL_PENDING:
595 case BIO_C_READ_BLOCKED:
596 status = (int)ptr->readBlocked;
599 case BIO_C_WRITE_BLOCKED:
600 status = (int)ptr->writeBlocked;
604 status = BIO_ctrl(BIO_next(bio), cmd, arg1, arg2);
611static int transport_bio_buffered_new(BIO* bio)
613 WINPR_BIO_BUFFERED_SOCKET* ptr =
nullptr;
614 BIO_set_init(bio, 1);
615 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
616 ptr = (WINPR_BIO_BUFFERED_SOCKET*)calloc(1,
sizeof(WINPR_BIO_BUFFERED_SOCKET));
621 BIO_set_data(bio, (
void*)ptr);
623 if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000))
632static int transport_bio_buffered_free(BIO* bio)
634 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
639 ringbuffer_destroy(&ptr->xmitBuffer);
644BIO_METHOD* BIO_s_buffered_socket(
void)
646 static BIO_METHOD* bio_methods =
nullptr;
648 if (bio_methods ==
nullptr)
650 if (!(bio_methods = BIO_meth_new(BIO_TYPE_BUFFERED,
"BufferedSocket")))
653 BIO_meth_set_write(bio_methods, transport_bio_buffered_write);
654 BIO_meth_set_read(bio_methods, transport_bio_buffered_read);
655 BIO_meth_set_puts(bio_methods, transport_bio_buffered_puts);
656 BIO_meth_set_gets(bio_methods, transport_bio_buffered_gets);
657 BIO_meth_set_ctrl(bio_methods, transport_bio_buffered_ctrl);
658 BIO_meth_set_create(bio_methods, transport_bio_buffered_new);
659 BIO_meth_set_destroy(bio_methods, transport_bio_buffered_free);
665char* freerdp_tcp_address_to_string(
const struct sockaddr_storage* addr, BOOL* pIPv6)
667 char ipAddress[INET6_ADDRSTRLEN + 1] = WINPR_C_ARRAY_INIT;
668 const struct sockaddr_in6* sockaddr_ipv6 = (
const struct sockaddr_in6*)addr;
669 const struct sockaddr_in* sockaddr_ipv4 = (
const struct sockaddr_in*)addr;
676 switch (sockaddr_ipv4->sin_family)
679 if (!inet_ntop(sockaddr_ipv4->sin_family, &sockaddr_ipv4->sin_addr, ipAddress,
686 if (!inet_ntop(sockaddr_ipv6->sin6_family, &sockaddr_ipv6->sin6_addr, ipAddress,
693 (void)sprintf_s(ipAddress, ARRAYSIZE(ipAddress),
"127.0.0.1");
700 if (pIPv6 !=
nullptr)
702 *pIPv6 = (sockaddr_ipv4->sin_family == AF_INET6);
705 return _strdup(ipAddress);
708static bool freerdp_tcp_get_ip_address(rdpSettings* settings,
int sockfd)
710 WINPR_ASSERT(settings);
712 struct sockaddr_storage saddr = WINPR_C_ARRAY_INIT;
713 socklen_t length =
sizeof(
struct sockaddr_storage);
719 if (getsockname(sockfd, (
struct sockaddr*)&saddr, &length) != 0)
721 settings->ClientAddress = freerdp_tcp_address_to_string(&saddr, &settings->IPv6Enabled);
722 return settings->ClientAddress !=
nullptr;
725char* freerdp_tcp_get_peer_address(SOCKET sockfd)
727 struct sockaddr_storage saddr = WINPR_C_ARRAY_INIT;
728 socklen_t length =
sizeof(
struct sockaddr_storage);
730 if (getpeername((
int)sockfd, (
struct sockaddr*)&saddr, &length) != 0)
735 return freerdp_tcp_address_to_string(&saddr,
nullptr);
738static int freerdp_uds_connect(
const char* path)
743 struct sockaddr_un addr = WINPR_C_ARRAY_INIT;
744 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
748 WLog_ERR(TAG,
"socket");
752 addr.sun_family = AF_UNIX;
753 strncpy(addr.sun_path, path,
sizeof(addr.sun_path) - 1);
754 status = connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr));
758 WLog_ERR(TAG,
"connect");
769struct addrinfo* freerdp_tcp_resolve_host(
const char* hostname,
int port,
int ai_flags)
771 char* service =
nullptr;
772 char port_str[16] = WINPR_C_ARRAY_INIT;
775 struct addrinfo* result =
nullptr;
776 struct addrinfo hints = { .ai_family = AF_UNSPEC,
777 .ai_socktype = SOCK_STREAM,
778 .ai_flags = ai_flags };
782 (void)sprintf_s(port_str,
sizeof(port_str) - 1,
"%d", port);
786 status = getaddrinfo(hostname, service, &hints, &result);
790 WLog_WARN(TAG,
"getaddrinfo(%s) failed with %s", hostname, gai_strerror(status));
797static BOOL freerdp_tcp_is_hostname_resolvable(rdpContext* context,
const char* hostname)
799 struct addrinfo* result = freerdp_tcp_resolve_host(hostname, -1, 0);
803 freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
808 freerdp_set_last_error_log(context, 0);
809 freeaddrinfo(result);
813static BOOL freerdp_tcp_connect_timeout(rdpContext* context,
int sockfd,
struct sockaddr* addr,
814 size_t addrlen, UINT32 timeout)
817 HANDLE handles[2] = WINPR_C_ARRAY_INIT;
820 DWORD tout = (timeout > 0) ? timeout : INFINITE;
822 handles[count] = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
827 const int wsastatus = WSAEventSelect((SOCKET)sockfd, handles[count++],
828 FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
832 WLog_ERR(TAG,
"WSAEventSelect failed with %d", WSAGetLastError());
836 handles[count++] = utils_get_abort_event(context->rdp);
839 const int constatus =
840 _connect((SOCKET)sockfd, addr, WINPR_ASSERTING_INT_CAST(
int, addrlen));
843 const int estatus = WSAGetLastError();
858 const DWORD wstatus = WaitForMultipleObjects(count, handles, FALSE, tout);
859 if (WAIT_OBJECT_0 != wstatus)
864 const int status = WSAEventSelect((SOCKET)sockfd, handles[0], 0);
867 WLog_ERR(TAG,
"WSAEventSelect failed with %d", WSAGetLastError());
872 if (_ioctlsocket((SOCKET)sockfd, FIONBIO, &arg) != 0)
879 socklen_t optlen =
sizeof(optval);
880 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
void*)&optval, &optlen) >= 0)
884 char ebuffer[256] = WINPR_C_ARRAY_INIT;
885 char hostname[512] = WINPR_C_ARRAY_INIT;
886 char serv[512] = WINPR_C_ARRAY_INIT;
888 getnameinfo(addr, WINPR_ASSERTING_INT_CAST(socklen_t, addrlen), hostname,
889 WINPR_ASSERTING_INT_CAST(socklen_t,
sizeof(hostname)), serv,
890 WINPR_ASSERTING_INT_CAST(socklen_t,
sizeof(serv)),
891 NI_NUMERICHOST | NI_NUMERICSERV);
892 WLog_WARN(TAG,
"connect to %s:%s failed with error: %s [%" PRId32
"]", hostname, serv,
893 winpr_strerror(optval, ebuffer,
sizeof(ebuffer)), optval);
899 (void)CloseHandle(handles[0]);
906 struct addrinfo* addr;
907 struct addrinfo* result;
910static void peer_free(t_peer* peer)
912 if (peer->s != INVALID_SOCKET)
913 closesocket(peer->s);
915 freeaddrinfo(peer->addr);
916 memset(peer, 0,
sizeof(t_peer));
917 peer->s = INVALID_SOCKET;
920static int freerdp_tcp_connect_multi(rdpContext* context,
char** hostnames,
const UINT32* ports,
921 UINT32 count, UINT16 port, WINPR_ATTR_UNUSED UINT32 timeout)
923 UINT32 sindex = count;
924 SOCKET sockfd = INVALID_SOCKET;
925 struct addrinfo* addr =
nullptr;
926 struct addrinfo* result =
nullptr;
928 HANDLE* events = (HANDLE*)calloc(count + 1,
sizeof(HANDLE));
929 t_peer* peers = (t_peer*)calloc(count,
sizeof(t_peer));
931 if (!peers || !events || (count < 1))
938 for (UINT32 index = 0; index < count; index++)
943 curPort = WINPR_ASSERTING_INT_CAST(
int, ports[index]);
945 result = freerdp_tcp_resolve_host(hostnames[index], curPort, 0);
952 if ((addr->ai_family == AF_INET6) && (addr->ai_next !=
nullptr))
954 while ((addr = addr->ai_next))
956 if (addr->ai_family == AF_INET)
964 peers[index].s = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
966 if (peers[index].s == INVALID_SOCKET)
968 freeaddrinfo(result);
972 peers[index].addr = addr;
973 peers[index].result = result;
976 for (UINT32 index = 0; index < count; index++)
978 sockfd = peers[index].s;
979 addr = peers[index].addr;
981 if ((sockfd == INVALID_SOCKET) || (!addr))
986 _connect(sockfd, addr->ai_addr, WINPR_ASSERTING_INT_CAST(
int, addr->ai_addrlen));
998 sockfd = peers[sindex].s;
999 peers[sindex].s = INVALID_SOCKET;
1002 freerdp_set_last_error_log(context, FREERDP_ERROR_CONNECT_CANCELLED);
1004 for (UINT32 index = 0; index < count; index++)
1005 peer_free(&peers[index]);
1008 free((
void*)events);
1012BOOL freerdp_tcp_set_keep_alive_mode(
const rdpSettings* settings,
int sockfd)
1016 socklen_t optlen = 0;
1020 socklen_t xoptlen =
sizeof(xoptval);
1021 const int rc = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (
void*)&xoptval, &xoptlen);
1024 isTcp = xoptval == SOCK_STREAM;
1026 optval = keepalive ? 1 : 0;
1027 optlen =
sizeof(optval);
1029 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
void*)&optval, optlen) < 0)
1031 WLog_WARN(TAG,
"setsockopt() SOL_SOCKET, SO_KEEPALIVE");
1040 optlen =
sizeof(optval);
1042 if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (
void*)&optval, optlen) < 0)
1044 WLog_WARN(TAG,
"setsockopt() IPPROTO_TCP, TCP_KEEPIDLE");
1054 optlen =
sizeof(optval);
1056 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (
void*)&optval, optlen) < 0)
1058 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_KEEPCNT");
1064 optlen =
sizeof(optval);
1066 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (
void*)&optval, optlen) < 0)
1068 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_KEEPINTVL");
1073#if defined(__MACOSX__) || defined(__IOS__)
1075 optlen =
sizeof(optval);
1077 if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (
void*)&optval, optlen) < 0)
1079 WLog_WARN(TAG,
"setsockopt() SOL_SOCKET, SO_NOSIGPIPE");
1083#ifdef TCP_USER_TIMEOUT
1085 optlen =
sizeof(optval);
1087 if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (
void*)&optval, optlen) < 0)
1089 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_USER_TIMEOUT");
1096int freerdp_tcp_connect(rdpContext* context,
const char* hostname,
int port, DWORD timeout)
1098 rdpTransport* transport =
nullptr;
1099 if (!context || !context->rdp)
1101 transport = context->rdp->transport;
1104 return transport_tcp_connect(context->rdp->transport, hostname, port, timeout);
1107static struct addrinfo* reorder_addrinfo_by_preference(rdpContext* context,
struct addrinfo* addr)
1109 WINPR_ASSERT(context);
1112 const BOOL preferIPv6 =
1117 struct addrinfo* ipv6Head =
nullptr;
1118 struct addrinfo* ipv6Tail =
nullptr;
1119 struct addrinfo* otherHead =
nullptr;
1120 struct addrinfo* otherTail =
nullptr;
1125 struct addrinfo* next = addr->ai_next;
1126 addr->ai_next =
nullptr;
1128 if (addr->ai_family == AF_INET6)
1133 ipv6Tail->ai_next = addr;
1141 otherTail->ai_next = addr;
1149 ipv6Tail->ai_next = otherHead;
1151 return ipv6Head ? ipv6Head : otherHead;
1154static int get_next_addrinfo(rdpContext* context,
struct addrinfo* input,
struct addrinfo** result,
1157 WINPR_ASSERT(context);
1158 WINPR_ASSERT(result);
1160 struct addrinfo* addr = input;
1172 const int family = (IPvX == 4) ? AF_INET : AF_INET6;
1173 while (addr && (addr->ai_family != family))
1174 addr = addr->ai_next;
1189 freerdp_set_last_error_if_not(context, errorCode);
1194static int freerdp_vsock_connect(rdpContext* context,
const char* hostname,
int port)
1196#if defined(HAVE_AF_VSOCK_H)
1197 int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
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);
1207 struct sockaddr_vm addr = WINPR_C_ARRAY_INIT;
1209 addr.svm_family = AF_VSOCK;
1210 addr.svm_port = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_port), port);
1213 char* ptr =
nullptr;
1214 unsigned long val = strtoul(hostname, &ptr, 10);
1215 if (errno || (val > UINT32_MAX))
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)));
1223 addr.svm_cid = WINPR_ASSERTING_INT_CAST(typeof(addr.svm_cid), val);
1224 if (addr.svm_cid == 2)
1226 addr.svm_flags = VMADDR_FLAG_TO_HOST;
1228 if ((connect(sockfd, (
struct sockaddr*)&addr,
sizeof(
struct sockaddr_vm))) == -1)
1230 WLog_ERR(TAG,
"failed to connect to %s", hostname);
1236 WLog_ERR(TAG,
"Compiled without AF_VSOCK, '%s' not supported", hostname);
1241static void log_connection_address(
const char* hostname,
struct addrinfo* addr)
1245 char* peerAddress = freerdp_tcp_address_to_string(
1246 WINPR_PACKED_ALIGN_CAST(
const struct sockaddr_storage*, addr->ai_addr),
nullptr);
1248 WLog_DBG(TAG,
"resolved %s: try to connect to %s", hostname, peerAddress);
1252static int freerdp_host_connect(rdpContext* context,
const char* hostname,
int port, DWORD timeout)
1255 struct addrinfo* addr =
nullptr;
1256 struct addrinfo* result = freerdp_tcp_resolve_host(hostname, port, 0);
1260 freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1263 freerdp_set_last_error_log(context, 0);
1269 result = reorder_addrinfo_by_preference(context, result);
1271 const int rc = get_next_addrinfo(context, result, &addr, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1277 sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
1280 log_connection_address(hostname, addr);
1282 if (!freerdp_tcp_connect_timeout(context, sockfd, addr->ai_addr, addr->ai_addrlen,
1293 get_next_addrinfo(context, addr->ai_next, &addr, FREERDP_ERROR_CONNECT_FAILED);
1297 }
while (sockfd < 0);
1300 freeaddrinfo(result);
1304int freerdp_tcp_default_connect(rdpContext* context, rdpSettings* settings,
const char* hostname,
1305 int port, DWORD timeout)
1308 BOOL ipcSocket = FALSE;
1309 BOOL useExternalDefinedSocket = FALSE;
1313 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1318 if (hostname[0] ==
'/')
1321 if (hostname[0] ==
'|')
1322 useExternalDefinedSocket = TRUE;
1324 const char* vsock = utils_is_vsock(hostname);
1327 sockfd = freerdp_uds_connect(hostname);
1331 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1335 else if (useExternalDefinedSocket)
1338 sockfd = freerdp_vsock_connect(context, vsock, port);
1341 if (!settings->GatewayEnabled)
1343 if (!freerdp_tcp_is_hostname_resolvable(context, hostname) ||
1344 settings->RemoteAssistanceMode)
1346 if (settings->TargetNetAddressCount > 0)
1348 WINPR_ASSERT(port <= UINT16_MAX);
1349 sockfd = freerdp_tcp_connect_multi(
1350 context, settings->TargetNetAddresses, settings->TargetNetPorts,
1351 settings->TargetNetAddressCount, (UINT16)port, timeout);
1357 sockfd = freerdp_host_connect(context, hostname, port, timeout);
1362 if (!freerdp_tcp_get_ip_address(settings, sockfd))
1364 if (!useExternalDefinedSocket)
1367 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1369 WLog_ERR(TAG,
"Couldn't get socket ip address");
1374 if (!ipcSocket && !useExternalDefinedSocket)
1376 (void)freerdp_tcp_set_nodelay(WLog_Get(TAG), WLOG_ERROR, sockfd);
1381 socklen_t optlen =
sizeof(optval);
1382 if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (
void*)&optval, &optlen) == 0)
1384 if (optval < (1024 * 32))
1387 optlen =
sizeof(optval);
1389 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (
void*)&optval, optlen) < 0)
1393 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1395 WLog_ERR(TAG,
"unable to set receive buffer len");
1401 if (!ipcSocket && !useExternalDefinedSocket)
1403 if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1407 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1409 WLog_ERR(TAG,
"Couldn't set keep alive mode.");
1414 if (WaitForSingleObject(utils_get_abort_event(context->rdp), 0) == WAIT_OBJECT_0)
1428typedef struct rdp_tcp_layer rdpTcpLayer;
1430static int freerdp_tcp_layer_read(
void* userContext,
void* data,
int bytes)
1434 if (!data || !bytes)
1437 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1439 (void)WSAResetEvent(tcpLayer->hEvent);
1440 const int status = _recv((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1445 const int error = WSAGetLastError();
1447 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1448 (error == WSAEALREADY))
1454static int freerdp_tcp_layer_write(
void* userContext,
const void* data,
int bytes)
1458 if (!data || !bytes)
1461 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1463 const int status = _send((SOCKET)tcpLayer->sockfd, data, bytes, 0);
1467 const int error = WSAGetLastError();
1469 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1470 (error == WSAEALREADY))
1476static BOOL freerdp_tcp_layer_close(
void* userContext)
1481 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1483 if (tcpLayer->sockfd >= 0)
1484 closesocket((SOCKET)tcpLayer->sockfd);
1485 if (tcpLayer->hEvent)
1486 (void)CloseHandle(tcpLayer->hEvent);
1491static BOOL freerdp_tcp_layer_wait(
void* userContext, BOOL waitWrite, DWORD timeout)
1496 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1499 int sockfd = tcpLayer->sockfd;
1500#ifdef WINPR_HAVE_POLL_H
1501 struct pollfd pollset = WINPR_C_ARRAY_INIT;
1502 pollset.fd = sockfd;
1503 pollset.events = waitWrite ? POLLOUT : POLLIN;
1507 status = poll(&pollset, 1, (
int)timeout);
1508 }
while ((status < 0) && (errno == EINTR));
1511 fd_set rset = WINPR_C_ARRAY_INIT;
1512 struct timeval tv = WINPR_C_ARRAY_INIT;
1514 FD_SET(sockfd, &rset);
1518 tv.tv_sec = timeout / 1000;
1519 tv.tv_usec = (timeout % 1000) * 1000;
1525 status = select(sockfd + 1,
nullptr, &rset,
nullptr, timeout ? &tv : nullptr);
1527 status = select(sockfd + 1, &rset,
nullptr,
nullptr, timeout ? &tv : nullptr);
1528 }
while ((status < 0) && (errno == EINTR));
1535static HANDLE freerdp_tcp_layer_get_event(
void* userContext)
1540 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1542 return tcpLayer->hEvent;
1545rdpTransportLayer* freerdp_tcp_connect_layer(rdpContext* context,
const char* hostname,
int port,
1548 WINPR_ASSERT(context);
1550 const rdpSettings* settings = context->settings;
1551 WINPR_ASSERT(settings);
1554 rdpTcpLayer* tcpLayer =
nullptr;
1556 int sockfd = freerdp_tcp_connect(context, hostname, port, timeout);
1559 if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1562 layer = transport_layer_new(freerdp_get_transport(context),
sizeof(rdpTcpLayer));
1566 layer->Read = freerdp_tcp_layer_read;
1567 layer->Write = freerdp_tcp_layer_write;
1568 layer->Close = freerdp_tcp_layer_close;
1569 layer->Wait = freerdp_tcp_layer_wait;
1570 layer->GetEvent = freerdp_tcp_layer_get_event;
1572 tcpLayer = (rdpTcpLayer*)layer->userContext;
1573 WINPR_ASSERT(tcpLayer);
1575 tcpLayer->sockfd = -1;
1576 tcpLayer->hEvent = WSACreateEvent();
1577 if (!tcpLayer->hEvent)
1581 if (WSAEventSelect((SOCKET)sockfd, tcpLayer->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
1583 WLog_ERR(TAG,
"WSAEventSelect returned 0x%08x", (
unsigned)WSAGetLastError());
1587 tcpLayer->sockfd = sockfd;
1593 closesocket((SOCKET)sockfd);
1594 transport_layer_free(layer);
1598BOOL freerdp_tcp_set_nodelay(wLog* log, DWORD level,
int sockfd)
1603 socklen_t typelen =
sizeof(type);
1604 char* ptype = (
char*)&type;
1605 const int rc = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, ptype, &typelen);
1608 char buffer[128] = WINPR_C_ARRAY_INIT;
1609 WLog_Print(log, level,
"can't get SOL_SOCKET|SO_TYPE (%s)",
1610 winpr_strerror(errno, buffer,
sizeof(buffer)));
1613 else if (type == SOCK_STREAM)
1615 int option_value = -1;
1616 const socklen_t option_len =
sizeof(option_value);
1618 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (
void*)&option_value, option_len);
1623 char buffer[128] = WINPR_C_ARRAY_INIT;
1624 WLog_Print(log, level,
"can't set TCP_NODELAY (%s)",
1625 winpr_strerror(errno, buffer,
sizeof(buffer)));
1631 WLog_Print(log, level,
"Socket SOL_SOCKET|SO_TYPE %d unsupported", type);
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
a piece of data in the ring buffer, exactly like a glibc iovec