SysEnv.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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()
  58. {
  59. }
  60. SysEnv::~SysEnv()
  61. {
  62. }
  63. #ifdef __APPLE__
  64. uint64_t SysEnv::getNetworkConfigurationFingerprint(const std::set<std::string> &ignoreDevices)
  65. {
  66. int mib[6];
  67. size_t needed;
  68. uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
  69. // Right now this just scans for changes in default routes. This is not
  70. // totally robust -- it will miss cases where we switch from one 10.0.0.0/24
  71. // network with gateway .1 to another -- but most of the time it'll pick
  72. // up shifts in connectivity. Combined with sleep/wake detection this seems
  73. // pretty solid so far on Mac for detecting when you change locations.
  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(const std::set<std::string> &ignoreDevices)
  126. {
  127. char buf[16384];
  128. uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
  129. char *t1,*t2;
  130. try {
  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 ((ignoreDevices.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 ((!(ignoreDevices.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 __WINDOWS__
  192. uint64_t SysEnv::getNetworkConfigurationFingerprint(const std::set<std::string> &ignoreDevices)
  193. {
  194. // TODO: windows version
  195. return 1;
  196. }
  197. #endif // __WINDOWS__
  198. } // namespace ZeroTier