📄 vncserver.cpp
字号:
for (i = m_authClients.begin(); i != m_authClients.end(); i++)
{
// Post the update
GetClient(*i)->UpdatePalette();
}
}
void
vncServer::UpdateLocalFormat()
{
vncClientList::iterator i;
omni_mutex_lock l(m_clientsLock);
// Post this update to all the connected clients
for (i = m_authClients.begin(); i != m_authClients.end(); i++)
{
// Post the update
GetClient(*i)->UpdateLocalFormat();
}
}
void
vncServer::UpdateLocalClipText(LPSTR text)
{
// vnclog.Print(LL_INTINFO, VNCLOG("Lock5"));
omni_mutex_lock l(m_desktopLock);
if (m_desktop != NULL)
m_desktop->SetClipText(text);
}
// Name and port number handling
void
vncServer::SetName(const char * name)
{
// Set the name of the desktop
if (m_name != NULL)
{
free(m_name);
m_name = NULL;
}
m_name = strdup(name);
}
// TightVNC 1.2.7
void
vncServer::SetPorts(const UINT port_rfb, const UINT port_http)
{
if (m_port != port_rfb || m_port_http != port_http) {
// Set port numbers to use
m_port = port_rfb;
m_port_http = port_http;
// If there is already a listening socket then close and re-open it...
BOOL socketon = SockConnected();
SockConnect(FALSE);
if (socketon)
SockConnect(TRUE);
}
}
// RealVNC method
/*
void
vncServer::SetPort(const UINT port)
{
if (m_port != port)
{
/////////////////////////////////
// Adjust the listen socket
// Set the port number to use
m_port = port;
// If there is already a listening socket then close and re-open it...
BOOL socketon = SockConnected();
SockConnect(FALSE);
if (socketon)
SockConnect(TRUE);
}
}
UINT
vncServer::GetPort()
{
return m_port;
}
*/
void
vncServer::SetPassword(const char *passwd)
{
memcpy(m_password, passwd, MAXPWLEN);
}
void
vncServer::GetPassword(char *passwd)
{
memcpy(passwd, m_password, MAXPWLEN);
}
// Remote input handling
void
vncServer::EnableRemoteInputs(BOOL enable)
{
m_enable_remote_inputs = enable;
vncClientList::iterator i;
omni_mutex_lock l(m_clientsLock);
for (i = m_authClients.begin(); i != m_authClients.end(); i++)
{
GetClient(*i)->EnableKeyboard(m_enable_remote_inputs);
GetClient(*i)->EnablePointer(m_enable_remote_inputs);
}
}
BOOL vncServer::RemoteInputsEnabled()
{
return m_enable_remote_inputs;
}
// Local input handling
void
vncServer::DisableLocalInputs(BOOL disable)
{
m_disable_local_inputs = disable;
}
BOOL vncServer::LocalInputsDisabled()
{
return m_disable_local_inputs;
}
// Socket connection handling
BOOL
vncServer::SockConnect(BOOL On)
{
// Are we being asked to switch socket connects on or off?
if (On)
{
// Is there a listening socket?
if (m_socketConn == NULL)
{
m_socketConn = new vncSockConnect();
if (m_socketConn == NULL)
return FALSE;
// Are we to use automatic port selection?
if (m_autoportselect)
{
BOOL ok = FALSE;
// Yes, so cycle through the ports, looking for a free one!
for (int i=0; i < 99; i++)
{
m_port = DISPLAY_TO_PORT(i);
m_port_http = DISPLAY_TO_HPORT(i);
vnclog.Print(LL_CLIENTS, VNCLOG("trying port number %d"), m_port);
// Attempt to connect to the port
VSocket tempsock;
if (tempsock.Create())
{
if (!tempsock.Connect("localhost", m_port))
{
// Couldn't connect, so this port is probably usable!
if (m_socketConn->Init(this, m_port))
{
ok = TRUE;
break;
}
}
}
}
if (!ok)
{
delete m_socketConn;
m_socketConn = NULL;
return FALSE;
}
} else
{
// No autoportselect
if (!m_socketConn->Init(this, m_port))
{
delete m_socketConn;
m_socketConn = NULL;
return FALSE;
}
}
// Now let's start the HTTP connection stuff
EnableHTTPConnect(m_enableHttpConn);
}
}
else
{
// *** JNW - Trying to fix up a lock-up when the listening socket closes
KillAuthClients();
KillUnauthClients();
WaitUntilAuthEmpty();
WaitUntilUnauthEmpty();
// Is there a listening socket?
if (m_socketConn != NULL)
{
// Close the socket
delete m_socketConn;
m_socketConn = NULL;
}
// Is there an HTTP socket active?
EnableHTTPConnect(m_enableHttpConn);
}
return TRUE;
}
BOOL
vncServer::SockConnected()
{
return m_socketConn != NULL;
}
BOOL
vncServer::EnableHTTPConnect(BOOL enable)
{
m_enableHttpConn = enable;
if (enable && m_socketConn)
{
#ifndef ROVNC_QS
if (m_httpConn==NULL && !isQuickSupport)
{
m_httpConn = new vncHTTPConnect;
if (m_httpConn != NULL)
{
// Start up the HTTP server
// TODO: allow applet params handling like in TightVNC 1.2.7
if (!m_httpConn->Init(this,
// PORT_TO_DISPLAY(m_port) + HTTP_PORT_OFFSET)
m_port_http)
)
{
delete m_httpConn;
m_httpConn = NULL;
return FALSE;
}
}
}
#endif
}
else
{
if (m_httpConn != NULL)
{
// Close the socket
delete m_httpConn;
m_httpConn = NULL;
}
}
return TRUE;
}
BOOL
vncServer::SetLoopbackOnly(BOOL loopbackOnly)
{
if (loopbackOnly != m_loopbackOnly)
{
m_loopbackOnly = loopbackOnly;
BOOL socketConn = SockConnected();
SockConnect(FALSE);
SockConnect(socketConn);
}
return TRUE;
}
BOOL
vncServer::LoopbackOnly()
{
return m_loopbackOnly;
}
void
vncServer::GetScreenInfo(int &width, int &height, int &depth)
{
rfbServerInitMsg scrinfo;
// vnclog.Print(LL_INTINFO, VNCLOG("Lock6"));
omni_mutex_lock l(m_desktopLock);
//vnclog.Print(LL_INTINFO, VNCLOG("GetScreenInfo called"));
// Is a desktop object currently active?
// No, so create a dummy desktop and interrogate it
if (m_desktop == NULL)
{
HDC hrootdc;
hrootdc = GetDC(NULL);
if (hrootdc == NULL) {
vnclog.Print(LL_INTERR, VNCLOG("Failed rootdc "));
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
scrinfo.framebufferWidth = GetDeviceCaps(hrootdc, HORZRES);
scrinfo.framebufferHeight = GetDeviceCaps(hrootdc, VERTRES);
HBITMAP membitmap = CreateCompatibleBitmap(hrootdc, scrinfo.framebufferWidth, scrinfo.framebufferHeight);
if (membitmap == NULL) {
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
struct _BMInfo {
BOOL truecolour;
BITMAPINFO bmi;
// Colormap info - comes straight after BITMAPINFO - **HACK**
RGBQUAD cmap[256];
} m_bminfo;
int result;
HDC hmemdc;
memset(&m_bminfo, 0, sizeof(m_bminfo));
m_bminfo.bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bminfo.bmi.bmiHeader.biBitCount = 0;
hmemdc = CreateCompatibleDC(hrootdc);
if (hmemdc == NULL)
{
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
result = ::GetDIBits(hmemdc, membitmap, 0, 1, NULL, &m_bminfo.bmi, DIB_RGB_COLORS);
if (result == 0)
{
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
result = ::GetDIBits(hmemdc, membitmap, 0, 1, NULL, &m_bminfo.bmi, DIB_RGB_COLORS);
if (result == 0)
{
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
scrinfo.format.bitsPerPixel = (CARD8)m_bminfo.bmi.bmiHeader.biBitCount;
if (scrinfo.format.bitsPerPixel==24) scrinfo.format.bitsPerPixel=32;
}
}//result
if (hmemdc != NULL) DeleteDC(hmemdc);
}//memdc
if (membitmap != NULL) DeleteObject(membitmap);
}//membitmap
if (hrootdc != NULL) ReleaseDC(NULL, hrootdc);
}//rootdc
// No, so create a dummy desktop and interrogate it
/* if (!desktop.Init(this))
{
scrinfo.framebufferWidth = 0;
scrinfo.framebufferHeight = 0;
scrinfo.format.bitsPerPixel = 0;
}
else
{
desktop.FillDisplayInfo(&scrinfo);
}*/
}
else
{
m_desktop->FillDisplayInfo(&scrinfo);
}
// Get the info from the scrinfo structure
width = scrinfo.framebufferWidth;
height = scrinfo.framebufferHeight;
depth = scrinfo.format.bitsPerPixel;
}
void
vncServer::SetAuthHosts(const char*hostlist) {
omni_mutex_lock l(m_clientsLock);
if (hostlist == 0) {
vnclog.Print(LL_INTINFO, VNCLOG("authhosts cleared"));
m_auth_hosts = 0;
return;
}
vnclog.Print(LL_INTINFO, VNCLOG("authhosts set to \"%s\""), hostlist);
if (m_auth_hosts != 0)
free(m_auth_hosts);
m_auth_hosts = strdup(hostlist);
}
char*
vncServer::AuthHosts() {
omni_mutex_lock l(m_clientsLock);
if (m_auth_hosts == 0)
return strdup("");
else
return strdup(m_auth_hosts);
}
inline BOOL
MatchStringToTemplate(const char *addr, UINT addrlen,
const char *filtstr, UINT filtlen) {
if (filtlen == 0)
return 1;
if (addrlen < filtlen)
return 0;
for (int x = 0; x < filtlen; x++) {
if (addr[x] != filtstr[x])
return 0;
}
if ((addrlen > filtlen) && (addr[filtlen] != '.'))
return 0;
return 1;
}
vncServer::AcceptQueryReject
vncServer::VerifyHost(const char *hostname) {
omni_mutex_lock l(m_clientsLock);
// -=- Is the specified host blacklisted?
vncServer::BlacklistEntry *current = m_blacklist;
vncServer::BlacklistEntry *previous = 0;
SYSTEMTIME systime;
FILETIME ftime;
LARGE_INTEGER now;
// Get the current time as a 64-bit value
GetSystemTime(&systime);
SystemTimeToFileTime(&systime, &ftime);
now.LowPart=ftime.dwLowDateTime;now.HighPart=ftime.dwHighDateTime;
now.QuadPart /= 10000000; // Convert it into seconds
while (current) {
// Has the blacklist entry timed out?
if ((now.QuadPart - current->_lastRefTime.QuadPart) > 0) {
// Yes. Is it a "blocked" entry?
if (current->_blocked) {
// Yes, so unblock it & re-set the reference time
current->_blocked = FALSE;
current->_lastRefTime.QuadPart = now.QuadPart + 10;
} else {
// No, so remove it
if (previous)
previous->_next = current->_next;
else
m_blacklist = current->_next;
vncServer::BlacklistEntry *next = current->_next;
free(current->_machineName);
delete current;
current = next;
continue;
}
}
// Is this the entry we're interested in?
if ((strcmp(current->_machineName, hostname) == 0) &&
(current->_blocked)) {
// Machine is blocked, so just reject it
return vncServer::aqrReject;
}
previous = current;
current = current->_next;
}
// Has a hostname been specified?
if (hostname == 0) {
vnclog.Print(LL_INTWARN, VNCLOG("verify failed - null hostname"));
return vncServer::aqrReject;
}
// Set the state machine into the correct mode & process the filter
enum vh_Mode {vh_ExpectDelimiter, vh_ExpectIncludeExclude, vh_ExpectPattern};
vh_Mode machineMode = vh_ExpectIncludeExclude;
vncServer::AcceptQueryReject verifiedHost = vncServer::aqrAccept;
vncServer::AcceptQueryReject patternType = vncServer::aqrReject;
UINT authHostsPos = 0;
UINT patternStart = 0;
UINT hostNameLen = strlen(hostname);
// Run through the auth hosts string until we hit the end
if (m_auth_hosts) {
while (1) {
// Which mode are we in?
switch (machineMode) {
// ExpectIncludeExclude - we should see a + or -.
case vh_ExpectIncludeExclude:
if (m_auth_hosts[authHostsPos] == '+') {
patternType = vncServer::aqrAccept;
patternStart = authHostsPos+1;
machineMode = vh_ExpectPattern;
} else if (m_auth_hosts[authHostsPos] == '-') {
patternType = vncServer::aqrReject;
patternStart = authHostsPos+1;
machineMode = vh_ExpectPattern;
} else if (m_auth_hosts[authHostsPos] == '?') {
patternType = vncServer::aqrQuery;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -