mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-21 05:43:59 -07:00
rename ipc/ to control/
This commit is contained in:
parent
13aba7640b
commit
2dcf584834
8 changed files with 0 additions and 0 deletions
224
control/IpcConnection.cpp
Normal file
224
control/IpcConnection.cpp
Normal file
|
@ -0,0 +1,224 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "IpcConnection.hpp"
|
||||
|
||||
#ifndef __WINDOWS__
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
|
||||
_handler(commandHandler),
|
||||
_arg(arg),
|
||||
#ifdef __WINDOWS__
|
||||
_sock(INVALID_HANDLE_VALUE),
|
||||
_incoming(false),
|
||||
#else
|
||||
_sock(-1),
|
||||
#endif
|
||||
_run(true),
|
||||
_running(true)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
_sock = CreateFileA(endpoint,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,NULL);
|
||||
if (_sock == INVALID_HANDLE_VALUE)
|
||||
throw std::runtime_error("IPC endpoint unreachable");
|
||||
DWORD pipeMode = PIPE_READMODE_BYTE;
|
||||
SetNamedPipeHandleState(_sock,&pipeMode,NULL,NULL);
|
||||
#else
|
||||
struct sockaddr_un unaddr;
|
||||
unaddr.sun_family = AF_UNIX;
|
||||
strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path));
|
||||
unaddr.sun_path[sizeof(unaddr.sun_path) - 1] = (char)0;
|
||||
|
||||
_sock = socket(AF_UNIX,SOCK_STREAM,0);
|
||||
if (_sock <= 0)
|
||||
throw std::runtime_error("unable to create socket of type AF_UNIX");
|
||||
|
||||
if (connect(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
|
||||
::close(_sock);
|
||||
throw std::runtime_error("IPC endpoint unreachable");
|
||||
}
|
||||
#endif
|
||||
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
IpcConnection::IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
|
||||
#else
|
||||
IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
|
||||
#endif
|
||||
_handler(commandHandler),
|
||||
_arg(arg),
|
||||
_sock(s),
|
||||
#ifdef __WINDOWS__
|
||||
_incoming(true),
|
||||
#endif
|
||||
_run(true),
|
||||
_running(true)
|
||||
{
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
IpcConnection::~IpcConnection()
|
||||
{
|
||||
_writeLock.lock();
|
||||
_run = false;
|
||||
_writeLock.unlock();
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
while (_running) {
|
||||
Thread::cancelIO(_thread);
|
||||
Sleep(100);
|
||||
}
|
||||
#else
|
||||
int s = _sock;
|
||||
_sock = 0;
|
||||
if (s > 0) {
|
||||
::shutdown(s,SHUT_RDWR);
|
||||
::close(s);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void IpcConnection::printf(const char *format,...)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
char tmp[65536];
|
||||
|
||||
va_start(ap,format);
|
||||
n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap);
|
||||
va_end(ap);
|
||||
if (n <= 0)
|
||||
return;
|
||||
|
||||
Mutex::Lock _l(_writeLock);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
_writeBuf.append(tmp,n);
|
||||
Thread::cancelIO(_thread);
|
||||
#else
|
||||
if (_sock > 0)
|
||||
::write(_sock,tmp,n);
|
||||
#endif
|
||||
}
|
||||
|
||||
void IpcConnection::threadMain()
|
||||
throw()
|
||||
{
|
||||
char tmp[65536];
|
||||
char linebuf[65536];
|
||||
unsigned int lineptr = 0;
|
||||
char c;
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
DWORD n,i;
|
||||
std::string wbuf;
|
||||
#else
|
||||
int s,n,i;
|
||||
#endif
|
||||
|
||||
while (_run) {
|
||||
#ifdef __WINDOWS__
|
||||
{
|
||||
Mutex::Lock _l(_writeLock);
|
||||
if (!_run)
|
||||
break;
|
||||
if (_writeBuf.length() > 0) {
|
||||
wbuf.append(_writeBuf);
|
||||
_writeBuf.clear();
|
||||
}
|
||||
}
|
||||
if (wbuf.length() > 0) {
|
||||
n = 0;
|
||||
if ((WriteFile(_sock,wbuf.data(),(DWORD)(wbuf.length()),&n,NULL))&&(n > 0)) {
|
||||
if (n < (DWORD)wbuf.length())
|
||||
wbuf.erase(0,n);
|
||||
else wbuf.clear();
|
||||
} else if (GetLastError() != ERROR_OPERATION_ABORTED)
|
||||
break;
|
||||
FlushFileBuffers(_sock);
|
||||
}
|
||||
if (!_run)
|
||||
break;
|
||||
n = 0;
|
||||
if ((!ReadFile(_sock,tmp,sizeof(tmp),&n,NULL))||(n <= 0)) {
|
||||
if (GetLastError() == ERROR_OPERATION_ABORTED)
|
||||
n = 0;
|
||||
else break;
|
||||
}
|
||||
if (!_run)
|
||||
break;
|
||||
#else
|
||||
if ((s = _sock) <= 0)
|
||||
break;
|
||||
n = (int)::read(s,tmp,sizeof(tmp));
|
||||
if ((n <= 0)||(_sock <= 0))
|
||||
break;
|
||||
#endif
|
||||
for(i=0;i<n;++i) {
|
||||
c = (linebuf[lineptr] = tmp[i]);
|
||||
if ((c == '\r')||(c == '\n')||(lineptr == (sizeof(linebuf) - 1))) {
|
||||
if (lineptr) {
|
||||
linebuf[lineptr] = (char)0;
|
||||
_handler(_arg,this,IPC_EVENT_COMMAND,linebuf);
|
||||
lineptr = 0;
|
||||
}
|
||||
} else ++lineptr;
|
||||
}
|
||||
}
|
||||
|
||||
_writeLock.lock();
|
||||
bool r = _run;
|
||||
_writeLock.unlock();
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
if (_incoming)
|
||||
DisconnectNamedPipe(_sock);
|
||||
CloseHandle(_sock);
|
||||
_running = false;
|
||||
#endif
|
||||
|
||||
if (r)
|
||||
_handler(_arg,this,IPC_EVENT_CONNECTION_CLOSED,(const char *)0);
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
105
control/IpcConnection.hpp
Normal file
105
control/IpcConnection.hpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_IPCCONNECTION_HPP
|
||||
#define ZT_IPCCONNECTION_HPP
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/Thread.hpp"
|
||||
#include "../node/NonCopyable.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class IpcListener;
|
||||
|
||||
/**
|
||||
* Interprocess communication connection
|
||||
*/
|
||||
class IpcConnection : NonCopyable
|
||||
{
|
||||
friend class IpcListener;
|
||||
|
||||
public:
|
||||
enum EventType
|
||||
{
|
||||
IPC_EVENT_COMMAND,
|
||||
IPC_EVENT_NEW_CONNECTION,
|
||||
IPC_EVENT_CONNECTION_CLOSED
|
||||
};
|
||||
|
||||
/**
|
||||
* Connect to an IPC endpoint
|
||||
*
|
||||
* @param endpoint Endpoint path
|
||||
* @param commandHandler Command handler function
|
||||
* @param arg First argument to command handler
|
||||
* @throws std::runtime_error Unable to connect
|
||||
*/
|
||||
IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
|
||||
~IpcConnection();
|
||||
|
||||
/**
|
||||
* @param format Printf format string
|
||||
* @param ... Printf arguments
|
||||
*/
|
||||
void printf(const char *format,...);
|
||||
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
private:
|
||||
// Used by IpcListener to construct incoming connections
|
||||
#ifdef __WINDOWS__
|
||||
IpcConnection(HANDLE s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
|
||||
#else
|
||||
IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
|
||||
#endif
|
||||
|
||||
void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *);
|
||||
void *_arg;
|
||||
#ifdef __WINDOWS__
|
||||
HANDLE _sock;
|
||||
std::string _writeBuf;
|
||||
bool _incoming;
|
||||
#else
|
||||
volatile int _sock;
|
||||
#endif
|
||||
Mutex _writeLock;
|
||||
Thread _thread;
|
||||
volatile bool _run;
|
||||
volatile bool _running;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
164
control/IpcListener.cpp
Normal file
164
control/IpcListener.cpp
Normal file
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "IpcListener.hpp"
|
||||
|
||||
#ifndef __WINDOWS__
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
IpcListener::IpcListener(const char *ep,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) :
|
||||
_endpoint(ep),
|
||||
_handler(commandHandler),
|
||||
_arg(arg),
|
||||
#ifdef __WINDOWS__
|
||||
_run(true),
|
||||
_running(true)
|
||||
#else
|
||||
_sock(0)
|
||||
#endif
|
||||
{
|
||||
#ifndef __WINDOWS__
|
||||
struct sockaddr_un unaddr;
|
||||
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;
|
||||
|
||||
struct stat stattmp;
|
||||
if (stat(_endpoint.c_str(),&stattmp)) {
|
||||
int testSock = socket(AF_UNIX,SOCK_STREAM,0);
|
||||
if (testSock <= 0)
|
||||
throw std::runtime_error("unable to create socket of type AF_UNIX");
|
||||
if (connect(testSock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
|
||||
// error means nothing is listening, orphaned name
|
||||
::close(testSock);
|
||||
} else {
|
||||
// success means endpoint is being actively listened to by a process
|
||||
::close(testSock);
|
||||
throw std::runtime_error("IPC endpoint address in use");
|
||||
}
|
||||
}
|
||||
::unlink(_endpoint.c_str());
|
||||
|
||||
_sock = socket(AF_UNIX,SOCK_STREAM,0);
|
||||
if (_sock <= 0)
|
||||
throw std::runtime_error("unable to create socket of type AF_UNIX");
|
||||
if (bind(_sock,(struct sockaddr *)&unaddr,sizeof(unaddr))) {
|
||||
::close(_sock);
|
||||
throw std::runtime_error("IPC endpoint could not be bound");
|
||||
}
|
||||
if (listen(_sock,8)) {
|
||||
::close(_sock);
|
||||
throw std::runtime_error("listen() failed for bound AF_UNIX socket");
|
||||
}
|
||||
::chmod(_endpoint.c_str(),0777);
|
||||
#endif
|
||||
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
IpcListener::~IpcListener()
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
_run = false;
|
||||
while (_running) {
|
||||
Thread::cancelIO(_thread);
|
||||
HANDLE tmp = CreateFileA(_endpoint.c_str(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,NULL);
|
||||
if (tmp != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(tmp);
|
||||
Sleep(250);
|
||||
}
|
||||
#else
|
||||
int s = _sock;
|
||||
_sock = 0;
|
||||
if (s > 0) {
|
||||
::shutdown(s,SHUT_RDWR);
|
||||
::close(s);
|
||||
}
|
||||
Thread::join(_thread);
|
||||
::unlink(_endpoint.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
void IpcListener::threadMain()
|
||||
throw()
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
HANDLE s;
|
||||
while (_run) {
|
||||
s = CreateNamedPipeA(_endpoint.c_str(),PIPE_ACCESS_DUPLEX,PIPE_READMODE_BYTE|PIPE_TYPE_BYTE|PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,1024,1024,0,NULL);
|
||||
if (s != INVALID_HANDLE_VALUE) {
|
||||
if ((ConnectNamedPipe(s,NULL))||(GetLastError() == ERROR_PIPE_CONNECTED)) {
|
||||
if (!_run) {
|
||||
DisconnectNamedPipe(s);
|
||||
CloseHandle(s);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
_handler(_arg,new IpcConnection(s,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
|
||||
} catch ( ... ) {} // handlers should not throw
|
||||
} else {
|
||||
CloseHandle(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
_running = false;
|
||||
#else
|
||||
struct sockaddr_un unaddr;
|
||||
socklen_t socklen;
|
||||
int s;
|
||||
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);
|
||||
s = accept(_sock,(struct sockaddr *)&unaddr,&socklen);
|
||||
if (s <= 0)
|
||||
break;
|
||||
if (!_sock) {
|
||||
::close(s);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
_handler(_arg,new IpcConnection(s,_handler,_arg),IpcConnection::IPC_EVENT_NEW_CONNECTION,(const char *)0);
|
||||
} catch ( ... ) {} // handlers should not throw
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
96
control/IpcListener.hpp
Normal file
96
control/IpcListener.hpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_IPCLISTENER_HPP
|
||||
#define ZT_IPCLISTENER_HPP
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/Thread.hpp"
|
||||
#include "../node/NonCopyable.hpp"
|
||||
#include "IpcConnection.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#define ZT_IPC_ENDPOINT_BASE "\\\\.\\pipe\\ZeroTierOne-"
|
||||
#else
|
||||
#define ZT_IPC_ENDPOINT_BASE "/tmp/.ZeroTierOne-"
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* IPC incoming connection listener (Unix domain sockets or named pipes on Windows)
|
||||
*/
|
||||
class IpcListener : NonCopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Listen for IPC connections
|
||||
*
|
||||
* The supplied handler is passed on to incoming instances of IpcConnection. When
|
||||
* a connection is first opened, it is called with IPC_EVENT_NEW_CONNECTION. The
|
||||
* receiver must take ownership of the connection object. When a connection is
|
||||
* closed, IPC_EVENT_CONNECTION_CLOSING is generated. At this point (or after) the
|
||||
* receiver must delete the object. IPC_EVENT_COMMAND is generated when lines of
|
||||
* text are read, and in this cases the last argument is not NULL. No closed event
|
||||
* is generated in the event of manual delete if the connection is still open.
|
||||
*
|
||||
* Yeah, this whole callback model sort of sucks. Might rethink and replace with
|
||||
* some kind of actor model or something if it gets too unweildy. But for now the
|
||||
* use cases are simple enough that it's not too bad.
|
||||
*
|
||||
* @param commandHandler Function to call for each command
|
||||
* @param arg First argument to pass to handler
|
||||
* @throws std::runtime_error Unable to bind to endpoint
|
||||
*/
|
||||
IpcListener(const char *ep,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg);
|
||||
|
||||
~IpcListener();
|
||||
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
private:
|
||||
std::string _endpoint;
|
||||
void (*_handler)(void *,IpcConnection *,IpcConnection::EventType,const char *);
|
||||
void *_arg;
|
||||
#ifdef __WINDOWS__
|
||||
volatile bool _run;
|
||||
volatile bool _running;
|
||||
#else
|
||||
volatile int _sock;
|
||||
#endif
|
||||
Thread _thread;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
170
control/NodeControlClient.cpp
Normal file
170
control/NodeControlClient.cpp
Normal file
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include "NodeControlClient.hpp"
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/Utils.hpp"
|
||||
#include "../node/Defaults.hpp"
|
||||
|
||||
#include "IpcConnection.hpp"
|
||||
#include "IpcListener.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
struct _NodeControlClientImpl
|
||||
{
|
||||
void (*resultHandler)(void *,const char *);
|
||||
void *arg;
|
||||
IpcConnection *ipcc;
|
||||
std::string err;
|
||||
};
|
||||
|
||||
static void _CBipcResultHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *result)
|
||||
{
|
||||
if ((event == IpcConnection::IPC_EVENT_COMMAND)&&(result)) {
|
||||
if (strcmp(result,"200 auth OK"))
|
||||
((_NodeControlClientImpl *)arg)->resultHandler(((_NodeControlClientImpl *)arg)->arg,result);
|
||||
}
|
||||
}
|
||||
|
||||
NodeControlClient::NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken)
|
||||
throw() :
|
||||
_impl((void *)new _NodeControlClientImpl)
|
||||
{
|
||||
_NodeControlClientImpl *impl = (_NodeControlClientImpl *)_impl;
|
||||
impl->ipcc = (IpcConnection *)0;
|
||||
|
||||
if (!hp)
|
||||
hp = ZT_DEFAULTS.defaultHomePath.c_str();
|
||||
|
||||
std::string at;
|
||||
if (authToken)
|
||||
at = authToken;
|
||||
else if (!Utils::readFile(authTokenDefaultSystemPath(),at)) {
|
||||
if (!Utils::readFile(authTokenDefaultUserPath(),at)) {
|
||||
impl->err = "no authentication token specified and authtoken.secret not readable";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string myid;
|
||||
if (Utils::readFile((std::string(hp) + ZT_PATH_SEPARATOR_S + "identity.public").c_str(),myid)) {
|
||||
std::string myaddr(myid.substr(0,myid.find(':')));
|
||||
if (myaddr.length() != 10)
|
||||
impl->err = "invalid address extracted from identity.public";
|
||||
else {
|
||||
try {
|
||||
impl->resultHandler = resultHandler;
|
||||
impl->arg = arg;
|
||||
impl->ipcc = new IpcConnection((std::string(ZT_IPC_ENDPOINT_BASE) + myaddr).c_str(),&_CBipcResultHandler,_impl);
|
||||
impl->ipcc->printf("auth %s"ZT_EOL_S,at.c_str());
|
||||
} catch ( ... ) {
|
||||
impl->ipcc = (IpcConnection *)0;
|
||||
impl->err = "failure connecting to running ZeroTier One service";
|
||||
}
|
||||
}
|
||||
} else impl->err = "unable to read identity.public";
|
||||
}
|
||||
|
||||
NodeControlClient::~NodeControlClient()
|
||||
{
|
||||
if (_impl) {
|
||||
delete ((_NodeControlClientImpl *)_impl)->ipcc;
|
||||
delete (_NodeControlClientImpl *)_impl;
|
||||
}
|
||||
}
|
||||
|
||||
const char *NodeControlClient::error() const
|
||||
throw()
|
||||
{
|
||||
if (((_NodeControlClientImpl *)_impl)->err.length())
|
||||
return ((_NodeControlClientImpl *)_impl)->err.c_str();
|
||||
return (const char *)0;
|
||||
}
|
||||
|
||||
void NodeControlClient::send(const char *command)
|
||||
throw()
|
||||
{
|
||||
try {
|
||||
if (((_NodeControlClientImpl *)_impl)->ipcc)
|
||||
((_NodeControlClientImpl *)_impl)->ipcc->printf("%s"ZT_EOL_S,command);
|
||||
} catch ( ... ) {}
|
||||
}
|
||||
|
||||
std::vector<std::string> NodeControlClient::splitLine(const char *line)
|
||||
{
|
||||
return Utils::split(line," ","\\","\"");
|
||||
}
|
||||
|
||||
const char *NodeControlClient::authTokenDefaultUserPath()
|
||||
{
|
||||
static std::string dlp;
|
||||
static Mutex dlp_m;
|
||||
|
||||
Mutex::Lock _l(dlp_m);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
if (!dlp.length()) {
|
||||
char buf[16384];
|
||||
if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_APPDATA,NULL,0,buf)))
|
||||
dlp = (std::string(buf) + "\\ZeroTier\\One\\authtoken.secret");
|
||||
}
|
||||
|
||||
#else // not __WINDOWS__
|
||||
|
||||
if (!dlp.length()) {
|
||||
const char *home = getenv("HOME");
|
||||
if (home) {
|
||||
#ifdef __APPLE__
|
||||
dlp = (std::string(home) + "/Library/Application Support/ZeroTier/One/authtoken.secret");
|
||||
#else
|
||||
dlp = (std::string(home) + "/.zeroTierOneAuthToken");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __WINDOWS__ or not __WINDOWS__
|
||||
|
||||
return dlp.c_str();
|
||||
}
|
||||
|
||||
const char *NodeControlClient::authTokenDefaultSystemPath()
|
||||
{
|
||||
static std::string dsp;
|
||||
static Mutex dsp_m;
|
||||
|
||||
Mutex::Lock _l(dsp_m);
|
||||
|
||||
if (!dsp.length())
|
||||
dsp = (ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S"authtoken.secret");
|
||||
|
||||
return dsp.c_str();
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
111
control/NodeControlClient.hpp
Normal file
111
control/NodeControlClient.hpp
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ZT_NODECONTROLCLIENT_HPP
|
||||
#define ZT_NODECONTROLCLIENT_HPP
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* Client for controlling a local ZeroTier One node
|
||||
*
|
||||
* Windows note: be sure you call WSAStartup() before using this,
|
||||
* otherwise it will be unable to open a local UDP socket to
|
||||
* communicate with the service.
|
||||
*/
|
||||
class NodeControlClient
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create a new node config client
|
||||
*
|
||||
* Initialization may fail. Call error() to check.
|
||||
*
|
||||
* @param hp Home path of ZeroTier One instance or NULL for default system home path
|
||||
* @param resultHandler Function to call when commands provide results
|
||||
* @param arg First argument to result handler
|
||||
* @param authToken Authentication token or NULL (default) to read from authtoken.secret in home path
|
||||
*/
|
||||
NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken = (const char *)0)
|
||||
throw();
|
||||
|
||||
~NodeControlClient();
|
||||
|
||||
/**
|
||||
* @return Initialization error or NULL if none
|
||||
*/
|
||||
const char *error() const
|
||||
throw();
|
||||
|
||||
/**
|
||||
* Send a command to the local node
|
||||
*
|
||||
* Note that the returned conversation ID will never be 0. A return value
|
||||
* of 0 indicates a fatal error such as failure to bind to any local UDP
|
||||
* port.
|
||||
*
|
||||
* @param command
|
||||
* @return Conversation ID that will be provided to result handler when/if results are sent back
|
||||
*/
|
||||
void send(const char *command)
|
||||
throw();
|
||||
inline void send(const std::string &command)
|
||||
throw() { return send(command.c_str()); }
|
||||
|
||||
/**
|
||||
* Split a line of results
|
||||
*
|
||||
* @param line Line to split
|
||||
* @return Vector of fields
|
||||
*/
|
||||
static std::vector<std::string> splitLine(const char *line);
|
||||
static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); }
|
||||
|
||||
/**
|
||||
* @return Default path for current user's authtoken.secret
|
||||
*/
|
||||
static const char *authTokenDefaultUserPath();
|
||||
|
||||
/**
|
||||
* @return Default path to system authtoken.secret
|
||||
*/
|
||||
static const char *authTokenDefaultSystemPath();
|
||||
|
||||
private:
|
||||
// NodeControlClient is not copyable
|
||||
NodeControlClient(const NodeControlClient&);
|
||||
const NodeControlClient& operator=(const NodeControlClient&);
|
||||
|
||||
void *_impl;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
0
control/NodeControlService.cpp
Normal file
0
control/NodeControlService.cpp
Normal file
0
control/NodeControlService.hpp
Normal file
0
control/NodeControlService.hpp
Normal file
Loading…
Add table
Add a link
Reference in a new issue