1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <stdio.h>
extern "C" {
#define class c_class
#define NEED_EVENTS
#include "X.h"
#include "Xproto.h"
#include "misc.h"
#include "os.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "scrnintstr.h"
#include "selection.h"
#define _VNCEXT_SERVER_
#define _VNCEXT_PROTO_
#include "vncExt.h"
#undef class
#undef xalloc
}
#include <rfb/Configuration.h>
#include <rfb/Logger_stdio.h>
#include <rfb/LogWriter.h>
#include <rfb/util.h>
#include <rfb/ServerCore.h>
#include <rfb/SSecurityFactoryStandard.h>
#include <rdr/HexOutStream.h>
#include <rfb/LogWriter.h>
#undef max
#undef min
#include <network/TcpSocket.h>
#include "XserverDesktop.h"
#include "vncHooks.h"
#include "vncExtInit.h"
extern "C" {
extern void vncExtensionInit();
static void vncResetProc(ExtensionEntry* extEntry);
static void vncBlockHandler(pointer data, OSTimePtr t, pointer readmask);
static void vncWakeupHandler(pointer data, int nfds, pointer readmask);
static void vncClientStateChange(CallbackListPtr*, pointer, pointer);
static void SendSelectionChangeEvent(Atom selection);
static int ProcVncExtDispatch(ClientPtr client);
static int SProcVncExtDispatch(ClientPtr client);
extern char *display;
extern Selection *CurrentSelections;
extern int NumCurrentSelections;
}
using namespace rfb;
static rfb::LogWriter vlog("vncext");
static unsigned long vncExtGeneration = 0;
static bool initialised = false;
static XserverDesktop* desktop[MAXSCREENS] = { 0, };
void* vncFbptr[MAXSCREENS] = { 0, };
static char* clientCutText = 0;
static int clientCutTextLen = 0;
static XserverDesktop* queryConnectDesktop = 0;
static void* queryConnectId = 0;
static int queryConnectTimeout = 0;
static OsTimerPtr queryConnectTimer = 0;
static struct VncInputSelect* vncInputSelectHead = 0;
struct VncInputSelect {
VncInputSelect(ClientPtr c, Window w, int m) : client(c), window(w), mask(m)
{
next = vncInputSelectHead;
vncInputSelectHead = this;
}
ClientPtr client;
Window window;
int mask;
VncInputSelect* next;
};
static int nPrevSelections = 0;
static TimeStamp* prevSelectionTimes = 0;
static int vncErrorBase = 0;
static int vncEventBase = 0;
static char* vncPasswdFile = 0;
int vncInetdSock = -1;
rfb::AliasParameter rfbauth("rfbauth", "Alias for PasswordFile",
&SSecurityFactoryStandard::vncAuthPasswdFile);
rfb::StringParameter httpDir("httpd",
"Directory containing files to serve via HTTP",
"");
rfb::IntParameter httpPort("httpPort", "TCP port to listen for HTTP",0);
rfb::AliasParameter rfbwait("rfbwait", "Alias for ClientWaitTimeMillis",
&rfb::Server::clientWaitTimeMillis);
rfb::IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",0);
rfb::StringParameter desktopName("desktop", "Name of VNC desktop","x11");
rfb::BoolParameter localhostOnly("localhost",
"Only allow connections from localhost",
false);
void vncExtensionInit()
{
if (vncExtGeneration == serverGeneration) {
vlog.error("vncExtensionInit: called twice in same generation?");
return;
}
vncExtGeneration = serverGeneration;
ExtensionEntry* extEntry
= AddExtension(VNCEXTNAME, VncExtNumberEvents, VncExtNumberErrors,
ProcVncExtDispatch, SProcVncExtDispatch, vncResetProc,
StandardMinorOpcode);
if (!extEntry) {
ErrorF("vncExtInit: AddExtension failed\n");
return;
}
vncErrorBase = extEntry->errorBase;
vncEventBase = extEntry->eventBase;
vlog.info("VNC extension running!");
if (!AddCallback(&ClientStateCallback, vncClientStateChange, 0)) {
FatalError("AddCallback failed\n");
}
try {
if (!initialised) {
rfb::initStdIOLoggers();
initialised = true;
}
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (!desktop[scr]) {
network::TcpListener* listener = 0;
network::TcpListener* httpListener = 0;
if (scr == 0 && vncInetdSock != -1) {
if (network::TcpSocket::isSocket(vncInetdSock) &&
!network::TcpSocket::isConnected(vncInetdSock))
{
listener = new network::TcpListener(0, 0, vncInetdSock, true);
vlog.info("inetd wait");
}
} else {
int port = rfbport;
if (port == 0) port = 5900 + atoi(display);
port += 1000 * scr;
listener = new network::TcpListener(port, localhostOnly);
vlog.info("Listening for VNC connections on port %d",port);
CharArray httpDirStr(httpDir.getData());
if (httpDirStr.buf[0]) {
port = httpPort;
if (port == 0) port = 5800 + atoi(display);
port += 1000 * scr;
httpListener = new network::TcpListener(port, localhostOnly);
vlog.info("Listening for HTTP connections on port %d",port);
}
}
CharArray desktopNameStr(desktopName.getData());
desktop[scr] = new XserverDesktop(screenInfo.screens[scr], listener,
httpListener,
desktopNameStr.buf,
vncFbptr[scr]);
vlog.info("created VNC server for screen %d", scr);
if (scr == 0 && vncInetdSock != -1 && !listener) {
network::Socket* sock = new network::TcpSocket(vncInetdSock);
desktop[scr]->addClient(sock, false);
vlog.info("added inetd sock");
}
} else {
desktop[scr]->serverReset(screenInfo.screens[scr]);
}
vncHooksInit(screenInfo.screens[scr], desktop[scr]);
}
RegisterBlockAndWakeupHandlers(vncBlockHandler, vncWakeupHandler, 0);
} catch (rdr::Exception& e) {
vlog.error("vncExtInit: %s",e.str());
}
}
static void vncResetProc(ExtensionEntry* extEntry)
{
}
//
// vncBlockHandler - called just before the X server goes into select(). Call
// on to the block handler for each desktop. Then check whether any of the
// selections have changed, and if so, notify any interested X clients.
//
static void vncBlockHandler(pointer data, OSTimePtr timeout, pointer readmask)
{
fd_set* fds = (fd_set*)readmask;
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (desktop[scr]) {
desktop[scr]->blockHandler(fds);
}
}
if (nPrevSelections != NumCurrentSelections) {
prevSelectionTimes
= (TimeStamp*)xnfrealloc(prevSelectionTimes,
NumCurrentSelections * sizeof(TimeStamp));
for (int i = nPrevSelections; i < NumCurrentSelections; i++) {
prevSelectionTimes[i].months = 0;
prevSelectionTimes[i].milliseconds = 0;
}
nPrevSelections = NumCurrentSelections;
}
for (int i = 0; i < NumCurrentSelections; i++) {
if (CurrentSelections[i].lastTimeChanged.months
!= prevSelectionTimes[i].months ||
CurrentSelections[i].lastTimeChanged.milliseconds
!= prevSelectionTimes[i].milliseconds)
{
SendSelectionChangeEvent(CurrentSelections[i].selection);
prevSelectionTimes[i] = CurrentSelections[i].lastTimeChanged;
}
}
}
static void vncWakeupHandler(pointer data, int nfds, pointer readmask)
{
fd_set* fds = (fd_set*)readmask;
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (desktop[scr]) {
desktop[scr]->wakeupHandler(fds, nfds);
}
}
}
static void vncClientStateChange(CallbackListPtr*, pointer, pointer p)
{
ClientPtr client = ((NewClientInfoRec*)p)->client;
if (client->clientState == ClientStateGone) {
VncInputSelect** nextPtr = &vncInputSelectHead;
for (VncInputSelect* cur = vncInputSelectHead; cur; cur = *nextPtr) {
if (cur->client == client) {
*nextPtr = cur->next;
delete cur;
continue;
}
nextPtr = &cur->next;
}
}
}
void vncBell()
{
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (desktop[scr]) {
desktop[scr]->bell();
}
}
}
void vncClientGone(int fd)
{
if (fd == vncInetdSock) {
fprintf(stderr,"inetdSock client gone\n");
GiveUp(0);
}
}
void vncClientCutText(const char* str, int len)
{
delete [] clientCutText;
clientCutText = new char[len];
memcpy(clientCutText, str, len);
clientCutTextLen = len;
xVncExtClientCutTextNotifyEvent ev;
ev.type = vncEventBase + VncExtClientCutTextNotify;
for (VncInputSelect* cur = vncInputSelectHead; cur; cur = cur->next) {
if (cur->mask & VncExtClientCutTextMask) {
ev.sequenceNumber = cur->client->sequence;
ev.window = cur->window;
ev.time = GetTimeInMillis();
if (cur->client->swapped) {
int n;
swaps(&ev.sequenceNumber, n);
swapl(&ev.window, n);
swapl(&ev.time, n);
}
WriteToClient(cur->client, sizeof(xVncExtClientCutTextNotifyEvent),
(char *)&ev);
}
}
}
static CARD32 queryConnectTimerCallback(OsTimerPtr timer,
CARD32 now, pointer arg)
{
if (queryConnectTimeout)
queryConnectDesktop->approveConnection(queryConnectId, false, "The attempt to prompt the user to accept the connection failed");
// Re-notify clients, causing them to discover that we're done
vncQueryConnect(queryConnectDesktop, queryConnectId);
return 0;
}
void vncQueryConnect(XserverDesktop* desktop, void* opaqueId)
{
// Only one query can be processed at any one time
if (queryConnectTimeout && ((desktop != queryConnectDesktop) ||
(opaqueId != queryConnectId))) {
desktop->approveConnection(opaqueId, false,
"Another connection is currently being queried.");
return;
}
// Get the query timeout. If it's zero, there is no query.
queryConnectTimeout = desktop->getQueryTimeout(opaqueId);
queryConnectId = queryConnectTimeout ? opaqueId : 0;
queryConnectDesktop = queryConnectTimeout ? desktop : 0;
// Notify clients
bool notified = false;
xVncExtQueryConnectNotifyEvent ev;
ev.type = vncEventBase + VncExtQueryConnectNotify;
for (VncInputSelect* cur = vncInputSelectHead; cur; cur = cur->next) {
if (cur->mask & VncExtQueryConnectMask) {
ev.sequenceNumber = cur->client->sequence;
ev.window = cur->window;
if (cur->client->swapped) {
int n;
swaps(&ev.sequenceNumber, n);
swapl(&ev.window, n);
}
WriteToClient(cur->client, sizeof(xVncExtQueryConnectNotifyEvent),
(char *)&ev);
notified = true;
}
}
// If we're being asked to query a connection (rather than to cancel
// a query), and haven't been able to notify clients then reject it.
if (queryConnectTimeout && !notified) {
queryConnectTimeout = 0;
queryConnectId = 0;
queryConnectDesktop = 0;
desktop->approveConnection(opaqueId, false,
"Unable to query the local user to accept the connection.");
return;
}
// Set a timer so that if no-one ever responds, we will eventually
// reject the connection
// NB: We don't set a timer if sock is null, since that indicates
// that pending queries should be cancelled.
if (queryConnectDesktop)
queryConnectTimer = TimerSet(queryConnectTimer, 0,
queryConnectTimeout*2000,
queryConnectTimerCallback, 0);
else
TimerCancel(queryConnectTimer);
}
static void SendSelectionChangeEvent(Atom selection)
{
xVncExtSelectionChangeNotifyEvent ev;
ev.type = vncEventBase + VncExtSelectionChangeNotify;
for (VncInputSelect* cur = vncInputSelectHead; cur; cur = cur->next) {
if (cur->mask & VncExtSelectionChangeMask) {
ev.sequenceNumber = cur->client->sequence;
ev.window = cur->window;
ev.selection = selection;
if (cur->client->swapped) {
int n;
swaps(&ev.sequenceNumber, n);
swapl(&ev.window, n);
swapl(&ev.selection, n);
}
WriteToClient(cur->client, sizeof(xVncExtSelectionChangeNotifyEvent),
(char *)&ev);
}
}
}
static int ProcVncExtSetParam(ClientPtr client)
{
REQUEST(xVncExtSetParamReq);
REQUEST_FIXED_SIZE(xVncExtSetParamReq, stuff->paramLen);
CharArray param(stuff->paramLen+1);
strncpy(param.buf, (char*)&stuff[1], stuff->paramLen);
param.buf[stuff->paramLen] = 0;
xVncExtSetParamReply rep;
int n;
rep.type = X_Reply;
rep.length = 0;
rep.sequenceNumber = client->sequence;
rep.success = rfb::Configuration::setParam(param.buf);
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
}
WriteToClient(client, sizeof(xVncExtSetParamReply), (char *)&rep);
return (client->noClientException);
}
static int SProcVncExtSetParam(ClientPtr client)
{
register char n;
REQUEST(xVncExtSetParamReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xVncExtSetParamReq);
return ProcVncExtSetParam(client);
}
static int ProcVncExtGetParam(ClientPtr client)
{
REQUEST(xVncExtGetParamReq);
REQUEST_FIXED_SIZE(xVncExtGetParamReq, stuff->paramLen);
CharArray param(stuff->paramLen+1);
strncpy(param.buf, (char*)&stuff[1], stuff->paramLen);
param.buf[stuff->paramLen] = 0;
xVncExtGetParamReply rep;
int n;
rep.type = X_Reply;
rep.sequenceNumber = client->sequence;
rep.success = 0;
int len = 0;
char* value = 0;
rfb::VoidParameter* p = rfb::Configuration::getParam(param.buf);
// Hack to avoid exposing password!
if (strcasecmp(param.buf, "Password") == 0)
p = 0;
if (p) {
value = p->getValueStr();
rep.success = 1;
len = value ? strlen(value) : 0;
}
rep.length = (len + 3) >> 2;
rep.valueLen = len;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
swaps(&rep.valueLen, n);
}
WriteToClient(client, sizeof(xVncExtGetParamReply), (char *)&rep);
if (value)
WriteToClient(client, len, value);
delete [] value;
return (client->noClientException);
}
static int SProcVncExtGetParam(ClientPtr client)
{
register char n;
REQUEST(xVncExtGetParamReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xVncExtGetParamReq);
return ProcVncExtGetParam(client);
}
static int ProcVncExtGetParamDesc(ClientPtr client)
{
REQUEST(xVncExtGetParamDescReq);
REQUEST_FIXED_SIZE(xVncExtGetParamDescReq, stuff->paramLen);
CharArray param(stuff->paramLen+1);
strncpy(param.buf, (char*)&stuff[1], stuff->paramLen);
param.buf[stuff->paramLen] = 0;
xVncExtGetParamDescReply rep;
int n;
rep.type = X_Reply;
rep.sequenceNumber = client->sequence;
rep.success = 0;
int len = 0;
const char* desc = 0;
rfb::VoidParameter* p = rfb::Configuration::getParam(param.buf);
if (p) {
desc = p->getDescription();
rep.success = 1;
len = desc ? strlen(desc) : 0;
}
rep.length = (len + 3) >> 2;
rep.descLen = len;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
swaps(&rep.descLen, n);
}
WriteToClient(client, sizeof(xVncExtGetParamDescReply), (char *)&rep);
if (desc)
WriteToClient(client, len, (char*)desc);
return (client->noClientException);
}
static int SProcVncExtGetParamDesc(ClientPtr client)
{
register char n;
REQUEST(xVncExtGetParamDescReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xVncExtGetParamDescReq);
return ProcVncExtGetParamDesc(client);
}
static int ProcVncExtListParams(ClientPtr client)
{
REQUEST(xVncExtListParamsReq);
REQUEST_SIZE_MATCH(xVncExtListParamsReq);
xVncExtListParamsReply rep;
int n;
rep.type = X_Reply;
rep.sequenceNumber = client->sequence;
int nParams = 0;
int len = 0;
for (ParameterIterator i(Configuration::global()); i.param; i.next()) {
int l = strlen(i.param->getName());
if (l <= 255) {
nParams++;
len += l + 1;
}
}
rep.length = (len + 3) >> 2;
rep.nParams = nParams;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
swaps(&rep.nParams, n);
}
WriteToClient(client, sizeof(xVncExtListParamsReply), (char *)&rep);
rdr::U8* data = new rdr::U8[len];
rdr::U8* ptr = data;
for (ParameterIterator i(Configuration::global()); i.param; i.next()) {
int l = strlen(i.param->getName());
if (l <= 255) {
*ptr++ = l;
memcpy(ptr, i.param->getName(), l);
ptr += l;
}
}
WriteToClient(client, len, (char*)data);
delete [] data;
return (client->noClientException);
}
static int SProcVncExtListParams(ClientPtr client)
{
register char n;
REQUEST(xVncExtListParamsReq);
swaps(&stuff->length, n);
REQUEST_SIZE_MATCH(xVncExtListParamsReq);
return ProcVncExtListParams(client);
}
static int ProcVncExtSetServerCutText(ClientPtr client)
{
REQUEST(xVncExtSetServerCutTextReq);
REQUEST_FIXED_SIZE(xVncExtSetServerCutTextReq, stuff->textLen);
char* str = new char[stuff->textLen+1];
strncpy(str, (char*)&stuff[1], stuff->textLen);
str[stuff->textLen] = 0;
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (desktop[scr]) {
desktop[scr]->serverCutText(str, stuff->textLen);
}
}
delete [] str;
return (client->noClientException);
}
static int SProcVncExtSetServerCutText(ClientPtr client)
{
register char n;
REQUEST(xVncExtSetServerCutTextReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xVncExtSetServerCutTextReq);
swapl(&stuff->textLen, n);
return ProcVncExtSetServerCutText(client);
}
static int ProcVncExtGetClientCutText(ClientPtr client)
{
REQUEST(xVncExtGetClientCutTextReq);
REQUEST_SIZE_MATCH(xVncExtGetClientCutTextReq);
xVncExtGetClientCutTextReply rep;
int n;
rep.type = X_Reply;
rep.length = (clientCutTextLen + 3) >> 2;
rep.sequenceNumber = client->sequence;
rep.textLen = clientCutTextLen;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
swapl(&rep.textLen, n);
}
WriteToClient(client, sizeof(xVncExtGetClientCutTextReply), (char *)&rep);
if (clientCutText)
WriteToClient(client, clientCutTextLen, clientCutText);
return (client->noClientException);
}
static int SProcVncExtGetClientCutText(ClientPtr client)
{
register char n;
REQUEST(xVncExtGetClientCutTextReq);
swaps(&stuff->length, n);
REQUEST_SIZE_MATCH(xVncExtGetClientCutTextReq);
return ProcVncExtGetClientCutText(client);
}
static int ProcVncExtSelectInput(ClientPtr client)
{
REQUEST(xVncExtSelectInputReq);
REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
VncInputSelect** nextPtr = &vncInputSelectHead;
VncInputSelect* cur;
for (cur = vncInputSelectHead; cur; cur = *nextPtr) {
if (cur->client == client && cur->window == stuff->window) {
cur->mask = stuff->mask;
if (!cur->mask) {
*nextPtr = cur->next;
delete cur;
}
break;
}
nextPtr = &cur->next;
}
if (!cur) {
cur = new VncInputSelect(client, stuff->window, stuff->mask);
}
return (client->noClientException);
}
static int SProcVncExtSelectInput(ClientPtr client)
{
register char n;
REQUEST(xVncExtSelectInputReq);
swaps(&stuff->length, n);
REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
swapl(&stuff->window, n);
swapl(&stuff->mask, n);
return ProcVncExtSelectInput(client);
}
static int ProcVncExtConnect(ClientPtr client)
{
REQUEST(xVncExtConnectReq);
REQUEST_FIXED_SIZE(xVncExtConnectReq, stuff->strLen);
CharArray str(stuff->strLen+1);
strncpy(str.buf, (char*)&stuff[1], stuff->strLen);
str.buf[stuff->strLen] = 0;
xVncExtConnectReply rep;
rep.success = 0;
if (desktop[0]) {
if (stuff->strLen == 0) {
try {
desktop[0]->disconnectClients();
rep.success = 1;
} catch (rdr::Exception& e) {
vlog.error("Disconnecting all clients: %s",e.str());
}
} else {
int port = 5500;
for (int i = 0; i < stuff->strLen; i++) {
if (str.buf[i] == ':') {
port = atoi(&str.buf[i+1]);
str.buf[i] = 0;
break;
}
}
try {
network::Socket* sock = new network::TcpSocket(str.buf, port);
desktop[0]->addClient(sock, true);
rep.success = 1;
} catch (rdr::Exception& e) {
vlog.error("Reverse connection: %s",e.str());
}
}
}
rep.type = X_Reply;
rep.length = 0;
rep.sequenceNumber = client->sequence;
if (client->swapped) {
int n;
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, n);
}
WriteToClient(client, sizeof(xVncExtConnectReply), (char *)&rep);
return (client->noClientException);
}
static int SProcVncExtConnect(ClientPtr client)
{
register char n;
REQUEST(xVncExtConnectReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xVncExtConnectReq);
return ProcVncExtConnect(client);
}
static int ProcVncExtGetQueryConnect(ClientPtr client)
{
REQUEST(xVncExtGetQueryConnectReq);
REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
const char *qcAddress=0, *qcUsername=0;
int qcTimeout;
if (queryConnectDesktop)
qcTimeout = queryConnectDesktop->getQueryTimeout(queryConnectId,
&qcAddress, &qcUsername);
else
qcTimeout = 0;
xVncExtGetQueryConnectReply rep;
int n;
rep.type = X_Reply;
rep.sequenceNumber = client->sequence;
rep.timeout = qcTimeout;
rep.addrLen = qcTimeout ? strlen(qcAddress) : 0;
rep.userLen = qcTimeout ? strlen(qcUsername) : 0;
rep.opaqueId = (CARD32)(long)queryConnectId;
rep.length = (rep.userLen + rep.addrLen + 3) >> 2;
if (client->swapped) {
swaps(&rep.sequenceNumber, n);
swapl(&rep.userLen, n);
swapl(&rep.addrLen, n);
swapl(&rep.timeout, n);
swapl(&rep.opaqueId, n);
}
WriteToClient(client, sizeof(xVncExtGetQueryConnectReply), (char *)&rep);
if (qcTimeout)
WriteToClient(client, strlen(qcAddress), (char*)qcAddress);
if (qcTimeout)
WriteToClient(client, strlen(qcUsername), (char*)qcUsername);
return (client->noClientException);
}
static int SProcVncExtGetQueryConnect(ClientPtr client)
{
register char n;
REQUEST(xVncExtGetQueryConnectReq);
swaps(&stuff->length, n);
REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
return ProcVncExtGetQueryConnect(client);
}
static int ProcVncExtApproveConnect(ClientPtr client)
{
REQUEST(xVncExtApproveConnectReq);
REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
if (queryConnectId == (void*)stuff->opaqueId) {
for (int scr = 0; scr < screenInfo.numScreens; scr++) {
if (desktop[scr]) {
desktop[scr]->approveConnection(queryConnectId, stuff->approve,
"Connection rejected by local user");
}
}
// Inform other clients of the event and tidy up
vncQueryConnect(queryConnectDesktop, queryConnectId);
}
return (client->noClientException);
}
static int SProcVncExtApproveConnect(ClientPtr client)
{
register char n;
REQUEST(xVncExtApproveConnectReq);
swaps(&stuff->length, n);
swapl(&stuff->opaqueId, n);
REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
return ProcVncExtApproveConnect(client);
}
static int ProcVncExtDispatch(ClientPtr client)
{
REQUEST(xReq);
switch (stuff->data) {
case X_VncExtSetParam:
return ProcVncExtSetParam(client);
case X_VncExtGetParam:
return ProcVncExtGetParam(client);
case X_VncExtGetParamDesc:
return ProcVncExtGetParamDesc(client);
case X_VncExtListParams:
return ProcVncExtListParams(client);
case X_VncExtSetServerCutText:
return ProcVncExtSetServerCutText(client);
case X_VncExtGetClientCutText:
return ProcVncExtGetClientCutText(client);
case X_VncExtSelectInput:
return ProcVncExtSelectInput(client);
case X_VncExtConnect:
return ProcVncExtConnect(client);
case X_VncExtGetQueryConnect:
return ProcVncExtGetQueryConnect(client);
case X_VncExtApproveConnect:
return ProcVncExtApproveConnect(client);
default:
return BadRequest;
}
}
static int SProcVncExtDispatch(ClientPtr client)
{
REQUEST(xReq);
switch (stuff->data) {
case X_VncExtSetParam:
return SProcVncExtSetParam(client);
case X_VncExtGetParam:
return SProcVncExtGetParam(client);
case X_VncExtGetParamDesc:
return SProcVncExtGetParamDesc(client);
case X_VncExtListParams:
return SProcVncExtListParams(client);
case X_VncExtSetServerCutText:
return SProcVncExtSetServerCutText(client);
case X_VncExtGetClientCutText:
return SProcVncExtGetClientCutText(client);
case X_VncExtSelectInput:
return SProcVncExtSelectInput(client);
case X_VncExtConnect:
return SProcVncExtConnect(client);
case X_VncExtGetQueryConnect:
return SProcVncExtGetQueryConnect(client);
case X_VncExtApproveConnect:
return SProcVncExtApproveConnect(client);
default:
return BadRequest;
}
}
|