|
@@ -33,30 +33,31 @@
|
|
#include <set>
|
|
#include <set>
|
|
|
|
|
|
#include "IpcListener.hpp"
|
|
#include "IpcListener.hpp"
|
|
|
|
+#include "IpcConnection.hpp"
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
#ifdef __WINDOWS__
|
|
#include <WinSock2.h>
|
|
#include <WinSock2.h>
|
|
#include <Windows.h>
|
|
#include <Windows.h>
|
|
#else
|
|
#else
|
|
#include <sys/socket.h>
|
|
#include <sys/socket.h>
|
|
-#include <sys/ud.h>
|
|
|
|
|
|
+#include <sys/un.h>
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
#endif
|
|
|
|
|
|
namespace ZeroTier {
|
|
namespace ZeroTier {
|
|
|
|
|
|
-IpcListener::IpcListener(cosnt char *ep,void (*commandHandler)(void *,const SharedPtr<IpcConnection> &,const char *),void *arg) :
|
|
|
|
|
|
+IpcListener::IpcListener(const char *ep,void (*commandHandler)(void *,const SharedPtr<IpcConnection> &,const char *),void *arg) :
|
|
_endpoint(ep),
|
|
_endpoint(ep),
|
|
_handler(commandHandler),
|
|
_handler(commandHandler),
|
|
_arg(arg),
|
|
_arg(arg),
|
|
_sock(0)
|
|
_sock(0)
|
|
{
|
|
{
|
|
|
|
+#ifdef __WINDOWS__
|
|
|
|
+#else
|
|
struct sockaddr_un unaddr;
|
|
struct sockaddr_un unaddr;
|
|
-
|
|
|
|
unaddr.sun_family = AF_UNIX;
|
|
unaddr.sun_family = AF_UNIX;
|
|
- if ((long)_endpoint.length() > (long)(sizeof(unaddr.sun_path) - 1))
|
|
|
|
- throw std::runtime_error("IPC endpoint path too long");
|
|
|
|
strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
|
|
strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
|
|
|
|
+ unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
|
|
|
|
|
|
for(int tries=0;tries<3;++tries) {
|
|
for(int tries=0;tries<3;++tries) {
|
|
_sock = socket(AF_UNIX,SOCK_STREAM,0);
|
|
_sock = socket(AF_UNIX,SOCK_STREAM,0);
|
|
@@ -85,12 +86,15 @@ IpcListener::IpcListener(cosnt char *ep,void (*commandHandler)(void *,const Shar
|
|
::close(_sock);
|
|
::close(_sock);
|
|
throw std::runtime_error("listen() failed for bound AF_UNIX socket");
|
|
throw std::runtime_error("listen() failed for bound AF_UNIX socket");
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
|
|
|
|
_thread = Thread::start(this);
|
|
_thread = Thread::start(this);
|
|
}
|
|
}
|
|
|
|
|
|
IpcListener::~IpcListener()
|
|
IpcListener::~IpcListener()
|
|
{
|
|
{
|
|
|
|
+#ifdef __WINDOWS__
|
|
|
|
+#else
|
|
int s = _sock;
|
|
int s = _sock;
|
|
_sock = 0;
|
|
_sock = 0;
|
|
if (s > 0) {
|
|
if (s > 0) {
|
|
@@ -99,19 +103,23 @@ IpcListener::~IpcListener()
|
|
}
|
|
}
|
|
Thread::join(_thread);
|
|
Thread::join(_thread);
|
|
unlink(_endpoint.c_str());
|
|
unlink(_endpoint.c_str());
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
void IpcListener::threadMain()
|
|
void IpcListener::threadMain()
|
|
throw()
|
|
throw()
|
|
{
|
|
{
|
|
|
|
+#ifdef __WINDOWS__
|
|
|
|
+#else
|
|
struct sockaddr_un unaddr;
|
|
struct sockaddr_un unaddr;
|
|
socklen_t socklen;
|
|
socklen_t socklen;
|
|
int s;
|
|
int s;
|
|
- unaddr.sun_family = AF_UNIX;
|
|
|
|
- strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
|
|
|
|
while (_sock > 0) {
|
|
while (_sock > 0) {
|
|
|
|
+ unaddr.sun_family = AF_UNIX;
|
|
|
|
+ strncpy(unaddr.sun_path,_endpoint.c_str(),sizeof(unaddr.sun_path));
|
|
|
|
+ unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
|
|
socklen = sizeof(unaddr);
|
|
socklen = sizeof(unaddr);
|
|
- s = accept(_sock,(struct sockaddr *)unaddr,&socklen);
|
|
|
|
|
|
+ s = accept(_sock,(struct sockaddr *)&unaddr,&socklen);
|
|
if (s <= 0)
|
|
if (s <= 0)
|
|
break;
|
|
break;
|
|
if (!_sock) {
|
|
if (!_sock) {
|
|
@@ -119,9 +127,10 @@ void IpcListener::threadMain()
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- _handler(_arg,SharedPtr<IpcConnection>(new IpcConnection(s)),(const char *)0);
|
|
|
|
|
|
+ _handler(_arg,SharedPtr<IpcConnection>(new IpcConnection(s,_handler,_arg)),(const char *)0);
|
|
} catch ( ... ) {} // handlers should not throw
|
|
} catch ( ... ) {} // handlers should not throw
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
} // namespace ZeroTier
|
|
} // namespace ZeroTier
|