OneService.cpp 59 KB

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