Added exception handling for virtual socket add/del functions
This commit is contained in:
@@ -105,9 +105,19 @@ struct zts_ifreq {
|
|||||||
// For LWIP configuration see: include/lwipopts.h
|
// For LWIP configuration see: include/lwipopts.h
|
||||||
|
|
||||||
#if defined(STACK_LWIP)
|
#if defined(STACK_LWIP)
|
||||||
|
/*
|
||||||
|
Specifies the polling interval and the callback function that should
|
||||||
|
be called to poll the application. The interval is specified in
|
||||||
|
number of TCP coarse grained timer shots, which typically occurs
|
||||||
|
twice a second. An interval of 10 means that the application would
|
||||||
|
be polled every 5 seconds. */
|
||||||
#define LWIP_APPLICATION_POLL_FREQ 2
|
#define LWIP_APPLICATION_POLL_FREQ 2
|
||||||
#define LWIP_TCP_TIMER_INTERVAL 50
|
|
||||||
#define LWIP_STATUS_TMR_INTERVAL 500 // How often we check VirtualSocket statuses (in ms)
|
#define LWIP_TCP_TIMER_INTERVAL 25
|
||||||
|
|
||||||
|
// How often we check VirtualSocket statuses (in ms)
|
||||||
|
#define LWIP_STATUS_TMR_INTERVAL 500
|
||||||
|
|
||||||
// #define LWIP_CHKSUM <your_checksum_routine>, See: RFC1071 for inspiration
|
// #define LWIP_CHKSUM <your_checksum_routine>, See: RFC1071 for inspiration
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -591,26 +601,26 @@ ZeroTier::VirtualSocket *get_virtual_socket(int fd);
|
|||||||
/*
|
/*
|
||||||
* Removes a VirtualSocket
|
* Removes a VirtualSocket
|
||||||
*/
|
*/
|
||||||
void del_virtual_socket(int fd);
|
int del_virtual_socket(int fd);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Adds a virtualSocket
|
* Adds a virtualSocket
|
||||||
*/
|
*/
|
||||||
void add_unassigned_virtual_socket(int fd, ZeroTier::VirtualSocket *vs);
|
int add_unassigned_virtual_socket(int fd, ZeroTier::VirtualSocket *vs);
|
||||||
/*
|
/*
|
||||||
* Removes unassigned VirtualSocket
|
* Removes unassigned VirtualSocket
|
||||||
*/
|
*/
|
||||||
void del_unassigned_virtual_socket(int fd);
|
int del_unassigned_virtual_socket(int fd);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Adds an assigned VirtualSocket
|
* Adds an assigned VirtualSocket
|
||||||
*/
|
*/
|
||||||
void add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd);
|
int add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Removes an assigned VirtualSocket
|
* Removes an assigned VirtualSocket
|
||||||
*/
|
*/
|
||||||
void del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd);
|
int del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gets a pair of associated virtual objects (VirtualSocket bound to a VirtualTap)
|
* Gets a pair of associated virtual objects (VirtualSocket bound to a VirtualTap)
|
||||||
|
|||||||
136
src/libzt.cpp
136
src/libzt.cpp
@@ -140,7 +140,16 @@ void zts_simple_start(const char *path, const char *nwid)
|
|||||||
while(!zts_running()) {
|
while(!zts_running()) {
|
||||||
nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
|
nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
|
||||||
}
|
}
|
||||||
zts_join(nwid);
|
while(1) {
|
||||||
|
try {
|
||||||
|
zts_join(nwid);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch( ... ) {
|
||||||
|
DEBUG_ERROR("there was a problem joining the virtual network");
|
||||||
|
handle_general_failure();
|
||||||
|
}
|
||||||
|
}
|
||||||
while(!zts_has_address(nwid)) {
|
while(!zts_has_address(nwid)) {
|
||||||
nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
|
nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
|
||||||
}
|
}
|
||||||
@@ -2153,66 +2162,111 @@ ZeroTier::VirtualSocket *get_virtual_socket(int fd)
|
|||||||
return vs;
|
return vs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void del_virtual_socket(int fd)
|
int del_virtual_socket(int fd)
|
||||||
{
|
{
|
||||||
|
int err = 0;
|
||||||
ZeroTier::_multiplexer_lock.lock();
|
ZeroTier::_multiplexer_lock.lock();
|
||||||
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
try {
|
||||||
if(un_iter != ZeroTier::unmap.end()) {
|
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
||||||
ZeroTier::unmap.erase(un_iter);
|
if(un_iter != ZeroTier::unmap.end()) {
|
||||||
|
ZeroTier::unmap.erase(un_iter);
|
||||||
|
}
|
||||||
|
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
||||||
|
if(fd_iter != ZeroTier::fdmap.end()) {
|
||||||
|
ZeroTier::fdmap.erase(fd_iter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
catch( ... ) {
|
||||||
if(fd_iter != ZeroTier::fdmap.end()) {
|
DEBUG_ERROR("unable to remove virtual socket");
|
||||||
ZeroTier::fdmap.erase(fd_iter);
|
|
||||||
}
|
|
||||||
ZeroTier::_multiplexer_lock.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_unassigned_virtual_socket(int fd, ZeroTier::VirtualSocket *vs)
|
|
||||||
{
|
|
||||||
ZeroTier::_multiplexer_lock.lock();
|
|
||||||
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
|
||||||
if(un_iter == ZeroTier::unmap.end()) {
|
|
||||||
ZeroTier::unmap[fd] = vs;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DEBUG_ERROR("fd=%d already contained in <fd:vs> map", fd);
|
|
||||||
handle_general_failure();
|
handle_general_failure();
|
||||||
|
err = -1;
|
||||||
}
|
}
|
||||||
ZeroTier::_multiplexer_lock.unlock();
|
ZeroTier::_multiplexer_lock.unlock();
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void del_unassigned_virtual_socket(int fd)
|
int add_unassigned_virtual_socket(int fd, ZeroTier::VirtualSocket *vs)
|
||||||
{
|
{
|
||||||
|
int err = 0;
|
||||||
ZeroTier::_multiplexer_lock.lock();
|
ZeroTier::_multiplexer_lock.lock();
|
||||||
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
try {
|
||||||
if(un_iter != ZeroTier::unmap.end()) {
|
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
||||||
ZeroTier::unmap.erase(un_iter);
|
if(un_iter == ZeroTier::unmap.end()) {
|
||||||
|
ZeroTier::unmap[fd] = vs;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DEBUG_ERROR("fd=%d already contained in <fd:vs> map", fd);
|
||||||
|
handle_general_failure();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ZeroTier::_multiplexer_lock.unlock();
|
catch( ... ) {
|
||||||
}
|
DEBUG_ERROR("unable to add virtual socket");
|
||||||
|
|
||||||
void add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
|
|
||||||
{
|
|
||||||
ZeroTier::_multiplexer_lock.lock();
|
|
||||||
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
|
||||||
if(fd_iter == ZeroTier::fdmap.end()) {
|
|
||||||
ZeroTier::fdmap[fd] = new std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>(vs, tap);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DEBUG_ERROR("fd=%d already contained in <fd,<vs,vt>> map", fd);
|
|
||||||
handle_general_failure();
|
handle_general_failure();
|
||||||
|
err = -1;
|
||||||
}
|
}
|
||||||
ZeroTier::_multiplexer_lock.unlock();
|
ZeroTier::_multiplexer_lock.unlock();
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
|
int del_unassigned_virtual_socket(int fd)
|
||||||
{
|
{
|
||||||
|
int err = 0;
|
||||||
ZeroTier::_multiplexer_lock.lock();
|
ZeroTier::_multiplexer_lock.lock();
|
||||||
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
try {
|
||||||
if(fd_iter != ZeroTier::fdmap.end()) {
|
std::map<int, ZeroTier::VirtualSocket*>::iterator un_iter = ZeroTier::unmap.find(fd);
|
||||||
ZeroTier::fdmap.erase(fd_iter);
|
if(un_iter != ZeroTier::unmap.end()) {
|
||||||
|
ZeroTier::unmap.erase(un_iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch( ... ) {
|
||||||
|
DEBUG_ERROR("unable to remove virtual socket");
|
||||||
|
handle_general_failure();
|
||||||
|
err = -1;
|
||||||
}
|
}
|
||||||
ZeroTier::_multiplexer_lock.unlock();
|
ZeroTier::_multiplexer_lock.unlock();
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
|
||||||
|
{
|
||||||
|
int err = 0;
|
||||||
|
ZeroTier::_multiplexer_lock.lock();
|
||||||
|
try {
|
||||||
|
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
||||||
|
if(fd_iter == ZeroTier::fdmap.end()) {
|
||||||
|
ZeroTier::fdmap[fd] = new std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>(vs, tap);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DEBUG_ERROR("fd=%d already contained in <fd,<vs,vt>> map", fd);
|
||||||
|
handle_general_failure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch( ... ) {
|
||||||
|
DEBUG_ERROR("unable to add virtual socket");
|
||||||
|
handle_general_failure();
|
||||||
|
err = -1;
|
||||||
|
}
|
||||||
|
ZeroTier::_multiplexer_lock.unlock();
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
int del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
|
||||||
|
{
|
||||||
|
int err = 0;
|
||||||
|
ZeroTier::_multiplexer_lock.lock();
|
||||||
|
try {
|
||||||
|
std::map<int, std::pair<ZeroTier::VirtualSocket*,ZeroTier::VirtualTap*>*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
|
||||||
|
if(fd_iter != ZeroTier::fdmap.end()) {
|
||||||
|
ZeroTier::fdmap.erase(fd_iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch( ... ) {
|
||||||
|
DEBUG_ERROR("unable to remove virtual socket");
|
||||||
|
handle_general_failure();
|
||||||
|
err = -1;
|
||||||
|
}
|
||||||
|
ZeroTier::_multiplexer_lock.unlock();
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *get_assigned_virtual_pair(int fd)
|
std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *get_assigned_virtual_pair(int fd)
|
||||||
|
|||||||
Reference in New Issue
Block a user