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-04-22 11:20:04 -07:00
|
|
|
* Change Date: 2026-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
|
|
|
|
|
*
|
2021-04-22 11:20:04 -07:00
|
|
|
* Callback event creation and distribution to user application
|
2020-05-01 19:15:38 -07:00
|
|
|
*/
|
|
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
#ifndef ZTS_USER_EVENTS_HPP
|
|
|
|
|
#define ZTS_USER_EVENTS_HPP
|
2020-05-01 19:15:38 -07:00
|
|
|
|
|
|
|
|
#include "ZeroTierSockets.h"
|
|
|
|
|
|
2020-05-30 18:29:04 -07:00
|
|
|
#ifdef __WINDOWS__
|
2021-12-29 16:23:32 -05:00
|
|
|
#include <basetsd.h>
|
2020-05-30 18:29:04 -07:00
|
|
|
#endif
|
|
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
/* Macro substitutions to standardize state checking of service, node, callbacks, and TCP/IP
|
|
|
|
|
* stack. These are used only for control functions that are called at a low frequency. All higher
|
2023-07-19 10:19:09 -04:00
|
|
|
* frequency socket calls use unguarded state flags */
|
2021-04-22 11:20:04 -07:00
|
|
|
|
|
|
|
|
// Lock service and check that it is running
|
2021-04-26 21:55:01 -07:00
|
|
|
#define ACQUIRE_SERVICE(x) \
|
2021-04-26 22:07:55 -07:00
|
|
|
Mutex::Lock _ls(service_m); \
|
|
|
|
|
if (! zts_service || ! zts_service->isRunning()) { \
|
|
|
|
|
return x; \
|
|
|
|
|
}
|
2021-04-22 11:20:04 -07:00
|
|
|
// Lock service and check that it is not currently running
|
2021-04-26 21:55:01 -07:00
|
|
|
#define ACQUIRE_SERVICE_OFFLINE() \
|
2021-04-26 22:07:55 -07:00
|
|
|
Mutex::Lock _ls(service_m); \
|
|
|
|
|
if (zts_service && zts_service->isRunning()) { \
|
|
|
|
|
return ZTS_ERR_SERVICE; \
|
|
|
|
|
} \
|
|
|
|
|
if (! zts_service) { \
|
|
|
|
|
init_subsystems(); \
|
|
|
|
|
}
|
2021-04-22 11:20:04 -07:00
|
|
|
// Unlock service
|
|
|
|
|
#define RELEASE_SERVICE() service_m.unlock();
|
|
|
|
|
// Lock service, ensure node is online
|
2021-04-26 21:55:01 -07:00
|
|
|
#define ACQUIRE_ONLINE_NODE() \
|
2021-04-26 22:07:55 -07:00
|
|
|
ACQUIRE_SERVICE() if (! zts_service->nodeIsOnline()) \
|
|
|
|
|
{ \
|
|
|
|
|
return ZTS_ERR_SERVICE; \
|
|
|
|
|
}
|
2021-04-22 11:20:04 -07:00
|
|
|
// Lock event callback
|
2021-04-26 21:55:01 -07:00
|
|
|
#define ACQUIRE_EVENTS() \
|
2021-04-26 22:07:55 -07:00
|
|
|
Mutex::Lock _lc(events_m); \
|
|
|
|
|
if (! zts_events) { \
|
|
|
|
|
return ZTS_ERR_SERVICE; \
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
namespace ZeroTier {
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
2021-04-22 11:20:04 -07:00
|
|
|
#include <jni.h>
|
2021-04-17 23:46:21 -07:00
|
|
|
// References to JNI objects and VM kept for future callbacks
|
|
|
|
|
extern JavaVM* jvm;
|
|
|
|
|
extern jobject objRef;
|
|
|
|
|
extern jmethodID _userCallbackMethodRef;
|
2020-05-01 19:15:38 -07:00
|
|
|
#endif
|
|
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
#define ZTS_STATE_NODE_RUNNING 0x01
|
|
|
|
|
#define ZTS_STATE_STACK_RUNNING 0x02
|
|
|
|
|
#define ZTS_STATE_NET_SERVICE_RUNNING 0x04
|
|
|
|
|
#define ZTS_STATE_CALLBACKS_RUNNING 0x08
|
|
|
|
|
#define ZTS_STATE_FREE_CALLED 0x10
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
extern volatile uint8_t service_state;
|
|
|
|
|
extern int last_state_check;
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
inline int transport_ok()
|
|
|
|
|
{
|
2021-04-26 22:07:55 -07:00
|
|
|
last_state_check = service_state & ZTS_STATE_NET_SERVICE_RUNNING;
|
|
|
|
|
return last_state_check;
|
2021-04-22 11:20:04 -07:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
|
|
|
|
|
/**
|
2021-04-22 11:20:04 -07:00
|
|
|
* How often callback messages are assembled and/or sent
|
2020-05-01 19:15:38 -07:00
|
|
|
*/
|
2021-04-22 11:20:04 -07:00
|
|
|
#define ZTS_CALLBACK_PROCESSING_INTERVAL 25
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
class Events {
|
2021-04-26 22:07:55 -07:00
|
|
|
bool _enabled;
|
2021-04-22 11:20:04 -07:00
|
|
|
|
|
|
|
|
public:
|
2021-04-26 22:07:55 -07:00
|
|
|
Events() : _enabled(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform one iteration of callback processing
|
|
|
|
|
*/
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable callback event processing
|
|
|
|
|
*/
|
|
|
|
|
void enable();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable callback event processing
|
|
|
|
|
*/
|
|
|
|
|
void disable();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enqueue an event to be sent to the user application
|
2023-08-21 11:50:05 -04:00
|
|
|
*
|
|
|
|
|
* Returns true if arg was enqueued.
|
|
|
|
|
* If enqueued, then ownership of arg has been transferred.
|
|
|
|
|
* If NOT enqueued, then ownership of arg has NOT been transferred.
|
2021-04-26 22:07:55 -07:00
|
|
|
*/
|
2023-08-21 11:50:05 -04:00
|
|
|
bool enqueue(unsigned int event_code, const void* arg, int len = 0);
|
2021-04-26 22:07:55 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send callback message to user application
|
|
|
|
|
*/
|
|
|
|
|
void sendToUser(zts_event_msg_t* msg);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free memory occupied by callback structures
|
|
|
|
|
*/
|
|
|
|
|
void destroy(zts_event_msg_t* msg);
|
|
|
|
|
|
2021-04-29 14:03:15 -07:00
|
|
|
#ifdef ZTS_ENABLE_JAVA
|
|
|
|
|
void setJavaCallback(jobject objRef, jmethodID methodId);
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-26 22:07:55 -07:00
|
|
|
/**
|
|
|
|
|
* Return whether a callback method has been set
|
|
|
|
|
*/
|
|
|
|
|
bool hasCallback();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear pointer reference to user-provided callback function
|
|
|
|
|
*/
|
|
|
|
|
void clrCallback();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return whether service operation can be performed at this time
|
|
|
|
|
*/
|
|
|
|
|
int canPerformServiceOperation();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set internal state flags
|
|
|
|
|
*/
|
|
|
|
|
void setState(uint8_t newFlags);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear internal state flags
|
|
|
|
|
*/
|
|
|
|
|
void clrState(uint8_t newFlags);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get internal state flags
|
|
|
|
|
*/
|
|
|
|
|
bool getState(uint8_t testFlags);
|
2021-04-22 11:20:04 -07:00
|
|
|
};
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-17 23:46:21 -07:00
|
|
|
} // namespace ZeroTier
|
2020-05-01 19:15:38 -07:00
|
|
|
|
2021-04-22 11:20:04 -07:00
|
|
|
#endif // _H
|