2012-09-16 04:29:33 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-09-16 04:29:33 +04:00
|
|
|
* FreeRDP Windows Server
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2012-10-09 06:31:40 +04:00
|
|
|
* Copyright 2012 Corey Clayton <can.of.tuna@gmail.com>
|
2012-09-16 04:29:33 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-09-16 04:29:33 +04:00
|
|
|
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/windows.h>
|
2014-11-12 22:06:34 +03:00
|
|
|
#include <winpr/winsock.h>
|
2021-08-24 16:04:58 +03:00
|
|
|
#include <winpr/assert.h>
|
2014-03-01 00:27:05 +04:00
|
|
|
|
|
|
|
#include <freerdp/freerdp.h>
|
|
|
|
#include <freerdp/listener.h>
|
|
|
|
#include <freerdp/constants.h>
|
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
|
|
#include <freerdp/channels/channels.h>
|
2015-11-05 16:02:07 +03:00
|
|
|
#include <freerdp/build-config.h>
|
2012-09-16 04:29:33 +04:00
|
|
|
|
|
|
|
#include "wf_peer.h"
|
|
|
|
#include "wf_settings.h"
|
2012-10-05 23:25:30 +04:00
|
|
|
#include "wf_info.h"
|
2012-09-16 04:29:33 +04:00
|
|
|
|
|
|
|
#include "wf_interface.h"
|
|
|
|
|
2017-02-20 16:03:52 +03:00
|
|
|
#define TAG SERVER_TAG("windows")
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define SERVER_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\Server"
|
2015-06-24 12:12:59 +03:00
|
|
|
|
2021-06-16 15:50:14 +03:00
|
|
|
static cbCallback cbEvent = NULL;
|
2012-10-06 07:25:18 +04:00
|
|
|
|
2021-06-16 15:50:14 +03:00
|
|
|
int get_screen_info(int id, _TCHAR* name, size_t length, int* width, int* height, int* bpp)
|
2012-10-24 02:49:03 +04:00
|
|
|
{
|
2021-06-16 15:50:14 +03:00
|
|
|
DISPLAY_DEVICE dd = { 0 };
|
2012-10-24 02:49:03 +04:00
|
|
|
|
|
|
|
dd.cb = sizeof(DISPLAY_DEVICE);
|
|
|
|
|
2012-10-24 03:41:50 +04:00
|
|
|
if (EnumDisplayDevices(NULL, id, &dd, 0) != 0)
|
2012-10-24 02:49:03 +04:00
|
|
|
{
|
2012-10-24 03:41:50 +04:00
|
|
|
HDC dc;
|
|
|
|
|
2012-10-24 04:32:29 +04:00
|
|
|
if (name != NULL)
|
2021-06-16 15:50:14 +03:00
|
|
|
_stprintf_s(name, length, _T("%s (%s)"), dd.DeviceName, dd.DeviceString);
|
2012-10-24 04:32:29 +04:00
|
|
|
|
2013-03-22 00:31:55 +04:00
|
|
|
dc = CreateDC(dd.DeviceName, NULL, NULL, NULL);
|
2012-10-24 03:41:50 +04:00
|
|
|
*width = GetDeviceCaps(dc, HORZRES);
|
|
|
|
*height = GetDeviceCaps(dc, VERTRES);
|
|
|
|
*bpp = GetDeviceCaps(dc, BITSPIXEL);
|
2019-11-06 17:24:51 +03:00
|
|
|
// ReleaseDC(NULL, dc);
|
2013-02-20 01:27:35 +04:00
|
|
|
DeleteDC(dc);
|
2012-10-24 02:49:03 +04:00
|
|
|
}
|
2012-10-24 03:41:50 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2012-10-24 02:49:03 +04:00
|
|
|
}
|
|
|
|
|
2012-10-24 04:32:29 +04:00
|
|
|
void set_screen_id(int id)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return;
|
2012-10-24 04:32:29 +04:00
|
|
|
wfi->screenID = id;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI wf_server_main_loop(LPVOID lpParam)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
|
|
|
freerdp_listener* instance;
|
2012-10-05 23:25:30 +04:00
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to get instance");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-05 23:25:30 +04:00
|
|
|
wfi->force_all_disconnect = FALSE;
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
instance = (freerdp_listener*)lpParam;
|
2021-08-24 16:04:58 +03:00
|
|
|
WINPR_ASSERT(instance);
|
|
|
|
WINPR_ASSERT(instance->GetEventHandles);
|
|
|
|
WINPR_ASSERT(instance->CheckFileDescriptor);
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
while (wfi->force_all_disconnect == FALSE)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
2021-08-24 16:04:58 +03:00
|
|
|
DWORD status;
|
|
|
|
HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
|
|
|
|
DWORD count = instance->GetEventHandles(instance, handles, ARRAYSIZE(handles));
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2021-08-24 16:04:58 +03:00
|
|
|
if (count == 0)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to get FreeRDP file descriptor");
|
2012-09-16 04:29:33 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
|
2021-08-24 16:04:58 +03:00
|
|
|
if (status == WAIT_FAILED)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
2021-08-24 16:04:58 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed");
|
2012-09-16 04:29:33 +04:00
|
|
|
break;
|
2021-08-24 16:04:58 +03:00
|
|
|
}
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (instance->CheckFileDescriptor(instance) != TRUE)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
|
2012-09-16 04:29:33 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "wf_server_main_loop terminating");
|
2012-09-16 04:29:33 +04:00
|
|
|
instance->Close(instance);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL wfreerdp_server_start(wfServer* server)
|
|
|
|
{
|
|
|
|
freerdp_listener* instance;
|
|
|
|
|
|
|
|
server->instance = freerdp_listener_new();
|
|
|
|
server->instance->PeerAccepted = wf_peer_accepted;
|
|
|
|
instance = server->instance;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
wf_settings_read_dword(HKEY_LOCAL_MACHINE, SERVER_KEY, _T("DefaultPort"), &server->port);
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!instance->Open(instance, NULL, (UINT16)server->port))
|
2015-05-05 14:55:48 +03:00
|
|
|
return FALSE;
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(server->thread = CreateThread(NULL, 0, wf_server_main_loop, (void*)instance, 0, NULL)))
|
2015-05-05 14:55:48 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-09-16 04:29:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL wfreerdp_server_stop(wfServer* server)
|
|
|
|
{
|
2012-10-05 23:25:30 +04:00
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return FALSE;
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Stopping server");
|
2012-10-05 23:25:30 +04:00
|
|
|
wfi->force_all_disconnect = TRUE;
|
2012-10-05 00:51:23 +04:00
|
|
|
server->instance->Close(server->instance);
|
2012-09-16 04:29:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wfServer* wfreerdp_server_new()
|
|
|
|
{
|
2014-11-12 22:06:34 +03:00
|
|
|
WSADATA wsaData;
|
2012-09-16 04:29:33 +04:00
|
|
|
wfServer* server;
|
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
|
|
|
return NULL;
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
server = (wfServer*)calloc(1, sizeof(wfServer));
|
2012-09-16 04:29:33 +04:00
|
|
|
|
|
|
|
if (server)
|
|
|
|
{
|
|
|
|
server->port = 3389;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:27:05 +04:00
|
|
|
WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
|
|
|
|
|
2012-10-09 06:31:40 +04:00
|
|
|
cbEvent = NULL;
|
2012-10-06 07:25:18 +04:00
|
|
|
|
2012-09-16 04:29:33 +04:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wfreerdp_server_free(wfServer* server)
|
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(server);
|
2012-09-16 04:29:33 +04:00
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
WSACleanup();
|
2012-09-16 04:29:33 +04:00
|
|
|
}
|
2012-10-05 00:51:23 +04:00
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
BOOL wfreerdp_server_is_running(wfServer* server)
|
2012-10-05 00:51:23 +04:00
|
|
|
{
|
|
|
|
DWORD tStatus;
|
|
|
|
BOOL bRet;
|
|
|
|
|
|
|
|
bRet = GetExitCodeThread(server->thread, &tStatus);
|
|
|
|
if (bRet == 0)
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Error in call to GetExitCodeThread");
|
2012-10-05 00:51:23 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tStatus == STILL_ACTIVE)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-10-06 02:18:59 +04:00
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
UINT32 wfreerdp_server_num_peers()
|
2012-10-06 02:18:59 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return -1;
|
2012-10-06 02:18:59 +04:00
|
|
|
return wfi->peerCount;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
UINT32 wfreerdp_server_get_peer_hostname(int pId, wchar_t* dstStr)
|
2012-10-06 02:18:59 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return 0;
|
2012-10-06 02:18:59 +04:00
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
UINT32 sLen;
|
|
|
|
|
|
|
|
sLen = strnlen_s(peer->hostname, 50);
|
|
|
|
swprintf(dstStr, 50, L"%hs", peer->hostname);
|
|
|
|
return sLen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_WARN(TAG, "nonexistent peer id=%d", pId);
|
2012-10-06 02:18:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-10-06 03:22:38 +04:00
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
BOOL wfreerdp_server_peer_is_local(int pId)
|
2012-10-06 03:22:38 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return FALSE;
|
2012-10-06 03:22:38 +04:00
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->local;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 00:27:05 +04:00
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
BOOL wfreerdp_server_peer_is_connected(int pId)
|
2012-10-06 03:22:38 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return FALSE;
|
2012-10-06 03:22:38 +04:00
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->connected;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
BOOL wfreerdp_server_peer_is_activated(int pId)
|
2012-10-06 03:22:38 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return FALSE;
|
2012-10-06 03:22:38 +04:00
|
|
|
peer = wfi->peers[pId];
|
2015-06-24 12:12:59 +03:00
|
|
|
|
2012-10-06 03:22:38 +04:00
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->activated;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
BOOL wfreerdp_server_peer_is_authenticated(int pId)
|
2012-10-06 03:22:38 +04:00
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!wfi)
|
|
|
|
return FALSE;
|
2012-10-06 03:22:38 +04:00
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->authenticated;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-10-06 07:25:18 +04:00
|
|
|
|
2016-02-27 13:36:34 +03:00
|
|
|
void wfreerdp_server_register_callback_event(cbCallback cb)
|
2012-10-06 07:25:18 +04:00
|
|
|
{
|
2012-10-09 06:31:40 +04:00
|
|
|
cbEvent = cb;
|
2012-10-06 07:25:18 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 06:31:40 +04:00
|
|
|
void wfreerdp_server_peer_callback_event(int pId, UINT32 eType)
|
2012-10-06 07:25:18 +04:00
|
|
|
{
|
2012-10-09 06:31:40 +04:00
|
|
|
if (cbEvent)
|
|
|
|
cbEvent(pId, eType);
|
2012-10-06 07:25:18 +04:00
|
|
|
}
|