Skip to content

Commit

Permalink
libvncclient: add hooks for custom socket I/O
Browse files Browse the repository at this point in the history
This allows using libvncclient on any kind of custom transport, e.g.
for TLS tunneling via a special TLS socket implementation.
  • Loading branch information
tobydox committed Jul 19, 2021
1 parent 01cdb04 commit 032bd7f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions libvncclient/rfbproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ ConnectToRFBServer(rfbClient* client,const char *hostname, int port)
return TRUE;
}

if(client->ConnectToRFBServer)
{
client->sock = client->ConnectToRFBServer(client, hostname, port);
}
else
#ifndef WIN32
if(IsUnixSocket(hostname))
/* serverHost is a UNIX socket. */
Expand Down
14 changes: 12 additions & 2 deletions libvncclient/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)

while (client->buffered < n) {
int i;
if (client->tlsSession)
if (client->ReadFromSocket)
i = client->ReadFromSocket(client, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
else if (client->tlsSession)
i = ReadFromTLS(client, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
else
#ifdef LIBVNCSERVER_HAVE_SASL
Expand Down Expand Up @@ -186,7 +188,9 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)

while (n > 0) {
int i;
if (client->tlsSession)
if (client->ReadFromSocket)
i = client->ReadFromSocket(client, out, n);
else if (client->tlsSession)
i = ReadFromTLS(client, out, n);
else
#ifdef LIBVNCSERVER_HAVE_SASL
Expand Down Expand Up @@ -262,6 +266,12 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
if (client->serverPort==-1)
return TRUE; /* vncrec playing */

if (client->WriteToSocket) {
i = client->WriteToSocket(client, buf, n);
if (i <= 0) return FALSE;

return TRUE;
}
if (client->tlsSession) {
/* WriteToTLS() will guarantee either everything is written, or error/eof returns */
i = WriteToTLS(client, buf, n);
Expand Down
4 changes: 4 additions & 0 deletions libvncclient/vncviewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,11 @@ void rfbClientCleanup(rfbClient* client) {
free(client->vncRec);

if (client->sock != RFB_INVALID_SOCKET)
{
if (client->CloseSocket)
client->CloseSocket(client);
rfbCloseSocket(client->sock);
}
if (client->listenSock != RFB_INVALID_SOCKET)
rfbCloseSocket(client->listenSock);
free(client->desktopName);
Expand Down
10 changes: 10 additions & 0 deletions rfb/rfbclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ typedef void (*GotBitmapProc)(struct _rfbClient* client, const uint8_t* buffer,
typedef rfbBool (*GotJpegProc)(struct _rfbClient* client, const uint8_t* buffer, int length, int x, int y, int w, int h);
typedef rfbBool (*LockWriteToTLSProc)(struct _rfbClient* client); /** @deprecated */
typedef rfbBool (*UnlockWriteToTLSProc)(struct _rfbClient* client); /** @deprecated */
typedef rfbSocket (*ConnectToRFBServerProc)(struct _rfbClient* client, const char* hostname, int port);
typedef int (*ReadFromSocketProc)(struct _rfbClient* client, char* buf, unsigned int len);
typedef int (*WriteToSocketProc)(struct _rfbClient* client, const char* buf, unsigned int len);
typedef void (*CloseSocketProc)(struct _rfbClient* client);

#ifdef LIBVNCSERVER_HAVE_SASL
typedef char* (*GetUserProc)(struct _rfbClient* client);
Expand Down Expand Up @@ -466,6 +470,12 @@ typedef struct _rfbClient {
* ReadFromRFBServer() - keep at 0 to disable timeout detection and handling */
unsigned int readTimeout;

/** hooks for custom socket I/O */
ConnectToRFBServerProc ConnectToRFBServer;
ReadFromSocketProc ReadFromSocket;
WriteToSocketProc WriteToSocket;
CloseSocketProc CloseSocket;

/**
* Mutex to protect concurrent TLS read/write.
* For internal use only.
Expand Down

0 comments on commit 032bd7f

Please sign in to comment.