14 #include <boost/filesystem/operations.hpp> 17 #include <event2/event.h> 18 #include <event2/http.h> 19 #include <event2/buffer.h> 20 #include <event2/keyvalq_struct.h> 36 strUsage +=
HelpMessageOpt(
"-datadir=<dir>",
_(
"Specify data directory"));
40 strUsage +=
HelpMessageOpt(
"-rpcwait",
_(
"Wait for RPC server to start"));
41 strUsage +=
HelpMessageOpt(
"-rpcuser=<user>",
_(
"Username for JSON-RPC connections"));
42 strUsage +=
HelpMessageOpt(
"-rpcpassword=<pw>",
_(
"Password for JSON-RPC connections"));
78 std::string strUsage =
_(
"Dash Core RPC client version") +
" " +
FormatFullVersion() +
"\n";
79 if (!
mapArgs.count(
"-version")) {
80 strUsage +=
"\n" +
_(
"Usage:") +
"\n" +
81 " dash-cli [options] <command> [params] " +
_(
"Send command to Dash Core") +
"\n" +
82 " dash-cli [options] help " +
_(
"List commands") +
"\n" +
83 " dash-cli [options] help <command> " +
_(
"Get help for a command") +
"\n";
88 fprintf(
stdout,
"%s", strUsage.c_str());
90 fprintf(
stderr,
"Error: too few parameters\n");
95 bool datadirFromCmdLine =
mapArgs.count(
"-datadir") != 0;
96 if (datadirFromCmdLine && !boost::filesystem::is_directory(
GetDataDir(
false))) {
97 fprintf(
stderr,
"Error: Specified data directory \"%s\" does not exist.\n",
mapArgs[
"-datadir"].c_str());
102 }
catch (
const std::exception& e) {
103 fprintf(
stderr,
"Error reading configuration file: %s\n", e.what());
106 if (!datadirFromCmdLine && !boost::filesystem::is_directory(
GetDataDir(
false))) {
107 fprintf(
stderr,
"Error: Specified data directory \"%s\" from config file does not exist.\n",
mapArgs[
"-datadir"].c_str());
113 }
catch (
const std::exception& e) {
114 fprintf(
stderr,
"Error: %s\n", e.what());
119 fprintf(
stderr,
"Error: SSL mode for RPC (-rpcssl) is no longer supported.\n");
145 reply->
status = evhttp_request_get_response_code(req);
147 struct evbuffer *buf = evhttp_request_get_input_buffer(req);
150 size_t size = evbuffer_get_length(buf);
151 const char *
data = (
const char*)evbuffer_pullup(buf, size);
153 reply->
body = std::string(
data, size);
154 evbuffer_drain(buf, size);
164 struct event_base *
base = event_base_new();
166 throw runtime_error(
"cannot create event_base");
169 struct evhttp_connection *evcon = evhttp_connection_base_new(
base, NULL, host.c_str(),
port);
171 throw runtime_error(
"create connection failed");
175 struct evhttp_request *req = evhttp_request_new(
http_request_done, (
void*)&response);
177 throw runtime_error(
"create http request failed");
181 if (
mapArgs[
"-rpcpassword"] ==
"") {
185 _(
"Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"),
193 struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
194 assert(output_headers);
195 evhttp_add_header(output_headers,
"Host", host.c_str());
196 evhttp_add_header(output_headers,
"Connection",
"close");
201 struct evbuffer * output_buffer = evhttp_request_get_output_buffer(req);
202 assert(output_buffer);
203 evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
205 int r = evhttp_make_request(evcon, req, EVHTTP_REQ_POST,
"/");
207 evhttp_connection_free(evcon);
208 event_base_free(
base);
212 event_base_dispatch(
base);
213 evhttp_connection_free(evcon);
214 event_base_free(
base);
216 if (response.status == 0)
219 throw runtime_error(
"incorrect rpcuser or rpcpassword (authorization failed)");
221 throw runtime_error(
strprintf(
"server returned HTTP error %d", response.status));
222 else if (response.body.empty())
223 throw runtime_error(
"no response from server");
227 if (!valReply.
read(response.body))
228 throw runtime_error(
"couldn't parse reply from server");
231 throw runtime_error(
"expected reply to have result, error and id properties");
249 throw runtime_error(
"too few parameters");
250 string strMethod = argv[1];
253 std::vector<std::string> strParams(&argv[2], &argv[argc]);
257 const bool fWait =
GetBoolArg(
"-rpcwait",
false);
266 if (!
error.isNull()) {
268 int code =
error[
"code"].get_int();
271 strPrint =
"error: " +
error.write();
273 if (
error.isObject())
277 strPrint = errCode.
isNull() ?
"" :
"error code: "+errCode.
getValStr()+
"\n";
280 strPrint +=
"error message:\n"+errMsg.
get_str();
287 strPrint =
result.get_str();
289 strPrint =
result.write(2);
302 catch (
const boost::thread_interrupted&) {
305 catch (
const std::exception& e) {
306 strPrint = string(
"error: ") + e.what();
314 if (strPrint !=
"") {
315 fprintf((nRet == 0 ?
stdout :
stderr),
"%s\n", strPrint.c_str());
320 int main(
int argc,
char* argv[])
324 fprintf(
stderr,
"Error: Initializing networking failed\n");
333 catch (
const std::exception& e) {
341 int ret = EXIT_FAILURE;
345 catch (
const std::exception& e) {
UniValue CallRPC(const string &strMethod, const UniValue ¶ms)
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
std::string HelpMessageOpt(const std::string &option, const std::string &message)
void AppendParamsHelpMessages(std::string &strUsage, bool debugHelp)
static const int DEFAULT_HTTP_CLIENT_TIMEOUT
void MilliSleep(int64_t n)
const char *const BITCOIN_CONF_FILENAME
std::string HelpMessageCli()
static std::string strRPCUserColonPass
int CommandLineRPC(int argc, char *argv[])
static const std::string TESTNET
const std::string & getValStr() const
const CBaseChainParams & BaseParams()
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
string JSONRPCRequest(const string &strMethod, const UniValue ¶ms, const UniValue &id)
int main(int argc, char *argv[])
Transaction already in chain.
const UniValue & find_value(const UniValue &obj, const std::string &name)
string EncodeBase64(const unsigned char *pch, size_t len)
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
bool GetAuthCookie(std::string *cookie_out)
const UniValue & get_obj() const
bool GetBoolArg(const std::string &strArg, bool fDefault)
static secp256k1_context * ctx
static bool error(const char *format)
static const char DEFAULT_RPCCONNECT[]
static int AppInitRPC(int argc, char *argv[])
void ParseParameters(int argc, const char *const argv[])
std::string FormatFullVersion()
static const int CONTINUE_EXECUTION
CConnectionFailed(const std::string &msg)
bool IsSwitchChar(char c)
void ReadConfigFile(map< string, string > &mapSettingsRet, map< string, vector< string > > &mapMultiSettingsRet)
std::string ChainNameFromCommandLine()
static void http_request_done(struct evhttp_request *req, void *ctx)
static const std::string MAIN
std::string GetArg(const std::string &strArg, const std::string &strDefault)
void SelectBaseParams(const std::string &chain)
bool read(const char *raw)
map< string, vector< string > > mapMultiArgs
std::string HelpMessageGroup(const std::string &message)
std::string get_str() const
std::string _(const char *psz)
map< string, string > mapArgs
boost::filesystem::path GetConfigFile()