mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-14 02:27:38 -07:00
Command line interface.
This commit is contained in:
parent
c345c699fd
commit
5f4eb1ebc6
5 changed files with 184 additions and 9 deletions
|
@ -79,13 +79,27 @@ struct _LocalClientImpl
|
|||
UdpSocket *sock;
|
||||
void (*resultHandler)(void *,unsigned long,const char *);
|
||||
void *arg;
|
||||
InetAddress localDestAddr;
|
||||
Mutex inUseLock;
|
||||
};
|
||||
|
||||
static void _CBlocalClientHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
|
||||
{
|
||||
_LocalClientImpl *impl = (_LocalClientImpl *)arg;
|
||||
if (!impl)
|
||||
return;
|
||||
if (!impl->resultHandler)
|
||||
return; // sanity check
|
||||
Mutex::Lock _l(impl->inUseLock);
|
||||
|
||||
try {
|
||||
unsigned long convId = 0;
|
||||
std::vector<std::string> results;
|
||||
if (!NodeConfig::decodeControlMessagePacket(impl->key,data,len,convId,results))
|
||||
return;
|
||||
for(std::vector<std::string>::iterator r(results.begin());r!=results.end();++r)
|
||||
impl->resultHandler(impl->arg,convId,r->c_str());
|
||||
} catch ( ... ) {}
|
||||
}
|
||||
|
||||
Node::LocalClient::LocalClient(const char *authToken,void (*resultHandler)(void *,unsigned long,const char *),void *arg)
|
||||
|
@ -114,6 +128,8 @@ Node::LocalClient::LocalClient(const char *authToken,void (*resultHandler)(void
|
|||
impl->sock = sock;
|
||||
impl->resultHandler = resultHandler;
|
||||
impl->arg = arg;
|
||||
impl->localDestAddr = InetAddress::LO4;
|
||||
impl->localDestAddr.setPort(ZT_CONTROL_UDP_PORT);
|
||||
_impl = impl;
|
||||
} else delete impl;
|
||||
}
|
||||
|
@ -131,9 +147,27 @@ Node::LocalClient::~LocalClient()
|
|||
unsigned long Node::LocalClient::send(const char *command)
|
||||
throw()
|
||||
{
|
||||
uint32_t convId = (uint32_t)rand();
|
||||
if (!_impl)
|
||||
return 0;
|
||||
_LocalClientImpl *impl = (_LocalClientImpl *)_impl;
|
||||
Mutex::Lock _l(impl->inUseLock);
|
||||
|
||||
return convId;
|
||||
try {
|
||||
uint32_t convId = (uint32_t)rand();
|
||||
if (!convId)
|
||||
convId = 1;
|
||||
|
||||
std::vector<std::string> tmp;
|
||||
tmp.push_back(std::string(command));
|
||||
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(impl->key,convId,tmp));
|
||||
|
||||
for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(packets.begin());p!=packets.end();++p)
|
||||
impl->sock->send(impl->localDestAddr,p->data(),p->size(),-1);
|
||||
|
||||
return convId;
|
||||
} catch ( ... ) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct _NodeImpl
|
||||
|
|
|
@ -49,11 +49,6 @@ public:
|
|||
/**
|
||||
* Create a new node config client
|
||||
*
|
||||
* The result handler will be called from a different thread. Its
|
||||
* arguments are the request ID generated by send() and each line
|
||||
* of output. It may be called more than once per request result
|
||||
* if the command generates more than one line of output.
|
||||
*
|
||||
* @param authToken Authentication token
|
||||
* @param resultHandler Function to call when commands provide results
|
||||
*/
|
||||
|
@ -65,8 +60,12 @@ public:
|
|||
/**
|
||||
* 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 Request ID that will be provided to result handler when/if results are sent back
|
||||
* @return Conversation ID that will be provided to result handler when/if results are sent back
|
||||
*/
|
||||
unsigned long send(const char *command)
|
||||
throw();
|
||||
|
|
|
@ -218,6 +218,20 @@ bool NodeConfig::decodeControlMessagePacket(const void *key,const void *data,uns
|
|||
|
||||
void NodeConfig::_CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
|
||||
{
|
||||
NodeConfig *nc = (NodeConfig *)arg;
|
||||
try {
|
||||
unsigned long convId = 0;
|
||||
std::vector<std::string> commands;
|
||||
|
||||
if (!decodeControlMessagePacket(nc->_controlSocketKey,data,len,convId,commands))
|
||||
return;
|
||||
|
||||
for(std::vector<std::string>::iterator c(commands.begin());c!=commands.end();++c) {
|
||||
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > resultPackets(encodeControlMessage(nc->_controlSocketKey,convId,nc->execute(c->c_str())));
|
||||
for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(resultPackets.begin());p!=resultPackets.end();++p)
|
||||
sock->send(remoteAddr,p->data(),p->size(),-1);
|
||||
}
|
||||
} catch ( ... ) {}
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue