diff options
| -rw-r--r-- | controller/FileDB.cpp | 2 | ||||
| -rw-r--r-- | controller/PostgreSQL.cpp | 32 | ||||
| -rw-r--r-- | osdep/WindowsEthernetTap.cpp | 57 |
3 files changed, 90 insertions, 1 deletions
diff --git a/controller/FileDB.cpp b/controller/FileDB.cpp index 64c305bb..eb2ec00d 100644 --- a/controller/FileDB.cpp +++ b/controller/FileDB.cpp @@ -75,7 +75,7 @@ FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const ch _onlineUpdateThread = std::thread([this]() { unsigned int cnt = 0; while (this->_running) { - usleep(250); + std::this_thread::sleep_for(std::chrono::microseconds(100)); if ((++cnt % 20) == 0) { // 5 seconds std::lock_guard<std::mutex> l(this->_online_l); if (!this->_running) return; diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp index de6b4f46..594591bd 100644 --- a/controller/PostgreSQL.cpp +++ b/controller/PostgreSQL.cpp @@ -39,6 +39,8 @@ using json = nlohmann::json; namespace { +static const int DB_MINIMUM_VERSION = 5; + static const char *_timestr() { time_t t = time(0); @@ -86,6 +88,36 @@ PostgreSQL::PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId { _connString = std::string(path) + " application_name=controller_" +_myAddressStr; + // Database Schema Version Check + PGconn *conn = getPgConn(); + if (PQstatus(conn) != CONNECTION_OK) { + fprintf(stderr, "Bad Database Connection: %s", PQerrorMessage(conn)); + exit(1); + } + + PGresult *res = PQexec(conn, "SELECT version FROM ztc_database"); + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + fprintf(stderr, "Error determining database version"); + exit(1); + } + + if (PQntuples(res) != 1) { + fprintf(stderr, "Invalid number of db version tuples returned."); + exit(1); + } + + int dbVersion = std::stoi(PQgetvalue(res, 0, 0)); + + if (dbVersion < DB_MINIMUM_VERSION) { + fprintf(stderr, "Central database schema version too low. This controller version requires a minimum schema version of %d. Please upgrade your Central instance", DB_MINIMUM_VERSION); + exit(1); + } + + PQclear(res); + res = NULL; + PQfinish(conn); + conn = NULL; + _readyLock.lock(); _heartbeatThread = std::thread(&PostgreSQL::heartbeat, this); _membersDbWatcher = std::thread(&PostgreSQL::membersDbWatcher, this); diff --git a/osdep/WindowsEthernetTap.cpp b/osdep/WindowsEthernetTap.cpp index 65335dca..773c56c1 100644 --- a/osdep/WindowsEthernetTap.cpp +++ b/osdep/WindowsEthernetTap.cpp @@ -58,6 +58,8 @@ #include "..\windows\TapDriver6\tap-windows.h" +#include <netcon.h> + // Create a fake unused default route to force detection of network type on networks without gateways #define ZT_WINDOWS_CREATE_FAKE_DEFAULT_ROUTE @@ -824,6 +826,61 @@ void WindowsEthernetTap::setFriendlyName(const char *dn) RegSetKeyValueA(ifp,"Connection","Name",REG_SZ,(LPCVOID)dn,(DWORD)(strlen(dn)+1)); RegCloseKey(ifp); } + + HRESULT hr = CoInitialize(nullptr); + if (hr != S_OK) return; + CoInitializeSecurity(NULL, -1, NULL, NULL, + RPC_C_AUTHN_LEVEL_PKT, + RPC_C_IMP_LEVEL_IMPERSONATE, + NULL, EOAC_NONE, NULL); + if (hr != S_OK) return; + + INetSharingManager *nsm; + hr = CoCreateInstance(__uuidof(NetSharingManager), NULL, CLSCTX_ALL, __uuidof(INetSharingManager), (void**)&nsm); + if (hr != S_OK) return; + + bool found = false; + INetSharingEveryConnectionCollection *nsecc = nullptr; + hr = nsm->get_EnumEveryConnection(&nsecc); + if (!nsecc) { + fprintf(stderr, "Failed to get NSM connections"); + return; + } + + IEnumVARIANT *ev = nullptr; + IUnknown *unk = nullptr; + hr = nsecc->get__NewEnum(&unk); + if (unk) { + hr = unk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&ev); + unk->Release(); + } + if (ev) { + VARIANT v; + VariantInit(&v); + + while ((S_OK == ev->Next(1, &v, NULL)) && found == FALSE) { + if (V_VT(&v) == VT_UNKNOWN) { + INetConnection *nc = nullptr; + V_UNKNOWN(&v)->QueryInterface(__uuidof(INetConnection), (void**)&nc); + if (nc) { + NETCON_PROPERTIES *ncp = nullptr; + nc->GetProperties(&ncp); + + GUID curId = ncp->guidId; + if (curId == _deviceGuid) { + wchar_t wtext[255]; + mbstowcs(wtext, dn, strlen(dn)+1); + nc->Rename(wtext); + found = true; + } + nc->Release(); + } + } + VariantClear(&v); + } + ev->Release(); + } + nsecc->Release(); } void WindowsEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) |
