OneService.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154
  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: 2025-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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include <string>
  18. #include <map>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <list>
  22. #include <thread>
  23. #include <mutex>
  24. #include <condition_variable>
  25. #include "../version.h"
  26. #include "../include/ZeroTierOne.h"
  27. #include "../node/Constants.hpp"
  28. #include "../node/Mutex.hpp"
  29. #include "../node/Node.hpp"
  30. #include "../node/Utils.hpp"
  31. #include "../node/InetAddress.hpp"
  32. #include "../node/MAC.hpp"
  33. #include "../node/Identity.hpp"
  34. #include "../node/World.hpp"
  35. #include "../node/Salsa20.hpp"
  36. #include "../node/Poly1305.hpp"
  37. #include "../node/SHA512.hpp"
  38. #include "../node/Bond.hpp"
  39. #include "../node/Peer.hpp"
  40. #include "../osdep/Phy.hpp"
  41. #include "../osdep/Thread.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 "../osdep/Link.hpp"
  49. #include "OneService.hpp"
  50. #include "SoftwareUpdater.hpp"
  51. #ifdef __WINDOWS__
  52. #include <WinSock2.h>
  53. #include <Windows.h>
  54. #include <ShlObj.h>
  55. #include <netioapi.h>
  56. #include <iphlpapi.h>
  57. //#include <unistd.h>
  58. #define stat _stat
  59. #else
  60. #include <sys/types.h>
  61. #include <sys/socket.h>
  62. #include <sys/stat.h>
  63. #include <sys/wait.h>
  64. #include <unistd.h>
  65. #include <ifaddrs.h>
  66. #endif
  67. #ifdef __APPLE__
  68. #include "../osdep/MacDNSHelper.hpp"
  69. #elif defined(__WINDOWS__)
  70. #include "../osdep/WinDNSHelper.hpp"
  71. #endif
  72. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  73. #include <http_parser.h>
  74. #else
  75. #include "../ext/http-parser/http_parser.h"
  76. #endif
  77. #if ZT_VAULT_SUPPORT
  78. extern "C" {
  79. #include <curl/curl.h>
  80. }
  81. #endif
  82. #include "../ext/json/json.hpp"
  83. using json = nlohmann::json;
  84. #include "../controller/EmbeddedNetworkController.hpp"
  85. #include "../controller/PostgreSQL.hpp"
  86. #include "../controller/Redis.hpp"
  87. #include "../osdep/EthernetTap.hpp"
  88. #ifdef __WINDOWS__
  89. #include "../osdep/WindowsEthernetTap.hpp"
  90. #endif
  91. #ifndef ZT_SOFTWARE_UPDATE_DEFAULT
  92. #define ZT_SOFTWARE_UPDATE_DEFAULT "disable"
  93. #endif
  94. // Sanity limits for HTTP
  95. #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
  96. #define ZT_MAX_HTTP_CONNECTIONS 65536
  97. // Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
  98. // bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.
  99. #define ZT_IF_METRIC 5000
  100. // How often to check for new multicast subscriptions on a tap device
  101. #define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
  102. // TCP fallback relay (run by ZeroTier, Inc. -- this will eventually go away)
  103. #ifndef ZT_SDK
  104. #define ZT_TCP_FALLBACK_RELAY "204.80.128.1/443"
  105. #endif
  106. // Frequency at which we re-resolve the TCP fallback relay
  107. #define ZT_TCP_FALLBACK_RERESOLVE_DELAY 86400000
  108. // Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs
  109. #define ZT_TCP_FALLBACK_AFTER 60000
  110. // How often to check for local interface addresses
  111. #define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
  112. // Maximum write buffer size for outgoing TCP connections (sanity limit)
  113. #define ZT_TCP_MAX_WRITEQ_SIZE 33554432
  114. // TCP activity timeout
  115. #define ZT_TCP_ACTIVITY_TIMEOUT 60000
  116. #if ZT_VAULT_SUPPORT
  117. size_t curlResponseWrite(void *ptr, size_t size, size_t nmemb, std::string *data)
  118. {
  119. data->append((char*)ptr, size * nmemb);
  120. return size * nmemb;
  121. }
  122. #endif
  123. namespace ZeroTier {
  124. namespace {
  125. static const InetAddress NULL_INET_ADDR;
  126. // Fake TLS hello for TCP tunnel outgoing connections (TUNNELED mode)
  127. 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) };
  128. static std::string _trimString(const std::string &s)
  129. {
  130. unsigned long end = (unsigned long)s.length();
  131. while (end) {
  132. char c = s[end - 1];
  133. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  134. --end;
  135. else break;
  136. }
  137. unsigned long start = 0;
  138. while (start < end) {
  139. char c = s[start];
  140. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  141. ++start;
  142. else break;
  143. }
  144. return s.substr(start,end - start);
  145. }
  146. static void _networkToJson(nlohmann::json &nj,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
  147. {
  148. char tmp[256];
  149. const char *nstatus = "",*ntype = "";
  150. switch(nc->status) {
  151. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  152. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  153. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  154. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  155. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  156. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  157. }
  158. switch(nc->type) {
  159. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  160. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  161. }
  162. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid);
  163. nj["id"] = tmp;
  164. nj["nwid"] = tmp;
  165. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff));
  166. nj["mac"] = tmp;
  167. nj["name"] = nc->name;
  168. nj["status"] = nstatus;
  169. nj["type"] = ntype;
  170. nj["mtu"] = nc->mtu;
  171. nj["dhcp"] = (bool)(nc->dhcp != 0);
  172. nj["bridge"] = (bool)(nc->bridge != 0);
  173. nj["broadcastEnabled"] = (bool)(nc->broadcastEnabled != 0);
  174. nj["portError"] = nc->portError;
  175. nj["netconfRevision"] = nc->netconfRevision;
  176. nj["portDeviceName"] = portDeviceName;
  177. nj["allowManaged"] = localSettings.allowManaged;
  178. nj["allowGlobal"] = localSettings.allowGlobal;
  179. nj["allowDefault"] = localSettings.allowDefault;
  180. nj["allowDNS"] = localSettings.allowDNS;
  181. nlohmann::json aa = nlohmann::json::array();
  182. for(unsigned int i=0;i<nc->assignedAddressCount;++i) {
  183. aa.push_back(reinterpret_cast<const InetAddress *>(&(nc->assignedAddresses[i]))->toString(tmp));
  184. }
  185. nj["assignedAddresses"] = aa;
  186. nlohmann::json ra = nlohmann::json::array();
  187. for(unsigned int i=0;i<nc->routeCount;++i) {
  188. nlohmann::json rj;
  189. rj["target"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].target))->toString(tmp);
  190. if (nc->routes[i].via.ss_family == nc->routes[i].target.ss_family)
  191. rj["via"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].via))->toIpString(tmp);
  192. else rj["via"] = nlohmann::json();
  193. rj["flags"] = (int)nc->routes[i].flags;
  194. rj["metric"] = (int)nc->routes[i].metric;
  195. ra.push_back(rj);
  196. }
  197. nj["routes"] = ra;
  198. nlohmann::json mca = nlohmann::json::array();
  199. for(unsigned int i=0;i<nc->multicastSubscriptionCount;++i) {
  200. nlohmann::json m;
  201. m["mac"] = MAC(nc->multicastSubscriptions[i].mac).toString(tmp);
  202. m["adi"] = nc->multicastSubscriptions[i].adi;
  203. mca.push_back(m);
  204. }
  205. nj["multicastSubscriptions"] = mca;
  206. nlohmann::json m;
  207. m["domain"] = nc->dns.domain;
  208. m["servers"] = nlohmann::json::array();
  209. for(int j=0;j<ZT_MAX_DNS_SERVERS;++j) {
  210. InetAddress a(nc->dns.server_addr[j]);
  211. if (a.isV4() || a.isV6()) {
  212. char buf[256];
  213. m["servers"].push_back(a.toIpString(buf));
  214. }
  215. }
  216. nj["dns"] = m;
  217. }
  218. static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer)
  219. {
  220. char tmp[256];
  221. const char *prole = "";
  222. switch(peer->role) {
  223. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  224. case ZT_PEER_ROLE_MOON: prole = "MOON"; break;
  225. case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break;
  226. }
  227. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  228. pj["address"] = tmp;
  229. pj["versionMajor"] = peer->versionMajor;
  230. pj["versionMinor"] = peer->versionMinor;
  231. pj["versionRev"] = peer->versionRev;
  232. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev);
  233. pj["version"] = tmp;
  234. pj["latency"] = peer->latency;
  235. pj["role"] = prole;
  236. pj["isBonded"] = peer->isBonded;
  237. pj["bondingPolicy"] = peer->bondingPolicy;
  238. pj["isHealthy"] = peer->isHealthy;
  239. pj["numAliveLinks"] = peer->numAliveLinks;
  240. pj["numTotalLinks"] = peer->numTotalLinks;
  241. nlohmann::json pa = nlohmann::json::array();
  242. for(unsigned int i=0;i<peer->pathCount;++i) {
  243. int64_t lastSend = peer->paths[i].lastSend;
  244. int64_t lastReceive = peer->paths[i].lastReceive;
  245. nlohmann::json j;
  246. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  247. j["lastSend"] = (lastSend < 0) ? 0 : lastSend;
  248. j["lastReceive"] = (lastReceive < 0) ? 0 : lastReceive;
  249. j["trustedPathId"] = peer->paths[i].trustedPathId;
  250. j["active"] = (bool)(peer->paths[i].expired == 0);
  251. j["expired"] = (bool)(peer->paths[i].expired != 0);
  252. j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  253. pa.push_back(j);
  254. }
  255. pj["paths"] = pa;
  256. }
  257. static void _peerBondToJson(nlohmann::json &pj,const ZT_Peer *peer)
  258. {
  259. char tmp[256];
  260. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  261. //pj["aggregateLinkLatency"] = peer->latency;
  262. std::string policyStr = BondController::getPolicyStrByCode(peer->bondingPolicy);
  263. pj["policy"] = policyStr;
  264. nlohmann::json pa = nlohmann::json::array();
  265. for(unsigned int i=0;i<peer->pathCount;++i) {
  266. int64_t lastSend = peer->paths[i].lastSend;
  267. int64_t lastReceive = peer->paths[i].lastReceive;
  268. nlohmann::json j;
  269. j["ifname"] = std::string(peer->paths[i].ifname);
  270. j["path"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  271. j["lastTX"] = (lastSend < 0) ? 0 : lastSend;
  272. j["lastRX"] = (lastReceive < 0) ? 0 : lastReceive;
  273. j["lat"] = peer->paths[i].latencyMean;
  274. j["pdv"] = peer->paths[i].latencyVariance;
  275. //j["trustedPathId"] = peer->paths[i].trustedPathId;
  276. //j["active"] = (bool)(peer->paths[i].expired == 0);
  277. //j["expired"] = (bool)(peer->paths[i].expired != 0);
  278. //j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  279. //j["ltm"] = peer->paths[i].latencyMax;
  280. //j["plr"] = peer->paths[i].packetLossRatio;
  281. //j["per"] = peer->paths[i].packetErrorRatio;
  282. //j["thr"] = peer->paths[i].throughputMean;
  283. //j["thm"] = peer->paths[i].throughputMax;
  284. //j["thv"] = peer->paths[i].throughputVariance;
  285. //j["avl"] = peer->paths[i].availability;
  286. //j["age"] = peer->paths[i].age;
  287. //j["alloc"] = peer->paths[i].allocation;
  288. //j["ifname"] = peer->paths[i].ifname;
  289. pa.push_back(j);
  290. }
  291. pj["links"] = pa;
  292. }
  293. static void _moonToJson(nlohmann::json &mj,const World &world)
  294. {
  295. char tmp[4096];
  296. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",world.id());
  297. mj["id"] = tmp;
  298. mj["timestamp"] = world.timestamp();
  299. mj["signature"] = Utils::hex(world.signature().data,ZT_C25519_SIGNATURE_LEN,tmp);
  300. mj["updatesMustBeSignedBy"] = Utils::hex(world.updatesMustBeSignedBy().data,ZT_C25519_PUBLIC_KEY_LEN,tmp);
  301. nlohmann::json ra = nlohmann::json::array();
  302. for(std::vector<World::Root>::const_iterator r(world.roots().begin());r!=world.roots().end();++r) {
  303. nlohmann::json rj;
  304. rj["identity"] = r->identity.toString(false,tmp);
  305. nlohmann::json eps = nlohmann::json::array();
  306. for(std::vector<InetAddress>::const_iterator a(r->stableEndpoints.begin());a!=r->stableEndpoints.end();++a)
  307. eps.push_back(a->toString(tmp));
  308. rj["stableEndpoints"] = eps;
  309. ra.push_back(rj);
  310. }
  311. mj["roots"] = ra;
  312. mj["waiting"] = false;
  313. }
  314. class OneServiceImpl;
  315. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf);
  316. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData);
  317. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len);
  318. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen);
  319. 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);
  320. 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);
  321. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr);
  322. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result);
  323. 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);
  324. static int ShttpOnMessageBegin(http_parser *parser);
  325. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  326. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  327. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  328. #else
  329. static int ShttpOnStatus(http_parser *parser);
  330. #endif
  331. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  332. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  333. static int ShttpOnHeadersComplete(http_parser *parser);
  334. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  335. static int ShttpOnMessageComplete(http_parser *parser);
  336. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  337. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  338. ShttpOnMessageBegin,
  339. ShttpOnUrl,
  340. ShttpOnStatus,
  341. ShttpOnHeaderField,
  342. ShttpOnValue,
  343. ShttpOnHeadersComplete,
  344. ShttpOnBody,
  345. ShttpOnMessageComplete
  346. };
  347. #else
  348. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  349. ShttpOnMessageBegin,
  350. ShttpOnUrl,
  351. ShttpOnHeaderField,
  352. ShttpOnValue,
  353. ShttpOnHeadersComplete,
  354. ShttpOnBody,
  355. ShttpOnMessageComplete
  356. };
  357. #endif
  358. /**
  359. * A TCP connection and related state and buffers
  360. */
  361. struct TcpConnection
  362. {
  363. enum {
  364. TCP_UNCATEGORIZED_INCOMING, // uncategorized incoming connection
  365. TCP_HTTP_INCOMING,
  366. TCP_HTTP_OUTGOING,
  367. TCP_TUNNEL_OUTGOING // TUNNELED mode proxy outbound connection
  368. } type;
  369. OneServiceImpl *parent;
  370. PhySocket *sock;
  371. InetAddress remoteAddr;
  372. uint64_t lastReceive;
  373. // Used for inbound HTTP connections
  374. http_parser parser;
  375. unsigned long messageSize;
  376. std::string currentHeaderField;
  377. std::string currentHeaderValue;
  378. std::string url;
  379. std::string status;
  380. std::map< std::string,std::string > headers;
  381. std::string readq;
  382. std::string writeq;
  383. Mutex writeq_m;
  384. };
  385. struct OneServiceIncomingPacket
  386. {
  387. uint64_t now;
  388. int64_t sock;
  389. struct sockaddr_storage from;
  390. unsigned int size;
  391. uint8_t data[ZT_MAX_MTU];
  392. };
  393. class OneServiceImpl : public OneService
  394. {
  395. public:
  396. // begin member variables --------------------------------------------------
  397. const std::string _homePath;
  398. std::string _authToken;
  399. std::string _controllerDbPath;
  400. const std::string _networksPath;
  401. const std::string _moonsPath;
  402. EmbeddedNetworkController *_controller;
  403. Phy<OneServiceImpl *> _phy;
  404. Node *_node;
  405. SoftwareUpdater *_updater;
  406. PhySocket *_localControlSocket4;
  407. PhySocket *_localControlSocket6;
  408. bool _updateAutoApply;
  409. bool _allowTcpFallbackRelay;
  410. bool _allowSecondaryPort;
  411. unsigned int _primaryPort;
  412. unsigned int _secondaryPort;
  413. unsigned int _tertiaryPort;
  414. volatile unsigned int _udpPortPickerCounter;
  415. // Local configuration and memo-ized information from it
  416. json _localConfig;
  417. Hashtable< uint64_t,std::vector<InetAddress> > _v4Hints;
  418. Hashtable< uint64_t,std::vector<InetAddress> > _v6Hints;
  419. Hashtable< uint64_t,std::vector<InetAddress> > _v4Blacklists;
  420. Hashtable< uint64_t,std::vector<InetAddress> > _v6Blacklists;
  421. std::vector< InetAddress > _globalV4Blacklist;
  422. std::vector< InetAddress > _globalV6Blacklist;
  423. std::vector< InetAddress > _allowManagementFrom;
  424. std::vector< std::string > _interfacePrefixBlacklist;
  425. Mutex _localConfig_m;
  426. std::vector<InetAddress> explicitBind;
  427. /*
  428. * To attempt to handle NAT/gateway craziness we use three local UDP ports:
  429. *
  430. * [0] is the normal/default port, usually 9993
  431. * [1] is a port derived from our ZeroTier address
  432. * [2] is a port computed from the normal/default for use with uPnP/NAT-PMP mappings
  433. *
  434. * [2] exists because on some gateways trying to do regular NAT-t interferes
  435. * destructively with uPnP port mapping behavior in very weird buggy ways.
  436. * It's only used if uPnP/NAT-PMP is enabled in this build.
  437. */
  438. unsigned int _ports[3];
  439. Binder _binder;
  440. // Time we last received a packet from a global address
  441. uint64_t _lastDirectReceiveFromGlobal;
  442. #ifdef ZT_TCP_FALLBACK_RELAY
  443. uint64_t _lastSendToGlobalV4;
  444. #endif
  445. // Last potential sleep/wake event
  446. uint64_t _lastRestart;
  447. // Deadline for the next background task service function
  448. volatile int64_t _nextBackgroundTaskDeadline;
  449. // Configured networks
  450. struct NetworkState
  451. {
  452. NetworkState() :
  453. tap((EthernetTap *)0)
  454. {
  455. // Real defaults are in network 'up' code in network event handler
  456. settings.allowManaged = true;
  457. settings.allowGlobal = false;
  458. settings.allowDefault = false;
  459. settings.allowDNS = false;
  460. memset(&config, 0, sizeof(ZT_VirtualNetworkConfig));
  461. }
  462. std::shared_ptr<EthernetTap> tap;
  463. ZT_VirtualNetworkConfig config; // memcpy() of raw config from core
  464. std::vector<InetAddress> managedIps;
  465. std::list< SharedPtr<ManagedRoute> > managedRoutes;
  466. NetworkSettings settings;
  467. };
  468. std::map<uint64_t,NetworkState> _nets;
  469. Mutex _nets_m;
  470. // Active TCP/IP connections
  471. std::vector< TcpConnection * > _tcpConnections;
  472. Mutex _tcpConnections_m;
  473. TcpConnection *_tcpFallbackTunnel;
  474. // Termination status information
  475. ReasonForTermination _termReason;
  476. std::string _fatalErrorMessage;
  477. Mutex _termReason_m;
  478. // uPnP/NAT-PMP port mapper if enabled
  479. bool _portMappingEnabled; // local.conf settings
  480. #ifdef ZT_USE_MINIUPNPC
  481. PortMapper *_portMapper;
  482. #endif
  483. // HashiCorp Vault Settings
  484. #if ZT_VAULT_SUPPORT
  485. bool _vaultEnabled;
  486. std::string _vaultURL;
  487. std::string _vaultToken;
  488. std::string _vaultPath; // defaults to cubbyhole/zerotier/identity.secret for per-access key storage
  489. #endif
  490. // Set to false to force service to stop
  491. volatile bool _run;
  492. Mutex _run_m;
  493. RedisConfig *_rc;
  494. // end member variables ----------------------------------------------------
  495. OneServiceImpl(const char *hp,unsigned int port) :
  496. _homePath((hp) ? hp : ".")
  497. ,_controllerDbPath(_homePath + ZT_PATH_SEPARATOR_S "controller.d")
  498. ,_networksPath(_homePath + ZT_PATH_SEPARATOR_S "networks.d")
  499. ,_moonsPath(_homePath + ZT_PATH_SEPARATOR_S "moons.d")
  500. ,_controller((EmbeddedNetworkController *)0)
  501. ,_phy(this,false,true)
  502. ,_node((Node *)0)
  503. ,_updater((SoftwareUpdater *)0)
  504. ,_localControlSocket4((PhySocket *)0)
  505. ,_localControlSocket6((PhySocket *)0)
  506. ,_updateAutoApply(false)
  507. ,_primaryPort(port)
  508. ,_udpPortPickerCounter(0)
  509. ,_lastDirectReceiveFromGlobal(0)
  510. #ifdef ZT_TCP_FALLBACK_RELAY
  511. ,_lastSendToGlobalV4(0)
  512. #endif
  513. ,_lastRestart(0)
  514. ,_nextBackgroundTaskDeadline(0)
  515. ,_tcpFallbackTunnel((TcpConnection *)0)
  516. ,_termReason(ONE_STILL_RUNNING)
  517. ,_portMappingEnabled(true)
  518. #ifdef ZT_USE_MINIUPNPC
  519. ,_portMapper((PortMapper *)0)
  520. #endif
  521. #ifdef ZT_VAULT_SUPPORT
  522. ,_vaultEnabled(false)
  523. ,_vaultURL()
  524. ,_vaultToken()
  525. ,_vaultPath("cubbyhole/zerotier")
  526. #endif
  527. ,_run(true)
  528. ,_rc(NULL)
  529. {
  530. _ports[0] = 0;
  531. _ports[1] = 0;
  532. _ports[2] = 0;
  533. #if ZT_VAULT_SUPPORT
  534. curl_global_init(CURL_GLOBAL_DEFAULT);
  535. #endif
  536. }
  537. virtual ~OneServiceImpl()
  538. {
  539. _binder.closeAll(_phy);
  540. _phy.close(_localControlSocket4);
  541. _phy.close(_localControlSocket6);
  542. #if ZT_VAULT_SUPPORT
  543. curl_global_cleanup();
  544. #endif
  545. #ifdef ZT_USE_MINIUPNPC
  546. delete _portMapper;
  547. #endif
  548. delete _controller;
  549. delete _rc;
  550. }
  551. virtual ReasonForTermination run()
  552. {
  553. try {
  554. {
  555. const std::string authTokenPath(_homePath + ZT_PATH_SEPARATOR_S "authtoken.secret");
  556. if (!OSUtils::readFile(authTokenPath.c_str(),_authToken)) {
  557. unsigned char foo[24];
  558. Utils::getSecureRandom(foo,sizeof(foo));
  559. _authToken = "";
  560. for(unsigned int i=0;i<sizeof(foo);++i)
  561. _authToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  562. if (!OSUtils::writeFile(authTokenPath.c_str(),_authToken)) {
  563. Mutex::Lock _l(_termReason_m);
  564. _termReason = ONE_UNRECOVERABLE_ERROR;
  565. _fatalErrorMessage = "authtoken.secret could not be written";
  566. return _termReason;
  567. } else {
  568. OSUtils::lockDownFile(authTokenPath.c_str(),false);
  569. }
  570. }
  571. _authToken = _trimString(_authToken);
  572. }
  573. {
  574. struct ZT_Node_Callbacks cb;
  575. cb.version = 0;
  576. cb.stateGetFunction = SnodeStateGetFunction;
  577. cb.statePutFunction = SnodeStatePutFunction;
  578. cb.wirePacketSendFunction = SnodeWirePacketSendFunction;
  579. cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction;
  580. cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction;
  581. cb.eventCallback = SnodeEventCallback;
  582. cb.pathCheckFunction = SnodePathCheckFunction;
  583. cb.pathLookupFunction = SnodePathLookupFunction;
  584. _node = new Node(this,(void *)0,&cb,OSUtils::now());
  585. }
  586. // local.conf
  587. readLocalSettings();
  588. applyLocalConfig();
  589. // Make sure we can use the primary port, and hunt for one if configured to do so
  590. const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random
  591. for(int k=0;k<portTrials;++k) {
  592. if (_primaryPort == 0) {
  593. unsigned int randp = 0;
  594. Utils::getSecureRandom(&randp,sizeof(randp));
  595. _primaryPort = 20000 + (randp % 45500);
  596. }
  597. if (_trialBind(_primaryPort)) {
  598. _ports[0] = _primaryPort;
  599. } else {
  600. _primaryPort = 0;
  601. }
  602. }
  603. if (_ports[0] == 0) {
  604. Mutex::Lock _l(_termReason_m);
  605. _termReason = ONE_UNRECOVERABLE_ERROR;
  606. _fatalErrorMessage = "cannot bind to local control interface port";
  607. return _termReason;
  608. }
  609. // Bind TCP control socket to 127.0.0.1 and ::1 as well for loopback TCP control socket queries
  610. {
  611. struct sockaddr_in lo4;
  612. memset(&lo4,0,sizeof(lo4));
  613. lo4.sin_family = AF_INET;
  614. lo4.sin_addr.s_addr = Utils::hton((uint32_t)0x7f000001);
  615. lo4.sin_port = Utils::hton((uint16_t)_ports[0]);
  616. _localControlSocket4 = _phy.tcpListen((const struct sockaddr *)&lo4);
  617. struct sockaddr_in6 lo6;
  618. memset(&lo6,0,sizeof(lo6));
  619. lo6.sin6_family = AF_INET6;
  620. lo6.sin6_addr.s6_addr[15] = 1;
  621. lo6.sin6_port = lo4.sin_port;
  622. _localControlSocket6 = _phy.tcpListen((const struct sockaddr *)&lo6);
  623. }
  624. // Save primary port to a file so CLIs and GUIs can learn it easily
  625. char portstr[64];
  626. OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]);
  627. OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "zerotier-one.port").c_str(),std::string(portstr));
  628. // Attempt to bind to a secondary port chosen from our ZeroTier address.
  629. // This exists because there are buggy NATs out there that fail if more
  630. // than one device behind the same NAT tries to use the same internal
  631. // private address port number. Buggy NATs are a running theme.
  632. if (_allowSecondaryPort) {
  633. if (_secondaryPort) {
  634. _ports[1] = _secondaryPort;
  635. } else {
  636. _ports[1] = 20000 + ((unsigned int)_node->address() % 45500);
  637. for(int i=0;;++i) {
  638. if (i > 1000) {
  639. _ports[1] = 0;
  640. break;
  641. } else if (++_ports[1] >= 65536) {
  642. _ports[1] = 20000;
  643. }
  644. if (_trialBind(_ports[1]))
  645. break;
  646. }
  647. }
  648. }
  649. #ifdef ZT_USE_MINIUPNPC
  650. if (_portMappingEnabled) {
  651. // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
  652. // use the other two ports for that because some NATs do really funky
  653. // stuff with ports that are explicitly mapped that breaks things.
  654. if (_ports[1]) {
  655. if (_tertiaryPort) {
  656. _ports[2] = _tertiaryPort;
  657. } else {
  658. _ports[2] = _ports[1];
  659. for(int i=0;;++i) {
  660. if (i > 1000) {
  661. _ports[2] = 0;
  662. break;
  663. } else if (++_ports[2] >= 65536) {
  664. _ports[2] = 20000;
  665. }
  666. if (_trialBind(_ports[2]))
  667. break;
  668. }
  669. if (_ports[2]) {
  670. char uniqueName[64];
  671. OSUtils::ztsnprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
  672. _portMapper = new PortMapper(_ports[2],uniqueName);
  673. }
  674. }
  675. }
  676. }
  677. #endif
  678. // Delete legacy iddb.d if present (cleanup)
  679. OSUtils::rmDashRf((_homePath + ZT_PATH_SEPARATOR_S "iddb.d").c_str());
  680. // Network controller is now enabled by default for desktop and server
  681. _controller = new EmbeddedNetworkController(_node,_homePath.c_str(),_controllerDbPath.c_str(),_ports[0], _rc);
  682. _node->setNetconfMaster((void *)_controller);
  683. // Join existing networks in networks.d
  684. {
  685. std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str()));
  686. for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
  687. std::size_t dot = f->find_last_of('.');
  688. if ((dot == 16)&&(f->substr(16) == ".conf"))
  689. _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0,(void *)0);
  690. }
  691. }
  692. // Orbit existing moons in moons.d
  693. {
  694. std::vector<std::string> moonsDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "moons.d").c_str()));
  695. for(std::vector<std::string>::iterator f(moonsDotD.begin());f!=moonsDotD.end();++f) {
  696. std::size_t dot = f->find_last_of('.');
  697. if ((dot == 16)&&(f->substr(16) == ".moon"))
  698. _node->orbit((void *)0,Utils::hexStrToU64(f->substr(0,dot).c_str()),0);
  699. }
  700. }
  701. // Main I/O loop
  702. _nextBackgroundTaskDeadline = 0;
  703. int64_t clockShouldBe = OSUtils::now();
  704. _lastRestart = clockShouldBe;
  705. int64_t lastTapMulticastGroupCheck = 0;
  706. int64_t lastBindRefresh = 0;
  707. int64_t lastUpdateCheck = clockShouldBe;
  708. int64_t lastCleanedPeersDb = 0;
  709. int64_t lastLocalInterfaceAddressCheck = (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to configure and other things time to settle
  710. int64_t lastLocalConfFileCheck = OSUtils::now();
  711. for(;;) {
  712. _run_m.lock();
  713. if (!_run) {
  714. _run_m.unlock();
  715. _termReason_m.lock();
  716. _termReason = ONE_NORMAL_TERMINATION;
  717. _termReason_m.unlock();
  718. break;
  719. } else {
  720. _run_m.unlock();
  721. }
  722. const int64_t now = OSUtils::now();
  723. // Attempt to detect sleep/wake events by detecting delay overruns
  724. bool restarted = false;
  725. if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
  726. _lastRestart = now;
  727. restarted = true;
  728. }
  729. // Check for updates (if enabled)
  730. if ((_updater)&&((now - lastUpdateCheck) > 10000)) {
  731. lastUpdateCheck = now;
  732. if (_updater->check(now) && _updateAutoApply)
  733. _updater->apply();
  734. }
  735. // Reload local.conf if anything changed recently
  736. if ((now - lastLocalConfFileCheck) >= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  737. lastLocalConfFileCheck = now;
  738. struct stat result;
  739. if(stat((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(), &result)==0) {
  740. int64_t mod_time = result.st_mtime * 1000;
  741. if ((now - mod_time) <= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  742. readLocalSettings();
  743. applyLocalConfig();
  744. }
  745. }
  746. }
  747. // Refresh bindings in case device's interfaces have changed, and also sync routes to update any shadow routes (e.g. shadow default)
  748. if (((now - lastBindRefresh) >= (_node->bondController()->inUse() ? ZT_BINDER_REFRESH_PERIOD / 4 : ZT_BINDER_REFRESH_PERIOD))||(restarted)) {
  749. lastBindRefresh = now;
  750. unsigned int p[3];
  751. unsigned int pc = 0;
  752. for(int i=0;i<3;++i) {
  753. if (_ports[i])
  754. p[pc++] = _ports[i];
  755. }
  756. _binder.refresh(_phy,p,pc,explicitBind,*this);
  757. {
  758. Mutex::Lock _l(_nets_m);
  759. for(std::map<uint64_t,NetworkState>::iterator n(_nets.begin());n!=_nets.end();++n) {
  760. if (n->second.tap)
  761. syncManagedStuff(n->second,false,true,false);
  762. }
  763. }
  764. }
  765. // Run background task processor in core if it's time to do so
  766. int64_t dl = _nextBackgroundTaskDeadline;
  767. if (dl <= now) {
  768. _node->processBackgroundTasks((void *)0,now,&_nextBackgroundTaskDeadline);
  769. dl = _nextBackgroundTaskDeadline;
  770. }
  771. // Close TCP fallback tunnel if we have direct UDP
  772. if ((_tcpFallbackTunnel)&&((now - _lastDirectReceiveFromGlobal) < (ZT_TCP_FALLBACK_AFTER / 2)))
  773. _phy.close(_tcpFallbackTunnel->sock);
  774. // Sync multicast group memberships
  775. if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) {
  776. lastTapMulticastGroupCheck = now;
  777. std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > > mgChanges;
  778. {
  779. Mutex::Lock _l(_nets_m);
  780. mgChanges.reserve(_nets.size() + 1);
  781. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  782. if (n->second.tap) {
  783. 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> >()));
  784. n->second.tap->scanMulticastGroups(mgChanges.back().second.first,mgChanges.back().second.second);
  785. }
  786. }
  787. }
  788. for(std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > >::iterator c(mgChanges.begin());c!=mgChanges.end();++c) {
  789. for(std::vector<MulticastGroup>::iterator m(c->second.first.begin());m!=c->second.first.end();++m)
  790. _node->multicastSubscribe((void *)0,c->first,m->mac().toInt(),m->adi());
  791. for(std::vector<MulticastGroup>::iterator m(c->second.second.begin());m!=c->second.second.end();++m)
  792. _node->multicastUnsubscribe(c->first,m->mac().toInt(),m->adi());
  793. }
  794. }
  795. // Sync information about physical network interfaces
  796. if ((now - lastLocalInterfaceAddressCheck) >= (_node->bondController()->inUse() ? ZT_LOCAL_INTERFACE_CHECK_INTERVAL / 8 : ZT_LOCAL_INTERFACE_CHECK_INTERVAL)) {
  797. lastLocalInterfaceAddressCheck = now;
  798. _node->clearLocalInterfaceAddresses();
  799. #ifdef ZT_USE_MINIUPNPC
  800. if (_portMapper) {
  801. std::vector<InetAddress> mappedAddresses(_portMapper->get());
  802. for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
  803. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
  804. }
  805. #endif
  806. std::vector<InetAddress> boundAddrs(_binder.allBoundLocalInterfaceAddresses());
  807. for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i) {
  808. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
  809. }
  810. }
  811. // Clean peers.d periodically
  812. if ((now - lastCleanedPeersDb) >= 3600000) {
  813. lastCleanedPeersDb = now;
  814. OSUtils::cleanDirectory((_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(),now - 2592000000LL); // delete older than 30 days
  815. }
  816. const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100;
  817. clockShouldBe = now + (uint64_t)delay;
  818. _phy.poll(delay);
  819. }
  820. } catch (std::exception &e) {
  821. Mutex::Lock _l(_termReason_m);
  822. _termReason = ONE_UNRECOVERABLE_ERROR;
  823. _fatalErrorMessage = std::string("unexpected exception in main thread: ")+e.what();
  824. } catch ( ... ) {
  825. Mutex::Lock _l(_termReason_m);
  826. _termReason = ONE_UNRECOVERABLE_ERROR;
  827. _fatalErrorMessage = "unexpected exception in main thread: unknown exception";
  828. }
  829. try {
  830. Mutex::Lock _l(_tcpConnections_m);
  831. while (!_tcpConnections.empty())
  832. _phy.close((*_tcpConnections.begin())->sock);
  833. } catch ( ... ) {}
  834. {
  835. Mutex::Lock _l(_nets_m);
  836. _nets.clear();
  837. }
  838. delete _updater;
  839. _updater = (SoftwareUpdater *)0;
  840. delete _node;
  841. _node = (Node *)0;
  842. return _termReason;
  843. }
  844. void readLocalSettings()
  845. {
  846. // Read local configuration
  847. std::map<InetAddress,ZT_PhysicalPathConfiguration> ppc;
  848. // LEGACY: support old "trustedpaths" flat file
  849. FILE *trustpaths = fopen((_homePath + ZT_PATH_SEPARATOR_S "trustedpaths").c_str(),"r");
  850. if (trustpaths) {
  851. fprintf(stderr,"WARNING: 'trustedpaths' flat file format is deprecated in favor of path definitions in local.conf" ZT_EOL_S);
  852. char buf[1024];
  853. while (fgets(buf,sizeof(buf),trustpaths)) {
  854. int fno = 0;
  855. char *saveptr = (char *)0;
  856. uint64_t trustedPathId = 0;
  857. InetAddress trustedPathNetwork;
  858. for(char *f=Utils::stok(buf,"=\r\n \t",&saveptr);(f);f=Utils::stok((char *)0,"=\r\n \t",&saveptr)) {
  859. if (fno == 0) {
  860. trustedPathId = Utils::hexStrToU64(f);
  861. } else if (fno == 1) {
  862. trustedPathNetwork = InetAddress(f);
  863. } else break;
  864. ++fno;
  865. }
  866. if ( (trustedPathId != 0) && ((trustedPathNetwork.ss_family == AF_INET)||(trustedPathNetwork.ss_family == AF_INET6)) && (trustedPathNetwork.netmaskBits() > 0) ) {
  867. ppc[trustedPathNetwork].trustedPathId = trustedPathId;
  868. ppc[trustedPathNetwork].mtu = 0; // use default
  869. }
  870. }
  871. fclose(trustpaths);
  872. }
  873. // Read local config file
  874. Mutex::Lock _l2(_localConfig_m);
  875. std::string lcbuf;
  876. if (OSUtils::readFile((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(),lcbuf)) {
  877. if (lcbuf.length() > 0) {
  878. try {
  879. _localConfig = OSUtils::jsonParse(lcbuf);
  880. if (!_localConfig.is_object()) {
  881. fprintf(stderr,"ERROR: unable to parse local.conf (root element is not a JSON object)" ZT_EOL_S);
  882. exit(1);
  883. }
  884. } catch ( ... ) {
  885. fprintf(stderr,"ERROR: unable to parse local.conf (invalid JSON)" ZT_EOL_S);
  886. exit(1);
  887. }
  888. }
  889. }
  890. // Get any trusted paths in local.conf (we'll parse the rest of physical[] elsewhere)
  891. json &physical = _localConfig["physical"];
  892. if (physical.is_object()) {
  893. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  894. InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  895. if (net) {
  896. if (phy.value().is_object()) {
  897. uint64_t tpid;
  898. if ((tpid = OSUtils::jsonInt(phy.value()["trustedPathId"],0ULL)) != 0ULL) {
  899. if ((net.ss_family == AF_INET)||(net.ss_family == AF_INET6))
  900. ppc[net].trustedPathId = tpid;
  901. }
  902. ppc[net].mtu = (int)OSUtils::jsonInt(phy.value()["mtu"],0ULL); // 0 means use default
  903. }
  904. }
  905. }
  906. }
  907. json &settings = _localConfig["settings"];
  908. if (settings.is_object()) {
  909. // Allow controller DB path to be put somewhere else
  910. const std::string cdbp(OSUtils::jsonString(settings["controllerDbPath"],""));
  911. if (cdbp.length() > 0)
  912. _controllerDbPath = cdbp;
  913. #ifdef ZT_CONTROLLER_USE_LIBPQ
  914. // TODO: Redis config
  915. json &redis = settings["redis"];
  916. if (redis.is_object() && _rc == NULL) {
  917. _rc = new RedisConfig;
  918. _rc->hostname = OSUtils::jsonString(redis["hostname"],"");
  919. _rc->port = redis["port"];
  920. _rc->password = OSUtils::jsonString(redis["password"],"");
  921. _rc->clusterMode = OSUtils::jsonBool(redis["clusterMode"], false);
  922. }
  923. #endif
  924. // Bind to wildcard instead of to specific interfaces (disables full tunnel capability)
  925. json &bind = settings["bind"];
  926. if (bind.is_array()) {
  927. for(unsigned long i=0;i<bind.size();++i) {
  928. const std::string ips(OSUtils::jsonString(bind[i],""));
  929. if (ips.length() > 0) {
  930. InetAddress ip(ips.c_str());
  931. if ((ip.ss_family == AF_INET)||(ip.ss_family == AF_INET6))
  932. explicitBind.push_back(ip);
  933. }
  934. }
  935. }
  936. }
  937. // Set trusted paths if there are any
  938. if (ppc.size() > 0) {
  939. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::iterator i(ppc.begin());i!=ppc.end();++i)
  940. _node->setPhysicalPathConfiguration(reinterpret_cast<const struct sockaddr_storage *>(&(i->first)),&(i->second));
  941. }
  942. }
  943. virtual ReasonForTermination reasonForTermination() const
  944. {
  945. Mutex::Lock _l(_termReason_m);
  946. return _termReason;
  947. }
  948. virtual std::string fatalErrorMessage() const
  949. {
  950. Mutex::Lock _l(_termReason_m);
  951. return _fatalErrorMessage;
  952. }
  953. virtual std::string portDeviceName(uint64_t nwid) const
  954. {
  955. Mutex::Lock _l(_nets_m);
  956. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  957. if ((n != _nets.end())&&(n->second.tap))
  958. return n->second.tap->deviceName();
  959. else return std::string();
  960. }
  961. #ifdef ZT_SDK
  962. virtual std::string givenHomePath()
  963. {
  964. return _homePath;
  965. }
  966. void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes)
  967. {
  968. Mutex::Lock _l(_nets_m);
  969. NetworkState &n = _nets[nwid];
  970. *numRoutes = *numRoutes < n.config.routeCount ? *numRoutes : n.config.routeCount;
  971. for(unsigned int i=0; i<*numRoutes; i++) {
  972. ZT_VirtualNetworkRoute *vnr = (ZT_VirtualNetworkRoute*)routeArray;
  973. memcpy(&vnr[i], &(n.config.routes[i]), sizeof(ZT_VirtualNetworkRoute));
  974. }
  975. }
  976. virtual Node *getNode()
  977. {
  978. return _node;
  979. }
  980. #endif // ZT_SDK
  981. virtual void terminate()
  982. {
  983. _run_m.lock();
  984. _run = false;
  985. _run_m.unlock();
  986. _phy.whack();
  987. }
  988. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const
  989. {
  990. Mutex::Lock _l(_nets_m);
  991. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  992. if (n == _nets.end())
  993. return false;
  994. settings = n->second.settings;
  995. return true;
  996. }
  997. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings)
  998. {
  999. Mutex::Lock _l(_nets_m);
  1000. std::map<uint64_t,NetworkState>::iterator n(_nets.find(nwid));
  1001. if (n == _nets.end())
  1002. return false;
  1003. n->second.settings = settings;
  1004. char nlcpath[4096];
  1005. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid);
  1006. FILE *out = fopen(nlcpath,"w");
  1007. if (out) {
  1008. fprintf(out,"allowManaged=%d\n",(int)n->second.settings.allowManaged);
  1009. fprintf(out,"allowGlobal=%d\n",(int)n->second.settings.allowGlobal);
  1010. fprintf(out,"allowDefault=%d\n",(int)n->second.settings.allowDefault);
  1011. fprintf(out,"allowDNS=%d\n",(int)n->second.settings.allowDNS);
  1012. fclose(out);
  1013. }
  1014. if (n->second.tap)
  1015. syncManagedStuff(n->second,true,true,true);
  1016. return true;
  1017. }
  1018. // =========================================================================
  1019. // Internal implementation methods for control plane, route setup, etc.
  1020. // =========================================================================
  1021. inline unsigned int handleControlPlaneHttpRequest(
  1022. const InetAddress &fromAddress,
  1023. unsigned int httpMethod,
  1024. const std::string &path,
  1025. const std::map<std::string,std::string> &headers,
  1026. const std::string &body,
  1027. std::string &responseBody,
  1028. std::string &responseContentType)
  1029. {
  1030. char tmp[256];
  1031. unsigned int scode = 404;
  1032. json res;
  1033. std::vector<std::string> ps(OSUtils::split(path.c_str(),"/","",""));
  1034. std::map<std::string,std::string> urlArgs;
  1035. /* Note: this is kind of restricted in what it'll take. It does not support
  1036. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  1037. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  1038. * paths to simply-named resources. */
  1039. if (ps.size() > 0) {
  1040. std::size_t qpos = ps[ps.size() - 1].find('?');
  1041. if (qpos != std::string::npos) {
  1042. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  1043. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  1044. std::vector<std::string> asplit(OSUtils::split(args.c_str(),"&","",""));
  1045. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  1046. std::size_t eqpos = a->find('=');
  1047. if (eqpos == std::string::npos)
  1048. urlArgs[*a] = "";
  1049. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  1050. }
  1051. }
  1052. } else {
  1053. return 404;
  1054. }
  1055. bool isAuth = false;
  1056. {
  1057. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  1058. if ((ah != headers.end())&&(_authToken == ah->second)) {
  1059. isAuth = true;
  1060. } else {
  1061. ah = urlArgs.find("auth");
  1062. if ((ah != urlArgs.end())&&(_authToken == ah->second))
  1063. isAuth = true;
  1064. }
  1065. }
  1066. #ifdef __SYNOLOGY__
  1067. // Authenticate via Synology's built-in cgi script
  1068. if (!isAuth) {
  1069. int synotoken_pos = path.find("SynoToken");
  1070. int argpos = path.find("?");
  1071. if(synotoken_pos != std::string::npos && argpos != std::string::npos) {
  1072. std::string cookie = path.substr(argpos+1, synotoken_pos-(argpos+1));
  1073. std::string synotoken = path.substr(synotoken_pos);
  1074. std::string cookie_val = cookie.substr(cookie.find("=")+1);
  1075. std::string synotoken_val = synotoken.substr(synotoken.find("=")+1);
  1076. // Set necessary env for auth script
  1077. std::map<std::string,std::string>::const_iterator ah2(headers.find("x-forwarded-for"));
  1078. setenv("HTTP_COOKIE", cookie_val.c_str(), true);
  1079. setenv("HTTP_X_SYNO_TOKEN", synotoken_val.c_str(), true);
  1080. setenv("REMOTE_ADDR", ah2->second.c_str(),true);
  1081. char user[256], buf[1024];
  1082. FILE *fp = NULL;
  1083. bzero(user, 256);
  1084. fp = popen("/usr/syno/synoman/webman/modules/authenticate.cgi", "r");
  1085. if(!fp)
  1086. isAuth = false;
  1087. else {
  1088. bzero(buf, sizeof(buf));
  1089. fread(buf, 1024, 1, fp);
  1090. if(strlen(buf) > 0) {
  1091. snprintf(user, 256, "%s", buf);
  1092. isAuth = true;
  1093. }
  1094. }
  1095. pclose(fp);
  1096. }
  1097. }
  1098. #endif
  1099. if (httpMethod == HTTP_GET) {
  1100. if (isAuth) {
  1101. if (ps[0] == "status") {
  1102. ZT_NodeStatus status;
  1103. _node->status(&status);
  1104. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",status.address);
  1105. res["address"] = tmp;
  1106. res["publicIdentity"] = status.publicIdentity;
  1107. res["online"] = (bool)(status.online != 0);
  1108. res["tcpFallbackActive"] = (_tcpFallbackTunnel != (TcpConnection *)0);
  1109. res["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  1110. res["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  1111. res["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  1112. res["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  1113. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  1114. res["version"] = tmp;
  1115. res["clock"] = OSUtils::now();
  1116. {
  1117. Mutex::Lock _l(_localConfig_m);
  1118. res["config"] = _localConfig;
  1119. }
  1120. json &settings = res["config"]["settings"];
  1121. settings["primaryPort"] = OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1122. settings["allowTcpFallbackRelay"] = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],_allowTcpFallbackRelay);
  1123. if (_node->bondController()->inUse()) {
  1124. json &multipathConfig = res["bonds"];
  1125. ZT_PeerList *pl = _node->peers();
  1126. char peerAddrStr[256];
  1127. if (pl) {
  1128. for(unsigned long i=0;i<pl->peerCount;++i) {
  1129. if (pl->peers[i].isBonded) {
  1130. nlohmann::json pj;
  1131. _peerBondToJson(pj,&(pl->peers[i]));
  1132. OSUtils::ztsnprintf(peerAddrStr,sizeof(peerAddrStr),"%.10llx",pl->peers[i].address);
  1133. multipathConfig[peerAddrStr] = (pj);
  1134. }
  1135. }
  1136. }
  1137. }
  1138. #ifdef ZT_USE_MINIUPNPC
  1139. settings["portMappingEnabled"] = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1140. #else
  1141. settings["portMappingEnabled"] = false; // not supported in build
  1142. #endif
  1143. #ifndef ZT_SDK
  1144. settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
  1145. settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
  1146. #endif
  1147. const World planet(_node->planet());
  1148. res["planetWorldId"] = planet.id();
  1149. res["planetWorldTimestamp"] = planet.timestamp();
  1150. scode = 200;
  1151. } else if (ps[0] == "moon") {
  1152. std::vector<World> moons(_node->moons());
  1153. if (ps.size() == 1) {
  1154. // Return [array] of all moons
  1155. res = json::array();
  1156. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1157. json mj;
  1158. _moonToJson(mj,*m);
  1159. res.push_back(mj);
  1160. }
  1161. scode = 200;
  1162. } else {
  1163. // Return a single moon by ID
  1164. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  1165. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1166. if (m->id() == id) {
  1167. _moonToJson(res,*m);
  1168. scode = 200;
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. } else if (ps[0] == "network") {
  1174. ZT_VirtualNetworkList *nws = _node->networks();
  1175. if (nws) {
  1176. if (ps.size() == 1) {
  1177. // Return [array] of all networks
  1178. res = nlohmann::json::array();
  1179. for(unsigned long i=0;i<nws->networkCount;++i) {
  1180. OneService::NetworkSettings localSettings;
  1181. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1182. nlohmann::json nj;
  1183. _networkToJson(nj,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1184. res.push_back(nj);
  1185. }
  1186. scode = 200;
  1187. } else if (ps.size() == 2) {
  1188. // Return a single network by ID or 404 if not found
  1189. const uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1190. for(unsigned long i=0;i<nws->networkCount;++i) {
  1191. if (nws->networks[i].nwid == wantnw) {
  1192. OneService::NetworkSettings localSettings;
  1193. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1194. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1195. scode = 200;
  1196. break;
  1197. }
  1198. }
  1199. } else scode = 404;
  1200. _node->freeQueryResult((void *)nws);
  1201. } else scode = 500;
  1202. } else if (ps[0] == "peer") {
  1203. ZT_PeerList *pl = _node->peers();
  1204. if (pl) {
  1205. if (ps.size() == 1) {
  1206. // Return [array] of all peers
  1207. res = nlohmann::json::array();
  1208. for(unsigned long i=0;i<pl->peerCount;++i) {
  1209. nlohmann::json pj;
  1210. _peerToJson(pj,&(pl->peers[i]));
  1211. res.push_back(pj);
  1212. }
  1213. scode = 200;
  1214. } else if (ps.size() == 2) {
  1215. // Return a single peer by ID or 404 if not found
  1216. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  1217. for(unsigned long i=0;i<pl->peerCount;++i) {
  1218. if (pl->peers[i].address == wantp) {
  1219. _peerToJson(res,&(pl->peers[i]));
  1220. scode = 200;
  1221. break;
  1222. }
  1223. }
  1224. } else scode = 404;
  1225. _node->freeQueryResult((void *)pl);
  1226. } else scode = 500;
  1227. } else if (ps[0] == "bonds") {
  1228. ZT_PeerList *pl = _node->peers();
  1229. if (pl) {
  1230. if (ps.size() == 1) {
  1231. // Return [array] of all peers
  1232. res = nlohmann::json::array();
  1233. for(unsigned long i=0;i<pl->peerCount;++i) {
  1234. nlohmann::json pj;
  1235. _peerToJson(pj,&(pl->peers[i]));
  1236. res.push_back(pj);
  1237. }
  1238. scode = 200;
  1239. } else if (ps.size() == 2) {
  1240. // Return a single peer by ID or 404 if not found
  1241. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  1242. for(unsigned long i=0;i<pl->peerCount;++i) {
  1243. if (pl->peers[i].address == wantp) {
  1244. _peerToJson(res,&(pl->peers[i]));
  1245. scode = 200;
  1246. break;
  1247. }
  1248. }
  1249. } else scode = 404;
  1250. _node->freeQueryResult((void *)pl);
  1251. } else scode = 500;
  1252. } else {
  1253. if (_controller) {
  1254. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1255. } else scode = 404;
  1256. }
  1257. } else scode = 401; // isAuth == false
  1258. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  1259. if (isAuth) {
  1260. if (ps[0] == "moon") {
  1261. if (ps.size() == 2) {
  1262. uint64_t seed = 0;
  1263. try {
  1264. json j(OSUtils::jsonParse(body));
  1265. if (j.is_object()) {
  1266. seed = Utils::hexStrToU64(OSUtils::jsonString(j["seed"],"0").c_str());
  1267. }
  1268. } catch ( ... ) {
  1269. // discard invalid JSON
  1270. }
  1271. std::vector<World> moons(_node->moons());
  1272. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  1273. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1274. if (m->id() == id) {
  1275. _moonToJson(res,*m);
  1276. scode = 200;
  1277. break;
  1278. }
  1279. }
  1280. if ((scode != 200)&&(seed != 0)) {
  1281. char tmp[64];
  1282. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",id);
  1283. res["id"] = tmp;
  1284. res["roots"] = json::array();
  1285. res["timestamp"] = 0;
  1286. res["signature"] = json();
  1287. res["updatesMustBeSignedBy"] = json();
  1288. res["waiting"] = true;
  1289. _node->orbit((void *)0,id,seed);
  1290. scode = 200;
  1291. }
  1292. } else scode = 404;
  1293. } else if (ps[0] == "network") {
  1294. if (ps.size() == 2) {
  1295. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1296. _node->join(wantnw,(void *)0,(void *)0); // does nothing if we are a member
  1297. ZT_VirtualNetworkList *nws = _node->networks();
  1298. if (nws) {
  1299. for(unsigned long i=0;i<nws->networkCount;++i) {
  1300. if (nws->networks[i].nwid == wantnw) {
  1301. OneService::NetworkSettings localSettings;
  1302. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1303. try {
  1304. json j(OSUtils::jsonParse(body));
  1305. if (j.is_object()) {
  1306. json &allowManaged = j["allowManaged"];
  1307. if (allowManaged.is_boolean()) localSettings.allowManaged = (bool)allowManaged;
  1308. json &allowGlobal = j["allowGlobal"];
  1309. if (allowGlobal.is_boolean()) localSettings.allowGlobal = (bool)allowGlobal;
  1310. json &allowDefault = j["allowDefault"];
  1311. if (allowDefault.is_boolean()) localSettings.allowDefault = (bool)allowDefault;
  1312. json &allowDNS = j["allowDNS"];
  1313. if (allowDNS.is_boolean()) localSettings.allowDNS = (bool)allowDNS;
  1314. }
  1315. } catch ( ... ) {
  1316. // discard invalid JSON
  1317. }
  1318. setNetworkSettings(nws->networks[i].nwid,localSettings);
  1319. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1320. scode = 200;
  1321. break;
  1322. }
  1323. }
  1324. _node->freeQueryResult((void *)nws);
  1325. } else scode = 500;
  1326. } else scode = 404;
  1327. } else {
  1328. if (_controller)
  1329. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1330. else scode = 404;
  1331. }
  1332. } else scode = 401; // isAuth == false
  1333. } else if (httpMethod == HTTP_DELETE) {
  1334. if (isAuth) {
  1335. if (ps[0] == "moon") {
  1336. if (ps.size() == 2) {
  1337. _node->deorbit((void *)0,Utils::hexStrToU64(ps[1].c_str()));
  1338. res["result"] = true;
  1339. scode = 200;
  1340. } // else 404
  1341. } else if (ps[0] == "network") {
  1342. ZT_VirtualNetworkList *nws = _node->networks();
  1343. if (nws) {
  1344. if (ps.size() == 2) {
  1345. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1346. for(unsigned long i=0;i<nws->networkCount;++i) {
  1347. if (nws->networks[i].nwid == wantnw) {
  1348. _node->leave(wantnw,(void **)0,(void *)0);
  1349. res["result"] = true;
  1350. scode = 200;
  1351. break;
  1352. }
  1353. }
  1354. } // else 404
  1355. _node->freeQueryResult((void *)nws);
  1356. } else scode = 500;
  1357. } else {
  1358. if (_controller)
  1359. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1360. else scode = 404;
  1361. }
  1362. } else scode = 401; // isAuth = false
  1363. } else {
  1364. scode = 400;
  1365. }
  1366. if (responseBody.length() == 0) {
  1367. if ((res.is_object())||(res.is_array()))
  1368. responseBody = OSUtils::jsonDump(res);
  1369. else responseBody = "{}";
  1370. responseContentType = "application/json";
  1371. }
  1372. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  1373. // Also double-check isAuth since forbidding this without auth feels safer.
  1374. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  1375. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  1376. if (responseBody.length() > 0)
  1377. responseBody = jsonp->second + "(" + responseBody + ");";
  1378. else responseBody = jsonp->second + "(null);";
  1379. responseContentType = "application/javascript";
  1380. }
  1381. return scode;
  1382. }
  1383. // Must be called after _localConfig is read or modified
  1384. void applyLocalConfig()
  1385. {
  1386. Mutex::Lock _l(_localConfig_m);
  1387. json lc(_localConfig);
  1388. _v4Hints.clear();
  1389. _v6Hints.clear();
  1390. _v4Blacklists.clear();
  1391. _v6Blacklists.clear();
  1392. json &virt = lc["virtual"];
  1393. if (virt.is_object()) {
  1394. for(json::iterator v(virt.begin());v!=virt.end();++v) {
  1395. const std::string nstr = v.key();
  1396. if ((nstr.length() == ZT_ADDRESS_LENGTH_HEX)&&(v.value().is_object())) {
  1397. const Address ztaddr(Utils::hexStrToU64(nstr.c_str()));
  1398. if (ztaddr) {
  1399. const uint64_t ztaddr2 = ztaddr.toInt();
  1400. std::vector<InetAddress> &v4h = _v4Hints[ztaddr2];
  1401. std::vector<InetAddress> &v6h = _v6Hints[ztaddr2];
  1402. std::vector<InetAddress> &v4b = _v4Blacklists[ztaddr2];
  1403. std::vector<InetAddress> &v6b = _v6Blacklists[ztaddr2];
  1404. json &tryAddrs = v.value()["try"];
  1405. if (tryAddrs.is_array()) {
  1406. for(unsigned long i=0;i<tryAddrs.size();++i) {
  1407. const InetAddress ip(OSUtils::jsonString(tryAddrs[i],"").c_str());
  1408. if (ip.ss_family == AF_INET)
  1409. v4h.push_back(ip);
  1410. else if (ip.ss_family == AF_INET6)
  1411. v6h.push_back(ip);
  1412. }
  1413. }
  1414. json &blAddrs = v.value()["blacklist"];
  1415. if (blAddrs.is_array()) {
  1416. for(unsigned long i=0;i<blAddrs.size();++i) {
  1417. const InetAddress ip(OSUtils::jsonString(blAddrs[i],"").c_str());
  1418. if (ip.ss_family == AF_INET)
  1419. v4b.push_back(ip);
  1420. else if (ip.ss_family == AF_INET6)
  1421. v6b.push_back(ip);
  1422. }
  1423. }
  1424. if (v4h.empty()) _v4Hints.erase(ztaddr2);
  1425. if (v6h.empty()) _v6Hints.erase(ztaddr2);
  1426. if (v4b.empty()) _v4Blacklists.erase(ztaddr2);
  1427. if (v6b.empty()) _v6Blacklists.erase(ztaddr2);
  1428. }
  1429. }
  1430. }
  1431. }
  1432. _globalV4Blacklist.clear();
  1433. _globalV6Blacklist.clear();
  1434. json &physical = lc["physical"];
  1435. if (physical.is_object()) {
  1436. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  1437. const InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  1438. if ((net)&&(net.netmaskBits() > 0)) {
  1439. if (phy.value().is_object()) {
  1440. if (OSUtils::jsonBool(phy.value()["blacklist"],false)) {
  1441. if (net.ss_family == AF_INET)
  1442. _globalV4Blacklist.push_back(net);
  1443. else if (net.ss_family == AF_INET6)
  1444. _globalV6Blacklist.push_back(net);
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. _allowManagementFrom.clear();
  1451. _interfacePrefixBlacklist.clear();
  1452. json &settings = lc["settings"];
  1453. if (!_node->bondController()->inUse()) {
  1454. // defaultBondingPolicy
  1455. std::string defaultBondingPolicyStr(OSUtils::jsonString(settings["defaultBondingPolicy"],""));
  1456. int defaultBondingPolicy = _node->bondController()->getPolicyCodeByStr(defaultBondingPolicyStr);
  1457. _node->bondController()->setBondingLayerDefaultPolicy(defaultBondingPolicy);
  1458. _node->bondController()->setBondingLayerDefaultPolicyStr(defaultBondingPolicyStr); // Used if custom policy
  1459. // Custom Policies
  1460. json &customBondingPolicies = settings["policies"];
  1461. for (json::iterator policyItr = customBondingPolicies.begin(); policyItr != customBondingPolicies.end();++policyItr) {
  1462. // Custom Policy
  1463. std::string customPolicyStr(policyItr.key());
  1464. json &customPolicy = policyItr.value();
  1465. std::string basePolicyStr(OSUtils::jsonString(customPolicy["basePolicy"],""));
  1466. if (_node->bondController()->getPolicyCodeByStr(basePolicyStr) == ZT_BONDING_POLICY_NONE) {
  1467. fprintf(stderr, "error: custom policy (%s) is invalid, unknown base policy (%s).\n",
  1468. customPolicyStr.c_str(), basePolicyStr.c_str());
  1469. continue;
  1470. } if (_node->bondController()->getPolicyCodeByStr(customPolicyStr) != ZT_BONDING_POLICY_NONE) {
  1471. fprintf(stderr, "error: custom policy (%s) will be ignored, cannot use standard policy names for custom policies.\n",
  1472. customPolicyStr.c_str());
  1473. continue;
  1474. }
  1475. // New bond, used as a copy template for new instances
  1476. SharedPtr<Bond> newTemplateBond = new Bond(NULL, basePolicyStr, customPolicyStr, SharedPtr<Peer>());
  1477. // Acceptable ranges
  1478. newTemplateBond->setMaxAcceptableLatency(OSUtils::jsonInt(customPolicy["maxAcceptableLatency"],-1));
  1479. newTemplateBond->setMaxAcceptableMeanLatency(OSUtils::jsonInt(customPolicy["maxAcceptableMeanLatency"],-1));
  1480. newTemplateBond->setMaxAcceptablePacketDelayVariance(OSUtils::jsonInt(customPolicy["maxAcceptablePacketDelayVariance"],-1));
  1481. newTemplateBond->setMaxAcceptablePacketLossRatio((float)OSUtils::jsonDouble(customPolicy["maxAcceptablePacketLossRatio"],-1));
  1482. newTemplateBond->setMaxAcceptablePacketErrorRatio((float)OSUtils::jsonDouble(customPolicy["maxAcceptablePacketErrorRatio"],-1));
  1483. newTemplateBond->setMinAcceptableAllocation((float)OSUtils::jsonDouble(customPolicy["minAcceptableAllocation"],0));
  1484. // Quality weights
  1485. json &qualityWeights = customPolicy["qualityWeights"];
  1486. if (qualityWeights.size() == ZT_QOS_WEIGHT_SIZE) { // TODO: Generalize this
  1487. float weights[ZT_QOS_WEIGHT_SIZE];
  1488. weights[ZT_QOS_LAT_IDX] = (float)OSUtils::jsonDouble(qualityWeights["lat"],0.0);
  1489. weights[ZT_QOS_LTM_IDX] = (float)OSUtils::jsonDouble(qualityWeights["ltm"],0.0);
  1490. weights[ZT_QOS_PDV_IDX] = (float)OSUtils::jsonDouble(qualityWeights["pdv"],0.0);
  1491. weights[ZT_QOS_PLR_IDX] = (float)OSUtils::jsonDouble(qualityWeights["plr"],0.0);
  1492. weights[ZT_QOS_PER_IDX] = (float)OSUtils::jsonDouble(qualityWeights["per"],0.0);
  1493. weights[ZT_QOS_THR_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thr"],0.0);
  1494. weights[ZT_QOS_THM_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thm"],0.0);
  1495. weights[ZT_QOS_THV_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thv"],0.0);
  1496. newTemplateBond->setUserQualityWeights(weights,ZT_QOS_WEIGHT_SIZE);
  1497. }
  1498. // Bond-specific properties
  1499. newTemplateBond->setUpDelay(OSUtils::jsonInt(customPolicy["upDelay"],-1));
  1500. newTemplateBond->setDownDelay(OSUtils::jsonInt(customPolicy["downDelay"],-1));
  1501. newTemplateBond->setFlowRebalanceStrategy(OSUtils::jsonInt(customPolicy["flowRebalanceStrategy"],(uint64_t)0));
  1502. newTemplateBond->setFailoverInterval(OSUtils::jsonInt(customPolicy["failoverInterval"],(uint64_t)0));
  1503. newTemplateBond->setPacketsPerLink(OSUtils::jsonInt(customPolicy["packetsPerLink"],-1));
  1504. std::string linkMonitorStrategyStr(OSUtils::jsonString(customPolicy["linkMonitorStrategy"],""));
  1505. uint8_t linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DEFAULT;
  1506. if (linkMonitorStrategyStr == "passive") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_PASSIVE; }
  1507. if (linkMonitorStrategyStr == "active") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_ACTIVE; }
  1508. if (linkMonitorStrategyStr == "dynamic") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DYNAMIC; }
  1509. newTemplateBond->setLinkMonitorStrategy(linkMonitorStrategy);
  1510. // Policy-Specific link set
  1511. json &links = customPolicy["links"];
  1512. for (json::iterator linkItr = links.begin(); linkItr != links.end();++linkItr) {
  1513. std::string linkNameStr(linkItr.key());
  1514. json &link = linkItr.value();
  1515. bool enabled = OSUtils::jsonInt(link["enabled"],true);
  1516. uint32_t speed = OSUtils::jsonInt(link["speed"],0);
  1517. float alloc = (float)OSUtils::jsonDouble(link["alloc"],0);
  1518. if (speed && alloc) {
  1519. fprintf(stderr, "error: cannot specify both speed (%d) and alloc (%f) for link (%s), pick one, link disabled.\n",
  1520. speed, alloc, linkNameStr.c_str());
  1521. enabled = false;
  1522. }
  1523. uint32_t upDelay = OSUtils::jsonInt(link["upDelay"],-1);
  1524. uint32_t downDelay = OSUtils::jsonInt(link["downDelay"],-1);
  1525. uint8_t ipvPref = OSUtils::jsonInt(link["ipvPref"],0);
  1526. uint32_t linkMonitorInterval = OSUtils::jsonInt(link["monitorInterval"],(uint64_t)0);
  1527. std::string failoverToStr(OSUtils::jsonString(link["failoverTo"],""));
  1528. // Mode
  1529. std::string linkModeStr(OSUtils::jsonString(link["mode"],"spare"));
  1530. uint8_t linkMode = ZT_MULTIPATH_SLAVE_MODE_SPARE;
  1531. if (linkModeStr == "primary") { linkMode = ZT_MULTIPATH_SLAVE_MODE_PRIMARY; }
  1532. if (linkModeStr == "spare") { linkMode = ZT_MULTIPATH_SLAVE_MODE_SPARE; }
  1533. // ipvPref
  1534. if ((ipvPref != 0) && (ipvPref != 4) && (ipvPref != 6) && (ipvPref != 46) && (ipvPref != 64)) {
  1535. fprintf(stderr, "error: invalid ipvPref value (%d), link disabled.\n", ipvPref);
  1536. enabled = false;
  1537. }
  1538. if (linkMode == ZT_MULTIPATH_SLAVE_MODE_SPARE && failoverToStr.length()) {
  1539. fprintf(stderr, "error: cannot specify failover links for spares, link disabled.\n");
  1540. failoverToStr = "";
  1541. enabled = false;
  1542. }
  1543. _node->bondController()->addCustomLink(customPolicyStr, new Link(linkNameStr,ipvPref,speed,linkMonitorInterval,upDelay,downDelay,enabled,linkMode,failoverToStr,alloc));
  1544. }
  1545. std::string linkSelectMethodStr(OSUtils::jsonString(customPolicy["activeReselect"],"optimize"));
  1546. if (linkSelectMethodStr == "always") {
  1547. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_ALWAYS);
  1548. }
  1549. if (linkSelectMethodStr == "better") {
  1550. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_BETTER);
  1551. }
  1552. if (linkSelectMethodStr == "failure") {
  1553. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_FAILURE);
  1554. }
  1555. if (linkSelectMethodStr == "optimize") {
  1556. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_OPTIMIZE);
  1557. }
  1558. if (newTemplateBond->getLinkSelectMethod() < 0 || newTemplateBond->getLinkSelectMethod() > 3) {
  1559. fprintf(stderr, "warning: invalid value (%s) for linkSelectMethod, assuming mode: always\n", linkSelectMethodStr.c_str());
  1560. }
  1561. /*
  1562. newBond->setPolicy(_node->bondController()->getPolicyCodeByStr(basePolicyStr));
  1563. newBond->setFlowHashing((bool)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["allowFlowHashing"],(bool)allowFlowHashing));
  1564. newBond->setBondMonitorInterval((unsigned int)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["monitorInterval"],(uint64_t)0));
  1565. newBond->setAllowPathNegotiation((bool)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["allowPathNegotiation"],(bool)false));
  1566. */
  1567. if (!_node->bondController()->addCustomPolicy(newTemplateBond)) {
  1568. fprintf(stderr, "error: a custom policy of this name (%s) already exists.\n", customPolicyStr.c_str());
  1569. }
  1570. }
  1571. // Peer-specific bonding
  1572. json &peerSpecificBonds = settings["peerSpecificBonds"];
  1573. for (json::iterator peerItr = peerSpecificBonds.begin(); peerItr != peerSpecificBonds.end();++peerItr) {
  1574. _node->bondController()->assignBondingPolicyToPeer(std::stoull(peerItr.key(),0,16), peerItr.value());
  1575. }
  1576. // Check settings
  1577. if (defaultBondingPolicyStr.length() && !defaultBondingPolicy && !_node->bondController()->inUse()) {
  1578. fprintf(stderr, "error: unknown policy (%s) specified by defaultBondingPolicy, link disabled.\n", defaultBondingPolicyStr.c_str());
  1579. }
  1580. }
  1581. // bondingPolicy cannot be used with allowTcpFallbackRelay
  1582. _allowTcpFallbackRelay = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true) && !(_node->bondController()->inUse());
  1583. _primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1584. _allowSecondaryPort = OSUtils::jsonBool(settings["allowSecondaryPort"],true);
  1585. _secondaryPort = (unsigned int)OSUtils::jsonInt(settings["secondaryPort"],0);
  1586. _tertiaryPort = (unsigned int)OSUtils::jsonInt(settings["tertiaryPort"],0);
  1587. if (_secondaryPort != 0 || _tertiaryPort != 0) {
  1588. fprintf(stderr,"WARNING: using manually-specified ports. This can cause NAT issues." ZT_EOL_S);
  1589. }
  1590. _portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1591. #ifndef ZT_SDK
  1592. const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
  1593. const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
  1594. if (((up == "apply")||(up == "download"))||(udist)) {
  1595. if (!_updater)
  1596. _updater = new SoftwareUpdater(*_node,_homePath);
  1597. _updateAutoApply = (up == "apply");
  1598. _updater->setUpdateDistribution(udist);
  1599. _updater->setChannel(OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL));
  1600. } else {
  1601. delete _updater;
  1602. _updater = (SoftwareUpdater *)0;
  1603. _updateAutoApply = false;
  1604. }
  1605. #endif
  1606. json &ignoreIfs = settings["interfacePrefixBlacklist"];
  1607. if (ignoreIfs.is_array()) {
  1608. for(unsigned long i=0;i<ignoreIfs.size();++i) {
  1609. const std::string tmp(OSUtils::jsonString(ignoreIfs[i],""));
  1610. if (tmp.length() > 0)
  1611. _interfacePrefixBlacklist.push_back(tmp);
  1612. }
  1613. }
  1614. json &amf = settings["allowManagementFrom"];
  1615. if (amf.is_array()) {
  1616. for(unsigned long i=0;i<amf.size();++i) {
  1617. const InetAddress nw(OSUtils::jsonString(amf[i],"").c_str());
  1618. if (nw)
  1619. _allowManagementFrom.push_back(nw);
  1620. }
  1621. }
  1622. }
  1623. #if ZT_VAULT_SUPPORT
  1624. json &vault = settings["vault"];
  1625. if (vault.is_object()) {
  1626. const std::string url(OSUtils::jsonString(vault["vaultURL"], "").c_str());
  1627. if (!url.empty()) {
  1628. _vaultURL = url;
  1629. }
  1630. const std::string token(OSUtils::jsonString(vault["vaultToken"], "").c_str());
  1631. if (!token.empty()) {
  1632. _vaultToken = token;
  1633. }
  1634. const std::string path(OSUtils::jsonString(vault["vaultPath"], "").c_str());
  1635. if (!path.empty()) {
  1636. _vaultPath = path;
  1637. }
  1638. }
  1639. // also check environment variables for values. Environment variables
  1640. // will override local.conf variables
  1641. const std::string envURL(getenv("VAULT_ADDR"));
  1642. if (!envURL.empty()) {
  1643. _vaultURL = envURL;
  1644. }
  1645. const std::string envToken(getenv("VAULT_TOKEN"));
  1646. if (!envToken.empty()) {
  1647. _vaultToken = envToken;
  1648. }
  1649. const std::string envPath(getenv("VAULT_PATH"));
  1650. if (!envPath.empty()) {
  1651. _vaultPath = envPath;
  1652. }
  1653. if (!_vaultURL.empty() && !_vaultToken.empty()) {
  1654. _vaultEnabled = true;
  1655. }
  1656. #endif
  1657. // Checks if a managed IP or route target is allowed
  1658. bool checkIfManagedIsAllowed(const NetworkState &n,const InetAddress &target)
  1659. {
  1660. if (!n.settings.allowManaged)
  1661. return false;
  1662. if (n.settings.allowManagedWhitelist.size() > 0) {
  1663. bool allowed = false;
  1664. for (InetAddress addr : n.settings.allowManagedWhitelist) {
  1665. if (addr.containsAddress(target) && addr.netmaskBits() <= target.netmaskBits()) {
  1666. allowed = true;
  1667. break;
  1668. }
  1669. }
  1670. if (!allowed) return false;
  1671. }
  1672. if (target.isDefaultRoute())
  1673. return n.settings.allowDefault;
  1674. switch(target.ipScope()) {
  1675. case InetAddress::IP_SCOPE_NONE:
  1676. case InetAddress::IP_SCOPE_MULTICAST:
  1677. case InetAddress::IP_SCOPE_LOOPBACK:
  1678. case InetAddress::IP_SCOPE_LINK_LOCAL:
  1679. return false;
  1680. case InetAddress::IP_SCOPE_GLOBAL:
  1681. return n.settings.allowGlobal;
  1682. default:
  1683. return true;
  1684. }
  1685. }
  1686. // Match only an IP from a vector of IPs -- used in syncManagedStuff()
  1687. bool matchIpOnly(const std::vector<InetAddress> &ips,const InetAddress &ip) const
  1688. {
  1689. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1690. if (i->ipsEqual(ip))
  1691. return true;
  1692. }
  1693. return false;
  1694. }
  1695. // Apply or update managed IPs for a configured network (be sure n.tap exists)
  1696. void syncManagedStuff(NetworkState &n,bool syncIps,bool syncRoutes, bool syncDns)
  1697. {
  1698. char ipbuf[64];
  1699. // assumes _nets_m is locked
  1700. if (syncIps) {
  1701. std::vector<InetAddress> newManagedIps;
  1702. newManagedIps.reserve(n.config.assignedAddressCount);
  1703. for(unsigned int i=0;i<n.config.assignedAddressCount;++i) {
  1704. const InetAddress *ii = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[i]));
  1705. if (checkIfManagedIsAllowed(n,*ii))
  1706. newManagedIps.push_back(*ii);
  1707. }
  1708. std::sort(newManagedIps.begin(),newManagedIps.end());
  1709. newManagedIps.erase(std::unique(newManagedIps.begin(),newManagedIps.end()),newManagedIps.end());
  1710. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1711. if (std::find(newManagedIps.begin(),newManagedIps.end(),*ip) == newManagedIps.end()) {
  1712. if (!n.tap->removeIp(*ip))
  1713. fprintf(stderr,"ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1714. }
  1715. }
  1716. #ifdef __SYNOLOGY__
  1717. if (!n.tap->addIpSyn(newManagedIps))
  1718. fprintf(stderr,"ERROR: unable to add ip addresses to ifcfg" ZT_EOL_S);
  1719. #else
  1720. for(std::vector<InetAddress>::iterator ip(newManagedIps.begin());ip!=newManagedIps.end();++ip) {
  1721. if (std::find(n.managedIps.begin(),n.managedIps.end(),*ip) == n.managedIps.end()) {
  1722. if (!n.tap->addIp(*ip))
  1723. fprintf(stderr,"ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1724. }
  1725. }
  1726. #endif
  1727. n.managedIps.swap(newManagedIps);
  1728. }
  1729. if (syncRoutes) {
  1730. char tapdev[64];
  1731. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1732. OSUtils::ztsnprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)((WindowsEthernetTap *)(n.tap.get()))->luid().Value);
  1733. #else
  1734. Utils::scopy(tapdev,sizeof(tapdev),n.tap->deviceName().c_str());
  1735. #endif
  1736. std::vector<InetAddress> myIps(n.tap->ips());
  1737. // Nuke applied routes that are no longer in n.config.routes[] and/or are not allowed
  1738. for(std::list< SharedPtr<ManagedRoute> >::iterator mr(n.managedRoutes.begin());mr!=n.managedRoutes.end();) {
  1739. bool haveRoute = false;
  1740. if ( (checkIfManagedIsAllowed(n,(*mr)->target())) && (((*mr)->via().ss_family != (*mr)->target().ss_family)||(!matchIpOnly(myIps,(*mr)->via()))) ) {
  1741. for(unsigned int i=0;i<n.config.routeCount;++i) {
  1742. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
  1743. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
  1744. if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (strcmp(tapdev,(*mr)->device())==0) ) ) {
  1745. haveRoute = true;
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. if (haveRoute) {
  1751. ++mr;
  1752. } else {
  1753. n.managedRoutes.erase(mr++);
  1754. }
  1755. }
  1756. // Apply routes in n.config.routes[] that we haven't applied yet, and sync those we have in case shadow routes need to change
  1757. for(unsigned int i=0;i<n.config.routeCount;++i) {
  1758. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
  1759. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
  1760. const InetAddress *src = NULL;
  1761. for (unsigned int j=0; j<n.config.assignedAddressCount; ++j) {
  1762. const InetAddress *const tmp = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[j]));
  1763. if (target->isV4() && tmp->isV4()) {
  1764. src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
  1765. break;
  1766. } else if (target->isV6() && tmp->isV6()) {
  1767. src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
  1768. break;
  1769. }
  1770. }
  1771. if (!src)
  1772. src = &NULL_INET_ADDR;
  1773. if ( (!checkIfManagedIsAllowed(n,*target)) || ((via->ss_family == target->ss_family)&&(matchIpOnly(myIps,*via))) )
  1774. continue;
  1775. bool haveRoute = false;
  1776. // Ignore routes implied by local managed IPs since adding the IP adds the route
  1777. #ifndef __APPLE__
  1778. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1779. if ((target->netmaskBits() == ip->netmaskBits())&&(target->containsAddress(*ip))) {
  1780. haveRoute = true;
  1781. break;
  1782. }
  1783. }
  1784. #endif
  1785. if (haveRoute)
  1786. continue;
  1787. #ifndef ZT_SDK
  1788. // If we've already applied this route, just sync it and continue
  1789. for(std::list< SharedPtr<ManagedRoute> >::iterator mr(n.managedRoutes.begin());mr!=n.managedRoutes.end();++mr) {
  1790. if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (tapdev == (*mr)->device()) ) ) {
  1791. haveRoute = true;
  1792. (*mr)->sync();
  1793. break;
  1794. }
  1795. }
  1796. if (haveRoute)
  1797. continue;
  1798. // Add and apply new routes
  1799. n.managedRoutes.push_back(SharedPtr<ManagedRoute>(new ManagedRoute(*target,*via,*src,tapdev)));
  1800. if (!n.managedRoutes.back()->sync())
  1801. n.managedRoutes.pop_back();
  1802. #endif
  1803. }
  1804. }
  1805. if (syncDns) {
  1806. if (n.settings.allowDNS) {
  1807. if (strlen(n.config.dns.domain) != 0) {
  1808. std::vector<InetAddress> servers;
  1809. for (int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  1810. InetAddress a(n.config.dns.server_addr[j]);
  1811. if (a.isV4() || a.isV6()) {
  1812. servers.push_back(a);
  1813. }
  1814. }
  1815. n.tap->setDns(n.config.dns.domain, servers);
  1816. }
  1817. } else {
  1818. #ifdef __APPLE__
  1819. MacDNSHelper::removeDNS(n.config.nwid);
  1820. #elif defined(__WINDOWS__)
  1821. WinDNSHelper::removeDNS(n.config.nwid);
  1822. #endif
  1823. }
  1824. }
  1825. }
  1826. // =========================================================================
  1827. // Handlers for Node and Phy<> callbacks
  1828. // =========================================================================
  1829. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len)
  1830. {
  1831. const uint64_t now = OSUtils::now();
  1832. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL))
  1833. _lastDirectReceiveFromGlobal = now;
  1834. const ZT_ResultCode rc = _node->processWirePacket(nullptr,now,reinterpret_cast<int64_t>(sock),reinterpret_cast<const struct sockaddr_storage *>(from),data,len,&_nextBackgroundTaskDeadline);
  1835. if (ZT_ResultCode_isFatal(rc)) {
  1836. char tmp[256];
  1837. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  1838. Mutex::Lock _l(_termReason_m);
  1839. _termReason = ONE_UNRECOVERABLE_ERROR;
  1840. _fatalErrorMessage = tmp;
  1841. this->terminate();
  1842. }
  1843. }
  1844. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  1845. {
  1846. if (!success) {
  1847. phyOnTcpClose(sock,uptr);
  1848. return;
  1849. }
  1850. TcpConnection *const tc = reinterpret_cast<TcpConnection *>(*uptr);
  1851. if (!tc) { // sanity check
  1852. _phy.close(sock,true);
  1853. return;
  1854. }
  1855. tc->sock = sock;
  1856. if (tc->type == TcpConnection::TCP_TUNNEL_OUTGOING) {
  1857. if (_tcpFallbackTunnel)
  1858. _phy.close(_tcpFallbackTunnel->sock);
  1859. _tcpFallbackTunnel = tc;
  1860. _phy.streamSend(sock,ZT_TCP_TUNNEL_HELLO,sizeof(ZT_TCP_TUNNEL_HELLO));
  1861. } else {
  1862. _phy.close(sock,true);
  1863. }
  1864. }
  1865. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  1866. {
  1867. if (!from) {
  1868. _phy.close(sockN,false);
  1869. return;
  1870. } else {
  1871. #ifdef ZT_SDK
  1872. // Immediately close new local connections. The intention is to prevent the backplane from being accessed when operating as libzt
  1873. if (!allowHttpBackplaneManagement && ((InetAddress*)from)->ipScope() == InetAddress::IP_SCOPE_LOOPBACK) {
  1874. _phy.close(sockN,false);
  1875. return;
  1876. }
  1877. #endif
  1878. TcpConnection *tc = new TcpConnection();
  1879. {
  1880. Mutex::Lock _l(_tcpConnections_m);
  1881. _tcpConnections.push_back(tc);
  1882. }
  1883. tc->type = TcpConnection::TCP_UNCATEGORIZED_INCOMING;
  1884. tc->parent = this;
  1885. tc->sock = sockN;
  1886. tc->remoteAddr = from;
  1887. tc->lastReceive = OSUtils::now();
  1888. http_parser_init(&(tc->parser),HTTP_REQUEST);
  1889. tc->parser.data = (void *)tc;
  1890. tc->messageSize = 0;
  1891. *uptrN = (void *)tc;
  1892. }
  1893. }
  1894. void phyOnTcpClose(PhySocket *sock,void **uptr)
  1895. {
  1896. TcpConnection *tc = (TcpConnection *)*uptr;
  1897. if (tc) {
  1898. if (tc == _tcpFallbackTunnel) {
  1899. _tcpFallbackTunnel = (TcpConnection *)0;
  1900. }
  1901. {
  1902. Mutex::Lock _l(_tcpConnections_m);
  1903. _tcpConnections.erase(std::remove(_tcpConnections.begin(),_tcpConnections.end(),tc),_tcpConnections.end());
  1904. }
  1905. delete tc;
  1906. }
  1907. }
  1908. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  1909. {
  1910. try {
  1911. if (!len) return; // sanity check, should never happen
  1912. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  1913. tc->lastReceive = OSUtils::now();
  1914. switch(tc->type) {
  1915. case TcpConnection::TCP_UNCATEGORIZED_INCOMING:
  1916. switch(reinterpret_cast<uint8_t *>(data)[0]) {
  1917. // HTTP: GET, PUT, POST, HEAD, DELETE
  1918. case 'G':
  1919. case 'P':
  1920. case 'D':
  1921. case 'H': {
  1922. // This is only allowed from IPs permitted to access the management
  1923. // backplane, which is just 127.0.0.1/::1 unless otherwise configured.
  1924. bool allow;
  1925. {
  1926. Mutex::Lock _l(_localConfig_m);
  1927. if (_allowManagementFrom.size() == 0) {
  1928. allow = (tc->remoteAddr.ipScope() == InetAddress::IP_SCOPE_LOOPBACK);
  1929. } else {
  1930. allow = false;
  1931. for(std::vector<InetAddress>::const_iterator i(_allowManagementFrom.begin());i!=_allowManagementFrom.end();++i) {
  1932. if (i->containsAddress(tc->remoteAddr)) {
  1933. allow = true;
  1934. break;
  1935. }
  1936. }
  1937. }
  1938. }
  1939. if (allow) {
  1940. tc->type = TcpConnection::TCP_HTTP_INCOMING;
  1941. phyOnTcpData(sock,uptr,data,len);
  1942. } else {
  1943. _phy.close(sock);
  1944. }
  1945. } break;
  1946. // Drop unknown protocols
  1947. default:
  1948. _phy.close(sock);
  1949. break;
  1950. }
  1951. return;
  1952. case TcpConnection::TCP_HTTP_INCOMING:
  1953. case TcpConnection::TCP_HTTP_OUTGOING:
  1954. http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len);
  1955. if ((tc->parser.upgrade)||(tc->parser.http_errno != HPE_OK))
  1956. _phy.close(sock);
  1957. return;
  1958. case TcpConnection::TCP_TUNNEL_OUTGOING:
  1959. tc->readq.append((const char *)data,len);
  1960. while (tc->readq.length() >= 5) {
  1961. const char *data = tc->readq.data();
  1962. const unsigned long mlen = ( ((((unsigned long)data[3]) & 0xff) << 8) | (((unsigned long)data[4]) & 0xff) );
  1963. if (tc->readq.length() >= (mlen + 5)) {
  1964. InetAddress from;
  1965. unsigned long plen = mlen; // payload length, modified if there's an IP header
  1966. data += 5; // skip forward past pseudo-TLS junk and mlen
  1967. if (plen == 4) {
  1968. // Hello message, which isn't sent by proxy and would be ignored by client
  1969. } else if (plen) {
  1970. // Messages should contain IPv4 or IPv6 source IP address data
  1971. switch(data[0]) {
  1972. case 4: // IPv4
  1973. if (plen >= 7) {
  1974. from.set((const void *)(data + 1),4,((((unsigned int)data[5]) & 0xff) << 8) | (((unsigned int)data[6]) & 0xff));
  1975. data += 7; // type + 4 byte IP + 2 byte port
  1976. plen -= 7;
  1977. } else {
  1978. _phy.close(sock);
  1979. return;
  1980. }
  1981. break;
  1982. case 6: // IPv6
  1983. if (plen >= 19) {
  1984. from.set((const void *)(data + 1),16,((((unsigned int)data[17]) & 0xff) << 8) | (((unsigned int)data[18]) & 0xff));
  1985. data += 19; // type + 16 byte IP + 2 byte port
  1986. plen -= 19;
  1987. } else {
  1988. _phy.close(sock);
  1989. return;
  1990. }
  1991. break;
  1992. case 0: // none/omitted
  1993. ++data;
  1994. --plen;
  1995. break;
  1996. default: // invalid address type
  1997. _phy.close(sock);
  1998. return;
  1999. }
  2000. if (from) {
  2001. InetAddress fakeTcpLocalInterfaceAddress((uint32_t)0xffffffff,0xffff);
  2002. const ZT_ResultCode rc = _node->processWirePacket(
  2003. (void *)0,
  2004. OSUtils::now(),
  2005. -1,
  2006. reinterpret_cast<struct sockaddr_storage *>(&from),
  2007. data,
  2008. plen,
  2009. &_nextBackgroundTaskDeadline);
  2010. if (ZT_ResultCode_isFatal(rc)) {
  2011. char tmp[256];
  2012. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  2013. Mutex::Lock _l(_termReason_m);
  2014. _termReason = ONE_UNRECOVERABLE_ERROR;
  2015. _fatalErrorMessage = tmp;
  2016. this->terminate();
  2017. _phy.close(sock);
  2018. return;
  2019. }
  2020. }
  2021. }
  2022. if (tc->readq.length() > (mlen + 5))
  2023. tc->readq.erase(tc->readq.begin(),tc->readq.begin() + (mlen + 5));
  2024. else tc->readq.clear();
  2025. } else break;
  2026. }
  2027. return;
  2028. }
  2029. } catch ( ... ) {
  2030. _phy.close(sock);
  2031. }
  2032. }
  2033. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  2034. {
  2035. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  2036. bool closeit = false;
  2037. {
  2038. Mutex::Lock _l(tc->writeq_m);
  2039. if (tc->writeq.length() > 0) {
  2040. long sent = (long)_phy.streamSend(sock,tc->writeq.data(),(unsigned long)tc->writeq.length(),true);
  2041. if (sent > 0) {
  2042. if ((unsigned long)sent >= (unsigned long)tc->writeq.length()) {
  2043. tc->writeq.clear();
  2044. _phy.setNotifyWritable(sock,false);
  2045. if (tc->type == TcpConnection::TCP_HTTP_INCOMING)
  2046. closeit = true; // HTTP keep alive not supported
  2047. } else {
  2048. tc->writeq.erase(tc->writeq.begin(),tc->writeq.begin() + sent);
  2049. }
  2050. }
  2051. } else {
  2052. _phy.setNotifyWritable(sock,false);
  2053. }
  2054. }
  2055. if (closeit)
  2056. _phy.close(sock);
  2057. }
  2058. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  2059. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  2060. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  2061. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  2062. inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
  2063. inline int nodeVirtualNetworkConfigFunction(uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwc)
  2064. {
  2065. Mutex::Lock _l(_nets_m);
  2066. NetworkState &n = _nets[nwid];
  2067. switch(op) {
  2068. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  2069. if (!n.tap) {
  2070. try {
  2071. char friendlyName[128];
  2072. OSUtils::ztsnprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid);
  2073. n.tap = EthernetTap::newInstance(
  2074. nullptr,
  2075. _homePath.c_str(),
  2076. MAC(nwc->mac),
  2077. nwc->mtu,
  2078. (unsigned int)ZT_IF_METRIC,
  2079. nwid,
  2080. friendlyName,
  2081. StapFrameHandler,
  2082. (void *)this);
  2083. *nuptr = (void *)&n;
  2084. char nlcpath[256];
  2085. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2086. std::string nlcbuf;
  2087. if (OSUtils::readFile(nlcpath,nlcbuf)) {
  2088. Dictionary<4096> nc;
  2089. nc.load(nlcbuf.c_str());
  2090. Buffer<1024> allowManaged;
  2091. if (nc.get("allowManaged", allowManaged) && allowManaged.size() != 0) {
  2092. std::string addresses (allowManaged.begin(), allowManaged.size());
  2093. if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility
  2094. if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') {
  2095. n.settings.allowManaged = true;
  2096. } else {
  2097. n.settings.allowManaged = false;
  2098. }
  2099. } else {
  2100. // this should be a list of IP addresses
  2101. n.settings.allowManaged = true;
  2102. size_t pos = 0;
  2103. while (true) {
  2104. size_t nextPos = addresses.find(',', pos);
  2105. std::string address = addresses.substr(pos, (nextPos == std::string::npos ? addresses.size() : nextPos) - pos);
  2106. n.settings.allowManagedWhitelist.push_back(InetAddress(address.c_str()));
  2107. if (nextPos == std::string::npos) break;
  2108. pos = nextPos + 1;
  2109. }
  2110. }
  2111. } else {
  2112. n.settings.allowManaged = true;
  2113. }
  2114. n.settings.allowGlobal = nc.getB("allowGlobal", false);
  2115. n.settings.allowDefault = nc.getB("allowDefault", false);
  2116. n.settings.allowDNS = nc.getB("allowDNS", false);
  2117. }
  2118. } catch (std::exception &exc) {
  2119. #ifdef __WINDOWS__
  2120. FILE *tapFailLog = fopen((_homePath + ZT_PATH_SEPARATOR_S"port_error_log.txt").c_str(),"a");
  2121. if (tapFailLog) {
  2122. fprintf(tapFailLog,"%.16llx: %s" ZT_EOL_S,(unsigned long long)nwid,exc.what());
  2123. fclose(tapFailLog);
  2124. }
  2125. #else
  2126. fprintf(stderr,"ERROR: unable to configure virtual network port: %s" ZT_EOL_S,exc.what());
  2127. #endif
  2128. _nets.erase(nwid);
  2129. return -999;
  2130. } catch ( ... ) {
  2131. return -999; // tap init failed
  2132. }
  2133. }
  2134. // After setting up tap, fall through to CONFIG_UPDATE since we also want to do this...
  2135. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  2136. memcpy(&(n.config),nwc,sizeof(ZT_VirtualNetworkConfig));
  2137. if (n.tap) { // sanity check
  2138. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2139. // wait for up to 5 seconds for the WindowsEthernetTap to actually be initialized
  2140. //
  2141. // without WindowsEthernetTap::isInitialized() returning true, the won't actually
  2142. // be online yet and setting managed routes on it will fail.
  2143. const int MAX_SLEEP_COUNT = 500;
  2144. for (int i = 0; !((WindowsEthernetTap *)(n.tap.get()))->isInitialized() && i < MAX_SLEEP_COUNT; i++) {
  2145. Sleep(10);
  2146. }
  2147. #endif
  2148. syncManagedStuff(n,true,true,true);
  2149. n.tap->setMtu(nwc->mtu);
  2150. } else {
  2151. _nets.erase(nwid);
  2152. return -999; // tap init failed
  2153. }
  2154. break;
  2155. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  2156. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  2157. if (n.tap) { // sanity check
  2158. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2159. std::string winInstanceId(((WindowsEthernetTap *)(n.tap.get()))->instanceId());
  2160. #endif
  2161. *nuptr = (void *)0;
  2162. n.tap.reset();
  2163. _nets.erase(nwid);
  2164. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2165. if ((op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY)&&(winInstanceId.length() > 0))
  2166. WindowsEthernetTap::deletePersistentTapDevice(winInstanceId.c_str());
  2167. #endif
  2168. if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) {
  2169. char nlcpath[256];
  2170. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2171. OSUtils::rm(nlcpath);
  2172. }
  2173. } else {
  2174. _nets.erase(nwid);
  2175. }
  2176. break;
  2177. }
  2178. return 0;
  2179. }
  2180. inline void nodeEventCallback(enum ZT_Event event,const void *metaData)
  2181. {
  2182. switch(event) {
  2183. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  2184. Mutex::Lock _l(_termReason_m);
  2185. _termReason = ONE_IDENTITY_COLLISION;
  2186. _fatalErrorMessage = "identity/address collision";
  2187. this->terminate();
  2188. } break;
  2189. case ZT_EVENT_TRACE: {
  2190. if (metaData) {
  2191. ::fprintf(stderr,"%s" ZT_EOL_S,(const char *)metaData);
  2192. ::fflush(stderr);
  2193. }
  2194. } break;
  2195. case ZT_EVENT_USER_MESSAGE: {
  2196. const ZT_UserMessage *um = reinterpret_cast<const ZT_UserMessage *>(metaData);
  2197. if ((um->typeId == ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE)&&(_updater)) {
  2198. _updater->handleSoftwareUpdateUserMessage(um->origin,um->data,um->length);
  2199. }
  2200. } break;
  2201. case ZT_EVENT_REMOTE_TRACE: {
  2202. const ZT_RemoteTrace *rt = reinterpret_cast<const ZT_RemoteTrace *>(metaData);
  2203. if ((rt)&&(rt->len > 0)&&(rt->len <= ZT_MAX_REMOTE_TRACE_SIZE)&&(rt->data))
  2204. _controller->handleRemoteTrace(*rt);
  2205. }
  2206. default:
  2207. break;
  2208. }
  2209. }
  2210. #if ZT_VAULT_SUPPORT
  2211. inline bool nodeVaultPutIdentity(enum ZT_StateObjectType type, const void *data, int len)
  2212. {
  2213. bool retval = false;
  2214. if (type != ZT_STATE_OBJECT_IDENTITY_PUBLIC && type != ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2215. return retval;
  2216. }
  2217. CURL *curl = curl_easy_init();
  2218. if (curl) {
  2219. char token[512] = { 0 };
  2220. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  2221. struct curl_slist *chunk = NULL;
  2222. chunk = curl_slist_append(chunk, token);
  2223. char content_type[512] = { 0 };
  2224. snprintf(content_type, sizeof(content_type), "Content-Type: application/json");
  2225. chunk = curl_slist_append(chunk, content_type);
  2226. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  2227. char url[2048] = { 0 };
  2228. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  2229. curl_easy_setopt(curl, CURLOPT_URL, url);
  2230. json d = json::object();
  2231. if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2232. std::string key((const char*)data, len);
  2233. d["public"] = key;
  2234. }
  2235. else if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2236. std::string key((const char*)data, len);
  2237. d["secret"] = key;
  2238. }
  2239. if (!d.empty()) {
  2240. std::string post = d.dump();
  2241. if (!post.empty()) {
  2242. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
  2243. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post.length());
  2244. #ifndef NDEBUG
  2245. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  2246. #endif
  2247. CURLcode res = curl_easy_perform(curl);
  2248. if (res == CURLE_OK) {
  2249. long response_code = 0;
  2250. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  2251. if (response_code == 200 || response_code == 204) {
  2252. retval = true;
  2253. }
  2254. }
  2255. }
  2256. }
  2257. curl_easy_cleanup(curl);
  2258. curl = NULL;
  2259. curl_slist_free_all(chunk);
  2260. chunk = NULL;
  2261. }
  2262. return retval;
  2263. }
  2264. #endif
  2265. inline void nodeStatePutFunction(enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2266. {
  2267. #if ZT_VAULT_SUPPORT
  2268. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  2269. if (nodeVaultPutIdentity(type, data, len)) {
  2270. // value successfully written to Vault
  2271. return;
  2272. }
  2273. // else fallback to disk
  2274. }
  2275. #endif
  2276. char p[1024];
  2277. FILE *f;
  2278. bool secure = false;
  2279. char dirname[1024];
  2280. dirname[0] = 0;
  2281. switch(type) {
  2282. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  2283. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  2284. break;
  2285. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  2286. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  2287. secure = true;
  2288. break;
  2289. case ZT_STATE_OBJECT_PLANET:
  2290. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  2291. break;
  2292. case ZT_STATE_OBJECT_MOON:
  2293. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "moons.d",_homePath.c_str());
  2294. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.moon",dirname,(unsigned long long)id[0]);
  2295. break;
  2296. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  2297. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str());
  2298. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]);
  2299. secure = true;
  2300. break;
  2301. case ZT_STATE_OBJECT_PEER:
  2302. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str());
  2303. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]);
  2304. break;
  2305. default:
  2306. return;
  2307. }
  2308. if ((len >= 0)&&(data)) {
  2309. // Check to see if we've already written this first. This reduces
  2310. // redundant writes and I/O overhead on most platforms and has
  2311. // little effect on others.
  2312. f = fopen(p,"rb");
  2313. if (f) {
  2314. char *const buf = (char *)malloc(len*4);
  2315. if (buf) {
  2316. long l = (long)fread(buf,1,(size_t)(len*4),f);
  2317. fclose(f);
  2318. if ((l == (long)len)&&(memcmp(data,buf,l) == 0)) {
  2319. free(buf);
  2320. return;
  2321. }
  2322. free(buf);
  2323. }
  2324. }
  2325. f = fopen(p,"wb");
  2326. if ((!f)&&(dirname[0])) { // create subdirectory if it does not exist
  2327. OSUtils::mkdir(dirname);
  2328. f = fopen(p,"wb");
  2329. }
  2330. if (f) {
  2331. if (fwrite(data,len,1,f) != 1)
  2332. fprintf(stderr,"WARNING: unable to write to file: %s (I/O error)" ZT_EOL_S,p);
  2333. fclose(f);
  2334. if (secure)
  2335. OSUtils::lockDownFile(p,false);
  2336. } else {
  2337. fprintf(stderr,"WARNING: unable to write to file: %s (unable to open)" ZT_EOL_S,p);
  2338. }
  2339. } else {
  2340. OSUtils::rm(p);
  2341. }
  2342. }
  2343. #if ZT_VAULT_SUPPORT
  2344. inline int nodeVaultGetIdentity(enum ZT_StateObjectType type, void *data, unsigned int maxlen)
  2345. {
  2346. if (type != ZT_STATE_OBJECT_IDENTITY_SECRET && type != ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2347. return -1;
  2348. }
  2349. int ret = -1;
  2350. CURL *curl = curl_easy_init();
  2351. if (curl) {
  2352. char token[512] = { 0 };
  2353. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  2354. struct curl_slist *chunk = NULL;
  2355. chunk = curl_slist_append(chunk, token);
  2356. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  2357. char url[2048] = { 0 };
  2358. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  2359. curl_easy_setopt(curl, CURLOPT_URL, url);
  2360. std::string response;
  2361. std::string res_headers;
  2362. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlResponseWrite);
  2363. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  2364. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &res_headers);
  2365. #ifndef NDEBUG
  2366. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  2367. #endif
  2368. CURLcode res = curl_easy_perform(curl);
  2369. if (res == CURLE_OK) {
  2370. long response_code = 0;
  2371. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  2372. if (response_code == 200) {
  2373. try {
  2374. json payload = json::parse(response);
  2375. if (!payload["data"].is_null()) {
  2376. json &d = payload["data"];
  2377. if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2378. std::string secret = OSUtils::jsonString(d["secret"],"");
  2379. if (!secret.empty()) {
  2380. ret = (int)secret.length();
  2381. memcpy(data, secret.c_str(), ret);
  2382. }
  2383. }
  2384. else if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2385. std::string pub = OSUtils::jsonString(d["public"],"");
  2386. if (!pub.empty()) {
  2387. ret = (int)pub.length();
  2388. memcpy(data, pub.c_str(), ret);
  2389. }
  2390. }
  2391. }
  2392. }
  2393. catch (...) {
  2394. ret = -1;
  2395. }
  2396. }
  2397. }
  2398. curl_easy_cleanup(curl);
  2399. curl = NULL;
  2400. curl_slist_free_all(chunk);
  2401. chunk = NULL;
  2402. }
  2403. return ret;
  2404. }
  2405. #endif
  2406. inline int nodeStateGetFunction(enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  2407. {
  2408. #if ZT_VAULT_SUPPORT
  2409. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) ) {
  2410. int retval = nodeVaultGetIdentity(type, data, maxlen);
  2411. if (retval >= 0)
  2412. return retval;
  2413. // else continue file based lookup
  2414. }
  2415. #endif
  2416. char p[4096];
  2417. switch(type) {
  2418. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  2419. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  2420. break;
  2421. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  2422. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  2423. break;
  2424. case ZT_STATE_OBJECT_PLANET:
  2425. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  2426. break;
  2427. case ZT_STATE_OBJECT_MOON:
  2428. 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]);
  2429. break;
  2430. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  2431. 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]);
  2432. break;
  2433. case ZT_STATE_OBJECT_PEER:
  2434. 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]);
  2435. break;
  2436. default:
  2437. return -1;
  2438. }
  2439. FILE *f = fopen(p,"rb");
  2440. if (f) {
  2441. int n = (int)fread(data,1,maxlen,f);
  2442. fclose(f);
  2443. #if ZT_VAULT_SUPPORT
  2444. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  2445. // If we've gotten here while Vault is enabled, Vault does not know the key and it's been
  2446. // read from disk instead.
  2447. //
  2448. // We should put the value in Vault and remove the local file.
  2449. if (nodeVaultPutIdentity(type, data, n)) {
  2450. unlink(p);
  2451. }
  2452. }
  2453. #endif
  2454. if (n >= 0)
  2455. return n;
  2456. }
  2457. return -1;
  2458. }
  2459. inline int nodeWirePacketSendFunction(const int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  2460. {
  2461. #ifdef ZT_TCP_FALLBACK_RELAY
  2462. if(_allowTcpFallbackRelay) {
  2463. if (addr->ss_family == AF_INET) {
  2464. // TCP fallback tunnel support, currently IPv4 only
  2465. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(addr)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  2466. // Engage TCP tunnel fallback if we haven't received anything valid from a global
  2467. // IP address in ZT_TCP_FALLBACK_AFTER milliseconds. If we do start getting
  2468. // valid direct traffic we'll stop using it and close the socket after a while.
  2469. const int64_t now = OSUtils::now();
  2470. if (((now - _lastDirectReceiveFromGlobal) > ZT_TCP_FALLBACK_AFTER)&&((now - _lastRestart) > ZT_TCP_FALLBACK_AFTER)) {
  2471. if (_tcpFallbackTunnel) {
  2472. bool flushNow = false;
  2473. {
  2474. Mutex::Lock _l(_tcpFallbackTunnel->writeq_m);
  2475. if (_tcpFallbackTunnel->writeq.size() < (1024 * 64)) {
  2476. if (_tcpFallbackTunnel->writeq.length() == 0) {
  2477. _phy.setNotifyWritable(_tcpFallbackTunnel->sock,true);
  2478. flushNow = true;
  2479. }
  2480. const unsigned long mlen = len + 7;
  2481. _tcpFallbackTunnel->writeq.push_back((char)0x17);
  2482. _tcpFallbackTunnel->writeq.push_back((char)0x03);
  2483. _tcpFallbackTunnel->writeq.push_back((char)0x03); // fake TLS 1.2 header
  2484. _tcpFallbackTunnel->writeq.push_back((char)((mlen >> 8) & 0xff));
  2485. _tcpFallbackTunnel->writeq.push_back((char)(mlen & 0xff));
  2486. _tcpFallbackTunnel->writeq.push_back((char)4); // IPv4
  2487. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_addr.s_addr))),4);
  2488. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_port))),2);
  2489. _tcpFallbackTunnel->writeq.append((const char *)data,len);
  2490. }
  2491. }
  2492. if (flushNow) {
  2493. void *tmpptr = (void *)_tcpFallbackTunnel;
  2494. phyOnTcpWritable(_tcpFallbackTunnel->sock,&tmpptr);
  2495. }
  2496. } else if (((now - _lastSendToGlobalV4) < ZT_TCP_FALLBACK_AFTER)&&((now - _lastSendToGlobalV4) > (ZT_PING_CHECK_INVERVAL / 2))) {
  2497. const InetAddress addr(ZT_TCP_FALLBACK_RELAY);
  2498. TcpConnection *tc = new TcpConnection();
  2499. {
  2500. Mutex::Lock _l(_tcpConnections_m);
  2501. _tcpConnections.push_back(tc);
  2502. }
  2503. tc->type = TcpConnection::TCP_TUNNEL_OUTGOING;
  2504. tc->remoteAddr = addr;
  2505. tc->lastReceive = OSUtils::now();
  2506. tc->parent = this;
  2507. tc->sock = (PhySocket *)0; // set in connect handler
  2508. tc->messageSize = 0;
  2509. bool connected = false;
  2510. _phy.tcpConnect(reinterpret_cast<const struct sockaddr *>(&addr),connected,(void *)tc,true);
  2511. }
  2512. }
  2513. _lastSendToGlobalV4 = now;
  2514. }
  2515. }
  2516. }
  2517. #endif // ZT_TCP_FALLBACK_RELAY
  2518. // Even when relaying we still send via UDP. This way if UDP starts
  2519. // working we can instantly "fail forward" to it and stop using TCP
  2520. // proxy fallback, which is slow.
  2521. if ((localSocket != -1)&&(localSocket != 0)&&(_binder.isUdpSocketValid((PhySocket *)((uintptr_t)localSocket)))) {
  2522. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl);
  2523. const bool r = _phy.udpSend((PhySocket *)((uintptr_t)localSocket),(const struct sockaddr *)addr,data,len);
  2524. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255);
  2525. return ((r) ? 0 : -1);
  2526. } else {
  2527. return ((_binder.udpSendAll(_phy,addr,data,len,ttl)) ? 0 : -1);
  2528. }
  2529. }
  2530. 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)
  2531. {
  2532. NetworkState *n = reinterpret_cast<NetworkState *>(*nuptr);
  2533. if ((!n)||(!n->tap))
  2534. return;
  2535. n->tap->put(MAC(sourceMac),MAC(destMac),etherType,data,len);
  2536. }
  2537. inline int nodePathCheckFunction(uint64_t ztaddr,const int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  2538. {
  2539. // Make sure we're not trying to do ZeroTier-over-ZeroTier
  2540. {
  2541. Mutex::Lock _l(_nets_m);
  2542. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  2543. if (n->second.tap) {
  2544. std::vector<InetAddress> ips(n->second.tap->ips());
  2545. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  2546. if (i->containsAddress(*(reinterpret_cast<const InetAddress *>(remoteAddr)))) {
  2547. return 0;
  2548. }
  2549. }
  2550. }
  2551. }
  2552. }
  2553. /* Note: I do not think we need to scan for overlap with managed routes
  2554. * because of the "route forking" and interface binding that we do. This
  2555. * ensures (we hope) that ZeroTier traffic will still take the physical
  2556. * path even if its managed routes override this for other traffic. Will
  2557. * revisit if we see recursion problems. */
  2558. // Check blacklists
  2559. const Hashtable< uint64_t,std::vector<InetAddress> > *blh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  2560. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  2561. if (remoteAddr->ss_family == AF_INET) {
  2562. blh = &_v4Blacklists;
  2563. gbl = &_globalV4Blacklist;
  2564. } else if (remoteAddr->ss_family == AF_INET6) {
  2565. blh = &_v6Blacklists;
  2566. gbl = &_globalV6Blacklist;
  2567. }
  2568. if (blh) {
  2569. Mutex::Lock _l(_localConfig_m);
  2570. const std::vector<InetAddress> *l = blh->get(ztaddr);
  2571. if (l) {
  2572. for(std::vector<InetAddress>::const_iterator a(l->begin());a!=l->end();++a) {
  2573. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  2574. return 0;
  2575. }
  2576. }
  2577. }
  2578. if (gbl) {
  2579. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  2580. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  2581. return 0;
  2582. }
  2583. }
  2584. return 1;
  2585. }
  2586. inline int nodePathLookupFunction(uint64_t ztaddr,int family,struct sockaddr_storage *result)
  2587. {
  2588. const Hashtable< uint64_t,std::vector<InetAddress> > *lh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  2589. if (family < 0)
  2590. lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints;
  2591. else if (family == AF_INET)
  2592. lh = &_v4Hints;
  2593. else if (family == AF_INET6)
  2594. lh = &_v6Hints;
  2595. else return 0;
  2596. const std::vector<InetAddress> *l = lh->get(ztaddr);
  2597. if ((l)&&(l->size() > 0)) {
  2598. memcpy(result,&((*l)[(unsigned long)_node->prng() % l->size()]),sizeof(struct sockaddr_storage));
  2599. return 1;
  2600. } else return 0;
  2601. }
  2602. inline void tapFrameHandler(uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  2603. {
  2604. _node->processVirtualNetworkFrame((void *)0,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&_nextBackgroundTaskDeadline);
  2605. }
  2606. inline void onHttpRequestToServer(TcpConnection *tc)
  2607. {
  2608. char tmpn[4096];
  2609. std::string data;
  2610. std::string contentType("text/plain"); // default if not changed in handleRequest()
  2611. unsigned int scode = 404;
  2612. // Note that we check allowed IP ranges when HTTP connections are first detected in
  2613. // phyOnTcpData(). If we made it here the source IP is okay.
  2614. try {
  2615. scode = handleControlPlaneHttpRequest(tc->remoteAddr,tc->parser.method,tc->url,tc->headers,tc->readq,data,contentType);
  2616. } catch (std::exception &exc) {
  2617. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: %s" ZT_EOL_S,exc.what());
  2618. scode = 500;
  2619. } catch ( ... ) {
  2620. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: unknown exception" ZT_EOL_S);
  2621. scode = 500;
  2622. }
  2623. const char *scodestr;
  2624. switch(scode) {
  2625. case 200: scodestr = "OK"; break;
  2626. case 400: scodestr = "Bad Request"; break;
  2627. case 401: scodestr = "Unauthorized"; break;
  2628. case 403: scodestr = "Forbidden"; break;
  2629. case 404: scodestr = "Not Found"; break;
  2630. case 500: scodestr = "Internal Server Error"; break;
  2631. case 501: scodestr = "Not Implemented"; break;
  2632. case 503: scodestr = "Service Unavailable"; break;
  2633. default: scodestr = "Error"; break;
  2634. }
  2635. OSUtils::ztsnprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Type: %s\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n",
  2636. scode,
  2637. scodestr,
  2638. contentType.c_str(),
  2639. (unsigned long)data.length());
  2640. {
  2641. Mutex::Lock _l(tc->writeq_m);
  2642. tc->writeq = tmpn;
  2643. if (tc->parser.method != HTTP_HEAD)
  2644. tc->writeq.append(data);
  2645. }
  2646. _phy.setNotifyWritable(tc->sock,true);
  2647. }
  2648. inline void onHttpResponseFromClient(TcpConnection *tc)
  2649. {
  2650. _phy.close(tc->sock);
  2651. }
  2652. bool shouldBindInterface(const char *ifname,const InetAddress &ifaddr)
  2653. {
  2654. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  2655. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  2656. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  2657. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  2658. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  2659. #endif
  2660. #ifdef __APPLE__
  2661. if ((ifname[0] == 'f')&&(ifname[1] == 'e')&&(ifname[2] == 't')&&(ifname[3] == 'h')) return false; // ... as is feth#
  2662. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  2663. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  2664. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  2665. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  2666. if ((ifname[0] == 'u')&&(ifname[1] == 't')&&(ifname[2] == 'u')&&(ifname[3] == 'n')) return false; // ... as is utun#
  2667. #endif
  2668. {
  2669. Mutex::Lock _l(_localConfig_m);
  2670. for(std::vector<std::string>::const_iterator p(_interfacePrefixBlacklist.begin());p!=_interfacePrefixBlacklist.end();++p) {
  2671. if (!strncmp(p->c_str(),ifname,p->length()))
  2672. return false;
  2673. }
  2674. return _node->bondController()->allowedToBind(std::string(ifname));
  2675. }
  2676. {
  2677. // Check global blacklists
  2678. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  2679. if (ifaddr.ss_family == AF_INET) {
  2680. gbl = &_globalV4Blacklist;
  2681. } else if (ifaddr.ss_family == AF_INET6) {
  2682. gbl = &_globalV6Blacklist;
  2683. }
  2684. if (gbl) {
  2685. Mutex::Lock _l(_localConfig_m);
  2686. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  2687. if (a->containsAddress(ifaddr))
  2688. return false;
  2689. }
  2690. }
  2691. }
  2692. {
  2693. Mutex::Lock _l(_nets_m);
  2694. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  2695. if (n->second.tap) {
  2696. std::vector<InetAddress> ips(n->second.tap->ips());
  2697. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  2698. if (i->ipsEqual(ifaddr))
  2699. return false;
  2700. }
  2701. }
  2702. }
  2703. }
  2704. return true;
  2705. }
  2706. bool _trialBind(unsigned int port)
  2707. {
  2708. struct sockaddr_in in4;
  2709. struct sockaddr_in6 in6;
  2710. PhySocket *tb;
  2711. memset(&in4,0,sizeof(in4));
  2712. in4.sin_family = AF_INET;
  2713. in4.sin_port = Utils::hton((uint16_t)port);
  2714. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0,0);
  2715. if (tb) {
  2716. _phy.close(tb,false);
  2717. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0);
  2718. if (tb) {
  2719. _phy.close(tb,false);
  2720. return true;
  2721. }
  2722. }
  2723. memset(&in6,0,sizeof(in6));
  2724. in6.sin6_family = AF_INET6;
  2725. in6.sin6_port = Utils::hton((uint16_t)port);
  2726. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0,0);
  2727. if (tb) {
  2728. _phy.close(tb,false);
  2729. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0);
  2730. if (tb) {
  2731. _phy.close(tb,false);
  2732. return true;
  2733. }
  2734. }
  2735. return false;
  2736. }
  2737. };
  2738. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf)
  2739. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkConfigFunction(nwid,nuptr,op,nwconf); }
  2740. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData)
  2741. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeEventCallback(event,metaData); }
  2742. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2743. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeStatePutFunction(type,id,data,len); }
  2744. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  2745. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeStateGetFunction(type,id,data,maxlen); }
  2746. 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)
  2747. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeWirePacketSendFunction(localSocket,addr,data,len,ttl); }
  2748. 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)
  2749. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkFrameFunction(nwid,nuptr,sourceMac,destMac,etherType,vlanId,data,len); }
  2750. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  2751. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathCheckFunction(ztaddr,localSocket,remoteAddr); }
  2752. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result)
  2753. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathLookupFunction(ztaddr,family,result); }
  2754. 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)
  2755. { reinterpret_cast<OneServiceImpl *>(uptr)->tapFrameHandler(nwid,from,to,etherType,vlanId,data,len); }
  2756. static int ShttpOnMessageBegin(http_parser *parser)
  2757. {
  2758. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2759. tc->currentHeaderField = "";
  2760. tc->currentHeaderValue = "";
  2761. tc->messageSize = 0;
  2762. tc->url.clear();
  2763. tc->status.clear();
  2764. tc->headers.clear();
  2765. tc->readq.clear();
  2766. return 0;
  2767. }
  2768. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  2769. {
  2770. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2771. tc->messageSize += (unsigned long)length;
  2772. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2773. return -1;
  2774. tc->url.append(ptr,length);
  2775. return 0;
  2776. }
  2777. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  2778. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  2779. #else
  2780. static int ShttpOnStatus(http_parser *parser)
  2781. #endif
  2782. { return 0; }
  2783. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  2784. {
  2785. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2786. tc->messageSize += (unsigned long)length;
  2787. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2788. return -1;
  2789. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length())) {
  2790. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2791. tc->currentHeaderField = "";
  2792. tc->currentHeaderValue = "";
  2793. }
  2794. for(size_t i=0;i<length;++i)
  2795. tc->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  2796. return 0;
  2797. }
  2798. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  2799. {
  2800. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2801. tc->messageSize += (unsigned long)length;
  2802. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2803. return -1;
  2804. tc->currentHeaderValue.append(ptr,length);
  2805. return 0;
  2806. }
  2807. static int ShttpOnHeadersComplete(http_parser *parser)
  2808. {
  2809. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2810. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length()))
  2811. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2812. return 0;
  2813. }
  2814. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  2815. {
  2816. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2817. tc->messageSize += (unsigned long)length;
  2818. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2819. return -1;
  2820. tc->readq.append(ptr,length);
  2821. return 0;
  2822. }
  2823. static int ShttpOnMessageComplete(http_parser *parser)
  2824. {
  2825. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2826. if (tc->type == TcpConnection::TCP_HTTP_INCOMING) {
  2827. tc->parent->onHttpRequestToServer(tc);
  2828. } else {
  2829. tc->parent->onHttpResponseFromClient(tc);
  2830. }
  2831. return 0;
  2832. }
  2833. } // anonymous namespace
  2834. std::string OneService::platformDefaultHomePath()
  2835. {
  2836. return OSUtils::platformDefaultHomePath();
  2837. }
  2838. OneService *OneService::newInstance(const char *hp,unsigned int port) { return new OneServiceImpl(hp,port); }
  2839. OneService::~OneService() {}
  2840. } // namespace ZeroTier