Skip to content

Commit dcd031d

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 b0609e9 commit dcd031d

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

libvncclient/rfbproto.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. */

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);

libvncclient/vncviewer.c

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

541541
if (client->sock != RFB_INVALID_SOCKET)
542+
{
543+
if (client->CloseSocket)
544+
client->CloseSocket(client);
542545
rfbCloseSocket(client->sock);
546+
}
543547
if (client->listenSock != RFB_INVALID_SOCKET)
544548
rfbCloseSocket(client->listenSock);
545549
free(client->desktopName);

rfb/rfbclient.h

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

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

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

0 commit comments

Comments
 (0)