summaryrefslogtreecommitdiff
path: root/osdep/Binder.hpp
blob: 2e4df20d9f18eb562b5c9f2659d8470710ce4476 (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
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2016  ZeroTier, Inc.  https://www.zerotier.com/
 *
 * 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/>.
 */

#ifndef ZT_BINDER_HPP
#define ZT_BINDER_HPP

#include "../node/Constants.hpp"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#include <ShlObj.h>
#include <netioapi.h>
#include <iphlpapi.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ifaddrs.h>
#endif

#include <string>
#include <vector>
#include <algorithm>
#include <utility>

#include "../node/InetAddress.hpp"
#include "../node/Mutex.hpp"

#include "Phy.hpp"

namespace ZeroTier {

/**
 * Enumerates local devices and binds ports to all potential ZeroTier path endpoints
 *
 * This replaces binding to wildcard (0.0.0.0 and ::0) with explicit binding
 * as part of the path to default gateway support. Under the hood it uses
 * different queries on different OSes to enumerate devices, and also exposes
 * device enumeration and endpoint IP data for use elsewhere.
 *
 * On OSes that do not support local port enumeration or where this is not
 * meaningful, this degrades to binding to wildcard.
 */
template<typename PHY_HANDLER_TYPE>
class Binder
{
private:
	struct _Binding
	{
		PhySocket *udpSock;
		PhySocket *tcpListenSock;
		InetAddress address;
	};

public:
	/**
	 * @param phy Physical interface to use -- be sure not to delete phy before binder
	 * @param port Port to bind to on all interfaces (TCP and UDP)
	 */
	Binder(typename Phy<PHY_HANDLER_TYPE> &phy,unsigned int port) :
		_phy(phy),
		_port(port)
	{
	}

	/**
	 * Closes all bound ports -- but NOT accepted connections on those ports
	 */
	~Binder()
	{
	}

	/**
	 * Scan local devices and addresses and rebind TCP and UDP
	 *
	 * This should be called after wake from sleep, on detected network device
	 * changes, on startup, or periodically (e.g. every 30-60s).
	 *
	 * @param ignoreInterfacesByName Ignore these interfaces by name
	 * @param ignoreInterfacesByNamePrefix Ignore these interfaces by name-prefix (starts-with, e.g. zt ignores zt*)
	 * @param ignoreInterfacesByAddress Ignore these interfaces by address
	 */
	void refresh(const std::vector<std::string> &ignoreInterfacesByName,const std::vector<std::string> &ignoreInterfacesByNamePrefix,const std::vector<InetAddress> &ignoreInterfacesByAddress)
	{
		std::vector<InetAddress> localIfAddrs;
		std::vector<_Binding> newBindings;
		const char *na,*nb;
		std::vector<std::string>::const_iterator si;
		std::vector<InetAddress>::const_iterator ii;
		std::vector<_Binding>::const_iterator bi;
		PhySocket *udps,*tcps;
		InetAddress ip;
		Mutex::Lock _l(_lock);

#ifdef __WINDOWS__

#else // not __WINDOWS__

		struct ifaddrs *ifatbl = (struct ifaddrs *)0;
		struct ifaddrs *ifa;

		if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
			ifa = ifatbl;
			while (ifa) {
				if ((ifa->ifa_name)&&(ifa->ifa_addr)) {
					for(si=ignoreInterfacesByName.begin();si!=ignoreInterfacesByName.end();++si) {
						if (*si == ifa->ifa_name)
							goto binder_hpp_ignore_interface;
					}

					for(si=ignoreInterfacesByNamePrefix.begin();si!=ignoreInterfacesByNamePrefix.end();++si) {
						na = si->c_str();
						nb = ifa->ifa_name;
						while (*nb) {
							if (*(na++) != *(nb++))
								goto binder_hpp_interface_prefixes_dont_match;
						}
						goto binder_hpp_ignore_interface;
					}

binder_hpp_interface_prefixes_dont_match:
					ip = *(ifa->ifa_addr);
					switch(ip.ipScope()) {
						default: break;
						case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
						case InetAddress::IP_SCOPE_GLOBAL:
						//case InetAddress::IP_SCOPE_LINK_LOCAL:
						case InetAddress::IP_SCOPE_SHARED:
						case InetAddress::IP_SCOPE_PRIVATE:
							for(ii=ignoreInterfacesByAddress.begin();ii!=ignoreInterfacesByAddress.end();++ii) {
								if (ip.ipsEqual(*ii))
									goto binder_hpp_ignore_interface;
							}

							ip.setPort(_port);
							localIfAddrs.push(ip);

							break;
					}
				}

binder_hpp_ignore_interface:
				ifa = ifa->ifa_next;
			}
		}

		freeifaddrs(ifatbl);

#endif

		// Default to binding to wildcard if we can't enumerate addresses
		if (localIfAddrs.size() == 0) {
			localIfAddrs.push_back(InetAddress((uint32_t)0,_port));
			localIfAddrs.push_back(InetAddress((const void *)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",16,_port));
		}

		// Close any bindings to anything that doesn't exist anymore
		for(bi=_bindings.begin();bi!=_bindings.end();++bi) {
			if (std::find(localIfAddrs.begin(),localIfAddrs.end(),bi->address) == localIfAddrs.end()) {
				_phy.close(bi->udpSock,false);
				_phy.close(bi->tcpListenSock,false);
			}
		}

		for(ii=localIfAddrs.begin();ii!=localIfAddrs.end();++ii) {
			// Copy over bindings that still are valid
			for(bi=_bindings.begin();bi!=_bindings.end();++bi) {
				if (bi->address == *ii) {
					newBindings.push_back(*bi);
					break;
				}
			}

			// Add new bindings
			if (bi == _bindings.end()) {
				udps = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&ii),(void *)0,131072);
				if (udps) {
					tcps = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&ii),(void *)0);
					if (tcps) {
						newBindings.push_back(_Binding());
						newBindings.back().udpSock = udps;
						newBindings.back().tcpListenSock = tcps;
						newBindings.back().address = *ii;
					} else {
						_phy.close(udps,false);
					}
				}
			}
		}

		// Swapping pointers and then letting the old one fall out of scope is faster than copying again
		_bindings.swap(newBindings);
	}

	/**
	 * Send a UDP packet from the specified local interface, or all
	 *
	 * Unfortunately even by examining the routing table there is no ultimately
	 * robust way to tell where we might reach another host that works in all
	 * environments. As a result, we send packets with null (wildcard) local
	 * addresses from *every* bound interface.
	 *
	 * These are typically initial HELLOs, path probes, etc., since normal
	 * conversations will have a local endpoint address. So the cost is low and
	 * if the peer is not reachable via that route then the packet will go
	 * nowhere and nothing will happen.
	 *
	 * It will of course only send via interface bindings of the same socket
	 * family. No point in sending V4 via V6 or vice versa.
	 *
	 * In any case on most hosts there's only one or two interfaces that we
	 * will use, so none of this is particularly costly.
	 *
	 * @param local Local interface address or null address for 'all'
	 * @param remote Remote address
	 * @param data Data to send
	 * @param len Length of data
	 */
	inline bool udpSend(const InetAddress &local,const InetAddress &remote,const void *data,unsigned int len) const
	{
		if (local) {
			for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
				if (i->address == local)
					return _phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
			}
			return false;
		} else {
			bool result = false;
			for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
				if (i->address.ss_family == remote.ss_family)
					result |= _phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
			}
			return result;
		}
	}

private:
	std::vector<_Binding> _bindings;
	typename Phy<PHY_HANDLER_TYPE> &_phy;
	unsigned int _port;
	Mutex _lock;
};

} // namespace ZeroTier

#endif