Network.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Network.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @author Gav Wood <i@gavwood.com>
  17. * @date 2014
  18. */
  19. #pragma once
  20. #include <memory>
  21. #include <vector>
  22. #include <deque>
  23. #include <array>
  24. #include <libdevcore/RLP.h>
  25. #include <libdevcore/Guards.h>
  26. #include "Common.h"
  27. namespace ba = boost::asio;
  28. namespace bi = ba::ip;
  29. namespace dev
  30. {
  31. namespace p2p
  32. {
  33. static const unsigned short c_defaultListenPort = 30303;
  34. struct NetworkPreferences
  35. {
  36. // Default Network Preferences
  37. NetworkPreferences(unsigned short lp = c_defaultListenPort): listenPort(lp) {}
  38. // Network Preferences with specific Listen IP
  39. NetworkPreferences(std::string const& l, unsigned short lp = c_defaultListenPort, bool u = true): publicIPAddress(), listenIPAddress(l), listenPort(lp), traverseNAT(u) {}
  40. // Network Preferences with intended Public IP
  41. NetworkPreferences(std::string const& publicIP, std::string const& l = std::string(), unsigned short lp = c_defaultListenPort, bool u = true): publicIPAddress(publicIP), listenIPAddress(l), listenPort(lp), traverseNAT(u) { if (!publicIPAddress.empty() && !isPublicAddress(publicIPAddress)) BOOST_THROW_EXCEPTION(InvalidPublicIPAddress()); }
  42. /// Addressing
  43. std::string publicIPAddress;
  44. std::string listenIPAddress;
  45. unsigned short listenPort = c_defaultListenPort;
  46. /// Preferences
  47. bool traverseNAT = true;
  48. bool discovery = true; // Discovery is activated with network.
  49. bool pin = false; // Only accept or connect to trusted peers.
  50. };
  51. /**
  52. * @brief Network Class
  53. * Static network operations and interface(s).
  54. */
  55. class Network
  56. {
  57. public:
  58. /// @returns public and private interface addresses
  59. static std::set<bi::address> getInterfaceAddresses();
  60. /// Try to bind and listen on _listenPort, else attempt net-allocated port.
  61. static int tcp4Listen(bi::tcp::acceptor& _acceptor, NetworkPreferences const& _netPrefs);
  62. /// Return public endpoint of upnp interface. If successful o_upnpifaddr will be a private interface address and endpoint will contain public address and port.
  63. static bi::tcp::endpoint traverseNAT(std::set<bi::address> const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpInterfaceAddr);
  64. /// Resolve "host:port" string as TCP endpoint. Returns unspecified endpoint on failure.
  65. static bi::tcp::endpoint resolveHost(std::string const& _host);
  66. };
  67. }
  68. }