summaryrefslogtreecommitdiff
path: root/node/InetAddress.cpp
blob: 832acf04c24f137d99fbcf66d477830e1a280070 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * 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 <string.h>
#include <stdint.h>

#include <string>

#include "Constants.hpp"
#include "InetAddress.hpp"
#include "Utils.hpp"

namespace ZeroTier {

const InetAddress InetAddress::LO4("127.0.0.1",0);
const InetAddress InetAddress::LO6("::1",0);
const InetAddress InetAddress::DEFAULT4((uint32_t)0,0);
const InetAddress InetAddress::DEFAULT6((const void *)0,16,0);

void InetAddress::set(const std::string &ip,unsigned int port)
	throw()
{
	memset(&_sa,0,sizeof(_sa));
	if (ip.find(':') != std::string::npos) {
		_sa.sin6.sin6_family = AF_INET6;
		_sa.sin6.sin6_port = Utils::hton((uint16_t)port);
		if (inet_pton(AF_INET6,ip.c_str(),(void *)&(_sa.sin6.sin6_addr.s6_addr)) <= 0)
			_sa.saddr.sa_family = 0;
	} else {
		_sa.sin.sin_family = AF_INET;
		_sa.sin.sin_port = Utils::hton((uint16_t)port);
		if (inet_pton(AF_INET,ip.c_str(),(void *)&(_sa.sin.sin_addr.s_addr)) <= 0)
			_sa.saddr.sa_family = 0;
	}
}

void InetAddress::set(const void *ipBytes,unsigned int ipLen,unsigned int port)
	throw()
{
	memset(&_sa,0,sizeof(_sa));
	if (ipLen == 4) {
		setV4();
		if (ipBytes)
			memcpy(rawIpData(),ipBytes,4);
		setPort(port);
	} else if (ipLen == 16) {
		setV6();
		if (ipBytes)
			memcpy(rawIpData(),ipBytes,16);
		setPort(port);
	}
}

bool InetAddress::isLinkLocal() const
	throw()
{
	if (_sa.saddr.sa_family == AF_INET)
		return ((Utils::ntoh((uint32_t)_sa.sin.sin_addr.s_addr) & 0xffff0000) == 0xa9fe0000);
	else if (_sa.saddr.sa_family == AF_INET6) {
		if (_sa.sin6.sin6_addr.s6_addr[0] != 0xfe) return false;
		if (_sa.sin6.sin6_addr.s6_addr[1] != 0x80) return false;
		if (_sa.sin6.sin6_addr.s6_addr[2] != 0x00) return false;
		if (_sa.sin6.sin6_addr.s6_addr[3] != 0x00) return false;
		if (_sa.sin6.sin6_addr.s6_addr[4] != 0x00) return false;
		if (_sa.sin6.sin6_addr.s6_addr[5] != 0x00) return false;
		if (_sa.sin6.sin6_addr.s6_addr[6] != 0x00) return false;
		if (_sa.sin6.sin6_addr.s6_addr[7] != 0x00) return false;
		return true;
	}
	return false;
}

bool InetAddress::isDefaultRoute() const
	throw()
{
	if (_sa.saddr.sa_family == AF_INET)
		return ((_sa.sin.sin_addr.s_addr == 0)&&(_sa.sin.sin_port == 0));
	else if (_sa.saddr.sa_family == AF_INET6)
		return ((Utils::isZero(_sa.sin6.sin6_addr.s6_addr,16))&&(_sa.sin6.sin6_port == 0));
	return false;
}

std::string InetAddress::toString() const
{
	char buf[128],buf2[128];

	switch(_sa.saddr.sa_family) {
		case AF_INET:
#ifdef __WINDOWS__
			if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
#else
			if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
#endif
				Utils::snprintf(buf2,sizeof(buf2),"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port));
				return std::string(buf2);
			}
			break;
		case AF_INET6:
#ifdef __WINDOWS__
			if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
#else
			if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
#endif
				Utils::snprintf(buf2,sizeof(buf2),"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port));
				return std::string(buf2);
			}
			break;
	}

	return std::string();
}

void InetAddress::fromString(const std::string &ipSlashPort)
{
	std::size_t slashAt = ipSlashPort.find('/');
	if ((slashAt == std::string::npos)||(slashAt >= ipSlashPort.length()))
		set(ipSlashPort,0);
	else {
		long p = strtol(ipSlashPort.substr(slashAt+1).c_str(),(char **)0,10);
		if ((p > 0)&&(p <= 0xffff))
			set(ipSlashPort.substr(0,slashAt),(unsigned int)p);
		else set(ipSlashPort.substr(0,slashAt),0);
	}
}

std::string InetAddress::toIpString() const
{
	char buf[128];
	switch(_sa.saddr.sa_family) {
		case AF_INET:
#ifdef __WINDOWS__
			if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
				return std::string(buf);
#else
			if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
				return std::string(buf);
#endif
			break;
		case AF_INET6:
#ifdef __WINDOWS__
			if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
				return std::string(buf);
#else
			if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
				return std::string(buf);
#endif
			break;
	}
	return std::string();
}

InetAddress InetAddress::netmask() const
	throw()
{
	InetAddress r(*this);
	switch(_sa.saddr.sa_family) {
		case AF_INET:
			r._sa.sin.sin_addr.s_addr = Utils::hton((uint32_t)(0xffffffff << (32 - netmaskBits())));
			break;
		case AF_INET6: {
			unsigned char *bf = (unsigned char *)r._sa.sin6.sin6_addr.s6_addr;
			signed int bitsLeft = (signed int)netmaskBits();
			for(unsigned int i=0;i<16;++i) {
				if (bitsLeft > 0) {
					bf[i] = (unsigned char)((bitsLeft >= 8) ? 0xff : (0xff << (8 - bitsLeft)));
					bitsLeft -= 8;
				} else bf[i] = (unsigned char)0;
			}
		}	break;
	}
	return r;
}

InetAddress InetAddress::broadcast() const
	throw()
{
	InetAddress r(*this);
	switch(_sa.saddr.sa_family) {
		case AF_INET:
			r._sa.sin.sin_addr.s_addr |= Utils::hton((uint32_t)(0xffffffff >> netmaskBits()));
			break;
		case AF_INET6: {
			unsigned char *bf = (unsigned char *)r._sa.sin6.sin6_addr.s6_addr;
			signed int bitsLeft = (signed int)netmaskBits();
			for(unsigned int i=0;i<16;++i) {
				if (bitsLeft > 0) {
					bf[i] |= (unsigned char)((bitsLeft >= 8) ? 0x00 : (0xff >> bitsLeft));
					bitsLeft -= 8;
				}
			}
		}	break;
	}
	return r;
}

bool InetAddress::sameNetworkAs(const InetAddress &ipnet) const
	throw()
{
	if (_sa.saddr.sa_family != ipnet._sa.saddr.sa_family)
		return false;

	unsigned int bits = netmaskBits();
	if (bits != ipnet.netmaskBits())
		return false;
	if (!bits)
		return true;
	switch(_sa.saddr.sa_family) {
		case AF_INET:
			if (bits >= 32) bits = 32;
			break;
		case AF_INET6:
			if (bits >= 128) bits = 128;
			break;
		default:
			return false;
	}

	const uint8_t *a = (const uint8_t *)rawIpData();
	const uint8_t *b = (const uint8_t *)ipnet.rawIpData();
	while (bits >= 8) {
		if (*(a++) != *(b++))
			return false;
		bits -= 8;
	}
	bits = 8 - bits;
	return ((*a >> bits) == (*b >> bits));
}

bool InetAddress::within(const InetAddress &ipnet) const
	throw()
{
	if (_sa.saddr.sa_family != ipnet._sa.saddr.sa_family)
		return false;

	unsigned int bits = ipnet.netmaskBits();
	switch(_sa.saddr.sa_family) {
		case AF_INET:
			if (bits > 32) return false;
			break;
		case AF_INET6:
			if (bits > 128) return false;
			break;
		default: return false;
	}

	const uint8_t *a = (const uint8_t *)rawIpData();
	const uint8_t *b = (const uint8_t *)ipnet.rawIpData();
	while (bits >= 8) {
		if (*(a++) != *(b++))
			return false;
		bits -= 8;
	}
	if (bits) {
		uint8_t mask = ((0xff << (8 - bits)) & 0xff);
		return ((*a & mask) == (*b & mask));
	} else return true;
}

bool InetAddress::operator==(const InetAddress &a) const
	throw()
{
	if (_sa.saddr.sa_family == AF_INET) {
		if (a._sa.saddr.sa_family == AF_INET)
			return ((_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr)&&(_sa.sin.sin_port == a._sa.sin.sin_port));
		return false;
	} else if (_sa.saddr.sa_family == AF_INET6) {
		if (a._sa.saddr.sa_family == AF_INET6) {
			if (_sa.sin6.sin6_port == a._sa.sin6.sin6_port)
				return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,sizeof(_sa.sin6.sin6_addr.s6_addr)));
		}
		return false;
	} else return (memcmp(&_sa,&a._sa,sizeof(_sa)) == 0);
}

