diff --git a/include/rfb/rfbclient.h b/include/rfb/rfbclient.h index db8d390..e56a0a8 100644 --- a/include/rfb/rfbclient.h +++ b/include/rfb/rfbclient.h @@ -684,9 +684,6 @@ extern rfbBool errorMessageOnReadFailure; extern rfbBool ReadFromRFBServer(rfbClient* client, char *out, unsigned int n); extern rfbBool WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n); -extern int FindFreeTcpPort(void); -extern rfbSocket ListenAtTcpPort(int port); -extern rfbSocket ListenAtTcpPortAndAddress(int port, const char *address); /** Tries to connect to an IPv4 host. @param host Binary IPv4 address @@ -730,24 +727,12 @@ extern rfbSocket ConnectClientToTcpAddr6WithTimeout(const char *hostname, int po @return A nonblocking socket or RFB_INVALID_SOCKET if the connection failed */ extern rfbSocket ConnectClientToUnixSockWithTimeout(const char *sockFile, unsigned int timeout); -extern rfbSocket AcceptTcpConnection(rfbSocket listenSock); extern rfbBool SetNonBlocking(rfbSocket sock); extern rfbBool SetBlocking(rfbSocket sock); extern rfbBool SetDSCP(rfbSocket sock, int dscp); extern rfbBool StringToIPAddr(const char *str, unsigned int *addr); extern rfbBool SameMachine(rfbSocket sock); -/** - * Waits for an RFB message to arrive from the server. Before handling a message - * with HandleRFBServerMessage(), you must wait for your client to receive one. - * This function blocks until a message is received. You may specify a timeout - * in microseconds. Once this number of microseconds have elapsed, the function - * will return. - * @param client The client to cause to wait until a message is received - * @param usecs The timeout in microseconds - * @return the return value of the underlying select() call - */ -extern int WaitForMessage(rfbClient* client,unsigned int usecs); /* vncviewer.c */ /** @@ -775,38 +760,6 @@ extern int WaitForMessage(rfbClient* client,unsigned int usecs); * @return a pointer to the allocated rfbClient structure */ rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel,int bytesPerPixel); -/** - * Initializes the client. The format is {PROGRAM_NAME, [OPTIONS]..., HOST}. This - * function does not initialize the program name if the rfbClient's program - * name is set already. The options are as follows: - * - * - * - * - * - * - * - * - * - * - *
OptionDescription
-listenListen for incoming connections.
-listennoforkListen for incoming connections without forking. - *
-playSet this client to replay a previously recorded session.
-encodingsSet the encodings to use. The next item in the - * argv array is the encodings string, consisting of comma separated encodings like 'tight,ultra,raw'.
-compressSet the compression level. The next item in the - * argv array is the compression level as an integer. Ranges from 0 (lowest) to 9 (highest). - *
-scaleSet the scaling level. The next item in the - * argv array is the scaling level as an integer. The screen will be scaled down by this factor.
-qosdscpSet the Quality of Service Differentiated Services - * Code Point (QoS DSCP). The next item in the argv array is the code point as - * an integer.
-repeaterdestSet a VNC repeater address. The next item in the argv array is - * the repeater's address as a string.
- * - * The host may include a port number (delimited by a ':'). - * @param client The client to initialize - * @param argc The number of arguments to the initializer - * @param argv The arguments to the initializer as an array of NULL terminated - * strings - * @return true if the client was initialized successfully, false otherwise. - */ -rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv); /** * Cleans up the client structure and releases the memory allocated for it. You * should call this when you're done with the rfbClient structure that you diff --git a/include/rfb/rfbproto.h b/include/rfb/rfbproto.h index 106bca9..31522c8 100644 --- a/include/rfb/rfbproto.h +++ b/include/rfb/rfbproto.h @@ -71,10 +71,6 @@ #endif #endif -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -# define LIBVNCSERVER_WORDS_BIGENDIAN 1 -#endif - /* MS compilers don't have strncasecmp */ #ifdef _MSC_VER #define strncasecmp _strnicmp diff --git a/meson.build b/meson.build index 8641616..b2830f9 100644 --- a/meson.build +++ b/meson.build @@ -78,7 +78,6 @@ sources = [ 'src/buffer.c', 'src/open-h264.c', 'src/cursor.c', - 'src/listen.c', 'src/rfbproto.c', 'src/sockets.c', 'src/vncviewer.c', diff --git a/src/listen.c b/src/listen.c deleted file mode 100644 index ecea567..0000000 --- a/src/listen.c +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (C) 2011-2012 Christian Beier - * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. - * - * This is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this software; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - * USA. - */ - -/* - * listen.c - listen for incoming connections - */ - -#ifdef __STRICT_ANSI__ -#define _BSD_SOURCE -#endif -#include -#include -#include -#include -#include -#include - -/* - * listenForIncomingConnections() - listen for incoming connections from - * servers, and fork a new process to deal with each connection. - */ - -void -listenForIncomingConnections(rfbClient* client) -{ - int listenSocket = RFB_INVALID_SOCKET, listen6Socket = RFB_INVALID_SOCKET; - fd_set fds; - - client->listenSpecified = TRUE; - - listenSocket = ListenAtTcpPortAndAddress(client->listenPort, client->listenAddress); - - if (listenSocket == RFB_INVALID_SOCKET) - return; - - rfbClientLog("%s -listen: Listening on port %d\n", - client->programName,client->listenPort); - rfbClientLog("%s -listen: Command line errors are not reported until " - "a connection comes in.\n", client->programName); - -#ifdef LIBVNCSERVER_IPv6 /* only try that if we're IPv6-capable, otherwise we may try to bind to the same port which would make all that listening fail */ - /* only do IPv6 listen of listen6Port is set */ - if (client->listen6Port != RFB_INVALID_SOCKET) - { - listen6Socket = ListenAtTcpPortAndAddress(client->listen6Port, client->listen6Address); - - if (listen6Socket == RFB_INVALID_SOCKET) - return; - - rfbClientLog("%s -listen: Listening on IPV6 port %d\n", - client->programName,client->listenPort); - rfbClientLog("%s -listen: Command line errors are not reported until " - "a connection comes in.\n", client->programName); - } - - while (TRUE) { - int r; - /* reap any zombies */ - int status, pid; - while ((pid= wait4(-1, &status, WNOHANG, (struct rusage *)0))>0); - - /* TODO: callback for discard any events (like X11 events) */ - - FD_ZERO(&fds); - - if(listenSocket != RFB_INVALID_SOCKET) - FD_SET(listenSocket, &fds); - if(listen6Socket != RFB_INVALID_SOCKET) - FD_SET(listen6Socket, &fds); - - r = select(rfbMax(listenSocket, listen6Socket)+1, &fds, NULL, NULL, NULL); - - if (r > 0) { - if (FD_ISSET(listenSocket, &fds)) - client->sock = AcceptTcpConnection(client->listenSock); - else if (FD_ISSET(listen6Socket, &fds)) - client->sock = AcceptTcpConnection(client->listen6Sock); - - if (client->sock == RFB_INVALID_SOCKET) - return; - if (!SetNonBlocking(client->sock)) - return; - - /* Now fork off a new process to deal with it... */ - - switch (fork()) { - - case -1: - rfbClientErr("fork\n"); - return; - - case 0: - /* child - return to caller */ - rfbCloseSocket(listenSocket); - rfbCloseSocket(listen6Socket); - return; - - default: - /* parent - go round and listen again */ - rfbCloseSocket(client->sock); - break; - } - } - } -#endif -} - - - -/* - * listenForIncomingConnectionsNoFork() - listen for incoming connections - * from servers, but DON'T fork, instead just wait timeout microseconds. - * If timeout is negative, block indefinitely. - * Returns 1 on success (there was an incoming connection on the listen socket - * and we accepted it successfully), -1 on error, 0 on timeout. - */ - -int -listenForIncomingConnectionsNoFork(rfbClient* client, int timeout) -{ - fd_set fds; - struct timeval to; - int r; - - to.tv_sec= timeout / 1000000; - to.tv_usec= timeout % 1000000; - - client->listenSpecified = TRUE; - - if (client->listenSock == RFB_INVALID_SOCKET) - { - client->listenSock = ListenAtTcpPortAndAddress(client->listenPort, client->listenAddress); - - if (client->listenSock == RFB_INVALID_SOCKET) - return -1; - - rfbClientLog("%s -listennofork: Listening on port %d\n", - client->programName,client->listenPort); - rfbClientLog("%s -listennofork: Command line errors are not reported until " - "a connection comes in.\n", client->programName); - } - -#ifdef LIBVNCSERVER_IPv6 /* only try that if we're IPv6-capable, otherwise we may try to bind to the same port which would make all that listening fail */ - /* only do IPv6 listen of listen6Port is set */ - if (client->listen6Port != RFB_INVALID_SOCKET && client->listen6Sock == RFB_INVALID_SOCKET) - { - client->listen6Sock = ListenAtTcpPortAndAddress(client->listen6Port, client->listen6Address); - - if (client->listen6Sock == RFB_INVALID_SOCKET) - return -1; - - rfbClientLog("%s -listennofork: Listening on IPV6 port %d\n", - client->programName,client->listenPort); - rfbClientLog("%s -listennofork: Command line errors are not reported until " - "a connection comes in.\n", client->programName); - } -#endif - - FD_ZERO(&fds); - - if(client->listenSock != RFB_INVALID_SOCKET) - FD_SET(client->listenSock, &fds); - if(client->listen6Sock != RFB_INVALID_SOCKET) - FD_SET(client->listen6Sock, &fds); - - if (timeout < 0) - r = select(rfbMax(client->listenSock, client->listen6Sock) +1, &fds, NULL, NULL, NULL); - else - r = select(rfbMax(client->listenSock, client->listen6Sock) +1, &fds, NULL, NULL, &to); - - if (r > 0) - { - if (FD_ISSET(client->listenSock, &fds)) - client->sock = AcceptTcpConnection(client->listenSock); - else if (FD_ISSET(client->listen6Sock, &fds)) - client->sock = AcceptTcpConnection(client->listen6Sock); - - if (client->sock == RFB_INVALID_SOCKET) - return -1; - if (!SetNonBlocking(client->sock)) - return -1; - - if(client->listenSock != RFB_INVALID_SOCKET) { - rfbCloseSocket(client->listenSock); - client->listenSock = RFB_INVALID_SOCKET; - } - if(client->listen6Sock != RFB_INVALID_SOCKET) { - rfbCloseSocket(client->listen6Sock); - client->listen6Sock = RFB_INVALID_SOCKET; - } - return r; - } - - /* r is now either 0 (timeout) or -1 (error) */ - return r; -} - - diff --git a/src/sockets.c b/src/sockets.c index 3fe9be1..5843c1d 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -43,8 +43,6 @@ void run_main_loop_once(void); -void PrintInHex(char *buf, int len); - rfbBool errorMessageOnReadFailure = TRUE; void ReadToBuffer(rfbClient* client) { @@ -365,189 +363,6 @@ ConnectClientToUnixSockWithTimeout(const char *sockFile, unsigned int timeout) } - -/* - * FindFreeTcpPort tries to find unused TCP port in the range - * (TUNNEL_PORT_OFFSET, TUNNEL_PORT_OFFSET + 99]. Returns 0 on failure. - */ - -int -FindFreeTcpPort(void) -{ - rfbSocket sock; - int port; - struct sockaddr_in addr; - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_ANY); - - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == RFB_INVALID_SOCKET) { - rfbClientErr(": FindFreeTcpPort: socket\n"); - return 0; - } - - for (port = TUNNEL_PORT_OFFSET + 99; port > TUNNEL_PORT_OFFSET; port--) { - addr.sin_port = htons((unsigned short)port); - if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) { - rfbCloseSocket(sock); - return port; - } - } - - rfbCloseSocket(sock); - return 0; -} - - -/* - * ListenAtTcpPort starts listening at the given TCP port. - */ - -rfbSocket -ListenAtTcpPort(int port) -{ - return ListenAtTcpPortAndAddress(port, NULL); -} - - - -/* - * ListenAtTcpPortAndAddress starts listening at the given TCP port on - * the given IP address - */ - -rfbSocket -ListenAtTcpPortAndAddress(int port, const char *address) -{ - rfbSocket sock = RFB_INVALID_SOCKET; - int one = 1; -#ifndef LIBVNCSERVER_IPv6 - struct sockaddr_in addr; - - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - if (address) { - addr.sin_addr.s_addr = inet_addr(address); - } else { - addr.sin_addr.s_addr = htonl(INADDR_ANY); - } - - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == RFB_INVALID_SOCKET) { - rfbClientErr("ListenAtTcpPort: socket\n"); - return RFB_INVALID_SOCKET; - } - - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - (const char *)&one, sizeof(one)) < 0) { - rfbClientErr("ListenAtTcpPort: setsockopt\n"); - rfbCloseSocket(sock); - return RFB_INVALID_SOCKET; - } - - if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - rfbClientErr("ListenAtTcpPort: bind\n"); - rfbCloseSocket(sock); - return RFB_INVALID_SOCKET; - } - -#else - int rv; - struct addrinfo hints, *servinfo, *p; - char port_str[8]; - - snprintf(port_str, 8, "%d", port); - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; /* fill in wildcard address if address == NULL */ - - if ((rv = getaddrinfo(address, port_str, &hints, &servinfo)) != 0) { - rfbClientErr("ListenAtTcpPortAndAddress: error in getaddrinfo: %s\n", gai_strerror(rv)); - return RFB_INVALID_SOCKET; - } - - /* loop through all the results and bind to the first we can */ - for(p = servinfo; p != NULL; p = p->ai_next) { - if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == RFB_INVALID_SOCKET) { - continue; - } - -#ifdef IPV6_V6ONLY - /* we have separate IPv4 and IPv6 sockets since some OS's do not support dual binding */ - if (p->ai_family == AF_INET6 && setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof(one)) < 0) { - rfbClientErr("ListenAtTcpPortAndAddress: error in setsockopt IPV6_V6ONLY: %s\n", strerror(errno)); - rfbCloseSocket(sock); - freeaddrinfo(servinfo); - return RFB_INVALID_SOCKET; - } -#endif - - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) < 0) { - rfbClientErr("ListenAtTcpPortAndAddress: error in setsockopt SO_REUSEADDR: %s\n", strerror(errno)); - rfbCloseSocket(sock); - freeaddrinfo(servinfo); - return RFB_INVALID_SOCKET; - } - - if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) { - rfbCloseSocket(sock); - continue; - } - - break; - } - - if (p == NULL) { - rfbClientErr("ListenAtTcpPortAndAddress: error in bind: %s\n", strerror(errno)); - return RFB_INVALID_SOCKET; - } - - /* all done with this structure now */ - freeaddrinfo(servinfo); -#endif - - if (listen(sock, 5) < 0) { - rfbClientErr("ListenAtTcpPort: listen\n"); - rfbCloseSocket(sock); - return RFB_INVALID_SOCKET; - } - - return sock; -} - - -/* - * AcceptTcpConnection accepts a TCP connection. - */ - -rfbSocket -AcceptTcpConnection(rfbSocket listenSock) -{ - rfbSocket sock; - struct sockaddr_in addr; - socklen_t addrlen = sizeof(addr); - int one = 1; - - sock = accept(listenSock, (struct sockaddr *) &addr, &addrlen); - if (sock == RFB_INVALID_SOCKET) { - rfbClientErr("AcceptTcpConnection: accept\n"); - return RFB_INVALID_SOCKET; - } - - if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - (char *)&one, sizeof(one)) < 0) { - rfbClientErr("AcceptTcpConnection: setsockopt\n"); - rfbCloseSocket(sock); - return RFB_INVALID_SOCKET; - } - - return sock; -} - - /* * SetNonBlocking sets a socket into non-blocking mode. */ @@ -663,73 +478,3 @@ SameMachine(rfbSocket sock) return (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr); } - - -/* - * Print out the contents of a packet for debugging. - */ - -void -PrintInHex(char *buf, int len) -{ - int i, j; - char c, str[17]; - - str[16] = 0; - - rfbClientLog("ReadExact: "); - - for (i = 0; i < len; i++) - { - if ((i % 16 == 0) && (i != 0)) { - rfbClientLog(" "); - } - c = buf[i]; - str[i % 16] = (((c > 31) && (c < 127)) ? c : '.'); - rfbClientLog("%02x ",(unsigned char)c); - if ((i % 4) == 3) - rfbClientLog(" "); - if ((i % 16) == 15) - { - rfbClientLog("%s\n",str); - } - } - if ((i % 16) != 0) - { - for (j = i % 16; j < 16; j++) - { - rfbClientLog(" "); - if ((j % 4) == 3) rfbClientLog(" "); - } - str[i % 16] = 0; - rfbClientLog("%s\n",str); - } - - fflush(stderr); -} - -int WaitForMessage(rfbClient* client,unsigned int usecs) -{ - fd_set fds; - struct timeval timeout; - int num; - - if (client->serverPort==-1) - /* playing back vncrec file */ - return 1; - - timeout.tv_sec=(usecs/1000000); - timeout.tv_usec=(usecs%1000000); - - FD_ZERO(&fds); - FD_SET(client->sock,&fds); - - num=select(client->sock+1, &fds, NULL, NULL, &timeout); - if(num<0) { - rfbClientLog("Waiting for message failed: %d (%s)\n",errno,strerror(errno)); - } - - return num; -} - - diff --git a/src/vncviewer.c b/src/vncviewer.c index 7dbd160..2a7bd43 100644 --- a/src/vncviewer.c +++ b/src/vncviewer.c @@ -347,147 +347,6 @@ rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel, return client; } -static rfbBool rfbInitConnection(rfbClient* client) -{ - /* Unless we accepted an incoming connection, make a TCP connection to the - given VNC server */ - - if (!client->listenSpecified) { - if (!client->serverHost) - return FALSE; - if (client->destHost) { - if (!ConnectToRFBRepeater(client,client->serverHost,client->serverPort,client->destHost,client->destPort)) - return FALSE; - } else { - if (!ConnectToRFBServer(client,client->serverHost,client->serverPort)) - return FALSE; - } - } - - /* Initialise the VNC connection, including reading the password */ - - if (!InitialiseRFBConnection(client)) - return FALSE; - - client->width=client->si.framebufferWidth; - client->height=client->si.framebufferHeight; - if (!client->MallocFrameBuffer(client)) - return FALSE; - - if (!SetFormatAndEncodings(client)) - return FALSE; - - if (client->updateRect.x < 0) { - client->updateRect.x = client->updateRect.y = 0; - client->updateRect.w = client->width; - client->updateRect.h = client->height; - } - - if (client->appData.scaleSetting>1) - { - if (!SendScaleSetting(client, client->appData.scaleSetting)) - return FALSE; - if (!SendFramebufferUpdateRequest(client, - client->updateRect.x / client->appData.scaleSetting, - client->updateRect.y / client->appData.scaleSetting, - client->updateRect.w / client->appData.scaleSetting, - client->updateRect.h / client->appData.scaleSetting, - FALSE)) - return FALSE; - } - else - { - if (!SendFramebufferUpdateRequest(client, - client->updateRect.x, client->updateRect.y, - client->updateRect.w, client->updateRect.h, - FALSE)) - return FALSE; - } - - return TRUE; -} - -rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) { - int i,j; - - if(argv && argc && *argc) { - if(client->programName==0) - client->programName=argv[0]; - - for (i = 1; i < *argc; i++) { - j = i; - if (strcmp(argv[i], "-listen") == 0) { - listenForIncomingConnections(client); - break; - } else if (strcmp(argv[i], "-listennofork") == 0) { - listenForIncomingConnectionsNoFork(client, -1); - break; - } else if (strcmp(argv[i], "-play") == 0) { - client->serverPort = -1; - j++; - } else if (i+1<*argc && strcmp(argv[i], "-encodings") == 0) { - client->appData.encodingsString = argv[i+1]; - j+=2; - } else if (i+1<*argc && strcmp(argv[i], "-compress") == 0) { - client->appData.compressLevel = atoi(argv[i+1]); - j+=2; - } else if (i+1<*argc && strcmp(argv[i], "-quality") == 0) { - client->appData.qualityLevel = atoi(argv[i+1]); - j+=2; - } else if (i+1<*argc && strcmp(argv[i], "-scale") == 0) { - client->appData.scaleSetting = atoi(argv[i+1]); - j+=2; - } else if (i+1<*argc && strcmp(argv[i], "-qosdscp") == 0) { - client->QoS_DSCP = atoi(argv[i+1]); - j+=2; - } else if (i+1<*argc && strcmp(argv[i], "-repeaterdest") == 0) { - char* colon=strchr(argv[i+1],':'); - - if(client->destHost) - free(client->destHost); - client->destPort = 5900; - - client->destHost = strdup(argv[i+1]); - if(client->destHost && colon) { - client->destHost[(int)(colon-argv[i+1])] = '\0'; - client->destPort = atoi(colon+1); - } - j+=2; - } else { - char* colon=strrchr(argv[i],':'); - - if(client->serverHost) - free(client->serverHost); - - if(colon) { - client->serverHost = strdup(argv[i]); - if(client->serverHost) { - client->serverHost[(int)(colon-argv[i])] = '\0'; - client->serverPort = atoi(colon+1); - } - } else { - client->serverHost = strdup(argv[i]); - } - if(client->serverPort >= 0 && client->serverPort < 5900) - client->serverPort += 5900; - } - /* purge arguments */ - if (j>i) { - *argc-=j-i; - memmove(argv+i,argv+j,(*argc-i)*sizeof(char*)); - i--; - } - } - } - - if(!rfbInitConnection(client)) { - rfbClientCleanup(client); - return FALSE; - } - - return TRUE; -} - void rfbClientCleanup(rfbClient* client) { #ifdef LIBVNCSERVER_HAVE_LIBZ int i;