From d7c0b34b62c8edd302e273588bae7467ecbfb987 Mon Sep 17 00:00:00 2001 From: Ben Klopfenstein Date: Tue, 12 May 2009 01:51:23 +0200 Subject: libvncclient: Unix sockets support by Ben Klopfenstein Signed-off-by: Johannes Schindelin --- libvncclient/rfbproto.c | 29 ++++++++++++++++++++++++----- libvncclient/sockets.c | 29 +++++++++++++++++++++++++++++ rfb/rfbclient.h | 1 + 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/libvncclient/rfbproto.c b/libvncclient/rfbproto.c index 07584f6..5a83e47 100644 --- a/libvncclient/rfbproto.c +++ b/libvncclient/rfbproto.c @@ -29,6 +29,8 @@ #endif #ifndef WIN32 #include +#include +#include #else #define strncasecmp _strnicmp #endif @@ -342,6 +344,15 @@ DefaultSupportedMessagesTightVNC(rfbClient* client) SetServer2Client(client, rfbTextChat); } +static rfbBool +IsUnixSocket(const char *name) +{ + struct stat sb; + if(stat(name, &sb) && (sb.st_mode & S_IFMT) == S_IFSOCK) + return TRUE; + return FALSE; +} + /* * ConnectToRFBServer. */ @@ -378,13 +389,21 @@ ConnectToRFBServer(rfbClient* client,const char *hostname, int port) return TRUE; } - if (!StringToIPAddr(hostname, &host)) { - rfbClientLog("Couldn't convert '%s' to host address\n", hostname); - return FALSE; +#ifndef WIN32 + if(IsUnixSocket(hostname)) + /* serverHost is a UNIX socket. */ + client->sock = ConnectClientToUnixSock(hostname); + else +#endif + { + /* serverHost is a hostname */ + if (!StringToIPAddr(hostname, &host)) { + rfbClientLog("Couldn't convert '%s' to host address\n", hostname); + return FALSE; + } + client->sock = ConnectClientToTcpAddr(host, port); } - client->sock = ConnectClientToTcpAddr(host, port); - if (client->sock < 0) { rfbClientLog("Unable to connect to VNC server\n"); return FALSE; diff --git a/libvncclient/sockets.c b/libvncclient/sockets.c index 7f350e2..5cfc743 100644 --- a/libvncclient/sockets.c +++ b/libvncclient/sockets.c @@ -38,6 +38,7 @@ #else #include #include +#include #include #include #include @@ -297,6 +298,34 @@ ConnectClientToTcpAddr(unsigned int host, int port) return sock; } +int +ConnectClientToUnixSock(const char *sockFile) +{ +#ifdef WIN32 + rfbClientErr("Windows doesn't support UNIX sockets\n"); + return -1; +#else + int sock; + struct sockaddr_un addr; + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, sockFile); + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + rfbClientErr("ConnectToUnixSock: socket (%s)\n",strerror(errno)); + return -1; + } + + if (connect(sock, (struct sockaddr *)&addr, sizeof(addr.sun_family) + strlen(addr.sun_path)) < 0) { + rfbClientErr("ConnectToUnixSock: connect\n"); + close(sock); + return -1; + } + + return sock; +#endif +} + /* diff --git a/rfb/rfbclient.h b/rfb/rfbclient.h index b92f220..be6f322 100644 --- a/rfb/rfbclient.h +++ b/rfb/rfbclient.h @@ -314,6 +314,7 @@ extern rfbBool WriteToRFBServer(rfbClient* client, char *buf, int n); extern int FindFreeTcpPort(void); extern int ListenAtTcpPort(int port); extern int ConnectClientToTcpAddr(unsigned int host, int port); +extern int ConnectClientToUnixSock(const char *sockFile); extern int AcceptTcpConnection(int listenSock); extern rfbBool SetNonBlocking(int sock); -- cgit v1.2.3