rippled
Connect.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/main/Application.h>
21 #include <ripple/core/Config.h>
22 #include <ripple/net/RPCErr.h>
23 #include <ripple/overlay/Overlay.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/SystemParameters.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/rpc/Context.h>
28 #include <ripple/rpc/impl/Handler.h>
29 
30 namespace ripple {
31 
32 // {
33 // ip: <string>,
34 // port: <number>
35 // }
36 // XXX Might allow domain for manual connections.
39 {
40  if (context.app.config().reporting())
42 
43  if (context.app.config().standalone())
44  return "cannot connect in standalone mode";
45 
46  if (!context.params.isMember(jss::ip))
47  return RPC::missing_field_error(jss::ip);
48 
49  if (context.params.isMember(jss::port) &&
50  !context.params[jss::port].isConvertibleTo(Json::intValue))
51  {
53  }
54 
55  int iPort;
56 
57  if (context.params.isMember(jss::port))
58  iPort = context.params[jss::port].asInt();
59  else
60  iPort = DEFAULT_PEER_PORT;
61 
62  auto const ip_str = context.params[jss::ip].asString();
63  auto ip = beast::IP::Endpoint::from_string(ip_str);
64 
65  if (!is_unspecified(ip))
66  context.app.overlay().connect(ip.at_port(iPort));
67 
68  return RPC::makeObjectValue(
69  "attempting connection to IP:" + ip_str +
70  " port: " + std::to_string(iPort));
71 }
72 
73 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
Json::Value::isConvertibleTo
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:651
ripple::Application::config
virtual Config & config()=0
ripple::Config::standalone
bool standalone() const
Definition: Config.h:332
std::to_string
T to_string(T... args)
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::Overlay::connect
virtual void connect(beast::IP::Endpoint const &address)=0
Establish a peer connection to the specified endpoint.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:37
beast::IP::Endpoint::from_string
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:49
ripple::Application::overlay
virtual Overlay & overlay()=0
ripple::RPC::makeObjectValue
Json::Value makeObjectValue(Value const &value, Json::StaticString const &field=jss::message)
Return a Json::objectValue with a single entry.
Definition: Handler.h:63
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
ripple::doConnect
Json::Value doConnect(RPC::JsonContext &context)
Definition: Connect.cpp:38
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469