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
|
/*
* 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 <vector>
#include "Constants.hpp"
#ifdef __WINDOWS__
#include <WinSock2.h>
#include <Windows.h>
#endif
#include "Demarc.hpp"
#include "RuntimeEnvironment.hpp"
#include "Logger.hpp"
#include "UdpSocket.hpp"
#include "InetAddress.hpp"
#include "Switch.hpp"
#include "Buffer.hpp"
#include "CMWC4096.hpp"
namespace ZeroTier {
const Demarc::Port Demarc::ANY_PORT = (Port)0xffffffffffffffffULL;
const Demarc::Port Demarc::NULL_PORT = (Port)0;
Demarc::Demarc(const RuntimeEnvironment *renv) :
_r(renv)
{
}
Demarc::~Demarc()
{
for(std::map< Port,DemarcPortObj >::iterator pe(_ports.begin());pe!=_ports.end();++pe) {
switch (pe->second.type) {
case PORT_TYPE_UDP_SOCKET_V4:
case PORT_TYPE_UDP_SOCKET_V6:
delete ((UdpSocket *)pe->second.obj);
break;
case PORT_TYPE_LOCAL_ETHERNET:
case PORT_TYPE_RELAY_TUNNEL:
break;
}
}
}
std::string Demarc::describe(Demarc::Port p)
{
char buf[64];
switch ((DemarcPortType)(((uint64_t)p) >> 60)) {
case PORT_TYPE_UDP_SOCKET_V4:
Utils::snprintf(buf,sizeof(buf),"udp/4/%d",(int)((uint64_t)p & 0xffff));
return std::string(buf);
case PORT_TYPE_UDP_SOCKET_V6:
Utils::snprintf(buf,sizeof(buf),"udp/6/%d",(int)((uint64_t)p & 0xffff));
return std::string(buf);
case PORT_TYPE_LOCAL_ETHERNET:
return std::string("ethernet");
case PORT_TYPE_RELAY_TUNNEL:
return std::string("relay");
}
return std::string("(null)");
}
bool Demarc::has(Port p) const
throw()
{
Mutex::Lock _l(_ports_m);
return (_ports.count(p) != 0);
}
bool Demarc::bindLocalUdp(unsigned int localPort)
{
Mutex::Lock _l(_ports_m);
uint64_t v4p = ((uint64_t)PORT_TYPE_UDP_SOCKET_V4 << 60) | (uint64_t)localPort;
uint64_t v6p = ((uint64_t)PORT_TYPE_UDP_SOCKET_V6 << 60) | (uint64_t)localPort;
if ((_ports.count((Port)v4p))||(_ports.count((Port)v6p)))
return true; // port already bound
UdpSocket *v4 = (UdpSocket *)0;
try {
DemarcPortObj *v4r = &(_ports[(Port)v4p]);
v4r->port = (Port)v4p;
v4r->parent = this;
v4r->obj = v4 = new UdpSocket(false,localPort,false,&Demarc::_CBudpSocketPacketHandler,v4r);
v4r->type = PORT_TYPE_UDP_SOCKET_V4;
} catch ( ... ) {
_ports.erase((Port)v4p);
v4 = (UdpSocket *)0;
}
UdpSocket *v6 = (UdpSocket *)0;
try {
DemarcPortObj *v6r = &(_ports[(Port)v6p]);
v6r->port = (Port)v6p;
v6r->parent = this;
v6r->obj = v6 = new UdpSocket(false,localPort,true,&Demarc::_CBudpSocketPacketHandler,v6r);
v6r->type = PORT_TYPE_UDP_SOCKET_V6;
} catch ( ... ) {
_ports.erase((Port)v6p);
v6 = (UdpSocket *)0;
}
return ((v4)||(v6));
}
Demarc::Port Demarc::pick(const InetAddress &to) const
throw()
{
Mutex::Lock _l(_ports_m);
try {
std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
for(std::map< Port,DemarcPortObj >::const_iterator pe(_ports.begin());pe!=_ports.end();++pe) {
switch (pe->second.type) {
case PORT_TYPE_UDP_SOCKET_V4:
if (to.isV4())
possibilities.push_back(pe);
break;
case PORT_TYPE_UDP_SOCKET_V6:
if (to.isV6())
possibilities.push_back(pe);
break;
default:
break;
}
}
if (possibilities.size())
return possibilities[_r->prng->next32() % possibilities.size()]->first;
else return NULL_PORT;
} catch ( ... ) {
return NULL_PORT;
}
}
Demarc::Port Demarc::send(Demarc::Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
throw()
{
_ports_m.lock();
std::map< Port,DemarcPortObj >::const_iterator pe(_ports.find(fromPort));
if (pe == _ports.end()) {
try {
std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
for(pe=_ports.begin();pe!=_ports.end();++pe) {
switch (pe->second.type) {
case PORT_TYPE_UDP_SOCKET_V4:
if (to.isV4())
possibilities.push_back(pe);
break;
case PORT_TYPE_UDP_SOCKET_V6:
if (to.isV6())
possibilities.push_back(pe);
break;
default:
break;
}
}
if (possibilities.size())
pe = possibilities[_r->prng->next32() % possibilities.size()];
else {
_ports_m.unlock();
return NULL_PORT;
}
} catch ( ... ) {
_ports_m.unlock();
return NULL_PORT;
}
}
switch (pe->second.type) {
case PORT_TYPE_UDP_SOCKET_V4:
case PORT_TYPE_UDP_SOCKET_V6:
_ports_m.unlock();
if (((UdpSocket *)pe->second.obj)->send(to,data,len,hopLimit))
return pe->first;
return NULL_PORT;
default:
break;
}
_ports_m.unlock();
return NULL_PORT;
}
void Demarc::_CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len)
{
if (!((DemarcPortObj *)arg)->parent->_r->shutdownInProgress)
((DemarcPortObj *)arg)->parent->_r->sw->onRemotePacket(((DemarcPortObj *)arg)->port,from,Buffer<4096>(data,len));
}
} // namespace ZeroTier
|