21#include <winpr/assert.h>
23#include <winpr/file.h>
24#include <winpr/pipe.h>
25#include <winpr/thread.h>
27#include <freerdp/freerdp.h>
28#include <freerdp/svc.h>
29#include <freerdp/channels/rdp2tcp.h>
31#include <freerdp/utils/warnings.h>
33#include <freerdp/log.h>
34#define TAG CLIENT_TAG(RDP2TCP_DVC_CHANNEL_NAME)
38 HANDLE hStdOutputRead;
39 HANDLE hStdInputWrite;
43 HANDLE writeTerminated;
47 char buffer[16 * 1024];
52static int init_external_addin(Plugin* plugin)
62 saAttr.bInheritHandle = TRUE;
63 saAttr.lpSecurityDescriptor =
nullptr;
65 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
66 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
69 if (!CreatePipe(&plugin->hStdOutputRead, &siStartInfo.hStdOutput, &saAttr, 0))
71 WLog_ERR(TAG,
"stdout CreatePipe");
75 if (!SetHandleInformation(plugin->hStdOutputRead, HANDLE_FLAG_INHERIT, 0))
77 WLog_ERR(TAG,
"stdout SetHandleInformation");
81 if (!CreatePipe(&siStartInfo.hStdInput, &plugin->hStdInputWrite, &saAttr, 0))
83 WLog_ERR(TAG,
"stdin CreatePipe");
87 if (!SetHandleInformation(plugin->hStdInputWrite, HANDLE_FLAG_INHERIT, 0))
89 WLog_ERR(TAG,
"stdin SetHandleInformation");
95 if (!args || (args->argc < 2))
97 WLog_ERR(TAG,
"missing command line options");
101 plugin->commandline = _strdup(args->argv[1]);
102 if (!CreateProcessA(
nullptr,
114 WLog_ERR(TAG,
"fork for addin");
118 plugin->hProcess = procInfo.hProcess;
122 (void)CloseHandle(procInfo.hThread);
123 (void)CloseHandle(siStartInfo.hStdOutput);
124 (void)CloseHandle(siStartInfo.hStdInput);
128static DWORD WINAPI copyThread(
void* data)
130 DWORD status = WAIT_OBJECT_0;
131 Plugin* plugin = (Plugin*)data;
132 size_t const bufsize = 16ULL * 1024ULL;
134 WINPR_ASSERT(plugin);
136 while (status == WAIT_OBJECT_0)
138 (void)ResetEvent(plugin->writeComplete);
141 char* buffer = calloc(bufsize,
sizeof(
char));
145 (void)fprintf(stderr,
"rdp2tcp copyThread: malloc failed\n");
151 if (!ReadFile(plugin->hStdOutputRead, buffer, bufsize, &dwRead,
nullptr))
157 if (plugin->channelEntryPoints.pVirtualChannelWriteEx(
158 plugin->initHandle, plugin->openHandle, buffer, dwRead, buffer) != CHANNEL_RC_OK)
161 (void)fprintf(stderr,
"rdp2tcp copyThread failed %i\n", (
int)dwRead);
165 HANDLE handles[] = { plugin->writeComplete,
166 freerdp_abort_event(plugin->channelEntryPoints.context),
167 plugin->writeTerminated };
168 status = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, INFINITE);
176static void closeChannel(Plugin* plugin)
178 WINPR_ASSERT(plugin);
179 WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelCloseEx);
180 plugin->channelEntryPoints.pVirtualChannelCloseEx(plugin->initHandle, plugin->openHandle);
183static void dataReceived(Plugin* plugin,
void* pData, UINT32 dataLength, UINT32 totalLength,
188 WINPR_ASSERT(plugin);
190 if (dataFlags & CHANNEL_FLAG_SUSPEND)
193 if (dataFlags & CHANNEL_FLAG_RESUME)
196 if (dataFlags & CHANNEL_FLAG_FIRST)
198 plugin->totalLength = totalLength;
199 if (!WriteFile(plugin->hStdInputWrite, &totalLength,
sizeof(totalLength), &dwWritten,
201 closeChannel(plugin);
204 if (!WriteFile(plugin->hStdInputWrite, pData, dataLength, &dwWritten,
nullptr))
205 closeChannel(plugin);
208static void VCAPITYPE VirtualChannelOpenEventEx(LPVOID lpUserParam,
209 WINPR_ATTR_UNUSED DWORD openHandle, UINT event,
210 LPVOID pData, UINT32 dataLength, UINT32 totalLength,
213 Plugin* plugin = (Plugin*)lpUserParam;
215 WINPR_ASSERT(plugin);
218 case CHANNEL_EVENT_DATA_RECEIVED:
219 dataReceived(plugin, pData, dataLength, totalLength, dataFlags);
222 case CHANNEL_EVENT_WRITE_CANCELLED:
225 case CHANNEL_EVENT_WRITE_COMPLETE:
226 (void)SetEvent(plugin->writeComplete);
234static void channel_terminated(Plugin* plugin)
239 if (plugin->writeTerminated)
240 SetEvent(plugin->writeTerminated);
242 if (plugin->copyThread)
244 (void)WaitForSingleObject(plugin->copyThread, INFINITE);
245 (void)CloseHandle(plugin->copyThread);
247 if (plugin->writeComplete)
248 (void)CloseHandle(plugin->writeComplete);
249 if (plugin->writeTerminated)
250 (void)CloseHandle(plugin->writeTerminated);
252 (void)CloseHandle(plugin->hStdInputWrite);
253 (void)CloseHandle(plugin->hStdOutputRead);
254 TerminateProcess(plugin->hProcess, 0);
255 (void)CloseHandle(plugin->hProcess);
256 free(plugin->commandline);
260static void channel_initialized(Plugin* plugin)
262 WINPR_ASSERT(plugin);
263 WINPR_ASSERT(!plugin->writeComplete);
264 plugin->writeComplete = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
265 WINPR_ASSERT(plugin->writeComplete);
267 WINPR_ASSERT(!plugin->writeTerminated);
268 plugin->writeTerminated = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
269 WINPR_ASSERT(plugin->writeTerminated);
271 WINPR_ASSERT(!plugin->copyThread);
272 plugin->copyThread = CreateThread(
nullptr, 0, copyThread, plugin, 0,
nullptr);
273 WINPR_ASSERT(plugin->copyThread);
276static VOID VCAPITYPE VirtualChannelInitEventEx(LPVOID lpUserParam, LPVOID pInitHandle, UINT event,
277 WINPR_ATTR_UNUSED LPVOID pData,
278 WINPR_ATTR_UNUSED UINT dataLength)
280 Plugin* plugin = (Plugin*)lpUserParam;
282 WINPR_ASSERT(plugin);
286 case CHANNEL_EVENT_INITIALIZED:
287 channel_initialized(plugin);
290 case CHANNEL_EVENT_CONNECTED:
291 WINPR_ASSERT(plugin);
292 WINPR_ASSERT(plugin->channelEntryPoints.pVirtualChannelOpenEx);
293 if (plugin->channelEntryPoints.pVirtualChannelOpenEx(
294 pInitHandle, &plugin->openHandle, RDP2TCP_DVC_CHANNEL_NAME,
295 VirtualChannelOpenEventEx) != CHANNEL_RC_OK)
300 case CHANNEL_EVENT_DISCONNECTED:
301 closeChannel(plugin);
304 case CHANNEL_EVENT_TERMINATED:
305 channel_terminated(plugin);
312#define VirtualChannelEntryEx rdp2tcp_VirtualChannelEntryEx
313FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
318 WINPR_ASSERT(pEntryPointsEx);
320 pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER);
322 freerdp_warn_unmaintained(WLog_Get(TAG),
"CHANNEL_RDP2TCP_CLIENT");
324 Plugin* plugin = (Plugin*)calloc(1,
sizeof(Plugin));
329 plugin->initHandle = pInitHandle;
330 plugin->channelEntryPoints = *pEntryPointsEx;
332 if (init_external_addin(plugin) < 0)
334 channel_terminated(plugin);
339 strncpy(channelDef.name, RDP2TCP_DVC_CHANNEL_NAME,
sizeof(channelDef.name));
341 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
343 if (pEntryPointsEx->pVirtualChannelInitEx(plugin,
nullptr, pInitHandle, &channelDef, 1,
344 VIRTUAL_CHANNEL_VERSION_WIN2000,
345 VirtualChannelInitEventEx) != CHANNEL_RC_OK)
347 channel_terminated(plugin);