2
0

OneService.cpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <exception>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdint.h>
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <list>
  23. #include <thread>
  24. #include <mutex>
  25. #include <condition_variable>
  26. #include "../version.h"
  27. #include "../include/ZeroTierOne.h"
  28. #include "../node/Constants.hpp"
  29. #include "../node/Mutex.hpp"
  30. #include "../node/Node.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../node/InetAddress.hpp"
  33. #include "../node/MAC.hpp"
  34. #include "../node/Identity.hpp"
  35. #include "../node/World.hpp"
  36. #include "../node/Salsa20.hpp"
  37. #include "../node/Poly1305.hpp"
  38. #include "../node/SHA512.hpp"
  39. #include "../node/Bond.hpp"
  40. #include "../node/Peer.hpp"
  41. #include "../osdep/Phy.hpp"
  42. #include "../osdep/OSUtils.hpp"
  43. #include "../osdep/Http.hpp"
  44. #include "../osdep/PortMapper.hpp"
  45. #include "../osdep/Binder.hpp"
  46. #include "../osdep/ManagedRoute.hpp"
  47. #include "../osdep/BlockingQueue.hpp"
  48. #include "OneService.hpp"
  49. #include "SoftwareUpdater.hpp"
  50. #include <cpp-httplib/httplib.h>
  51. #if ZT_SSO_ENABLED
  52. #include <zeroidc.h>
  53. #endif
  54. #ifdef __WINDOWS__
  55. #include <winsock2.h>
  56. #include <windows.h>
  57. #include <shlobj.h>
  58. #include <netioapi.h>
  59. #include <iphlpapi.h>
  60. //#include <unistd.h>
  61. #define stat _stat
  62. #else
  63. #include <sys/types.h>
  64. #include <sys/socket.h>
  65. #include <sys/stat.h>
  66. #include <sys/wait.h>
  67. #include <unistd.h>
  68. #include <ifaddrs.h>
  69. #endif
  70. #ifdef __APPLE__
  71. #include "../osdep/MacDNSHelper.hpp"
  72. #elif defined(__WINDOWS__)
  73. #include "../osdep/WinDNSHelper.hpp"
  74. #include "../osdep/WinFWHelper.hpp"
  75. #endif
  76. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  77. #include <http_parser.h>
  78. #else
  79. #include "../ext/http-parser/http_parser.h"
  80. #endif
  81. #include "../node/Metrics.hpp"
  82. #if ZT_VAULT_SUPPORT
  83. extern "C" {
  84. #include <curl/curl.h>
  85. }
  86. #endif
  87. #include <nlohmann/json.hpp>
  88. #include <inja/inja.hpp>
  89. using json = nlohmann::json;
  90. #include "../controller/EmbeddedNetworkController.hpp"
  91. #include "../controller/PostgreSQL.hpp"
  92. #include "../controller/Redis.hpp"
  93. #include "../osdep/EthernetTap.hpp"
  94. #ifdef __WINDOWS__
  95. #include "../osdep/WindowsEthernetTap.hpp"
  96. #endif
  97. #ifndef ZT_SOFTWARE_UPDATE_DEFAULT
  98. #define ZT_SOFTWARE_UPDATE_DEFAULT "disable"
  99. #endif
  100. // Sanity limits for HTTP
  101. #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
  102. #define ZT_MAX_HTTP_CONNECTIONS 65536
  103. // Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
  104. // bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.
  105. #define ZT_IF_METRIC 5000
  106. // How often to check for new multicast subscriptions on a tap device
  107. #define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
  108. // TCP fallback relay (run by ZeroTier, Inc. -- this will eventually go away)
  109. #ifndef ZT_SDK
  110. #define ZT_TCP_FALLBACK_RELAY "204.80.128.1/443"
  111. #endif
  112. // Frequency at which we re-resolve the TCP fallback relay
  113. #define ZT_TCP_FALLBACK_RERESOLVE_DELAY 86400000
  114. // Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs
  115. #define ZT_TCP_FALLBACK_AFTER 60000
  116. // How often to check for local interface addresses
  117. #define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
  118. // Maximum write buffer size for outgoing TCP connections (sanity limit)
  119. #define ZT_TCP_MAX_WRITEQ_SIZE 33554432
  120. // TCP activity timeout
  121. #define ZT_TCP_ACTIVITY_TIMEOUT 60000
  122. #if ZT_VAULT_SUPPORT
  123. size_t curlResponseWrite(void *ptr, size_t size, size_t nmemb, std::string *data)
  124. {
  125. data->append((char*)ptr, size * nmemb);
  126. return size * nmemb;
  127. }
  128. #endif
  129. namespace ZeroTier {
  130. std::string ssoResponseTemplate = R"""(
  131. <!doctype html>
  132. <html class="no-js" lang="">
  133. <head>
  134. <meta charset="utf-8">
  135. <meta http-equiv="x-ua-compatible" content="ie=edge">
  136. <title>Network SSO Login {{ networkId }}</title>
  137. <meta name="description" content="">
  138. <meta name="viewport" content="width=device-width, initial-scale=1">
  139. <style type="text/css">
  140. html,body {
  141. background: #eeeeee;
  142. margin: 0;
  143. padding: 0;
  144. font-family: "System Sans Serif";
  145. font-weight: normal;
  146. font-size: 12pt;
  147. height: 100%;
  148. width: 100%;
  149. }
  150. .container {
  151. position: absolute;
  152. left: 50%;
  153. top: 50%;
  154. -webkit-transform: translate(-50%, -50%);
  155. transform: translate(-50%, -50%);
  156. }
  157. .iconwrapper {
  158. margin: 10px 10px 10px 10px;
  159. }
  160. </style>
  161. </head>
  162. <body>
  163. <div class="container">
  164. <div class="iconwrapper">
  165. <svg id="Layer_1" width="225px" height="225px" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 225 225"><defs><style>.cls-1{fill:#fdb25d;}.cls-2{fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:6.99px;}</style></defs><rect class="cls-1" width="225" height="225" rx="35.74"/><line class="cls-2" x1="25.65" y1="32.64" x2="199.35" y2="32.64"/><line class="cls-2" x1="112.5" y1="201.02" x2="112.5" y2="32.64"/><circle class="cls-2" cx="112.5" cy="115.22" r="56.54"/></svg>
  166. </div>
  167. <div class="text">{{ messageText }}</div>
  168. </div>
  169. </body>
  170. </html>
  171. )""";
  172. bool bearerTokenValid(const std::string authHeader, const std::string &checkToken) {
  173. std::vector<std::string> tokens = OSUtils::split(authHeader.c_str(), " ", NULL, NULL);
  174. if (tokens.size() != 2) {
  175. return false;
  176. }
  177. std::string bearer = tokens[0];
  178. std::string token = tokens[1];
  179. std::transform(bearer.begin(), bearer.end(), bearer.begin(), [](unsigned char c){return std::tolower(c);});
  180. if (bearer != "bearer") {
  181. return false;
  182. }
  183. if (token != checkToken) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. #if ZT_DEBUG==1
  189. std::string dump_headers(const httplib::Headers &headers) {
  190. std::string s;
  191. char buf[BUFSIZ];
  192. for (auto it = headers.begin(); it != headers.end(); ++it) {
  193. const auto &x = *it;
  194. snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
  195. s += buf;
  196. }
  197. return s;
  198. }
  199. std::string http_log(const httplib::Request &req, const httplib::Response &res) {
  200. std::string s;
  201. char buf[BUFSIZ];
  202. s += "================================\n";
  203. snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
  204. req.version.c_str(), req.path.c_str());
  205. s += buf;
  206. std::string query;
  207. for (auto it = req.params.begin(); it != req.params.end(); ++it) {
  208. const auto &x = *it;
  209. snprintf(buf, sizeof(buf), "%c%s=%s",
  210. (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
  211. x.second.c_str());
  212. query += buf;
  213. }
  214. snprintf(buf, sizeof(buf), "%s\n", query.c_str());
  215. s += buf;
  216. s += dump_headers(req.headers);
  217. s += "--------------------------------\n";
  218. snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
  219. s += buf;
  220. s += dump_headers(res.headers);
  221. s += "\n";
  222. if (!res.body.empty()) { s += res.body; }
  223. s += "\n";
  224. return s;
  225. }
  226. #endif
  227. // Configured networks
  228. class NetworkState
  229. {
  230. public:
  231. NetworkState()
  232. : _webPort(9993)
  233. , _tap((EthernetTap *)0)
  234. #if ZT_SSO_ENABLED
  235. , _idc(nullptr)
  236. #endif
  237. {
  238. // Real defaults are in network 'up' code in network event handler
  239. _settings.allowManaged = true;
  240. _settings.allowGlobal = false;
  241. _settings.allowDefault = false;
  242. _settings.allowDNS = false;
  243. memset(&_config, 0, sizeof(ZT_VirtualNetworkConfig));
  244. }
  245. ~NetworkState()
  246. {
  247. this->_managedRoutes.clear();
  248. this->_tap.reset();
  249. #if ZT_SSO_ENABLED
  250. if (_idc) {
  251. zeroidc::zeroidc_stop(_idc);
  252. zeroidc::zeroidc_delete(_idc);
  253. _idc = nullptr;
  254. }
  255. #endif
  256. }
  257. void setWebPort(unsigned int port) {
  258. _webPort = port;
  259. }
  260. void setTap(std::shared_ptr<EthernetTap> tap) {
  261. this->_tap = tap;
  262. }
  263. std::shared_ptr<EthernetTap> tap() const {
  264. return _tap;
  265. }
  266. OneService::NetworkSettings settings() const {
  267. return _settings;
  268. }
  269. void setSettings(const OneService::NetworkSettings &settings) {
  270. _settings = settings;
  271. }
  272. void setAllowManaged(bool allow) {
  273. _settings.allowManaged = allow;
  274. }
  275. bool allowManaged() const {
  276. return _settings.allowManaged;
  277. }
  278. void setAllowGlobal(bool allow) {
  279. _settings.allowGlobal = allow;
  280. }
  281. bool allowGlobal() const {
  282. return _settings.allowGlobal;
  283. }
  284. void setAllowDefault(bool allow) {
  285. _settings.allowDefault = allow;
  286. }
  287. bool allowDefault() const {
  288. return _settings.allowDefault;
  289. }
  290. void setAllowDNS(bool allow) {
  291. _settings.allowDNS = allow;
  292. }
  293. bool allowDNS() const {
  294. return _settings.allowDNS;
  295. }
  296. std::vector<InetAddress> allowManagedWhitelist() const {
  297. return _settings.allowManagedWhitelist;
  298. }
  299. void addToAllowManagedWhiteList(const InetAddress& addr) {
  300. _settings.allowManagedWhitelist.push_back(addr);
  301. }
  302. const ZT_VirtualNetworkConfig& config() {
  303. return _config;
  304. }
  305. void setConfig(const ZT_VirtualNetworkConfig *nwc) {
  306. memcpy(&_config, nwc, sizeof(ZT_VirtualNetworkConfig));
  307. if (_config.ssoEnabled && _config.ssoVersion == 1) {
  308. #if ZT_SSO_ENABLED
  309. if (_idc == nullptr)
  310. {
  311. assert(_config.issuerURL != nullptr);
  312. assert(_config.ssoClientID != nullptr);
  313. assert(_config.centralAuthURL != nullptr);
  314. assert(_config.ssoProvider != nullptr);
  315. _idc = zeroidc::zeroidc_new(
  316. _config.issuerURL,
  317. _config.ssoClientID,
  318. _config.centralAuthURL,
  319. _config.ssoProvider,
  320. _webPort
  321. );
  322. if (_idc == nullptr) {
  323. fprintf(stderr, "idc is null\n");
  324. return;
  325. }
  326. }
  327. zeroidc::zeroidc_set_nonce_and_csrf(
  328. _idc,
  329. _config.ssoState,
  330. _config.ssoNonce
  331. );
  332. char* url = zeroidc::zeroidc_get_auth_url(_idc);
  333. memcpy(_config.authenticationURL, url, strlen(url));
  334. _config.authenticationURL[strlen(url)] = 0;
  335. zeroidc::free_cstr(url);
  336. if (zeroidc::zeroidc_is_running(_idc) && nwc->status == ZT_NETWORK_STATUS_AUTHENTICATION_REQUIRED) {
  337. zeroidc::zeroidc_kick_refresh_thread(_idc);
  338. }
  339. #endif
  340. }
  341. }
  342. std::vector<InetAddress>& managedIps() {
  343. return _managedIps;
  344. }
  345. void setManagedIps(const std::vector<InetAddress> &managedIps) {
  346. _managedIps = managedIps;
  347. }
  348. std::map< InetAddress, SharedPtr<ManagedRoute> >& managedRoutes() {
  349. return _managedRoutes;
  350. }
  351. char* doTokenExchange(const char *code) {
  352. char *ret = nullptr;
  353. #if ZT_SSO_ENABLED
  354. if (_idc == nullptr) {
  355. fprintf(stderr, "ainfo or idc null\n");
  356. return ret;
  357. }
  358. ret = zeroidc::zeroidc_token_exchange(_idc, code);
  359. zeroidc::zeroidc_set_nonce_and_csrf(
  360. _idc,
  361. _config.ssoState,
  362. _config.ssoNonce
  363. );
  364. char* url = zeroidc::zeroidc_get_auth_url(_idc);
  365. memcpy(_config.authenticationURL, url, strlen(url));
  366. _config.authenticationURL[strlen(url)] = 0;
  367. zeroidc::free_cstr(url);
  368. #endif
  369. return ret;
  370. }
  371. uint64_t getExpiryTime() {
  372. #if ZT_SSO_ENABLED
  373. if (_idc == nullptr) {
  374. fprintf(stderr, "idc is null\n");
  375. return 0;
  376. }
  377. return zeroidc::zeroidc_get_exp_time(_idc);
  378. #else
  379. return 0;
  380. #endif
  381. }
  382. private:
  383. unsigned int _webPort;
  384. std::shared_ptr<EthernetTap> _tap;
  385. ZT_VirtualNetworkConfig _config; // memcpy() of raw config from core
  386. std::vector<InetAddress> _managedIps;
  387. std::map< InetAddress, SharedPtr<ManagedRoute> > _managedRoutes;
  388. OneService::NetworkSettings _settings;
  389. #if ZT_SSO_ENABLED
  390. zeroidc::ZeroIDC *_idc;
  391. #endif
  392. };
  393. namespace {
  394. static const InetAddress NULL_INET_ADDR;
  395. // Fake TLS hello for TCP tunnel outgoing connections (TUNNELED mode)
  396. static const char ZT_TCP_TUNNEL_HELLO[9] = { 0x17,0x03,0x03,0x00,0x04,(char)ZEROTIER_ONE_VERSION_MAJOR,(char)ZEROTIER_ONE_VERSION_MINOR,(char)((ZEROTIER_ONE_VERSION_REVISION >> 8) & 0xff),(char)(ZEROTIER_ONE_VERSION_REVISION & 0xff) };
  397. static std::string _trimString(const std::string &s)
  398. {
  399. unsigned long end = (unsigned long)s.length();
  400. while (end) {
  401. char c = s[end - 1];
  402. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  403. --end;
  404. else break;
  405. }
  406. unsigned long start = 0;
  407. while (start < end) {
  408. char c = s[start];
  409. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  410. ++start;
  411. else break;
  412. }
  413. return s.substr(start,end - start);
  414. }
  415. static void _networkToJson(nlohmann::json &nj,NetworkState &ns)
  416. {
  417. char tmp[256];
  418. const char *nstatus = "",*ntype = "";
  419. switch(ns.config().status) {
  420. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  421. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  422. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  423. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  424. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  425. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  426. case ZT_NETWORK_STATUS_AUTHENTICATION_REQUIRED: nstatus = "AUTHENTICATION_REQUIRED"; break;
  427. }
  428. switch(ns.config().type) {
  429. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  430. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  431. }
  432. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",ns.config().nwid);
  433. nj["id"] = tmp;
  434. nj["nwid"] = tmp;
  435. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)((ns.config().mac >> 40) & 0xff),(unsigned int)((ns.config().mac >> 32) & 0xff),(unsigned int)((ns.config().mac >> 24) & 0xff),(unsigned int)((ns.config().mac >> 16) & 0xff),(unsigned int)((ns.config().mac >> 8) & 0xff),(unsigned int)(ns.config().mac & 0xff));
  436. nj["mac"] = tmp;
  437. nj["name"] = ns.config().name;
  438. nj["status"] = nstatus;
  439. nj["type"] = ntype;
  440. nj["mtu"] = ns.config().mtu;
  441. nj["dhcp"] = (bool)(ns.config().dhcp != 0);
  442. nj["bridge"] = (bool)(ns.config().bridge != 0);
  443. nj["broadcastEnabled"] = (bool)(ns.config().broadcastEnabled != 0);
  444. nj["portError"] = ns.config().portError;
  445. nj["netconfRevision"] = ns.config().netconfRevision;
  446. nj["portDeviceName"] = ns.tap()->deviceName();
  447. OneService::NetworkSettings localSettings = ns.settings();
  448. nj["allowManaged"] = localSettings.allowManaged;
  449. nj["allowGlobal"] = localSettings.allowGlobal;
  450. nj["allowDefault"] = localSettings.allowDefault;
  451. nj["allowDNS"] = localSettings.allowDNS;
  452. nlohmann::json aa = nlohmann::json::array();
  453. for(unsigned int i=0;i<ns.config().assignedAddressCount;++i) {
  454. aa.push_back(reinterpret_cast<const InetAddress *>(&(ns.config().assignedAddresses[i]))->toString(tmp));
  455. }
  456. nj["assignedAddresses"] = aa;
  457. nlohmann::json ra = nlohmann::json::array();
  458. for(unsigned int i=0;i<ns.config().routeCount;++i) {
  459. nlohmann::json rj;
  460. rj["target"] = reinterpret_cast<const InetAddress *>(&(ns.config().routes[i].target))->toString(tmp);
  461. if (ns.config().routes[i].via.ss_family == ns.config().routes[i].target.ss_family)
  462. rj["via"] = reinterpret_cast<const InetAddress *>(&(ns.config().routes[i].via))->toIpString(tmp);
  463. else rj["via"] = nlohmann::json();
  464. rj["flags"] = (int)ns.config().routes[i].flags;
  465. rj["metric"] = (int)ns.config().routes[i].metric;
  466. ra.push_back(rj);
  467. }
  468. nj["routes"] = ra;
  469. nlohmann::json mca = nlohmann::json::array();
  470. for(unsigned int i=0;i<ns.config().multicastSubscriptionCount;++i) {
  471. nlohmann::json m;
  472. m["mac"] = MAC(ns.config().multicastSubscriptions[i].mac).toString(tmp);
  473. m["adi"] = ns.config().multicastSubscriptions[i].adi;
  474. mca.push_back(m);
  475. }
  476. nj["multicastSubscriptions"] = mca;
  477. nlohmann::json m;
  478. m["domain"] = ns.config().dns.domain;
  479. m["servers"] = nlohmann::json::array();
  480. for(int j=0;j<ZT_MAX_DNS_SERVERS;++j) {
  481. InetAddress a(ns.config().dns.server_addr[j]);
  482. if (a.isV4() || a.isV6()) {
  483. char buf[256];
  484. m["servers"].push_back(a.toIpString(buf));
  485. }
  486. }
  487. nj["dns"] = m;
  488. if (ns.config().ssoEnabled) {
  489. const char* authURL = ns.config().authenticationURL;
  490. //fprintf(stderr, "Auth URL: %s\n", authURL);
  491. nj["authenticationURL"] = authURL;
  492. nj["authenticationExpiryTime"] = (ns.getExpiryTime()*1000);
  493. nj["ssoEnabled"] = ns.config().ssoEnabled;
  494. }
  495. }
  496. static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer, SharedPtr<Bond> &bond, bool isTunneled)
  497. {
  498. char tmp[256];
  499. const char *prole = "";
  500. switch(peer->role) {
  501. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  502. case ZT_PEER_ROLE_MOON: prole = "MOON"; break;
  503. case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break;
  504. }
  505. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  506. pj["address"] = tmp;
  507. pj["versionMajor"] = peer->versionMajor;
  508. pj["versionMinor"] = peer->versionMinor;
  509. pj["versionRev"] = peer->versionRev;
  510. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev);
  511. pj["version"] = tmp;
  512. pj["latency"] = peer->latency;
  513. pj["role"] = prole;
  514. pj["isBonded"] = peer->isBonded;
  515. pj["tunneled"] = isTunneled;
  516. if (bond && peer->isBonded) {
  517. pj["bondingPolicyCode"] = peer->bondingPolicy;
  518. pj["bondingPolicyStr"] = Bond::getPolicyStrByCode(peer->bondingPolicy);
  519. pj["numAliveLinks"] = peer->numAliveLinks;
  520. pj["numTotalLinks"] = peer->numTotalLinks;
  521. pj["failoverInterval"] = bond->getFailoverInterval();
  522. pj["downDelay"] = bond->getDownDelay();
  523. pj["upDelay"] = bond->getUpDelay();
  524. pj["packetsPerLink"] = bond->getPacketsPerLink();
  525. }
  526. nlohmann::json pa = nlohmann::json::array();
  527. for(unsigned int i=0;i<peer->pathCount;++i) {
  528. int64_t lastSend = peer->paths[i].lastSend;
  529. int64_t lastReceive = peer->paths[i].lastReceive;
  530. nlohmann::json j;
  531. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  532. j["lastSend"] = (lastSend < 0) ? 0 : lastSend;
  533. j["lastReceive"] = (lastReceive < 0) ? 0 : lastReceive;
  534. j["trustedPathId"] = peer->paths[i].trustedPathId;
  535. j["active"] = (bool)(peer->paths[i].expired == 0);
  536. j["expired"] = (bool)(peer->paths[i].expired != 0);
  537. j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  538. j["localSocket"] = peer->paths[i].localSocket;
  539. j["localPort"] = peer->paths[i].localPort;
  540. if (bond && peer->isBonded) {
  541. uint64_t now = OSUtils::now();
  542. j["ifname"] = std::string(peer->paths[i].ifname);
  543. j["latencyMean"] = peer->paths[i].latencyMean;
  544. j["latencyVariance"] = peer->paths[i].latencyVariance;
  545. j["packetLossRatio"] = peer->paths[i].packetLossRatio;
  546. j["packetErrorRatio"] = peer->paths[i].packetErrorRatio;
  547. j["assignedFlowCount"] = peer->paths[i].assignedFlowCount;
  548. j["lastInAge"] = (now - lastReceive);
  549. j["lastOutAge"] = (now - lastSend);
  550. j["bonded"] = peer->paths[i].bonded;
  551. j["eligible"] = peer->paths[i].eligible;
  552. j["givenLinkSpeed"] = peer->paths[i].linkSpeed;
  553. j["relativeQuality"] = peer->paths[i].relativeQuality;
  554. }
  555. pa.push_back(j);
  556. }
  557. pj["paths"] = pa;
  558. }
  559. static void _moonToJson(nlohmann::json &mj,const World &world)
  560. {
  561. char tmp[4096];
  562. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",world.id());
  563. mj["id"] = tmp;
  564. mj["timestamp"] = world.timestamp();
  565. mj["signature"] = Utils::hex(world.signature().data,ZT_C25519_SIGNATURE_LEN,tmp);
  566. mj["updatesMustBeSignedBy"] = Utils::hex(world.updatesMustBeSignedBy().data,ZT_C25519_PUBLIC_KEY_LEN,tmp);
  567. nlohmann::json ra = nlohmann::json::array();
  568. for(std::vector<World::Root>::const_iterator r(world.roots().begin());r!=world.roots().end();++r) {
  569. nlohmann::json rj;
  570. rj["identity"] = r->identity.toString(false,tmp);
  571. nlohmann::json eps = nlohmann::json::array();
  572. for(std::vector<InetAddress>::const_iterator a(r->stableEndpoints.begin());a!=r->stableEndpoints.end();++a)
  573. eps.push_back(a->toString(tmp));
  574. rj["stableEndpoints"] = eps;
  575. ra.push_back(rj);
  576. }
  577. mj["roots"] = ra;
  578. mj["waiting"] = false;
  579. }
  580. class OneServiceImpl;
  581. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf);
  582. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData);
  583. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len);
  584. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen);
  585. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,void *tptr,int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl);
  586. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  587. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr);
  588. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result);
  589. static void StapFrameHandler(void *uptr,void *tptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  590. static int ShttpOnMessageBegin(http_parser *parser);
  591. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  592. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  593. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  594. #else
  595. static int ShttpOnStatus(http_parser *parser);
  596. #endif
  597. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  598. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  599. static int ShttpOnHeadersComplete(http_parser *parser);
  600. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  601. static int ShttpOnMessageComplete(http_parser *parser);
  602. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  603. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  604. ShttpOnMessageBegin,
  605. ShttpOnUrl,
  606. ShttpOnStatus,
  607. ShttpOnHeaderField,
  608. ShttpOnValue,
  609. ShttpOnHeadersComplete,
  610. ShttpOnBody,
  611. ShttpOnMessageComplete
  612. };
  613. #else
  614. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  615. ShttpOnMessageBegin,
  616. ShttpOnUrl,
  617. ShttpOnHeaderField,
  618. ShttpOnValue,
  619. ShttpOnHeadersComplete,
  620. ShttpOnBody,
  621. ShttpOnMessageComplete
  622. };
  623. #endif
  624. /**
  625. * A TCP connection and related state and buffers
  626. */
  627. struct TcpConnection
  628. {
  629. enum {
  630. TCP_UNCATEGORIZED_INCOMING, // uncategorized incoming connection
  631. TCP_HTTP_INCOMING,
  632. TCP_HTTP_OUTGOING,
  633. TCP_TUNNEL_OUTGOING // TUNNELED mode proxy outbound connection
  634. } type;
  635. OneServiceImpl *parent;
  636. PhySocket *sock;
  637. InetAddress remoteAddr;
  638. uint64_t lastReceive;
  639. // Used for inbound HTTP connections
  640. http_parser parser;
  641. unsigned long messageSize;
  642. std::string currentHeaderField;
  643. std::string currentHeaderValue;
  644. std::string url;
  645. std::string status;
  646. std::map< std::string,std::string > headers;
  647. std::string readq;
  648. std::string writeq;
  649. Mutex writeq_m;
  650. };
  651. struct OneServiceIncomingPacket
  652. {
  653. uint64_t now;
  654. int64_t sock;
  655. struct sockaddr_storage from;
  656. unsigned int size;
  657. uint8_t data[ZT_MAX_MTU];
  658. };
  659. class OneServiceImpl : public OneService
  660. {
  661. public:
  662. // begin member variables --------------------------------------------------
  663. const std::string _homePath;
  664. std::string _authToken;
  665. std::string _metricsToken;
  666. std::string _controllerDbPath;
  667. const std::string _networksPath;
  668. const std::string _moonsPath;
  669. EmbeddedNetworkController *_controller;
  670. Phy<OneServiceImpl *> _phy;
  671. Node *_node;
  672. SoftwareUpdater *_updater;
  673. bool _updateAutoApply;
  674. httplib::Server _controlPlane;
  675. httplib::Server _controlPlaneV6;
  676. std::thread _serverThread;
  677. std::thread _serverThreadV6;
  678. bool _serverThreadRunning;
  679. bool _serverThreadRunningV6;
  680. bool _allowTcpFallbackRelay;
  681. bool _forceTcpRelay;
  682. bool _allowSecondaryPort;
  683. bool _enableWebServer;
  684. unsigned int _primaryPort;
  685. unsigned int _secondaryPort;
  686. unsigned int _tertiaryPort;
  687. volatile unsigned int _udpPortPickerCounter;
  688. // Local configuration and memo-ized information from it
  689. json _localConfig;
  690. Hashtable< uint64_t,std::vector<InetAddress> > _v4Hints;
  691. Hashtable< uint64_t,std::vector<InetAddress> > _v6Hints;
  692. Hashtable< uint64_t,std::vector<InetAddress> > _v4Blacklists;
  693. Hashtable< uint64_t,std::vector<InetAddress> > _v6Blacklists;
  694. std::vector< InetAddress > _globalV4Blacklist;
  695. std::vector< InetAddress > _globalV6Blacklist;
  696. std::vector< InetAddress > _allowManagementFrom;
  697. std::vector< std::string > _interfacePrefixBlacklist;
  698. Mutex _localConfig_m;
  699. std::vector<InetAddress> explicitBind;
  700. /*
  701. * To attempt to handle NAT/gateway craziness we use three local UDP ports:
  702. *
  703. * [0] is the normal/default port, usually 9993
  704. * [1] is a port derived from our ZeroTier address
  705. * [2] is a port computed from the normal/default for use with uPnP/NAT-PMP mappings
  706. *
  707. * [2] exists because on some gateways trying to do regular NAT-t interferes
  708. * destructively with uPnP port mapping behavior in very weird buggy ways.
  709. * It's only used if uPnP/NAT-PMP is enabled in this build.
  710. */
  711. unsigned int _ports[3];
  712. Binder _binder;
  713. // Time we last received a packet from a global address
  714. uint64_t _lastDirectReceiveFromGlobal;
  715. #ifdef ZT_TCP_FALLBACK_RELAY
  716. InetAddress _fallbackRelayAddress;
  717. uint64_t _lastSendToGlobalV4;
  718. #endif
  719. // Last potential sleep/wake event
  720. uint64_t _lastRestart;
  721. // Deadline for the next background task service function
  722. volatile int64_t _nextBackgroundTaskDeadline;
  723. std::map<uint64_t,NetworkState> _nets;
  724. Mutex _nets_m;
  725. // Active TCP/IP connections
  726. std::vector< TcpConnection * > _tcpConnections;
  727. Mutex _tcpConnections_m;
  728. TcpConnection *_tcpFallbackTunnel;
  729. // Termination status information
  730. ReasonForTermination _termReason;
  731. std::string _fatalErrorMessage;
  732. Mutex _termReason_m;
  733. // uPnP/NAT-PMP port mapper if enabled
  734. bool _portMappingEnabled; // local.conf settings
  735. #ifdef ZT_USE_MINIUPNPC
  736. PortMapper *_portMapper;
  737. #endif
  738. // HashiCorp Vault Settings
  739. #if ZT_VAULT_SUPPORT
  740. bool _vaultEnabled;
  741. std::string _vaultURL;
  742. std::string _vaultToken;
  743. std::string _vaultPath; // defaults to cubbyhole/zerotier/identity.secret for per-access key storage
  744. #endif
  745. // Set to false to force service to stop
  746. volatile bool _run;
  747. Mutex _run_m;
  748. RedisConfig *_rc;
  749. std::string _ssoRedirectURL;
  750. // end member variables ----------------------------------------------------
  751. OneServiceImpl(const char *hp,unsigned int port) :
  752. _homePath((hp) ? hp : ".")
  753. ,_controllerDbPath(_homePath + ZT_PATH_SEPARATOR_S "controller.d")
  754. ,_networksPath(_homePath + ZT_PATH_SEPARATOR_S "networks.d")
  755. ,_moonsPath(_homePath + ZT_PATH_SEPARATOR_S "moons.d")
  756. ,_controller((EmbeddedNetworkController *)0)
  757. ,_phy(this,false,true)
  758. ,_node((Node *)0)
  759. ,_updater((SoftwareUpdater *)0)
  760. ,_updateAutoApply(false)
  761. ,_controlPlane()
  762. ,_controlPlaneV6()
  763. ,_serverThread()
  764. ,_serverThreadV6()
  765. ,_serverThreadRunning(false)
  766. ,_serverThreadRunningV6(false)
  767. ,_forceTcpRelay(false)
  768. ,_primaryPort(port)
  769. ,_udpPortPickerCounter(0)
  770. ,_lastDirectReceiveFromGlobal(0)
  771. #ifdef ZT_TCP_FALLBACK_RELAY
  772. , _fallbackRelayAddress(ZT_TCP_FALLBACK_RELAY)
  773. ,_lastSendToGlobalV4(0)
  774. #endif
  775. ,_lastRestart(0)
  776. ,_nextBackgroundTaskDeadline(0)
  777. ,_tcpFallbackTunnel((TcpConnection *)0)
  778. ,_termReason(ONE_STILL_RUNNING)
  779. ,_portMappingEnabled(true)
  780. #ifdef ZT_USE_MINIUPNPC
  781. ,_portMapper((PortMapper *)0)
  782. #endif
  783. #ifdef ZT_VAULT_SUPPORT
  784. ,_vaultEnabled(false)
  785. ,_vaultURL()
  786. ,_vaultToken()
  787. ,_vaultPath("cubbyhole/zerotier")
  788. #endif
  789. ,_run(true)
  790. ,_rc(NULL)
  791. ,_ssoRedirectURL()
  792. {
  793. _ports[0] = 0;
  794. _ports[1] = 0;
  795. _ports[2] = 0;
  796. prometheus::simpleapi::saver.set_registry(prometheus::simpleapi::registry_ptr);
  797. prometheus::simpleapi::saver.set_delay(std::chrono::seconds(5));
  798. prometheus::simpleapi::saver.set_out_file(_homePath + ZT_PATH_SEPARATOR + "metrics.prom");
  799. #if ZT_VAULT_SUPPORT
  800. curl_global_init(CURL_GLOBAL_DEFAULT);
  801. #endif
  802. }
  803. virtual ~OneServiceImpl()
  804. {
  805. #ifdef __WINDOWS__
  806. WinFWHelper::removeICMPRules();
  807. #endif
  808. _binder.closeAll(_phy);
  809. #if ZT_VAULT_SUPPORT
  810. curl_global_cleanup();
  811. #endif
  812. _controlPlane.stop();
  813. if (_serverThreadRunning) {
  814. _serverThread.join();
  815. }
  816. _controlPlaneV6.stop();
  817. if (_serverThreadRunningV6) {
  818. _serverThreadV6.join();
  819. }
  820. #ifdef ZT_USE_MINIUPNPC
  821. delete _portMapper;
  822. #endif
  823. delete _controller;
  824. delete _rc;
  825. }
  826. virtual ReasonForTermination run()
  827. {
  828. try {
  829. {
  830. const std::string authTokenPath(_homePath + ZT_PATH_SEPARATOR_S "authtoken.secret");
  831. if (!OSUtils::readFile(authTokenPath.c_str(),_authToken)) {
  832. unsigned char foo[24];
  833. Utils::getSecureRandom(foo,sizeof(foo));
  834. _authToken = "";
  835. for(unsigned int i=0;i<sizeof(foo);++i)
  836. _authToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  837. if (!OSUtils::writeFile(authTokenPath.c_str(),_authToken)) {
  838. Mutex::Lock _l(_termReason_m);
  839. _termReason = ONE_UNRECOVERABLE_ERROR;
  840. _fatalErrorMessage = "authtoken.secret could not be written (try running with -U to prevent dropping of privileges)";
  841. return _termReason;
  842. } else {
  843. OSUtils::lockDownFile(authTokenPath.c_str(),false);
  844. }
  845. }
  846. _authToken = _trimString(_authToken);
  847. }
  848. {
  849. const std::string metricsTokenPath(_homePath + ZT_PATH_SEPARATOR_S "metricstoken.secret");
  850. if (!OSUtils::readFile(metricsTokenPath.c_str(),_metricsToken)) {
  851. unsigned char foo[24];
  852. Utils::getSecureRandom(foo,sizeof(foo));
  853. _metricsToken = "";
  854. for(unsigned int i=0;i<sizeof(foo);++i)
  855. _metricsToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  856. if (!OSUtils::writeFile(metricsTokenPath.c_str(),_metricsToken)) {
  857. Mutex::Lock _l(_termReason_m);
  858. _termReason = ONE_UNRECOVERABLE_ERROR;
  859. _fatalErrorMessage = "metricstoken.secret could not be written (try running with -U to prevent dropping of privileges)";
  860. return _termReason;
  861. } else {
  862. OSUtils::lockDownFile(metricsTokenPath.c_str(),false);
  863. }
  864. }
  865. _metricsToken = _trimString(_metricsToken);
  866. }
  867. {
  868. struct ZT_Node_Callbacks cb;
  869. cb.version = 0;
  870. cb.stateGetFunction = SnodeStateGetFunction;
  871. cb.statePutFunction = SnodeStatePutFunction;
  872. cb.wirePacketSendFunction = SnodeWirePacketSendFunction;
  873. cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction;
  874. cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction;
  875. cb.eventCallback = SnodeEventCallback;
  876. cb.pathCheckFunction = SnodePathCheckFunction;
  877. cb.pathLookupFunction = SnodePathLookupFunction;
  878. _node = new Node(this,(void *)0,&cb,OSUtils::now());
  879. }
  880. // local.conf
  881. readLocalSettings();
  882. applyLocalConfig();
  883. // Save original port number to show it if bind error
  884. const int _configuredPort = _primaryPort;
  885. // Make sure we can use the primary port, and hunt for one if configured to do so
  886. const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random
  887. for(int k=0;k<portTrials;++k) {
  888. if (_primaryPort == 0) {
  889. unsigned int randp = 0;
  890. Utils::getSecureRandom(&randp,sizeof(randp));
  891. _primaryPort = 20000 + (randp % 45500);
  892. }
  893. if (_trialBind(_primaryPort)) {
  894. _ports[0] = _primaryPort;
  895. } else {
  896. _primaryPort = 0;
  897. }
  898. }
  899. if (_ports[0] == 0) {
  900. Mutex::Lock _l(_termReason_m);
  901. _termReason = ONE_UNRECOVERABLE_ERROR;
  902. _fatalErrorMessage = std::string("cannot bind to local control interface port ")+std::to_string(_configuredPort);
  903. return _termReason;
  904. }
  905. // Save primary port to a file so CLIs and GUIs can learn it easily
  906. char portstr[64];
  907. OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]);
  908. OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "zerotier-one.port").c_str(),std::string(portstr));
  909. // Attempt to bind to a secondary port.
  910. // This exists because there are buggy NATs out there that fail if more
  911. // than one device behind the same NAT tries to use the same internal
  912. // private address port number. Buggy NATs are a running theme.
  913. //
  914. // This used to pick the secondary port based on the node ID until we
  915. // discovered another problem: buggy routers and malicious traffic
  916. // "detection". A lot of routers have such things built in these days
  917. // and mis-detect ZeroTier traffic as malicious and block it resulting
  918. // in a node that appears to be in a coma. Secondary ports are now
  919. // randomized on startup.
  920. if (_allowSecondaryPort) {
  921. if (_secondaryPort) {
  922. _ports[1] = _secondaryPort;
  923. } else {
  924. _ports[1] = _getRandomPort();
  925. }
  926. }
  927. #ifdef ZT_USE_MINIUPNPC
  928. if (_portMappingEnabled) {
  929. // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
  930. // use the other two ports for that because some NATs do really funky
  931. // stuff with ports that are explicitly mapped that breaks things.
  932. if (_tertiaryPort) {
  933. _ports[2] = _tertiaryPort;
  934. } else {
  935. _ports[2] = _tertiaryPort = _getRandomPort();
  936. }
  937. if (_ports[2]) {
  938. char uniqueName[64];
  939. OSUtils::ztsnprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
  940. _portMapper = new PortMapper(_ports[2],uniqueName);
  941. }
  942. }
  943. #endif
  944. // Delete legacy iddb.d if present (cleanup)
  945. OSUtils::rmDashRf((_homePath + ZT_PATH_SEPARATOR_S "iddb.d").c_str());
  946. // Network controller is now enabled by default for desktop and server
  947. _controller = new EmbeddedNetworkController(_node,_homePath.c_str(),_controllerDbPath.c_str(),_ports[0], _rc);
  948. if (!_ssoRedirectURL.empty()) {
  949. _controller->setSSORedirectURL(_ssoRedirectURL);
  950. }
  951. _node->setNetconfMaster((void *)_controller);
  952. startHTTPControlPlane();
  953. // Join existing networks in networks.d
  954. {
  955. std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str()));
  956. for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
  957. std::size_t dot = f->find_last_of('.');
  958. if ((dot == 16)&&(f->substr(16) == ".conf"))
  959. _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0,(void *)0);
  960. }
  961. }
  962. // Orbit existing moons in moons.d
  963. {
  964. std::vector<std::string> moonsDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "moons.d").c_str()));
  965. for(std::vector<std::string>::iterator f(moonsDotD.begin());f!=moonsDotD.end();++f) {
  966. std::size_t dot = f->find_last_of('.');
  967. if ((dot == 16)&&(f->substr(16) == ".moon"))
  968. _node->orbit((void *)0,Utils::hexStrToU64(f->substr(0,dot).c_str()),0);
  969. }
  970. }
  971. // Main I/O loop
  972. _nextBackgroundTaskDeadline = 0;
  973. int64_t clockShouldBe = OSUtils::now();
  974. _lastRestart = clockShouldBe;
  975. int64_t lastTapMulticastGroupCheck = 0;
  976. int64_t lastBindRefresh = 0;
  977. int64_t lastUpdateCheck = clockShouldBe;
  978. int64_t lastCleanedPeersDb = 0;
  979. int64_t lastLocalConfFileCheck = OSUtils::now();
  980. int64_t lastOnline = lastLocalConfFileCheck;
  981. for(;;) {
  982. _run_m.lock();
  983. if (!_run) {
  984. _run_m.unlock();
  985. _termReason_m.lock();
  986. _termReason = ONE_NORMAL_TERMINATION;
  987. _termReason_m.unlock();
  988. break;
  989. } else {
  990. _run_m.unlock();
  991. }
  992. const int64_t now = OSUtils::now();
  993. // Attempt to detect sleep/wake events by detecting delay overruns
  994. bool restarted = false;
  995. if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
  996. _lastRestart = now;
  997. restarted = true;
  998. }
  999. // Check for updates (if enabled)
  1000. if ((_updater)&&((now - lastUpdateCheck) > 10000)) {
  1001. lastUpdateCheck = now;
  1002. if (_updater->check(now) && _updateAutoApply)
  1003. _updater->apply();
  1004. }
  1005. // Reload local.conf if anything changed recently
  1006. if ((now - lastLocalConfFileCheck) >= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  1007. lastLocalConfFileCheck = now;
  1008. struct stat result;
  1009. if(stat((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(), &result)==0) {
  1010. int64_t mod_time = result.st_mtime * 1000;
  1011. if ((now - mod_time) <= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  1012. readLocalSettings();
  1013. applyLocalConfig();
  1014. }
  1015. }
  1016. }
  1017. // Refresh bindings in case device's interfaces have changed, and also sync routes to update any shadow routes (e.g. shadow default)
  1018. if (((now - lastBindRefresh) >= (_node->bondController()->inUse() ? ZT_BINDER_REFRESH_PERIOD / 4 : ZT_BINDER_REFRESH_PERIOD))||restarted) {
  1019. // If secondary port is not configured to a constant value and we've been offline for a while,
  1020. // bind a new secondary port. This is a workaround for a "coma" issue caused by buggy NATs that stop
  1021. // working on one port after a while.
  1022. if (_secondaryPort == 0) {
  1023. if (_node->online()) {
  1024. lastOnline = now;
  1025. }
  1026. else if (now - lastOnline > (ZT_PEER_PING_PERIOD * 2) || restarted) {
  1027. lastOnline = now; // don't keep changing the port before we have a chance to connect
  1028. _ports[1] = _getRandomPort();
  1029. #if ZT_DEBUG==1
  1030. fprintf(stderr, "Randomized secondary port. Now it's %d\n", _ports[1]);
  1031. #endif
  1032. }
  1033. }
  1034. unsigned int p[3];
  1035. unsigned int pc = 0;
  1036. for(int i=0;i<3;++i) {
  1037. if (_ports[i])
  1038. p[pc++] = _ports[i];
  1039. }
  1040. if (!_forceTcpRelay) {
  1041. // Only bother binding UDP ports if we aren't forcing TCP-relay mode
  1042. _binder.refresh(_phy,p,pc,explicitBind,*this);
  1043. }
  1044. lastBindRefresh = now;
  1045. // Sync information about physical network interfaces
  1046. _node->clearLocalInterfaceAddresses();
  1047. #ifdef ZT_USE_MINIUPNPC
  1048. if (_portMapper) {
  1049. std::vector<InetAddress> mappedAddresses(_portMapper->get());
  1050. for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
  1051. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
  1052. }
  1053. #endif
  1054. std::vector<InetAddress> boundAddrs(_binder.allBoundLocalInterfaceAddresses());
  1055. for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i) {
  1056. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
  1057. }
  1058. {
  1059. Mutex::Lock _l(_nets_m);
  1060. for(std::map<uint64_t,NetworkState>::iterator n(_nets.begin());n!=_nets.end();++n) {
  1061. if (n->second.tap())
  1062. syncManagedStuff(n->second,false,true,false);
  1063. }
  1064. }
  1065. }
  1066. // Run background task processor in core if it's time to do so
  1067. int64_t dl = _nextBackgroundTaskDeadline;
  1068. if (dl <= now) {
  1069. _node->processBackgroundTasks((void *)0,now,&_nextBackgroundTaskDeadline);
  1070. dl = _nextBackgroundTaskDeadline;
  1071. }
  1072. // Close TCP fallback tunnel if we have direct UDP
  1073. if (!_forceTcpRelay && (_tcpFallbackTunnel) && ((now - _lastDirectReceiveFromGlobal) < (ZT_TCP_FALLBACK_AFTER / 2))) {
  1074. _phy.close(_tcpFallbackTunnel->sock);
  1075. }
  1076. // Sync multicast group memberships
  1077. if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) {
  1078. lastTapMulticastGroupCheck = now;
  1079. std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > > mgChanges;
  1080. {
  1081. Mutex::Lock _l(_nets_m);
  1082. mgChanges.reserve(_nets.size() + 1);
  1083. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  1084. if (n->second.tap()) {
  1085. mgChanges.push_back(std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > >(n->first,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> >()));
  1086. n->second.tap()->scanMulticastGroups(mgChanges.back().second.first,mgChanges.back().second.second);
  1087. }
  1088. }
  1089. }
  1090. for(std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > >::iterator c(mgChanges.begin());c!=mgChanges.end();++c) {
  1091. for(std::vector<MulticastGroup>::iterator m(c->second.first.begin());m!=c->second.first.end();++m)
  1092. _node->multicastSubscribe((void *)0,c->first,m->mac().toInt(),m->adi());
  1093. for(std::vector<MulticastGroup>::iterator m(c->second.second.begin());m!=c->second.second.end();++m)
  1094. _node->multicastUnsubscribe(c->first,m->mac().toInt(),m->adi());
  1095. }
  1096. }
  1097. // Clean peers.d periodically
  1098. if ((now - lastCleanedPeersDb) >= 3600000) {
  1099. lastCleanedPeersDb = now;
  1100. OSUtils::cleanDirectory((_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(),now - 2592000000LL); // delete older than 30 days
  1101. }
  1102. const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 500;
  1103. clockShouldBe = now + (int64_t)delay;
  1104. _phy.poll(delay);
  1105. }
  1106. } catch (std::exception &e) {
  1107. Mutex::Lock _l(_termReason_m);
  1108. _termReason = ONE_UNRECOVERABLE_ERROR;
  1109. _fatalErrorMessage = std::string("unexpected exception in main thread: ")+e.what();
  1110. } catch (int e) {
  1111. Mutex::Lock _l(_termReason_m);
  1112. _termReason = ONE_UNRECOVERABLE_ERROR;
  1113. switch (e) {
  1114. case ZT_EXCEPTION_OUT_OF_BOUNDS: {
  1115. _fatalErrorMessage = "out of bounds exception";
  1116. break;
  1117. }
  1118. case ZT_EXCEPTION_OUT_OF_MEMORY: {
  1119. _fatalErrorMessage = "out of memory";
  1120. break;
  1121. }
  1122. case ZT_EXCEPTION_PRIVATE_KEY_REQUIRED: {
  1123. _fatalErrorMessage = "private key required";
  1124. break;
  1125. }
  1126. case ZT_EXCEPTION_INVALID_ARGUMENT: {
  1127. _fatalErrorMessage = "invalid argument";
  1128. break;
  1129. }
  1130. case ZT_EXCEPTION_INVALID_IDENTITY: {
  1131. _fatalErrorMessage = "invalid identity loaded from disk. Please remove identity.public and identity.secret from " + _homePath + " and try again";
  1132. break;
  1133. }
  1134. case ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE: {
  1135. _fatalErrorMessage = "invalid serialized data: invalid type";
  1136. break;
  1137. }
  1138. case ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW: {
  1139. _fatalErrorMessage = "invalid serialized data: overflow";
  1140. break;
  1141. }
  1142. case ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN: {
  1143. _fatalErrorMessage = "invalid serialized data: invalid cryptographic token";
  1144. break;
  1145. }
  1146. case ZT_EXCEPTION_INVALID_SERIALIZED_DATA_BAD_ENCODING: {
  1147. _fatalErrorMessage = "invalid serialized data: bad encoding";
  1148. break;
  1149. }
  1150. default: {
  1151. _fatalErrorMessage = "unexpected exception code: " + std::to_string(e);
  1152. break;
  1153. }
  1154. }
  1155. } catch ( ... ) {
  1156. Mutex::Lock _l(_termReason_m);
  1157. _termReason = ONE_UNRECOVERABLE_ERROR;
  1158. _fatalErrorMessage = "unexpected exception in main thread: unknown exception";
  1159. }
  1160. try {
  1161. Mutex::Lock _l(_tcpConnections_m);
  1162. while (!_tcpConnections.empty())
  1163. _phy.close((*_tcpConnections.begin())->sock);
  1164. } catch ( ... ) {}
  1165. {
  1166. Mutex::Lock _l(_nets_m);
  1167. _nets.clear();
  1168. }
  1169. delete _updater;
  1170. _updater = (SoftwareUpdater *)0;
  1171. delete _node;
  1172. _node = (Node *)0;
  1173. return _termReason;
  1174. }
  1175. void readLocalSettings()
  1176. {
  1177. // Read local configuration
  1178. std::map<InetAddress,ZT_PhysicalPathConfiguration> ppc;
  1179. // LEGACY: support old "trustedpaths" flat file
  1180. FILE *trustpaths = fopen((_homePath + ZT_PATH_SEPARATOR_S "trustedpaths").c_str(),"r");
  1181. if (trustpaths) {
  1182. fprintf(stderr,"WARNING: 'trustedpaths' flat file format is deprecated in favor of path definitions in local.conf" ZT_EOL_S);
  1183. char buf[1024];
  1184. while (fgets(buf,sizeof(buf),trustpaths)) {
  1185. int fno = 0;
  1186. char *saveptr = (char *)0;
  1187. uint64_t trustedPathId = 0;
  1188. InetAddress trustedPathNetwork;
  1189. for(char *f=Utils::stok(buf,"=\r\n \t",&saveptr);(f);f=Utils::stok((char *)0,"=\r\n \t",&saveptr)) {
  1190. if (fno == 0) {
  1191. trustedPathId = Utils::hexStrToU64(f);
  1192. } else if (fno == 1) {
  1193. trustedPathNetwork = InetAddress(f);
  1194. } else break;
  1195. ++fno;
  1196. }
  1197. if ( (trustedPathId != 0) && ((trustedPathNetwork.ss_family == AF_INET)||(trustedPathNetwork.ss_family == AF_INET6)) && (trustedPathNetwork.netmaskBits() > 0) ) {
  1198. ppc[trustedPathNetwork].trustedPathId = trustedPathId;
  1199. ppc[trustedPathNetwork].mtu = 0; // use default
  1200. }
  1201. }
  1202. fclose(trustpaths);
  1203. }
  1204. // Read local config file
  1205. Mutex::Lock _l2(_localConfig_m);
  1206. std::string lcbuf;
  1207. if (OSUtils::readFile((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(),lcbuf)) {
  1208. if (lcbuf.length() > 0) {
  1209. try {
  1210. _localConfig = OSUtils::jsonParse(lcbuf);
  1211. if (!_localConfig.is_object()) {
  1212. fprintf(stderr,"ERROR: unable to parse local.conf (root element is not a JSON object)" ZT_EOL_S);
  1213. exit(1);
  1214. }
  1215. } catch ( ... ) {
  1216. fprintf(stderr,"ERROR: unable to parse local.conf (invalid JSON)" ZT_EOL_S);
  1217. exit(1);
  1218. }
  1219. }
  1220. }
  1221. // Make a copy so lookups don't modify in place;
  1222. json lc(_localConfig);
  1223. // Get any trusted paths in local.conf (we'll parse the rest of physical[] elsewhere)
  1224. json &physical = lc["physical"];
  1225. if (physical.is_object()) {
  1226. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  1227. InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  1228. if (net) {
  1229. if (phy.value().is_object()) {
  1230. uint64_t tpid;
  1231. if ((tpid = OSUtils::jsonInt(phy.value()["trustedPathId"],0ULL)) != 0ULL) {
  1232. if ((net.ss_family == AF_INET)||(net.ss_family == AF_INET6))
  1233. ppc[net].trustedPathId = tpid;
  1234. }
  1235. ppc[net].mtu = (int)OSUtils::jsonInt(phy.value()["mtu"],0ULL); // 0 means use default
  1236. }
  1237. }
  1238. }
  1239. }
  1240. json &settings = lc["settings"];
  1241. if (settings.is_object()) {
  1242. // Allow controller DB path to be put somewhere else
  1243. const std::string cdbp(OSUtils::jsonString(settings["controllerDbPath"],""));
  1244. if (cdbp.length() > 0)
  1245. _controllerDbPath = cdbp;
  1246. _ssoRedirectURL = OSUtils::jsonString(settings["ssoRedirectURL"], "");
  1247. #ifdef ZT_CONTROLLER_USE_LIBPQ
  1248. // TODO: Redis config
  1249. json &redis = settings["redis"];
  1250. if (redis.is_object() && _rc == NULL) {
  1251. _rc = new RedisConfig;
  1252. _rc->hostname = OSUtils::jsonString(redis["hostname"],"");
  1253. _rc->port = OSUtils::jsonInt(redis["port"],0);
  1254. _rc->password = OSUtils::jsonString(redis["password"],"");
  1255. _rc->clusterMode = OSUtils::jsonBool(redis["clusterMode"], false);
  1256. }
  1257. #endif
  1258. // Bind to wildcard instead of to specific interfaces (disables full tunnel capability)
  1259. json &bind = settings["bind"];
  1260. if (bind.is_array()) {
  1261. for(unsigned long i=0;i<bind.size();++i) {
  1262. const std::string ips(OSUtils::jsonString(bind[i],""));
  1263. if (ips.length() > 0) {
  1264. InetAddress ip(ips.c_str());
  1265. if ((ip.ss_family == AF_INET)||(ip.ss_family == AF_INET6))
  1266. explicitBind.push_back(ip);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. // Set trusted paths if there are any
  1272. if (!ppc.empty()) {
  1273. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::iterator i(ppc.begin());i!=ppc.end();++i)
  1274. _node->setPhysicalPathConfiguration(reinterpret_cast<const struct sockaddr_storage *>(&(i->first)),&(i->second));
  1275. }
  1276. }
  1277. virtual ReasonForTermination reasonForTermination() const
  1278. {
  1279. Mutex::Lock _l(_termReason_m);
  1280. return _termReason;
  1281. }
  1282. virtual std::string fatalErrorMessage() const
  1283. {
  1284. Mutex::Lock _l(_termReason_m);
  1285. return _fatalErrorMessage;
  1286. }
  1287. virtual std::string portDeviceName(uint64_t nwid) const
  1288. {
  1289. Mutex::Lock _l(_nets_m);
  1290. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  1291. if ((n != _nets.end())&&(n->second.tap()))
  1292. return n->second.tap()->deviceName();
  1293. else return std::string();
  1294. }
  1295. #ifdef ZT_SDK
  1296. virtual std::string givenHomePath()
  1297. {
  1298. return _homePath;
  1299. }
  1300. void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes)
  1301. {
  1302. Mutex::Lock _l(_nets_m);
  1303. NetworkState &n = _nets[nwid];
  1304. *numRoutes = *numRoutes < n.config().routeCount ? *numRoutes : n.config().routeCount;
  1305. for(unsigned int i=0; i<*numRoutes; i++) {
  1306. ZT_VirtualNetworkRoute *vnr = (ZT_VirtualNetworkRoute*)routeArray;
  1307. memcpy(&vnr[i], &(n.config().routes[i]), sizeof(ZT_VirtualNetworkRoute));
  1308. }
  1309. }
  1310. virtual Node *getNode()
  1311. {
  1312. return _node;
  1313. }
  1314. #endif // ZT_SDK
  1315. virtual void terminate()
  1316. {
  1317. _run_m.lock();
  1318. _run = false;
  1319. _run_m.unlock();
  1320. _phy.whack();
  1321. }
  1322. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const
  1323. {
  1324. Mutex::Lock _l(_nets_m);
  1325. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  1326. if (n == _nets.end())
  1327. return false;
  1328. settings = n->second.settings();
  1329. return true;
  1330. }
  1331. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings)
  1332. {
  1333. char nlcpath[4096];
  1334. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid);
  1335. FILE *out = fopen(nlcpath,"w");
  1336. if (out) {
  1337. fprintf(out,"allowManaged=%d\n",(int)settings.allowManaged);
  1338. fprintf(out,"allowGlobal=%d\n",(int)settings.allowGlobal);
  1339. fprintf(out,"allowDefault=%d\n",(int)settings.allowDefault);
  1340. fprintf(out,"allowDNS=%d\n",(int)settings.allowDNS);
  1341. fclose(out);
  1342. }
  1343. return true;
  1344. }
  1345. // Internal HTTP Control Plane
  1346. void startHTTPControlPlane() {
  1347. // control plane endpoints
  1348. std::string bondShowPath = "/bond/show/([0-9a-fA-F]{10})";
  1349. std::string bondRotatePath = "/bond/rotate/([0-9a-fA-F]{10})";
  1350. std::string setBondMtuPath = "/bond/setmtu/([0-9]{1,6})/([a-zA-Z0-9_]{1,16})/([0-9a-fA-F\\.\\:]{1,39})";
  1351. std::string configPath = "/config";
  1352. std::string configPostPath = "/config/settings";
  1353. std::string healthPath = "/health";
  1354. std::string moonListPath = "/moon";
  1355. std::string moonPath = "/moon/([0-9a-fA-F]{10})";
  1356. std::string networkListPath = "/network";
  1357. std::string networkPath = "/network/([0-9a-fA-F]{16})";
  1358. std::string peerListPath = "/peer";
  1359. std::string peerPath = "/peer/([0-9a-fA-F]{10})";
  1360. std::string statusPath = "/status";
  1361. std::string metricsPath = "/metrics";
  1362. std::vector<std::string> noAuthEndpoints { "/sso", "/health" };
  1363. auto setContent = [=] (const httplib::Request &req, httplib::Response &res, std::string content) {
  1364. if (req.has_param("jsonp")) {
  1365. if (content.length() > 0) {
  1366. res.set_content(req.get_param_value("jsonp") + "(" + content + ");", "application/javascript");
  1367. } else {
  1368. res.set_content(req.get_param_value("jsonp") + "(null);", "application/javascript");
  1369. }
  1370. } else {
  1371. if (content.length() > 0) {
  1372. res.set_content(content, "application/json");
  1373. } else {
  1374. res.set_content("{}", "application/json");
  1375. }
  1376. }
  1377. };
  1378. //
  1379. // static file server for app ui'
  1380. //
  1381. if (_enableWebServer) {
  1382. static std::string appUiPath = "/app";
  1383. static char appUiDir[16384];
  1384. sprintf(appUiDir,"%s%s",_homePath.c_str(),appUiPath.c_str());
  1385. auto ret = _controlPlane.set_mount_point(appUiPath, appUiDir);
  1386. _controlPlaneV6.set_mount_point(appUiPath, appUiDir);
  1387. if (!ret) {
  1388. fprintf(stderr, "Mounting app directory failed. Creating it. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir);
  1389. if (!OSUtils::mkdir(appUiDir)) {
  1390. fprintf(stderr, "Could not create app directory either. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir);
  1391. } else {
  1392. ret = _controlPlane.set_mount_point(appUiPath, appUiDir);
  1393. _controlPlaneV6.set_mount_point(appUiPath, appUiDir);
  1394. if (!ret) {
  1395. fprintf(stderr, "Really could not create and mount directory. Path: %s - Dir: %s\nWeb apps won't work.\n", appUiPath.c_str(), appUiDir);
  1396. }
  1397. }
  1398. }
  1399. if (ret) {
  1400. // fallback to /index.html for paths that don't exist for SPAs
  1401. auto indexFallbackGet = [](const httplib::Request &req, httplib::Response &res) {
  1402. // fprintf(stderr, "fallback \n");
  1403. auto match = req.matches[1];
  1404. if (match.matched) {
  1405. // fallback
  1406. char indexHtmlPath[16384];
  1407. sprintf(indexHtmlPath,"%s/%s/%s", appUiDir, match.str().c_str(), "index.html");
  1408. // fprintf(stderr, "fallback path %s\n", indexHtmlPath);
  1409. std::string indexHtml;
  1410. if (!OSUtils::readFile(indexHtmlPath, indexHtml)) {
  1411. res.status = 500;
  1412. return;
  1413. }
  1414. res.set_content(indexHtml.c_str(), "text/html");
  1415. } else {
  1416. res.status = 500;
  1417. return;
  1418. }
  1419. };
  1420. auto slashRedirect = [](const httplib::Request &req, httplib::Response &res) {
  1421. // fprintf(stderr, "redirect \n");
  1422. // add .html
  1423. std::string htmlFile;
  1424. char htmlPath[16384];
  1425. sprintf(htmlPath,"%s%s%s", appUiDir, (req.path).substr(appUiPath.length()).c_str(), ".html");
  1426. // fprintf(stderr, "path: %s\n", htmlPath);
  1427. if (OSUtils::readFile(htmlPath, htmlFile)) {
  1428. res.set_content(htmlFile.c_str(), "text/html");
  1429. return;
  1430. } else {
  1431. res.status = 301;
  1432. res.set_header("location", req.path + "/");
  1433. }
  1434. };
  1435. // auto missingAssetGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1436. // fprintf(stderr, "missing \n");
  1437. // res.status = 404;
  1438. // std::string html = "oops";
  1439. // res.set_content(html, "text/plain");
  1440. // res.set_header("Content-Type", "text/plain");
  1441. // return;
  1442. // };
  1443. // auto fix no trailing slash by adding .html or redirecting to path/
  1444. _controlPlane.Get(appUiPath + R"((/[\w|-]+)+$)", slashRedirect);
  1445. _controlPlaneV6.Get(appUiPath + R"((/[\w|-]+)+$)", slashRedirect);
  1446. // // 404 missing assets for *.ext paths
  1447. // s.Get(appUiPath + R"(/\.\w+$)", missingAssetGet);
  1448. // sv6.Get(appUiPath + R"(/\.\w+$)", missingAssetGet);
  1449. // fallback to index.html for unknown paths/files
  1450. _controlPlane.Get(appUiPath + R"((/[\w|-]+)(/[\w|-]+)*/$)", indexFallbackGet);
  1451. _controlPlaneV6.Get(appUiPath + R"((/[\w|-]+)(/[\w|-]+)*/$)", indexFallbackGet);
  1452. }
  1453. }
  1454. auto authCheck = [=] (const httplib::Request &req, httplib::Response &res) {
  1455. if (req.path == "/metrics") {
  1456. if (req.has_header("x-zt1-auth")) {
  1457. std::string token = req.get_header_value("x-zt1-auth");
  1458. if (token == _metricsToken || token == _authToken) {
  1459. return httplib::Server::HandlerResponse::Unhandled;
  1460. }
  1461. } else if (req.has_param("auth")) {
  1462. std::string token = req.get_param_value("auth");
  1463. if (token == _metricsToken || token == _authToken) {
  1464. return httplib::Server::HandlerResponse::Unhandled;
  1465. }
  1466. } else if (req.has_header("authorization")) {
  1467. std::string auth = req.get_header_value("authorization");
  1468. if (bearerTokenValid(auth, _metricsToken) || bearerTokenValid(auth, _authToken)) {
  1469. return httplib::Server::HandlerResponse::Unhandled;
  1470. }
  1471. }
  1472. setContent(req, res, "{}");
  1473. res.status = 401;
  1474. return httplib::Server::HandlerResponse::Handled;
  1475. } else {
  1476. std::string r = req.remote_addr;
  1477. InetAddress remoteAddr(r.c_str());
  1478. bool ipAllowed = false;
  1479. bool isAuth = false;
  1480. // If localhost, allow
  1481. if (remoteAddr.ipScope() == InetAddress::IP_SCOPE_LOOPBACK) {
  1482. ipAllowed = true;
  1483. }
  1484. if (!ipAllowed) {
  1485. for (auto i = _allowManagementFrom.begin(); i != _allowManagementFrom.end(); ++i) {
  1486. if (i->containsAddress(remoteAddr)) {
  1487. ipAllowed = true;
  1488. break;
  1489. }
  1490. }
  1491. }
  1492. if (ipAllowed) {
  1493. // auto-pass endpoints in `noAuthEndpoints`. No auth token required
  1494. if (std::find(noAuthEndpoints.begin(), noAuthEndpoints.end(), req.path) != noAuthEndpoints.end()) {
  1495. isAuth = true;
  1496. }
  1497. // Web Apps base path
  1498. if (req.path.rfind("/app", 0) == 0) { //starts with /app
  1499. isAuth = true;
  1500. }
  1501. if (!isAuth) {
  1502. // check auth token
  1503. if (req.has_header("x-zt1-auth")) {
  1504. std::string token = req.get_header_value("x-zt1-auth");
  1505. if (token == _authToken) {
  1506. isAuth = true;
  1507. }
  1508. } else if (req.has_param("auth")) {
  1509. std::string token = req.get_param_value("auth");
  1510. if (token == _authToken) {
  1511. isAuth = true;
  1512. }
  1513. } else if (req.has_header("authorization")) {
  1514. std::string auth = req.get_header_value("authorization");
  1515. isAuth = bearerTokenValid(auth, _authToken);
  1516. }
  1517. }
  1518. }
  1519. if (ipAllowed && isAuth) {
  1520. return httplib::Server::HandlerResponse::Unhandled;
  1521. }
  1522. setContent(req, res, "{}");
  1523. res.status = 401;
  1524. return httplib::Server::HandlerResponse::Handled;
  1525. }
  1526. };
  1527. auto bondShow = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1528. if (!_node->bondController()->inUse()) {
  1529. setContent(req, res, "");
  1530. res.status = 400;
  1531. return;
  1532. }
  1533. ZT_PeerList *pl = _node->peers();
  1534. if (pl) {
  1535. bool foundBond = false;
  1536. auto id = req.matches[1];
  1537. auto out = json::object();
  1538. uint64_t wantp = Utils::hexStrToU64(id.str().c_str());
  1539. for(unsigned long i=0;i<pl->peerCount;++i) {
  1540. if (pl->peers[i].address == wantp) {
  1541. SharedPtr<Bond> bond = _node->bondController()->getBondByPeerId(wantp);
  1542. if (bond) {
  1543. _peerToJson(out,&(pl->peers[i]),bond,(_tcpFallbackTunnel != (TcpConnection *)0));
  1544. setContent(req, res, out.dump());
  1545. foundBond = true;
  1546. } else {
  1547. setContent(req, res, "");
  1548. res.status = 400;
  1549. }
  1550. break;
  1551. }
  1552. }
  1553. if (!foundBond) {
  1554. setContent(req, res, "");
  1555. res.status = 400;
  1556. }
  1557. }
  1558. _node->freeQueryResult((void *)pl);
  1559. };
  1560. _controlPlane.Get(bondShowPath, bondShow);
  1561. _controlPlaneV6.Get(bondShowPath, bondShow);
  1562. auto bondRotate = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1563. if (!_node->bondController()->inUse()) {
  1564. setContent(req, res, "");
  1565. res.status = 400;
  1566. return;
  1567. }
  1568. auto bondID = req.matches[1];
  1569. uint64_t id = Utils::hexStrToU64(bondID.str().c_str());
  1570. SharedPtr<Bond> bond = _node->bondController()->getBondByPeerId(id);
  1571. if (bond) {
  1572. if (bond->abForciblyRotateLink()) {
  1573. res.status = 200;
  1574. } else {
  1575. res.status = 400;
  1576. }
  1577. } else {
  1578. fprintf(stderr, "unable to find bond to peer %llx\n", (unsigned long long)id);
  1579. res.status = 400;
  1580. }
  1581. setContent(req, res, "{}");
  1582. };
  1583. _controlPlane.Post(bondRotatePath, bondRotate);
  1584. _controlPlane.Put(bondRotatePath, bondRotate);
  1585. _controlPlaneV6.Post(bondRotatePath, bondRotate);
  1586. _controlPlaneV6.Put(bondRotatePath, bondRotate);
  1587. auto setMtu = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1588. if (!_node->bondController()->inUse()) {
  1589. setContent(req, res, "Bonding layer isn't active yet");
  1590. res.status = 400;
  1591. return;
  1592. }
  1593. uint32_t mtu = atoi(req.matches[1].str().c_str());
  1594. if (mtu < 68 || mtu > 65535) {
  1595. setContent(req, res, "Specified MTU is not reasonable");
  1596. res.status = 400;
  1597. return;
  1598. }
  1599. res.status = _node->bondController()->setAllMtuByTuple(mtu, req.matches[2].str().c_str(), req.matches[3].str().c_str()) ? 200 : 400;
  1600. if (res.status == 400) {
  1601. setContent(req, res, "Unable to find specified link");
  1602. return;
  1603. }
  1604. setContent(req, res, "{}");
  1605. };
  1606. _controlPlane.Post(setBondMtuPath, setMtu);
  1607. _controlPlane.Put(setBondMtuPath, setMtu);
  1608. _controlPlaneV6.Post(setBondMtuPath, setMtu);
  1609. _controlPlaneV6.Put(setBondMtuPath, setMtu);
  1610. auto getConfig = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1611. std::string config;
  1612. {
  1613. Mutex::Lock lc(_localConfig_m);
  1614. config = _localConfig.dump();
  1615. }
  1616. if (config == "null") {
  1617. config = "{}";
  1618. }
  1619. setContent(req, res, config);
  1620. };
  1621. _controlPlane.Get(configPath, getConfig);
  1622. _controlPlaneV6.Get(configPath, getConfig);
  1623. auto configPost = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1624. json j(OSUtils::jsonParse(req.body));
  1625. if (j.is_object()) {
  1626. Mutex::Lock lcl(_localConfig_m);
  1627. json lc(_localConfig);
  1628. for(json::const_iterator s(j.begin()); s != j.end(); ++s) {
  1629. lc["settings"][s.key()] = s.value();
  1630. }
  1631. std::string lcStr = OSUtils::jsonDump(lc, 4);
  1632. if (OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(), lcStr)) {
  1633. _localConfig = lc;
  1634. }
  1635. }
  1636. setContent(req, res, "{}");
  1637. };
  1638. _controlPlane.Post(configPostPath, configPost);
  1639. _controlPlane.Put(configPostPath, configPost);
  1640. _controlPlaneV6.Post(configPostPath, configPost);
  1641. _controlPlaneV6.Put(configPostPath, configPost);
  1642. auto healthGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1643. json out = json::object();
  1644. char tmp[256];
  1645. ZT_NodeStatus status;
  1646. _node->status(&status);
  1647. out["online"] = (bool)(status.online != 0);
  1648. out["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  1649. out["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  1650. out["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  1651. out["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  1652. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  1653. out["version"] = tmp;
  1654. out["clock"] = OSUtils::now();
  1655. setContent(req, res, out.dump());
  1656. };
  1657. _controlPlane.Get(healthPath, healthGet);
  1658. _controlPlaneV6.Get(healthPath, healthGet);
  1659. auto moonListGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1660. std::vector<World> moons(_node->moons());
  1661. auto out = json::array();
  1662. for (auto i = moons.begin(); i != moons.end(); ++i) {
  1663. json mj;
  1664. _moonToJson(mj, *i);
  1665. out.push_back(mj);
  1666. }
  1667. setContent(req, res, out.dump());
  1668. };
  1669. _controlPlane.Get(moonListPath, moonListGet);
  1670. _controlPlaneV6.Get(moonListPath, moonListGet);
  1671. auto moonGet = [&, setContent](const httplib::Request &req, httplib::Response &res){
  1672. std::vector<World> moons(_node->moons());
  1673. auto input = req.matches[1];
  1674. auto out = json::object();
  1675. const uint64_t id = Utils::hexStrToU64(input.str().c_str());
  1676. for (auto i = moons.begin(); i != moons.end(); ++i) {
  1677. if (i->id() == id) {
  1678. _moonToJson(out, *i);
  1679. break;
  1680. }
  1681. }
  1682. setContent(req, res, out.dump());
  1683. };
  1684. _controlPlane.Get(moonPath, moonGet);
  1685. _controlPlaneV6.Get(moonPath, moonGet);
  1686. auto moonPost = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1687. auto input = req.matches[1];
  1688. uint64_t seed = 0;
  1689. try {
  1690. json j(OSUtils::jsonParse(req.body));
  1691. if (j.is_object()) {
  1692. seed = Utils::hexStrToU64(OSUtils::jsonString(j["seed"],"0").c_str());
  1693. }
  1694. } catch ( ... ) {
  1695. // discard invalid JSON
  1696. }
  1697. std::vector<World> moons(_node->moons());
  1698. const uint64_t id = Utils::hexStrToU64(input.str().c_str());
  1699. bool found = false;
  1700. auto out = json::object();
  1701. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1702. if (m->id() == id) {
  1703. _moonToJson(out,*m);
  1704. found = true;
  1705. break;
  1706. }
  1707. }
  1708. if (!found && seed != 0) {
  1709. char tmp[64];
  1710. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",id);
  1711. out["id"] = tmp;
  1712. out["roots"] = json::array();
  1713. out["timestamp"] = 0;
  1714. out["signature"] = json();
  1715. out["updatesMustBeSignedBy"] = json();
  1716. out["waiting"] = true;
  1717. _node->orbit((void *)0,id,seed);
  1718. }
  1719. setContent(req, res, out.dump());
  1720. };
  1721. _controlPlane.Post(moonPath, moonPost);
  1722. _controlPlane.Put(moonPath, moonPost);
  1723. _controlPlaneV6.Post(moonPath, moonPost);
  1724. _controlPlaneV6.Put(moonPath, moonPost);
  1725. auto moonDelete = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1726. auto input = req.matches[1];
  1727. uint64_t id = Utils::hexStrToU64(input.str().c_str());
  1728. auto out = json::object();
  1729. _node->deorbit((void*)0,id);
  1730. out["result"] = true;
  1731. setContent(req, res, out.dump());
  1732. };
  1733. _controlPlane.Delete(moonPath, moonDelete);
  1734. auto networkListGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1735. Mutex::Lock _l(_nets_m);
  1736. auto out = json::array();
  1737. for (auto it = _nets.begin(); it != _nets.end(); ++it) {
  1738. NetworkState &ns = it->second;
  1739. json nj;
  1740. _networkToJson(nj, ns);
  1741. out.push_back(nj);
  1742. }
  1743. setContent(req, res, out.dump());
  1744. };
  1745. _controlPlane.Get(networkListPath, networkListGet);
  1746. _controlPlaneV6.Get(networkListPath, networkListGet);
  1747. auto networkGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1748. Mutex::Lock _l(_nets_m);
  1749. auto input = req.matches[1];
  1750. const uint64_t nwid = Utils::hexStrToU64(input.str().c_str());
  1751. if (_nets.find(nwid) != _nets.end()) {
  1752. auto out = json::object();
  1753. NetworkState &ns = _nets[nwid];
  1754. _networkToJson(out, ns);
  1755. setContent(req, res, out.dump());
  1756. return;
  1757. }
  1758. setContent(req, res, "");
  1759. res.status = 404;
  1760. };
  1761. _controlPlane.Get(networkPath, networkGet);
  1762. _controlPlaneV6.Get(networkPath, networkGet);
  1763. auto networkPost = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1764. auto input = req.matches[1];
  1765. uint64_t wantnw = Utils::hexStrToU64(input.str().c_str());
  1766. _node->join(wantnw, (void*)0, (void*)0);
  1767. auto out = json::object();
  1768. Mutex::Lock l(_nets_m);
  1769. bool allowDefault = false;
  1770. if (!_nets.empty()) {
  1771. NetworkState &ns = _nets[wantnw];
  1772. try {
  1773. json j(OSUtils::jsonParse(req.body));
  1774. json &allowManaged = j["allowManaged"];
  1775. if (allowManaged.is_boolean()) {
  1776. ns.setAllowManaged((bool)allowManaged);
  1777. }
  1778. json& allowGlobal = j["allowGlobal"];
  1779. if (allowGlobal.is_boolean()) {
  1780. ns.setAllowGlobal((bool)allowGlobal);
  1781. }
  1782. json& _allowDefault = j["allowDefault"];
  1783. if (_allowDefault.is_boolean()) {
  1784. allowDefault = _allowDefault;
  1785. ns.setAllowDefault((bool)allowDefault);
  1786. }
  1787. json& allowDNS = j["allowDNS"];
  1788. if (allowDNS.is_boolean()) {
  1789. ns.setAllowDNS((bool)allowDNS);
  1790. }
  1791. } catch (...) {
  1792. // discard invalid JSON
  1793. }
  1794. setNetworkSettings(wantnw, ns.settings());
  1795. if (ns.tap()) {
  1796. syncManagedStuff(ns,true,true,true);
  1797. }
  1798. _networkToJson(out, ns);
  1799. }
  1800. #ifdef __FreeBSD__
  1801. if(!!allowDefault){
  1802. res.status = 400;
  1803. setContent(req, res, "Allow Default does not work properly on FreeBSD. See #580");
  1804. } else {
  1805. setContent(req, res, out.dump());
  1806. }
  1807. #else
  1808. setContent(req, res, out.dump());
  1809. #endif
  1810. };
  1811. _controlPlane.Post(networkPath, networkPost);
  1812. _controlPlane.Put(networkPath, networkPost);
  1813. _controlPlaneV6.Post(networkPath, networkPost);
  1814. _controlPlaneV6.Put(networkPath, networkPost);
  1815. auto networkDelete = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1816. auto input = req.matches[1];
  1817. auto out = json::object();
  1818. ZT_VirtualNetworkList *nws = _node->networks();
  1819. uint64_t wantnw = Utils::hexStrToU64(input.str().c_str());
  1820. for(unsigned long i=0; i < nws->networkCount; ++i) {
  1821. if (nws->networks[i].nwid == wantnw) {
  1822. _node->leave(wantnw, (void**)0, (void*)0);
  1823. out["result"] = true;
  1824. }
  1825. }
  1826. _node->freeQueryResult((void*)nws);
  1827. setContent(req, res, out.dump());
  1828. };
  1829. _controlPlane.Delete(networkPath, networkDelete);
  1830. _controlPlaneV6.Delete(networkPath, networkDelete);
  1831. auto peerListGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1832. ZT_PeerList *pl = _node->peers();
  1833. auto out = nlohmann::json::array();
  1834. for(unsigned long i=0;i<pl->peerCount;++i) {
  1835. nlohmann::json pj;
  1836. SharedPtr<Bond> bond = SharedPtr<Bond>();
  1837. if (pl->peers[i].isBonded) {
  1838. const uint64_t id = pl->peers[i].address;
  1839. bond = _node->bondController()->getBondByPeerId(id);
  1840. }
  1841. _peerToJson(pj,&(pl->peers[i]),bond,(_tcpFallbackTunnel != (TcpConnection *)0));
  1842. out.push_back(pj);
  1843. }
  1844. _node->freeQueryResult((void*)pl);
  1845. setContent(req, res, out.dump());
  1846. };
  1847. _controlPlane.Get(peerListPath, peerListGet);
  1848. _controlPlaneV6.Get(peerListPath, peerListGet);
  1849. auto peerGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1850. ZT_PeerList *pl = _node->peers();
  1851. auto input = req.matches[1];
  1852. uint64_t wantp = Utils::hexStrToU64(input.str().c_str());
  1853. auto out = json::object();
  1854. for(unsigned long i=0;i<pl->peerCount;++i) {
  1855. if (pl->peers[i].address == wantp) {
  1856. SharedPtr<Bond> bond = SharedPtr<Bond>();
  1857. if (pl->peers[i].isBonded) {
  1858. bond = _node->bondController()->getBondByPeerId(wantp);
  1859. }
  1860. _peerToJson(out,&(pl->peers[i]),bond,(_tcpFallbackTunnel != (TcpConnection *)0));
  1861. break;
  1862. }
  1863. }
  1864. _node->freeQueryResult((void*)pl);
  1865. setContent(req, res, out.dump());
  1866. };
  1867. _controlPlane.Get(peerPath, peerGet);
  1868. _controlPlaneV6.Get(peerPath, peerGet);
  1869. auto statusGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
  1870. ZT_NodeStatus status;
  1871. _node->status(&status);
  1872. auto out = json::object();
  1873. char tmp[256] = {};
  1874. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",status.address);
  1875. out["address"] = tmp;
  1876. out["publicIdentity"] = status.publicIdentity;
  1877. out["online"] = (bool)(status.online != 0);
  1878. out["tcpFallbackActive"] = (_tcpFallbackTunnel != (TcpConnection *)0);
  1879. out["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  1880. out["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  1881. out["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  1882. out["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  1883. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  1884. out["version"] = tmp;
  1885. out["clock"] = OSUtils::now();
  1886. {
  1887. Mutex::Lock _l(_localConfig_m);
  1888. out["config"] = _localConfig;
  1889. }
  1890. json &settings = out["config"]["settings"];
  1891. settings["allowTcpFallbackRelay"] = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],_allowTcpFallbackRelay);
  1892. settings["forceTcpRelay"] = OSUtils::jsonBool(settings["forceTcpRelay"],_forceTcpRelay);
  1893. settings["primaryPort"] = OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1894. settings["secondaryPort"] = OSUtils::jsonInt(settings["secondaryPort"],(uint64_t)_ports[1]) & 0xffff;
  1895. settings["tertiaryPort"] = OSUtils::jsonInt(settings["tertiaryPort"],(uint64_t)_tertiaryPort) & 0xffff;
  1896. settings["homeDir"] = _homePath;
  1897. // Enumerate all local address/port pairs that this node is listening on
  1898. std::vector<InetAddress> boundAddrs(_binder.allBoundLocalInterfaceAddresses());
  1899. auto boundAddrArray = json::array();
  1900. for (int i = 0; i < boundAddrs.size(); i++) {
  1901. char ipBuf[64] = { 0 };
  1902. boundAddrs[i].toString(ipBuf);
  1903. boundAddrArray.push_back(ipBuf);
  1904. }
  1905. settings["listeningOn"] = boundAddrArray;
  1906. // Enumerate all external address/port pairs that are reported for this node
  1907. std::vector<InetAddress> surfaceAddrs = _node->SurfaceAddresses();
  1908. auto surfaceAddrArray = json::array();
  1909. for (int i = 0; i < surfaceAddrs.size(); i++) {
  1910. char ipBuf[64] = { 0 };
  1911. surfaceAddrs[i].toString(ipBuf);
  1912. surfaceAddrArray.push_back(ipBuf);
  1913. }
  1914. settings["surfaceAddresses"] = surfaceAddrArray;
  1915. #ifdef ZT_USE_MINIUPNPC
  1916. settings["portMappingEnabled"] = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1917. #else
  1918. settings["portMappingEnabled"] = false; // not supported in build
  1919. #endif
  1920. #ifndef ZT_SDK
  1921. settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
  1922. settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
  1923. #endif
  1924. const World planet(_node->planet());
  1925. out["planetWorldId"] = planet.id();
  1926. out["planetWorldTimestamp"] = planet.timestamp();
  1927. setContent(req, res, out.dump());
  1928. };
  1929. _controlPlane.Get(statusPath, statusGet);
  1930. _controlPlaneV6.Get(statusPath, statusGet);
  1931. #if ZT_SSO_ENABLED
  1932. std::string ssoPath = "/sso";
  1933. auto ssoGet = [this](const httplib::Request &req, httplib::Response &res) {
  1934. std::string htmlTemplatePath = _homePath + ZT_PATH_SEPARATOR + "sso-auth.template.html";
  1935. std::string htmlTemplate;
  1936. if (!OSUtils::readFile(htmlTemplatePath.c_str(), htmlTemplate)) {
  1937. htmlTemplate = ssoResponseTemplate;
  1938. }
  1939. std::string responseContentType = "text/html";
  1940. std::string responseBody = "";
  1941. json outData;
  1942. if (req.has_param("error")) {
  1943. std::string error = req.get_param_value("error");
  1944. std::string desc = req.get_param_value("error_description");
  1945. json data;
  1946. outData["isError"] = true;
  1947. outData["messageText"] = (std::string("ERROR ") + error + std::string(": ") + desc);
  1948. responseBody = inja::render(htmlTemplate, outData);
  1949. res.set_content(responseBody, responseContentType);
  1950. res.status = 500;
  1951. return;
  1952. }
  1953. // SSO redirect handling
  1954. std::string state = req.get_param_value("state");
  1955. char* nwid = zeroidc::zeroidc_network_id_from_state(state.c_str());
  1956. outData["networkId"] = std::string(nwid);
  1957. const uint64_t id = Utils::hexStrToU64(nwid);
  1958. zeroidc::free_cstr(nwid);
  1959. Mutex::Lock l(_nets_m);
  1960. if (_nets.find(id) != _nets.end()) {
  1961. NetworkState& ns = _nets[id];
  1962. std::string code = req.get_param_value("code");
  1963. char *ret = ns.doTokenExchange(code.c_str());
  1964. json ssoResult = json::parse(ret);
  1965. if (ssoResult.is_object()) {
  1966. if (ssoResult.contains("errorMessage")) {
  1967. outData["isError"] = true;
  1968. outData["messageText"] = ssoResult["errorMessage"];
  1969. responseBody = inja::render(htmlTemplate, outData);
  1970. res.set_content(responseBody, responseContentType);
  1971. res.status = 500;
  1972. } else {
  1973. outData["isError"] = false;
  1974. outData["messageText"] = "Authentication Successful. You may now access the network.";
  1975. responseBody = inja::render(htmlTemplate, outData);
  1976. res.set_content(responseBody, responseContentType);
  1977. }
  1978. } else {
  1979. // not an object? We got a problem
  1980. outData["isError"] = true;
  1981. outData["messageText"] = "ERROR: Unkown SSO response. Please contact your administrator.";
  1982. responseBody = inja::render(htmlTemplate, outData);
  1983. res.set_content(responseBody, responseContentType);
  1984. res.status = 500;
  1985. }
  1986. zeroidc::free_cstr(ret);
  1987. }
  1988. };
  1989. _controlPlane.Get(ssoPath, ssoGet);
  1990. _controlPlaneV6.Get(ssoPath, ssoGet);
  1991. #endif
  1992. auto metricsGet = [this](const httplib::Request &req, httplib::Response &res) {
  1993. std::string statspath = _homePath + ZT_PATH_SEPARATOR + "metrics.prom";
  1994. std::string metrics;
  1995. if (OSUtils::readFile(statspath.c_str(), metrics)) {
  1996. res.set_content(metrics, "text/plain");
  1997. } else {
  1998. res.set_content("{}", "application/json");
  1999. res.status = 500;
  2000. }
  2001. };
  2002. _controlPlane.Get(metricsPath, metricsGet);
  2003. _controlPlaneV6.Get(metricsPath, metricsGet);
  2004. auto exceptionHandler = [&, setContent](const httplib::Request &req, httplib::Response &res, std::exception_ptr ep) {
  2005. char buf[1024];
  2006. auto fmt = "{\"error\": %d, \"description\": \"%s\"}";
  2007. try {
  2008. std::rethrow_exception(ep);
  2009. } catch (std::exception &e) {
  2010. snprintf(buf, sizeof(buf), fmt, 500, e.what());
  2011. } catch (...) {
  2012. snprintf(buf, sizeof(buf), fmt, 500, "Unknown Exception");
  2013. }
  2014. setContent(req, res, buf);
  2015. res.status = 500;
  2016. };
  2017. _controlPlane.set_exception_handler(exceptionHandler);
  2018. _controlPlaneV6.set_exception_handler(exceptionHandler);
  2019. if (_controller) {
  2020. _controller->configureHTTPControlPlane(_controlPlane, _controlPlaneV6, setContent);
  2021. }
  2022. _controlPlane.set_pre_routing_handler(authCheck);
  2023. _controlPlaneV6.set_pre_routing_handler(authCheck);
  2024. #if ZT_DEBUG==1
  2025. _controlPlane.set_logger([](const httplib::Request &req, const httplib::Response &res) {
  2026. fprintf(stderr, "%s", http_log(req, res).c_str());
  2027. });
  2028. _controlPlaneV6.set_logger([](const httplib::Request &req, const httplib::Response &res) {
  2029. fprintf(stderr, "%s", http_log(req, res).c_str());
  2030. });
  2031. #endif
  2032. if (_primaryPort==0) {
  2033. fprintf(stderr, "unable to determine local control port");
  2034. exit(-1);
  2035. }
  2036. bool v4controlPlaneBound = false;
  2037. _controlPlane.set_address_family(AF_INET);
  2038. if(_controlPlane.bind_to_port("0.0.0.0", _primaryPort)) {
  2039. _serverThread = std::thread([&] {
  2040. _serverThreadRunning = true;
  2041. fprintf(stderr, "Starting Control Plane...\n");
  2042. if(!_controlPlane.listen_after_bind()) {
  2043. fprintf(stderr, "Error on listen_after_bind()\n");
  2044. }
  2045. fprintf(stderr, "Control Plane Stopped\n");
  2046. _serverThreadRunning = false;
  2047. });
  2048. v4controlPlaneBound = true;
  2049. } else {
  2050. fprintf(stderr, "Error binding control plane to 0.0.0.0:%d\n", _primaryPort);
  2051. v4controlPlaneBound = false;
  2052. }
  2053. bool v6controlPlaneBound = false;
  2054. _controlPlaneV6.set_address_family(AF_INET6);
  2055. if(_controlPlaneV6.bind_to_port("::", _primaryPort)) {
  2056. _serverThreadV6 = std::thread([&] {
  2057. _serverThreadRunningV6 = true;
  2058. fprintf(stderr, "Starting V6 Control Plane...\n");
  2059. if(!_controlPlaneV6.listen_after_bind()) {
  2060. fprintf(stderr, "Error on V6 listen_after_bind()\n");
  2061. }
  2062. fprintf(stderr, "V6 Control Plane Stopped\n");
  2063. _serverThreadRunningV6 = false;
  2064. });
  2065. v6controlPlaneBound = true;
  2066. } else {
  2067. fprintf(stderr, "Error binding control plane to [::]:%d\n", _primaryPort);
  2068. v6controlPlaneBound = false;
  2069. }
  2070. if (!v4controlPlaneBound && !v6controlPlaneBound) {
  2071. fprintf(stderr, "ERROR: Could not bind control plane. Exiting...\n");
  2072. exit(-1);
  2073. }
  2074. }
  2075. // Must be called after _localConfig is read or modified
  2076. void applyLocalConfig()
  2077. {
  2078. Mutex::Lock _l(_localConfig_m);
  2079. json lc(_localConfig);
  2080. _v4Hints.clear();
  2081. _v6Hints.clear();
  2082. _v4Blacklists.clear();
  2083. _v6Blacklists.clear();
  2084. json &virt = lc["virtual"];
  2085. if (virt.is_object()) {
  2086. for(json::iterator v(virt.begin());v!=virt.end();++v) {
  2087. const std::string nstr = v.key();
  2088. if ((nstr.length() == ZT_ADDRESS_LENGTH_HEX)&&(v.value().is_object())) {
  2089. const Address ztaddr(Utils::hexStrToU64(nstr.c_str()));
  2090. if (ztaddr) {
  2091. const uint64_t ztaddr2 = ztaddr.toInt();
  2092. std::vector<InetAddress> &v4h = _v4Hints[ztaddr2];
  2093. std::vector<InetAddress> &v6h = _v6Hints[ztaddr2];
  2094. std::vector<InetAddress> &v4b = _v4Blacklists[ztaddr2];
  2095. std::vector<InetAddress> &v6b = _v6Blacklists[ztaddr2];
  2096. json &tryAddrs = v.value()["try"];
  2097. if (tryAddrs.is_array()) {
  2098. for(unsigned long i=0;i<tryAddrs.size();++i) {
  2099. const InetAddress ip(OSUtils::jsonString(tryAddrs[i],"").c_str());
  2100. if (ip.ss_family == AF_INET)
  2101. v4h.push_back(ip);
  2102. else if (ip.ss_family == AF_INET6)
  2103. v6h.push_back(ip);
  2104. }
  2105. }
  2106. json &blAddrs = v.value()["blacklist"];
  2107. if (blAddrs.is_array()) {
  2108. for(unsigned long i=0;i<blAddrs.size();++i) {
  2109. const InetAddress ip(OSUtils::jsonString(blAddrs[i],"").c_str());
  2110. if (ip.ss_family == AF_INET)
  2111. v4b.push_back(ip);
  2112. else if (ip.ss_family == AF_INET6)
  2113. v6b.push_back(ip);
  2114. }
  2115. }
  2116. if (v4h.empty()) _v4Hints.erase(ztaddr2);
  2117. if (v6h.empty()) _v6Hints.erase(ztaddr2);
  2118. if (v4b.empty()) _v4Blacklists.erase(ztaddr2);
  2119. if (v6b.empty()) _v6Blacklists.erase(ztaddr2);
  2120. }
  2121. }
  2122. }
  2123. }
  2124. _globalV4Blacklist.clear();
  2125. _globalV6Blacklist.clear();
  2126. json &physical = lc["physical"];
  2127. if (physical.is_object()) {
  2128. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  2129. const InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  2130. if ((net)&&(net.netmaskBits() > 0)) {
  2131. if (phy.value().is_object()) {
  2132. if (OSUtils::jsonBool(phy.value()["blacklist"],false)) {
  2133. if (net.ss_family == AF_INET)
  2134. _globalV4Blacklist.push_back(net);
  2135. else if (net.ss_family == AF_INET6)
  2136. _globalV6Blacklist.push_back(net);
  2137. }
  2138. }
  2139. }
  2140. }
  2141. }
  2142. _allowManagementFrom.clear();
  2143. _interfacePrefixBlacklist.clear();
  2144. json &settings = lc["settings"];
  2145. if (!_node->bondController()->inUse()) {
  2146. _node->bondController()->setBinder(&_binder);
  2147. // defaultBondingPolicy
  2148. std::string defaultBondingPolicyStr(OSUtils::jsonString(settings["defaultBondingPolicy"],""));
  2149. int defaultBondingPolicy = _node->bondController()->getPolicyCodeByStr(defaultBondingPolicyStr);
  2150. _node->bondController()->setBondingLayerDefaultPolicy(defaultBondingPolicy);
  2151. _node->bondController()->setBondingLayerDefaultPolicyStr(defaultBondingPolicyStr); // Used if custom policy
  2152. // Custom Policies
  2153. json &customBondingPolicies = settings["policies"];
  2154. for (json::iterator policyItr = customBondingPolicies.begin(); policyItr != customBondingPolicies.end();++policyItr) {
  2155. // Custom Policy
  2156. std::string customPolicyStr(policyItr.key());
  2157. json &customPolicy = policyItr.value();
  2158. std::string basePolicyStr(OSUtils::jsonString(customPolicy["basePolicy"],""));
  2159. if (basePolicyStr.empty()) {
  2160. fprintf(stderr, "error: no base policy was specified for custom policy (%s)\n", customPolicyStr.c_str());
  2161. }
  2162. int basePolicyCode = _node->bondController()->getPolicyCodeByStr(basePolicyStr);
  2163. if (basePolicyCode == ZT_BOND_POLICY_NONE) {
  2164. fprintf(stderr, "error: custom policy (%s) is invalid, unknown base policy (%s).\n",
  2165. customPolicyStr.c_str(), basePolicyStr.c_str());
  2166. continue;
  2167. } if (_node->bondController()->getPolicyCodeByStr(customPolicyStr) != ZT_BOND_POLICY_NONE) {
  2168. fprintf(stderr, "error: custom policy (%s) will be ignored, cannot use standard policy names for custom policies.\n",
  2169. customPolicyStr.c_str());
  2170. continue;
  2171. }
  2172. // New bond, used as a copy template for new instances
  2173. SharedPtr<Bond> newTemplateBond = new Bond(NULL, basePolicyStr, customPolicyStr, SharedPtr<Peer>());
  2174. newTemplateBond->setPolicy(basePolicyCode);
  2175. // Custom link quality spec
  2176. json &linkQualitySpec = customPolicy["linkQuality"];
  2177. if (linkQualitySpec.size() == ZT_QOS_PARAMETER_SIZE) {
  2178. float weights[ZT_QOS_PARAMETER_SIZE] = {};
  2179. weights[ZT_QOS_LAT_MAX_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["lat_max"],0.0);
  2180. weights[ZT_QOS_PDV_MAX_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["pdv_max"],0.0);
  2181. weights[ZT_QOS_PLR_MAX_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["plr_max"],0.0);
  2182. weights[ZT_QOS_PER_MAX_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["per_max"],0.0);
  2183. weights[ZT_QOS_LAT_WEIGHT_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["lat_weight"],0.0);
  2184. weights[ZT_QOS_PDV_WEIGHT_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["pdv_weight"],0.0);
  2185. weights[ZT_QOS_PLR_WEIGHT_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["plr_weight"],0.0);
  2186. weights[ZT_QOS_PER_WEIGHT_IDX] = (float)OSUtils::jsonDouble(linkQualitySpec["per_weight"],0.0);
  2187. newTemplateBond->setUserLinkQualitySpec(weights,ZT_QOS_PARAMETER_SIZE);
  2188. }
  2189. // Bond-specific properties
  2190. newTemplateBond->setUpDelay(OSUtils::jsonInt(customPolicy["upDelay"],-1));
  2191. newTemplateBond->setDownDelay(OSUtils::jsonInt(customPolicy["downDelay"],-1));
  2192. newTemplateBond->setFailoverInterval(OSUtils::jsonInt(customPolicy["failoverInterval"],ZT_BOND_FAILOVER_DEFAULT_INTERVAL));
  2193. newTemplateBond->setPacketsPerLink(OSUtils::jsonInt(customPolicy["packetsPerLink"],-1));
  2194. // Policy-Specific link set
  2195. json &links = customPolicy["links"];
  2196. for (json::iterator linkItr = links.begin(); linkItr != links.end();++linkItr) {
  2197. std::string linkNameStr(linkItr.key());
  2198. json &link = linkItr.value();
  2199. bool enabled = OSUtils::jsonInt(link["enabled"],true);
  2200. uint32_t capacity = OSUtils::jsonInt(link["capacity"],0);
  2201. uint8_t ipvPref = OSUtils::jsonInt(link["ipvPref"],0);
  2202. uint16_t mtu = OSUtils::jsonInt(link["mtu"],0);
  2203. std::string failoverToStr(OSUtils::jsonString(link["failoverTo"],""));
  2204. // Mode
  2205. std::string linkModeStr(OSUtils::jsonString(link["mode"],"spare"));
  2206. uint8_t linkMode = ZT_BOND_SLAVE_MODE_SPARE;
  2207. if (linkModeStr == "primary") { linkMode = ZT_BOND_SLAVE_MODE_PRIMARY; }
  2208. if (linkModeStr == "spare") { linkMode = ZT_BOND_SLAVE_MODE_SPARE; }
  2209. // ipvPref
  2210. if ((ipvPref != 0) && (ipvPref != 4) && (ipvPref != 6) && (ipvPref != 46) && (ipvPref != 64)) {
  2211. fprintf(stderr, "error: invalid ipvPref value (%d), link disabled.\n", ipvPref);
  2212. enabled = false;
  2213. }
  2214. if (linkMode == ZT_BOND_SLAVE_MODE_SPARE && failoverToStr.length()) {
  2215. fprintf(stderr, "error: cannot specify failover links for spares, link disabled.\n");
  2216. failoverToStr = "";
  2217. enabled = false;
  2218. }
  2219. _node->bondController()->addCustomLink(customPolicyStr, new Link(linkNameStr,ipvPref,mtu,capacity,enabled,linkMode,failoverToStr));
  2220. }
  2221. std::string linkSelectMethodStr(OSUtils::jsonString(customPolicy["activeReselect"],"optimize"));
  2222. if (linkSelectMethodStr == "always") {
  2223. newTemplateBond->setLinkSelectMethod(ZT_BOND_RESELECTION_POLICY_ALWAYS);
  2224. }
  2225. if (linkSelectMethodStr == "better") {
  2226. newTemplateBond->setLinkSelectMethod(ZT_BOND_RESELECTION_POLICY_BETTER);
  2227. }
  2228. if (linkSelectMethodStr == "failure") {
  2229. newTemplateBond->setLinkSelectMethod(ZT_BOND_RESELECTION_POLICY_FAILURE);
  2230. }
  2231. if (linkSelectMethodStr == "optimize") {
  2232. newTemplateBond->setLinkSelectMethod(ZT_BOND_RESELECTION_POLICY_OPTIMIZE);
  2233. }
  2234. if (newTemplateBond->getLinkSelectMethod() < 0 || newTemplateBond->getLinkSelectMethod() > 3) {
  2235. fprintf(stderr, "warning: invalid value (%s) for linkSelectMethod, assuming mode: always\n", linkSelectMethodStr.c_str());
  2236. }
  2237. if (!_node->bondController()->addCustomPolicy(newTemplateBond)) {
  2238. fprintf(stderr, "error: a custom policy of this name (%s) already exists.\n", customPolicyStr.c_str());
  2239. }
  2240. }
  2241. // Peer-specific bonding
  2242. json &peerSpecificBonds = settings["peerSpecificBonds"];
  2243. for (json::iterator peerItr = peerSpecificBonds.begin(); peerItr != peerSpecificBonds.end();++peerItr) {
  2244. _node->bondController()->assignBondingPolicyToPeer(std::stoull(peerItr.key(),0,16), peerItr.value());
  2245. }
  2246. // Check settings
  2247. if (defaultBondingPolicyStr.length() && !defaultBondingPolicy && !_node->bondController()->inUse()) {
  2248. fprintf(stderr, "error: unknown policy (%s) specified by defaultBondingPolicy, bond disabled.\n", defaultBondingPolicyStr.c_str());
  2249. }
  2250. }
  2251. // bondingPolicy cannot be used with allowTcpFallbackRelay
  2252. bool _forceTcpRelayTmp = (OSUtils::jsonBool(settings["forceTcpRelay"],false));
  2253. bool _bondInUse = _node->bondController()->inUse();
  2254. if (_forceTcpRelayTmp && _bondInUse) {
  2255. fprintf(stderr, "Warning: forceTcpRelay cannot be used with multipath. Disabling forceTcpRelay\n");
  2256. }
  2257. _allowTcpFallbackRelay = (OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true) && !_node->bondController()->inUse());
  2258. _forceTcpRelay = (_forceTcpRelayTmp && !_node->bondController()->inUse());
  2259. _enableWebServer = (OSUtils::jsonBool(settings["enableWebServer"],false));
  2260. #ifdef ZT_TCP_FALLBACK_RELAY
  2261. _fallbackRelayAddress = InetAddress(OSUtils::jsonString(settings["tcpFallbackRelay"], ZT_TCP_FALLBACK_RELAY).c_str());
  2262. #endif
  2263. _primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  2264. _allowSecondaryPort = OSUtils::jsonBool(settings["allowSecondaryPort"],true);
  2265. _secondaryPort = (unsigned int)OSUtils::jsonInt(settings["secondaryPort"],0);
  2266. _tertiaryPort = (unsigned int)OSUtils::jsonInt(settings["tertiaryPort"],0);
  2267. if (_secondaryPort != 0 || _tertiaryPort != 0) {
  2268. fprintf(stderr,"WARNING: using manually-specified secondary and/or tertiary ports. This can cause NAT issues." ZT_EOL_S);
  2269. }
  2270. _portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  2271. _node->setLowBandwidthMode(OSUtils::jsonBool(settings["lowBandwidthMode"],false));
  2272. #ifndef ZT_SDK
  2273. const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
  2274. const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
  2275. if (((up == "apply")||(up == "download"))||(udist)) {
  2276. if (!_updater)
  2277. _updater = new SoftwareUpdater(*_node,_homePath);
  2278. _updateAutoApply = (up == "apply");
  2279. _updater->setUpdateDistribution(udist);
  2280. _updater->setChannel(OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL));
  2281. } else {
  2282. delete _updater;
  2283. _updater = (SoftwareUpdater *)0;
  2284. _updateAutoApply = false;
  2285. }
  2286. #endif
  2287. json &ignoreIfs = settings["interfacePrefixBlacklist"];
  2288. if (ignoreIfs.is_array()) {
  2289. for(unsigned long i=0;i<ignoreIfs.size();++i) {
  2290. const std::string tmp(OSUtils::jsonString(ignoreIfs[i],""));
  2291. if (tmp.length() > 0)
  2292. _interfacePrefixBlacklist.push_back(tmp);
  2293. }
  2294. }
  2295. json &amf = settings["allowManagementFrom"];
  2296. if (amf.is_array()) {
  2297. for(unsigned long i=0;i<amf.size();++i) {
  2298. const InetAddress nw(OSUtils::jsonString(amf[i],"").c_str());
  2299. if (nw)
  2300. _allowManagementFrom.push_back(nw);
  2301. }
  2302. }
  2303. }
  2304. #if ZT_VAULT_SUPPORT
  2305. json &vault = settings["vault"];
  2306. if (vault.is_object()) {
  2307. const std::string url(OSUtils::jsonString(vault["vaultURL"], "").c_str());
  2308. if (!url.empty()) {
  2309. _vaultURL = url;
  2310. }
  2311. const std::string token(OSUtils::jsonString(vault["vaultToken"], "").c_str());
  2312. if (!token.empty()) {
  2313. _vaultToken = token;
  2314. }
  2315. const std::string path(OSUtils::jsonString(vault["vaultPath"], "").c_str());
  2316. if (!path.empty()) {
  2317. _vaultPath = path;
  2318. }
  2319. }
  2320. // also check environment variables for values. Environment variables
  2321. // will override local.conf variables
  2322. const std::string envURL(getenv("VAULT_ADDR"));
  2323. if (!envURL.empty()) {
  2324. _vaultURL = envURL;
  2325. }
  2326. const std::string envToken(getenv("VAULT_TOKEN"));
  2327. if (!envToken.empty()) {
  2328. _vaultToken = envToken;
  2329. }
  2330. const std::string envPath(getenv("VAULT_PATH"));
  2331. if (!envPath.empty()) {
  2332. _vaultPath = envPath;
  2333. }
  2334. if (!_vaultURL.empty() && !_vaultToken.empty()) {
  2335. _vaultEnabled = true;
  2336. }
  2337. #endif
  2338. // Checks if a managed IP or route target is allowed
  2339. bool checkIfManagedIsAllowed(const NetworkState &n,const InetAddress &target)
  2340. {
  2341. if (!n.allowManaged())
  2342. return false;
  2343. if (!n.allowManagedWhitelist().empty()) {
  2344. bool allowed = false;
  2345. for (InetAddress addr : n.allowManagedWhitelist()) {
  2346. if (addr.containsAddress(target) && addr.netmaskBits() <= target.netmaskBits()) {
  2347. allowed = true;
  2348. break;
  2349. }
  2350. }
  2351. if (!allowed) return false;
  2352. }
  2353. if (target.isDefaultRoute())
  2354. return n.allowDefault();
  2355. switch(target.ipScope()) {
  2356. case InetAddress::IP_SCOPE_NONE:
  2357. case InetAddress::IP_SCOPE_MULTICAST:
  2358. case InetAddress::IP_SCOPE_LOOPBACK:
  2359. case InetAddress::IP_SCOPE_LINK_LOCAL:
  2360. return false;
  2361. case InetAddress::IP_SCOPE_GLOBAL:
  2362. return n.allowGlobal();
  2363. default:
  2364. return true;
  2365. }
  2366. }
  2367. // Match only an IP from a vector of IPs -- used in syncManagedStuff()
  2368. inline bool matchIpOnly(const std::set<InetAddress> &ips,const InetAddress &ip) const
  2369. {
  2370. for(std::set<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  2371. if (i->ipsEqual(ip))
  2372. return true;
  2373. }
  2374. return false;
  2375. }
  2376. // Apply or update managed IPs for a configured network (be sure n.tap exists)
  2377. void syncManagedStuff(NetworkState &n,bool syncIps,bool syncRoutes, bool syncDns)
  2378. {
  2379. char ipbuf[64];
  2380. // assumes _nets_m is locked
  2381. if (syncIps) {
  2382. std::vector<InetAddress> newManagedIps;
  2383. newManagedIps.reserve(n.config().assignedAddressCount);
  2384. #ifdef __APPLE__
  2385. std::vector<InetAddress> newManagedIps2;
  2386. newManagedIps2.reserve(n.config().assignedAddressCount);
  2387. #endif
  2388. for(unsigned int i=0;i<n.config().assignedAddressCount;++i) {
  2389. const InetAddress *ii = reinterpret_cast<const InetAddress *>(&(n.config().assignedAddresses[i]));
  2390. if (checkIfManagedIsAllowed(n,*ii))
  2391. newManagedIps.push_back(*ii);
  2392. }
  2393. std::sort(newManagedIps.begin(),newManagedIps.end());
  2394. newManagedIps.erase(std::unique(newManagedIps.begin(),newManagedIps.end()),newManagedIps.end());
  2395. for(std::vector<InetAddress>::iterator ip(n.managedIps().begin());ip!=n.managedIps().end();++ip) {
  2396. if (std::find(newManagedIps.begin(),newManagedIps.end(),*ip) == newManagedIps.end()) {
  2397. if (!n.tap()->removeIp(*ip))
  2398. fprintf(stderr,"ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  2399. #ifdef __WINDOWS__
  2400. WinFWHelper::removeICMPRule(*ip, n.config().nwid);
  2401. #endif
  2402. }
  2403. }
  2404. for(std::vector<InetAddress>::iterator ip(newManagedIps.begin());ip!=newManagedIps.end();++ip) {
  2405. #ifdef __APPLE__
  2406. // We can't add multiple addresses to an interface on macOs unless we futz with the netmask.
  2407. // see `man ifconfig`, alias section
  2408. // "If the address is on the same subnet as the first network address for this interface, a non-conflicting netmask must be given. Usually 0xffffffff is most appropriate."
  2409. auto same_subnet = [ip](InetAddress i){
  2410. return ip->network() == i.network();
  2411. };
  2412. #endif
  2413. if (std::find(n.managedIps().begin(),n.managedIps().end(),*ip) == n.managedIps().end()) {
  2414. #ifdef __APPLE__
  2415. // if same subnet as a previously added address
  2416. if (
  2417. std::find_if(n.managedIps().begin(),n.managedIps().end(), same_subnet) != n.managedIps().end() ||
  2418. std::find_if(newManagedIps2.begin(),newManagedIps2.end(), same_subnet) != newManagedIps2.end()
  2419. ) {
  2420. if (ip->isV4()) {
  2421. ip->setPort(32);
  2422. } else {
  2423. ip->setPort(128);
  2424. }
  2425. } else {
  2426. newManagedIps2.push_back(*ip);
  2427. }
  2428. #endif
  2429. if (! n.tap()->addIp(*ip)) {
  2430. fprintf(stderr, "ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  2431. }
  2432. else {
  2433. #ifdef __WINDOWS__
  2434. WinFWHelper::newICMPRule(*ip, n.config().nwid);
  2435. #endif
  2436. }
  2437. }
  2438. }
  2439. #ifdef __APPLE__
  2440. if (!MacDNSHelper::addIps6(n.config().nwid, n.config().mac, n.tap()->deviceName().c_str(), newManagedIps)) {
  2441. fprintf(stderr, "ERROR: unable to add v6 addresses to system configuration" ZT_EOL_S);
  2442. }
  2443. if (!MacDNSHelper::addIps4(n.config().nwid, n.config().mac, n.tap()->deviceName().c_str(), newManagedIps)) {
  2444. fprintf(stderr, "ERROR: unable to add v4 addresses to system configuration" ZT_EOL_S);
  2445. }
  2446. #endif
  2447. n.setManagedIps(newManagedIps);
  2448. }
  2449. if (syncRoutes) {
  2450. // Get tap device name (use LUID in hex on Windows) and IP addresses.
  2451. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2452. char tapdevbuf[64];
  2453. OSUtils::ztsnprintf(tapdevbuf,sizeof(tapdevbuf),"%.16llx",(unsigned long long)((WindowsEthernetTap *)(n.tap().get()))->luid().Value);
  2454. std::string tapdev(tapdevbuf);
  2455. #else
  2456. std::string tapdev(n.tap()->deviceName());
  2457. #endif
  2458. std::vector<InetAddress> tapIps(n.tap()->ips());
  2459. std::set<InetAddress> myIps(tapIps.begin(), tapIps.end());
  2460. for(unsigned int i=0;i<n.config().assignedAddressCount;++i)
  2461. myIps.insert(InetAddress(n.config().assignedAddresses[i]));
  2462. std::set<InetAddress> haveRouteTargets;
  2463. for(unsigned int i=0;i<n.config().routeCount;++i) {
  2464. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config().routes[i].target));
  2465. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config().routes[i].via));
  2466. // Make sure we are allowed to set this managed route, and that 'via' is not our IP. The latter
  2467. // avoids setting routes via the router on the router.
  2468. if ( (!checkIfManagedIsAllowed(n,*target)) || ((via->ss_family == target->ss_family)&&(matchIpOnly(myIps,*via))) )
  2469. continue;
  2470. // Find an IP on the interface that can be a source IP, abort if no IPs assigned.
  2471. const InetAddress *src = nullptr;
  2472. unsigned int mostMatchingPrefixBits = 0;
  2473. for(std::set<InetAddress>::const_iterator i(myIps.begin());i!=myIps.end();++i) {
  2474. const unsigned int matchingPrefixBits = i->matchingPrefixBits(*target);
  2475. if (matchingPrefixBits >= mostMatchingPrefixBits && ((target->isV4() && i->isV4()) || (target->isV6() && i->isV6()))) {
  2476. mostMatchingPrefixBits = matchingPrefixBits;
  2477. src = &(*i);
  2478. }
  2479. }
  2480. if (!src)
  2481. continue;
  2482. // Ignore routes implied by local managed IPs since adding the IP adds the route.
  2483. // Apple on the other hand seems to need this at least on some versions.
  2484. #ifndef __APPLE__
  2485. bool haveRoute = false;
  2486. for(std::vector<InetAddress>::iterator ip(n.managedIps().begin());ip!=n.managedIps().end();++ip) {
  2487. if ((target->netmaskBits() == ip->netmaskBits())&&(target->containsAddress(*ip))) {
  2488. haveRoute = true;
  2489. break;
  2490. }
  2491. }
  2492. if (haveRoute)
  2493. continue;
  2494. #endif
  2495. haveRouteTargets.insert(*target);
  2496. #ifndef ZT_SDK
  2497. SharedPtr<ManagedRoute> &mr = n.managedRoutes()[*target];
  2498. if (!mr)
  2499. mr.set(new ManagedRoute(*target, *via, *src, tapdev.c_str()));
  2500. #endif
  2501. }
  2502. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes().begin());r!=n.managedRoutes().end();) {
  2503. if (haveRouteTargets.find(r->first) == haveRouteTargets.end())
  2504. n.managedRoutes().erase(r++);
  2505. else ++r;
  2506. }
  2507. // Sync device-local managed routes first, then indirect results. That way
  2508. // we don't get destination unreachable for routes that are via things
  2509. // that do not yet have routes in the system.
  2510. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes().begin());r!=n.managedRoutes().end();++r) {
  2511. if (!r->second->via())
  2512. r->second->sync();
  2513. }
  2514. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes().begin());r!=n.managedRoutes().end();++r) {
  2515. if (r->second->via() && (!r->second->target().isDefaultRoute() || _node->online())) {
  2516. r->second->sync();
  2517. }
  2518. }
  2519. }
  2520. if (syncDns) {
  2521. if (n.allowDNS()) {
  2522. if (strlen(n.config().dns.domain) != 0) {
  2523. std::vector<InetAddress> servers;
  2524. for (int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  2525. InetAddress a(n.config().dns.server_addr[j]);
  2526. if (a.isV4() || a.isV6()) {
  2527. servers.push_back(a);
  2528. }
  2529. }
  2530. n.tap()->setDns(n.config().dns.domain, servers);
  2531. }
  2532. } else {
  2533. #ifdef __APPLE__
  2534. MacDNSHelper::removeDNS(n.config().nwid);
  2535. #elif defined(__WINDOWS__)
  2536. WinDNSHelper::removeDNS(n.config().nwid);
  2537. #endif
  2538. }
  2539. }
  2540. }
  2541. // =========================================================================
  2542. // Handlers for Node and Phy<> callbacks
  2543. // =========================================================================
  2544. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len)
  2545. {
  2546. if (_forceTcpRelay) {
  2547. return;
  2548. }
  2549. Metrics::udp_recv += len;
  2550. const uint64_t now = OSUtils::now();
  2551. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  2552. _lastDirectReceiveFromGlobal = now;
  2553. }
  2554. const ZT_ResultCode rc = _node->processWirePacket(nullptr,now,reinterpret_cast<int64_t>(sock),reinterpret_cast<const struct sockaddr_storage *>(from),data,len,&_nextBackgroundTaskDeadline);
  2555. if (ZT_ResultCode_isFatal(rc)) {
  2556. char tmp[256];
  2557. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  2558. Mutex::Lock _l(_termReason_m);
  2559. _termReason = ONE_UNRECOVERABLE_ERROR;
  2560. _fatalErrorMessage = tmp;
  2561. this->terminate();
  2562. }
  2563. }
  2564. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  2565. {
  2566. if (!success) {
  2567. phyOnTcpClose(sock,uptr);
  2568. return;
  2569. }
  2570. TcpConnection *const tc = reinterpret_cast<TcpConnection *>(*uptr);
  2571. if (!tc) { // sanity check
  2572. _phy.close(sock,true);
  2573. return;
  2574. }
  2575. tc->sock = sock;
  2576. if (tc->type == TcpConnection::TCP_TUNNEL_OUTGOING) {
  2577. if (_tcpFallbackTunnel)
  2578. _phy.close(_tcpFallbackTunnel->sock);
  2579. _tcpFallbackTunnel = tc;
  2580. _phy.streamSend(sock,ZT_TCP_TUNNEL_HELLO,sizeof(ZT_TCP_TUNNEL_HELLO));
  2581. } else {
  2582. _phy.close(sock,true);
  2583. }
  2584. }
  2585. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  2586. {
  2587. if (!from) {
  2588. _phy.close(sockN,false);
  2589. return;
  2590. } else {
  2591. #ifdef ZT_SDK
  2592. // Immediately close new local connections. The intention is to prevent the backplane from being accessed when operating as libzt
  2593. if (!allowHttpBackplaneManagement && ((InetAddress*)from)->ipScope() == InetAddress::IP_SCOPE_LOOPBACK) {
  2594. _phy.close(sockN,false);
  2595. return;
  2596. }
  2597. #endif
  2598. TcpConnection *tc = new TcpConnection();
  2599. {
  2600. Mutex::Lock _l(_tcpConnections_m);
  2601. _tcpConnections.push_back(tc);
  2602. }
  2603. tc->type = TcpConnection::TCP_UNCATEGORIZED_INCOMING;
  2604. tc->parent = this;
  2605. tc->sock = sockN;
  2606. tc->remoteAddr = from;
  2607. tc->lastReceive = OSUtils::now();
  2608. http_parser_init(&(tc->parser),HTTP_REQUEST);
  2609. tc->parser.data = (void *)tc;
  2610. tc->messageSize = 0;
  2611. *uptrN = (void *)tc;
  2612. }
  2613. }
  2614. void phyOnTcpClose(PhySocket *sock,void **uptr)
  2615. {
  2616. TcpConnection *tc = (TcpConnection *)*uptr;
  2617. if (tc) {
  2618. if (tc == _tcpFallbackTunnel) {
  2619. _tcpFallbackTunnel = (TcpConnection *)0;
  2620. }
  2621. {
  2622. Mutex::Lock _l(_tcpConnections_m);
  2623. _tcpConnections.erase(std::remove(_tcpConnections.begin(),_tcpConnections.end(),tc),_tcpConnections.end());
  2624. }
  2625. delete tc;
  2626. }
  2627. }
  2628. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  2629. {
  2630. try {
  2631. if (!len) return; // sanity check, should never happen
  2632. Metrics::tcp_recv += len;
  2633. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  2634. tc->lastReceive = OSUtils::now();
  2635. switch(tc->type) {
  2636. case TcpConnection::TCP_UNCATEGORIZED_INCOMING:
  2637. return;
  2638. case TcpConnection::TCP_HTTP_INCOMING:
  2639. case TcpConnection::TCP_HTTP_OUTGOING:
  2640. http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len);
  2641. if ((tc->parser.upgrade)||(tc->parser.http_errno != HPE_OK))
  2642. _phy.close(sock);
  2643. return;
  2644. case TcpConnection::TCP_TUNNEL_OUTGOING:
  2645. tc->readq.append((const char *)data,len);
  2646. while (tc->readq.length() >= 5) {
  2647. const char *data = tc->readq.data();
  2648. const unsigned long mlen = ( ((((unsigned long)data[3]) & 0xff) << 8) | (((unsigned long)data[4]) & 0xff) );
  2649. if (tc->readq.length() >= (mlen + 5)) {
  2650. InetAddress from;
  2651. unsigned long plen = mlen; // payload length, modified if there's an IP header
  2652. data += 5; // skip forward past pseudo-TLS junk and mlen
  2653. if (plen == 4) {
  2654. // Hello message, which isn't sent by proxy and would be ignored by client
  2655. } else if (plen) {
  2656. // Messages should contain IPv4 or IPv6 source IP address data
  2657. switch(data[0]) {
  2658. case 4: // IPv4
  2659. if (plen >= 7) {
  2660. from.set((const void *)(data + 1),4,((((unsigned int)data[5]) & 0xff) << 8) | (((unsigned int)data[6]) & 0xff));
  2661. data += 7; // type + 4 byte IP + 2 byte port
  2662. plen -= 7;
  2663. } else {
  2664. _phy.close(sock);
  2665. return;
  2666. }
  2667. break;
  2668. case 6: // IPv6
  2669. if (plen >= 19) {
  2670. from.set((const void *)(data + 1),16,((((unsigned int)data[17]) & 0xff) << 8) | (((unsigned int)data[18]) & 0xff));
  2671. data += 19; // type + 16 byte IP + 2 byte port
  2672. plen -= 19;
  2673. } else {
  2674. _phy.close(sock);
  2675. return;
  2676. }
  2677. break;
  2678. case 0: // none/omitted
  2679. ++data;
  2680. --plen;
  2681. break;
  2682. default: // invalid address type
  2683. _phy.close(sock);
  2684. return;
  2685. }
  2686. if (from) {
  2687. InetAddress fakeTcpLocalInterfaceAddress((uint32_t)0xffffffff,0xffff);
  2688. const ZT_ResultCode rc = _node->processWirePacket(
  2689. (void *)0,
  2690. OSUtils::now(),
  2691. -1,
  2692. reinterpret_cast<struct sockaddr_storage *>(&from),
  2693. data,
  2694. plen,
  2695. &_nextBackgroundTaskDeadline);
  2696. if (ZT_ResultCode_isFatal(rc)) {
  2697. char tmp[256];
  2698. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  2699. Mutex::Lock _l(_termReason_m);
  2700. _termReason = ONE_UNRECOVERABLE_ERROR;
  2701. _fatalErrorMessage = tmp;
  2702. this->terminate();
  2703. _phy.close(sock);
  2704. return;
  2705. }
  2706. }
  2707. }
  2708. if (tc->readq.length() > (mlen + 5))
  2709. tc->readq.erase(tc->readq.begin(),tc->readq.begin() + (mlen + 5));
  2710. else tc->readq.clear();
  2711. } else break;
  2712. }
  2713. return;
  2714. }
  2715. } catch ( ... ) {
  2716. _phy.close(sock);
  2717. }
  2718. }
  2719. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  2720. {
  2721. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  2722. bool closeit = false;
  2723. {
  2724. Mutex::Lock _l(tc->writeq_m);
  2725. if (tc->writeq.length() > 0) {
  2726. long sent = (long)_phy.streamSend(sock,tc->writeq.data(),(unsigned long)tc->writeq.length(),true);
  2727. Metrics::tcp_send += sent;
  2728. if (sent > 0) {
  2729. if ((unsigned long)sent >= (unsigned long)tc->writeq.length()) {
  2730. tc->writeq.clear();
  2731. _phy.setNotifyWritable(sock,false);
  2732. if (tc->type == TcpConnection::TCP_HTTP_INCOMING)
  2733. closeit = true; // HTTP keep alive not supported
  2734. } else {
  2735. tc->writeq.erase(tc->writeq.begin(),tc->writeq.begin() + sent);
  2736. }
  2737. }
  2738. } else {
  2739. _phy.setNotifyWritable(sock,false);
  2740. }
  2741. }
  2742. if (closeit)
  2743. _phy.close(sock);
  2744. }
  2745. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  2746. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  2747. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  2748. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  2749. inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
  2750. inline int nodeVirtualNetworkConfigFunction(uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwc)
  2751. {
  2752. Mutex::Lock _l(_nets_m);
  2753. NetworkState &n = _nets[nwid];
  2754. n.setWebPort(_primaryPort);
  2755. switch (op) {
  2756. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  2757. if (!n.tap()) {
  2758. try {
  2759. char friendlyName[128];
  2760. OSUtils::ztsnprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid);
  2761. n.setTap(EthernetTap::newInstance(
  2762. nullptr,
  2763. _homePath.c_str(),
  2764. MAC(nwc->mac),
  2765. nwc->mtu,
  2766. (unsigned int)ZT_IF_METRIC,
  2767. nwid,
  2768. friendlyName,
  2769. StapFrameHandler,
  2770. (void *)this));
  2771. *nuptr = (void *)&n;
  2772. char nlcpath[256];
  2773. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2774. std::string nlcbuf;
  2775. if (OSUtils::readFile(nlcpath,nlcbuf)) {
  2776. Dictionary<4096> nc;
  2777. nc.load(nlcbuf.c_str());
  2778. Buffer<1024> allowManaged;
  2779. if (nc.get("allowManaged", allowManaged) && allowManaged.size() > 0) {
  2780. std::string addresses (allowManaged.begin(), allowManaged.size());
  2781. if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility
  2782. if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') {
  2783. n.setAllowManaged(true);
  2784. } else {
  2785. n.setAllowManaged(false);
  2786. }
  2787. } else {
  2788. // this should be a list of IP addresses
  2789. n.setAllowManaged(true);
  2790. size_t pos = 0;
  2791. while (true) {
  2792. size_t nextPos = addresses.find(',', pos);
  2793. std::string address = addresses.substr(pos, (nextPos == std::string::npos ? addresses.size() : nextPos) - pos);
  2794. n.addToAllowManagedWhiteList(InetAddress(address.c_str()));
  2795. if (nextPos == std::string::npos) break;
  2796. pos = nextPos + 1;
  2797. }
  2798. }
  2799. } else {
  2800. n.setAllowManaged(true);
  2801. }
  2802. n.setAllowGlobal(nc.getB("allowGlobal", false));
  2803. n.setAllowDefault(nc.getB("allowDefault", false));
  2804. n.setAllowDNS(nc.getB("allowDNS", false));
  2805. }
  2806. } catch (std::exception &exc) {
  2807. #ifdef __WINDOWS__
  2808. FILE *tapFailLog = fopen((_homePath + ZT_PATH_SEPARATOR_S"port_error_log.txt").c_str(),"a");
  2809. if (tapFailLog) {
  2810. fprintf(tapFailLog,"%.16llx: %s" ZT_EOL_S,(unsigned long long)nwid,exc.what());
  2811. fclose(tapFailLog);
  2812. }
  2813. #else
  2814. fprintf(stderr,"ERROR: unable to configure virtual network port: %s" ZT_EOL_S,exc.what());
  2815. #endif
  2816. _nets.erase(nwid);
  2817. return -999;
  2818. } catch ( ... ) {
  2819. return -999; // tap init failed
  2820. }
  2821. }
  2822. // After setting up tap, fall through to CONFIG_UPDATE since we also want to do this...
  2823. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  2824. n.setConfig(nwc);
  2825. if (n.tap()) { // sanity check
  2826. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2827. // wait for up to 5 seconds for the WindowsEthernetTap to actually be initialized
  2828. //
  2829. // without WindowsEthernetTap::isInitialized() returning true, the won't actually
  2830. // be online yet and setting managed routes on it will fail.
  2831. const int MAX_SLEEP_COUNT = 500;
  2832. for (int i = 0; !((WindowsEthernetTap *)(n.tap().get()))->isInitialized() && i < MAX_SLEEP_COUNT; i++) {
  2833. Sleep(10);
  2834. }
  2835. #endif
  2836. syncManagedStuff(n,true,true,true);
  2837. n.tap()->setMtu(nwc->mtu);
  2838. } else {
  2839. _nets.erase(nwid);
  2840. return -999; // tap init failed
  2841. }
  2842. break;
  2843. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  2844. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  2845. if (n.tap()) { // sanity check
  2846. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2847. std::string winInstanceId(((WindowsEthernetTap *)(n.tap().get()))->instanceId());
  2848. #endif
  2849. *nuptr = (void *)0;
  2850. n.tap().reset();
  2851. _nets.erase(nwid);
  2852. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2853. if ((op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) && (winInstanceId.length() > 0)) {
  2854. WindowsEthernetTap::deletePersistentTapDevice(winInstanceId.c_str());
  2855. WinFWHelper::removeICMPRules(nwid);
  2856. }
  2857. #endif
  2858. if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) {
  2859. char nlcpath[256];
  2860. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2861. OSUtils::rm(nlcpath);
  2862. }
  2863. } else {
  2864. _nets.erase(nwid);
  2865. }
  2866. break;
  2867. }
  2868. return 0;
  2869. }
  2870. inline void nodeEventCallback(enum ZT_Event event,const void *metaData)
  2871. {
  2872. switch(event) {
  2873. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  2874. Mutex::Lock _l(_termReason_m);
  2875. _termReason = ONE_IDENTITY_COLLISION;
  2876. _fatalErrorMessage = "identity/address collision";
  2877. this->terminate();
  2878. } break;
  2879. case ZT_EVENT_TRACE: {
  2880. if (metaData) {
  2881. ::fprintf(stderr,"%s" ZT_EOL_S,(const char *)metaData);
  2882. ::fflush(stderr);
  2883. }
  2884. } break;
  2885. case ZT_EVENT_USER_MESSAGE: {
  2886. const ZT_UserMessage *um = reinterpret_cast<const ZT_UserMessage *>(metaData);
  2887. if ((um->typeId == ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE)&&(_updater)) {
  2888. _updater->handleSoftwareUpdateUserMessage(um->origin,um->data,um->length);
  2889. }
  2890. } break;
  2891. case ZT_EVENT_REMOTE_TRACE: {
  2892. const ZT_RemoteTrace *rt = reinterpret_cast<const ZT_RemoteTrace *>(metaData);
  2893. if ((rt)&&(rt->len > 0)&&(rt->len <= ZT_MAX_REMOTE_TRACE_SIZE)&&(rt->data))
  2894. _controller->handleRemoteTrace(*rt);
  2895. }
  2896. default:
  2897. break;
  2898. }
  2899. }
  2900. #if ZT_VAULT_SUPPORT
  2901. inline bool nodeVaultPutIdentity(enum ZT_StateObjectType type, const void *data, int len)
  2902. {
  2903. bool retval = false;
  2904. if (type != ZT_STATE_OBJECT_IDENTITY_PUBLIC && type != ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2905. return retval;
  2906. }
  2907. CURL *curl = curl_easy_init();
  2908. if (curl) {
  2909. char token[512] = { 0 };
  2910. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  2911. struct curl_slist *chunk = NULL;
  2912. chunk = curl_slist_append(chunk, token);
  2913. char content_type[512] = { 0 };
  2914. snprintf(content_type, sizeof(content_type), "Content-Type: application/json");
  2915. chunk = curl_slist_append(chunk, content_type);
  2916. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  2917. char url[2048] = { 0 };
  2918. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  2919. curl_easy_setopt(curl, CURLOPT_URL, url);
  2920. json d = json::object();
  2921. if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2922. std::string key((const char*)data, len);
  2923. d["public"] = key;
  2924. }
  2925. else if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2926. std::string key((const char*)data, len);
  2927. d["secret"] = key;
  2928. }
  2929. if (!d.empty()) {
  2930. std::string post = d.dump();
  2931. if (!post.empty()) {
  2932. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
  2933. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post.length());
  2934. #ifndef NDEBUG
  2935. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  2936. #endif
  2937. CURLcode res = curl_easy_perform(curl);
  2938. if (res == CURLE_OK) {
  2939. long response_code = 0;
  2940. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  2941. if (response_code == 200 || response_code == 204) {
  2942. retval = true;
  2943. }
  2944. }
  2945. }
  2946. }
  2947. curl_easy_cleanup(curl);
  2948. curl = NULL;
  2949. curl_slist_free_all(chunk);
  2950. chunk = NULL;
  2951. }
  2952. return retval;
  2953. }
  2954. #endif
  2955. inline void nodeStatePutFunction(enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2956. {
  2957. #if ZT_VAULT_SUPPORT
  2958. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  2959. if (nodeVaultPutIdentity(type, data, len)) {
  2960. // value successfully written to Vault
  2961. return;
  2962. }
  2963. // else fallback to disk
  2964. }
  2965. #endif
  2966. char p[1024];
  2967. FILE *f;
  2968. bool secure = false;
  2969. char dirname[1024];
  2970. dirname[0] = 0;
  2971. switch(type) {
  2972. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  2973. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  2974. break;
  2975. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  2976. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  2977. secure = true;
  2978. break;
  2979. case ZT_STATE_OBJECT_PLANET:
  2980. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  2981. break;
  2982. case ZT_STATE_OBJECT_MOON:
  2983. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "moons.d",_homePath.c_str());
  2984. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.moon",dirname,(unsigned long long)id[0]);
  2985. break;
  2986. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  2987. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str());
  2988. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]);
  2989. break;
  2990. case ZT_STATE_OBJECT_PEER:
  2991. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str());
  2992. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]);
  2993. break;
  2994. default:
  2995. return;
  2996. }
  2997. if ((len >= 0)&&(data)) {
  2998. // Check to see if we've already written this first. This reduces
  2999. // redundant writes and I/O overhead on most platforms and has
  3000. // little effect on others.
  3001. f = fopen(p,"rb");
  3002. if (f) {
  3003. char *const buf = (char *)malloc(len*4);
  3004. if (buf) {
  3005. long l = (long)fread(buf,1,(size_t)(len*4),f);
  3006. fclose(f);
  3007. if ((l == (long)len)&&(memcmp(data,buf,l) == 0)) {
  3008. free(buf);
  3009. return;
  3010. }
  3011. free(buf);
  3012. }
  3013. }
  3014. f = fopen(p,"wb");
  3015. if ((!f)&&(dirname[0])) { // create subdirectory if it does not exist
  3016. OSUtils::mkdir(dirname);
  3017. f = fopen(p,"wb");
  3018. }
  3019. if (f) {
  3020. if (fwrite(data,len,1,f) != 1)
  3021. fprintf(stderr,"WARNING: unable to write to file: %s (I/O error)" ZT_EOL_S,p);
  3022. fclose(f);
  3023. if (secure)
  3024. OSUtils::lockDownFile(p,false);
  3025. } else {
  3026. fprintf(stderr,"WARNING: unable to write to file: %s (unable to open)" ZT_EOL_S,p);
  3027. }
  3028. } else {
  3029. OSUtils::rm(p);
  3030. }
  3031. }
  3032. #if ZT_VAULT_SUPPORT
  3033. inline int nodeVaultGetIdentity(enum ZT_StateObjectType type, void *data, unsigned int maxlen)
  3034. {
  3035. if (type != ZT_STATE_OBJECT_IDENTITY_SECRET && type != ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  3036. return -1;
  3037. }
  3038. int ret = -1;
  3039. CURL *curl = curl_easy_init();
  3040. if (curl) {
  3041. char token[512] = { 0 };
  3042. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  3043. struct curl_slist *chunk = NULL;
  3044. chunk = curl_slist_append(chunk, token);
  3045. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  3046. char url[2048] = { 0 };
  3047. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  3048. curl_easy_setopt(curl, CURLOPT_URL, url);
  3049. std::string response;
  3050. std::string res_headers;
  3051. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlResponseWrite);
  3052. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  3053. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &res_headers);
  3054. #ifndef NDEBUG
  3055. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  3056. #endif
  3057. CURLcode res = curl_easy_perform(curl);
  3058. if (res == CURLE_OK) {
  3059. long response_code = 0;
  3060. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  3061. if (response_code == 200) {
  3062. try {
  3063. json payload = json::parse(response);
  3064. if (!payload["data"].is_null()) {
  3065. json &d = payload["data"];
  3066. if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  3067. std::string secret = OSUtils::jsonString(d["secret"],"");
  3068. if (!secret.empty()) {
  3069. ret = (int)secret.length();
  3070. memcpy(data, secret.c_str(), ret);
  3071. }
  3072. }
  3073. else if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  3074. std::string pub = OSUtils::jsonString(d["public"],"");
  3075. if (!pub.empty()) {
  3076. ret = (int)pub.length();
  3077. memcpy(data, pub.c_str(), ret);
  3078. }
  3079. }
  3080. }
  3081. }
  3082. catch (...) {
  3083. ret = -1;
  3084. }
  3085. }
  3086. }
  3087. curl_easy_cleanup(curl);
  3088. curl = NULL;
  3089. curl_slist_free_all(chunk);
  3090. chunk = NULL;
  3091. }
  3092. return ret;
  3093. }
  3094. #endif
  3095. inline int nodeStateGetFunction(enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  3096. {
  3097. #if ZT_VAULT_SUPPORT
  3098. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) ) {
  3099. int retval = nodeVaultGetIdentity(type, data, maxlen);
  3100. if (retval >= 0)
  3101. return retval;
  3102. // else continue file based lookup
  3103. }
  3104. #endif
  3105. char p[4096];
  3106. switch(type) {
  3107. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  3108. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  3109. break;
  3110. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  3111. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  3112. break;
  3113. case ZT_STATE_OBJECT_PLANET:
  3114. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  3115. break;
  3116. case ZT_STATE_OBJECT_MOON:
  3117. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d" ZT_PATH_SEPARATOR_S "%.16llx.moon",_homePath.c_str(),(unsigned long long)id[0]);
  3118. break;
  3119. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  3120. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_homePath.c_str(),(unsigned long long)id[0]);
  3121. break;
  3122. case ZT_STATE_OBJECT_PEER:
  3123. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer",_homePath.c_str(),(unsigned long long)id[0]);
  3124. break;
  3125. default:
  3126. return -1;
  3127. }
  3128. FILE *f = fopen(p,"rb");
  3129. if (f) {
  3130. int n = (int)fread(data,1,maxlen,f);
  3131. fclose(f);
  3132. #if ZT_VAULT_SUPPORT
  3133. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  3134. // If we've gotten here while Vault is enabled, Vault does not know the key and it's been
  3135. // read from disk instead.
  3136. //
  3137. // We should put the value in Vault and remove the local file.
  3138. if (nodeVaultPutIdentity(type, data, n)) {
  3139. unlink(p);
  3140. }
  3141. }
  3142. #endif
  3143. if (n >= 0)
  3144. return n;
  3145. }
  3146. return -1;
  3147. }
  3148. inline int nodeWirePacketSendFunction(const int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  3149. {
  3150. #ifdef ZT_TCP_FALLBACK_RELAY
  3151. if(_allowTcpFallbackRelay) {
  3152. if (addr->ss_family == AF_INET) {
  3153. // TCP fallback tunnel support, currently IPv4 only
  3154. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(addr)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  3155. // Engage TCP tunnel fallback if we haven't received anything valid from a global
  3156. // IP address in ZT_TCP_FALLBACK_AFTER milliseconds. If we do start getting
  3157. // valid direct traffic we'll stop using it and close the socket after a while.
  3158. const int64_t now = OSUtils::now();
  3159. if (_forceTcpRelay || (((now - _lastDirectReceiveFromGlobal) > ZT_TCP_FALLBACK_AFTER)&&((now - _lastRestart) > ZT_TCP_FALLBACK_AFTER))) {
  3160. if (_tcpFallbackTunnel) {
  3161. bool flushNow = false;
  3162. {
  3163. Mutex::Lock _l(_tcpFallbackTunnel->writeq_m);
  3164. if (_tcpFallbackTunnel->writeq.size() < (1024 * 64)) {
  3165. if (_tcpFallbackTunnel->writeq.length() == 0) {
  3166. _phy.setNotifyWritable(_tcpFallbackTunnel->sock,true);
  3167. flushNow = true;
  3168. }
  3169. const unsigned long mlen = len + 7;
  3170. _tcpFallbackTunnel->writeq.push_back((char)0x17);
  3171. _tcpFallbackTunnel->writeq.push_back((char)0x03);
  3172. _tcpFallbackTunnel->writeq.push_back((char)0x03); // fake TLS 1.2 header
  3173. _tcpFallbackTunnel->writeq.push_back((char)((mlen >> 8) & 0xff));
  3174. _tcpFallbackTunnel->writeq.push_back((char)(mlen & 0xff));
  3175. _tcpFallbackTunnel->writeq.push_back((char)4); // IPv4
  3176. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_addr.s_addr))),4);
  3177. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_port))),2);
  3178. _tcpFallbackTunnel->writeq.append((const char *)data,len);
  3179. }
  3180. }
  3181. if (flushNow) {
  3182. void *tmpptr = (void *)_tcpFallbackTunnel;
  3183. phyOnTcpWritable(_tcpFallbackTunnel->sock,&tmpptr);
  3184. }
  3185. } else if (_forceTcpRelay || (((now - _lastSendToGlobalV4) < ZT_TCP_FALLBACK_AFTER)&&((now - _lastSendToGlobalV4) > (ZT_PING_CHECK_INTERVAL / 2)))) {
  3186. const InetAddress addr(_fallbackRelayAddress);
  3187. TcpConnection *tc = new TcpConnection();
  3188. {
  3189. Mutex::Lock _l(_tcpConnections_m);
  3190. _tcpConnections.push_back(tc);
  3191. }
  3192. tc->type = TcpConnection::TCP_TUNNEL_OUTGOING;
  3193. tc->remoteAddr = addr;
  3194. tc->lastReceive = OSUtils::now();
  3195. tc->parent = this;
  3196. tc->sock = (PhySocket *)0; // set in connect handler
  3197. tc->messageSize = 0;
  3198. bool connected = false;
  3199. _phy.tcpConnect(reinterpret_cast<const struct sockaddr *>(&addr),connected,(void *)tc,true);
  3200. }
  3201. }
  3202. _lastSendToGlobalV4 = now;
  3203. }
  3204. }
  3205. }
  3206. if (_forceTcpRelay) {
  3207. // Shortcut here so that we don't emit any UDP packets
  3208. return 0;
  3209. }
  3210. #endif // ZT_TCP_FALLBACK_RELAY
  3211. // Even when relaying we still send via UDP. This way if UDP starts
  3212. // working we can instantly "fail forward" to it and stop using TCP
  3213. // proxy fallback, which is slow.
  3214. if ((localSocket != -1)&&(localSocket != 0)&&(_binder.isUdpSocketValid((PhySocket *)((uintptr_t)localSocket)))) {
  3215. if ((ttl)&&(addr->ss_family == AF_INET)) {
  3216. _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl);
  3217. }
  3218. const bool r = _phy.udpSend((PhySocket *)((uintptr_t)localSocket),(const struct sockaddr *)addr,data,len);
  3219. if ((ttl)&&(addr->ss_family == AF_INET)) {
  3220. _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255);
  3221. }
  3222. return ((r) ? 0 : -1);
  3223. } else {
  3224. return ((_binder.udpSendAll(_phy,addr,data,len,ttl)) ? 0 : -1);
  3225. }
  3226. }
  3227. inline void nodeVirtualNetworkFrameFunction(uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  3228. {
  3229. NetworkState *n = reinterpret_cast<NetworkState *>(*nuptr);
  3230. if ((!n)||(!n->tap()))
  3231. return;
  3232. n->tap()->put(MAC(sourceMac),MAC(destMac),etherType,data,len);
  3233. }
  3234. inline int nodePathCheckFunction(uint64_t ztaddr,const int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  3235. {
  3236. // Make sure we're not trying to do ZeroTier-over-ZeroTier
  3237. {
  3238. Mutex::Lock _l(_nets_m);
  3239. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  3240. if (n->second.tap()) {
  3241. std::vector<InetAddress> ips(n->second.tap()->ips());
  3242. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  3243. if (i->containsAddress(*(reinterpret_cast<const InetAddress *>(remoteAddr)))) {
  3244. return 0;
  3245. }
  3246. }
  3247. }
  3248. }
  3249. }
  3250. /* Note: I do not think we need to scan for overlap with managed routes
  3251. * because of the "route forking" and interface binding that we do. This
  3252. * ensures (we hope) that ZeroTier traffic will still take the physical
  3253. * path even if its managed routes override this for other traffic. Will
  3254. * revisit if we see recursion problems. */
  3255. // Check blacklists
  3256. const Hashtable< uint64_t,std::vector<InetAddress> > *blh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  3257. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  3258. if (remoteAddr->ss_family == AF_INET) {
  3259. blh = &_v4Blacklists;
  3260. gbl = &_globalV4Blacklist;
  3261. } else if (remoteAddr->ss_family == AF_INET6) {
  3262. blh = &_v6Blacklists;
  3263. gbl = &_globalV6Blacklist;
  3264. }
  3265. if (blh) {
  3266. Mutex::Lock _l(_localConfig_m);
  3267. const std::vector<InetAddress> *l = blh->get(ztaddr);
  3268. if (l) {
  3269. for(std::vector<InetAddress>::const_iterator a(l->begin());a!=l->end();++a) {
  3270. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  3271. return 0;
  3272. }
  3273. }
  3274. }
  3275. if (gbl) {
  3276. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  3277. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  3278. return 0;
  3279. }
  3280. }
  3281. return 1;
  3282. }
  3283. inline int nodePathLookupFunction(uint64_t ztaddr, int family, struct sockaddr_storage* result)
  3284. {
  3285. const Hashtable< uint64_t, std::vector<InetAddress> >* lh = (const Hashtable< uint64_t, std::vector<InetAddress> > *)0;
  3286. if (family < 0)
  3287. lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints;
  3288. else if (family == AF_INET)
  3289. lh = &_v4Hints;
  3290. else if (family == AF_INET6)
  3291. lh = &_v6Hints;
  3292. else return 0;
  3293. const std::vector<InetAddress>* l = lh->get(ztaddr);
  3294. if ((l) && (!l->empty())) {
  3295. memcpy(result, &((*l)[(unsigned long)_node->prng() % l->size()]), sizeof(struct sockaddr_storage));
  3296. return 1;
  3297. }
  3298. else return 0;
  3299. }
  3300. inline void tapFrameHandler(uint64_t nwid, const MAC& from, const MAC& to, unsigned int etherType, unsigned int vlanId, const void* data, unsigned int len)
  3301. {
  3302. _node->processVirtualNetworkFrame((void*)0, OSUtils::now(), nwid, from.toInt(), to.toInt(), etherType, vlanId, data, len, &_nextBackgroundTaskDeadline);
  3303. }
  3304. inline void onHttpResponseFromClient(TcpConnection* tc)
  3305. {
  3306. _phy.close(tc->sock);
  3307. }
  3308. bool shouldBindInterface(const char* ifname, const InetAddress& ifaddr)
  3309. {
  3310. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  3311. if ((ifname[0] == 'l') && (ifname[1] == 'o')) return false; // loopback
  3312. if ((ifname[0] == 'z') && (ifname[1] == 't')) return false; // sanity check: zt#
  3313. if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  3314. if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  3315. #endif
  3316. #ifdef __APPLE__
  3317. if ((ifname[0] == 'f') && (ifname[1] == 'e') && (ifname[2] == 't') && (ifname[3] == 'h')) return false; // ... as is feth#
  3318. if ((ifname[0] == 'l') && (ifname[1] == 'o')) return false; // loopback
  3319. if ((ifname[0] == 'z') && (ifname[1] == 't')) return false; // sanity check: zt#
  3320. if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  3321. if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  3322. if ((ifname[0] == 'u') && (ifname[1] == 't') && (ifname[2] == 'u') && (ifname[3] == 'n')) return false; // ... as is utun#
  3323. #endif
  3324. #ifdef __FreeBSD__
  3325. if ((ifname[0] == 'l') && (ifname[1] == 'o')) return false; // loopback
  3326. if ((ifname[0] == 'z') && (ifname[1] == 't')) return false; // sanity check: zt#
  3327. #endif
  3328. {
  3329. Mutex::Lock _l(_localConfig_m);
  3330. for(std::vector<std::string>::const_iterator p(_interfacePrefixBlacklist.begin());p!=_interfacePrefixBlacklist.end();++p) {
  3331. if (!strncmp(p->c_str(),ifname,p->length()))
  3332. return false;
  3333. }
  3334. }
  3335. {
  3336. // Check global blacklists
  3337. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  3338. if (ifaddr.ss_family == AF_INET) {
  3339. gbl = &_globalV4Blacklist;
  3340. } else if (ifaddr.ss_family == AF_INET6) {
  3341. gbl = &_globalV6Blacklist;
  3342. }
  3343. if (gbl) {
  3344. Mutex::Lock _l(_localConfig_m);
  3345. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  3346. if (a->containsAddress(ifaddr))
  3347. return false;
  3348. }
  3349. }
  3350. }
  3351. {
  3352. Mutex::Lock _l(_nets_m);
  3353. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  3354. if (n->second.tap()) {
  3355. std::vector<InetAddress> ips(n->second.tap()->ips());
  3356. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  3357. if (i->ipsEqual(ifaddr))
  3358. return false;
  3359. }
  3360. #ifdef _WIN32
  3361. if (n->second.tap()->friendlyName() == ifname)
  3362. return false;
  3363. #endif
  3364. }
  3365. }
  3366. }
  3367. return true;
  3368. }
  3369. unsigned int _getRandomPort()
  3370. {
  3371. unsigned int randp = 0;
  3372. Utils::getSecureRandom(&randp,sizeof(randp));
  3373. randp = 20000 + (randp % 45500);
  3374. for(int i=0;;++i) {
  3375. if (i > 1000) {
  3376. return 0;
  3377. } else if (++randp >= 65536) {
  3378. randp = 20000;
  3379. }
  3380. if (_trialBind(randp))
  3381. break;
  3382. }
  3383. return randp;
  3384. }
  3385. bool _trialBind(unsigned int port)
  3386. {
  3387. struct sockaddr_in in4;
  3388. struct sockaddr_in6 in6;
  3389. PhySocket *tb;
  3390. memset(&in4,0,sizeof(in4));
  3391. in4.sin_family = AF_INET;
  3392. in4.sin_port = Utils::hton((uint16_t)port);
  3393. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0,0);
  3394. if (tb) {
  3395. _phy.close(tb,false);
  3396. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0);
  3397. if (tb) {
  3398. _phy.close(tb,false);
  3399. return true;
  3400. }
  3401. }
  3402. memset(&in6,0,sizeof(in6));
  3403. in6.sin6_family = AF_INET6;
  3404. in6.sin6_port = Utils::hton((uint16_t)port);
  3405. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0,0);
  3406. if (tb) {
  3407. _phy.close(tb,false);
  3408. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0);
  3409. if (tb) {
  3410. _phy.close(tb,false);
  3411. return true;
  3412. }
  3413. }
  3414. return false;
  3415. }
  3416. };
  3417. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf)
  3418. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkConfigFunction(nwid,nuptr,op,nwconf); }
  3419. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData)
  3420. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeEventCallback(event,metaData); }
  3421. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  3422. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeStatePutFunction(type,id,data,len); }
  3423. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  3424. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeStateGetFunction(type,id,data,maxlen); }
  3425. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,void *tptr,int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  3426. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeWirePacketSendFunction(localSocket,addr,data,len,ttl); }
  3427. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  3428. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkFrameFunction(nwid,nuptr,sourceMac,destMac,etherType,vlanId,data,len); }
  3429. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  3430. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathCheckFunction(ztaddr,localSocket,remoteAddr); }
  3431. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result)
  3432. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathLookupFunction(ztaddr,family,result); }
  3433. static void StapFrameHandler(void *uptr,void *tptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  3434. { reinterpret_cast<OneServiceImpl *>(uptr)->tapFrameHandler(nwid,from,to,etherType,vlanId,data,len); }
  3435. static int ShttpOnMessageBegin(http_parser *parser)
  3436. {
  3437. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3438. tc->currentHeaderField = "";
  3439. tc->currentHeaderValue = "";
  3440. tc->messageSize = 0;
  3441. tc->url.clear();
  3442. tc->status.clear();
  3443. tc->headers.clear();
  3444. tc->readq.clear();
  3445. return 0;
  3446. }
  3447. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  3448. {
  3449. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3450. tc->messageSize += (unsigned long)length;
  3451. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  3452. return -1;
  3453. tc->url.append(ptr,length);
  3454. return 0;
  3455. }
  3456. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  3457. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  3458. #else
  3459. static int ShttpOnStatus(http_parser *parser)
  3460. #endif
  3461. { return 0; }
  3462. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  3463. {
  3464. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3465. tc->messageSize += (unsigned long)length;
  3466. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  3467. return -1;
  3468. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length())) {
  3469. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  3470. tc->currentHeaderField = "";
  3471. tc->currentHeaderValue = "";
  3472. }
  3473. for(size_t i=0;i<length;++i)
  3474. tc->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  3475. return 0;
  3476. }
  3477. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  3478. {
  3479. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3480. tc->messageSize += (unsigned long)length;
  3481. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  3482. return -1;
  3483. tc->currentHeaderValue.append(ptr,length);
  3484. return 0;
  3485. }
  3486. static int ShttpOnHeadersComplete(http_parser *parser)
  3487. {
  3488. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3489. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length()))
  3490. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  3491. return 0;
  3492. }
  3493. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  3494. {
  3495. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3496. tc->messageSize += (unsigned long)length;
  3497. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  3498. return -1;
  3499. tc->readq.append(ptr,length);
  3500. return 0;
  3501. }
  3502. static int ShttpOnMessageComplete(http_parser *parser)
  3503. {
  3504. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  3505. if (tc->type == TcpConnection::TCP_HTTP_INCOMING) {
  3506. } else {
  3507. tc->parent->onHttpResponseFromClient(tc);
  3508. }
  3509. return 0;
  3510. }
  3511. } // anonymous namespace
  3512. std::string OneService::platformDefaultHomePath()
  3513. {
  3514. return OSUtils::platformDefaultHomePath();
  3515. }
  3516. OneService *OneService::newInstance(const char *hp,unsigned int port) { return new OneServiceImpl(hp,port); }
  3517. OneService::~OneService() {}
  3518. } // namespace ZeroTier