forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cpp
More file actions
252 lines (218 loc) · 7.14 KB
/
Copy pathNetwork.cpp
File metadata and controls
252 lines (218 loc) · 7.14 KB
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
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Network.cpp
* @author Alex Leverington <nessence@gmail.com>
* @author Gav Wood <i@gavwood.com>
* @author Eric Lombrozo <elombrozo@gmail.com> (Windows version of getInterfaceAddresses())
* @date 2014
*/
#include <sys/types.h>
#ifndef _WIN32
#include <ifaddrs.h>
#endif
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/Exceptions.h>
#include "Common.h"
#include "UPnP.h"
#include "Network.h"
using namespace std;
using namespace dev;
using namespace dev::p2p;
static_assert(BOOST_VERSION == 106300, "Wrong boost headers version");
std::set<bi::address> Network::getInterfaceAddresses()
{
std::set<bi::address> addresses;
#if defined(_WIN32)
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
BOOST_THROW_EXCEPTION(NoNetworking());
char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
{
clog(NetWarn) << "Error " << WSAGetLastError() << " when getting local host name.";
WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
struct hostent* phe = gethostbyname(ac);
if (phe == 0)
{
clog(NetWarn) << "Bad host lookup.";
WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
char *addrStr = inet_ntoa(addr);
bi::address address(bi::address::from_string(addrStr));
if (!isLocalHostAddress(address))
addresses.insert(address.to_v4());
}
WSACleanup();
#else
ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1)
BOOST_THROW_EXCEPTION(NoNetworking());
for (auto ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr || string(ifa->ifa_name) == "lo0" || !(ifa->ifa_flags & IFF_UP))
continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
in_addr addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
boost::asio::ip::address_v4 address(boost::asio::detail::socket_ops::network_to_host_long(addr.s_addr));
if (!isLocalHostAddress(address))
addresses.insert(address);
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
sockaddr_in6* sockaddr = ((struct sockaddr_in6 *)ifa->ifa_addr);
in6_addr addr = sockaddr->sin6_addr;
boost::asio::ip::address_v6::bytes_type bytes;
memcpy(&bytes[0], addr.s6_addr, 16);
boost::asio::ip::address_v6 address(bytes, sockaddr->sin6_scope_id);
if (!isLocalHostAddress(address))
addresses.insert(address);
}
}
if (ifaddr!=NULL)
freeifaddrs(ifaddr);
#endif
return addresses;
}
int Network::tcp4Listen(bi::tcp::acceptor& _acceptor, NetworkPreferences const& _netPrefs)
{
// Due to the complexities of NAT and network environments (multiple NICs, tunnels, etc)
// and security concerns automation is the enemy of network configuration.
// If a preference cannot be accommodate the network must fail to start.
//
// Preferred IP: Attempt if set, else, try 0.0.0.0 (all interfaces)
// Preferred Port: Attempt if set, else, try c_defaultListenPort or 0 (random)
// TODO: throw instead of returning -1 and rename NetworkPreferences to NetworkConfig
bi::address listenIP;
try
{
listenIP = _netPrefs.listenIPAddress.empty() ? bi::address_v4() : bi::address::from_string(_netPrefs.listenIPAddress);
}
catch (...)
{
cwarn << "Couldn't start accepting connections on host. Failed to accept socket on " << listenIP << ":" << _netPrefs.listenPort << ".\n" << boost::current_exception_diagnostic_information();
return -1;
}
bool requirePort = (bool)_netPrefs.listenPort;
for (unsigned i = 0; i < 2; ++i)
{
bi::tcp::endpoint endpoint(listenIP, requirePort ? _netPrefs.listenPort : (i ? 0 : c_defaultListenPort));
try
{
#if defined(_WIN32)
bool reuse = false;
#else
bool reuse = true;
#endif
_acceptor.open(endpoint.protocol());
_acceptor.set_option(ba::socket_base::reuse_address(reuse));
_acceptor.bind(endpoint);
_acceptor.listen();
return _acceptor.local_endpoint().port();
}
catch (...)
{
// bail if this is first attempt && port was specificed, or second attempt failed (random port)
if (i || requirePort)
{
// both attempts failed
cwarn << "Couldn't start accepting connections on host. Failed to accept socket on " << listenIP << ":" << _netPrefs.listenPort << ".\n" << boost::current_exception_diagnostic_information();
_acceptor.close();
return -1;
}
_acceptor.close();
continue;
}
}
return -1;
}
bi::tcp::endpoint Network::traverseNAT(std::set<bi::address> const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpInterfaceAddr)
{
asserts(_listenPort != 0);
unique_ptr<UPnP> upnp;
try
{
upnp.reset(new UPnP);
}
// let m_upnp continue as null - we handle it properly.
catch (...) {}
bi::tcp::endpoint upnpEP;
if (upnp && upnp->isValid())
{
bi::address pAddr;
int extPort = 0;
for (auto const& addr: _ifAddresses)
if (addr.is_v4() && isPrivateAddress(addr) && (extPort = upnp->addRedirect(addr.to_string().c_str(), _listenPort)))
{
pAddr = addr;
break;
}
auto eIP = upnp->externalIP();
bi::address eIPAddr(bi::address::from_string(eIP));
if (extPort && eIP != string("0.0.0.0") && !isPrivateAddress(eIPAddr))
{
clog(NetNote) << "Punched through NAT and mapped local port" << _listenPort << "onto external port" << extPort << ".";
clog(NetNote) << "External addr:" << eIP;
o_upnpInterfaceAddr = pAddr;
upnpEP = bi::tcp::endpoint(eIPAddr, (unsigned short)extPort);
}
else
clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place).";
}
return upnpEP;
}
bi::tcp::endpoint Network::resolveHost(string const& _addr)
{
static boost::asio::io_service s_resolverIoService;
vector<string> split;
boost::split(split, _addr, boost::is_any_of(":"));
unsigned port = dev::p2p::c_defaultIPPort;
try
{
if (split.size() > 1)
port = static_cast<unsigned>(stoi(split.at(1)));
}
catch(...) {}
boost::system::error_code ec;
bi::address address = bi::address::from_string(split[0], ec);
bi::tcp::endpoint ep(bi::address(), port);
if (!ec)
ep.address(address);
else
{
boost::system::error_code ec;
// resolve returns an iterator (host can resolve to multiple addresses)
bi::tcp::resolver r(s_resolverIoService);
auto it = r.resolve({bi::tcp::v4(), split[0], toString(port)}, ec);
if (ec)
{
clog(NetWarn) << "Error resolving host address..." << LogTag::Url << _addr << ":" << LogTag::Error << ec.message();
return bi::tcp::endpoint();
}
else
ep = *it;
}
return ep;
}