Skip to content

Commit 0ee9b2a

Browse files
committed
libvncclient: add hooks for custom socket I/O
This allows using libvncclient on any kind of custom transport, e.g. for TLS tunneling via a special TLS socket implementation.
1 parent b5dfe0d commit 0ee9b2a

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

include/rfb/rfbclient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ typedef void (*GotBitmapProc)(struct _rfbClient* client, const uint8_t* buffer,
233233
typedef rfbBool (*GotJpegProc)(struct _rfbClient* client, const uint8_t* buffer, int length, int x, int y, int w, int h);
234234
typedef rfbBool (*LockWriteToTLSProc)(struct _rfbClient* client); /** @deprecated */
235235
typedef rfbBool (*UnlockWriteToTLSProc)(struct _rfbClient* client); /** @deprecated */
236+
typedef rfbSocket (*ConnectToRFBServerProc)(struct _rfbClient* client, const char* hostname, int port);
237+
typedef int (*ReadFromSocketProc)(struct _rfbClient* client, char* buf, unsigned int len);
238+
typedef int (*WriteToSocketProc)(struct _rfbClient* client, const char* buf, unsigned int len);
239+
typedef void (*CloseSocketProc)(struct _rfbClient* client);
236240

237241
#ifdef LIBVNCSERVER_HAVE_SASL
238242
typedef char* (*GetUserProc)(struct _rfbClient* client);
@@ -467,6 +471,12 @@ typedef struct _rfbClient {
467471
* ReadFromRFBServer() - keep at 0 to disable timeout detection and handling */
468472
unsigned int readTimeout;
469473

474+
/** hooks for custom socket I/O */
475+
ConnectToRFBServerProc ConnectToRFBServer;
476+
ReadFromSocketProc ReadFromSocket;
477+
WriteToSocketProc WriteToSocket;
478+
CloseSocketProc CloseSocket;
479+
470480
/**
471481
* Mutex to protect concurrent TLS read/write.
472482
* For internal use only.

src/libvncclient/rfbclient.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ ConnectToRFBServer(rfbClient* client,const char *hostname, int port)
324324
return TRUE;
325325
}
326326

327+
if(client->ConnectToRFBServer)
328+
{
329+
client->sock = client->ConnectToRFBServer(client, hostname, port);
330+
}
331+
else
327332
#ifndef WIN32
328333
if(IsUnixSocket(hostname))
329334
/* serverHost is a UNIX socket. */

src/libvncclient/sockets.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
134134

135135
while (client->buffered < n) {
136136
int i;
137-
if (client->tlsSession)
137+
if (client->ReadFromSocket)
138+
i = client->ReadFromSocket(client, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
139+
else if (client->tlsSession)
138140
i = ReadFromTLS(client, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
139141
else
140142
#ifdef LIBVNCSERVER_HAVE_SASL
@@ -186,7 +188,9 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
186188

187189
while (n > 0) {
188190
int i;
189-
if (client->tlsSession)
191+
if (client->ReadFromSocket)
192+
i = client->ReadFromSocket(client, out, n);
193+
else if (client->tlsSession)
190194
i = ReadFromTLS(client, out, n);
191195
else
192196
#ifdef LIBVNCSERVER_HAVE_SASL
@@ -262,6 +266,12 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
262266
if (client->serverPort==-1)
263267
return TRUE; /* vncrec playing */
264268

269+
if (client->WriteToSocket) {
270+
i = client->WriteToSocket(client, buf, n);
271+
if (i <= 0) return FALSE;
272+
273+
return TRUE;
274+
}
265275
if (client->tlsSession) {
266276
/* WriteToTLS() will guarantee either everything is written, or error/eof returns */
267277
i = WriteToTLS(client, buf, n);

src/libvncclient/vncviewer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,11 @@ void rfbClientCleanup(rfbClient* client) {
552552
free(client->vncRec);
553553

554554
if (client->sock != RFB_INVALID_SOCKET)
555+
{
556+
if (client->CloseSocket)
557+
client->CloseSocket(client);
555558
rfbCloseSocket(client->sock);
559+
}
556560
if (client->listenSock != RFB_INVALID_SOCKET)
557561
rfbCloseSocket(client->listenSock);
558562
if (client->listen6Sock != RFB_INVALID_SOCKET)

0 commit comments

Comments
 (0)