Commit c6e95651 authored by Mike Gabriel's avatar Mike Gabriel

nxcomp/Loop.cpp: Add Unix file socket support for proxy <-> proxy connection.

parent b23dcd10
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h>
#include "ChannelEndPoint.h" #include "ChannelEndPoint.h"
...@@ -31,8 +32,21 @@ ...@@ -31,8 +32,21 @@
ChannelEndPoint::ChannelEndPoint(const char *spec) ChannelEndPoint::ChannelEndPoint(const char *spec)
: defaultTCPPort_(0), defaultTCPInterface_(0), : defaultTCPPort_(0), defaultTCPInterface_(0),
defaultUnixPath_(NULL) { defaultUnixPath_(NULL), spec_(NULL) {
spec_ = (spec ? strdup(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 void
...@@ -40,21 +54,92 @@ ChannelEndPoint::setSpec(const char *spec) { ...@@ -40,21 +54,92 @@ ChannelEndPoint::setSpec(const char *spec) {
if (spec_) free(spec_); if (spec_) free(spec_);
if (spec && strlen(spec)) if (spec && strlen(spec))
{
spec_ = strdup(spec); spec_ = strdup(spec);
isUnix_ = getUnixPath();
isTCP_ = getTCPHostAndPort();
}
else else
{
spec_ = NULL; spec_ = NULL;
isUnix_ = false;
isTCP_ = false;
}
} }
void void
ChannelEndPoint::setSpec(int port) { ChannelEndPoint::setSpec(long port) {
if (port >= 0) { if (port >= 0) {
char tmp[20]; char tmp[20];
sprintf(tmp, "%d", port); sprintf(tmp, "%ld", port);
setSpec(tmp); 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); 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 void
ChannelEndPoint::setDefaultTCPPort(long port) { ChannelEndPoint::setDefaultTCPPort(long port) {
defaultTCPPort_ = port; defaultTCPPort_ = port;
...@@ -76,10 +161,12 @@ ChannelEndPoint::setDefaultUnixPath(char *path) { ...@@ -76,10 +161,12 @@ ChannelEndPoint::setDefaultUnixPath(char *path) {
} }
void void
ChannelEndPoint::disable() { setSpec("0"); } ChannelEndPoint::disable() {
setSpec("0");
}
bool bool
ChannelEndPoint::specIsPort(long *port) const { ChannelEndPoint::getPort(long *port) const {
if (port) *port = 0; if (port) *port = 0;
long p = -1; long p = -1;
if (spec_) { if (spec_) {
...@@ -101,7 +188,7 @@ ChannelEndPoint::getUnixPath(char **unixPath) const { ...@@ -101,7 +188,7 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
long p; long p;
char *path = NULL; char *path = NULL;
if (specIsPort(&p)) { if (getPort(&p)) {
if (p != 1) return false; if (p != 1) return false;
} }
else if (spec_ && (strncmp("unix:", spec_, 5) == 0)) { else if (spec_ && (strncmp("unix:", spec_, 5) == 0)) {
...@@ -122,6 +209,11 @@ ChannelEndPoint::getUnixPath(char **unixPath) const { ...@@ -122,6 +209,11 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
return true; return true;
} }
bool
ChannelEndPoint::isUnixSocket() const {
return isUnix_;
}
// FIXME!!! // FIXME!!!
static const char * static const char *
getComputerName() { getComputerName() {
...@@ -158,7 +250,7 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const { ...@@ -158,7 +250,7 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
if (host) *host = NULL; if (host) *host = NULL;
if (port) *port = 0; if (port) *port = 0;
if (specIsPort(&p)) { if (getPort(&p)) {
h_len = 0; h_len = 0;
} }
else if (spec_ && (strncmp("tcp:", spec_, 4) == 0)) { else if (spec_ && (strncmp("tcp:", spec_, 4) == 0)) {
...@@ -194,8 +286,8 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const { ...@@ -194,8 +286,8 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
} }
bool bool
ChannelEndPoint::enabled() const { ChannelEndPoint::isTCPSocket() const {
return (getUnixPath() || getTCPHostAndPort()); return isTCP_;
} }
long ChannelEndPoint::getTCPPort() const { long ChannelEndPoint::getTCPPort() const {
...@@ -205,8 +297,15 @@ long ChannelEndPoint::getTCPPort() const { ...@@ -205,8 +297,15 @@ long ChannelEndPoint::getTCPPort() const {
} }
bool bool
ChannelEndPoint::enabled() const {
return (isUnixSocket() || isTCPSocket());
}
bool
ChannelEndPoint::validateSpec() { ChannelEndPoint::validateSpec() {
return (specIsPort() || getUnixPath() || getTCPHostAndPort()); isTCP_ = getTCPHostAndPort();
isUnix_ = getUnixPath();
return ( getPort() || isUnix_ || isTCP_ );
} }
ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) { ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) {
...@@ -219,26 +318,24 @@ ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) { ...@@ -219,26 +318,24 @@ ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) {
old = spec_; old = spec_;
spec_ = (other.spec_ ? strdup(other.spec_) : NULL); spec_ = (other.spec_ ? strdup(other.spec_) : NULL);
free(old); free(old);
isUnix_ = getUnixPath();
isTCP_ = getTCPHostAndPort();
return *this; return *this;
} }
std::ostream& operator<<(std::ostream& os, const ChannelEndPoint& endPoint) { std::ostream& operator<<(std::ostream& os, const ChannelEndPoint& endPoint) {
if (endPoint.enabled()) { if (endPoint.enabled()) {
char *unixPath, *host; char* endPointSpec = NULL;
long port; if (endPoint.getSpec(&endPointSpec))
if (endPoint.getUnixPath(&unixPath)) { {
os << "unix:" << unixPath; os << endPointSpec;
free(unixPath); free(endPointSpec);
} }
else if (endPoint.getTCPHostAndPort(&host, &port)) { else
os << "tcp:" << host << ":" << port;
free(host);
}
else {
os << "(invalid)"; os << "(invalid)";
}
} }
else { else
{
os << "(disabled)"; os << "(disabled)";
} }
return os; return os;
......
...@@ -33,25 +33,32 @@ class ChannelEndPoint ...@@ -33,25 +33,32 @@ class ChannelEndPoint
int defaultTCPInterface_; // 0=localhost, otherwise IP of public interface. int defaultTCPInterface_; // 0=localhost, otherwise IP of public interface.
char *defaultUnixPath_; char *defaultUnixPath_;
char *spec_; char *spec_;
bool isUnix_;
bool isTCP_;
bool specIsPort(long *port = NULL) const; bool getPort(long *port = NULL) const;
public: public:
ChannelEndPoint(const char *spec = NULL); ChannelEndPoint(const char *spec = NULL);
~ChannelEndPoint();
ChannelEndPoint &operator=(const ChannelEndPoint &other); ChannelEndPoint &operator=(const ChannelEndPoint &other);
bool enabled() const; bool enabled() const;
bool disabled() { return !enabled(); } bool disabled() { return !enabled(); }
void disable(); void disable();
void setSpec(const char *spec); void setSpec(const char *spec);
void setSpec(int port); void setSpec(long port);
void setSpec(const char *hostName, long port);
bool getSpec(char **socketUri) const;
void setDefaultTCPPort(long port); void setDefaultTCPPort(long port);
void setDefaultTCPInterface(int publicInterface); void setDefaultTCPInterface(int publicInterface);
void setDefaultUnixPath(char *path); void setDefaultUnixPath(char *path);
bool getUnixPath(char **path = NULL) const; bool getUnixPath(char **path = NULL) const;
bool isUnixSocket() const;
bool getTCPHostAndPort(char **hostname = NULL, long *port = NULL) const; bool getTCPHostAndPort(char **hostname = NULL, long *port = NULL) const;
long getTCPPort() const; long getTCPPort() const;
bool isTCPSocket() const;
bool validateSpec(); bool validateSpec();
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment