Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include "config/bitcoin-config.h" | #include "config/bitcoin-config.h" | ||||
#endif | #endif | ||||
#include "chainparamsbase.h" | #include "chainparamsbase.h" | ||||
#include "clientversion.h" | #include "clientversion.h" | ||||
#include "fs.h" | |||||
#include "rpc/client.h" | #include "rpc/client.h" | ||||
#include "rpc/protocol.h" | #include "rpc/protocol.h" | ||||
#include "support/events.h" | #include "support/events.h" | ||||
#include "util.h" | #include "util.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
#include <boost/filesystem/operations.hpp> | #include <boost/filesystem/operations.hpp> | ||||
#include <cstdio> | #include <cstdio> | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | strUsage += | ||||
strprintf(_("Timeout in seconds during HTTP requests, " | strprintf(_("Timeout in seconds during HTTP requests, " | ||||
"or 0 for no timeout. (default: %d)"), | "or 0 for no timeout. (default: %d)"), | ||||
DEFAULT_HTTP_CLIENT_TIMEOUT)); | DEFAULT_HTTP_CLIENT_TIMEOUT)); | ||||
strUsage += HelpMessageOpt( | strUsage += HelpMessageOpt( | ||||
"-stdin", _("Read extra arguments from standard input, one per line " | "-stdin", _("Read extra arguments from standard input, one per line " | ||||
"until EOF/Ctrl-D (recommended for sensitive information " | "until EOF/Ctrl-D (recommended for sensitive information " | ||||
"such as passphrases)")); | "such as passphrases)")); | ||||
strUsage += HelpMessageOpt( | strUsage += HelpMessageOpt( | ||||
"-usewallet=<walletname>", | "-rpcwallet=<walletname>", | ||||
_("Send RPC for non-default wallet on RPC server (argument is wallet " | _("Send RPC for non-default wallet on RPC server (argument is wallet " | ||||
"filename in bitcoind directory, required if bitcoind/-Qt runs with " | "filename in bitcoind directory, required if bitcoind/-Qt runs with " | ||||
"multiple wallets)")); | "multiple wallets)")); | ||||
return strUsage; | return strUsage; | ||||
} | } | ||||
////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////// | ||||
▲ Show 20 Lines • Show All 132 Lines • ▼ Show 20 Lines | |||||
#if LIBEVENT_VERSION_NUMBER >= 0x02010300 | #if LIBEVENT_VERSION_NUMBER >= 0x02010300 | ||||
static void http_error_cb(enum evhttp_request_error err, void *ctx) { | static void http_error_cb(enum evhttp_request_error err, void *ctx) { | ||||
HTTPReply *reply = static_cast<HTTPReply *>(ctx); | HTTPReply *reply = static_cast<HTTPReply *>(ctx); | ||||
reply->error = err; | reply->error = err; | ||||
} | } | ||||
#endif | #endif | ||||
UniValue CallRPC(const std::string &strMethod, const UniValue ¶ms) { | UniValue CallRPC(const std::string &strMethod, const UniValue ¶ms) { | ||||
std::string host = gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT); | std::string host; | ||||
int port = gArgs.GetArg("-rpcport", BaseParams().RPCPort()); | // In preference order, we choose the following for the port: | ||||
// 1. -rpcport | |||||
// 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6) | |||||
// 3. default port for chain | |||||
int port = BaseParams().RPCPort(); | |||||
SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host); | |||||
port = gArgs.GetArg("-rpcport", port); | |||||
// Obtain event base | // Obtain event base | ||||
raii_event_base base = obtain_event_base(); | raii_event_base base = obtain_event_base(); | ||||
// Synchronously look up hostname | // Synchronously look up hostname | ||||
raii_evhttp_connection evcon = | raii_evhttp_connection evcon = | ||||
obtain_evhttp_connection_base(base.get(), host, port); | obtain_evhttp_connection_base(base.get(), host, port); | ||||
evhttp_connection_set_timeout( | evhttp_connection_set_timeout( | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | std::string strRequest = | ||||
JSONRPCRequestObj(strMethod, params, 1).write() + "\n"; | JSONRPCRequestObj(strMethod, params, 1).write() + "\n"; | ||||
struct evbuffer *output_buffer = | struct evbuffer *output_buffer = | ||||
evhttp_request_get_output_buffer(req.get()); | evhttp_request_get_output_buffer(req.get()); | ||||
assert(output_buffer); | assert(output_buffer); | ||||
evbuffer_add(output_buffer, strRequest.data(), strRequest.size()); | evbuffer_add(output_buffer, strRequest.data(), strRequest.size()); | ||||
// check if we should use a special wallet endpoint | // check if we should use a special wallet endpoint | ||||
std::string endpoint = "/"; | std::string endpoint = "/"; | ||||
std::string walletName = gArgs.GetArg("-usewallet", ""); | std::string walletName = gArgs.GetArg("-rpcwallet", ""); | ||||
if (!walletName.empty()) { | if (!walletName.empty()) { | ||||
char *encodedURI = | char *encodedURI = | ||||
evhttp_uriencode(walletName.c_str(), walletName.size(), false); | evhttp_uriencode(walletName.c_str(), walletName.size(), false); | ||||
if (encodedURI) { | if (encodedURI) { | ||||
endpoint = "/wallet/" + std::string(encodedURI); | endpoint = "/wallet/" + std::string(encodedURI); | ||||
free(encodedURI); | free(encodedURI); | ||||
} else { | } else { | ||||
throw CConnectionFailed("uri-encode failed"); | throw CConnectionFailed("uri-encode failed"); | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |