danicoin/src/P2p/NetNodeConfig.cpp

231 lines
7.4 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#include "NetNodeConfig.h"
2015-05-27 12:08:46 +00:00
#include <boost/utility/value_init.hpp>
2015-07-30 15:22:07 +00:00
#include <Common/Util.h>
#include "Common/CommandLine.h"
2015-05-27 12:08:46 +00:00
#include "Common/StringTools.h"
#include "crypto/crypto.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteConfig.h"
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
namespace {
const command_line::arg_descriptor<std::string> arg_p2p_bind_ip = {"p2p-bind-ip", "Interface for p2p network protocol", "0.0.0.0"};
2015-07-30 15:22:07 +00:00
const command_line::arg_descriptor<uint16_t> arg_p2p_bind_port = {"p2p-bind-port", "Port for p2p network protocol", P2P_DEFAULT_PORT};
const command_line::arg_descriptor<uint16_t> arg_p2p_external_port = { "p2p-external-port", "External port for p2p network protocol (if port forwarding used with NAT)", 0 };
const command_line::arg_descriptor<bool> arg_p2p_allow_local_ip = {"allow-local-ip", "Allow local ip add to peer list, mostly in debug purposes"};
const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_add_peer = {"add-peer", "Manually add peer to local peerlist"};
const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_add_priority_node = {"add-priority-node", "Specify list of peers to connect to and attempt to keep the connection open"};
const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_add_exclusive_node = {"add-exclusive-node", "Specify list of peers to connect to only."
" If this option is given the options add-priority-node and seed-node are ignored"};
const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_seed_node = {"seed-node", "Connect to a node to retrieve peer addresses, and disconnect"};
const command_line::arg_descriptor<bool> arg_p2p_hide_my_port = {"hide-my-port", "Do not announce yourself as peerlist candidate", false, true};
2015-07-30 15:22:07 +00:00
bool parsePeerFromString(NetworkAddress& pe, const std::string& node_addr) {
2015-05-27 12:08:46 +00:00
return Common::parseIpAddressAndPort(pe.ip, pe.port, node_addr);
}
bool parsePeersAndAddToContainer(const boost::program_options::variables_map& vm,
2015-07-30 15:22:07 +00:00
const command_line::arg_descriptor<std::vector<std::string>>& arg, std::vector<NetworkAddress>& container)
{
std::vector<std::string> peers = command_line::get_arg(vm, arg);
for(const std::string& str: peers) {
2015-07-30 15:22:07 +00:00
NetworkAddress na = boost::value_initialized<NetworkAddress>();
if (!parsePeerFromString(na, str)) {
return false;
}
container.push_back(na);
}
return true;
}
} //namespace
void NetNodeConfig::initOptions(boost::program_options::options_description& desc) {
command_line::add_arg(desc, arg_p2p_bind_ip);
command_line::add_arg(desc, arg_p2p_bind_port);
command_line::add_arg(desc, arg_p2p_external_port);
command_line::add_arg(desc, arg_p2p_allow_local_ip);
command_line::add_arg(desc, arg_p2p_add_peer);
command_line::add_arg(desc, arg_p2p_add_priority_node);
command_line::add_arg(desc, arg_p2p_add_exclusive_node);
command_line::add_arg(desc, arg_p2p_seed_node);
command_line::add_arg(desc, arg_p2p_hide_my_port);
}
NetNodeConfig::NetNodeConfig() {
bindIp = "0.0.0.0";
2015-07-30 15:22:07 +00:00
bindPort = P2P_DEFAULT_PORT;
externalPort = 0;
allowLocalIp = false;
hideMyPort = false;
2015-07-30 15:22:07 +00:00
configFolder = Tools::getDefaultDataDirectory();
}
bool NetNodeConfig::init(const boost::program_options::variables_map& vm)
{
bindIp = command_line::get_arg(vm, arg_p2p_bind_ip);
bindPort = command_line::get_arg(vm, arg_p2p_bind_port);
externalPort = command_line::get_arg(vm, arg_p2p_external_port);
allowLocalIp = command_line::get_arg(vm, arg_p2p_allow_local_ip);
configFolder = command_line::get_arg(vm, command_line::arg_data_dir);
2015-07-30 15:22:07 +00:00
p2pStateFilename = CryptoNote::parameters::P2P_NET_DATA_FILENAME;
if (command_line::has_arg(vm, arg_p2p_add_peer))
{
std::vector<std::string> perrs = command_line::get_arg(vm, arg_p2p_add_peer);
for(const std::string& pr_str: perrs)
{
2015-07-30 15:22:07 +00:00
PeerlistEntry pe = boost::value_initialized<PeerlistEntry>();
pe.id = Crypto::rand<uint64_t>();
if (!parsePeerFromString(pe.adr, pr_str)) {
return false;
}
peers.push_back(pe);
}
}
if (command_line::has_arg(vm,arg_p2p_add_exclusive_node)) {
if (!parsePeersAndAddToContainer(vm, arg_p2p_add_exclusive_node, exclusiveNodes))
return false;
}
if (command_line::has_arg(vm, arg_p2p_add_priority_node)) {
if (!parsePeersAndAddToContainer(vm, arg_p2p_add_priority_node, priorityNodes))
return false;
}
if (command_line::has_arg(vm, arg_p2p_seed_node)) {
if (!parsePeersAndAddToContainer(vm, arg_p2p_seed_node, seedNodes))
return false;
}
if(command_line::has_arg(vm, arg_p2p_hide_my_port))
hideMyPort = true;
return true;
}
2015-07-30 15:22:07 +00:00
void NetNodeConfig::setTestnet(bool isTestnet) {
testnet = isTestnet;
}
std::string NetNodeConfig::getP2pStateFilename() const {
if (testnet) {
return "testnet_" + p2pStateFilename;
}
return p2pStateFilename;
}
bool NetNodeConfig::getTestnet() const {
return testnet;
}
std::string NetNodeConfig::getBindIp() const {
return bindIp;
}
uint16_t NetNodeConfig::getBindPort() const {
return bindPort;
}
uint16_t NetNodeConfig::getExternalPort() const {
return externalPort;
}
bool NetNodeConfig::getAllowLocalIp() const {
return allowLocalIp;
}
std::vector<PeerlistEntry> NetNodeConfig::getPeers() const {
return peers;
}
std::vector<NetworkAddress> NetNodeConfig::getPriorityNodes() const {
return priorityNodes;
}
std::vector<NetworkAddress> NetNodeConfig::getExclusiveNodes() const {
return exclusiveNodes;
}
std::vector<NetworkAddress> NetNodeConfig::getSeedNodes() const {
return seedNodes;
}
bool NetNodeConfig::getHideMyPort() const {
return hideMyPort;
}
std::string NetNodeConfig::getConfigFolder() const {
return configFolder;
}
void NetNodeConfig::setP2pStateFilename(const std::string& filename) {
p2pStateFilename = filename;
}
void NetNodeConfig::setBindIp(const std::string& ip) {
bindIp = ip;
}
void NetNodeConfig::setBindPort(uint16_t port) {
bindPort = port;
}
void NetNodeConfig::setExternalPort(uint16_t port) {
externalPort = port;
}
void NetNodeConfig::setAllowLocalIp(bool allow) {
allowLocalIp = allow;
}
void NetNodeConfig::setPeers(const std::vector<PeerlistEntry>& peerList) {
peers = peerList;
}
void NetNodeConfig::setPriorityNodes(const std::vector<NetworkAddress>& addresses) {
priorityNodes = addresses;
}
void NetNodeConfig::setExclusiveNodes(const std::vector<NetworkAddress>& addresses) {
exclusiveNodes = addresses;
}
void NetNodeConfig::setSeedNodes(const std::vector<NetworkAddress>& addresses) {
seedNodes = addresses;
}
void NetNodeConfig::setHideMyPort(bool hide) {
hideMyPort = hide;
}
void NetNodeConfig::setConfigFolder(const std::string& folder) {
configFolder = folder;
}
} //namespace nodetool