21 #include <freerdp/config.h>
29 #include <winpr/crt.h>
30 #include <winpr/platform.h>
31 #include <winpr/winsock.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
45 #include <sys/types.h>
46 #include <arpa/inet.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>
74 #include <winpr/crt.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;
103 static int transport_bio_simple_init(BIO* bio, SOCKET socket,
int shutdown);
104 static int transport_bio_simple_uninit(BIO* bio);
106 static long transport_bio_simple_callback(BIO* bio,
int mode,
const char* argp,
int argi,
long argl,
112 static int transport_bio_simple_write(BIO* bio,
const char* buf,
int size)
116 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
121 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
122 status = _send(ptr->socket, buf, size, 0);
126 error = WSAGetLastError();
128 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
129 (error == WSAEALREADY))
131 BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
135 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
142 static int transport_bio_simple_read(BIO* bio,
char* buf,
int size)
146 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
151 BIO_clear_flags(bio, BIO_FLAGS_READ);
152 (void)WSAResetEvent(ptr->hEvent);
153 status = _recv(ptr->socket, buf, size, 0);
162 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
166 error = WSAGetLastError();
168 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
169 (error == WSAEALREADY))
171 BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
175 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
181 static int transport_bio_simple_puts(BIO* bio,
const char* str)
186 static int transport_bio_simple_gets(BIO* bio,
char* str,
int size)
191 static long transport_bio_simple_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
194 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
198 case BIO_C_SET_SOCKET:
199 transport_bio_simple_uninit(bio);
200 transport_bio_simple_init(bio, (SOCKET)arg2, (
int)arg1);
202 case BIO_C_GET_SOCKET:
203 if (!BIO_get_init(bio) || !arg2)
206 *((SOCKET*)arg2) = ptr->socket;
208 case BIO_C_GET_EVENT:
209 if (!BIO_get_init(bio) || !arg2)
212 *((HANDLE*)arg2) = ptr->hEvent;
214 case BIO_C_SET_NONBLOCK:
218 flags = fcntl((
int)ptr->socket, F_GETFL);
224 (void)fcntl((
int)ptr->socket, F_SETFL, flags | O_NONBLOCK);
226 (
void)fcntl((
int)ptr->socket, F_SETFL, flags & ~(O_NONBLOCK));
233 case BIO_C_WAIT_READ:
235 int timeout = (int)arg1;
236 int sockfd = (int)ptr->socket;
237 #ifdef WINPR_HAVE_POLL_H
238 struct pollfd pollset;
240 pollset.events = POLLIN;
245 status = poll(&pollset, 1, timeout);
246 }
while ((status < 0) && (errno == EINTR));
250 struct timeval tv = { 0 };
252 FD_SET(sockfd, &rset);
256 tv.tv_sec = timeout / 1000;
257 tv.tv_usec = (timeout % 1000) * 1000;
262 status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
263 }
while ((status < 0) && (errno == EINTR));
272 case BIO_C_WAIT_WRITE:
274 int timeout = (int)arg1;
275 int sockfd = (int)ptr->socket;
276 #ifdef WINPR_HAVE_POLL_H
277 struct pollfd pollset;
279 pollset.events = POLLOUT;
284 status = poll(&pollset, 1, timeout);
285 }
while ((status < 0) && (errno == EINTR));
289 struct timeval tv = { 0 };
291 FD_SET(sockfd, &rset);
295 tv.tv_sec = timeout / 1000;
296 tv.tv_usec = (timeout % 1000) * 1000;
301 status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL);
302 }
while ((status < 0) && (errno == EINTR));
314 transport_bio_simple_uninit(bio);
315 transport_bio_simple_init(bio, (SOCKET) * ((
int*)arg2), (
int)arg1);
322 if (BIO_get_init(bio))
325 *((
int*)arg2) = (int)ptr->socket;
327 status = (
int)ptr->socket;
332 case BIO_CTRL_GET_CLOSE:
333 status = BIO_get_shutdown(bio);
336 case BIO_CTRL_SET_CLOSE:
337 BIO_set_shutdown(bio, (
int)arg1);
354 static int transport_bio_simple_init(BIO* bio, SOCKET socket,
int shutdown)
356 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
357 ptr->socket = socket;
358 BIO_set_shutdown(bio, shutdown);
359 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
360 BIO_set_init(bio, 1);
361 ptr->hEvent = WSACreateEvent();
367 if (WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
369 WLog_ERR(TAG,
"WSAEventSelect returned 0x%08X", WSAGetLastError());
376 static int transport_bio_simple_uninit(BIO* bio)
378 WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
380 if (BIO_get_shutdown(bio))
382 if (BIO_get_init(bio) && ptr)
384 _shutdown(ptr->socket, SD_BOTH);
385 closesocket(ptr->socket);
390 if (ptr && ptr->hEvent)
392 (void)CloseHandle(ptr->hEvent);
396 BIO_set_init(bio, 0);
397 BIO_set_flags(bio, 0);
401 static int transport_bio_simple_new(BIO* bio)
403 WINPR_BIO_SIMPLE_SOCKET* ptr = NULL;
404 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
405 ptr = (WINPR_BIO_SIMPLE_SOCKET*)calloc(1,
sizeof(WINPR_BIO_SIMPLE_SOCKET));
410 BIO_set_data(bio, ptr);
414 static int transport_bio_simple_free(BIO* bio)
416 WINPR_BIO_SIMPLE_SOCKET* ptr = NULL;
421 transport_bio_simple_uninit(bio);
422 ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
426 BIO_set_data(bio, NULL);
433 BIO_METHOD* BIO_s_simple_socket(
void)
435 static BIO_METHOD* bio_methods = NULL;
437 if (bio_methods == NULL)
439 if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE,
"SimpleSocket")))
442 BIO_meth_set_write(bio_methods, transport_bio_simple_write);
443 BIO_meth_set_read(bio_methods, transport_bio_simple_read);
444 BIO_meth_set_puts(bio_methods, transport_bio_simple_puts);
445 BIO_meth_set_gets(bio_methods, transport_bio_simple_gets);
446 BIO_meth_set_ctrl(bio_methods, transport_bio_simple_ctrl);
447 BIO_meth_set_create(bio_methods, transport_bio_simple_new);
448 BIO_meth_set_destroy(bio_methods, transport_bio_simple_free);
462 } WINPR_BIO_BUFFERED_SOCKET;
464 static long transport_bio_buffered_callback(BIO* bio,
int mode,
const char* argp,
int argi,
470 static int transport_bio_buffered_write(BIO* bio,
const char* buf,
int num)
474 size_t committedBytes = 0;
476 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
477 BIO* next_bio = NULL;
482 ptr->writeBlocked = FALSE;
483 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
488 if (buf && num && !ringbuffer_write(&ptr->xmitBuffer, (
const BYTE*)buf, num))
490 WLog_ERR(TAG,
"an error occurred when writing (num: %d)", num);
494 nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer));
495 next_bio = BIO_next(bio);
497 for (
int i = 0; i < nchunks; i++)
499 while (chunks[i].size)
503 const size_t wr = MIN(INT32_MAX, chunks[i].size);
504 const int status = BIO_write(next_bio, chunks[i].data, (
int)wr);
508 if (!BIO_should_retry(next_bio))
510 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
515 if (BIO_should_write(next_bio))
517 BIO_set_flags(bio, BIO_FLAGS_WRITE);
518 ptr->writeBlocked = TRUE;
524 committedBytes += (size_t)status;
525 chunks[i].size -= (size_t)status;
526 chunks[i].data += status;
532 ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes);
536 static int transport_bio_buffered_read(BIO* bio,
char* buf,
int size)
539 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
540 BIO* next_bio = BIO_next(bio);
541 ptr->readBlocked = FALSE;
542 BIO_clear_flags(bio, BIO_FLAGS_READ);
544 status = BIO_read(next_bio, buf, size);
548 if (!BIO_should_retry(next_bio))
550 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
554 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
556 if (BIO_should_read(next_bio))
558 BIO_set_flags(bio, BIO_FLAGS_READ);
559 ptr->readBlocked = TRUE;
568 static int transport_bio_buffered_puts(BIO* bio,
const char* str)
573 static int transport_bio_buffered_gets(BIO* bio,
char* str,
int size)
578 static long transport_bio_buffered_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
581 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
586 if (!ringbuffer_used(&ptr->xmitBuffer))
589 status = (transport_bio_buffered_write(bio, NULL, 0) >= 0) ? 1 : -1;
593 case BIO_CTRL_WPENDING:
594 status = ringbuffer_used(&ptr->xmitBuffer);
597 case BIO_CTRL_PENDING:
601 case BIO_C_READ_BLOCKED:
602 status = (int)ptr->readBlocked;
605 case BIO_C_WRITE_BLOCKED:
606 status = (
int)ptr->writeBlocked;
610 status = BIO_ctrl(BIO_next(bio), cmd, arg1, arg2);
617 static int transport_bio_buffered_new(BIO* bio)
619 WINPR_BIO_BUFFERED_SOCKET* ptr = NULL;
620 BIO_set_init(bio, 1);
621 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
622 ptr = (WINPR_BIO_BUFFERED_SOCKET*)calloc(1,
sizeof(WINPR_BIO_BUFFERED_SOCKET));
627 BIO_set_data(bio, (
void*)ptr);
629 if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000))
638 static int transport_bio_buffered_free(BIO* bio)
640 WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
645 ringbuffer_destroy(&ptr->xmitBuffer);
650 BIO_METHOD* BIO_s_buffered_socket(
void)
652 static BIO_METHOD* bio_methods = NULL;
654 if (bio_methods == NULL)
656 if (!(bio_methods = BIO_meth_new(BIO_TYPE_BUFFERED,
"BufferedSocket")))
659 BIO_meth_set_write(bio_methods, transport_bio_buffered_write);
660 BIO_meth_set_read(bio_methods, transport_bio_buffered_read);
661 BIO_meth_set_puts(bio_methods, transport_bio_buffered_puts);
662 BIO_meth_set_gets(bio_methods, transport_bio_buffered_gets);
663 BIO_meth_set_ctrl(bio_methods, transport_bio_buffered_ctrl);
664 BIO_meth_set_create(bio_methods, transport_bio_buffered_new);
665 BIO_meth_set_destroy(bio_methods, transport_bio_buffered_free);
671 char* freerdp_tcp_address_to_string(
const struct sockaddr_storage* addr, BOOL* pIPv6)
673 char ipAddress[INET6_ADDRSTRLEN + 1] = { 0 };
674 const struct sockaddr_in6* sockaddr_ipv6 = (
const struct sockaddr_in6*)addr;
675 const struct sockaddr_in* sockaddr_ipv4 = (
const struct sockaddr_in*)addr;
682 switch (sockaddr_ipv4->sin_family)
685 if (!inet_ntop(sockaddr_ipv4->sin_family, &sockaddr_ipv4->sin_addr, ipAddress,
692 if (!inet_ntop(sockaddr_ipv6->sin6_family, &sockaddr_ipv6->sin6_addr, ipAddress,
699 (void)sprintf_s(ipAddress, ARRAYSIZE(ipAddress),
"127.0.0.1");
708 *pIPv6 = (sockaddr_ipv4->sin_family == AF_INET6);
711 return _strdup(ipAddress);
714 static char* freerdp_tcp_get_ip_address(
int sockfd, BOOL* pIPv6)
716 struct sockaddr_storage saddr = { 0 };
717 socklen_t length =
sizeof(
struct sockaddr_storage);
719 if (getsockname(sockfd, (
struct sockaddr*)&saddr, &length) != 0)
724 return freerdp_tcp_address_to_string(&saddr, pIPv6);
727 char* freerdp_tcp_get_peer_address(SOCKET sockfd)
729 struct sockaddr_storage saddr = { 0 };
730 socklen_t length =
sizeof(
struct sockaddr_storage);
732 if (getpeername((
int)sockfd, (
struct sockaddr*)&saddr, &length) != 0)
737 return freerdp_tcp_address_to_string(&saddr, NULL);
740 static int freerdp_uds_connect(
const char* path)
745 struct sockaddr_un addr = { 0 };
746 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
750 WLog_ERR(TAG,
"socket");
754 addr.sun_family = AF_UNIX;
755 strncpy(addr.sun_path, path,
sizeof(addr.sun_path) - 1);
756 status = connect(sockfd, (
struct sockaddr*)&addr,
sizeof(addr));
760 WLog_ERR(TAG,
"connect");
771 struct addrinfo* freerdp_tcp_resolve_host(
const char* hostname,
int port,
int ai_flags)
773 char* service = NULL;
776 struct addrinfo hints = { 0 };
777 struct addrinfo* result = NULL;
778 hints.ai_family = AF_UNSPEC;
779 hints.ai_socktype = SOCK_STREAM;
780 hints.ai_flags = ai_flags;
784 (void)sprintf_s(port_str,
sizeof(port_str) - 1,
"%d", port);
788 status = getaddrinfo(hostname, service, &hints, &result);
796 static BOOL freerdp_tcp_is_hostname_resolvable(rdpContext* context,
const char* hostname)
798 struct addrinfo* result = freerdp_tcp_resolve_host(hostname, -1, 0);
802 freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
807 freerdp_set_last_error_log(context, 0);
808 freeaddrinfo(result);
812 static BOOL freerdp_tcp_connect_timeout(rdpContext* context,
int sockfd,
struct sockaddr* addr,
813 socklen_t addrlen, UINT32 timeout)
820 DWORD tout = (timeout > 0) ? timeout : INFINITE;
822 handles[count] = CreateEvent(NULL, TRUE, FALSE, NULL);
827 status = WSAEventSelect(sockfd, handles[count++], FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
831 WLog_ERR(TAG,
"WSAEventSelect failed with %d", WSAGetLastError());
835 handles[count++] = utils_get_abort_event(context->rdp);
836 status = _connect(sockfd, addr, addrlen);
840 status = WSAGetLastError();
853 status = WaitForMultipleObjects(count, handles, FALSE, tout);
855 if (WAIT_OBJECT_0 != status)
858 const SSIZE_T res = recv(sockfd, NULL, 0, 0);
860 if (res == SOCKET_ERROR)
862 if (WSAGetLastError() == WSAECONNRESET)
866 status = WSAEventSelect(sockfd, handles[0], 0);
870 WLog_ERR(TAG,
"WSAEventSelect failed with %d", WSAGetLastError());
874 if (_ioctlsocket(sockfd, FIONBIO, &arg) != 0)
879 (void)CloseHandle(handles[0]);
886 struct addrinfo* addr;
887 struct addrinfo* result;
890 static void peer_free(t_peer* peer)
892 if (peer->s != INVALID_SOCKET)
893 closesocket(peer->s);
895 freeaddrinfo(peer->addr);
896 memset(peer, 0,
sizeof(t_peer));
897 peer->s = INVALID_SOCKET;
900 static int freerdp_tcp_connect_multi(rdpContext* context,
char** hostnames,
const UINT32* ports,
901 UINT32 count, UINT16 port, UINT32 timeout)
903 UINT32 sindex = count;
905 SOCKET sockfd = INVALID_SOCKET;
906 struct addrinfo* addr = NULL;
907 struct addrinfo* result = NULL;
909 HANDLE* events = (HANDLE*)calloc(count + 1,
sizeof(HANDLE));
910 t_peer* peers = (t_peer*)calloc(count,
sizeof(t_peer));
912 if (!peers || !events || (count < 1))
919 for (UINT32 index = 0; index < count; index++)
924 curPort = ports[index];
926 result = freerdp_tcp_resolve_host(hostnames[index], curPort, 0);
933 if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0))
935 while ((addr = addr->ai_next))
937 if (addr->ai_family == AF_INET)
945 peers[index].s = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
947 if (peers[index].s == INVALID_SOCKET)
949 freeaddrinfo(result);
953 peers[index].addr = addr;
954 peers[index].result = result;
957 for (UINT32 index = 0; index < count; index++)
959 sockfd = peers[index].s;
960 addr = peers[index].addr;
962 if ((sockfd == INVALID_SOCKET) || (!addr))
966 status = _connect(sockfd, addr->ai_addr, addr->ai_addrlen);
978 sockfd = peers[sindex].s;
979 peers[sindex].s = INVALID_SOCKET;
982 freerdp_set_last_error_log(context, FREERDP_ERROR_CONNECT_CANCELLED);
984 for (UINT32 index = 0; index < count; index++)
985 peer_free(&peers[index]);
992 BOOL freerdp_tcp_set_keep_alive_mode(
const rdpSettings* settings,
int sockfd)
996 socklen_t optlen = 0;
997 optval = keepalive ? 1 : 0;
998 optlen =
sizeof(optval);
1000 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
void*)&optval, optlen) < 0)
1002 WLog_WARN(TAG,
"setsockopt() SOL_SOCKET, SO_KEEPALIVE");
1008 optlen =
sizeof(optval);
1010 if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (
void*)&optval, optlen) < 0)
1012 WLog_WARN(TAG,
"setsockopt() IPPROTO_TCP, TCP_KEEPIDLE");
1022 optlen =
sizeof(optval);
1024 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (
void*)&optval, optlen) < 0)
1026 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_KEEPCNT");
1030 #ifdef TCP_KEEPINTVL
1032 optlen =
sizeof(optval);
1034 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (
void*)&optval, optlen) < 0)
1036 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_KEEPINTVL");
1041 #if defined(__MACOSX__) || defined(__IOS__)
1043 optlen =
sizeof(optval);
1045 if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (
void*)&optval, optlen) < 0)
1047 WLog_WARN(TAG,
"setsockopt() SOL_SOCKET, SO_NOSIGPIPE");
1051 #ifdef TCP_USER_TIMEOUT
1053 optlen =
sizeof(optval);
1055 if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (
void*)&optval, optlen) < 0)
1057 WLog_WARN(TAG,
"setsockopt() SOL_TCP, TCP_USER_TIMEOUT");
1064 int freerdp_tcp_connect(rdpContext* context,
const char* hostname,
int port, DWORD timeout)
1066 rdpTransport* transport = NULL;
1067 if (!context || !context->rdp)
1069 transport = context->rdp->transport;
1072 return transport_tcp_connect(context->rdp->transport, hostname, port, timeout);
1075 static int get_next_addrinfo(rdpContext* context,
struct addrinfo* input,
struct addrinfo** result,
1078 WINPR_ASSERT(context);
1079 WINPR_ASSERT(result);
1081 struct addrinfo* addr = input;
1087 while (addr && (addr->ai_family != AF_INET6))
1088 addr = addr->ai_next;
1100 const int family = (IPvX == 4) ? AF_INET : AF_INET6;
1101 while (addr && (addr->ai_family != family))
1102 addr = addr->ai_next;
1118 freerdp_set_last_error_if_not(context, errorCode);
1119 freeaddrinfo(input);
1123 int freerdp_tcp_default_connect(rdpContext* context, rdpSettings* settings,
const char* hostname,
1124 int port, DWORD timeout)
1128 socklen_t optlen = 0;
1129 BOOL ipcSocket = FALSE;
1130 BOOL useExternalDefinedSocket = FALSE;
1134 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1139 if (hostname[0] ==
'/')
1142 if (hostname[0] ==
'|')
1143 useExternalDefinedSocket = TRUE;
1145 const char* vsock = utils_is_vsock(hostname);
1148 sockfd = freerdp_uds_connect(hostname);
1152 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1157 else if (useExternalDefinedSocket)
1161 #if defined(HAVE_AF_VSOCK_H)
1163 sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
1164 struct sockaddr_vm addr = { 0 };
1166 addr.svm_family = AF_VSOCK;
1167 addr.svm_port = port;
1171 unsigned long val = strtoul(hostname, &ptr, 10);
1172 if (errno || (val > UINT32_MAX))
1174 char ebuffer[256] = { 0 };
1175 WLog_ERR(TAG,
"could not extract port from '%s', value=%ul, error=%s", hostname, val,
1176 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
1180 if (addr.svm_cid == 2)
1182 addr.svm_flags = VMADDR_FLAG_TO_HOST;
1184 if ((connect(sockfd, (
struct sockaddr*)&addr,
sizeof(
struct sockaddr_vm))) == -1)
1186 WLog_ERR(TAG,
"failed to connect to %s", hostname);
1190 WLog_ERR(TAG,
"Compiled without AF_VSOCK, '%s' not supported", hostname);
1198 if (!settings->GatewayEnabled)
1200 if (!freerdp_tcp_is_hostname_resolvable(context, hostname) ||
1201 settings->RemoteAssistanceMode)
1203 if (settings->TargetNetAddressCount > 0)
1205 WINPR_ASSERT(port <= UINT16_MAX);
1206 sockfd = freerdp_tcp_connect_multi(
1207 context, settings->TargetNetAddresses, settings->TargetNetPorts,
1208 settings->TargetNetAddressCount, (UINT16)port, timeout);
1215 char* peerAddress = NULL;
1216 struct addrinfo* addr = NULL;
1217 struct addrinfo* result = NULL;
1219 result = freerdp_tcp_resolve_host(hostname, port, 0);
1223 freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1227 freerdp_set_last_error_log(context, 0);
1235 get_next_addrinfo(context, result, &addr, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
1241 sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
1244 const int lrc = get_next_addrinfo(context, addr->ai_next, &addr,
1245 FREERDP_ERROR_CONNECT_FAILED);
1249 }
while (sockfd < 0);
1251 if ((peerAddress = freerdp_tcp_address_to_string(
1252 (
const struct sockaddr_storage*)addr->ai_addr, NULL)) != NULL)
1254 WLog_DBG(TAG,
"connecting to peer %s", peerAddress);
1258 if (!freerdp_tcp_connect_timeout(context, sockfd, addr->ai_addr, addr->ai_addrlen,
1261 freeaddrinfo(result);
1264 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1266 WLog_ERR(TAG,
"failed to connect to %s", hostname);
1270 freeaddrinfo(result);
1276 free(settings->ClientAddress);
1277 settings->ClientAddress = freerdp_tcp_get_ip_address(sockfd, &settings->IPv6Enabled);
1279 if (!settings->ClientAddress)
1281 if (!useExternalDefinedSocket)
1284 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1286 WLog_ERR(TAG,
"Couldn't get socket ip address");
1292 optlen =
sizeof(optval);
1294 if (!ipcSocket && !useExternalDefinedSocket)
1296 if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (
void*)&optval, optlen) < 0)
1297 WLog_ERR(TAG,
"unable to set TCP_NODELAY");
1301 if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (
void*)&optval, &optlen) == 0)
1303 if (optval < (1024 * 32))
1306 optlen =
sizeof(optval);
1308 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (
void*)&optval, optlen) < 0)
1312 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1314 WLog_ERR(TAG,
"unable to set receive buffer len");
1320 if (!ipcSocket && !useExternalDefinedSocket)
1322 if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1326 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
1328 WLog_ERR(TAG,
"Couldn't set keep alive mode.");
1333 if (WaitForSingleObject(utils_get_abort_event(context->rdp), 0) == WAIT_OBJECT_0)
1342 struct rdp_tcp_layer
1347 typedef struct rdp_tcp_layer rdpTcpLayer;
1349 static int freerdp_tcp_layer_read(
void* userContext,
void* data,
int bytes)
1353 if (!data || !bytes)
1356 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1361 (void)WSAResetEvent(tcpLayer->hEvent);
1362 status = _recv(tcpLayer->sockfd, data, bytes, 0);
1370 error = WSAGetLastError();
1372 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1373 (error == WSAEALREADY))
1385 static int freerdp_tcp_layer_write(
void* userContext,
const void* data,
int bytes)
1389 if (!data || !bytes)
1392 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1397 status = _send(tcpLayer->sockfd, data, bytes, 0);
1401 error = WSAGetLastError();
1403 if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
1404 (error == WSAEALREADY))
1417 static BOOL freerdp_tcp_layer_close(
void* userContext)
1422 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1424 if (tcpLayer->sockfd >= 0)
1425 closesocket(tcpLayer->sockfd);
1426 if (tcpLayer->hEvent)
1427 (void)CloseHandle(tcpLayer->hEvent);
1432 static BOOL freerdp_tcp_layer_wait(
void* userContext, BOOL waitWrite, DWORD timeout)
1437 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1440 int sockfd = tcpLayer->sockfd;
1441 #ifdef WINPR_HAVE_POLL_H
1442 struct pollfd pollset = { 0 };
1443 pollset.fd = sockfd;
1444 pollset.events = waitWrite ? POLLOUT : POLLIN;
1448 status = poll(&pollset, 1, (
int)timeout);
1449 }
while ((status < 0) && (errno == EINTR));
1452 fd_set rset = { 0 };
1453 struct timeval tv = { 0 };
1455 FD_SET(sockfd, &rset);
1459 tv.tv_sec = timeout / 1000;
1460 tv.tv_usec = (timeout % 1000) * 1000;
1466 status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL);
1468 status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
1469 }
while ((status < 0) && (errno == EINTR));
1476 static HANDLE freerdp_tcp_layer_get_event(
void* userContext)
1481 rdpTcpLayer* tcpLayer = (rdpTcpLayer*)userContext;
1483 return tcpLayer->hEvent;
1486 rdpTransportLayer* freerdp_tcp_connect_layer(rdpContext* context,
const char* hostname,
int port,
1489 WINPR_ASSERT(context);
1491 const rdpSettings* settings = context->settings;
1492 WINPR_ASSERT(settings);
1495 rdpTcpLayer* tcpLayer = NULL;
1497 int sockfd = freerdp_tcp_connect(context, hostname, port, timeout);
1500 if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
1503 layer = transport_layer_new(freerdp_get_transport(context),
sizeof(rdpTcpLayer));
1507 layer->Read = freerdp_tcp_layer_read;
1508 layer->Write = freerdp_tcp_layer_write;
1509 layer->Close = freerdp_tcp_layer_close;
1510 layer->Wait = freerdp_tcp_layer_wait;
1511 layer->GetEvent = freerdp_tcp_layer_get_event;
1513 tcpLayer = (rdpTcpLayer*)layer->userContext;
1514 WINPR_ASSERT(tcpLayer);
1516 tcpLayer->sockfd = -1;
1517 tcpLayer->hEvent = WSACreateEvent();
1518 if (!tcpLayer->hEvent)
1522 if (WSAEventSelect(sockfd, tcpLayer->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
1524 WLog_ERR(TAG,
"WSAEventSelect returned 0x%08X", WSAGetLastError());
1528 tcpLayer->sockfd = sockfd;
1534 closesocket(sockfd);
1535 transport_layer_free(layer);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
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