OneService.cpp 58 KB

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