23 #include <freerdp/config.h>
27 #include <winpr/assert.h>
28 #include <winpr/cast.h>
29 #include <winpr/crt.h>
30 #include <winpr/winsock.h>
35 #include <freerdp/log.h>
36 #include <freerdp/streamdump.h>
37 #include <freerdp/redirection.h>
38 #include <freerdp/crypto/certificate.h>
42 #include "multitransport.h"
44 #define TAG FREERDP_TAG("core.peer")
46 static state_run_t peer_recv_pdu(freerdp_peer* client,
wStream* s);
48 static HANDLE freerdp_peer_virtual_channel_open(freerdp_peer* client,
const char* name,
53 rdpMcsChannel* mcsChannel = NULL;
54 rdpPeerChannel* peerChannel = NULL;
58 WINPR_ASSERT(client->context);
59 WINPR_ASSERT(client->context->rdp);
61 mcs = client->context->rdp->mcs;
64 if (flags & WTS_CHANNEL_OPTION_DYNAMIC)
67 const size_t length = strnlen(name, 9);
72 for (; index < mcs->channelCount; index++)
74 mcsChannel = &(mcs->channels[index]);
76 if (!mcsChannel->joined)
79 if (_strnicmp(name, mcsChannel->Name, length) == 0)
89 peerChannel = (rdpPeerChannel*)mcsChannel->handle;
94 return (HANDLE)peerChannel;
97 WINPR_ASSERT(index <= UINT16_MAX);
99 server_channel_common_new(client, (UINT16)index, mcsChannel->ChannelId, 128, NULL, name);
103 peerChannel->channelFlags = flags;
104 peerChannel->mcsChannel = mcsChannel;
105 mcsChannel->handle = (
void*)peerChannel;
108 return (HANDLE)peerChannel;
111 static BOOL freerdp_peer_virtual_channel_close(freerdp_peer* client, HANDLE hChannel)
113 rdpMcsChannel* mcsChannel = NULL;
114 rdpPeerChannel* peerChannel = NULL;
116 WINPR_ASSERT(client);
121 peerChannel = (rdpPeerChannel*)hChannel;
122 mcsChannel = peerChannel->mcsChannel;
123 WINPR_ASSERT(mcsChannel);
124 mcsChannel->handle = NULL;
125 server_channel_common_free(peerChannel);
129 static int freerdp_peer_virtual_channel_write(freerdp_peer* client, HANDLE hChannel,
130 const BYTE* buffer, UINT32 length)
134 UINT32 chunkSize = 0;
135 UINT32 maxChunkSize = 0;
136 UINT32 totalLength = 0;
137 rdpPeerChannel* peerChannel = NULL;
138 rdpMcsChannel* mcsChannel = NULL;
141 WINPR_ASSERT(client);
142 WINPR_ASSERT(client->context);
144 rdp = client->context->rdp;
146 WINPR_ASSERT(rdp->settings);
151 peerChannel = (rdpPeerChannel*)hChannel;
152 mcsChannel = peerChannel->mcsChannel;
153 WINPR_ASSERT(peerChannel);
154 WINPR_ASSERT(mcsChannel);
155 if (peerChannel->channelFlags & WTS_CHANNEL_OPTION_DYNAMIC)
158 maxChunkSize = rdp->settings->VCChunkSize;
159 totalLength = length;
160 flags = CHANNEL_FLAG_FIRST;
164 s = rdp_send_stream_init(rdp);
169 if (length > maxChunkSize)
171 chunkSize = rdp->settings->VCChunkSize;
176 flags |= CHANNEL_FLAG_LAST;
179 if (mcsChannel->options & CHANNEL_OPTION_SHOW_PROTOCOL)
180 flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
182 Stream_Write_UINT32(s, totalLength);
183 Stream_Write_UINT32(s, flags);
185 if (!Stream_EnsureRemainingCapacity(s, chunkSize))
191 Stream_Write(s, buffer, chunkSize);
193 WINPR_ASSERT(peerChannel->channelId <= UINT16_MAX);
194 if (!rdp_send(rdp, s, (UINT16)peerChannel->channelId))
205 static void* freerdp_peer_virtual_channel_get_data(freerdp_peer* client, HANDLE hChannel)
207 rdpPeerChannel* peerChannel = (rdpPeerChannel*)hChannel;
209 WINPR_ASSERT(client);
213 return peerChannel->extra;
216 static int freerdp_peer_virtual_channel_set_data(freerdp_peer* client, HANDLE hChannel,
void* data)
218 rdpPeerChannel* peerChannel = (rdpPeerChannel*)hChannel;
220 WINPR_ASSERT(client);
224 peerChannel->extra = data;
228 static BOOL freerdp_peer_set_state(freerdp_peer* client, CONNECTION_STATE state)
230 WINPR_ASSERT(client);
231 WINPR_ASSERT(client->context);
232 return rdp_server_transition_to_state(client->context->rdp, state);
235 static BOOL freerdp_peer_initialize(freerdp_peer* client)
238 rdpSettings* settings = NULL;
240 WINPR_ASSERT(client);
241 WINPR_ASSERT(client->context);
243 rdp = client->context->rdp;
246 settings = rdp->settings;
247 WINPR_ASSERT(settings);
249 settings->ServerMode = TRUE;
250 settings->FrameAcknowledge = 0;
251 settings->LocalConnection = client->local;
253 const rdpCertificate* cert =
257 WLog_ERR(TAG,
"Missing server certificate, can not continue.");
264 if (!freerdp_certificate_is_rdp_security_compatible(cert))
273 nego_set_RCG_supported(rdp->nego, settings->RemoteCredentialGuard);
274 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_INITIAL))
280 #if defined(WITH_FREERDP_DEPRECATED)
281 static BOOL freerdp_peer_get_fds(freerdp_peer* client,
void** rfds,
int* rcount)
283 rdpTransport* transport = NULL;
284 WINPR_ASSERT(client);
285 WINPR_ASSERT(client->context);
286 WINPR_ASSERT(client->context->rdp);
288 transport = client->context->rdp->transport;
289 WINPR_ASSERT(transport);
290 transport_get_fds(transport, rfds, rcount);
295 static HANDLE freerdp_peer_get_event_handle(freerdp_peer* client)
297 HANDLE hEvent = NULL;
298 rdpTransport* transport = NULL;
299 WINPR_ASSERT(client);
300 WINPR_ASSERT(client->context);
301 WINPR_ASSERT(client->context->rdp);
303 transport = client->context->rdp->transport;
304 hEvent = transport_get_front_bio(transport);
308 static DWORD freerdp_peer_get_event_handles(freerdp_peer* client, HANDLE* events, DWORD count)
310 WINPR_ASSERT(client);
311 WINPR_ASSERT(client->context);
312 WINPR_ASSERT(client->context->rdp);
313 return transport_get_event_handles(client->context->rdp->transport, events, count);
316 static BOOL freerdp_peer_check_fds(freerdp_peer* peer)
322 WINPR_ASSERT(peer->context);
324 rdp = peer->context->rdp;
325 status = rdp_check_fds(rdp);
333 static state_run_t peer_recv_data_pdu(freerdp_peer* client,
wStream* s, UINT16 totalLength)
338 BYTE compressed_type = 0;
339 UINT16 compressed_len = 0;
340 rdpUpdate* update = NULL;
343 WINPR_ASSERT(client);
344 WINPR_ASSERT(client->context);
345 rdpRdp* rdp = client->context->rdp;
347 WINPR_ASSERT(rdp->mcs);
349 update = client->context->update;
350 WINPR_ASSERT(update);
352 if (!rdp_read_share_data_header(rdp, s, &length, &type, &share_id, &compressed_type,
354 return STATE_RUN_FAILED;
356 #ifdef WITH_DEBUG_RDP
357 WLog_Print(rdp->log, WLOG_DEBUG,
"recv %s Data PDU (0x%02" PRIX8
"), length: %" PRIu16
"",
358 data_pdu_type_to_string(type), type, length);
363 case DATA_PDU_TYPE_SYNCHRONIZE:
364 if (!rdp_recv_client_synchronize_pdu(rdp, s))
365 return STATE_RUN_FAILED;
369 case DATA_PDU_TYPE_CONTROL:
370 if (!rdp_server_accept_client_control_pdu(rdp, s))
371 return STATE_RUN_FAILED;
375 case DATA_PDU_TYPE_INPUT:
376 if (!input_recv(rdp->input, s))
377 return STATE_RUN_FAILED;
381 case DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST:
382 if (!rdp_server_accept_client_persistent_key_list_pdu(rdp, s))
383 return STATE_RUN_FAILED;
386 case DATA_PDU_TYPE_FONT_LIST:
387 if (!rdp_server_accept_client_font_list_pdu(rdp, s))
388 return STATE_RUN_FAILED;
390 return STATE_RUN_CONTINUE;
392 case DATA_PDU_TYPE_SHUTDOWN_REQUEST:
393 mcs_send_disconnect_provider_ultimatum(rdp->mcs);
394 WLog_WARN(TAG,
"disconnect provider ultimatum sent to peer, closing connection");
395 return STATE_RUN_QUIT_SESSION;
397 case DATA_PDU_TYPE_FRAME_ACKNOWLEDGE:
398 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
399 return STATE_RUN_FAILED;
401 Stream_Read_UINT32(s, client->ack_frame_id);
402 IFCALL(update->SurfaceFrameAcknowledge, update->context, client->ack_frame_id);
405 case DATA_PDU_TYPE_REFRESH_RECT:
406 if (!update_read_refresh_rect(update, s))
407 return STATE_RUN_FAILED;
411 case DATA_PDU_TYPE_SUPPRESS_OUTPUT:
412 if (!update_read_suppress_output(update, s))
413 return STATE_RUN_FAILED;
418 WLog_ERR(TAG,
"Data PDU type %" PRIu8
"", type);
422 return STATE_RUN_SUCCESS;
425 static state_run_t peer_recv_tpkt_pdu(freerdp_peer* client,
wStream* s)
427 state_run_t rc = STATE_RUN_SUCCESS;
431 UINT16 pduSource = 0;
432 UINT16 channelId = 0;
433 UINT16 securityFlags = 0;
434 rdpSettings* settings = NULL;
437 WINPR_ASSERT(client);
438 WINPR_ASSERT(client->context);
440 rdp = client->context->rdp;
442 WINPR_ASSERT(rdp->mcs);
444 settings = client->context->settings;
445 WINPR_ASSERT(settings);
447 if (!rdp_read_header(rdp, s, &length, &channelId))
448 return STATE_RUN_FAILED;
451 if (freerdp_shall_disconnect_context(rdp->context))
452 return STATE_RUN_SUCCESS;
454 if (rdp_get_state(rdp) <= CONNECTION_STATE_LICENSING)
456 if (!rdp_handle_message_channel(rdp, s, channelId, length))
457 return STATE_RUN_FAILED;
458 return STATE_RUN_SUCCESS;
461 if (!rdp_handle_optional_rdp_decryption(rdp, s, &length, &securityFlags))
462 return STATE_RUN_FAILED;
464 if (channelId == MCS_GLOBAL_CHANNEL_ID)
466 char buffer[256] = { 0 };
467 UINT16 pduLength = 0;
469 if (!rdp_read_share_control_header(rdp, s, &pduLength, &remain, &pduType, &pduSource))
470 return STATE_RUN_FAILED;
472 settings->PduSource = pduSource;
474 WLog_DBG(TAG,
"Received %s", pdu_type_to_str(pduType, buffer,
sizeof(buffer)));
478 rc = peer_recv_data_pdu(client, s, pduLength);
481 case PDU_TYPE_CONFIRM_ACTIVE:
482 if (!rdp_server_accept_confirm_active(rdp, s, pduLength))
483 return STATE_RUN_FAILED;
487 case PDU_TYPE_FLOW_RESPONSE:
488 case PDU_TYPE_FLOW_STOP:
489 case PDU_TYPE_FLOW_TEST:
490 if (!Stream_SafeSeek(s, remain))
492 WLog_WARN(TAG,
"Short PDU, need %" PRIuz
" bytes, got %" PRIuz, remain,
493 Stream_GetRemainingLength(s));
494 return STATE_RUN_FAILED;
499 WLog_ERR(TAG,
"Client sent unknown pduType %" PRIu16
"", pduType);
500 return STATE_RUN_FAILED;
503 else if ((rdp->mcs->messageChannelId > 0) && (channelId == rdp->mcs->messageChannelId))
505 if (!settings->UseRdpSecurityLayer)
507 if (!rdp_read_security_header(rdp, s, &securityFlags, NULL))
508 return STATE_RUN_FAILED;
511 return rdp_recv_message_channel_pdu(rdp, s, securityFlags);
515 if (!freerdp_channel_peer_process(client, s, channelId))
516 return STATE_RUN_FAILED;
518 if (!tpkt_ensure_stream_consumed(s, length))
519 return STATE_RUN_FAILED;
524 static state_run_t peer_recv_handle_auto_detect(freerdp_peer* client,
wStream* s)
526 state_run_t ret = STATE_RUN_FAILED;
529 WINPR_ASSERT(client);
531 WINPR_ASSERT(client->context);
533 rdp = client->context->rdp;
536 const rdpSettings* settings = client->context->settings;
537 WINPR_ASSERT(settings);
541 switch (rdp_get_state(rdp))
543 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
544 autodetect_on_connect_time_auto_detect_begin(rdp->autodetect);
545 switch (autodetect_get_state(rdp->autodetect))
547 case FREERDP_AUTODETECT_STATE_REQUEST:
548 ret = STATE_RUN_SUCCESS;
549 if (!rdp_server_transition_to_state(
550 rdp, CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE))
551 return STATE_RUN_FAILED;
553 case FREERDP_AUTODETECT_STATE_COMPLETE:
554 ret = STATE_RUN_CONTINUE;
555 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_LICENSING))
556 return STATE_RUN_FAILED;
562 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE:
563 ret = peer_recv_pdu(client, s);
564 if (state_run_success(ret))
566 autodetect_on_connect_time_auto_detect_progress(rdp->autodetect);
567 switch (autodetect_get_state(rdp->autodetect))
569 case FREERDP_AUTODETECT_STATE_REQUEST:
570 ret = STATE_RUN_SUCCESS;
572 case FREERDP_AUTODETECT_STATE_COMPLETE:
573 ret = STATE_RUN_CONTINUE;
574 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_LICENSING))
575 return STATE_RUN_FAILED;
589 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_LICENSING))
590 return STATE_RUN_FAILED;
592 ret = STATE_RUN_CONTINUE;
598 static state_run_t peer_recv_handle_licensing(freerdp_peer* client,
wStream* s)
600 state_run_t ret = STATE_RUN_FAILED;
602 rdpSettings* settings = NULL;
604 WINPR_ASSERT(client);
606 WINPR_ASSERT(client->context);
608 rdp = client->context->rdp;
611 settings = rdp->settings;
612 WINPR_ASSERT(settings);
614 switch (license_get_state(rdp->license))
616 case LICENSE_STATE_INITIAL:
618 const BOOL required =
623 if (!license_server_configure(rdp->license))
624 ret = STATE_RUN_FAILED;
625 else if (!license_server_send_request(rdp->license))
626 ret = STATE_RUN_FAILED;
628 ret = STATE_RUN_SUCCESS;
632 if (license_send_valid_client_error_packet(rdp))
633 ret = STATE_RUN_CONTINUE;
637 case LICENSE_STATE_COMPLETED:
638 ret = STATE_RUN_CONTINUE;
640 case LICENSE_STATE_ABORTED:
641 ret = STATE_RUN_FAILED;
644 ret = peer_recv_pdu(client, s);
651 static state_run_t peer_recv_fastpath_pdu(freerdp_peer* client,
wStream* s)
656 rdpFastPath* fastpath = NULL;
659 WINPR_ASSERT(client);
660 WINPR_ASSERT(client->context);
662 rdp = client->context->rdp;
665 fastpath = rdp->fastpath;
666 WINPR_ASSERT(fastpath);
668 rc = fastpath_read_header_rdp(fastpath, s, &length);
670 if (!rc || (length == 0))
672 WLog_ERR(TAG,
"incorrect FastPath PDU header length %" PRIu16
"", length);
673 return STATE_RUN_FAILED;
675 if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
676 return STATE_RUN_FAILED;
678 if (!fastpath_decrypt(fastpath, s, &length))
679 return STATE_RUN_FAILED;
683 return fastpath_recv_inputs(fastpath, s);
686 state_run_t peer_recv_pdu(freerdp_peer* client,
wStream* s)
688 int rc = tpkt_verify_header(s);
691 return peer_recv_tpkt_pdu(client, s);
693 return peer_recv_fastpath_pdu(client, s);
695 return STATE_RUN_FAILED;
698 static state_run_t peer_unexpected_client_message(rdpRdp* rdp, UINT32 flag)
700 char buffer[1024] = { 0 };
701 WLog_WARN(TAG,
"Unexpected client message in state %s, missing flag %s",
702 rdp_get_state_string(rdp), rdp_finalize_flags_to_str(flag, buffer,
sizeof(buffer)));
703 return STATE_RUN_SUCCESS;
706 state_run_t rdp_peer_handle_state_demand_active(freerdp_peer* client)
708 state_run_t ret = STATE_RUN_FAILED;
710 WINPR_ASSERT(client);
711 WINPR_ASSERT(client->context);
713 rdpRdp* rdp = client->context->rdp;
716 if (client->Capabilities && !client->Capabilities(client))
718 WLog_ERR(TAG,
"[%s] freerdp_peer::Capabilities() callback failed",
719 rdp_get_state_string(rdp));
721 else if (!rdp_send_demand_active(rdp))
723 WLog_ERR(TAG,
"[%s] rdp_send_demand_active() fail", rdp_get_state_string(rdp));
727 if (!rdp_server_transition_to_state(rdp,
728 CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT))
729 return STATE_RUN_FAILED;
730 ret = STATE_RUN_CONTINUE;
741 static state_run_t rdp_peer_handle_state_active(freerdp_peer* client)
743 state_run_t ret = STATE_RUN_FAILED;
745 WINPR_ASSERT(client);
746 WINPR_ASSERT(client->context);
748 if (!client->connected)
754 IFCALLRET(client->PostConnect, client->connected, client);
756 if (!client->connected)
758 WLog_ERR(TAG,
"PostConnect for peer %p failed", client);
759 ret = STATE_RUN_FAILED;
761 else if (!client->activated)
763 BOOL activated = TRUE;
768 client->activated = TRUE;
769 IFCALLRET(client->Activate, activated, client);
773 WLog_ERR(TAG,
"Activate for peer %p failed", client);
774 ret = STATE_RUN_FAILED;
777 ret = STATE_RUN_SUCCESS;
780 ret = STATE_RUN_ACTIVE;
784 static state_run_t peer_recv_callback_internal(rdpTransport* transport,
wStream* s,
void* extra)
786 UINT32 SelectedProtocol = 0;
787 freerdp_peer* client = (freerdp_peer*)extra;
789 state_run_t ret = STATE_RUN_FAILED;
790 rdpSettings* settings = NULL;
792 WINPR_ASSERT(transport);
793 WINPR_ASSERT(client);
794 WINPR_ASSERT(client->context);
796 rdp = client->context->rdp;
799 settings = client->context->settings;
800 WINPR_ASSERT(settings);
802 IFCALL(client->ReachedState, client, rdp_get_state(rdp));
803 switch (rdp_get_state(rdp))
805 case CONNECTION_STATE_INITIAL:
806 if (rdp_server_transition_to_state(rdp, CONNECTION_STATE_NEGO))
807 ret = STATE_RUN_CONTINUE;
810 case CONNECTION_STATE_NEGO:
811 if (!rdp_server_accept_nego(rdp, s))
813 WLog_ERR(TAG,
"%s - rdp_server_accept_nego() fail", rdp_get_state_string(rdp));
817 SelectedProtocol = nego_get_selected_protocol(rdp->nego);
818 settings->RdstlsSecurity = (SelectedProtocol & PROTOCOL_RDSTLS) ? TRUE : FALSE;
819 settings->NlaSecurity = (SelectedProtocol & PROTOCOL_HYBRID) ? TRUE : FALSE;
820 settings->TlsSecurity = (SelectedProtocol & PROTOCOL_SSL) ? TRUE : FALSE;
821 settings->RdpSecurity = (SelectedProtocol == PROTOCOL_RDP) ? TRUE : FALSE;
823 if (SelectedProtocol & PROTOCOL_HYBRID)
827 sspi_CopyAuthIdentity(&client->identity, identity);
828 IFCALLRET(client->Logon, client->authenticated, client, &client->identity,
830 nego_free_nla(rdp->nego);
834 IFCALLRET(client->Logon, client->authenticated, client, &client->identity,
837 if (rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
838 ret = STATE_RUN_SUCCESS;
842 case CONNECTION_STATE_NLA:
846 case CONNECTION_STATE_MCS_CREATE_REQUEST:
847 if (!rdp_server_accept_mcs_connect_initial(rdp, s))
851 "rdp_server_accept_mcs_connect_initial() fail",
852 rdp_get_state_string(rdp));
855 ret = STATE_RUN_SUCCESS;
859 case CONNECTION_STATE_MCS_ERECT_DOMAIN:
860 if (!rdp_server_accept_mcs_erect_domain_request(rdp, s))
864 "rdp_server_accept_mcs_erect_domain_request() fail",
865 rdp_get_state_string(rdp));
868 ret = STATE_RUN_SUCCESS;
872 case CONNECTION_STATE_MCS_ATTACH_USER:
873 if (!rdp_server_accept_mcs_attach_user_request(rdp, s))
877 "rdp_server_accept_mcs_attach_user_request() fail",
878 rdp_get_state_string(rdp));
881 ret = STATE_RUN_SUCCESS;
885 case CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST:
886 if (!rdp_server_accept_mcs_channel_join_request(rdp, s))
890 "rdp_server_accept_mcs_channel_join_request() fail",
891 rdp_get_state_string(rdp));
894 ret = STATE_RUN_SUCCESS;
897 case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
898 ret = STATE_RUN_SUCCESS;
900 if (!rdp_server_establish_keys(rdp, s))
904 "rdp_server_establish_keys() fail",
905 rdp_get_state_string(rdp));
906 ret = STATE_RUN_FAILED;
909 if (state_run_success(ret))
911 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE))
912 ret = STATE_RUN_FAILED;
913 else if (Stream_GetRemainingLength(s) > 0)
914 ret = STATE_RUN_CONTINUE;
918 case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
919 if (rdp_recv_client_info(rdp, s))
921 if (rdp_server_transition_to_state(
922 rdp, CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST))
923 ret = STATE_RUN_CONTINUE;
927 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
928 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE:
929 ret = peer_recv_handle_auto_detect(client, s);
932 case CONNECTION_STATE_LICENSING:
933 ret = peer_recv_handle_licensing(client, s);
934 if (ret == STATE_RUN_CONTINUE)
936 if (!rdp_server_transition_to_state(
937 rdp, CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST))
938 ret = STATE_RUN_FAILED;
942 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST:
943 if (settings->SupportMultitransport &&
944 ((settings->MultitransportFlags & INITIATE_REQUEST_PROTOCOL_UDPFECR) != 0))
947 ret = multitransport_server_request(rdp->multitransport,
948 INITIATE_REQUEST_PROTOCOL_UDPFECR);
951 case STATE_RUN_SUCCESS:
952 rdp_server_transition_to_state(
953 rdp, CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE);
955 case STATE_RUN_CONTINUE:
957 rdp_server_transition_to_state(
958 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE);
966 if (rdp_server_transition_to_state(
967 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
968 ret = STATE_RUN_CONTINUE;
971 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE:
972 ret = peer_recv_pdu(client, s);
975 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE:
976 ret = rdp_peer_handle_state_demand_active(client);
979 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT:
984 IFCALL(client->AdjustMonitorsLayout, client);
987 ret = STATE_RUN_SUCCESS;
994 .width = WINPR_ASSERTING_INT_CAST(int32_t, w),
995 .height = WINPR_ASSERTING_INT_CAST(int32_t, h),
998 .attributes = { .physicalWidth = w,
1001 ORIENTATION_LANDSCAPE,
1002 .desktopScaleFactor = 100,
1003 .deviceScaleFactor = 100 } };
1004 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_MonitorDefArray, 0,
1006 ret = STATE_RUN_FAILED;
1008 ret = STATE_RUN_FAILED;
1010 if (state_run_failed(ret))
1013 else if (!display_convert_rdp_monitor_to_monitor_def(
1014 settings->MonitorCount, settings->MonitorDefArray, &monitors))
1016 ret = STATE_RUN_FAILED;
1018 else if (!freerdp_display_send_monitor_layout(rdp->context, settings->MonitorCount,
1021 ret = STATE_RUN_FAILED;
1024 ret = STATE_RUN_SUCCESS;
1027 const size_t len = Stream_GetRemainingLength(s);
1028 if (!state_run_failed(ret) && (len > 0))
1029 ret = STATE_RUN_CONTINUE;
1033 const size_t len = Stream_GetRemainingLength(s);
1035 ret = STATE_RUN_CONTINUE;
1037 ret = STATE_RUN_SUCCESS;
1039 if (state_run_success(ret))
1041 if (!rdp_server_transition_to_state(
1042 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE))
1043 ret = STATE_RUN_FAILED;
1047 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
1052 ret = peer_recv_pdu(client, s);
1055 case CONNECTION_STATE_FINALIZATION_SYNC:
1056 ret = peer_recv_pdu(client, s);
1057 if (rdp_finalize_is_flag_set(rdp, FINALIZE_CS_SYNCHRONIZE_PDU))
1059 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_COOPERATE))
1060 ret = STATE_RUN_FAILED;
1063 ret = peer_unexpected_client_message(rdp, FINALIZE_CS_SYNCHRONIZE_PDU);
1065 case CONNECTION_STATE_FINALIZATION_COOPERATE:
1066 ret = peer_recv_pdu(client, s);
1067 if (rdp_finalize_is_flag_set(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU))
1069 if (!rdp_server_transition_to_state(rdp,
1070 CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL))
1071 ret = STATE_RUN_FAILED;
1074 ret = peer_unexpected_client_message(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU);
1076 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
1077 ret = peer_recv_pdu(client, s);
1078 if (rdp_finalize_is_flag_set(rdp, FINALIZE_CS_CONTROL_REQUEST_PDU))
1080 if (!rdp_send_server_control_granted_pdu(rdp))
1081 ret = STATE_RUN_FAILED;
1082 else if (!rdp_server_transition_to_state(
1083 rdp, CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST))
1084 ret = STATE_RUN_FAILED;
1087 ret = peer_unexpected_client_message(rdp, FINALIZE_CS_CONTROL_REQUEST_PDU);
1089 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
1091 !rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE))
1093 ret = peer_recv_pdu(client, s);
1095 if (rdp_finalize_is_flag_set(rdp, FINALIZE_CS_PERSISTENT_KEY_LIST_PDU))
1097 if (!rdp_server_transition_to_state(rdp,
1098 CONNECTION_STATE_FINALIZATION_FONT_LIST))
1099 ret = STATE_RUN_FAILED;
1102 ret = peer_unexpected_client_message(rdp,
1103 CONNECTION_STATE_FINALIZATION_FONT_LIST);
1107 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_FONT_LIST))
1108 ret = STATE_RUN_FAILED;
1110 ret = STATE_RUN_CONTINUE;
1113 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
1114 ret = peer_recv_pdu(client, s);
1115 if (state_run_success(ret))
1117 if (rdp_finalize_is_flag_set(rdp, FINALIZE_CS_FONT_LIST_PDU))
1119 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_ACTIVE))
1120 ret = STATE_RUN_FAILED;
1123 update_reset_state(rdp->update);
1124 ret = STATE_RUN_CONTINUE;
1128 ret = peer_unexpected_client_message(rdp, FINALIZE_CS_FONT_LIST_PDU);
1132 case CONNECTION_STATE_ACTIVE:
1133 ret = rdp_peer_handle_state_active(client);
1134 if (ret >= STATE_RUN_ACTIVE)
1135 ret = peer_recv_pdu(client, s);
1139 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
1140 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
1141 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
1142 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
1144 WLog_ERR(TAG,
"%s state %d", rdp_get_state_string(rdp), rdp_get_state(rdp));
1151 static state_run_t peer_recv_callback(rdpTransport* transport,
wStream* s,
void* extra)
1153 char buffer[64] = { 0 };
1154 state_run_t rc = STATE_RUN_FAILED;
1155 const size_t start = Stream_GetPosition(s);
1156 const rdpContext* context = transport_get_context(transport);
1157 DWORD level = WLOG_TRACE;
1158 static wLog* log = NULL;
1160 log = WLog_Get(TAG);
1162 WINPR_ASSERT(context);
1165 const rdpRdp* rdp = context->rdp;
1166 const char* old = rdp_get_state_string(rdp);
1168 if (rc == STATE_RUN_TRY_AGAIN)
1169 Stream_SetPosition(s, start);
1170 rc = peer_recv_callback_internal(transport, s, extra);
1172 const size_t len = Stream_GetRemainingLength(s);
1173 if ((len > 0) && !state_run_continue(rc))
1175 WLog_Print(log, level,
1176 "(server)[%s -> %s] current return %s [%" PRIuz
" bytes not processed]", old,
1177 rdp_get_state_string(rdp), state_run_result_string(rc, buffer,
sizeof(buffer)),
1179 }
while (state_run_continue(rc));
1184 static BOOL freerdp_peer_close(freerdp_peer* client)
1186 UINT32 SelectedProtocol = 0;
1187 rdpContext* context = NULL;
1189 WINPR_ASSERT(client);
1191 context = client->context;
1192 WINPR_ASSERT(context);
1193 WINPR_ASSERT(context->settings);
1194 WINPR_ASSERT(context->rdp);
1199 SelectedProtocol = nego_get_selected_protocol(context->rdp->nego);
1201 if (SelectedProtocol & PROTOCOL_FAILED_NEGO)
1209 if (!rdp_send_deactivate_all(context->rdp))
1214 rdp_send_error_info(context->rdp);
1217 return mcs_send_disconnect_provider_ultimatum(context->rdp->mcs);
1220 static void freerdp_peer_disconnect(freerdp_peer* client)
1222 rdpTransport* transport = NULL;
1223 WINPR_ASSERT(client);
1225 transport = freerdp_get_transport(client->context);
1226 transport_disconnect(transport);
1229 static BOOL freerdp_peer_send_channel_data(freerdp_peer* client, UINT16 channelId,
const BYTE* data,
1232 WINPR_ASSERT(client);
1233 WINPR_ASSERT(client->context);
1234 WINPR_ASSERT(client->context->rdp);
1235 return rdp_send_channel_data(client->context->rdp, channelId, data, size);
1238 static BOOL freerdp_peer_send_server_redirection_pdu(freerdp_peer* peer,
1239 const rdpRedirection* redirection)
1243 WINPR_ASSERT(peer->context);
1245 wStream* s = rdp_send_stream_pdu_init(peer->context->rdp);
1248 if (!rdp_write_enhanced_security_redirection_packet(s, redirection))
1250 if (!rdp_send_pdu(peer->context->rdp, s, PDU_TYPE_SERVER_REDIRECTION, 0))
1252 rc = rdp_reset_runtime_settings(peer->context->rdp);
1258 static BOOL freerdp_peer_send_channel_packet(freerdp_peer* client, UINT16 channelId,
1259 size_t totalSize, UINT32 flags,
const BYTE* data,
1262 WINPR_ASSERT(client);
1263 WINPR_ASSERT(client->context);
1264 WINPR_ASSERT(client->context->rdp);
1265 return rdp_channel_send_packet(client->context->rdp, channelId, totalSize, flags, data,
1269 static BOOL freerdp_peer_is_write_blocked(freerdp_peer* peer)
1271 rdpTransport* transport = NULL;
1273 WINPR_ASSERT(peer->context);
1274 WINPR_ASSERT(peer->context->rdp);
1275 WINPR_ASSERT(peer->context->rdp->transport);
1276 transport = peer->context->rdp->transport;
1277 return transport_is_write_blocked(transport);
1280 static int freerdp_peer_drain_output_buffer(freerdp_peer* peer)
1282 rdpTransport* transport = NULL;
1284 WINPR_ASSERT(peer->context);
1285 WINPR_ASSERT(peer->context->rdp);
1286 WINPR_ASSERT(peer->context->rdp->transport);
1287 transport = peer->context->rdp->transport;
1288 return transport_drain_output_buffer(transport);
1291 static BOOL freerdp_peer_has_more_to_read(freerdp_peer* peer)
1294 WINPR_ASSERT(peer->context);
1295 WINPR_ASSERT(peer->context->rdp);
1296 return transport_have_more_bytes_to_read(peer->context->rdp->transport);
1299 static LicenseCallbackResult freerdp_peer_nolicense(freerdp_peer* peer,
wStream* s)
1304 WINPR_ASSERT(peer->context);
1306 rdp = peer->context->rdp;
1308 if (!license_send_valid_client_error_packet(rdp))
1310 WLog_ERR(TAG,
"freerdp_peer_nolicense: license_send_valid_client_error_packet() failed");
1311 return LICENSE_CB_ABORT;
1314 return LICENSE_CB_COMPLETED;
1317 BOOL freerdp_peer_context_new(freerdp_peer* client)
1319 return freerdp_peer_context_new_ex(client, NULL);
1322 void freerdp_peer_context_free(freerdp_peer* client)
1327 IFCALL(client->ContextFree, client, client->context);
1329 if (client->context)
1331 rdpContext* ctx = client->context;
1333 (void)CloseHandle(ctx->channelErrorEvent);
1334 ctx->channelErrorEvent = NULL;
1335 free(ctx->errorDescription);
1336 ctx->errorDescription = NULL;
1339 metrics_free(ctx->metrics);
1340 ctx->metrics = NULL;
1341 stream_dump_free(ctx->dump);
1345 client->context = NULL;
1348 static const char* os_major_type_to_string(UINT16 osMajorType)
1350 switch (osMajorType)
1352 case OSMAJORTYPE_UNSPECIFIED:
1353 return "Unspecified platform";
1354 case OSMAJORTYPE_WINDOWS:
1355 return "Windows platform";
1356 case OSMAJORTYPE_OS2:
1357 return "OS/2 platform";
1358 case OSMAJORTYPE_MACINTOSH:
1359 return "Macintosh platform";
1360 case OSMAJORTYPE_UNIX:
1361 return "UNIX platform";
1362 case OSMAJORTYPE_IOS:
1363 return "iOS platform";
1364 case OSMAJORTYPE_OSX:
1365 return "OS X platform";
1366 case OSMAJORTYPE_ANDROID:
1367 return "Android platform";
1368 case OSMAJORTYPE_CHROME_OS:
1369 return "Chrome OS platform";
1374 return "Unknown platform";
1377 const char* freerdp_peer_os_major_type_string(freerdp_peer* client)
1379 WINPR_ASSERT(client);
1381 rdpContext* context = client->context;
1382 WINPR_ASSERT(context);
1383 WINPR_ASSERT(context->settings);
1386 WINPR_ASSERT(osMajorType <= UINT16_MAX);
1387 return os_major_type_to_string((UINT16)osMajorType);
1390 static const char* os_minor_type_to_string(UINT16 osMinorType)
1392 switch (osMinorType)
1394 case OSMINORTYPE_UNSPECIFIED:
1395 return "Unspecified version";
1396 case OSMINORTYPE_WINDOWS_31X:
1397 return "Windows 3.1x";
1398 case OSMINORTYPE_WINDOWS_95:
1399 return "Windows 95";
1400 case OSMINORTYPE_WINDOWS_NT:
1401 return "Windows NT";
1402 case OSMINORTYPE_OS2_V21:
1404 case OSMINORTYPE_POWER_PC:
1406 case OSMINORTYPE_MACINTOSH:
1408 case OSMINORTYPE_NATIVE_XSERVER:
1409 return "Native X Server";
1410 case OSMINORTYPE_PSEUDO_XSERVER:
1411 return "Pseudo X Server";
1412 case OSMINORTYPE_WINDOWS_RT:
1413 return "Windows RT";
1418 return "Unknown version";
1421 const char* freerdp_peer_os_minor_type_string(freerdp_peer* client)
1423 WINPR_ASSERT(client);
1425 rdpContext* context = client->context;
1426 WINPR_ASSERT(context);
1427 WINPR_ASSERT(context->settings);
1430 WINPR_ASSERT(osMinorType <= UINT16_MAX);
1431 return os_minor_type_to_string((UINT16)osMinorType);
1434 freerdp_peer* freerdp_peer_new(
int sockfd)
1436 UINT32 option_value = 0;
1437 socklen_t option_len = 0;
1438 freerdp_peer* client = (freerdp_peer*)calloc(1,
sizeof(freerdp_peer));
1443 option_value = TRUE;
1444 option_len =
sizeof(option_value);
1448 if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (
void*)&option_value, option_len) < 0)
1452 WLog_DBG(TAG,
"can't set TCP_NODELAY, continuing anyway");
1458 client->sockfd = sockfd;
1459 client->ContextSize =
sizeof(rdpContext);
1460 client->Initialize = freerdp_peer_initialize;
1461 #if defined(WITH_FREERDP_DEPRECATED)
1462 client->GetFileDescriptor = freerdp_peer_get_fds;
1464 client->GetEventHandle = freerdp_peer_get_event_handle;
1465 client->GetEventHandles = freerdp_peer_get_event_handles;
1466 client->CheckFileDescriptor = freerdp_peer_check_fds;
1467 client->Close = freerdp_peer_close;
1468 client->Disconnect = freerdp_peer_disconnect;
1469 client->SendChannelData = freerdp_peer_send_channel_data;
1470 client->SendChannelPacket = freerdp_peer_send_channel_packet;
1471 client->SendServerRedirection = freerdp_peer_send_server_redirection_pdu;
1472 client->IsWriteBlocked = freerdp_peer_is_write_blocked;
1473 client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
1474 client->HasMoreToRead = freerdp_peer_has_more_to_read;
1475 client->VirtualChannelOpen = freerdp_peer_virtual_channel_open;
1476 client->VirtualChannelClose = freerdp_peer_virtual_channel_close;
1477 client->VirtualChannelWrite = freerdp_peer_virtual_channel_write;
1478 client->VirtualChannelRead = NULL;
1479 client->VirtualChannelGetData = freerdp_peer_virtual_channel_get_data;
1480 client->VirtualChannelSetData = freerdp_peer_virtual_channel_set_data;
1481 client->SetState = freerdp_peer_set_state;
1487 void freerdp_peer_free(freerdp_peer* client)
1492 sspi_FreeAuthIdentity(&client->identity);
1493 if (client->sockfd >= 0)
1494 closesocket((SOCKET)client->sockfd);
1498 static BOOL freerdp_peer_transport_setup(freerdp_peer* client)
1502 WINPR_ASSERT(client);
1503 WINPR_ASSERT(client->context);
1505 rdp = client->context->rdp;
1508 if (!transport_attach(rdp->transport, client->sockfd))
1510 client->sockfd = -1;
1512 if (!transport_set_recv_callbacks(rdp->transport, peer_recv_callback, client))
1515 if (!transport_set_blocking_mode(rdp->transport, FALSE))
1521 BOOL freerdp_peer_context_new_ex(freerdp_peer* client,
const rdpSettings* settings)
1524 rdpContext* context = NULL;
1530 WINPR_ASSERT(client->ContextSize >=
sizeof(rdpContext));
1531 if (!(context = (rdpContext*)calloc(1, client->ContextSize)))
1534 client->context = context;
1535 context->peer = client;
1536 context->ServerMode = TRUE;
1537 context->log = WLog_Get(TAG);
1544 if (!context->settings)
1548 context->dump = stream_dump_new();
1551 if (!(context->metrics = metrics_new(context)))
1554 if (!(rdp = rdp_new(context)))
1557 rdp_log_build_warnings(rdp);
1559 #if defined(WITH_FREERDP_DEPRECATED)
1560 client->update = rdp->update;
1561 client->settings = rdp->settings;
1562 client->autodetect = rdp->autodetect;
1565 context->input = rdp->input;
1566 context->update = rdp->update;
1567 context->settings = rdp->settings;
1568 context->autodetect = rdp->autodetect;
1569 update_register_server_callbacks(rdp->update);
1570 autodetect_register_server_callbacks(rdp->autodetect);
1572 if (!(context->channelErrorEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
1574 WLog_ERR(TAG,
"CreateEvent failed!");
1578 if (!(context->errorDescription = calloc(1, 500)))
1580 WLog_ERR(TAG,
"calloc failed!");
1584 if (!freerdp_peer_transport_setup(client))
1587 client->IsWriteBlocked = freerdp_peer_is_write_blocked;
1588 client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
1589 client->HasMoreToRead = freerdp_peer_has_more_to_read;
1590 client->LicenseCallback = freerdp_peer_nolicense;
1591 IFCALLRET(client->ContextNew, ret, client, client->context);
1598 WLog_ERR(TAG,
"ContextNew callback failed");
1599 freerdp_peer_context_free(client);
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.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API rdpSettings * freerdp_settings_clone(const rdpSettings *settings)
Creates a deep copy of settings.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.