SysEnv.cpp 5.9 KB

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