summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Henry <[email protected]>2023-08-21 13:07:46 -0700
committerGitHub <[email protected]>2023-08-21 13:07:46 -0700
commitaf217eb083fb43de83fab3b5e2c94f7dd2282865 (patch)
tree5ca7f39a8410ec26e8e0da42b8b13e6a5879f77c
parentf800bba4d1692f9219e03f857f0cdeaeba4f11b1 (diff)
parentee5047b59af26d8c2e37f597cc2f3665dbf80b3a (diff)
Merge pull request #251 from zerotier/bugfix/244
-rw-r--r--src/Events.cpp26
-rw-r--r--src/Events.hpp6
-rw-r--r--src/NodeService.cpp123
-rw-r--r--src/VirtualTap.cpp4
4 files changed, 116 insertions, 43 deletions
diff --git a/src/Events.cpp b/src/Events.cpp
index 30c6845..f549782 100644
--- a/src/Events.cpp
+++ b/src/Events.cpp
@@ -93,11 +93,18 @@ void Events::run()
}
}
-void Events::enqueue(unsigned int event_code, const void* arg, int len)
+bool Events::enqueue(unsigned int event_code, const void* arg, int len)
{
if (! _enabled) {
- return;
+ return false;
+ }
+ if (_callbackMsgQueue.size_approx() > 1024) {
+ /* Rate-limit number of events. This value should only grow if the
+ user application isn't returning from the event handler in a timely manner.
+ For most applications it should hover around 1 to 2 */
+ return false;
}
+
zts_event_msg_t* msg = new zts_event_msg_t();
msg->event_code = event_code;
@@ -132,15 +139,12 @@ void Events::enqueue(unsigned int event_code, const void* arg, int len)
msg->cache = (void*)arg;
msg->len = len;
}
- if (msg && _callbackMsgQueue.size_approx() > 1024) {
- /* Rate-limit number of events. This value should only grow if the
- user application isn't returning from the event handler in a timely manner.
- For most applications it should hover around 1 to 2 */
- destroy(msg);
- }
- else {
- _callbackMsgQueue.enqueue(msg);
- }
+
+ //
+ // ownership of arg is now transferred
+ //
+ _callbackMsgQueue.enqueue(msg);
+ return true;
}
void Events::destroy(zts_event_msg_t* msg)
diff --git a/src/Events.hpp b/src/Events.hpp
index f0ceb26..9064d69 100644
--- a/src/Events.hpp
+++ b/src/Events.hpp
@@ -115,8 +115,12 @@ class Events {
/**
* Enqueue an event to be sent to the user application
+ *
+ * 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.
*/
- void enqueue(unsigned int event_code, const void* arg, int len = 0);
+ bool enqueue(unsigned int event_code, const void* arg, int len = 0);
/**
* Send callback message to user application
diff --git a/src/NodeService.cpp b/src/NodeService.cpp
index b25bb80..501bc13 100644
--- a/src/NodeService.cpp
+++ b/src/NodeService.cpp
@@ -957,13 +957,17 @@ void NodeService::sendEventToUser(unsigned int zt_event_code, const void* obj, u
void* objptr = NULL;
+ zts_node_info_t* nd;
+ zts_net_info_t* nt;
+ zts_peer_info_t* pr;
+
switch (zt_event_code) {
case ZTS_EVENT_NODE_UP:
case ZTS_EVENT_NODE_ONLINE:
case ZTS_EVENT_NODE_OFFLINE:
case ZTS_EVENT_NODE_DOWN:
case ZTS_EVENT_NODE_FATAL_ERROR: {
- zts_node_info_t* nd = new zts_node_info_t;
+ nd = new zts_node_info_t;
nd->node_id = _nodeId;
nd->ver_major = ZEROTIER_ONE_VERSION_MAJOR;
nd->ver_minor = ZEROTIER_ONE_VERSION_MINOR;
@@ -980,9 +984,9 @@ void NodeService::sendEventToUser(unsigned int zt_event_code, const void* obj, u
case ZTS_EVENT_NETWORK_ACCESS_DENIED:
case ZTS_EVENT_NETWORK_DOWN: {
NetworkState* ns = (NetworkState*)obj;
- zts_net_info_t* nd = new zts_net_info_t();
- nd->net_id = ns->config.nwid;
- objptr = (void*)nd;
+ nt = new zts_net_info_t();
+ nt->net_id = ns->config.nwid;
+ objptr = (void*)nt;
break;
}
case ZTS_EVENT_NETWORK_UPDATE:
@@ -990,33 +994,33 @@ void NodeService::sendEventToUser(unsigned int zt_event_code, const void* obj, u
case ZTS_EVENT_NETWORK_READY_IP6:
case ZTS_EVENT_NETWORK_OK: {
NetworkState* ns = (NetworkState*)obj;
- zts_net_info_t* nd = new zts_net_info_t();
- nd->net_id = ns->config.nwid;
- nd->mac = ns->config.mac;
- strncpy(nd->name, ns->config.name, sizeof(ns->config.name));
- nd->status = (zts_network_status_t)ns->config.status;
- nd->type = (zts_net_info_type_t)ns->config.type;
- nd->mtu = ns->config.mtu;
- nd->dhcp = ns->config.dhcp;
- nd->bridge = ns->config.bridge;
- nd->broadcast_enabled = ns->config.broadcastEnabled;
- nd->port_error = ns->config.portError;
- nd->netconf_rev = ns->config.netconfRevision;
+ nt = new zts_net_info_t();
+ nt->net_id = ns->config.nwid;
+ nt->mac = ns->config.mac;
+ strncpy(nt->name, ns->config.name, sizeof(ns->config.name));
+ nt->status = (zts_network_status_t)ns->config.status;
+ nt->type = (zts_net_info_type_t)ns->config.type;
+ nt->mtu = ns->config.mtu;
+ nt->dhcp = ns->config.dhcp;
+ nt->bridge = ns->config.bridge;
+ nt->broadcast_enabled = ns->config.broadcastEnabled;
+ nt->port_error = ns->config.portError;
+ nt->netconf_rev = ns->config.netconfRevision;
// Copy and convert address structures
- nd->assigned_addr_count = ns->config.assignedAddressCount;
+ nt->assigned_addr_count = ns->config.assignedAddressCount;
for (unsigned int i = 0; i < ns->config.assignedAddressCount; i++) {
- native_ss_to_zts_ss(&(nd->assigned_addrs[i]), &(ns->config.assignedAddresses[i]));
+ native_ss_to_zts_ss(&(nt->assigned_addrs[i]), &(ns->config.assignedAddresses[i]));
}
- nd->route_count = ns->config.routeCount;
+ nt->route_count = ns->config.routeCount;
for (unsigned int i = 0; i < ns->config.routeCount; i++) {
- native_ss_to_zts_ss(&(nd->routes[i].target), &(ns->config.routes[i].target));
- native_ss_to_zts_ss(&(nd->routes[i].via), &(ns->config.routes[i].via));
- nd->routes[i].flags = ns->config.routes[i].flags;
- nd->routes[i].metric = ns->config.routes[i].metric;
+ native_ss_to_zts_ss(&(nt->routes[i].target), &(ns->config.routes[i].target));
+ native_ss_to_zts_ss(&(nt->routes[i].via), &(ns->config.routes[i].via));
+ nt->routes[i].flags = ns->config.routes[i].flags;
+ nt->routes[i].metric = ns->config.routes[i].metric;
}
- nd->multicast_sub_count = ns->config.multicastSubscriptionCount;
- memcpy(nd->multicast_subs, &(ns->config.multicastSubscriptions), sizeof(ns->config.multicastSubscriptions));
- objptr = (void*)nd;
+ nt->multicast_sub_count = ns->config.multicastSubscriptionCount;
+ memcpy(nt->multicast_subs, &(ns->config.multicastSubscriptions), sizeof(ns->config.multicastSubscriptions));
+ objptr = (void*)nt;
break;
}
case ZTS_EVENT_ADDR_ADDED_IP4:
@@ -1051,13 +1055,13 @@ void NodeService::sendEventToUser(unsigned int zt_event_code, const void* obj, u
case ZTS_EVENT_PEER_UNREACHABLE:
case ZTS_EVENT_PEER_PATH_DISCOVERED:
case ZTS_EVENT_PEER_PATH_DEAD: {
- zts_peer_info_t* pd = new zts_peer_info_t();
+ pr = new zts_peer_info_t();
ZT_Peer* peer = (ZT_Peer*)obj;
- memcpy(pd, peer, sizeof(zts_peer_info_t));
+ memcpy(pr, peer, sizeof(zts_peer_info_t));
for (unsigned int j = 0; j < peer->pathCount; j++) {
- native_ss_to_zts_ss(&(pd->paths[j].address), &(peer->paths[j].address));
+ native_ss_to_zts_ss(&(pr->paths[j].address), &(peer->paths[j].address));
}
- objptr = (void*)pd;
+ objptr = (void*)pr;
break;
}
default:
@@ -1067,7 +1071,64 @@ void NodeService::sendEventToUser(unsigned int zt_event_code, const void* obj, u
// Send event
if (objptr) {
- _events->enqueue(zt_event_code, objptr, len);
+ if (!_events->enqueue(zt_event_code, objptr, len)) {
+ //
+ // ownership of objptr was NOT transferred, so delete any news from above
+ //
+ switch (zt_event_code) {
+ case ZTS_EVENT_NODE_UP:
+ case ZTS_EVENT_NODE_ONLINE:
+ case ZTS_EVENT_NODE_OFFLINE:
+ case ZTS_EVENT_NODE_DOWN:
+ case ZTS_EVENT_NODE_FATAL_ERROR: {
+ delete nd;
+ break;
+ }
+ case ZTS_EVENT_NETWORK_NOT_FOUND:
+ case ZTS_EVENT_NETWORK_CLIENT_TOO_OLD:
+ case ZTS_EVENT_NETWORK_REQ_CONFIG:
+ case ZTS_EVENT_NETWORK_ACCESS_DENIED:
+ case ZTS_EVENT_NETWORK_DOWN: {
+ delete nt;
+ break;
+ }
+ case ZTS_EVENT_NETWORK_UPDATE:
+ case ZTS_EVENT_NETWORK_READY_IP4:
+ case ZTS_EVENT_NETWORK_READY_IP6:
+ case ZTS_EVENT_NETWORK_OK: {
+ delete nt;
+ break;
+ }
+ case ZTS_EVENT_ADDR_ADDED_IP4:
+ break;
+ case ZTS_EVENT_ADDR_ADDED_IP6:
+ break;
+ case ZTS_EVENT_ADDR_REMOVED_IP4:
+ break;
+ case ZTS_EVENT_ADDR_REMOVED_IP6:
+ break;
+ case ZTS_EVENT_STORE_IDENTITY_PUBLIC:
+ break;
+ case ZTS_EVENT_STORE_IDENTITY_SECRET:
+ break;
+ case ZTS_EVENT_STORE_PLANET:
+ break;
+ case ZTS_EVENT_STORE_PEER:
+ break;
+ case ZTS_EVENT_STORE_NETWORK:
+ break;
+ case ZTS_EVENT_PEER_DIRECT:
+ case ZTS_EVENT_PEER_RELAY:
+ case ZTS_EVENT_PEER_UNREACHABLE:
+ case ZTS_EVENT_PEER_PATH_DISCOVERED:
+ case ZTS_EVENT_PEER_PATH_DEAD: {
+ delete pr;
+ break;
+ }
+ default:
+ break;
+ }
+ }
}
}
diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp
index a13dcbd..605ae4d 100644
--- a/src/VirtualTap.cpp
+++ b/src/VirtualTap.cpp
@@ -312,6 +312,10 @@ static void zts_main_lwip_driver_loop(void* arg)
zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL);
}
_has_exited = true;
+
+ //
+ // no need to check if event was enqueued since NULL is being passed
+ //
zts_events->enqueue(ZTS_EVENT_STACK_DOWN, NULL);
}