2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2015-04-06 16:13:07 +00:00
//
// 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>
# include <Common/util.h>
# include "Common/command_line.h"
# include "Common/StringTools.h"
# include "crypto/crypto.h"
2015-04-06 16:13:07 +00:00
# include "cryptonote_config.h"
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2015-04-06 16:13:07 +00:00
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-05-27 12:08:46 +00:00
const command_line : : arg_descriptor < std : : string > arg_p2p_bind_port = { " p2p-bind-port " , " Port for p2p network protocol " , std : : to_string ( P2P_DEFAULT_PORT ) } ;
2015-04-06 16:13:07 +00:00
const command_line : : arg_descriptor < uint32_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-05-27 12:08:46 +00:00
bool parsePeerFromString ( net_address & pe , const std : : string & node_addr ) {
return Common : : parseIpAddressAndPort ( pe . ip , pe . port , node_addr ) ;
2015-04-06 16:13:07 +00:00
}
bool parsePeersAndAddToContainer ( const boost : : program_options : : variables_map & vm ,
2015-05-27 12:08:46 +00:00
const command_line : : arg_descriptor < std : : vector < std : : string > > & arg , std : : vector < net_address > & container )
2015-04-06 16:13:07 +00:00
{
std : : vector < std : : string > peers = command_line : : get_arg ( vm , arg ) ;
for ( const std : : string & str : peers ) {
2015-05-27 12:08:46 +00:00
net_address na = boost : : value_initialized < net_address > ( ) ;
2015-04-06 16:13:07 +00:00
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-05-27 12:08:46 +00:00
bindPort = std : : to_string ( P2P_DEFAULT_PORT ) ;
2015-04-06 16:13:07 +00:00
externalPort = 0 ;
allowLocalIp = false ;
hideMyPort = false ;
configFolder = tools : : get_default_data_dir ( ) ;
}
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 ) ;
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-05-27 12:08:46 +00:00
peerlist_entry pe = boost : : value_initialized < peerlist_entry > ( ) ;
2015-04-06 16:13:07 +00:00
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 ;
}
} //namespace nodetool