@@ -35,13 +35,11 @@ namespace network { | |||
Socket(int fd) | |||
: instream(new rdr::FdInStream(fd)), | |||
outstream(new rdr::FdOutStream(fd)), | |||
ownStreams(true), isShutdown_(false), | |||
isShutdown_(false), | |||
queryConnection(false) {} | |||
virtual ~Socket() { | |||
if (ownStreams) { | |||
delete instream; | |||
delete outstream; | |||
} | |||
delete instream; | |||
delete outstream; | |||
} | |||
rdr::FdInStream &inStream() {return *instream;} | |||
rdr::FdOutStream &outStream() {return *outstream;} | |||
@@ -52,30 +50,22 @@ namespace network { | |||
bool isShutdown() const {return isShutdown_;} | |||
virtual bool cork(bool enable) = 0; | |||
// information about this end of the socket | |||
virtual int getMyPort() = 0; | |||
// information about the remote end of the socket | |||
virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1" | |||
virtual int getPeerPort() = 0; | |||
virtual char* getPeerEndpoint() = 0; // <address>::<port> | |||
// Is the remote end on the same machine? | |||
virtual bool sameMachine() = 0; | |||
// Was there a "?" in the ConnectionFilter used to accept this Socket? | |||
void setRequiresQuery() {queryConnection = true;} | |||
bool requiresQuery() const {return queryConnection;} | |||
protected: | |||
Socket() : instream(0), outstream(0), ownStreams(false), | |||
Socket() : instream(0), outstream(0), | |||
isShutdown_(false), queryConnection(false) {} | |||
Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own) | |||
: instream(i), outstream(o), ownStreams(own), | |||
Socket(rdr::FdInStream* i, rdr::FdOutStream* o) | |||
: instream(i), outstream(o), | |||
isShutdown_(false), queryConnection(false) {} | |||
rdr::FdInStream* instream; | |||
rdr::FdOutStream* outstream; | |||
bool ownStreams; | |||
bool isShutdown_; | |||
bool queryConnection; | |||
}; |
@@ -28,21 +28,19 @@ | |||
#else | |||
#define errorNumber errno | |||
#define closesocket close | |||
#include <sys/types.h> | |||
#include <sys/socket.h> | |||
#include <arpa/inet.h> | |||
#include <netinet/tcp.h> | |||
#include <netdb.h> | |||
#include <errno.h> | |||
#include <string.h> | |||
#include <signal.h> | |||
#include <fcntl.h> | |||
#endif | |||
#include <stdlib.h> | |||
#include <unistd.h> | |||
#include <network/TcpSocket.h> | |||
#include <rfb/util.h> | |||
#include <rfb/LogWriter.h> | |||
#include <rfb/Configuration.h> | |||
@@ -120,13 +118,12 @@ static void initSockets() { | |||
// -=- TcpSocket | |||
TcpSocket::TcpSocket(int sock, bool close) | |||
: Socket(new FdInStream(sock), new FdOutStream(sock), true), closeFd(close) | |||
TcpSocket::TcpSocket(int sock) | |||
: Socket(new FdInStream(sock), new FdOutStream(sock)) | |||
{ | |||
} | |||
TcpSocket::TcpSocket(const char *host, int port) | |||
: closeFd(true) | |||
{ | |||
int sock, err, result; | |||
struct addrinfo *ai, *current, hints; | |||
@@ -225,16 +222,10 @@ TcpSocket::TcpSocket(const char *host, int port) | |||
// Create the input and output streams | |||
instream = new FdInStream(sock); | |||
outstream = new FdOutStream(sock); | |||
ownStreams = true; | |||
} | |||
TcpSocket::~TcpSocket() { | |||
if (closeFd) | |||
closesocket(getFd()); | |||
} | |||
int TcpSocket::getMyPort() { | |||
return getSockPort(getFd()); | |||
closesocket(getFd()); | |||
} | |||
char* TcpSocket::getPeerAddress() { | |||
@@ -281,25 +272,20 @@ char* TcpSocket::getPeerAddress() { | |||
return rfb::strDup(""); | |||
} | |||
int TcpSocket::getPeerPort() { | |||
char* TcpSocket::getPeerEndpoint() { | |||
rfb::CharArray address; address.buf = getPeerAddress(); | |||
vnc_sockaddr_t sa; | |||
socklen_t sa_size = sizeof(sa); | |||
int port; | |||
getpeername(getFd(), &sa.u.sa, &sa_size); | |||
switch (sa.u.sa.sa_family) { | |||
case AF_INET6: | |||
return ntohs(sa.u.sin6.sin6_port); | |||
case AF_INET: | |||
return ntohs(sa.u.sin.sin_port); | |||
default: | |||
return 0; | |||
} | |||
} | |||
char* TcpSocket::getPeerEndpoint() { | |||
rfb::CharArray address; address.buf = getPeerAddress(); | |||
int port = getPeerPort(); | |||
if (sa.u.sa.sa_family == AF_INET6) | |||
port = ntohs(sa.u.sin6.sin6_port); | |||
else if (sa.u.sa.sa_family == AF_INET) | |||
port = ntohs(sa.u.sin.sin_port); | |||
else | |||
port = 0; | |||
int buflen = strlen(address.buf) + 32; | |||
char* buffer = new char[buflen]; | |||
@@ -307,31 +293,6 @@ char* TcpSocket::getPeerEndpoint() { | |||
return buffer; | |||
} | |||
bool TcpSocket::sameMachine() { | |||
vnc_sockaddr_t peeraddr, myaddr; | |||
socklen_t addrlen; | |||
addrlen = sizeof(peeraddr); | |||
if (getpeername(getFd(), &peeraddr.u.sa, &addrlen) < 0) | |||
throw SocketException ("unable to get peer address", errorNumber); | |||
addrlen = sizeof(myaddr); /* need to reset, since getpeername overwrote */ | |||
if (getsockname(getFd(), &myaddr.u.sa, &addrlen) < 0) | |||
throw SocketException ("unable to get my address", errorNumber); | |||
if (peeraddr.u.sa.sa_family != myaddr.u.sa.sa_family) | |||
return false; | |||
if (peeraddr.u.sa.sa_family == AF_INET6) | |||
return IN6_ARE_ADDR_EQUAL(&peeraddr.u.sin6.sin6_addr, | |||
&myaddr.u.sin6.sin6_addr); | |||
if (peeraddr.u.sa.sa_family == AF_INET) | |||
return (peeraddr.u.sin.sin_addr.s_addr == myaddr.u.sin.sin_addr.s_addr); | |||
// No idea what this is. Assume we're on different machines. | |||
return false; | |||
} | |||
void TcpSocket::shutdown() | |||
{ | |||
Socket::shutdown(); |
@@ -50,16 +50,12 @@ namespace network { | |||
class TcpSocket : public Socket { | |||
public: | |||
TcpSocket(int sock, bool close=true); | |||
TcpSocket(int sock); | |||
TcpSocket(const char *name, int port); | |||
virtual ~TcpSocket(); | |||
virtual int getMyPort(); | |||
virtual char* getPeerAddress(); | |||
virtual int getPeerPort(); | |||
virtual char* getPeerEndpoint(); | |||
virtual bool sameMachine(); | |||
virtual void shutdown(); | |||
virtual bool cork(bool enable); | |||
@@ -67,8 +63,6 @@ namespace network { | |||
static bool enableNagles(int sock, bool enable); | |||
static bool isListening(int sock); | |||
static int getSockPort(int sock); | |||
private: | |||
bool closeFd; | |||
}; | |||
class TcpListener : public SocketListener { |
@@ -21,13 +21,11 @@ | |||
#include <config.h> | |||
#endif | |||
#include <sys/types.h> | |||
#include <sys/stat.h> | |||
#include <sys/socket.h> | |||
#include <sys/un.h> | |||
#include <unistd.h> | |||
#include <errno.h> | |||
#include <string.h> | |||
#include <signal.h> | |||
#include <fcntl.h> | |||
#include <stdlib.h> | |||
@@ -53,13 +51,12 @@ static void initSockets() { | |||
// -=- UnixSocket | |||
UnixSocket::UnixSocket(int sock, bool close) | |||
: Socket(new FdInStream(sock), new FdOutStream(sock), true), closeFd(close) | |||
UnixSocket::UnixSocket(int sock) | |||
: Socket(new FdInStream(sock), new FdOutStream(sock)) | |||
{ | |||
} | |||
UnixSocket::UnixSocket(const char *path) | |||
: closeFd(true) | |||
{ | |||
int sock, err, result; | |||
sockaddr_un addr; | |||
@@ -94,16 +91,10 @@ UnixSocket::UnixSocket(const char *path) | |||
// Create the input and output streams | |||
instream = new FdInStream(sock); | |||
outstream = new FdOutStream(sock); | |||
ownStreams = true; | |||
} | |||
UnixSocket::~UnixSocket() { | |||
if (closeFd) | |||
close(getFd()); | |||
} | |||
int UnixSocket::getMyPort() { | |||
return 0; | |||
close(getFd()); | |||
} | |||
char* UnixSocket::getPeerAddress() { | |||
@@ -137,18 +128,10 @@ char* UnixSocket::getPeerAddress() { | |||
return rfb::strDup("(unnamed UNIX socket)"); | |||
} | |||
int UnixSocket::getPeerPort() { | |||
return 0; | |||
} | |||
char* UnixSocket::getPeerEndpoint() { | |||
return getPeerAddress(); | |||
} | |||
bool UnixSocket::sameMachine() { | |||
return true; | |||
} | |||
void UnixSocket::shutdown() | |||
{ | |||
Socket::shutdown(); | |||
@@ -208,11 +191,6 @@ UnixListener::UnixListener(const char *path, int mode) | |||
} | |||
} | |||
UnixListener::UnixListener(int sock) | |||
{ | |||
fd = sock; | |||
} | |||
UnixListener::~UnixListener() | |||
{ | |||
struct sockaddr_un addr; |
@@ -35,28 +35,20 @@ namespace network { | |||
class UnixSocket : public Socket { | |||
public: | |||
UnixSocket(int sock, bool close=true); | |||
UnixSocket(int sock); | |||
UnixSocket(const char *name); | |||
virtual ~UnixSocket(); | |||
virtual int getMyPort(); | |||
virtual char* getPeerAddress(); | |||
virtual int getPeerPort(); | |||
virtual char* getPeerEndpoint(); | |||
virtual bool sameMachine(); | |||
virtual void shutdown(); | |||
virtual bool cork(bool enable); | |||
private: | |||
bool closeFd; | |||
}; | |||
class UnixListener : public SocketListener { | |||
public: | |||
UnixListener(const char *listenaddr, int mode); | |||
UnixListener(int sock); | |||
virtual ~UnixListener(); | |||
virtual void shutdown(); |