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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-10-05 00:51:23 +04:00
|
|
|
|
2012-09-16 04:29:33 +04:00
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
#include <freerdp/utils/tcp.h>
|
2012-10-09 06:31:40 +04:00
|
|
|
#include <freerdp\listener.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"
|
|
|
|
|
2012-10-09 06:31:40 +04:00
|
|
|
cbCallback cbEvent;
|
2012-10-06 07:25:18 +04:00
|
|
|
|
2012-10-24 03:41:50 +04:00
|
|
|
int get_screen_info(int id, _TCHAR* name, int* width, int* height, int* bpp)
|
2012-10-24 02:49:03 +04:00
|
|
|
{
|
|
|
|
DISPLAY_DEVICE dd;
|
|
|
|
|
|
|
|
memset(&dd, 0, sizeof(DISPLAY_DEVICE));
|
|
|
|
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)
|
2012-10-25 23:25:55 +04:00
|
|
|
_stprintf(name, _T("%s (%s)"), dd.DeviceName, dd.DeviceString);
|
2012-10-24 04:32:29 +04:00
|
|
|
|
2012-10-24 03:41:50 +04:00
|
|
|
dc = CreateDC(NULL, dd.DeviceName, NULL, NULL);
|
|
|
|
*width = GetDeviceCaps(dc, HORZRES);
|
|
|
|
*height = GetDeviceCaps(dc, VERTRES);
|
|
|
|
*bpp = GetDeviceCaps(dc, BITSPIXEL);
|
2013-02-20 01:27:35 +04:00
|
|
|
//ReleaseDC(NULL, dc);
|
|
|
|
DeleteDC(dc);
|
2012-10-24 03:41:50 +04:00
|
|
|
|
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();
|
|
|
|
wfi->screenID = id;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-16 04:29:33 +04:00
|
|
|
DWORD WINAPI wf_server_main_loop(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
int i, fds;
|
|
|
|
int rcount;
|
|
|
|
int max_fds;
|
|
|
|
void* rfds[32];
|
|
|
|
fd_set rfds_set;
|
|
|
|
freerdp_listener* instance;
|
2012-10-05 23:25:30 +04:00
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
wfi->force_all_disconnect = FALSE;
|
2012-09-16 04:29:33 +04:00
|
|
|
|
|
|
|
ZeroMemory(rfds, sizeof(rfds));
|
|
|
|
instance = (freerdp_listener*) lpParam;
|
|
|
|
|
2012-10-09 06:31:40 +04:00
|
|
|
while(wfi->force_all_disconnect == FALSE)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
|
|
|
rcount = 0;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (instance->GetFileDescriptor(instance, rfds, &rcount) != TRUE)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
|
|
|
printf("Failed to get FreeRDP file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_fds = 0;
|
|
|
|
FD_ZERO(&rfds_set);
|
|
|
|
|
|
|
|
for (i = 0; i < rcount; i++)
|
|
|
|
{
|
|
|
|
fds = (int)(long)(rfds[i]);
|
|
|
|
|
|
|
|
if (fds > max_fds)
|
|
|
|
max_fds = fds;
|
|
|
|
|
|
|
|
FD_SET(fds, &rfds_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_fds == 0)
|
|
|
|
break;
|
|
|
|
|
2012-10-05 00:51:23 +04:00
|
|
|
|
2012-09-16 04:29:33 +04:00
|
|
|
select(max_fds + 1, &rfds_set, NULL, NULL, NULL);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (instance->CheckFileDescriptor(instance) != TRUE)
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
|
|
|
printf("Failed to check FreeRDP file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-05 00:51:23 +04:00
|
|
|
printf("wf_server_main_loop terminating\n");
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
wf_settings_read_dword(HKEY_LOCAL_MACHINE, _T("Software\\FreeRDP\\Server"), _T("DefaultPort"), &server->port);
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
if (instance->Open(instance, NULL, (UINT16) server->port))
|
2012-09-16 04:29:33 +04:00
|
|
|
{
|
|
|
|
server->thread = CreateThread(NULL, 0, wf_server_main_loop, (void*) instance, 0, NULL);
|
2012-09-16 23:30:11 +04:00
|
|
|
return TRUE;
|
2012-09-16 04:29:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
return FALSE;
|
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();
|
|
|
|
|
|
|
|
printf("Stopping server\n");
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
wfServer* server;
|
|
|
|
|
|
|
|
server = (wfServer*) malloc(sizeof(wfServer));
|
|
|
|
ZeroMemory(server, sizeof(wfServer));
|
|
|
|
|
|
|
|
freerdp_wsa_startup();
|
|
|
|
|
|
|
|
if (server)
|
|
|
|
{
|
|
|
|
server->port = 3389;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (server)
|
|
|
|
{
|
|
|
|
free(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_wsa_cleanup();
|
|
|
|
}
|
2012-10-05 00:51:23 +04:00
|
|
|
|
|
|
|
|
|
|
|
FREERDP_API BOOL wfreerdp_server_is_running(wfServer* server)
|
|
|
|
{
|
|
|
|
DWORD tStatus;
|
|
|
|
BOOL bRet;
|
|
|
|
|
|
|
|
bRet = GetExitCodeThread(server->thread, &tStatus);
|
|
|
|
if (bRet == 0)
|
|
|
|
{
|
|
|
|
printf("Error in call to GetExitCodeThread\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tStatus == STILL_ACTIVE)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-10-06 02:18:59 +04:00
|
|
|
|
|
|
|
FREERDP_API UINT32 wfreerdp_server_num_peers()
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
return wfi->peerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREERDP_API UINT32 wfreerdp_server_get_peer_hostname(int pId, wchar_t * dstStr)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
UINT32 sLen;
|
|
|
|
|
|
|
|
sLen = strnlen_s(peer->hostname, 50);
|
|
|
|
swprintf(dstStr, 50, L"%hs", peer->hostname);
|
|
|
|
return sLen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 06:31:40 +04:00
|
|
|
printf("nonexistent peer id=%d\n", pId);
|
2012-10-06 02:18:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-10-06 03:22:38 +04:00
|
|
|
|
|
|
|
FREERDP_API BOOL wfreerdp_server_peer_is_local(int pId)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->local;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FREERDP_API BOOL wfreerdp_server_peer_is_connected(int pId)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->connected;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FREERDP_API BOOL wfreerdp_server_peer_is_activated(int pId)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->activated;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FREERDP_API BOOL wfreerdp_server_peer_is_authenticated(int pId)
|
|
|
|
{
|
|
|
|
wfInfo* wfi;
|
|
|
|
freerdp_peer* peer;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
peer = wfi->peers[pId];
|
|
|
|
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
return peer->authenticated;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-10-06 07:25:18 +04:00
|
|
|
|
2012-10-09 06:31:40 +04:00
|
|
|
FREERDP_API 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
|
|
|
}
|
|
|
|
|