bool InetAddress::operator<(const InetAddress &a) const
	throw()
{
	if (_sa.saddr.sa_family < a._sa.saddr.sa_family)
		return true;
	else if (_sa.saddr.sa_family == a._sa.saddr.sa_family) {
		if (_sa.saddr.sa_family == AF_INET) {
			unsigned long x = Utils::ntoh((uint32_t)_sa.sin.sin_addr.s_addr);
			unsigned long y = Utils::ntoh((uint32_t)a._sa.sin.sin_addr.s_addr);
			if (x == y)
				return (Utils::ntoh((uint16_t)_sa.sin.sin_port) < Utils::ntoh((uint16_t)a._sa.sin.sin_port));
			else return (x < y);
		} else if (_sa.saddr.sa_family == AF_INET6) {
			int cmp = (int)memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16);
			if (cmp == 0)
				return (Utils::ntoh((uint16_t)_sa.sin6.sin6_port) < Utils::ntoh((uint16_t)a._sa.sin6.sin6_port));
			else return (cmp < 0);
		} else return (memcmp(&_sa,&a._sa,sizeof(_sa)) < 0);
	}
	return false;
}

InetAddress InetAddress::makeIpv6LinkLocal(const MAC &mac)
	throw()
{
	InetAddress ip;
	ip._sa.saddr.sa_family = AF_INET6;
	ip._sa.sin6.sin6_addr.s6_addr[0] = 0xfe;
	ip._sa.sin6.sin6_addr.s6_addr[1] = 0x80;
	ip._sa.sin6.sin6_addr.s6_addr[2] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[3] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[4] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[5] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[6] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[7] = 0x00;
	ip._sa.sin6.sin6_addr.s6_addr[8] = mac[0] & 0xfd;
	ip._sa.sin6.sin6_addr.s6_addr[9] = mac[1];
	ip._sa.sin6.sin6_addr.s6_addr[10] = mac[2];
	ip._sa.sin6.sin6_addr.s6_addr[11] = 0xff;
	ip._sa.sin6.sin6_addr.s6_addr[12] = 0xfe;
	ip._sa.sin6.sin6_addr.s6_addr[13] = mac[3];
	ip._sa.sin6.sin6_addr.s6_addr[14] = mac[4];
	ip._sa.sin6.sin6_addr.s6_addr[15] = mac[5];
	ip._sa.sin6.sin6_port = Utils::hton((uint16_t)64);
	return ip;
}

} // namespace ZeroTier