OneService.cpp 60 KB

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