aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/ChannelEndPoint.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nxcomp/ChannelEndPoint.cpp')
-rw-r--r--nxcomp/ChannelEndPoint.cpp143
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;