OneService.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include <string>
  23. #include <map>
  24. #include <set>
  25. #include <vector>
  26. #include <algorithm>
  27. #include "../version.h"
  28. #include "../include/ZeroTierOne.h"
  29. #include "../ext/http-parser/http_parser.h"
  30. #include "../node/Constants.hpp"
  31. #include "../node/Mutex.hpp"
  32. #include "../node/Node.hpp"
  33. #include "../node/Utils.hpp"
  34. #include "../node/InetAddress.hpp"
  35. #include "../node/MAC.hpp"
  36. #include "../node/Identity.hpp"
  37. #include "../osdep/Phy.hpp"
  38. #include "../osdep/Thread.hpp"
  39. #include "../osdep/OSUtils.hpp"
  40. #include "../osdep/Http.hpp"
  41. #include "../osdep/BackgroundResolver.hpp"
  42. #include "../osdep/PortMapper.hpp"
  43. #include "../osdep/Binder.hpp"
  44. #include "OneService.hpp"
  45. #include "ControlPlane.hpp"
  46. #include "ClusterGeoIpService.hpp"
  47. #include "ClusterDefinition.hpp"
  48. /**
  49. * Uncomment to enable UDP breakage switch
  50. *
  51. * If this is defined, the presence of a file called /tmp/ZT_BREAK_UDP
  52. * will cause direct UDP TX/RX to stop working. This can be used to
  53. * test TCP tunneling fallback and other robustness features. Deleting
  54. * this file will cause it to start working again.
  55. */
  56. //#define ZT_BREAK_UDP
  57. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  58. #include "../controller/SqliteNetworkController.hpp"
  59. #else
  60. class SqliteNetworkController;
  61. #endif // ZT_ENABLE_NETWORK_CONTROLLER
  62. #ifdef __WINDOWS__
  63. #include <WinSock2.h>
  64. #include <Windows.h>
  65. #include <ShlObj.h>
  66. #include <netioapi.h>
  67. #include <iphlpapi.h>
  68. #else
  69. #include <sys/types.h>
  70. #include <sys/socket.h>
  71. #include <sys/wait.h>
  72. #include <unistd.h>
  73. #include <ifaddrs.h>
  74. #endif
  75. // Include the right tap device driver for this platform -- add new platforms here
  76. #ifdef ZT_SERVICE_NETCON
  77. // In network containers builds, use the virtual netcon endpoint instead of a tun/tap port driver
  78. #include "../netcon/NetconEthernetTap.hpp"
  79. namespace ZeroTier { typedef NetconEthernetTap EthernetTap; }
  80. #else // not ZT_SERVICE_NETCON so pick a tap driver
  81. #ifdef __APPLE__
  82. #include "../osdep/OSXEthernetTap.hpp"
  83. namespace ZeroTier { typedef OSXEthernetTap EthernetTap; }
  84. #endif // __APPLE__
  85. #ifdef __LINUX__
  86. #include "../osdep/LinuxEthernetTap.hpp"
  87. namespace ZeroTier { typedef LinuxEthernetTap EthernetTap; }
  88. #endif // __LINUX__
  89. #ifdef __WINDOWS__
  90. #include "../osdep/WindowsEthernetTap.hpp"
  91. namespace ZeroTier { typedef WindowsEthernetTap EthernetTap; }
  92. #endif // __WINDOWS__
  93. #ifdef __FreeBSD__
  94. #include "../osdep/BSDEthernetTap.hpp"
  95. namespace ZeroTier { typedef BSDEthernetTap EthernetTap; }
  96. #endif // __FreeBSD__
  97. #endif // ZT_SERVICE_NETCON
  98. // Sanity limits for HTTP
  99. #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
  100. #define ZT_MAX_HTTP_CONNECTIONS 64
  101. // Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
  102. // bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.
  103. #define ZT_IF_METRIC 5000
  104. // How often to check for new multicast subscriptions on a tap device
  105. #define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
  106. // Path under ZT1 home for controller database if controller is enabled
  107. #define ZT_CONTROLLER_DB_PATH "controller.db"
  108. // TCP fallback relay host -- geo-distributed using Amazon Route53 geo-aware DNS
  109. #define ZT_TCP_FALLBACK_RELAY "tcp-fallback.zerotier.com"
  110. #define ZT_TCP_FALLBACK_RELAY_PORT 443
  111. // Frequency at which we re-resolve the TCP fallback relay
  112. #define ZT_TCP_FALLBACK_RERESOLVE_DELAY 86400000
  113. // Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs
  114. #define ZT_TCP_FALLBACK_AFTER 60000
  115. // How often to check for local interface addresses
  116. #define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
  117. namespace ZeroTier {
  118. namespace {
  119. #ifdef ZT_AUTO_UPDATE
  120. #define ZT_AUTO_UPDATE_MAX_HTTP_RESPONSE_SIZE (1024 * 1024 * 64)
  121. #define ZT_AUTO_UPDATE_CHECK_PERIOD 21600000
  122. class BackgroundSoftwareUpdateChecker
  123. {
  124. public:
  125. bool isValidSigningIdentity(const Identity &id)
  126. {
  127. return (
  128. /* 0001 - 0004 : obsolete, used in old versions */
  129. /* 0005 */ (id == Identity("ba57ea350e:0:9d4be6d7f86c5660d5ee1951a3d759aa6e12a84fc0c0b74639500f1dbc1a8c566622e7d1c531967ebceb1e9d1761342f88324a8ba520c93c35f92f35080fa23f"))
  130. /* 0006 */ ||(id == Identity("5067b21b83:0:8af477730f5055c48135b84bed6720a35bca4c0e34be4060a4c636288b1ec22217eb22709d610c66ed464c643130c51411bbb0294eef12fbe8ecc1a1e2c63a7a"))
  131. /* 0007 */ ||(id == Identity("4f5e97a8f1:0:57880d056d7baeb04bbc057d6f16e6cb41388570e87f01492fce882485f65a798648595610a3ad49885604e7fb1db2dd3c2c534b75e42c3c0b110ad07b4bb138"))
  132. /* 0008 */ ||(id == Identity("580bbb8e15:0:ad5ef31155bebc6bc413991992387e083fed26d699997ef76e7c947781edd47d1997161fa56ba337b1a2b44b129fd7c7197ce5185382f06011bc88d1363b4ddd"))
  133. );
  134. }
  135. void doUpdateCheck()
  136. {
  137. std::string url(OneService::autoUpdateUrl());
  138. if ((url.length() <= 7)||(url.substr(0,7) != "http://"))
  139. return;
  140. std::string httpHost;
  141. std::string httpPath;
  142. {
  143. std::size_t slashIdx = url.substr(7).find_first_of('/');
  144. if (slashIdx == std::string::npos) {
  145. httpHost = url.substr(7);
  146. httpPath = "/";
  147. } else {
  148. httpHost = url.substr(7,slashIdx);
  149. httpPath = url.substr(slashIdx + 7);
  150. }
  151. }
  152. if (httpHost.length() == 0)
  153. return;
  154. std::vector<InetAddress> ips(OSUtils::resolve(httpHost.c_str()));
  155. for(std::vector<InetAddress>::iterator ip(ips.begin());ip!=ips.end();++ip) {
  156. if (!ip->port())
  157. ip->setPort(80);
  158. std::string nfoPath = httpPath + "LATEST.nfo";
  159. std::map<std::string,std::string> requestHeaders,responseHeaders;
  160. std::string body;
  161. requestHeaders["Host"] = httpHost;
  162. unsigned int scode = Http::GET(ZT_AUTO_UPDATE_MAX_HTTP_RESPONSE_SIZE,60000,reinterpret_cast<const struct sockaddr *>(&(*ip)),nfoPath.c_str(),requestHeaders,responseHeaders,body);
  163. //fprintf(stderr,"UPDATE %s %s %u %lu\n",ip->toString().c_str(),nfoPath.c_str(),scode,body.length());
  164. if ((scode == 200)&&(body.length() > 0)) {
  165. /* NFO fields:
  166. *
  167. * file=<filename>
  168. * signedBy=<signing identity>
  169. * ed25519=<ed25519 ECC signature of archive>
  170. * vMajor=<major version>
  171. * vMinor=<minor version>
  172. * vRevision=<revision> */
  173. Dictionary nfo(body);
  174. unsigned int vMajor = Utils::strToUInt(nfo.get("vMajor","0").c_str());
  175. unsigned int vMinor = Utils::strToUInt(nfo.get("vMinor","0").c_str());
  176. unsigned int vRevision = Utils::strToUInt(nfo.get("vRevision","0").c_str());
  177. if (Utils::compareVersion(vMajor,vMinor,vRevision,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION) <= 0) {
  178. //fprintf(stderr,"UPDATE %u.%u.%u is not newer than our version\n",vMajor,vMinor,vRevision);
  179. return;
  180. }
  181. Identity signedBy;
  182. if ((!signedBy.fromString(nfo.get("signedBy","")))||(!isValidSigningIdentity(signedBy))) {
  183. //fprintf(stderr,"UPDATE invalid signedBy or not authorized signing identity.\n");
  184. return;
  185. }
  186. std::string filePath(nfo.get("file",""));
  187. if ((!filePath.length())||(filePath.find("..") != std::string::npos))
  188. return;
  189. filePath = httpPath + filePath;
  190. std::string fileData;
  191. if (Http::GET(ZT_AUTO_UPDATE_MAX_HTTP_RESPONSE_SIZE,60000,reinterpret_cast<const struct sockaddr *>(&(*ip)),filePath.c_str(),requestHeaders,responseHeaders,fileData) != 200) {
  192. //fprintf(stderr,"UPDATE GET %s failed\n",filePath.c_str());
  193. return;
  194. }
  195. std::string ed25519(Utils::unhex(nfo.get("ed25519","")));
  196. if ((ed25519.length() == 0)||(!signedBy.verify(fileData.data(),(unsigned int)fileData.length(),ed25519.data(),(unsigned int)ed25519.length()))) {
  197. //fprintf(stderr,"UPDATE %s failed signature check!\n",filePath.c_str());
  198. return;
  199. }
  200. /* --------------------------------------------------------------- */
  201. /* We made it! Begin OS-specific installation code. */
  202. #ifdef __APPLE__
  203. /* OSX version is in the form of a MacOSX .pkg file, so we will
  204. * launch installer (normally in /usr/sbin) to install it. It will
  205. * then turn around and shut down the service, update files, and
  206. * relaunch. */
  207. {
  208. char bashp[128],pkgp[128];
  209. Utils::snprintf(bashp,sizeof(bashp),"/tmp/ZeroTierOne-update-%u.%u.%u.sh",vMajor,vMinor,vRevision);
  210. Utils::snprintf(pkgp,sizeof(pkgp),"/tmp/ZeroTierOne-update-%u.%u.%u.pkg",vMajor,vMinor,vRevision);
  211. FILE *pkg = fopen(pkgp,"w");
  212. if ((!pkg)||(fwrite(fileData.data(),fileData.length(),1,pkg) != 1)) {
  213. fclose(pkg);
  214. unlink(bashp);
  215. unlink(pkgp);
  216. fprintf(stderr,"UPDATE error writing %s\n",pkgp);
  217. return;
  218. }
  219. fclose(pkg);
  220. FILE *bash = fopen(bashp,"w");
  221. if (!bash) {
  222. fclose(pkg);
  223. unlink(bashp);
  224. unlink(pkgp);
  225. fprintf(stderr,"UPDATE error writing %s\n",bashp);
  226. return;
  227. }
  228. fprintf(bash,
  229. "#!/bin/bash\n"
  230. "export PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin\n"
  231. "sleep 1\n"
  232. "installer -pkg \"%s\" -target /\n"
  233. "sleep 1\n"
  234. "rm -f \"%s\" \"%s\"\n"
  235. "exit 0\n",
  236. pkgp,
  237. pkgp,
  238. bashp);
  239. fclose(bash);
  240. long pid = (long)vfork();
  241. if (pid == 0) {
  242. setsid(); // detach from parent so that shell isn't killed when parent is killed
  243. signal(SIGHUP,SIG_IGN);
  244. signal(SIGTERM,SIG_IGN);
  245. signal(SIGQUIT,SIG_IGN);
  246. execl("/bin/bash","/bin/bash",bashp,(char *)0);
  247. exit(0);
  248. }
  249. }
  250. #endif // __APPLE__
  251. #ifdef __WINDOWS__
  252. /* Windows version comes in the form of .MSI package that
  253. * takes care of everything. */
  254. {
  255. char tempp[512],batp[512],msip[512],cmdline[512];
  256. if (GetTempPathA(sizeof(tempp),tempp) <= 0)
  257. return;
  258. CreateDirectoryA(tempp,(LPSECURITY_ATTRIBUTES)0);
  259. Utils::snprintf(batp,sizeof(batp),"%s\\ZeroTierOne-update-%u.%u.%u.bat",tempp,vMajor,vMinor,vRevision);
  260. Utils::snprintf(msip,sizeof(msip),"%s\\ZeroTierOne-update-%u.%u.%u.msi",tempp,vMajor,vMinor,vRevision);
  261. FILE *msi = fopen(msip,"wb");
  262. if ((!msi)||(fwrite(fileData.data(),(size_t)fileData.length(),1,msi) != 1)) {
  263. fclose(msi);
  264. return;
  265. }
  266. fclose(msi);
  267. FILE *bat = fopen(batp,"wb");
  268. if (!bat)
  269. return;
  270. fprintf(bat,
  271. "TIMEOUT.EXE /T 1 /NOBREAK\r\n"
  272. "NET.EXE STOP \"ZeroTierOneService\"\r\n"
  273. "TIMEOUT.EXE /T 1 /NOBREAK\r\n"
  274. "MSIEXEC.EXE /i \"%s\" /qn\r\n"
  275. "TIMEOUT.EXE /T 1 /NOBREAK\r\n"
  276. "NET.EXE START \"ZeroTierOneService\"\r\n"
  277. "DEL \"%s\"\r\n"
  278. "DEL \"%s\"\r\n",
  279. msip,
  280. msip,
  281. batp);
  282. fclose(bat);
  283. STARTUPINFOA si;
  284. PROCESS_INFORMATION pi;
  285. memset(&si,0,sizeof(si));
  286. memset(&pi,0,sizeof(pi));
  287. Utils::snprintf(cmdline,sizeof(cmdline),"CMD.EXE /c \"%s\"",batp);
  288. CreateProcessA(NULL,cmdline,NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi);
  289. }
  290. #endif // __WINDOWS__
  291. /* --------------------------------------------------------------- */
  292. return;
  293. } // else try to fetch from next IP address
  294. }
  295. }
  296. void threadMain()
  297. throw()
  298. {
  299. try {
  300. this->doUpdateCheck();
  301. } catch ( ... ) {}
  302. }
  303. };
  304. static BackgroundSoftwareUpdateChecker backgroundSoftwareUpdateChecker;
  305. #endif // ZT_AUTO_UPDATE
  306. static bool isBlacklistedLocalInterfaceForZeroTierTraffic(const char *ifn)
  307. {
  308. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  309. if ((ifn[0] == 'l')&&(ifn[1] == 'o')) return true; // loopback
  310. if ((ifn[0] == 'z')&&(ifn[1] == 't')) return true; // sanity check: zt#
  311. if ((ifn[0] == 't')&&(ifn[1] == 'u')&&(ifn[2] == 'n')) return true; // tun# is probably an OpenVPN tunnel or similar
  312. if ((ifn[0] == 't')&&(ifn[1] == 'a')&&(ifn[2] == 'p')) return true; // tap# is probably an OpenVPN tunnel or similar
  313. #endif
  314. #ifdef __APPLE__
  315. if ((ifn[0] == 'l')&&(ifn[1] == 'o')) return true; // loopback
  316. if ((ifn[0] == 'z')&&(ifn[1] == 't')) return true; // sanity check: zt#
  317. if ((ifn[0] == 't')&&(ifn[1] == 'u')&&(ifn[2] == 'n')) return true; // tun# is probably an OpenVPN tunnel or similar
  318. if ((ifn[0] == 't')&&(ifn[1] == 'a')&&(ifn[2] == 'p')) return true; // tap# is probably an OpenVPN tunnel or similar
  319. if ((ifn[0] == 'u')&&(ifn[1] == 't')&&(ifn[2] == 'u')&&(ifn[3] == 'n')) return true; // ... as is utun#
  320. #endif
  321. return false;
  322. }
  323. static std::string _trimString(const std::string &s)
  324. {
  325. unsigned long end = (unsigned long)s.length();
  326. while (end) {
  327. char c = s[end - 1];
  328. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  329. --end;
  330. else break;
  331. }
  332. unsigned long start = 0;
  333. while (start < end) {
  334. char c = s[start];
  335. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  336. ++start;
  337. else break;
  338. }
  339. return s.substr(start,end - start);
  340. }
  341. class OneServiceImpl;
  342. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf);
  343. static void SnodeEventCallback(ZT_Node *node,void *uptr,enum ZT_Event event,const void *metaData);
  344. static long SnodeDataStoreGetFunction(ZT_Node *node,void *uptr,const char *name,void *buf,unsigned long bufSize,unsigned long readIndex,unsigned long *totalSize);
  345. static int SnodeDataStorePutFunction(ZT_Node *node,void *uptr,const char *name,const void *data,unsigned long len,int secure);
  346. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,const struct sockaddr_storage *localAddr,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl);
  347. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  348. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,const struct sockaddr_storage *localAddr,const struct sockaddr_storage *remoteAddr);
  349. #ifdef ZT_ENABLE_CLUSTER
  350. static void SclusterSendFunction(void *uptr,unsigned int toMemberId,const void *data,unsigned int len);
  351. static int SclusterGeoIpFunction(void *uptr,const struct sockaddr_storage *addr,int *x,int *y,int *z);
  352. #endif
  353. static void StapFrameHandler(void *uptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  354. static int ShttpOnMessageBegin(http_parser *parser);
  355. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  356. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  357. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  358. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  359. static int ShttpOnHeadersComplete(http_parser *parser);
  360. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  361. static int ShttpOnMessageComplete(http_parser *parser);
  362. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  363. ShttpOnMessageBegin,
  364. ShttpOnUrl,
  365. ShttpOnStatus,
  366. ShttpOnHeaderField,
  367. ShttpOnValue,
  368. ShttpOnHeadersComplete,
  369. ShttpOnBody,
  370. ShttpOnMessageComplete
  371. };
  372. struct TcpConnection
  373. {
  374. enum {
  375. TCP_HTTP_INCOMING,
  376. TCP_HTTP_OUTGOING, // not currently used
  377. TCP_TUNNEL_OUTGOING // fale-SSL outgoing tunnel -- HTTP-related fields are not used
  378. } type;
  379. bool shouldKeepAlive;
  380. OneServiceImpl *parent;
  381. PhySocket *sock;
  382. InetAddress from;
  383. http_parser parser;
  384. unsigned long messageSize;
  385. uint64_t lastActivity;
  386. std::string currentHeaderField;
  387. std::string currentHeaderValue;
  388. std::string url;
  389. std::string status;
  390. std::map< std::string,std::string > headers;
  391. std::string body;
  392. std::string writeBuf;
  393. Mutex writeBuf_m;
  394. };
  395. // Used to pseudo-randomize local source port picking
  396. static volatile unsigned int _udpPortPickerCounter = 0;
  397. class OneServiceImpl : public OneService
  398. {
  399. public:
  400. // begin member variables --------------------------------------------------
  401. const std::string _homePath;
  402. BackgroundResolver _tcpFallbackResolver;
  403. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  404. SqliteNetworkController *_controller;
  405. #endif
  406. Phy<OneServiceImpl *> _phy;
  407. Node *_node;
  408. /*
  409. * To properly handle NAT/gateway craziness we use three local UDP ports:
  410. *
  411. * [0] is the normal/default port, usually 9993
  412. * [1] is a port dervied from our ZeroTier address
  413. * [2] is a port computed from the normal/default for use with uPnP/NAT-PMP mappings
  414. *
  415. * [2] exists because on some gateways trying to do regular NAT-t interferes
  416. * destructively with uPnP port mapping behavior in very weird buggy ways.
  417. * It's only used if uPnP/NAT-PMP is enabled in this build.
  418. */
  419. Binder _bindings[3];
  420. unsigned int _ports[3];
  421. uint16_t _portsBE[3]; // ports in big-endian network byte order as in sockaddr
  422. // Sockets for JSON API -- bound only to V4 and V6 localhost
  423. PhySocket *_v4TcpControlSocket;
  424. PhySocket *_v6TcpControlSocket;
  425. // JSON API handler
  426. ControlPlane *_controlPlane;
  427. // Time we last received a packet from a global address
  428. uint64_t _lastDirectReceiveFromGlobal;
  429. #ifdef ZT_TCP_FALLBACK_RELAY
  430. uint64_t _lastSendToGlobalV4;
  431. #endif
  432. // Last potential sleep/wake event
  433. uint64_t _lastRestart;
  434. // Deadline for the next background task service function
  435. volatile uint64_t _nextBackgroundTaskDeadline;
  436. // Tap devices by network ID
  437. std::map< uint64_t,EthernetTap * > _taps;
  438. std::map< uint64_t,std::vector<InetAddress> > _tapAssignedIps; // ZeroTier assigned IPs, not user or dhcp assigned
  439. Mutex _taps_m;
  440. // Active TCP/IP connections
  441. std::set< TcpConnection * > _tcpConnections; // no mutex for this since it's done in the main loop thread only
  442. TcpConnection *_tcpFallbackTunnel;
  443. // Termination status information
  444. ReasonForTermination _termReason;
  445. std::string _fatalErrorMessage;
  446. Mutex _termReason_m;
  447. // uPnP/NAT-PMP port mapper if enabled
  448. #ifdef ZT_USE_MINIUPNPC
  449. PortMapper *_portMapper;
  450. #endif
  451. // Cluster management instance if enabled
  452. #ifdef ZT_ENABLE_CLUSTER
  453. PhySocket *_clusterMessageSocket;
  454. ClusterGeoIpService *_clusterGeoIpService;
  455. ClusterDefinition *_clusterDefinition;
  456. unsigned int _clusterMemberId;
  457. #endif
  458. // Set to false to force service to stop
  459. volatile bool _run;
  460. Mutex _run_m;
  461. // end member variables ----------------------------------------------------
  462. OneServiceImpl(const char *hp,unsigned int port) :
  463. _homePath((hp) ? hp : ".")
  464. ,_tcpFallbackResolver(ZT_TCP_FALLBACK_RELAY)
  465. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  466. ,_controller((SqliteNetworkController *)0)
  467. #endif
  468. ,_phy(this,false,true)
  469. ,_node((Node *)0)
  470. ,_controlPlane((ControlPlane *)0)
  471. ,_lastDirectReceiveFromGlobal(0)
  472. #ifdef ZT_TCP_FALLBACK_RELAY
  473. ,_lastSendToGlobalV4(0)
  474. #endif
  475. ,_lastRestart(0)
  476. ,_nextBackgroundTaskDeadline(0)
  477. ,_tcpFallbackTunnel((TcpConnection *)0)
  478. ,_termReason(ONE_STILL_RUNNING)
  479. #ifdef ZT_USE_MINIUPNPC
  480. ,_portMapper((PortMapper *)0)
  481. #endif
  482. #ifdef ZT_ENABLE_CLUSTER
  483. ,_clusterMessageSocket((PhySocket *)0)
  484. ,_clusterGeoIpService((ClusterGeoIpService *)0)
  485. ,_clusterDefinition((ClusterDefinition *)0)
  486. ,_clusterMemberId(0)
  487. #endif
  488. ,_run(true)
  489. {
  490. _ports[0] = 0;
  491. _ports[1] = 0;
  492. _ports[2] = 0;
  493. // The control socket is bound to the default/static port on localhost. If we
  494. // can do this, we have successfully allocated a port. The binders will take
  495. // care of binding non-local addresses for ZeroTier traffic.
  496. const int portTrials = (port == 0) ? 256 : 1; // if port is 0, pick random
  497. for(int k=0;k<portTrials;++k) {
  498. if (port == 0) {
  499. unsigned int randp = 0;
  500. Utils::getSecureRandom(&randp,sizeof(randp));
  501. port = 20000 + (randp % 45500);
  502. }
  503. if (_trialBind(port)) {
  504. struct sockaddr_in in4;
  505. memset(&in4,0,sizeof(in4));
  506. in4.sin_family = AF_INET;
  507. in4.sin_addr.s_addr = Utils::hton((uint32_t)0x7f000001); // right now we just listen for TCP @127.0.0.1
  508. in4.sin_port = Utils::hton((uint16_t)port);
  509. _v4TcpControlSocket = _phy.tcpListen((const struct sockaddr *)&in4,this);
  510. struct sockaddr_in6 in6;
  511. memset((void *)&in6,0,sizeof(in6));
  512. in6.sin6_family = AF_INET6;
  513. in6.sin6_port = in4.sin_port;
  514. in6.sin6_addr.s6_addr[15] = 1; // IPv6 localhost == ::1
  515. _v6TcpControlSocket = _phy.tcpListen((const struct sockaddr *)&in6,this);
  516. // We must bind one of IPv4 or IPv6 -- support either failing to support hosts that
  517. // have only IPv4 or only IPv6 stacks.
  518. if ((_v4TcpControlSocket)||(_v6TcpControlSocket)) {
  519. _ports[0] = port;
  520. break;
  521. } else {
  522. if (_v4TcpControlSocket)
  523. _phy.close(_v4TcpControlSocket,false);
  524. if (_v6TcpControlSocket)
  525. _phy.close(_v6TcpControlSocket,false);
  526. port = 0;
  527. }
  528. } else {
  529. port = 0;
  530. }
  531. }
  532. if (_ports[0] == 0)
  533. throw std::runtime_error("cannot bind to local control interface port");
  534. char portstr[64];
  535. Utils::snprintf(portstr,sizeof(portstr),"%u",_ports[0]);
  536. OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S + "zerotier-one.port").c_str(),std::string(portstr));
  537. }
  538. virtual ~OneServiceImpl()
  539. {
  540. for(int i=0;i<3;++i)
  541. _bindings[i].closeAll(_phy);
  542. _phy.close(_v4TcpControlSocket);
  543. _phy.close(_v6TcpControlSocket);
  544. #ifdef ZT_ENABLE_CLUSTER
  545. _phy.close(_clusterMessageSocket);
  546. #endif
  547. #ifdef ZT_USE_MINIUPNPC
  548. delete _portMapper;
  549. #endif
  550. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  551. delete _controller;
  552. #endif
  553. #ifdef ZT_ENABLE_CLUSTER
  554. delete _clusterGeoIpService;
  555. delete _clusterDefinition;
  556. #endif
  557. }
  558. virtual ReasonForTermination run()
  559. {
  560. try {
  561. std::string authToken;
  562. {
  563. std::string authTokenPath(_homePath + ZT_PATH_SEPARATOR_S + "authtoken.secret");
  564. if (!OSUtils::readFile(authTokenPath.c_str(),authToken)) {
  565. unsigned char foo[24];
  566. Utils::getSecureRandom(foo,sizeof(foo));
  567. authToken = "";
  568. for(unsigned int i=0;i<sizeof(foo);++i)
  569. authToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  570. if (!OSUtils::writeFile(authTokenPath.c_str(),authToken)) {
  571. Mutex::Lock _l(_termReason_m);
  572. _termReason = ONE_UNRECOVERABLE_ERROR;
  573. _fatalErrorMessage = "authtoken.secret could not be written";
  574. return _termReason;
  575. } else {
  576. OSUtils::lockDownFile(authTokenPath.c_str(),false);
  577. }
  578. }
  579. }
  580. authToken = _trimString(authToken);
  581. _node = new Node(
  582. OSUtils::now(),
  583. this,
  584. SnodeDataStoreGetFunction,
  585. SnodeDataStorePutFunction,
  586. SnodeWirePacketSendFunction,
  587. SnodeVirtualNetworkFrameFunction,
  588. SnodeVirtualNetworkConfigFunction,
  589. SnodePathCheckFunction,
  590. SnodeEventCallback);
  591. // Attempt to bind to a secondary port chosen from our ZeroTier address.
  592. // This exists because there are buggy NATs out there that fail if more
  593. // than one device behind the same NAT tries to use the same internal
  594. // private address port number.
  595. _ports[1] = 20000 + ((unsigned int)_node->address() % 45500);
  596. for(int i=0;;++i) {
  597. if (i > 1000) {
  598. _ports[1] = 0;
  599. break;
  600. } else if (++_ports[1] >= 65536) {
  601. _ports[1] = 20000;
  602. }
  603. if (_trialBind(_ports[1]))
  604. break;
  605. }
  606. #ifdef ZT_USE_MINIUPNPC
  607. // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
  608. // use the other two ports for that because some NATs do really funky
  609. // stuff with ports that are explicitly mapped that breaks things.
  610. if (_ports[1]) {
  611. _ports[2] = _ports[1];
  612. for(int i=0;;++i) {
  613. if (i > 1000) {
  614. _ports[2] = 0;
  615. break;
  616. } else if (++_ports[2] >= 65536) {
  617. _ports[2] = 20000;
  618. }
  619. if (_trialBind(_ports[2]))
  620. break;
  621. }
  622. if (_ports[2]) {
  623. char uniqueName[64];
  624. Utils::snprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
  625. _portMapper = new PortMapper(_ports[2],uniqueName);
  626. }
  627. }
  628. #endif
  629. for(int i=0;i<3;++i)
  630. _portsBE[i] = Utils::hton((uint16_t)_ports[i]);
  631. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  632. _controller = new SqliteNetworkController(_node,(_homePath + ZT_PATH_SEPARATOR_S + ZT_CONTROLLER_DB_PATH).c_str(),(_homePath + ZT_PATH_SEPARATOR_S + "circuitTestResults.d").c_str());
  633. _node->setNetconfMaster((void *)_controller);
  634. #endif
  635. #ifdef ZT_ENABLE_CLUSTER
  636. if (OSUtils::fileExists((_homePath + ZT_PATH_SEPARATOR_S + "cluster").c_str())) {
  637. _clusterDefinition = new ClusterDefinition(_node->address(),(_homePath + ZT_PATH_SEPARATOR_S + "cluster").c_str());
  638. if (_clusterDefinition->size() > 0) {
  639. std::vector<ClusterDefinition::MemberDefinition> members(_clusterDefinition->members());
  640. for(std::vector<ClusterDefinition::MemberDefinition>::iterator m(members.begin());m!=members.end();++m) {
  641. PhySocket *cs = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&(m->clusterEndpoint)));
  642. if (cs) {
  643. if (_clusterMessageSocket) {
  644. _phy.close(_clusterMessageSocket,false);
  645. _phy.close(cs,false);
  646. Mutex::Lock _l(_termReason_m);
  647. _termReason = ONE_UNRECOVERABLE_ERROR;
  648. _fatalErrorMessage = "Cluster: can't determine my cluster member ID: able to bind more than one cluster message socket IP/port!";
  649. return _termReason;
  650. }
  651. _clusterMessageSocket = cs;
  652. _clusterMemberId = m->id;
  653. }
  654. }
  655. if (!_clusterMessageSocket) {
  656. Mutex::Lock _l(_termReason_m);
  657. _termReason = ONE_UNRECOVERABLE_ERROR;
  658. _fatalErrorMessage = "Cluster: can't determine my cluster member ID: unable to bind to any cluster message socket IP/port.";
  659. return _termReason;
  660. }
  661. if (OSUtils::fileExists((_homePath + ZT_PATH_SEPARATOR_S + "cluster-geo.exe").c_str()))
  662. _clusterGeoIpService = new ClusterGeoIpService((_homePath + ZT_PATH_SEPARATOR_S + "cluster-geo.exe").c_str());
  663. const ClusterDefinition::MemberDefinition &me = (*_clusterDefinition)[_clusterMemberId];
  664. InetAddress endpoints[255];
  665. unsigned int numEndpoints = 0;
  666. for(std::vector<InetAddress>::const_iterator i(me.zeroTierEndpoints.begin());i!=me.zeroTierEndpoints.end();++i)
  667. endpoints[numEndpoints++] = *i;
  668. if (_node->clusterInit(
  669. _clusterMemberId,
  670. reinterpret_cast<const struct sockaddr_storage *>(endpoints),
  671. numEndpoints,
  672. me.x,
  673. me.y,
  674. me.z,
  675. &SclusterSendFunction,
  676. this,
  677. (_clusterGeoIpService) ? &SclusterGeoIpFunction : 0,
  678. this) == ZT_RESULT_OK) {
  679. std::vector<ClusterDefinition::MemberDefinition> members(_clusterDefinition->members());
  680. for(std::vector<ClusterDefinition::MemberDefinition>::iterator m(members.begin());m!=members.end();++m) {
  681. if (m->id != _clusterMemberId)
  682. _node->clusterAddMember(m->id);
  683. }
  684. }
  685. } else {
  686. delete _clusterDefinition;
  687. _clusterDefinition = (ClusterDefinition *)0;
  688. }
  689. }
  690. #endif
  691. _controlPlane = new ControlPlane(this,_node,(_homePath + ZT_PATH_SEPARATOR_S + "ui").c_str());
  692. _controlPlane->addAuthToken(authToken.c_str());
  693. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  694. _controlPlane->setController(_controller);
  695. #endif
  696. { // Remember networks from previous session
  697. std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str()));
  698. for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
  699. std::size_t dot = f->find_last_of('.');
  700. if ((dot == 16)&&(f->substr(16) == ".conf"))
  701. _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0);
  702. }
  703. }
  704. // Start two background threads to handle expensive ops out of line
  705. Thread::start(_node);
  706. Thread::start(_node);
  707. _nextBackgroundTaskDeadline = 0;
  708. uint64_t clockShouldBe = OSUtils::now();
  709. _lastRestart = clockShouldBe;
  710. uint64_t lastTapMulticastGroupCheck = 0;
  711. uint64_t lastTcpFallbackResolve = 0;
  712. uint64_t lastBindRefresh = 0;
  713. uint64_t lastLocalInterfaceAddressCheck = (OSUtils::now() - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to configure and other things time to settle
  714. #ifdef ZT_AUTO_UPDATE
  715. uint64_t lastSoftwareUpdateCheck = 0;
  716. #endif // ZT_AUTO_UPDATE
  717. for(;;) {
  718. _run_m.lock();
  719. if (!_run) {
  720. _run_m.unlock();
  721. _termReason_m.lock();
  722. _termReason = ONE_NORMAL_TERMINATION;
  723. _termReason_m.unlock();
  724. break;
  725. } else {
  726. _run_m.unlock();
  727. }
  728. const uint64_t now = OSUtils::now();
  729. // Attempt to detect sleep/wake events by detecting delay overruns
  730. bool restarted = false;
  731. if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
  732. _lastRestart = now;
  733. restarted = true;
  734. }
  735. // Refresh bindings in case device's interfaces have changed
  736. if (((now - lastBindRefresh) >= ZT_BINDER_REFRESH_PERIOD)||(restarted)) {
  737. lastBindRefresh = now;
  738. for(int i=0;i<3;++i) {
  739. if (_ports[i]) {
  740. _bindings[i].refresh(_phy,_ports[i],*this);
  741. }
  742. }
  743. }
  744. uint64_t dl = _nextBackgroundTaskDeadline;
  745. if (dl <= now) {
  746. _node->processBackgroundTasks(now,&_nextBackgroundTaskDeadline);
  747. dl = _nextBackgroundTaskDeadline;
  748. }
  749. #ifdef ZT_AUTO_UPDATE
  750. if ((now - lastSoftwareUpdateCheck) >= ZT_AUTO_UPDATE_CHECK_PERIOD) {
  751. lastSoftwareUpdateCheck = now;
  752. Thread::start(&backgroundSoftwareUpdateChecker);
  753. }
  754. #endif // ZT_AUTO_UPDATE
  755. if ((now - lastTcpFallbackResolve) >= ZT_TCP_FALLBACK_RERESOLVE_DELAY) {
  756. lastTcpFallbackResolve = now;
  757. _tcpFallbackResolver.resolveNow();
  758. }
  759. if ((_tcpFallbackTunnel)&&((now - _lastDirectReceiveFromGlobal) < (ZT_TCP_FALLBACK_AFTER / 2)))
  760. _phy.close(_tcpFallbackTunnel->sock);
  761. if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) {
  762. lastTapMulticastGroupCheck = now;
  763. Mutex::Lock _l(_taps_m);
  764. for(std::map< uint64_t,EthernetTap *>::const_iterator t(_taps.begin());t!=_taps.end();++t) {
  765. std::vector<MulticastGroup> added,removed;
  766. t->second->scanMulticastGroups(added,removed);
  767. for(std::vector<MulticastGroup>::iterator m(added.begin());m!=added.end();++m)
  768. _node->multicastSubscribe(t->first,m->mac().toInt(),m->adi());
  769. for(std::vector<MulticastGroup>::iterator m(removed.begin());m!=removed.end();++m)
  770. _node->multicastUnsubscribe(t->first,m->mac().toInt(),m->adi());
  771. }
  772. }
  773. if ((now - lastLocalInterfaceAddressCheck) >= ZT_LOCAL_INTERFACE_CHECK_INTERVAL) {
  774. lastLocalInterfaceAddressCheck = now;
  775. _node->clearLocalInterfaceAddresses();
  776. #ifdef ZT_USE_MINIUPNPC
  777. if (_portMapper) {
  778. std::vector<InetAddress> mappedAddresses(_portMapper->get());
  779. for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
  780. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
  781. }
  782. #endif
  783. std::vector<InetAddress> boundAddrs(_bindings[0].allBoundLocalInterfaceAddresses());
  784. for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i)
  785. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
  786. }
  787. const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100;
  788. clockShouldBe = now + (uint64_t)delay;
  789. _phy.poll(delay);
  790. }
  791. } catch (std::exception &exc) {
  792. Mutex::Lock _l(_termReason_m);
  793. _termReason = ONE_UNRECOVERABLE_ERROR;
  794. _fatalErrorMessage = exc.what();
  795. } catch ( ... ) {
  796. Mutex::Lock _l(_termReason_m);
  797. _termReason = ONE_UNRECOVERABLE_ERROR;
  798. _fatalErrorMessage = "unexpected exception in main thread";
  799. }
  800. try {
  801. while (!_tcpConnections.empty())
  802. _phy.close((*_tcpConnections.begin())->sock);
  803. } catch ( ... ) {}
  804. {
  805. Mutex::Lock _l(_taps_m);
  806. for(std::map< uint64_t,EthernetTap * >::iterator t(_taps.begin());t!=_taps.end();++t)
  807. delete t->second;
  808. _taps.clear();
  809. }
  810. delete _controlPlane;
  811. _controlPlane = (ControlPlane *)0;
  812. delete _node;
  813. _node = (Node *)0;
  814. return _termReason;
  815. }
  816. virtual ReasonForTermination reasonForTermination() const
  817. {
  818. Mutex::Lock _l(_termReason_m);
  819. return _termReason;
  820. }
  821. virtual std::string fatalErrorMessage() const
  822. {
  823. Mutex::Lock _l(_termReason_m);
  824. return _fatalErrorMessage;
  825. }
  826. virtual std::string portDeviceName(uint64_t nwid) const
  827. {
  828. Mutex::Lock _l(_taps_m);
  829. std::map< uint64_t,EthernetTap * >::const_iterator t(_taps.find(nwid));
  830. if (t != _taps.end())
  831. return t->second->deviceName();
  832. return std::string();
  833. }
  834. virtual bool tcpFallbackActive() const
  835. {
  836. return (_tcpFallbackTunnel != (TcpConnection *)0);
  837. }
  838. virtual void terminate()
  839. {
  840. _run_m.lock();
  841. _run = false;
  842. _run_m.unlock();
  843. _phy.whack();
  844. }
  845. // Begin private implementation methods
  846. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len)
  847. {
  848. #ifdef ZT_ENABLE_CLUSTER
  849. if (sock == _clusterMessageSocket) {
  850. _lastDirectReceiveFromGlobal = OSUtils::now();
  851. _node->clusterHandleIncomingMessage(data,len);
  852. return;
  853. }
  854. #endif
  855. #ifdef ZT_BREAK_UDP
  856. if (OSUtils::fileExists("/tmp/ZT_BREAK_UDP"))
  857. return;
  858. #endif
  859. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL))
  860. _lastDirectReceiveFromGlobal = OSUtils::now();
  861. const ZT_ResultCode rc = _node->processWirePacket(
  862. OSUtils::now(),
  863. reinterpret_cast<const struct sockaddr_storage *>(localAddr),
  864. (const struct sockaddr_storage *)from, // Phy<> uses sockaddr_storage, so it'll always be that big
  865. data,
  866. len,
  867. &_nextBackgroundTaskDeadline);
  868. if (ZT_ResultCode_isFatal(rc)) {
  869. char tmp[256];
  870. Utils::snprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  871. Mutex::Lock _l(_termReason_m);
  872. _termReason = ONE_UNRECOVERABLE_ERROR;
  873. _fatalErrorMessage = tmp;
  874. this->terminate();
  875. }
  876. }
  877. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  878. {
  879. if (!success)
  880. return;
  881. // Outgoing TCP connections are always TCP fallback tunnel connections.
  882. TcpConnection *tc = new TcpConnection();
  883. _tcpConnections.insert(tc);
  884. tc->type = TcpConnection::TCP_TUNNEL_OUTGOING;
  885. tc->shouldKeepAlive = true;
  886. tc->parent = this;
  887. tc->sock = sock;
  888. // from and parser are not used
  889. tc->messageSize = 0; // unused
  890. tc->lastActivity = OSUtils::now();
  891. // HTTP stuff is not used
  892. tc->writeBuf = "";
  893. *uptr = (void *)tc;
  894. // Send "hello" message
  895. tc->writeBuf.push_back((char)0x17);
  896. tc->writeBuf.push_back((char)0x03);
  897. tc->writeBuf.push_back((char)0x03); // fake TLS 1.2 header
  898. tc->writeBuf.push_back((char)0x00);
  899. tc->writeBuf.push_back((char)0x04); // mlen == 4
  900. tc->writeBuf.push_back((char)ZEROTIER_ONE_VERSION_MAJOR);
  901. tc->writeBuf.push_back((char)ZEROTIER_ONE_VERSION_MINOR);
  902. tc->writeBuf.push_back((char)((ZEROTIER_ONE_VERSION_REVISION >> 8) & 0xff));
  903. tc->writeBuf.push_back((char)(ZEROTIER_ONE_VERSION_REVISION & 0xff));
  904. _phy.setNotifyWritable(sock,true);
  905. _tcpFallbackTunnel = tc;
  906. }
  907. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  908. {
  909. if ((!from)||(reinterpret_cast<const InetAddress *>(from)->ipScope() != InetAddress::IP_SCOPE_LOOPBACK)) {
  910. // Non-Loopback: deny (for now)
  911. _phy.close(sockN,false);
  912. return;
  913. } else {
  914. // Loopback == HTTP JSON API request
  915. TcpConnection *tc = new TcpConnection();
  916. _tcpConnections.insert(tc);
  917. tc->type = TcpConnection::TCP_HTTP_INCOMING;
  918. tc->shouldKeepAlive = true;
  919. tc->parent = this;
  920. tc->sock = sockN;
  921. tc->from = from;
  922. http_parser_init(&(tc->parser),HTTP_REQUEST);
  923. tc->parser.data = (void *)tc;
  924. tc->messageSize = 0;
  925. tc->lastActivity = OSUtils::now();
  926. tc->currentHeaderField = "";
  927. tc->currentHeaderValue = "";
  928. tc->url = "";
  929. tc->status = "";
  930. tc->headers.clear();
  931. tc->body = "";
  932. tc->writeBuf = "";
  933. *uptrN = (void *)tc;
  934. }
  935. }
  936. inline void phyOnTcpClose(PhySocket *sock,void **uptr)
  937. {
  938. TcpConnection *tc = (TcpConnection *)*uptr;
  939. if (tc) {
  940. if (tc == _tcpFallbackTunnel)
  941. _tcpFallbackTunnel = (TcpConnection *)0;
  942. _tcpConnections.erase(tc);
  943. delete tc;
  944. }
  945. }
  946. inline void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  947. {
  948. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  949. switch(tc->type) {
  950. case TcpConnection::TCP_HTTP_INCOMING:
  951. case TcpConnection::TCP_HTTP_OUTGOING:
  952. http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len);
  953. if ((tc->parser.upgrade)||(tc->parser.http_errno != HPE_OK)) {
  954. _phy.close(sock);
  955. return;
  956. }
  957. break;
  958. case TcpConnection::TCP_TUNNEL_OUTGOING:
  959. tc->body.append((const char *)data,len);
  960. while (tc->body.length() >= 5) {
  961. const char *data = tc->body.data();
  962. const unsigned long mlen = ( ((((unsigned long)data[3]) & 0xff) << 8) | (((unsigned long)data[4]) & 0xff) );
  963. if (tc->body.length() >= (mlen + 5)) {
  964. InetAddress from;
  965. unsigned long plen = mlen; // payload length, modified if there's an IP header
  966. data += 5; // skip forward past pseudo-TLS junk and mlen
  967. if (plen == 4) {
  968. // Hello message, which isn't sent by proxy and would be ignored by client
  969. } else if (plen) {
  970. // Messages should contain IPv4 or IPv6 source IP address data
  971. switch(data[0]) {
  972. case 4: // IPv4
  973. if (plen >= 7) {
  974. from.set((const void *)(data + 1),4,((((unsigned int)data[5]) & 0xff) << 8) | (((unsigned int)data[6]) & 0xff));
  975. data += 7; // type + 4 byte IP + 2 byte port
  976. plen -= 7;
  977. } else {
  978. _phy.close(sock);
  979. return;
  980. }
  981. break;
  982. case 6: // IPv6
  983. if (plen >= 19) {
  984. from.set((const void *)(data + 1),16,((((unsigned int)data[17]) & 0xff) << 8) | (((unsigned int)data[18]) & 0xff));
  985. data += 19; // type + 16 byte IP + 2 byte port
  986. plen -= 19;
  987. } else {
  988. _phy.close(sock);
  989. return;
  990. }
  991. break;
  992. case 0: // none/omitted
  993. ++data;
  994. --plen;
  995. break;
  996. default: // invalid address type
  997. _phy.close(sock);
  998. return;
  999. }
  1000. if (from) {
  1001. InetAddress fakeTcpLocalInterfaceAddress((uint32_t)0xffffffff,0xffff);
  1002. const ZT_ResultCode rc = _node->processWirePacket(
  1003. OSUtils::now(),
  1004. reinterpret_cast<struct sockaddr_storage *>(&fakeTcpLocalInterfaceAddress),
  1005. reinterpret_cast<struct sockaddr_storage *>(&from),
  1006. data,
  1007. plen,
  1008. &_nextBackgroundTaskDeadline);
  1009. if (ZT_ResultCode_isFatal(rc)) {
  1010. char tmp[256];
  1011. Utils::snprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  1012. Mutex::Lock _l(_termReason_m);
  1013. _termReason = ONE_UNRECOVERABLE_ERROR;
  1014. _fatalErrorMessage = tmp;
  1015. this->terminate();
  1016. _phy.close(sock);
  1017. return;
  1018. }
  1019. }
  1020. }
  1021. if (tc->body.length() > (mlen + 5))
  1022. tc->body = tc->body.substr(mlen + 5);
  1023. else tc->body = "";
  1024. } else break;
  1025. }
  1026. break;
  1027. }
  1028. }
  1029. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  1030. {
  1031. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  1032. Mutex::Lock _l(tc->writeBuf_m);
  1033. if (tc->writeBuf.length() > 0) {
  1034. long sent = (long)_phy.streamSend(sock,tc->writeBuf.data(),(unsigned long)tc->writeBuf.length(),true);
  1035. if (sent > 0) {
  1036. tc->lastActivity = OSUtils::now();
  1037. if ((unsigned long)sent >= (unsigned long)tc->writeBuf.length()) {
  1038. tc->writeBuf = "";
  1039. _phy.setNotifyWritable(sock,false);
  1040. if (!tc->shouldKeepAlive)
  1041. _phy.close(sock); // will call close handler to delete from _tcpConnections
  1042. } else {
  1043. tc->writeBuf = tc->writeBuf.substr(sent);
  1044. }
  1045. }
  1046. } else {
  1047. _phy.setNotifyWritable(sock,false);
  1048. }
  1049. }
  1050. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  1051. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  1052. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  1053. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  1054. inline void phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked) {}
  1055. inline int nodeVirtualNetworkConfigFunction(uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwc)
  1056. {
  1057. Mutex::Lock _l(_taps_m);
  1058. std::map< uint64_t,EthernetTap * >::iterator t(_taps.find(nwid));
  1059. switch(op) {
  1060. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  1061. if (t == _taps.end()) {
  1062. try {
  1063. char friendlyName[1024];
  1064. Utils::snprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid);
  1065. t = _taps.insert(std::pair< uint64_t,EthernetTap *>(nwid,new EthernetTap(
  1066. _homePath.c_str(),
  1067. MAC(nwc->mac),
  1068. nwc->mtu,
  1069. (unsigned int)ZT_IF_METRIC,
  1070. nwid,
  1071. friendlyName,
  1072. StapFrameHandler,
  1073. (void *)this))).first;
  1074. *nuptr = (void *)t->second;
  1075. } catch (std::exception &exc) {
  1076. #ifdef __WINDOWS__
  1077. FILE *tapFailLog = fopen((_homePath + ZT_PATH_SEPARATOR_S"port_error_log.txt").c_str(),"a");
  1078. if (tapFailLog) {
  1079. fprintf(tapFailLog,"%.16llx: %s"ZT_EOL_S,(unsigned long long)nwid,exc.what());
  1080. fclose(tapFailLog);
  1081. }
  1082. #else
  1083. fprintf(stderr,"ERROR: unable to configure virtual network port: %s"ZT_EOL_S,exc.what());
  1084. #endif
  1085. return -999;
  1086. } catch ( ... ) {
  1087. return -999; // tap init failed
  1088. }
  1089. }
  1090. // fall through...
  1091. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  1092. if (t != _taps.end()) {
  1093. t->second->setEnabled(nwc->enabled != 0);
  1094. std::vector<InetAddress> &assignedIps = _tapAssignedIps[nwid];
  1095. std::vector<InetAddress> newAssignedIps;
  1096. for(unsigned int i=0;i<nwc->assignedAddressCount;++i)
  1097. newAssignedIps.push_back(InetAddress(nwc->assignedAddresses[i]));
  1098. std::sort(newAssignedIps.begin(),newAssignedIps.end());
  1099. newAssignedIps.erase(std::unique(newAssignedIps.begin(),newAssignedIps.end()),newAssignedIps.end());
  1100. for(std::vector<InetAddress>::iterator ip(newAssignedIps.begin());ip!=newAssignedIps.end();++ip) {
  1101. if (!std::binary_search(assignedIps.begin(),assignedIps.end(),*ip))
  1102. if (!t->second->addIp(*ip))
  1103. fprintf(stderr,"ERROR: unable to add ip address %s"ZT_EOL_S, ip->toString().c_str());
  1104. }
  1105. for(std::vector<InetAddress>::iterator ip(assignedIps.begin());ip!=assignedIps.end();++ip) {
  1106. if (!std::binary_search(newAssignedIps.begin(),newAssignedIps.end(),*ip))
  1107. if (!t->second->removeIp(*ip))
  1108. fprintf(stderr,"ERROR: unable to remove ip address %s"ZT_EOL_S, ip->toString().c_str());
  1109. }
  1110. assignedIps.swap(newAssignedIps);
  1111. } else {
  1112. return -999; // tap init failed
  1113. }
  1114. break;
  1115. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  1116. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  1117. if (t != _taps.end()) {
  1118. #ifdef __WINDOWS__
  1119. std::string winInstanceId(t->second->instanceId());
  1120. #endif
  1121. *nuptr = (void *)0;
  1122. delete t->second;
  1123. _taps.erase(t);
  1124. _tapAssignedIps.erase(nwid);
  1125. #ifdef __WINDOWS__
  1126. if ((op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY)&&(winInstanceId.length() > 0))
  1127. WindowsEthernetTap::deletePersistentTapDevice(winInstanceId.c_str());
  1128. #endif
  1129. }
  1130. break;
  1131. }
  1132. return 0;
  1133. }
  1134. inline void nodeEventCallback(enum ZT_Event event,const void *metaData)
  1135. {
  1136. switch(event) {
  1137. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  1138. Mutex::Lock _l(_termReason_m);
  1139. _termReason = ONE_IDENTITY_COLLISION;
  1140. _fatalErrorMessage = "identity/address collision";
  1141. this->terminate();
  1142. } break;
  1143. case ZT_EVENT_TRACE: {
  1144. if (metaData) {
  1145. ::fprintf(stderr,"%s"ZT_EOL_S,(const char *)metaData);
  1146. ::fflush(stderr);
  1147. }
  1148. } break;
  1149. default:
  1150. break;
  1151. }
  1152. }
  1153. inline long nodeDataStoreGetFunction(const char *name,void *buf,unsigned long bufSize,unsigned long readIndex,unsigned long *totalSize)
  1154. {
  1155. std::string p(_dataStorePrepPath(name));
  1156. if (!p.length())
  1157. return -2;
  1158. FILE *f = fopen(p.c_str(),"rb");
  1159. if (!f)
  1160. return -1;
  1161. if (fseek(f,0,SEEK_END) != 0) {
  1162. fclose(f);
  1163. return -2;
  1164. }
  1165. long ts = ftell(f);
  1166. if (ts < 0) {
  1167. fclose(f);
  1168. return -2;
  1169. }
  1170. *totalSize = (unsigned long)ts;
  1171. if (fseek(f,(long)readIndex,SEEK_SET) != 0) {
  1172. fclose(f);
  1173. return -2;
  1174. }
  1175. long n = (long)fread(buf,1,bufSize,f);
  1176. fclose(f);
  1177. return n;
  1178. }
  1179. inline int nodeDataStorePutFunction(const char *name,const void *data,unsigned long len,int secure)
  1180. {
  1181. std::string p(_dataStorePrepPath(name));
  1182. if (!p.length())
  1183. return -2;
  1184. if (!data) {
  1185. OSUtils::rm(p.c_str());
  1186. return 0;
  1187. }
  1188. FILE *f = fopen(p.c_str(),"wb");
  1189. if (!f)
  1190. return -1;
  1191. if (fwrite(data,len,1,f) == 1) {
  1192. fclose(f);
  1193. if (secure)
  1194. OSUtils::lockDownFile(p.c_str(),false);
  1195. return 0;
  1196. } else {
  1197. fclose(f);
  1198. OSUtils::rm(p.c_str());
  1199. return -1;
  1200. }
  1201. }
  1202. inline int nodeWirePacketSendFunction(const struct sockaddr_storage *localAddr,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  1203. {
  1204. unsigned int fromBindingNo = 0;
  1205. if (addr->ss_family == AF_INET) {
  1206. if (reinterpret_cast<const struct sockaddr_in *>(localAddr)->sin_port == 0) {
  1207. // If sender is sending from wildcard (null address), choose the secondary backup
  1208. // port 1/4 of the time. (but only for IPv4)
  1209. fromBindingNo = (++_udpPortPickerCounter & 0x4) >> 2;
  1210. if (!_ports[fromBindingNo])
  1211. fromBindingNo = 0;
  1212. } else {
  1213. const uint16_t lp = reinterpret_cast<const struct sockaddr_in *>(localAddr)->sin_port;
  1214. if (lp == _portsBE[1])
  1215. fromBindingNo = 1;
  1216. else if (lp == _portsBE[2])
  1217. fromBindingNo = 2;
  1218. }
  1219. #ifdef ZT_TCP_FALLBACK_RELAY
  1220. // TCP fallback tunnel support, currently IPv4 only
  1221. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(addr)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  1222. // Engage TCP tunnel fallback if we haven't received anything valid from a global
  1223. // IP address in ZT_TCP_FALLBACK_AFTER milliseconds. If we do start getting
  1224. // valid direct traffic we'll stop using it and close the socket after a while.
  1225. const uint64_t now = OSUtils::now();
  1226. if (((now - _lastDirectReceiveFromGlobal) > ZT_TCP_FALLBACK_AFTER)&&((now - _lastRestart) > ZT_TCP_FALLBACK_AFTER)) {
  1227. if (_tcpFallbackTunnel) {
  1228. Mutex::Lock _l(_tcpFallbackTunnel->writeBuf_m);
  1229. if (!_tcpFallbackTunnel->writeBuf.length())
  1230. _phy.setNotifyWritable(_tcpFallbackTunnel->sock,true);
  1231. unsigned long mlen = len + 7;
  1232. _tcpFallbackTunnel->writeBuf.push_back((char)0x17);
  1233. _tcpFallbackTunnel->writeBuf.push_back((char)0x03);
  1234. _tcpFallbackTunnel->writeBuf.push_back((char)0x03); // fake TLS 1.2 header
  1235. _tcpFallbackTunnel->writeBuf.push_back((char)((mlen >> 8) & 0xff));
  1236. _tcpFallbackTunnel->writeBuf.push_back((char)(mlen & 0xff));
  1237. _tcpFallbackTunnel->writeBuf.push_back((char)4); // IPv4
  1238. _tcpFallbackTunnel->writeBuf.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_addr.s_addr))),4);
  1239. _tcpFallbackTunnel->writeBuf.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_port))),2);
  1240. _tcpFallbackTunnel->writeBuf.append((const char *)data,len);
  1241. } else if (((now - _lastSendToGlobalV4) < ZT_TCP_FALLBACK_AFTER)&&((now - _lastSendToGlobalV4) > (ZT_PING_CHECK_INVERVAL / 2))) {
  1242. std::vector<InetAddress> tunnelIps(_tcpFallbackResolver.get());
  1243. if (tunnelIps.empty()) {
  1244. if (!_tcpFallbackResolver.running())
  1245. _tcpFallbackResolver.resolveNow();
  1246. } else {
  1247. bool connected = false;
  1248. InetAddress addr(tunnelIps[(unsigned long)now % tunnelIps.size()]);
  1249. addr.setPort(ZT_TCP_FALLBACK_RELAY_PORT);
  1250. _phy.tcpConnect(reinterpret_cast<const struct sockaddr *>(&addr),connected);
  1251. }
  1252. }
  1253. }
  1254. _lastSendToGlobalV4 = now;
  1255. }
  1256. #endif // ZT_TCP_FALLBACK_RELAY
  1257. } else if (addr->ss_family == AF_INET6) {
  1258. if (reinterpret_cast<const struct sockaddr_in6 *>(localAddr)->sin6_port != 0) {
  1259. const uint16_t lp = reinterpret_cast<const struct sockaddr_in6 *>(localAddr)->sin6_port;
  1260. if (lp == _portsBE[1])
  1261. fromBindingNo = 1;
  1262. else if (lp == _portsBE[2])
  1263. fromBindingNo = 2;
  1264. }
  1265. } else {
  1266. return -1;
  1267. }
  1268. #ifdef ZT_BREAK_UDP
  1269. if (OSUtils::fileExists("/tmp/ZT_BREAK_UDP"))
  1270. return 0; // silently break UDP
  1271. #endif
  1272. return (_bindings[fromBindingNo].udpSend(_phy,*(reinterpret_cast<const InetAddress *>(localAddr)),*(reinterpret_cast<const InetAddress *>(addr)),data,len,ttl)) ? 0 : -1;
  1273. }
  1274. 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)
  1275. {
  1276. EthernetTap *tap = reinterpret_cast<EthernetTap *>(*nuptr);
  1277. if (!tap)
  1278. return;
  1279. tap->put(MAC(sourceMac),MAC(destMac),etherType,data,len);
  1280. }
  1281. inline int nodePathCheckFunction(const struct sockaddr_storage *localAddr,const struct sockaddr_storage *remoteAddr)
  1282. {
  1283. Mutex::Lock _l(_taps_m);
  1284. for(std::map< uint64_t,EthernetTap * >::const_iterator t(_taps.begin());t!=_taps.end();++t) {
  1285. if (t->second) {
  1286. std::vector<InetAddress> ips(t->second->ips());
  1287. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1288. if (i->containsAddress(*(reinterpret_cast<const InetAddress *>(remoteAddr)))) {
  1289. return 0;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. return 1;
  1295. }
  1296. inline void tapFrameHandler(uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  1297. {
  1298. _node->processVirtualNetworkFrame(OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&_nextBackgroundTaskDeadline);
  1299. }
  1300. inline void onHttpRequestToServer(TcpConnection *tc)
  1301. {
  1302. char tmpn[256];
  1303. std::string data;
  1304. std::string contentType("text/plain"); // default if not changed in handleRequest()
  1305. unsigned int scode = 404;
  1306. try {
  1307. if (_controlPlane)
  1308. scode = _controlPlane->handleRequest(tc->from,tc->parser.method,tc->url,tc->headers,tc->body,data,contentType);
  1309. else scode = 500;
  1310. } catch ( ... ) {
  1311. scode = 500;
  1312. }
  1313. const char *scodestr;
  1314. switch(scode) {
  1315. case 200: scodestr = "OK"; break;
  1316. case 400: scodestr = "Bad Request"; break;
  1317. case 401: scodestr = "Unauthorized"; break;
  1318. case 403: scodestr = "Forbidden"; break;
  1319. case 404: scodestr = "Not Found"; break;
  1320. case 500: scodestr = "Internal Server Error"; break;
  1321. case 501: scodestr = "Not Implemented"; break;
  1322. case 503: scodestr = "Service Unavailable"; break;
  1323. default: scodestr = "Error"; break;
  1324. }
  1325. Utils::snprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\n",scode,scodestr);
  1326. {
  1327. Mutex::Lock _l(tc->writeBuf_m);
  1328. tc->writeBuf.assign(tmpn);
  1329. tc->writeBuf.append("Content-Type: ");
  1330. tc->writeBuf.append(contentType);
  1331. Utils::snprintf(tmpn,sizeof(tmpn),"\r\nContent-Length: %lu\r\n",(unsigned long)data.length());
  1332. tc->writeBuf.append(tmpn);
  1333. if (!tc->shouldKeepAlive)
  1334. tc->writeBuf.append("Connection: close\r\n");
  1335. tc->writeBuf.append("\r\n");
  1336. if (tc->parser.method != HTTP_HEAD)
  1337. tc->writeBuf.append(data);
  1338. }
  1339. _phy.setNotifyWritable(tc->sock,true);
  1340. }
  1341. inline void onHttpResponseFromClient(TcpConnection *tc)
  1342. {
  1343. if (!tc->shouldKeepAlive)
  1344. _phy.close(tc->sock); // will call close handler, which deletes from _tcpConnections
  1345. }
  1346. bool shouldBindInterface(const char *ifname,const InetAddress &ifaddr)
  1347. {
  1348. if (isBlacklistedLocalInterfaceForZeroTierTraffic(ifname))
  1349. return false;
  1350. Mutex::Lock _l(_taps_m);
  1351. for(std::map< uint64_t,EthernetTap * >::const_iterator t(_taps.begin());t!=_taps.end();++t) {
  1352. if (t->second) {
  1353. std::vector<InetAddress> ips(t->second->ips());
  1354. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1355. if (i->ipsEqual(ifaddr))
  1356. return false;
  1357. }
  1358. }
  1359. }
  1360. return true;
  1361. }
  1362. std::string _dataStorePrepPath(const char *name) const
  1363. {
  1364. std::string p(_homePath);
  1365. p.push_back(ZT_PATH_SEPARATOR);
  1366. char lastc = (char)0;
  1367. for(const char *n=name;(*n);++n) {
  1368. if ((*n == '.')&&(lastc == '.'))
  1369. return std::string(); // don't allow ../../ stuff as a precaution
  1370. if (*n == '/') {
  1371. OSUtils::mkdir(p.c_str());
  1372. p.push_back(ZT_PATH_SEPARATOR);
  1373. } else p.push_back(*n);
  1374. lastc = *n;
  1375. }
  1376. return p;
  1377. }
  1378. bool _trialBind(unsigned int port)
  1379. {
  1380. struct sockaddr_in in4;
  1381. struct sockaddr_in6 in6;
  1382. PhySocket *tb;
  1383. memset(&in4,0,sizeof(in4));
  1384. in4.sin_family = AF_INET;
  1385. in4.sin_port = Utils::hton((uint16_t)port);
  1386. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0,0);
  1387. if (tb) {
  1388. _phy.close(tb,false);
  1389. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0);
  1390. if (tb) {
  1391. _phy.close(tb,false);
  1392. return true;
  1393. }
  1394. }
  1395. memset(&in6,0,sizeof(in6));
  1396. in6.sin6_family = AF_INET6;
  1397. in6.sin6_port = Utils::hton((uint16_t)port);
  1398. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0,0);
  1399. if (tb) {
  1400. _phy.close(tb,false);
  1401. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0);
  1402. if (tb) {
  1403. _phy.close(tb,false);
  1404. return true;
  1405. }
  1406. }
  1407. return false;
  1408. }
  1409. };
  1410. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf)
  1411. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkConfigFunction(nwid,nuptr,op,nwconf); }
  1412. static void SnodeEventCallback(ZT_Node *node,void *uptr,enum ZT_Event event,const void *metaData)
  1413. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeEventCallback(event,metaData); }
  1414. static long SnodeDataStoreGetFunction(ZT_Node *node,void *uptr,const char *name,void *buf,unsigned long bufSize,unsigned long readIndex,unsigned long *totalSize)
  1415. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeDataStoreGetFunction(name,buf,bufSize,readIndex,totalSize); }
  1416. static int SnodeDataStorePutFunction(ZT_Node *node,void *uptr,const char *name,const void *data,unsigned long len,int secure)
  1417. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeDataStorePutFunction(name,data,len,secure); }
  1418. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,const struct sockaddr_storage *localAddr,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  1419. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeWirePacketSendFunction(localAddr,addr,data,len,ttl); }
  1420. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  1421. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkFrameFunction(nwid,nuptr,sourceMac,destMac,etherType,vlanId,data,len); }
  1422. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,const struct sockaddr_storage *localAddr,const struct sockaddr_storage *remoteAddr)
  1423. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathCheckFunction(localAddr,remoteAddr); }
  1424. #ifdef ZT_ENABLE_CLUSTER
  1425. static void SclusterSendFunction(void *uptr,unsigned int toMemberId,const void *data,unsigned int len)
  1426. {
  1427. OneServiceImpl *const impl = reinterpret_cast<OneServiceImpl *>(uptr);
  1428. const ClusterDefinition::MemberDefinition &md = (*(impl->_clusterDefinition))[toMemberId];
  1429. if (md.clusterEndpoint)
  1430. impl->_phy.udpSend(impl->_clusterMessageSocket,reinterpret_cast<const struct sockaddr *>(&(md.clusterEndpoint)),data,len);
  1431. }
  1432. static int SclusterGeoIpFunction(void *uptr,const struct sockaddr_storage *addr,int *x,int *y,int *z)
  1433. {
  1434. OneServiceImpl *const impl = reinterpret_cast<OneServiceImpl *>(uptr);
  1435. return (int)(impl->_clusterGeoIpService->locate(*(reinterpret_cast<const InetAddress *>(addr)),*x,*y,*z));
  1436. }
  1437. #endif
  1438. static void StapFrameHandler(void *uptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  1439. { reinterpret_cast<OneServiceImpl *>(uptr)->tapFrameHandler(nwid,from,to,etherType,vlanId,data,len); }
  1440. static int ShttpOnMessageBegin(http_parser *parser)
  1441. {
  1442. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1443. tc->currentHeaderField = "";
  1444. tc->currentHeaderValue = "";
  1445. tc->messageSize = 0;
  1446. tc->url = "";
  1447. tc->status = "";
  1448. tc->headers.clear();
  1449. tc->body = "";
  1450. return 0;
  1451. }
  1452. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  1453. {
  1454. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1455. tc->messageSize += (unsigned long)length;
  1456. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  1457. return -1;
  1458. tc->url.append(ptr,length);
  1459. return 0;
  1460. }
  1461. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  1462. {
  1463. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1464. tc->messageSize += (unsigned long)length;
  1465. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  1466. return -1;
  1467. tc->status.append(ptr,length);
  1468. return 0;
  1469. }
  1470. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  1471. {
  1472. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1473. tc->messageSize += (unsigned long)length;
  1474. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  1475. return -1;
  1476. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length())) {
  1477. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  1478. tc->currentHeaderField = "";
  1479. tc->currentHeaderValue = "";
  1480. }
  1481. for(size_t i=0;i<length;++i)
  1482. tc->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  1483. return 0;
  1484. }
  1485. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  1486. {
  1487. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1488. tc->messageSize += (unsigned long)length;
  1489. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  1490. return -1;
  1491. tc->currentHeaderValue.append(ptr,length);
  1492. return 0;
  1493. }
  1494. static int ShttpOnHeadersComplete(http_parser *parser)
  1495. {
  1496. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1497. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length()))
  1498. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  1499. return 0;
  1500. }
  1501. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  1502. {
  1503. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1504. tc->messageSize += (unsigned long)length;
  1505. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  1506. return -1;
  1507. tc->body.append(ptr,length);
  1508. return 0;
  1509. }
  1510. static int ShttpOnMessageComplete(http_parser *parser)
  1511. {
  1512. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  1513. tc->shouldKeepAlive = (http_should_keep_alive(parser) != 0);
  1514. tc->lastActivity = OSUtils::now();
  1515. if (tc->type == TcpConnection::TCP_HTTP_INCOMING) {
  1516. tc->parent->onHttpRequestToServer(tc);
  1517. } else {
  1518. tc->parent->onHttpResponseFromClient(tc);
  1519. }
  1520. return 0;
  1521. }
  1522. } // anonymous namespace
  1523. std::string OneService::platformDefaultHomePath()
  1524. {
  1525. #ifdef __UNIX_LIKE__
  1526. #ifdef __APPLE__
  1527. // /Library/... on Apple
  1528. return std::string("/Library/Application Support/ZeroTier/One");
  1529. #else
  1530. #ifdef __BSD__
  1531. // BSD likes /var/db instead of /var/lib
  1532. return std::string("/var/db/zerotier-one");
  1533. #else
  1534. // Use /var/lib for Linux and other *nix
  1535. return std::string("/var/lib/zerotier-one");
  1536. #endif
  1537. #endif
  1538. #else // not __UNIX_LIKE__
  1539. #ifdef __WINDOWS__
  1540. // Look up app data folder on Windows, e.g. C:\ProgramData\...
  1541. char buf[16384];
  1542. if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_COMMON_APPDATA,NULL,0,buf)))
  1543. return (std::string(buf) + "\\ZeroTier\\One");
  1544. else return std::string("C:\\ZeroTier\\One");
  1545. #else
  1546. return std::string(); // UNKNOWN PLATFORM
  1547. #endif
  1548. #endif // __UNIX_LIKE__ or not...
  1549. }
  1550. std::string OneService::autoUpdateUrl()
  1551. {
  1552. #ifdef ZT_AUTO_UPDATE
  1553. /*
  1554. #if defined(__LINUX__) && ( defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(__i386) )
  1555. if (sizeof(void *) == 8)
  1556. return "http://download.zerotier.com/ZeroTierOneInstaller-linux-x64-LATEST.nfo";
  1557. else return "http://download.zerotier.com/ZeroTierOneInstaller-linux-x86-LATEST.nfo";
  1558. #endif
  1559. */
  1560. #if defined(__APPLE__) && ( defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(__i386) )
  1561. return "http://download.zerotier.com/update/mac_intel/";
  1562. #endif
  1563. #ifdef __WINDOWS__
  1564. return "http://download.zerotier.com/update/win_intel/";
  1565. #endif
  1566. #endif // ZT_AUTO_UPDATE
  1567. return std::string();
  1568. }
  1569. OneService *OneService::newInstance(const char *hp,unsigned int port) { return new OneServiceImpl(hp,port); }
  1570. OneService::~OneService() {}
  1571. } // namespace ZeroTier