1
0
mirror of https://invent.kde.org/network/krfb synced 2024-07-06 01:48:36 +00:00

libvncserver wants to handle the tcp connection itself, so drop QTcpServer & friends

svn path=/trunk/KDE/kdenetwork/krfb/; revision=651193
This commit is contained in:
Alessandro Praduroux 2007-04-06 21:45:19 +00:00
parent 379d4d2fc0
commit cb1280f2b7
5 changed files with 219 additions and 272 deletions

View File

@ -23,97 +23,20 @@
#include "connectioncontroller.h"
#include "connectioncontroller.moc"
#include "events.h"
#include "invitationmanager.h"
#include "connectiondialog.h"
#include "events.h"
#include "krfbserver.h"
#include "framebuffer.h"
#include <X11/Xutil.h>
const int UPDATE_TIME = 100;
static const char* cur=
" "
" x "
" xx "
" xxx "
" xxxx "
" xxxxx "
" xxxxxx "
" xxxxxxx "
" xxxxxxxx "
" xxxxxxxxx "
" xxxxxxxxxx "
" xxxxx "
" xx xxx "
" x xxx "
" xxx "
" xxx "
" xxx "
" xxx "
" ";
static const char* mask=
"xx "
"xxx "
"xxxx "
"xxxxx "
"xxxxxx "
"xxxxxxx "
"xxxxxxxx "
"xxxxxxxxx "
"xxxxxxxxxx "
"xxxxxxxxxxx "
"xxxxxxxxxxxx "
"xxxxxxxxxx "
"xxxxxxxx "
"xxxxxxxx "
"xx xxxxx "
" xxxxx "
" xxxxx "
" xxxxx "
" xxx ";
static rfbCursorPtr myCursor;
static enum rfbNewClientAction newClientHook(struct _rfbClientRec *cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
return cc->handleNewClient(cl);
}
static rfbBool passwordCheck(rfbClientPtr cl,
const char* encryptedPassword,
int len)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
return cc->handleCheckPassword(cl, encryptedPassword, len);
}
static void keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
cc->handleKeyEvent(down ? true : false, keySym);
}
static void pointerHook(int bm, int x, int y, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
cc->handlePointerEvent(bm, x, y);
}
static void clientGoneHook(rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
ConnectionController *cc = static_cast<ConnectionController *>(cl->clientData);
cc->handleClientGone();
}
static void clipboardHook(char* str,int len, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->screen->screenData);
cc->clipboardToServer(QString::fromUtf8(str, len));
}
static bool checkPassword(const QString &p, unsigned char *ochallenge, const char *response, int len)
{
@ -137,17 +60,65 @@ static bool checkPassword(const QString &p, unsigned char *ochallenge, const cha
}
ConnectionController::ConnectionController(int connFd, KrfbServer *parent)
: QObject(parent), fd(connFd), server(parent), fb(0)
ConnectionController::ConnectionController(struct _rfbClientRec *_cl, KrfbServer * parent)
: QObject(parent), cl(_cl)
{
fb = new FrameBuffer(QApplication::desktop()->winId(), this);
cl->clientData = (void*)this;
}
ConnectionController::~ConnectionController()
{
}
enum rfbNewClientAction ConnectionController::handleNewClient()
{
KSharedConfigPtr conf = KGlobal::config();
KConfigGroup srvconf(conf, "Server");
bool allowDesktopControl = srvconf.readEntry("allowDesktopControl",false);
bool askOnConnect = srvconf.readEntry("askOnConnect",true);
int socket = cl->sock;
// cl->negotiationFinishedHook = negotiationFinishedHook; ???
#if 0
// TODO: this drops the connection >.<
QTcpSocket t;
t.setSocketDescriptor(socket); //, QAbstractSocket::ConnectedState, QIODevice::NotOpen);
host = t.peerAddress().toString();
#endif
QString remoteIp;
if (!askOnConnect && InvitationManager::self()->invitations().size() == 0) {
KNotification::event("NewConnectionAutoAccepted",
i18n("Accepted uninvited connection from %1",
remoteIp));
sendSessionEstablished();
return RFB_CLIENT_ACCEPT;
}
KNotification::event("NewConnectionOnHold",
i18n("Received connection from %1, on hold (waiting for confirmation)",
remoteIp));
//cl->screen->authPasswdData = (void *)1;
cl->clientGoneHook = clientGoneHook;
ConnectionDialog *dialog = new ConnectionDialog(0);
dialog->setRemoteHost(remoteIp);
dialog->setAllowRemoteControl( true );
connect(dialog, SIGNAL(okClicked()), SLOT(dialogAccepted()));
connect(dialog, SIGNAL(cancelClicked()), SLOT(dialogRejected()));
dialog->show();
return RFB_CLIENT_ON_HOLD;
}
bool ConnectionController::handleCheckPassword(rfbClientPtr cl, const char *response, int len)
{
KSharedConfigPtr conf = KGlobal::config();
@ -167,6 +138,7 @@ bool ConnectionController::handleCheckPassword(rfbClientPtr cl, const char *resp
QList<Invitation> invlist = InvitationManager::self()->invitations();
foreach(Invitation it, invlist) {
kDebug() << "checking password????!?!?!" << endl;
if (checkPassword(it.password(), cl->authChallenge, response, len) && it.isValid()) {
authd = true;
//configuration->removeInvitation(it);
@ -177,77 +149,22 @@ bool ConnectionController::handleCheckPassword(rfbClientPtr cl, const char *resp
if (!authd) {
if (InvitationManager::self()->invitations().size() > 0) {
sendKNotifyEvent("InvalidPasswordInvitations",
KNotification::event("InvalidPasswordInvitations",
i18n("Failed login attempt from %1: wrong password",
remoteIp));
} else {
sendKNotifyEvent("InvalidPassword",
KNotification::event("InvalidPassword",
i18n("Failed login attempt from %1: wrong password",
remoteIp));
}
return false;
}
#if 0
asyncMutex.lock();
asyncQueue.append(new SessionEstablishedEvent(this));
asyncMutex.unlock();
#endif
return true;
}
enum rfbNewClientAction ConnectionController::handleNewClient(rfbClientPtr cl)
{
KSharedConfigPtr conf = KGlobal::config();
KConfigGroup srvconf(conf, "Server");
bool allowDesktopControl = srvconf.readEntry("allowDesktopControl",false);
bool askOnConnect = srvconf.readEntry("askOnConnect",false);
client = cl;
int socket = cl->sock;
// cl->negotiationFinishedHook = negotiationFinishedHook; ???
QString host;
#if 0
// TODO: this drops the connection >.<
QTcpSocket t;
t.setSocketDescriptor(socket); //, QAbstractSocket::ConnectedState, QIODevice::NotOpen);
host = t.peerAddress().toString();
#endif
remoteIp = host;
if (!askOnConnect && InvitationManager::self()->invitations().size() == 0) {
sendKNotifyEvent("NewConnectionAutoAccepted",
i18n("Accepted uninvited connection from %1",
remoteIp));
sendSessionEstablished();
return RFB_CLIENT_ACCEPT;
}
sendKNotifyEvent("NewConnectionOnHold",
i18n("Received connection from %1, on hold (waiting for confirmation)",
remoteIp));
//cl->screen->authPasswdData = (void *)1;
cl->clientGoneHook = clientGoneHook;
// dialog.setRemoteHost(remoteIp);
// dialog.setAllowRemoteControl( true );
// dialog.setFixedSize(dialog.sizeHint());
// dialog.show();
return RFB_CLIENT_ON_HOLD;
}
void ConnectionController::sendKNotifyEvent(const QString & name, const QString & desc)
{
emit notification(name, desc);
}
void ConnectionController::sendSessionEstablished()
{
@ -268,7 +185,8 @@ void ConnectionController::handlePointerEvent(int bm, int x, int y)
void ConnectionController::handleClientGone()
{
rfbCloseClient(client);
kDebug() << "client gone" << endl;
deleteLater();
}
void ConnectionController::clipboardToServer(const QString &s)
@ -277,69 +195,15 @@ void ConnectionController::clipboardToServer(const QString &s)
ev.exec();
}
void ConnectionController::run()
void ConnectionController::dialogAccepted()
{
kDebug() << "starting server connection" << endl;
connect(this, SIGNAL(sessionEstablished(QString)), server, SIGNAL(sessionEstablished(QString)));
connect(this, SIGNAL(notification(QString,QString)), server, SLOT(handleNotifications(QString, QString)));
int w = fb->width();
int h = fb->height();
int depth = fb->depth();
rfbLogEnable(0);
screen = rfbGetScreen(0, 0, w, h, 8, 3,depth / 8);
screen->screenData = (void *)this;
screen->paddedWidthInBytes = w * 4;
fb->getServerFormat(screen->serverFormat);
screen->frameBuffer = fb->data();
screen->autoPort = true;
screen->inetdSock = fd;
// server hooks
screen->newClientHook = newClientHook;
screen->kbdAddEvent = keyboardHook;
screen->ptrAddEvent = pointerHook;
screen->newClientHook = newClientHook;
screen->passwordCheck = passwordCheck;
screen->setXCutText = clipboardHook;
screen->desktopName = i18n("%1@%2 (shared desktop)", KUser().loginName(), QHostInfo::localHostName()).toLatin1();
if (!myCursor) {
myCursor = rfbMakeXCursor(19, 19, (char*) cur, (char*) mask);
}
screen->cursor = myCursor;
rfbInitServer(screen);
while (true) {
foreach(QRect r, fb->modifiedTiles()) {
rfbMarkRectAsModified(screen, r.top(), r.left(), r.left() + r.width(), r.top() + r.height());
}
fb->modifiedTiles().clear();
rfbProcessEvents(screen, 100);
qApp->processEvents();
}
/*
QTimer *t = new QTimer();
connect(t, SIGNAL(timeout()), SLOT(processEvents()));
rfbProcessEvents(screen, 10);
t->start(UPDATE_TIME);
*/
// rfbStartOnHoldClient(cl);
cl->onHold = false;
}
void ConnectionController::processEvents()
void ConnectionController::dialogRejected()
{
kDebug() << "refused connection" << endl;
rfbRefuseOnHoldClient(cl);
}

View File

@ -10,16 +10,9 @@
#ifndef CONNECTIONCONTROLLER_H
#define CONNECTIONCONTROLLER_H
#include <QThread>
#include <QImage>
#include <QMutex>
#include <QVector>
#include <QRect>
#include <rfb/rfb.h>
class KrfbServer;
class FrameBuffer;
/**
@author Alessandro Praduroux <pradu@pradu.it>
@ -28,13 +21,11 @@ class ConnectionController : public QObject
{
Q_OBJECT
public:
explicit ConnectionController(int connFD, KrfbServer *parent);
ConnectionController(struct _rfbClientRec *_cl, KrfbServer *parent);
~ConnectionController();
bool handleCheckPassword(rfbClientPtr cl, const char *response, int len);
enum rfbNewClientAction handleNewClient(struct _rfbClientRec *cl);
void sendKNotifyEvent(const QString &name, const QString &desc);
void handleNegotiationFinished(struct _rfbClientRec *cl);
void handleKeyEvent(bool down , rfbKeySym keySym );
@ -42,23 +33,27 @@ public:
void handleClientGone();
void clipboardToServer(const QString &);
void sendSessionEstablished();
void run();
enum rfbNewClientAction handleNewClient();
Q_SIGNALS:
void sessionEstablished(QString);
void notification(QString, QString);
public Q_SLOTS:
void processEvents();
protected Q_SLOTS:
void dialogAccepted();
void dialogRejected();
private:
QString remoteIp;
struct _rfbClientRec *cl;
/*
int fd;
KrfbServer *server;
FrameBuffer *fb;
rfbScreenInfoPtr screen;
QString remoteIp;
rfbClientPtr client;
QTcpSocket *tcpConn;
*/
};
#endif

View File

@ -58,6 +58,7 @@ void InvitationManager::loadInvitations()
KConfigGroup invitationConfig(conf, "Invitations");
int numInv = invitationConfig.readEntry("invitation_num",0);
invitationList.clear();
for (int i = 0; i < numInv; i++) {
KConfigGroup ic(conf, QString("Invitation_%1").arg(i));
invitationList.append(Invitation(ic));

View File

@ -25,9 +25,89 @@
#include <KUser>
#include <KLocale>
#include <KStaticDeleter>
#include <KNotification>
#include <KMessageBox>
#include "connectioncontroller.h"
#include "framebuffer.h"
static const char* cur=
" "
" x "
" xx "
" xxx "
" xxxx "
" xxxxx "
" xxxxxx "
" xxxxxxx "
" xxxxxxxx "
" xxxxxxxxx "
" xxxxxxxxxx "
" xxxxx "
" xx xxx "
" x xxx "
" xxx "
" xxx "
" xxx "
" xxx "
" ";
static const char* mask=
"xx "
"xxx "
"xxxx "
"xxxxx "
"xxxxxx "
"xxxxxxx "
"xxxxxxxx "
"xxxxxxxxx "
"xxxxxxxxxx "
"xxxxxxxxxxx "
"xxxxxxxxxxxx "
"xxxxxxxxxx "
"xxxxxxxx "
"xxxxxxxx "
"xx xxxxx "
" xxxxx "
" xxxxx "
" xxxxx "
" xxx ";
static rfbCursorPtr myCursor;
static enum rfbNewClientAction newClientHook(struct _rfbClientRec *cl)
{
KrfbServer *server = KrfbServer::self();
return server->handleNewClient(cl);
}
static rfbBool passwordCheck(rfbClientPtr cl,
const char* encryptedPassword,
int len)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->clientData);
return cc->handleCheckPassword(cl, encryptedPassword, len);
}
static void keyboardHook(rfbBool down, rfbKeySym keySym, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->clientData);
cc->handleKeyEvent(down ? true : false, keySym);
}
static void pointerHook(int bm, int x, int y, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->clientData);
cc->handlePointerEvent(bm, x, y);
}
static void clipboardHook(char* str,int len, rfbClientPtr cl)
{
ConnectionController *cc = static_cast<ConnectionController *>(cl->clientData);
cc->clipboardToServer(QString::fromUtf8(str, len));
}
const int DEFAULT_TCP_PORT = 5900;
@ -42,26 +122,66 @@ KrfbServer * KrfbServer::self() {
KrfbServer::KrfbServer()
{
kDebug() << "starting " << endl;
fb = new FrameBuffer(QApplication::desktop()->winId(), this);
QTimer::singleShot(0, this, SLOT(startListening()));
}
void KrfbServer::startListening() {
void KrfbServer::startListening()
{
rfbScreenInfoPtr screen;
KSharedConfigPtr conf = KGlobal::config();
KConfigGroup tcpConfig(conf, "TCP");
int port = tcpConfig.readEntry("port",DEFAULT_TCP_PORT);
_server = new TcpServer(this);
connect(_server,SIGNAL(connectionReceived(int)),SLOT(newConnection(int)));
int w = fb->width();
int h = fb->height();
int depth = fb->depth();
if (!_server->listen(QHostAddress::Any, port)) {
// TODO: handle error more gracefully
kDebug() << "server listen error" << endl;
deleteLater();
return;
rfbLogEnable(0);
screen = rfbGetScreen(0, 0, w, h, 8, 3,depth / 8);
screen->paddedWidthInBytes = w * 4;
fb->getServerFormat(screen->serverFormat);
screen->frameBuffer = fb->data();
screen->autoPort = 0;
screen->port = port;
// server hooks
screen->newClientHook = newClientHook;
screen->kbdAddEvent = keyboardHook;
screen->ptrAddEvent = pointerHook;
screen->newClientHook = newClientHook;
screen->passwordCheck = passwordCheck;
screen->setXCutText = clipboardHook;
screen->desktopName = i18n("%1@%2 (shared desktop)", KUser().loginName(), QHostInfo::localHostName()).toLatin1().data();
if (!myCursor) {
myCursor = rfbMakeXCursor(19, 19, (char*) cur, (char*) mask);
}
screen->cursor = myCursor;
// TODO: configure password data to handle authentication
screen->authPasswdData = (void *)1;
rfbInitServer(screen);
if (!rfbIsActive(screen)) {
KMessageBox::error(0,"krfb","Address already in use");
disconnectAndQuit();
};
while (true) {
foreach(QRect r, fb->modifiedTiles()) {
rfbMarkRectAsModified(screen, r.top(), r.left(), r.left() + r.width(), r.top() + r.height());
}
fb->modifiedTiles().clear();
rfbProcessEvents(screen, 100);
qApp->processEvents();
}
kDebug() << "server listening on port " << DEFAULT_TCP_PORT << endl;
}
@ -70,12 +190,6 @@ KrfbServer::~KrfbServer()
//delete _controller;
}
void KrfbServer::newConnection(int fdNum)
{
// TODO: get peer address
startServer(fdNum);
}
void KrfbServer::enableDesktopControl(bool enable)
{
// TODO
@ -84,30 +198,16 @@ void KrfbServer::enableDesktopControl(bool enable)
void KrfbServer::disconnectAndQuit()
{
// TODO: cleanup of existing connections
_server->close();
emit quitApp();
}
void KrfbServer::startServer(int fd)
enum rfbNewClientAction KrfbServer::handleNewClient(struct _rfbClientRec * cl)
{
ConnectionController *cc = new ConnectionController(fd, this);
cc->run();
}
ConnectionController *cc = new ConnectionController(cl, this);
TcpServer::TcpServer(QObject * parent)
:QTcpServer(parent)
{
}
connect(cc, SIGNAL(sessionEstablished(QString)), SIGNAL(sessionEstablished(QString)));
void TcpServer::incomingConnection(int fd)
{
emit connectionReceived(fd);
}
void KrfbServer::handleNotifications(QString name, QString desc )
{
KNotification::event(name, desc);
return cc->handleNewClient();
}

View File

@ -11,22 +11,9 @@
#define KRFBSERVER_H
#include <QObject>
#include <QTcpServer>
#include <rfb/rfb.h>
class TcpServer: public QTcpServer {
Q_OBJECT
public:
TcpServer(QObject *parent = 0);
signals:
void connectionReceived(int fd);
protected:
virtual void incomingConnection(int fd);
};
class FrameBuffer;
/**
This class implements the listening server for the RFB protocol.
@ -42,6 +29,8 @@ public:
~KrfbServer();
enum rfbNewClientAction handleNewClient(struct _rfbClientRec *cl);
signals:
void sessionEstablished(QString);
void sessionFinished();
@ -50,18 +39,16 @@ signals:
public Q_SLOTS:
void newConnection(int fd);
void startListening();
void enableDesktopControl(bool);
void disconnectAndQuit();
void handleNotifications(QString, QString);
private:
KrfbServer();
static KrfbServer *_self;
void startServer(int fd);
TcpServer *_server;
FrameBuffer *fb;
};
#endif