20 #include <winpr/assert.h>
21 #include <freerdp/config.h>
22 #include <freerdp/log.h>
26 #include "multitransport.h"
28 struct rdp_multitransport
32 MultiTransportRequestCb MtRequest;
33 MultiTransportResponseCb MtResponse;
38 BYTE reliableCookie[RDPUDP_COOKIE_LEN];
39 BYTE reliableCookieHash[RDPUDP_COOKIE_HASHLEN];
44 RDPTUNNEL_ACTION_CREATEREQUEST = 0x00,
45 RDPTUNNEL_ACTION_CREATERESPONSE = 0x01,
46 RDPTUNNEL_ACTION_DATA = 0x02
49 #define TAG FREERDP_TAG("core.multitransport")
51 state_run_t multitransport_recv_request(rdpMultitransport* multi,
wStream* s)
54 rdpSettings* settings = multi->rdp->settings;
56 if (settings->ServerMode)
58 WLog_ERR(TAG,
"not expecting a multi-transport request in server mode");
59 return STATE_RUN_FAILED;
62 if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
63 return STATE_RUN_FAILED;
66 UINT16 requestedProto = 0;
68 const BYTE* cookie = NULL;
70 Stream_Read_UINT32(s, requestId);
71 Stream_Read_UINT16(s, requestedProto);
72 Stream_Read_UINT16(s, reserved);
73 cookie = Stream_ConstPointer(s);
74 Stream_Seek(s, RDPUDP_COOKIE_LEN);
86 "reserved is %" PRIu16
" instead of 0, skipping %" PRIuz
"bytes of unknown data",
87 reserved, Stream_GetRemainingLength(s));
88 (void)Stream_SafeSeek(s, Stream_GetRemainingLength(s));
91 WINPR_ASSERT(multi->MtRequest);
92 return multi->MtRequest(multi, requestId, requestedProto, cookie);
95 static BOOL multitransport_request_send(rdpMultitransport* multi, UINT32 reqId, UINT16 reqProto,
99 wStream* s = rdp_message_channel_pdu_init(multi->rdp);
103 if (!Stream_EnsureRemainingCapacity(s, 24))
109 Stream_Write_UINT32(s, reqId);
110 Stream_Write_UINT16(s, reqProto);
112 Stream_Write(s, cookie, RDPUDP_COOKIE_LEN);
114 return rdp_send_message_channel_pdu(multi->rdp, s, SEC_TRANSPORT_REQ);
117 state_run_t multitransport_server_request(rdpMultitransport* multi, UINT16 reqProto)
122 static UINT32 reqId = 0;
124 if (reqProto == INITIATE_REQUEST_PROTOCOL_UDPFECR)
126 multi->reliableReqId = reqId++;
127 winpr_RAND(multi->reliableCookie,
sizeof(multi->reliableCookie));
129 return multitransport_request_send(multi, multi->reliableReqId, reqProto,
130 multi->reliableCookie)
135 WLog_ERR(TAG,
"only reliable transport is supported");
136 return STATE_RUN_CONTINUE;
139 BOOL multitransport_client_send_response(rdpMultitransport* multi, UINT32 reqId, HRESULT hr)
143 wStream* s = rdp_message_channel_pdu_init(multi->rdp);
147 if (!Stream_EnsureRemainingCapacity(s, 28))
153 Stream_Write_UINT32(s, reqId);
158 Stream_Write_INT32(s, hr);
159 return rdp_send_message_channel_pdu(multi->rdp, s, SEC_TRANSPORT_RSP);
162 state_run_t multitransport_recv_response(rdpMultitransport* multi,
wStream* s)
164 WINPR_ASSERT(multi && multi->rdp);
167 rdpSettings* settings = multi->rdp->settings;
168 WINPR_ASSERT(settings);
170 if (!settings->ServerMode)
172 WLog_ERR(TAG,
"client is not expecting a multi-transport resp packet");
173 return STATE_RUN_FAILED;
176 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
177 return STATE_RUN_FAILED;
179 UINT32 requestId = 0;
182 Stream_Read_UINT32(s, requestId);
183 Stream_Read_UINT32(s, hr);
185 return IFCALLRESULT(STATE_RUN_SUCCESS, multi->MtResponse, multi, requestId, hr);
188 static state_run_t multitransport_no_udp(rdpMultitransport* multi, UINT32 reqId, UINT16 reqProto,
191 return multitransport_client_send_response(multi, reqId, E_ABORT) ? STATE_RUN_SUCCESS
195 static state_run_t multitransport_server_handle_response(rdpMultitransport* multi, UINT32 reqId,
198 rdpRdp* rdp = multi->rdp;
200 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
201 return STATE_RUN_FAILED;
203 return STATE_RUN_CONTINUE;
206 rdpMultitransport* multitransport_new(rdpRdp* rdp, UINT16 protocol)
210 rdpSettings* settings = rdp->settings;
211 WINPR_ASSERT(settings);
213 rdpMultitransport* multi = calloc(1,
sizeof(rdpMultitransport));
217 if (settings->ServerMode)
219 multi->MtResponse = multitransport_server_handle_response;
223 multi->MtRequest = multitransport_no_udp;
230 void multitransport_free(rdpMultitransport* multitransport)
232 free(multitransport);