mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 03:53:44 +02:00
.
This commit is contained in:
parent
7762cd680b
commit
3cb2e1197f
1 changed files with 47 additions and 57 deletions
|
@ -819,15 +819,16 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
return 404;
|
return 404;
|
||||||
Mutex::Lock _l(_lock);
|
Mutex::Lock _l(_lock);
|
||||||
|
|
||||||
_backupNeeded = true;
|
|
||||||
|
|
||||||
if (path[0] == "network") {
|
if (path[0] == "network") {
|
||||||
|
json &networks = _db["network"];
|
||||||
|
if (!networks.is_object()) networks = json::object();
|
||||||
|
|
||||||
if ((path.size() >= 2)&&(path[1].length() == 16)) {
|
if ((path.size() >= 2)&&(path[1].length() == 16)) {
|
||||||
uint64_t nwid = Utils::hexStrToU64(path[1].c_str());
|
uint64_t nwid = Utils::hexStrToU64(path[1].c_str());
|
||||||
char nwids[24];
|
char nwids[24];
|
||||||
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
||||||
|
|
||||||
|
/*
|
||||||
int64_t revision = 0;
|
int64_t revision = 0;
|
||||||
sqlite3_reset(_sGetNetworkRevision);
|
sqlite3_reset(_sGetNetworkRevision);
|
||||||
sqlite3_bind_text(_sGetNetworkRevision,1,nwids,16,SQLITE_STATIC);
|
sqlite3_bind_text(_sGetNetworkRevision,1,nwids,16,SQLITE_STATIC);
|
||||||
|
@ -836,17 +837,23 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
networkExists = true;
|
networkExists = true;
|
||||||
revision = sqlite3_column_int64(_sGetNetworkRevision,0);
|
revision = sqlite3_column_int64(_sGetNetworkRevision,0);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if (path.size() >= 3) {
|
if (path.size() >= 3) {
|
||||||
|
auto network = networks.get<const json::object_t *>(nwids);
|
||||||
if (!networkExists)
|
if (!network) return 404;
|
||||||
return 404;
|
|
||||||
|
|
||||||
if ((path.size() == 4)&&(path[2] == "member")&&(path[3].length() == 10)) {
|
if ((path.size() == 4)&&(path[2] == "member")&&(path[3].length() == 10)) {
|
||||||
|
json &members = (*network)["member"];
|
||||||
|
if (!members.is_object()) members = json::object();
|
||||||
|
|
||||||
uint64_t address = Utils::hexStrToU64(path[3].c_str());
|
uint64_t address = Utils::hexStrToU64(path[3].c_str());
|
||||||
char addrs[24];
|
char addrs[24];
|
||||||
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",address);
|
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",address);
|
||||||
|
json &member = members[addrs];
|
||||||
|
if (!member.is_object()) member = json::object();
|
||||||
|
|
||||||
|
/*
|
||||||
int64_t addToNetworkRevision = 0;
|
int64_t addToNetworkRevision = 0;
|
||||||
|
|
||||||
int64_t memberRowId = 0;
|
int64_t memberRowId = 0;
|
||||||
|
@ -913,7 +920,7 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
sqlite3_reset(_sDeleteIpAllocations);
|
sqlite3_reset(_sDeleteIpAllocations);
|
||||||
sqlite3_bind_text(_sDeleteIpAllocations,1,nwids,16,SQLITE_STATIC);
|
sqlite3_bind_text(_sDeleteIpAllocations,1,nwids,16,SQLITE_STATIC);
|
||||||
sqlite3_bind_text(_sDeleteIpAllocations,2,addrs,10,SQLITE_STATIC);
|
sqlite3_bind_text(_sDeleteIpAllocations,2,addrs,10,SQLITE_STATIC);
|
||||||
sqlite3_bind_int(_sDeleteIpAllocations,3,(int)0 /*ZT_IP_ASSIGNMENT_TYPE_ADDRESS*/);
|
sqlite3_bind_int(_sDeleteIpAllocations,3,(int)0);
|
||||||
if (sqlite3_step(_sDeleteIpAllocations) != SQLITE_DONE)
|
if (sqlite3_step(_sDeleteIpAllocations) != SQLITE_DONE)
|
||||||
return 500;
|
return 500;
|
||||||
for(unsigned int kk=0;kk<j->u.object.values[k].value->u.array.length;++kk) {
|
for(unsigned int kk=0;kk<j->u.object.values[k].value->u.array.length;++kk) {
|
||||||
|
@ -927,7 +934,7 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
sqlite3_reset(_sAllocateIp);
|
sqlite3_reset(_sAllocateIp);
|
||||||
sqlite3_bind_text(_sAllocateIp,1,nwids,16,SQLITE_STATIC);
|
sqlite3_bind_text(_sAllocateIp,1,nwids,16,SQLITE_STATIC);
|
||||||
sqlite3_bind_text(_sAllocateIp,2,addrs,10,SQLITE_STATIC);
|
sqlite3_bind_text(_sAllocateIp,2,addrs,10,SQLITE_STATIC);
|
||||||
sqlite3_bind_int(_sAllocateIp,3,(int)0 /*ZT_IP_ASSIGNMENT_TYPE_ADDRESS*/);
|
sqlite3_bind_int(_sAllocateIp,3,(int)0);
|
||||||
sqlite3_bind_blob(_sAllocateIp,4,(const void *)ipBlob,16,SQLITE_STATIC);
|
sqlite3_bind_blob(_sAllocateIp,4,(const void *)ipBlob,16,SQLITE_STATIC);
|
||||||
sqlite3_bind_int(_sAllocateIp,5,(int)a.netmaskBits()); // NOTE: this field is now ignored but set it anyway
|
sqlite3_bind_int(_sAllocateIp,5,(int)a.netmaskBits()); // NOTE: this field is now ignored but set it anyway
|
||||||
sqlite3_bind_int(_sAllocateIp,6,ipVersion);
|
sqlite3_bind_int(_sAllocateIp,6,ipVersion);
|
||||||
|
@ -980,6 +987,7 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
sqlite3_bind_text(_sSetNetworkRevision,2,nwids,16,SQLITE_STATIC);
|
sqlite3_bind_text(_sSetNetworkRevision,2,nwids,16,SQLITE_STATIC);
|
||||||
sqlite3_step(_sSetNetworkRevision);
|
sqlite3_step(_sSetNetworkRevision);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
return _doCPGet(path,urlArgs,headers,body,responseBody,responseContentType);
|
return _doCPGet(path,urlArgs,headers,body,responseBody,responseContentType);
|
||||||
} else if ((path.size() == 3)&&(path[2] == "test")) {
|
} else if ((path.size() == 3)&&(path[2] == "test")) {
|
||||||
|
@ -990,6 +998,7 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
test->credentialNetworkId = nwid;
|
test->credentialNetworkId = nwid;
|
||||||
test->ptr = (void *)this;
|
test->ptr = (void *)this;
|
||||||
|
|
||||||
|
/*
|
||||||
json_value *j = json_parse(body.c_str(),body.length());
|
json_value *j = json_parse(body.c_str(),body.length());
|
||||||
if (j) {
|
if (j) {
|
||||||
if (j->type == json_object) {
|
if (j->type == json_object) {
|
||||||
|
@ -1018,6 +1027,7 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
}
|
}
|
||||||
json_value_free(j);
|
json_value_free(j);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if (!test->hopCount) {
|
if (!test->hopCount) {
|
||||||
::free((void *)test);
|
::free((void *)test);
|
||||||
|
@ -1036,7 +1046,6 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpPOST(
|
||||||
Utils::snprintf(json,sizeof(json),"{\"testId\":\"%.16llx\"}",test->testId);
|
Utils::snprintf(json,sizeof(json),"{\"testId\":\"%.16llx\"}",test->testId);
|
||||||
responseBody = json;
|
responseBody = json;
|
||||||
responseContentType = "application/json";
|
responseContentType = "application/json";
|
||||||
|
|
||||||
return 200;
|
return 200;
|
||||||
} // else 404
|
} // else 404
|
||||||
|
|
||||||
|
@ -1420,59 +1429,38 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpDELETE(
|
||||||
return 404;
|
return 404;
|
||||||
Mutex::Lock _l(_lock);
|
Mutex::Lock _l(_lock);
|
||||||
|
|
||||||
_backupNeeded = true;
|
|
||||||
|
|
||||||
if (path[0] == "network") {
|
if (path[0] == "network") {
|
||||||
|
auto networks = _db.get<const json::object_t *>("network");
|
||||||
|
if (!networks) return 404;
|
||||||
|
|
||||||
if ((path.size() >= 2)&&(path[1].length() == 16)) {
|
if ((path.size() >= 2)&&(path[1].length() == 16)) {
|
||||||
uint64_t nwid = Utils::hexStrToU64(path[1].c_str());
|
const uint64_t nwid = Utils::hexStrToU64(path[1].c_str());
|
||||||
char nwids[24];
|
char nwids[24];
|
||||||
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
||||||
|
auto network = _db.get<const json::object_t *>(nwids);
|
||||||
sqlite3_reset(_sGetNetworkById);
|
if (!network) return 404;
|
||||||
sqlite3_bind_text(_sGetNetworkById,1,nwids,16,SQLITE_STATIC);
|
|
||||||
if (sqlite3_step(_sGetNetworkById) != SQLITE_ROW)
|
|
||||||
return 404;
|
|
||||||
|
|
||||||
if (path.size() >= 3) {
|
if (path.size() >= 3) {
|
||||||
|
|
||||||
if ((path.size() == 4)&&(path[2] == "member")&&(path[3].length() == 10)) {
|
if ((path.size() == 4)&&(path[2] == "member")&&(path[3].length() == 10)) {
|
||||||
uint64_t address = Utils::hexStrToU64(path[3].c_str());
|
auto members = network->get<const json::object_t *>("member");
|
||||||
|
if (!members) return 404;
|
||||||
|
|
||||||
|
const uint64_t address = Utils::hexStrToU64(path[3].c_str());
|
||||||
char addrs[24];
|
char addrs[24];
|
||||||
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",address);
|
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",address);
|
||||||
|
auto member = members->get<const json::object_t *>(addrs);
|
||||||
|
if (!member) return 404;
|
||||||
|
|
||||||
sqlite3_reset(_sGetMember);
|
members->erase(addrs);
|
||||||
sqlite3_bind_text(_sGetMember,1,nwids,16,SQLITE_STATIC);
|
responseBody = member->dump(2);
|
||||||
sqlite3_bind_text(_sGetMember,2,addrs,10,SQLITE_STATIC);
|
responseContentType = "application/json";
|
||||||
if (sqlite3_step(_sGetMember) != SQLITE_ROW)
|
|
||||||
return 404;
|
|
||||||
|
|
||||||
sqlite3_reset(_sDeleteIpAllocations);
|
|
||||||
sqlite3_bind_text(_sDeleteIpAllocations,1,nwids,16,SQLITE_STATIC);
|
|
||||||
sqlite3_bind_text(_sDeleteIpAllocations,2,addrs,10,SQLITE_STATIC);
|
|
||||||
sqlite3_bind_int(_sDeleteIpAllocations,3,(int)0 /*ZT_IP_ASSIGNMENT_TYPE_ADDRESS*/);
|
|
||||||
if (sqlite3_step(_sDeleteIpAllocations) == SQLITE_DONE) {
|
|
||||||
sqlite3_reset(_sDeleteMember);
|
|
||||||
sqlite3_bind_text(_sDeleteMember,1,nwids,16,SQLITE_STATIC);
|
|
||||||
sqlite3_bind_text(_sDeleteMember,2,addrs,10,SQLITE_STATIC);
|
|
||||||
if (sqlite3_step(_sDeleteMember) != SQLITE_DONE)
|
|
||||||
return 500;
|
|
||||||
} else return 500;
|
|
||||||
|
|
||||||
return 200;
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
networks->erase(nwids);
|
||||||
sqlite3_reset(_sDeleteNetwork);
|
responseBody = network->dump(2);
|
||||||
sqlite3_bind_text(_sDeleteNetwork,1,nwids,16,SQLITE_STATIC);
|
responseContentType = "application/json";
|
||||||
if (sqlite3_step(_sDeleteNetwork) == SQLITE_DONE) {
|
return 200;
|
||||||
sqlite3_reset(_sDeleteAllNetworkMembers);
|
|
||||||
sqlite3_bind_text(_sDeleteAllNetworkMembers,1,nwids,16,SQLITE_STATIC);
|
|
||||||
sqlite3_step(_sDeleteAllNetworkMembers);
|
|
||||||
return 200;
|
|
||||||
} else return 500;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} // else 404
|
} // else 404
|
||||||
|
|
||||||
|
@ -1484,18 +1472,20 @@ unsigned int SqliteNetworkController::handleControlPlaneHttpDELETE(
|
||||||
void SqliteNetworkController::threadMain()
|
void SqliteNetworkController::threadMain()
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
bool run = true;
|
uint64_t lastCommit = OSUtils::now();
|
||||||
while(run) {
|
while(_dbCommitThreadRun) {
|
||||||
Thread::sleep(250);
|
Thread::sleep(200);
|
||||||
try {
|
if ((OSUtils::now() - lastCommit) > 2000) {
|
||||||
std::vector<std::string> errors;
|
lastCommit = OSUtils::now();
|
||||||
Mutex::Lock _l(_lock);
|
try {
|
||||||
run = _dbCommitThreadRun;
|
std::vector<std::string> errors;
|
||||||
if (!_db.commit(&errors)) {
|
Mutex::Lock _l(_lock);
|
||||||
|
if (!_db.commit(&errors)) {
|
||||||
|
// TODO: handle anything really bad
|
||||||
|
}
|
||||||
|
} catch ( ... ) {
|
||||||
// TODO: handle anything really bad
|
// TODO: handle anything really bad
|
||||||
}
|
}
|
||||||
} catch ( ... ) {
|
|
||||||
// TODO: handle anything really bad
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue