diff options
author | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2016-07-05 08:09:24 +0200 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2016-07-05 08:09:24 +0200 |
commit | 854f3fa49351ec72da432f195806162fcf5e5ddc (patch) | |
tree | 2366136004c46641fa72b8a717663c7ac08ac440 /nxcomp/ChannelEndPoint.cpp | |
parent | e0edae04db6dc189512b3a1eda7fe4516af56512 (diff) | |
parent | 5a22f2ec1fc5f40257bb61939f9cad4187e00ab8 (diff) | |
download | nx-libs-854f3fa49351ec72da432f195806162fcf5e5ddc.tar.gz nx-libs-854f3fa49351ec72da432f195806162fcf5e5ddc.tar.bz2 nx-libs-854f3fa49351ec72da432f195806162fcf5e5ddc.zip |
Merge branch 'sunweaver-pr/proxy-to-proxy-over-unix-socket' into 3.6.x
Attributes GH PR #142: https://github.com/ArcticaProject/nx-libs/pull/142
Reviewed by: Salvador Fandino <sfandino@yahoo.com> -- Mon, 04 Jul 2016 16:31:00 +0200
Diffstat (limited to 'nxcomp/ChannelEndPoint.cpp')
-rw-r--r-- | nxcomp/ChannelEndPoint.cpp | 143 |
1 files changed, 120 insertions, 23 deletions
diff --git a/nxcomp/ChannelEndPoint.cpp b/nxcomp/ChannelEndPoint.cpp index cb35e52d2..3e3053927 100644 --- a/nxcomp/ChannelEndPoint.cpp +++ b/nxcomp/ChannelEndPoint.cpp @@ -24,6 +24,7 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> +#include <sys/stat.h> #include "ChannelEndPoint.h" @@ -31,8 +32,21 @@ ChannelEndPoint::ChannelEndPoint(const char *spec) : defaultTCPPort_(0), defaultTCPInterface_(0), - defaultUnixPath_(NULL) { - spec_ = (spec ? strdup(spec) : NULL); + defaultUnixPath_(NULL), spec_(NULL) { + setSpec(spec); +} + +ChannelEndPoint::~ChannelEndPoint() +{ + char *unixPath = NULL; + + if (getUnixPath(&unixPath)) + { + struct stat st; + lstat(unixPath, &st); + if(S_ISSOCK(st.st_mode)) + unlink(unixPath); + } } void @@ -40,21 +54,92 @@ ChannelEndPoint::setSpec(const char *spec) { if (spec_) free(spec_); if (spec && strlen(spec)) + { spec_ = strdup(spec); + isUnix_ = getUnixPath(); + isTCP_ = getTCPHostAndPort(); + } else + { spec_ = NULL; + isUnix_ = false; + isTCP_ = false; + } } void -ChannelEndPoint::setSpec(int port) { +ChannelEndPoint::setSpec(long port) { if (port >= 0) { char tmp[20]; - sprintf(tmp, "%d", port); + sprintf(tmp, "%ld", port); setSpec(tmp); } + else { + disable(); + } +} + +void +ChannelEndPoint::setSpec(const char *hostName, long port) { + int length; + + if (spec_) free(spec_); + isUnix_ = false; + isTCP_ = false; + + if (hostName && strlen(hostName) && port >= 1) + { + length = snprintf(NULL, 0, "tcp:%s:%ld", hostName, port); + spec_ = (char *)calloc(length + 1, sizeof(char)); + snprintf(spec_, length+1, "tcp:%s:%ld", hostName, port); + isTCP_ = true; + } else setSpec((char*)NULL); } +bool +ChannelEndPoint::getSpec(char **socketUri) const { + + if (socketUri) *socketUri = NULL; + + char *unixPath = NULL; + char *hostName = NULL; + long port = -1; + + char *newSocketUri = NULL; + int length = -1; + + if (getUnixPath(&unixPath)) + { + length = snprintf(NULL, 0, "unix:%s", unixPath); + } + else if (getTCPHostAndPort(&hostName, &port)) + { + length = snprintf(NULL, 0, "tcp:%s:%ld", hostName, port); + } + + if (length > 0) + { + newSocketUri = (char *)calloc(length + 1, sizeof(char)); + if (isUnixSocket()) + snprintf(newSocketUri, length+1, "unix:%s", unixPath); + else + snprintf(newSocketUri, length+1, "tcp:%s:%ld", hostName, port); + + if (socketUri) + *socketUri = strdup(newSocketUri); + } + + free(newSocketUri); + free(unixPath); + free(hostName); + + if (*socketUri != '\0') + return true; + + return false; +} + void ChannelEndPoint::setDefaultTCPPort(long port) { defaultTCPPort_ = port; @@ -76,10 +161,12 @@ ChannelEndPoint::setDefaultUnixPath(char *path) { } void -ChannelEndPoint::disable() { setSpec("0"); } +ChannelEndPoint::disable() { + setSpec("0"); +} bool -ChannelEndPoint::specIsPort(long *port) const { +ChannelEndPoint::getPort(long *port) const { if (port) *port = 0; long p = -1; if (spec_) { @@ -101,7 +188,7 @@ ChannelEndPoint::getUnixPath(char **unixPath) const { long p; char *path = NULL; - if (specIsPort(&p)) { + if (getPort(&p)) { if (p != 1) return false; } else if (spec_ && (strncmp("unix:", spec_, 5) == 0)) { @@ -122,6 +209,11 @@ ChannelEndPoint::getUnixPath(char **unixPath) const { return true; } +bool +ChannelEndPoint::isUnixSocket() const { + return isUnix_; +} + // FIXME!!! static const char * getComputerName() { @@ -158,7 +250,7 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const { if (host) *host = NULL; if (port) *port = 0; - if (specIsPort(&p)) { + if (getPort(&p)) { h_len = 0; } else if (spec_ && (strncmp("tcp:", spec_, 4) == 0)) { @@ -194,8 +286,8 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const { } bool -ChannelEndPoint::enabled() const { - return (getUnixPath() || getTCPHostAndPort()); +ChannelEndPoint::isTCPSocket() const { + return isTCP_; } long ChannelEndPoint::getTCPPort() const { @@ -205,8 +297,15 @@ long ChannelEndPoint::getTCPPort() const { } bool +ChannelEndPoint::enabled() const { + return (isUnixSocket() || isTCPSocket()); +} + +bool ChannelEndPoint::validateSpec() { - return (specIsPort() || getUnixPath() || getTCPHostAndPort()); + isTCP_ = getTCPHostAndPort(); + isUnix_ = getUnixPath(); + return ( getPort() || isUnix_ || isTCP_ ); } ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) { @@ -219,26 +318,24 @@ ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) { old = spec_; spec_ = (other.spec_ ? strdup(other.spec_) : NULL); free(old); + isUnix_ = getUnixPath(); + isTCP_ = getTCPHostAndPort(); return *this; } std::ostream& operator<<(std::ostream& os, const ChannelEndPoint& endPoint) { if (endPoint.enabled()) { - char *unixPath, *host; - long port; - if (endPoint.getUnixPath(&unixPath)) { - os << "unix:" << unixPath; - free(unixPath); + char* endPointSpec = NULL; + if (endPoint.getSpec(&endPointSpec)) + { + os << endPointSpec; + free(endPointSpec); } - else if (endPoint.getTCPHostAndPort(&host, &port)) { - os << "tcp:" << host << ":" << port; - free(host); - } - else { + else os << "(invalid)"; - } } - else { + else + { os << "(disabled)"; } return os; |