SysEnv.cpp 6.0 KB

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