20 #include <freerdp/config.h>
24 #include <winpr/assert.h>
26 #include <winpr/crt.h>
27 #include <winpr/synch.h>
28 #include <winpr/print.h>
29 #include <winpr/stream.h>
30 #include <winpr/winsock.h>
31 #include <winpr/crypto.h>
33 #include <freerdp/log.h>
34 #include <freerdp/error.h>
35 #include <freerdp/utils/ringbuffer.h>
37 #include <openssl/bio.h>
44 #include <sys/socket.h>
47 #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
48 #include <valgrind/memcheck.h>
53 #include "transport.h"
58 #include "childsession.h"
60 #include "gateway/rdg.h"
61 #include "gateway/wst.h"
62 #include "gateway/arm.h"
64 #define TAG FREERDP_TAG("core.transport")
66 #define BUFFER_SIZE 16384
70 TRANSPORT_LAYER layer;
80 TransportRecv ReceiveCallback;
81 wStreamPool* ReceivePool;
82 HANDLE connectedEvent;
92 BOOL haveMoreBytesToRead;
100 static void transport_ssl_cb(
const SSL* ssl,
int where,
int ret)
102 if (where & SSL_CB_ALERT)
104 rdpTransport* transport = (rdpTransport*)SSL_get_app_data(ssl);
105 WINPR_ASSERT(transport);
109 case (SSL3_AL_FATAL << 8) | SSL_AD_ACCESS_DENIED:
111 if (!freerdp_get_last_error(transport_get_context(transport)))
113 WLog_Print(transport->log, WLOG_ERROR,
"ACCESS DENIED");
114 freerdp_set_last_error_log(transport_get_context(transport),
115 FREERDP_ERROR_AUTHENTICATION_FAILED);
120 case (SSL3_AL_FATAL << 8) | SSL_AD_INTERNAL_ERROR:
122 if (transport->NlaMode)
124 if (!freerdp_get_last_error(transport_get_context(transport)))
128 kret = nla_get_error(transport->nla);
130 kret = FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED;
131 freerdp_set_last_error_log(transport_get_context(transport), kret);
137 case (SSL3_AL_WARNING << 8) | SSL3_AD_CLOSE_NOTIFY:
141 WLog_Print(transport->log, WLOG_WARN,
142 "Unhandled SSL error (where=%d, ret=%d [%s, %s])", where, ret,
143 SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
150 wStream* transport_send_stream_init(rdpTransport* transport,
size_t size)
153 WINPR_ASSERT(transport);
155 if (!(s = StreamPool_Take(transport->ReceivePool, size)))
158 if (!Stream_EnsureCapacity(s, size))
164 Stream_SetPosition(s, 0);
168 BOOL transport_attach(rdpTransport* transport,
int sockfd)
172 return IFCALLRESULT(FALSE, transport->io.TransportAttach, transport, sockfd);
175 static BOOL transport_default_attach(rdpTransport* transport,
int sockfd)
177 BIO* socketBio = NULL;
178 BIO* bufferedBio = NULL;
179 const rdpSettings* settings = NULL;
180 rdpContext* context = transport_get_context(transport);
184 WLog_WARN(TAG,
"Running peer without socket (sockfd=%d)", sockfd);
188 settings = context->settings;
189 WINPR_ASSERT(settings);
193 if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
196 socketBio = BIO_new(BIO_s_simple_socket());
202 bufferedBio = BIO_new(BIO_s_buffered_socket());
208 bufferedBio = BIO_push(bufferedBio, socketBio);
217 BIO_set_fd(socketBio, sockfd, BIO_CLOSE);
219 transport->frontBio = bufferedBio;
224 BIO_free_all(socketBio);
231 BOOL transport_connect_rdp(rdpTransport* transport)
236 switch (utils_authenticate(transport_get_context(transport)->instance, AUTH_RDP, FALSE))
240 case AUTH_NO_CREDENTIALS:
243 freerdp_set_last_error_if_not(transport_get_context(transport),
244 FREERDP_ERROR_CONNECT_CANCELLED);
251 BOOL transport_connect_tls(rdpTransport* transport)
253 const rdpSettings* settings = NULL;
254 rdpContext* context = transport_get_context(transport);
256 settings = context->settings;
257 WINPR_ASSERT(settings);
260 if (settings->SelectedProtocol == PROTOCOL_SSL)
262 switch (utils_authenticate(context->instance, AUTH_TLS, FALSE))
266 case AUTH_NO_CREDENTIALS:
269 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
276 return IFCALLRESULT(FALSE, transport->io.TLSConnect, transport);
279 static BOOL transport_default_connect_tls(rdpTransport* transport)
283 rdpContext* context = NULL;
284 rdpSettings* settings = NULL;
286 WINPR_ASSERT(transport);
288 context = transport_get_context(transport);
289 WINPR_ASSERT(context);
291 settings = context->settings;
292 WINPR_ASSERT(settings);
294 if (!(tls = freerdp_tls_new(context)))
297 transport->tls = tls;
299 if (transport->GatewayEnabled)
300 transport->layer = TRANSPORT_LAYER_TSG_TLS;
302 transport->layer = TRANSPORT_LAYER_TLS;
304 tls->hostname = settings->ServerHostname;
305 tls->serverName = settings->UserSpecifiedServerName;
306 tls->port = MIN(UINT16_MAX, settings->ServerPort);
311 tls->isGatewayTransport = FALSE;
312 tlsStatus = freerdp_tls_connect(tls, transport->frontBio);
318 freerdp_set_last_error_if_not(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
322 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
328 transport->frontBio = tls->bio;
337 typedef void (*ssl_cb_t)(
const SSL* ssl,
int type,
int val);
338 ssl_cb_t fkt = transport_ssl_cb;
340 BIO_info_cb* bfkt = WINPR_FUNC_PTR_CAST(fkt, BIO_info_cb*);
341 BIO_callback_ctrl(tls->bio, BIO_CTRL_SET_CALLBACK, bfkt);
342 SSL_set_app_data(tls->ssl, transport);
344 if (!transport->frontBio)
346 WLog_Print(transport->log, WLOG_ERROR,
"unable to prepend a filtering TLS bio");
353 BOOL transport_connect_nla(rdpTransport* transport, BOOL earlyUserAuth)
355 rdpContext* context = NULL;
356 rdpSettings* settings = NULL;
361 context = transport_get_context(transport);
362 WINPR_ASSERT(context);
364 settings = context->settings;
365 WINPR_ASSERT(settings);
370 if (!transport_connect_tls(transport))
373 if (!settings->Authentication)
377 rdp->nla = nla_new(context, transport);
382 nla_set_early_user_auth(rdp->nla, earlyUserAuth);
384 transport_set_nla_mode(transport, TRUE);
386 if (settings->AuthenticationServiceClass)
388 if (!nla_set_service_principal(rdp->nla, settings->AuthenticationServiceClass,
393 if (nla_client_begin(rdp->nla) < 0)
395 WLog_Print(transport->log, WLOG_ERROR,
"NLA begin failed");
397 freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
399 transport_set_nla_mode(transport, FALSE);
403 return rdp_client_transition_to_state(rdp, CONNECTION_STATE_NLA);
406 BOOL transport_connect_rdstls(rdpTransport* transport)
409 rdpRdstls* rdstls = NULL;
410 rdpContext* context = NULL;
412 WINPR_ASSERT(transport);
414 context = transport_get_context(transport);
415 WINPR_ASSERT(context);
417 if (!transport_connect_tls(transport))
420 rdstls = rdstls_new(context, transport);
424 transport_set_rdstls_mode(transport, TRUE);
426 if (rdstls_authenticate(rdstls) < 0)
428 WLog_Print(transport->log, WLOG_ERROR,
"RDSTLS authentication failed");
429 freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
433 transport_set_rdstls_mode(transport, FALSE);
440 BOOL transport_connect_aad(rdpTransport* transport)
442 rdpContext* context = NULL;
443 rdpSettings* settings = NULL;
448 context = transport_get_context(transport);
449 WINPR_ASSERT(context);
451 settings = context->settings;
452 WINPR_ASSERT(settings);
457 if (!transport_connect_tls(transport))
460 if (!settings->Authentication)
466 transport_set_aad_mode(transport, TRUE);
468 if (aad_client_begin(rdp->aad) < 0)
470 WLog_Print(transport->log, WLOG_ERROR,
"AAD begin failed");
472 freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
474 transport_set_aad_mode(transport, FALSE);
478 return rdp_client_transition_to_state(rdp, CONNECTION_STATE_AAD);
481 BOOL transport_connect(rdpTransport* transport,
const char* hostname, UINT16 port, DWORD timeout)
484 rdpSettings* settings = NULL;
485 rdpContext* context = transport_get_context(transport);
486 BOOL rpcFallback = 0;
488 WINPR_ASSERT(context);
489 WINPR_ASSERT(hostname);
491 settings = context->settings;
492 WINPR_ASSERT(settings);
494 rpcFallback = !settings->GatewayHttpTransport;
496 if (transport->GatewayEnabled)
498 if (settings->GatewayUrl)
500 WINPR_ASSERT(!transport->wst);
501 transport->wst = wst_new(context);
506 status = wst_connect(transport->wst, timeout);
510 transport->frontBio = wst_get_front_bio_and_take_ownership(transport->wst);
511 WINPR_ASSERT(transport->frontBio);
512 BIO_set_nonblock(transport->frontBio, 0);
513 transport->layer = TRANSPORT_LAYER_TSG;
518 wst_free(transport->wst);
519 transport->wst = NULL;
522 if (!status && settings->GatewayHttpTransport)
524 WINPR_ASSERT(!transport->rdg);
525 transport->rdg = rdg_new(context);
530 status = rdg_connect(transport->rdg, timeout, &rpcFallback);
534 transport->frontBio = rdg_get_front_bio_and_take_ownership(transport->rdg);
535 WINPR_ASSERT(transport->frontBio);
536 BIO_set_nonblock(transport->frontBio, 0);
537 transport->layer = TRANSPORT_LAYER_TSG;
542 rdg_free(transport->rdg);
543 transport->rdg = NULL;
547 if (!status && settings->GatewayRpcTransport && rpcFallback)
549 WINPR_ASSERT(!transport->tsg);
550 transport->tsg = tsg_new(transport);
556 freerdp_set_last_error_log(context, FREERDP_ERROR_SUCCESS);
557 status = tsg_connect(transport->tsg, hostname, port, timeout);
561 transport->frontBio = tsg_get_bio(transport->tsg);
562 transport->layer = TRANSPORT_LAYER_TSG;
567 tsg_free(transport->tsg);
568 transport->tsg = NULL;
575 const char* proxyHostname = NULL;
576 const char* proxyUsername = NULL;
577 const char* proxyPassword = NULL;
578 BOOL isProxyConnection =
579 proxy_prepare(settings, &proxyHostname, &peerPort, &proxyUsername, &proxyPassword);
582 if (isProxyConnection)
583 layer = transport_connect_layer(transport, proxyHostname, peerPort, timeout);
585 layer = transport_connect_layer(transport, hostname, port, timeout);
590 if (!transport_attach_layer(transport, layer))
592 transport_layer_free(layer);
596 if (isProxyConnection)
598 if (!proxy_connect(context, transport->frontBio, proxyUsername, proxyPassword, hostname,
609 BOOL transport_connect_childsession(rdpTransport* transport)
611 WINPR_ASSERT(transport);
613 transport->frontBio = createChildSessionBio();
614 if (!transport->frontBio)
617 transport->layer = TRANSPORT_LAYER_TSG;
621 BOOL transport_accept_rdp(rdpTransport* transport)
629 BOOL transport_accept_tls(rdpTransport* transport)
633 return IFCALLRESULT(FALSE, transport->io.TLSAccept, transport);
636 static BOOL transport_default_accept_tls(rdpTransport* transport)
638 rdpContext* context = transport_get_context(transport);
639 rdpSettings* settings = NULL;
641 WINPR_ASSERT(context);
643 settings = context->settings;
644 WINPR_ASSERT(settings);
647 transport->tls = freerdp_tls_new(context);
649 transport->layer = TRANSPORT_LAYER_TLS;
651 if (!freerdp_tls_accept(transport->tls, transport->frontBio, settings))
654 transport->frontBio = transport->tls->bio;
658 BOOL transport_accept_nla(rdpTransport* transport)
660 rdpContext* context = transport_get_context(transport);
661 rdpSettings* settings = NULL;
663 WINPR_ASSERT(context);
665 settings = context->settings;
666 WINPR_ASSERT(settings);
668 if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
673 if (!settings->Authentication)
678 transport->nla = nla_new(context, transport);
679 transport_set_nla_mode(transport, TRUE);
682 if (nla_authenticate(transport->nla) < 0)
684 WLog_Print(transport->log, WLOG_ERROR,
"client authentication failure");
685 transport_set_nla_mode(transport, FALSE);
686 nla_free(transport->nla);
687 transport->nla = NULL;
688 freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
689 TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
690 freerdp_tls_send_alert(transport->tls);
695 transport_set_nla_mode(transport, FALSE);
699 BOOL transport_accept_rdstls(rdpTransport* transport)
702 rdpRdstls* rdstls = NULL;
703 rdpContext* context = NULL;
705 WINPR_ASSERT(transport);
707 context = transport_get_context(transport);
708 WINPR_ASSERT(context);
710 if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
713 rdstls = rdstls_new(context, transport);
717 transport_set_rdstls_mode(transport, TRUE);
719 if (rdstls_authenticate(rdstls) < 0)
721 WLog_Print(transport->log, WLOG_ERROR,
"client authentication failure");
722 freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
723 TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
724 freerdp_tls_send_alert(transport->tls);
728 transport_set_rdstls_mode(transport, FALSE);
735 #define WLog_ERR_BIO(transport, biofunc, bio) \
736 transport_bio_error_log(transport, biofunc, bio, __FILE__, __func__, __LINE__)
738 static void transport_bio_error_log(rdpTransport* transport, LPCSTR biofunc, BIO* bio, LPCSTR file,
739 LPCSTR func, DWORD line)
741 unsigned long sslerr = 0;
745 WINPR_ASSERT(transport);
750 if (level < WLog_GetLogLevel(transport->log))
753 if (ERR_peek_error() == 0)
755 char ebuffer[256] = { 0 };
756 const char* fmt =
"%s returned a system error %d: %s";
758 fmt =
"%s retries exceeded";
759 WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
760 saveerrno, winpr_strerror(saveerrno, ebuffer,
sizeof(ebuffer)));
764 while ((sslerr = ERR_get_error()))
766 char buf[120] = { 0 };
767 const char* fmt =
"%s returned an error: %s";
769 ERR_error_string_n(sslerr, buf, 120);
770 WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
775 static SSIZE_T transport_read_layer(rdpTransport* transport, BYTE* data,
size_t bytes)
779 rdpContext* context = NULL;
781 WINPR_ASSERT(transport);
783 context = transport_get_context(transport);
784 WINPR_ASSERT(context);
789 if (!transport->frontBio || (bytes > SSIZE_MAX))
791 transport->layer = TRANSPORT_LAYER_CLOSED;
792 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
796 while (read < (SSIZE_T)bytes)
798 const SSIZE_T tr = (SSIZE_T)bytes - read;
799 int r = (int)((tr > INT_MAX) ? INT_MAX : tr);
801 int status = BIO_read(transport->frontBio, data + read, r);
803 if (freerdp_shall_disconnect_context(context))
808 if (!transport->frontBio || !BIO_should_retry(transport->frontBio))
811 if (!transport->frontBio)
813 WLog_Print(transport->log, WLOG_ERROR,
"BIO_read: transport->frontBio null");
817 WLog_ERR_BIO(transport,
"BIO_read", transport->frontBio);
818 transport->layer = TRANSPORT_LAYER_CLOSED;
819 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
824 if (!transport->blocking)
829 if (BIO_wait_read(transport->frontBio, 100) < 0)
831 WLog_ERR_BIO(transport,
"BIO_wait_read", transport->frontBio);
838 #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
839 VALGRIND_MAKE_MEM_DEFINED(data + read, bytes - read);
842 rdp->inBytes += status;
862 static SSIZE_T transport_read_layer_bytes(rdpTransport* transport,
wStream* s,
size_t toRead)
868 if (toRead > SSIZE_MAX)
871 status = IFCALLRESULT(-1, transport->io.ReadBytes, transport, Stream_Pointer(s), toRead);
876 Stream_Seek(s, (
size_t)status);
877 return status == (SSIZE_T)toRead ? 1 : 0;
892 int transport_read_pdu(rdpTransport* transport,
wStream* s)
896 return IFCALLRESULT(-1, transport->io.ReadPdu, transport, s);
899 static SSIZE_T parse_nla_mode_pdu(rdpTransport* transport,
wStream* stream)
901 SSIZE_T pduLength = 0;
903 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
910 UINT8 typeEncoding = 0;
911 if (Stream_GetRemainingLength(s) < 1)
913 Stream_Read_UINT8(s, typeEncoding);
914 if (typeEncoding == 0x30)
917 UINT8 lengthEncoding = 0;
918 if (Stream_GetRemainingLength(s) < 1)
920 Stream_Read_UINT8(s, lengthEncoding);
921 if (lengthEncoding & 0x80)
923 if ((lengthEncoding & ~(0x80)) == 1)
926 if (Stream_GetRemainingLength(s) < 1)
928 Stream_Read_UINT8(s, length);
932 else if ((lengthEncoding & ~(0x80)) == 2)
936 if (Stream_GetRemainingLength(s) < 2)
938 Stream_Read_UINT16_BE(s, length);
944 WLog_Print(transport->log, WLOG_ERROR,
"Error reading TSRequest!");
950 pduLength = lengthEncoding;
958 static SSIZE_T parse_default_mode_pdu(rdpTransport* transport,
wStream* stream)
960 SSIZE_T pduLength = 0;
962 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
965 if (Stream_GetRemainingLength(s) < 1)
967 Stream_Read_UINT8(s, version);
972 if (Stream_GetRemainingLength(s) < 3)
975 Stream_Read_UINT16_BE(s, length);
979 if ((pduLength < 7) || (pduLength > 0xFFFF))
981 WLog_Print(transport->log, WLOG_ERROR,
"tpkt - invalid pduLength: %" PRIdz, pduLength);
989 if (Stream_GetRemainingLength(s) < 1)
991 Stream_Read_UINT8(s, length1);
995 if (Stream_GetRemainingLength(s) < 1)
997 Stream_Read_UINT8(s, length2);
998 pduLength = ((length1 & 0x7F) << 8) | length2;
1001 pduLength = length1;
1008 if (pduLength < 3 || pduLength > 0x8000)
1010 WLog_Print(transport->log, WLOG_ERROR,
"fast path - invalid pduLength: %" PRIdz,
1019 SSIZE_T transport_parse_pdu(rdpTransport* transport,
wStream* s, BOOL* incomplete)
1021 size_t pduLength = 0;
1032 Stream_SealLength(s);
1033 if (transport->NlaMode)
1034 pduLength = parse_nla_mode_pdu(transport, s);
1035 else if (transport->RdstlsMode)
1036 pduLength = rdstls_parse_pdu(transport->log, s);
1038 pduLength = parse_default_mode_pdu(transport, s);
1043 if (pduLength > SSIZE_MAX)
1046 const size_t len = Stream_Length(s);
1047 if (len > pduLength)
1051 *incomplete = len < pduLength;
1053 return (
int)pduLength;
1056 static int transport_default_read_pdu(rdpTransport* transport,
wStream* s)
1058 BOOL incomplete = 0;
1060 size_t pduLength = 0;
1061 size_t position = 0;
1063 WINPR_ASSERT(transport);
1068 if (transport->AadMode)
1073 const SSIZE_T rc = transport_read_layer(transport, &c, 1);
1075 return (rc == 0) ? 0 : -1;
1076 if (!Stream_EnsureRemainingCapacity(s, 1))
1078 Stream_Write_UINT8(s, c);
1079 }
while (c !=
'\0');
1081 else if (transport->earlyUserAuth)
1083 if (!Stream_EnsureCapacity(s, 4))
1085 const SSIZE_T rc = transport_read_layer_bytes(transport, s, 4);
1087 return (rc == 0) ? 0 : -1;
1092 status = transport_parse_pdu(transport, s, &incomplete);
1093 while ((status == 0) && incomplete)
1095 if (!Stream_EnsureRemainingCapacity(s, 1))
1097 SSIZE_T rc = transport_read_layer_bytes(transport, s, 1);
1102 status = transport_parse_pdu(transport, s, &incomplete);
1108 pduLength = (size_t)status;
1111 if (!Stream_EnsureCapacity(s, pduLength))
1114 position = Stream_GetPosition(s);
1115 if (position > pduLength)
1118 status = transport_read_layer_bytes(transport, s, pduLength - Stream_GetPosition(s));
1122 if ((status < INT32_MIN) || (status > INT32_MAX))
1127 if (Stream_GetPosition(s) >= pduLength)
1128 WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), pduLength,
1129 WLOG_PACKET_INBOUND);
1132 Stream_SealLength(s);
1133 Stream_SetPosition(s, 0);
1134 const size_t len = Stream_Length(s);
1135 if (len > INT32_MAX)
1140 int transport_write(rdpTransport* transport,
wStream* s)
1145 return IFCALLRESULT(-1, transport->io.WritePdu, transport, s);
1148 static int transport_default_write(rdpTransport* transport,
wStream* s)
1151 rdpContext* context = transport_get_context(transport);
1153 WINPR_ASSERT(transport);
1154 WINPR_ASSERT(context);
1161 rdpRdp* rdp = context->rdp;
1165 EnterCriticalSection(&(transport->WriteLock));
1166 if (!transport->frontBio)
1169 size_t length = Stream_GetPosition(s);
1170 size_t writtenlength = length;
1171 Stream_SetPosition(s, 0);
1175 rdp->outBytes += length;
1176 WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), length, WLOG_PACKET_OUTBOUND);
1182 const int towrite = (length > INT32_MAX) ? INT32_MAX : (
int)length;
1183 status = BIO_write(transport->frontBio, Stream_ConstPointer(s), towrite);
1191 if (!BIO_should_retry(transport->frontBio))
1193 WLog_ERR_BIO(transport,
"BIO_should_retry", transport->frontBio);
1198 if (!transport->blocking)
1200 WLog_ERR_BIO(transport,
"BIO_write", transport->frontBio);
1204 if (BIO_wait_write(transport->frontBio, 100) < 0)
1206 WLog_ERR_BIO(transport,
"BIO_wait_write", transport->frontBio);
1214 WINPR_ASSERT(context->settings);
1215 if (transport->blocking || context->settings->WaitForOutputBufferFlush)
1217 while (BIO_write_blocked(transport->frontBio))
1219 if (BIO_wait_write(transport->frontBio, 100) < 0)
1221 WLog_Print(transport->log, WLOG_ERROR,
"error when selecting for write");
1226 if (BIO_flush(transport->frontBio) < 1)
1228 WLog_Print(transport->log, WLOG_ERROR,
"error when flushing outputBuffer");
1235 length -= (size_t)status;
1236 Stream_Seek(s, (
size_t)status);
1239 transport->written += writtenlength;
1245 transport->layer = TRANSPORT_LAYER_CLOSED;
1246 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
1249 LeaveCriticalSection(&(transport->WriteLock));
1255 BOOL transport_get_public_key(rdpTransport* transport,
const BYTE** data, DWORD* length)
1257 return IFCALLRESULT(FALSE, transport->io.GetPublicKey, transport, data, length);
1260 static BOOL transport_default_get_public_key(rdpTransport* transport,
const BYTE** data,
1263 rdpTls* tls = transport_get_tls(transport);
1267 *data = tls->PublicKey;
1268 *length = tls->PublicKeyLength;
1273 DWORD transport_get_event_handles(rdpTransport* transport, HANDLE* events, DWORD count)
1277 WINPR_ASSERT(transport);
1278 WINPR_ASSERT(events);
1279 WINPR_ASSERT(count > 0);
1285 WLog_Print(transport->log, WLOG_ERROR,
"provided handles array is too small");
1289 events[nCount++] = transport->rereadEvent;
1291 if (transport->useIoEvent)
1295 events[nCount++] = transport->ioEvent;
1299 if (!transport->GatewayEnabled)
1303 if (nCount >= count)
1305 WLog_Print(transport->log, WLOG_ERROR,
1306 "provided handles array is too small (count=%" PRIu32
" nCount=%" PRIu32
1312 if (transport->frontBio)
1314 if (BIO_get_event(transport->frontBio, &events[nCount]) != 1)
1316 WLog_Print(transport->log, WLOG_ERROR,
"error getting the frontBio handle");
1328 rdg_get_event_handles(transport->rdg, &events[nCount], count - nCount);
1335 else if (transport->tsg)
1338 tsg_get_event_handles(transport->tsg, &events[nCount], count - nCount);
1345 else if (transport->wst)
1348 wst_get_event_handles(transport->wst, &events[nCount], count - nCount);
1360 #if defined(WITH_FREERDP_DEPRECATED)
1361 void transport_get_fds(rdpTransport* transport,
void** rfds,
int* rcount)
1364 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
1366 WINPR_ASSERT(transport);
1368 WINPR_ASSERT(rcount);
1370 nCount = transport_get_event_handles(transport, events, ARRAYSIZE(events));
1371 *rcount = nCount + 1;
1373 for (DWORD index = 0; index < nCount; index++)
1375 rfds[index] = GetEventWaitObject(events[index]);
1378 rfds[nCount] = GetEventWaitObject(transport->rereadEvent);
1382 BOOL transport_is_write_blocked(rdpTransport* transport)
1384 WINPR_ASSERT(transport);
1385 WINPR_ASSERT(transport->frontBio);
1386 return BIO_write_blocked(transport->frontBio) != 0;
1389 int transport_drain_output_buffer(rdpTransport* transport)
1391 BOOL status = FALSE;
1393 WINPR_ASSERT(transport);
1394 WINPR_ASSERT(transport->frontBio);
1395 if (BIO_write_blocked(transport->frontBio))
1397 if (BIO_flush(transport->frontBio) < 1)
1400 const long rc = BIO_write_blocked(transport->frontBio);
1407 int transport_check_fds(rdpTransport* transport)
1410 state_run_t recv_status = STATE_RUN_FAILED;
1412 rdpContext* context = transport_get_context(transport);
1414 WINPR_ASSERT(context);
1416 if (transport->layer == TRANSPORT_LAYER_CLOSED)
1418 WLog_Print(transport->log, WLOG_DEBUG,
"transport_check_fds: transport layer closed");
1419 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
1432 if ((status = transport_read_pdu(transport, transport->ReceiveBuffer)) <= 0)
1435 WLog_Print(transport->log, WLOG_DEBUG,
"transport_check_fds: transport_read_pdu() - %i",
1437 if (transport->haveMoreBytesToRead)
1439 transport->haveMoreBytesToRead = FALSE;
1440 (void)ResetEvent(transport->rereadEvent);
1445 received = transport->ReceiveBuffer;
1447 if (!(transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0)))
1456 WINPR_ASSERT(transport->ReceiveCallback);
1457 recv_status = transport->ReceiveCallback(transport, received, transport->ReceiveExtra);
1458 Stream_Release(received);
1460 if (state_run_failed(recv_status))
1462 char buffer[64] = { 0 };
1463 WLog_Print(transport->log, WLOG_ERROR,
1464 "transport_check_fds: transport->ReceiveCallback() - %s",
1465 state_run_result_string(recv_status, buffer, ARRAYSIZE(buffer)));
1472 if (!transport->haveMoreBytesToRead)
1474 transport->haveMoreBytesToRead = TRUE;
1475 (void)SetEvent(transport->rereadEvent);
1480 BOOL transport_set_blocking_mode(rdpTransport* transport, BOOL blocking)
1482 WINPR_ASSERT(transport);
1484 return IFCALLRESULT(FALSE, transport->io.SetBlockingMode, transport, blocking);
1487 static BOOL transport_default_set_blocking_mode(rdpTransport* transport, BOOL blocking)
1489 WINPR_ASSERT(transport);
1491 transport->blocking = blocking;
1493 if (transport->frontBio)
1495 if (!BIO_set_nonblock(transport->frontBio, blocking ? FALSE : TRUE))
1502 rdpTransportLayer* transport_connect_layer(rdpTransport* transport,
const char* hostname,
int port,
1505 WINPR_ASSERT(transport);
1507 return IFCALLRESULT(NULL, transport->io.ConnectLayer, transport, hostname, port, timeout);
1510 static rdpTransportLayer* transport_default_connect_layer(rdpTransport* transport,
1511 const char* hostname,
int port,
1514 rdpContext* context = transport_get_context(transport);
1515 WINPR_ASSERT(context);
1517 return freerdp_tcp_connect_layer(context, hostname, port, timeout);
1522 WINPR_ASSERT(transport);
1523 WINPR_ASSERT(layer);
1525 return IFCALLRESULT(FALSE, transport->io.AttachLayer, transport, layer);
1528 static BOOL transport_default_attach_layer(rdpTransport* transport,
rdpTransportLayer* layer)
1530 BIO* layerBio = BIO_new(BIO_s_transport_layer());
1534 BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
1538 bufferedBio = BIO_push(bufferedBio, layerBio);
1543 BIO_set_data(layerBio, layer);
1545 transport->frontBio = bufferedBio;
1551 BIO_free_all(layerBio);
1556 void transport_set_gateway_enabled(rdpTransport* transport, BOOL GatewayEnabled)
1558 WINPR_ASSERT(transport);
1559 transport->GatewayEnabled = GatewayEnabled;
1562 void transport_set_nla_mode(rdpTransport* transport, BOOL NlaMode)
1564 WINPR_ASSERT(transport);
1565 transport->NlaMode = NlaMode;
1568 void transport_set_rdstls_mode(rdpTransport* transport, BOOL RdstlsMode)
1570 WINPR_ASSERT(transport);
1571 transport->RdstlsMode = RdstlsMode;
1574 void transport_set_aad_mode(rdpTransport* transport, BOOL AadMode)
1576 WINPR_ASSERT(transport);
1577 transport->AadMode = AadMode;
1580 BOOL transport_disconnect(rdpTransport* transport)
1584 return IFCALLRESULT(FALSE, transport->io.TransportDisconnect, transport);
1587 static BOOL transport_default_disconnect(rdpTransport* transport)
1596 freerdp_tls_free(transport->tls);
1597 transport->tls = NULL;
1601 if (transport->frontBio)
1602 BIO_free_all(transport->frontBio);
1607 tsg_free(transport->tsg);
1608 transport->tsg = NULL;
1613 rdg_free(transport->rdg);
1614 transport->rdg = NULL;
1619 wst_free(transport->wst);
1620 transport->wst = NULL;
1623 transport->frontBio = NULL;
1624 transport->layer = TRANSPORT_LAYER_TCP;
1625 transport->earlyUserAuth = FALSE;
1629 rdpTransport* transport_new(rdpContext* context)
1631 rdpTransport* transport = (rdpTransport*)calloc(1,
sizeof(rdpTransport));
1633 WINPR_ASSERT(context);
1637 transport->log = WLog_Get(TAG);
1639 if (!transport->log)
1643 transport->io.TCPConnect = freerdp_tcp_default_connect;
1644 transport->io.TLSConnect = transport_default_connect_tls;
1645 transport->io.TLSAccept = transport_default_accept_tls;
1646 transport->io.TransportAttach = transport_default_attach;
1647 transport->io.TransportDisconnect = transport_default_disconnect;
1648 transport->io.ReadPdu = transport_default_read_pdu;
1649 transport->io.WritePdu = transport_default_write;
1650 transport->io.ReadBytes = transport_read_layer;
1651 transport->io.GetPublicKey = transport_default_get_public_key;
1652 transport->io.SetBlockingMode = transport_default_set_blocking_mode;
1653 transport->io.ConnectLayer = transport_default_connect_layer;
1654 transport->io.AttachLayer = transport_default_attach_layer;
1656 transport->context = context;
1657 transport->ReceivePool = StreamPool_New(TRUE, BUFFER_SIZE);
1659 if (!transport->ReceivePool)
1663 transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0);
1665 if (!transport->ReceiveBuffer)
1668 transport->connectedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1670 if (!transport->connectedEvent || transport->connectedEvent == INVALID_HANDLE_VALUE)
1673 transport->rereadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1675 if (!transport->rereadEvent || transport->rereadEvent == INVALID_HANDLE_VALUE)
1678 transport->ioEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1680 if (!transport->ioEvent || transport->ioEvent == INVALID_HANDLE_VALUE)
1683 transport->haveMoreBytesToRead = FALSE;
1684 transport->blocking = TRUE;
1685 transport->GatewayEnabled = FALSE;
1686 transport->layer = TRANSPORT_LAYER_TCP;
1688 if (!InitializeCriticalSectionAndSpinCount(&(transport->ReadLock), 4000))
1691 if (!InitializeCriticalSectionAndSpinCount(&(transport->WriteLock), 4000))
1696 WINPR_PRAGMA_DIAG_PUSH
1697 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1698 transport_free(transport);
1699 WINPR_PRAGMA_DIAG_POP
1703 void transport_free(rdpTransport* transport)
1708 transport_disconnect(transport);
1710 if (transport->ReceiveBuffer)
1711 Stream_Release(transport->ReceiveBuffer);
1713 nla_free(transport->nla);
1714 StreamPool_Free(transport->ReceivePool);
1715 (void)CloseHandle(transport->connectedEvent);
1716 (void)CloseHandle(transport->rereadEvent);
1717 (void)CloseHandle(transport->ioEvent);
1718 DeleteCriticalSection(&(transport->ReadLock));
1719 DeleteCriticalSection(&(transport->WriteLock));
1723 BOOL transport_set_io_callbacks(rdpTransport* transport,
const rdpTransportIo* io_callbacks)
1725 if (!transport || !io_callbacks)
1728 transport->io = *io_callbacks;
1732 const rdpTransportIo* transport_get_io_callbacks(rdpTransport* transport)
1736 return &transport->io;
1739 rdpContext* transport_get_context(rdpTransport* transport)
1741 WINPR_ASSERT(transport);
1742 return transport->context;
1745 rdpTransport* freerdp_get_transport(rdpContext* context)
1747 WINPR_ASSERT(context);
1748 WINPR_ASSERT(context->rdp);
1749 return context->rdp->transport;
1752 BOOL transport_set_nla(rdpTransport* transport, rdpNla* nla)
1754 WINPR_ASSERT(transport);
1755 nla_free(transport->nla);
1756 transport->nla = nla;
1760 rdpNla* transport_get_nla(rdpTransport* transport)
1762 WINPR_ASSERT(transport);
1763 return transport->nla;
1766 BOOL transport_set_tls(rdpTransport* transport, rdpTls* tls)
1768 WINPR_ASSERT(transport);
1769 freerdp_tls_free(transport->tls);
1770 transport->tls = tls;
1774 rdpTls* transport_get_tls(rdpTransport* transport)
1776 WINPR_ASSERT(transport);
1777 return transport->tls;
1780 BOOL transport_set_tsg(rdpTransport* transport, rdpTsg* tsg)
1782 WINPR_ASSERT(transport);
1783 tsg_free(transport->tsg);
1784 transport->tsg = tsg;
1788 rdpTsg* transport_get_tsg(rdpTransport* transport)
1790 WINPR_ASSERT(transport);
1791 return transport->tsg;
1794 wStream* transport_take_from_pool(rdpTransport* transport,
size_t size)
1796 WINPR_ASSERT(transport);
1797 return StreamPool_Take(transport->ReceivePool, size);
1800 ULONG transport_get_bytes_sent(rdpTransport* transport, BOOL resetCount)
1803 WINPR_ASSERT(transport);
1804 rc = transport->written;
1806 transport->written = 0;
1810 TRANSPORT_LAYER transport_get_layer(rdpTransport* transport)
1812 WINPR_ASSERT(transport);
1813 return transport->layer;
1816 BOOL transport_set_layer(rdpTransport* transport, TRANSPORT_LAYER layer)
1818 WINPR_ASSERT(transport);
1819 transport->layer = layer;
1823 BOOL transport_set_connected_event(rdpTransport* transport)
1825 WINPR_ASSERT(transport);
1826 return SetEvent(transport->connectedEvent);
1829 BOOL transport_set_recv_callbacks(rdpTransport* transport, TransportRecv recv,
void* extra)
1831 WINPR_ASSERT(transport);
1832 transport->ReceiveCallback = recv;
1833 transport->ReceiveExtra = extra;
1837 BOOL transport_get_blocking(rdpTransport* transport)
1839 WINPR_ASSERT(transport);
1840 return transport->blocking;
1843 BOOL transport_set_blocking(rdpTransport* transport, BOOL blocking)
1845 WINPR_ASSERT(transport);
1846 transport->blocking = blocking;
1850 BOOL transport_have_more_bytes_to_read(rdpTransport* transport)
1852 WINPR_ASSERT(transport);
1853 return transport->haveMoreBytesToRead;
1856 int transport_tcp_connect(rdpTransport* transport,
const char* hostname,
int port, DWORD timeout)
1858 rdpContext* context = transport_get_context(transport);
1859 WINPR_ASSERT(context);
1860 WINPR_ASSERT(context->settings);
1861 return IFCALLRESULT(-1, transport->io.TCPConnect, context, context->settings, hostname, port,
1865 HANDLE transport_get_front_bio(rdpTransport* transport)
1867 HANDLE hEvent = NULL;
1868 WINPR_ASSERT(transport);
1869 WINPR_ASSERT(transport->frontBio);
1871 BIO_get_event(transport->frontBio, &hEvent);
1875 BOOL transport_io_callback_set_event(rdpTransport* transport, BOOL set)
1877 WINPR_ASSERT(transport);
1878 transport->useIoEvent = TRUE;
1880 return ResetEvent(transport->ioEvent);
1881 return SetEvent(transport->ioEvent);
1884 void transport_set_early_user_auth_mode(rdpTransport* transport, BOOL EUAMode)
1886 WINPR_ASSERT(transport);
1887 transport->earlyUserAuth = EUAMode;
1888 WLog_Print(transport->log, WLOG_DEBUG,
"Early User Auth Mode: %s", EUAMode ?
"on" :
"off");
1891 rdpTransportLayer* transport_layer_new(rdpTransport* transport,
size_t contextSize)
1899 layer->userContext = calloc(1, contextSize);
1900 if (!layer->userContext)
1915 IFCALL(layer->Close, layer->userContext);
1916 free(layer->userContext);
1920 static int transport_layer_bio_write(BIO* bio,
const char* buf,
int size)
1933 BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
1935 int status = IFCALLRESULT(-1, layer->Write, layer->userContext, buf, size);
1937 if (status >= 0 && status < size)
1938 BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
1943 static int transport_layer_bio_read(BIO* bio,
char* buf,
int size)
1956 BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
1958 int status = IFCALLRESULT(-1, layer->Read, layer->userContext, buf, size);
1961 BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
1966 static int transport_layer_bio_puts(BIO* bio,
const char* str)
1971 static int transport_layer_bio_gets(BIO* bio,
char* str,
int size)
1976 static long transport_layer_bio_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
1987 case BIO_C_GET_EVENT:
1988 *((HANDLE*)arg2) = IFCALLRESULT(NULL, layer->GetEvent, layer->userContext);
1992 case BIO_C_SET_NONBLOCK:
1996 case BIO_C_WAIT_READ:
1998 int timeout = (int)arg1;
1999 BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, FALSE, timeout);
2011 case BIO_C_WAIT_WRITE:
2013 int timeout = (int)arg1;
2014 BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, TRUE, timeout);
2026 case BIO_CTRL_GET_CLOSE:
2027 status = BIO_get_shutdown(bio);
2030 case BIO_CTRL_SET_CLOSE:
2031 BIO_set_shutdown(bio, (
int)arg1);
2035 case BIO_CTRL_FLUSH:
2048 static int transport_layer_bio_new(BIO* bio)
2052 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
2053 BIO_set_init(bio, 1);
2057 static int transport_layer_bio_free(BIO* bio)
2064 transport_layer_free(layer);
2066 BIO_set_data(bio, NULL);
2067 BIO_set_init(bio, 0);
2068 BIO_set_flags(bio, 0);
2073 BIO_METHOD* BIO_s_transport_layer(
void)
2075 static BIO_METHOD* bio_methods = NULL;
2077 if (bio_methods == NULL)
2079 if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE,
"TransportLayer")))
2082 BIO_meth_set_write(bio_methods, transport_layer_bio_write);
2083 BIO_meth_set_read(bio_methods, transport_layer_bio_read);
2084 BIO_meth_set_puts(bio_methods, transport_layer_bio_puts);
2085 BIO_meth_set_gets(bio_methods, transport_layer_bio_gets);
2086 BIO_meth_set_ctrl(bio_methods, transport_layer_bio_ctrl);
2087 BIO_meth_set_create(bio_methods, transport_layer_bio_new);
2088 BIO_meth_set_destroy(bio_methods, transport_layer_bio_free);
FREERDP_API const char * freerdp_settings_get_server_name(const rdpSettings *settings)
A helper function to return the correct server name.