SysEnv.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 <fcntl.h>
  31. #include <errno.h>
  32. #include <sys/types.h>
  33. #include <set>
  34. #include <string>
  35. #include "Constants.hpp"
  36. #include "SysEnv.hpp"
  37. #include "Utils.hpp"
  38. #include "RuntimeEnvironment.hpp"
  39. #include "NodeConfig.hpp"
  40. #ifdef __UNIX_LIKE__
  41. #include <arpa/inet.h>
  42. #include <sys/socket.h>
  43. #include <unistd.h>
  44. #include <signal.h>
  45. #endif
  46. #ifdef __APPLE__
  47. #include <sys/sysctl.h>
  48. #include <sys/uio.h>
  49. #include <sys/param.h>
  50. #include <net/route.h>
  51. #endif
  52. #ifdef __WINDOWS__
  53. #include <Windows.h>
  54. #include <WinSock2.h>
  55. #endif
  56. namespace ZeroTier {
  57. SysEnv::SysEnv(const RuntimeEnvironment *renv) :
  58. _r(renv)
  59. {
  60. }
  61. SysEnv::~SysEnv()
  62. {
  63. }
  64. #ifdef __APPLE__
  65. uint64_t SysEnv::getNetworkConfigurationFingerprint()
  66. throw()
  67. {
  68. int mib[6];
  69. size_t needed;
  70. uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
  71. // Right now this just scans for changes in default routes. This is not
  72. // totally robust -- it will miss cases where we switch from one 10.0.0.0/24
  73. // network with gateway .1 to another -- but most of the time it'll pick
  74. // up shifts in connectivity.
  75. mib[0] = CTL_NET;
  76. mib[1] = PF_ROUTE;
  77. mib[2] = 0;
  78. mib[3] = AF_UNSPEC;
  79. mib[4] = NET_RT_DUMP;
  80. mib[5] = 0;
  81. if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
  82. char *buf = (char *)malloc(needed);
  83. if (buf) {
  84. if (!sysctl(mib,6,buf,&needed,NULL,0)) {
  85. struct rt_msghdr *rtm;
  86. for(char *next=buf,*end=buf+needed;next<end;) {
  87. rtm = (struct rt_msghdr *)next;
  88. char *saptr = (char *)(rtm + 1);
  89. char *saend = next + rtm->rtm_msglen;
  90. if (((rtm->rtm_addrs & RTA_DST))&&((rtm->rtm_addrs & RTA_GATEWAY))) {
  91. int sano = 0;
  92. struct sockaddr *dst = (struct sockaddr *)0;
  93. struct sockaddr *gateway = (struct sockaddr *)0;
  94. while (saptr < saend) {
  95. struct sockaddr *sa = (struct sockaddr *)saptr;
  96. if (!sa->sa_len)
  97. break;
  98. if (sano == 0)
  99. dst = sa;
  100. else if (sano == 1)
  101. gateway = sa;
  102. else if (sano > 1)
  103. break;
  104. ++sano;
  105. saptr += sa->sa_len;
  106. }
  107. if ((dst)&&(gateway)) {
  108. if ((dst->sa_family == AF_INET)&&(gateway->sa_family == AF_INET)&&(!((struct sockaddr_in *)dst)->sin_addr.s_addr)) {
  109. fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)((struct sockaddr_in *)gateway)->sin_addr.s_addr;
  110. } else if ((dst->sa_family == AF_INET6)&&(gateway->sa_family == AF_INET6)&&(Utils::isZero(((struct sockaddr_in6 *)dst)->sin6_addr.s6_addr,16))) {
  111. for(unsigned int i=0;i<16;++i)
  112. fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)((struct sockaddr_in6 *)gateway)->sin6_addr.s6_addr[i];
  113. }
  114. }
  115. }
  116. next = saend;
  117. }
  118. }
  119. free(buf);
  120. }
  121. }
  122. return fingerprint;
  123. }
  124. #endif // __APPLE__
  125. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  126. uint64_t SysEnv::getNetworkConfigurationFingerprint()
  127. throw()
  128. {
  129. char buf[16384];
  130. uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
  131. char *t1,*t2;
  132. try {
  133. std::set<std::string> tapDevs(_r->nc->networkTapDeviceNames());
  134. // Include default IPv4 route if available
  135. int fd = open("/proc/net/route",O_RDONLY);
  136. if (fd > 0) {
  137. long n = read(fd,buf,sizeof(buf) - 1);
  138. ::close(fd);
  139. if (n > 0) {
  140. buf[n] = 0;
  141. for(char *line=strtok_r(buf,"\r\n",&t1);(line);line=strtok_r((char *)0,"\r\n",&t1)) {
  142. int fno = 0;
  143. for(char *field=strtok_r(line," \t",&t2);(field);field=strtok_r((char *)0," \t",&t2)) {
  144. if (fno == 0) { // device name
  145. if ((tapDevs.count(std::string(field)))||(!strcmp(field,"lo")))
  146. break;
  147. } else if ((fno == 1)||(fno == 2)) { // destination, gateway
  148. if (strlen(field) == 8) { // ignore header junk, use only hex route info
  149. while (*field)
  150. fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)*(field++);
  151. }
  152. } else if (fno > 2)
  153. break;
  154. ++fno;
  155. }
  156. }
  157. }
  158. }
  159. // Include IPs of IPv6 enabled interfaces if available
  160. fd = open("/proc/net/if_inet6",O_RDONLY);
  161. if (fd > 0) {
  162. long n = read(fd,buf,sizeof(buf) - 1);
  163. ::close(fd);
  164. if (n > 0) {
  165. buf[n] = 0;
  166. for(char *line=strtok_r(buf,"\r\n",&t1);(line);line=strtok_r((char *)0,"\r\n",&t1)) {
  167. int fno = 0;
  168. const char *v6ip = (const char *)0;
  169. const char *devname = (const char *)0;
  170. for(char *field=strtok_r(line," \t",&t2);(field);field=strtok_r((char *)0," \t",&t2)) {
  171. switch(fno) {
  172. case 0:
  173. v6ip = field;
  174. break;
  175. case 5:
  176. devname = field;
  177. break;
  178. }
  179. ++fno;
  180. }
  181. if ((v6ip)&&(devname)) {
  182. if ((!(tapDevs.count(std::string(devname))))&&(strcmp(devname,"lo"))) {
  183. while (*v6ip)
  184. fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)*(v6ip++);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. } catch ( ... ) {}
  191. return fingerprint;
  192. }
  193. #endif // __linux__
  194. #ifdef __WINDOWS__
  195. uint64_t SysEnv::getNetworkConfigurationFingerprint()
  196. throw()
  197. {
  198. // TODO: windows version
  199. return 1;
  200. }
  201. #endif // __WINDOWS__
  202. } // namespace ZeroTier