1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* ZeroTier One - Global Peer to Peer Ethernet
* Copyright (C) 2011-2014 ZeroTier Networks LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <set>
#include <string>
#include "Constants.hpp"
#include "SysEnv.hpp"
#include "Utils.hpp"
#include "RuntimeEnvironment.hpp"
#include "NodeConfig.hpp"
#ifdef __UNIX_LIKE__
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#endif
#ifdef __APPLE__
#include <sys/sysctl.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <net/route.h>
#endif
#ifdef __WINDOWS__
#include <Windows.h>
#include <WinSock2.h>
#endif
namespace ZeroTier {
SysEnv::SysEnv()
{
}
SysEnv::~SysEnv()
{
}
#ifdef __APPLE__
uint64_t SysEnv::getNetworkConfigurationFingerprint(const std::set<std::string> &ignoreDevices)
{
int mib[6];
size_t needed;
uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
// Right now this just scans for changes in default routes. This is not
// totally robust -- it will miss cases where we switch from one 10.0.0.0/24
// network with gateway .1 to another -- but most of the time it'll pick
// up shifts in connectivity. Combined with sleep/wake detection this seems
// pretty solid so far on Mac for detecting when you change locations.
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_UNSPEC;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
char *buf = (char *)malloc(needed);
if (buf) {
if (!sysctl(mib,6,buf,&needed,NULL,0)) {
struct rt_msghdr *rtm;
for(char *next=buf,*end=buf+needed;next<end;) {
rtm = (struct rt_msghdr *)next;
char *saptr = (char *)(rtm + 1);
char *saend = next + rtm->rtm_msglen;
if (((rtm->rtm_addrs & RTA_DST))&&((rtm->rtm_addrs & RTA_GATEWAY))) {
int sano = 0;
struct sockaddr *dst = (struct sockaddr *)0;
struct sockaddr *gateway = (struct sockaddr *)0;
while (saptr < saend) {
struct sockaddr *sa = (struct sockaddr *)saptr;
if (!sa->sa_len)
break;
if (sano == 0)
dst = sa;
else if (sano == 1)
gateway = sa;
else if (sano > 1)
break;
++sano;
saptr += sa->sa_len;
}
if ((dst)&&(gateway)) {
if ((dst->sa_family == AF_INET)&&(gateway->sa_family == AF_INET)&&(!((struct sockaddr_in *)dst)->sin_addr.s_addr)) {
fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)((struct sockaddr_in *)gateway)->sin_addr.s_addr;
} else if ((dst->sa_family == AF_INET6)&&(gateway->sa_family == AF_INET6)&&(Utils::isZero(((struct sockaddr_in6 *)dst)->sin6_addr.s6_addr,16))) {
for(unsigned int i=0;i<16;++i)
fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)((struct sockaddr_in6 *)gateway)->sin6_addr.s6_addr[i];
}
}
}
next = saend;
}
}
free(buf);
}
}
return fingerprint;
}
#endif // __APPLE__
#if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
uint64_t SysEnv::getNetworkConfigurationFingerprint(const std::set<std::string> &ignoreDevices)
{
char buf[16384];
uint64_t fingerprint = 5381; // djb2 hash algorithm is used below
char *t1,*t2;
try {
// Include default IPv4 route if available
int fd = open("/proc/net/route",O_RDONLY);
if (fd > 0) {
long n = read(fd,buf,sizeof(buf) - 1);
::close(fd);
if (n > 0) {
buf[n] = 0;
for(char *line=strtok_r(buf,"\r\n",&t1);(line);line=strtok_r((char *)0,"\r\n",&t1)) {
int fno = 0;
for(char *field=strtok_r(line," \t",&t2);(field);field=strtok_r((char *)0," \t",&t2)) {
if (fno == 0) { // device name
if ((ignoreDevices.count(std::string(field)))||(!strcmp(field,"lo")))
break;
} else if ((fno == 1)||(fno == 2)) { // destination, gateway
if (strlen(field) == 8) { // ignore header junk, use only hex route info
while (*field)
fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)*(field++);
}
} else if (fno > 2)
break;
++fno;
}
}
}
}
// Include IPs of IPv6 enabled interfaces if available
fd = open("/proc/net/if_inet6",O_RDONLY);
if (fd > 0) {
long n = read(fd,buf,sizeof(buf) - 1);
::close(fd);
if (n > 0) {
buf[n] = 0;
for(char *line=strtok_r(buf,"\r\n",&t1);(line);line=strtok_r((char *)0,"\r\n",&t1)) {
int fno = 0;
const char *v6ip = (const char *)0;
const char *devname = (const char *)0;
for(char *field=strtok_r(line," \t",&t2);(field);field=strtok_r((char *)0," \t",&t2)) {
switch(fno) {
case 0:
v6ip = field;
break;
case 5:
devname = field;
break;
}
++fno;
}
if ((v6ip)&&(devname)) {
if ((!(ignoreDevices.count(std::string(devname))))&&(strcmp(devname,"lo"))) {
while (*v6ip)
fingerprint = ((fingerprint << 5) + fingerprint) + (uint64_t)*(v6ip++);
}
}
}
}
}
} catch ( ... ) {}
return fingerprint;
}
#endif // __linux__
#ifdef __WINDOWS__
uint64_t SysEnv::getNetworkConfigurationFingerprint(const std::set<std::string> &ignoreDevices)
{
// TODO: windows version
return 1;
}
#endif // __WINDOWS__
} // namespace ZeroTier
|