2020-05-01 19:15:38 -07:00
|
|
|
/*
|
2021-02-04 11:03:55 -08:00
|
|
|
* Copyright (c)2013-2021 ZeroTier, Inc.
|
2020-05-01 19:15:38 -07:00
|
|
|
*
|
|
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
|
|
|
|
*
|
2021-02-04 11:03:55 -08:00
|
|
|
* Change Date: 2025-01-01
|
2020-05-01 19:15:38 -07:00
|
|
|
*
|
|
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
|
|
|
|
*/
|
|
|
|
|
/****/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
|
|
|
|
* Callback event processing logic
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "concurrentqueue.h"
|
|
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
2020-05-01 19:15:38 -07:00
|
|
|
#include <jni.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-05-30 18:29:04 -07:00
|
|
|
#include "Constants.hpp"
|
2020-05-01 19:15:38 -07:00
|
|
|
#include "Debug.hpp"
|
|
|
|
|
#include "Events.hpp"
|
2021-04-17 23:46:21 -07:00
|
|
|
#include "Node.hpp"
|
2020-05-01 19:15:38 -07:00
|
|
|
#include "NodeService.hpp"
|
2021-04-17 23:46:21 -07:00
|
|
|
#include "OSUtils.hpp"
|
|
|
|
|
#include "ZeroTierSockets.h"
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
#define NODE_EVENT_TYPE(code) code >= ZTS_EVENT_NODE_UP&& code <= ZTS_EVENT_NODE_NORMAL_TERMINATION
|
|
|
|
|
#define NETWORK_EVENT_TYPE(code) \
|
|
|
|
|
code >= ZTS_EVENT_NETWORK_NOT_FOUND&& code <= ZTS_EVENT_NETWORK_UPDATE
|
|
|
|
|
#define STACK_EVENT_TYPE(code) code >= ZTS_EVENT_STACK_UP&& code <= ZTS_EVENT_STACK_DOWN
|
|
|
|
|
#define NETIF_EVENT_TYPE(code) code >= ZTS_EVENT_NETIF_UP&& code <= ZTS_EVENT_NETIF_LINK_DOWN
|
|
|
|
|
#define PEER_EVENT_TYPE(code) code >= ZTS_EVENT_PEER_DIRECT&& code <= ZTS_EVENT_PEER_PATH_DEAD
|
|
|
|
|
#define ROUTE_EVENT_TYPE(code) code >= ZTS_EVENT_ROUTE_ADDED&& code <= ZTS_EVENT_ROUTE_REMOVED
|
|
|
|
|
#define ADDR_EVENT_TYPE(code) code >= ZTS_EVENT_ADDR_ADDED_IP4&& code <= ZTS_EVENT_ADDR_REMOVED_IP6
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_PYTHON
|
|
|
|
|
#include "Python.h"
|
2021-04-17 23:46:21 -07:00
|
|
|
PythonDirectorCallbackClass* _userEventCallback = NULL;
|
|
|
|
|
void PythonDirectorCallbackClass::on_zerotier_event(struct zts_callback_msg* msg)
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-02-24 01:25:15 -08:00
|
|
|
#endif
|
|
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
extern NodeService* service;
|
2020-05-01 19:15:38 -07:00
|
|
|
|
|
|
|
|
// Global state variable shared between Socket, Control, Event and NodeService logic.
|
|
|
|
|
uint8_t _serviceStateFlags;
|
|
|
|
|
|
|
|
|
|
// Lock to guard access to callback function pointers.
|
|
|
|
|
Mutex _callbackLock;
|
|
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_PINVOKE
|
2021-04-17 23:46:21 -07:00
|
|
|
void (*_userEventCallback)(void*);
|
2021-02-24 01:25:15 -08:00
|
|
|
#endif
|
|
|
|
|
#ifdef ZTS_C_API_ONLY
|
2021-04-17 23:46:21 -07:00
|
|
|
void (*_userEventCallback)(void*);
|
2021-02-24 01:25:15 -08:00
|
|
|
#endif
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2020-05-30 18:29:04 -07:00
|
|
|
moodycamel::ConcurrentQueue<struct zts_callback_msg*> _callbackMsgQueue;
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
void _enqueueEvent(int16_t eventCode, void* arg)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
2021-04-17 23:46:21 -07:00
|
|
|
struct zts_callback_msg* msg = new zts_callback_msg();
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->eventCode = eventCode;
|
|
|
|
|
|
|
|
|
|
if (NODE_EVENT_TYPE(eventCode)) {
|
|
|
|
|
msg->node = (struct zts_node_details*)arg;
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (NETWORK_EVENT_TYPE(eventCode)) {
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->network = (struct zts_network_details*)arg;
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (STACK_EVENT_TYPE(eventCode)) {
|
2020-05-30 18:29:04 -07:00
|
|
|
/* nothing to convey to user */
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (NETIF_EVENT_TYPE(eventCode)) {
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->netif = (struct zts_netif_details*)arg;
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (ROUTE_EVENT_TYPE(eventCode)) {
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->route = (struct zts_virtual_network_route*)arg;
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (PEER_EVENT_TYPE(eventCode)) {
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->peer = (struct zts_peer_details*)arg;
|
2021-04-17 23:46:21 -07:00
|
|
|
}
|
|
|
|
|
if (ADDR_EVENT_TYPE(eventCode)) {
|
2020-05-01 19:15:38 -07:00
|
|
|
msg->addr = (struct zts_addr_details*)arg;
|
|
|
|
|
}
|
2020-05-30 18:29:04 -07:00
|
|
|
|
|
|
|
|
if (msg && _callbackMsgQueue.size_approx() > 1024) {
|
|
|
|
|
// Rate-limit number of events
|
|
|
|
|
_freeEvent(msg);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
_callbackMsgQueue.enqueue(msg);
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
void _freeEvent(struct zts_callback_msg* msg)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
2021-04-17 23:46:21 -07:00
|
|
|
if (! msg) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2021-04-17 23:46:21 -07:00
|
|
|
if (msg->node) {
|
|
|
|
|
delete msg->node;
|
|
|
|
|
}
|
|
|
|
|
if (msg->network) {
|
|
|
|
|
delete msg->network;
|
|
|
|
|
}
|
|
|
|
|
if (msg->netif) {
|
|
|
|
|
delete msg->netif;
|
|
|
|
|
}
|
|
|
|
|
if (msg->route) {
|
|
|
|
|
delete msg->route;
|
|
|
|
|
}
|
|
|
|
|
if (msg->peer) {
|
|
|
|
|
delete msg->peer;
|
|
|
|
|
}
|
|
|
|
|
if (msg->addr) {
|
|
|
|
|
delete msg->addr;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
void _passDequeuedEventToUser(struct zts_callback_msg* msg)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
2020-05-30 18:29:04 -07:00
|
|
|
bool bShouldStopCallbackThread = (msg->eventCode == ZTS_EVENT_STACK_DOWN);
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_PYTHON
|
|
|
|
|
PyGILState_STATE state = PyGILState_Ensure();
|
|
|
|
|
_userEventCallback->on_zerotier_event(msg);
|
|
|
|
|
PyGILState_Release(state);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
2021-04-17 23:46:21 -07:00
|
|
|
if (_userCallbackMethodRef) {
|
|
|
|
|
JNIEnv* env;
|
2020-05-30 18:29:04 -07:00
|
|
|
#if defined(__ANDROID__)
|
2020-05-01 19:15:38 -07:00
|
|
|
jint rs = jvm->AttachCurrentThread(&env, NULL);
|
2020-05-30 18:29:04 -07:00
|
|
|
#else
|
2021-04-17 23:46:21 -07:00
|
|
|
jint rs = jvm->AttachCurrentThread((void**)&env, NULL);
|
2020-05-30 18:29:04 -07:00
|
|
|
#endif
|
2021-04-17 23:46:21 -07:00
|
|
|
assert(rs == JNI_OK);
|
2020-05-01 19:15:38 -07:00
|
|
|
uint64_t arg = 0;
|
|
|
|
|
uint64_t id = 0;
|
|
|
|
|
if (NODE_EVENT_TYPE(msg->eventCode)) {
|
|
|
|
|
id = msg->node ? msg->node->address : 0;
|
|
|
|
|
}
|
|
|
|
|
if (NETWORK_EVENT_TYPE(msg->eventCode)) {
|
|
|
|
|
id = msg->network ? msg->network->nwid : 0;
|
|
|
|
|
}
|
|
|
|
|
if (PEER_EVENT_TYPE(msg->eventCode)) {
|
|
|
|
|
id = msg->peer ? msg->peer->address : 0;
|
|
|
|
|
}
|
|
|
|
|
env->CallVoidMethod(objRef, _userCallbackMethodRef, id, msg->eventCode);
|
|
|
|
|
}
|
2021-04-17 23:46:21 -07:00
|
|
|
#endif // ZTS_ENABLE_JAVA
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_PINVOKE
|
|
|
|
|
if (_userEventCallback) {
|
|
|
|
|
_userEventCallback(msg);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ZTS_C_API_ONLY
|
|
|
|
|
if (_userEventCallback) {
|
|
|
|
|
_userEventCallback(msg);
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
2021-02-24 01:25:15 -08:00
|
|
|
_freeEvent(msg);
|
2020-05-30 18:29:04 -07:00
|
|
|
if (bShouldStopCallbackThread) {
|
|
|
|
|
/* Ensure last possible callback ZTS_EVENT_STACK_DOWN is
|
|
|
|
|
delivered before callback thread is finally stopped. */
|
|
|
|
|
_clrState(ZTS_STATE_CALLBACKS_RUNNING);
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _isCallbackRegistered()
|
|
|
|
|
{
|
|
|
|
|
_callbackLock.lock();
|
|
|
|
|
bool retval = false;
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
2020-05-01 19:15:38 -07:00
|
|
|
retval = (jvm && objRef && _userCallbackMethodRef);
|
|
|
|
|
#else
|
2021-02-24 01:25:15 -08:00
|
|
|
retval = _userEventCallback;
|
2020-05-01 19:15:38 -07:00
|
|
|
#endif
|
|
|
|
|
_callbackLock.unlock();
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _clearRegisteredCallback()
|
|
|
|
|
{
|
|
|
|
|
_callbackLock.lock();
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
2020-05-01 19:15:38 -07:00
|
|
|
objRef = NULL;
|
2021-04-17 23:46:21 -07:00
|
|
|
_userCallbackMethodRef = NULL;
|
2020-05-01 19:15:38 -07:00
|
|
|
#else
|
2021-02-24 01:25:15 -08:00
|
|
|
_userEventCallback = NULL;
|
2020-05-01 19:15:38 -07:00
|
|
|
#endif
|
|
|
|
|
_callbackLock.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int _canPerformServiceOperation()
|
|
|
|
|
{
|
2021-04-17 23:46:21 -07:00
|
|
|
return service && service->isRunning() && service->getNode() && service->getNode()->online()
|
|
|
|
|
&& ! _getState(ZTS_STATE_FREE_CALLED);
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
#define RESET_FLAGS() _serviceStateFlags = 0;
|
|
|
|
|
#define SET_FLAGS(f) _serviceStateFlags |= f;
|
|
|
|
|
#define CLR_FLAGS(f) _serviceStateFlags &= ~f;
|
|
|
|
|
#define GET_FLAGS(f) ((_serviceStateFlags & f) > 0)
|
2020-05-01 19:15:38 -07:00
|
|
|
|
|
|
|
|
void _setState(uint8_t newFlags)
|
|
|
|
|
{
|
|
|
|
|
if ((newFlags ^ _serviceStateFlags) & ZTS_STATE_NET_SERVICE_RUNNING) {
|
2021-04-17 23:46:21 -07:00
|
|
|
return; // No effect. Not allowed to set this flag manually
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
SET_FLAGS(newFlags);
|
2021-04-17 23:46:21 -07:00
|
|
|
if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING)
|
|
|
|
|
&& ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) {
|
2020-05-01 19:15:38 -07:00
|
|
|
SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _clrState(uint8_t newFlags)
|
|
|
|
|
{
|
|
|
|
|
if (newFlags & ZTS_STATE_NET_SERVICE_RUNNING) {
|
2021-04-17 23:46:21 -07:00
|
|
|
return; // No effect. Not allowed to set this flag manually
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
CLR_FLAGS(newFlags);
|
2021-04-17 23:46:21 -07:00
|
|
|
if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING)
|
|
|
|
|
&& ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) {
|
2020-05-01 19:15:38 -07:00
|
|
|
SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _getState(uint8_t testFlags)
|
|
|
|
|
{
|
|
|
|
|
return testFlags & _serviceStateFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
|
DWORD WINAPI _runCallbacks(LPVOID thread_id)
|
|
|
|
|
#else
|
2021-04-17 23:46:21 -07:00
|
|
|
void* _runCallbacks(void* thread_id)
|
2020-05-01 19:15:38 -07:00
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
pthread_setname_np(ZTS_EVENT_CALLBACK_THREAD_NAME);
|
|
|
|
|
#endif
|
2021-04-17 23:46:21 -07:00
|
|
|
while (_getState(ZTS_STATE_CALLBACKS_RUNNING) || _callbackMsgQueue.size_approx() > 0) {
|
|
|
|
|
struct zts_callback_msg* msg;
|
2020-05-01 19:15:38 -07:00
|
|
|
size_t sz = _callbackMsgQueue.size_approx();
|
|
|
|
|
for (size_t j = 0; j < sz; j++) {
|
|
|
|
|
if (_callbackMsgQueue.try_dequeue(msg)) {
|
|
|
|
|
_callbackLock.lock();
|
|
|
|
|
_passDequeuedEventToUser(msg);
|
|
|
|
|
_callbackLock.unlock();
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-17 23:46:21 -07:00
|
|
|
zts_delay_ms(ZTS_CALLBACK_PROCESSING_INTERVAL);
|
|
|
|
|
}
|
2021-02-24 01:25:15 -08:00
|
|
|
#if ZTS_ENABLE_JAVA
|
2021-04-17 23:46:21 -07:00
|
|
|
JNIEnv* env;
|
2020-05-01 19:15:38 -07:00
|
|
|
jint rs = jvm->DetachCurrentThread();
|
2021-04-17 23:46:21 -07:00
|
|
|
pthread_exit(0);
|
2020-05-01 19:15:38 -07:00
|
|
|
#endif
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
} // namespace ZeroTier
|