1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_RPCPROTOCOL_H
7 #define BITCOIN_RPCPROTOCOL_H
13 #include <boost/iostreams/concepts.hpp>
14 #include <boost/iostreams/stream.hpp>
15 #include <boost/asio.hpp>
16 #include <boost/asio/ssl.hpp>
18 #include "json/json_spirit_reader_template.h"
19 #include "json/json_spirit_utils.h"
20 #include "json/json_spirit_writer_template.h"
26 HTTP_BAD_REQUEST = 400,
27 HTTP_UNAUTHORIZED = 401,
30 HTTP_INTERNAL_SERVER_ERROR = 500,
31 HTTP_SERVICE_UNAVAILABLE = 503,
34 //! Bitcoin RPC error codes
37 //! Standard JSON-RPC 2.0 errors
38 RPC_INVALID_REQUEST = -32600,
39 RPC_METHOD_NOT_FOUND = -32601,
40 RPC_INVALID_PARAMS = -32602,
41 RPC_INTERNAL_ERROR = -32603,
42 RPC_PARSE_ERROR = -32700,
44 //! General application defined errors
45 RPC_MISC_ERROR = -1, //! std::exception thrown in command handling
46 RPC_FORBIDDEN_BY_SAFE_MODE = -2, //! Server is in safe mode, and command is not allowed in safe mode
47 RPC_TYPE_ERROR = -3, //! Unexpected type was passed as parameter
48 RPC_INVALID_ADDRESS_OR_KEY = -5, //! Invalid address or key
49 RPC_OUT_OF_MEMORY = -7, //! Ran out of memory during operation
50 RPC_INVALID_PARAMETER = -8, //! Invalid, missing or duplicate parameter
51 RPC_DATABASE_ERROR = -20, //! Database error
52 RPC_DESERIALIZATION_ERROR = -22, //! Error parsing or validating structure in raw format
53 RPC_VERIFY_ERROR = -25, //! General error during transaction or block submission
54 RPC_VERIFY_REJECTED = -26, //! Transaction or block was rejected by network rules
55 RPC_VERIFY_ALREADY_IN_CHAIN = -27, //! Transaction already in chain
56 RPC_IN_WARMUP = -28, //! Client still warming up
58 //! Aliases for backward compatibility
59 RPC_TRANSACTION_ERROR = RPC_VERIFY_ERROR,
60 RPC_TRANSACTION_REJECTED = RPC_VERIFY_REJECTED,
61 RPC_TRANSACTION_ALREADY_IN_CHAIN= RPC_VERIFY_ALREADY_IN_CHAIN,
64 RPC_CLIENT_NOT_CONNECTED = -9, //! Bitcoin is not connected
65 RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, //! Still downloading initial blocks
66 RPC_CLIENT_NODE_ALREADY_ADDED = -23, //! Node is already added
67 RPC_CLIENT_NODE_NOT_ADDED = -24, //! Node has not been added before
70 RPC_WALLET_ERROR = -4, //! Unspecified problem with wallet (key not found etc.)
71 RPC_WALLET_INSUFFICIENT_FUNDS = -6, //! Not enough funds in wallet or account
72 RPC_WALLET_ACCOUNTS_DEPRECATED = -11, //! Accounts are deprecated
73 RPC_WALLET_KEYPOOL_RAN_OUT = -12, //! Keypool ran out, call keypoolrefill first
74 RPC_WALLET_UNLOCK_NEEDED = -13, //! Enter the wallet passphrase with walletpassphrase first
75 RPC_WALLET_PASSPHRASE_INCORRECT = -14, //! The wallet passphrase entered was incorrect
76 RPC_WALLET_WRONG_ENC_STATE = -15, //! Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
77 RPC_WALLET_ENCRYPTION_FAILED = -16, //! Failed to encrypt the wallet
78 RPC_WALLET_ALREADY_UNLOCKED = -17, //! Wallet is already unlocked
82 * IOStream device that speaks SSL but can also speak non-SSL
84 template <typename Protocol>
85 class SSLIOStreamDevice : public boost::iostreams::device<boost::iostreams::bidirectional> {
87 SSLIOStreamDevice(boost::asio::ssl::stream<typename Protocol::socket> &streamIn, bool fUseSSLIn) : stream(streamIn)
90 fNeedHandshake = fUseSSLIn;
93 void handshake(boost::asio::ssl::stream_base::handshake_type role)
95 if (!fNeedHandshake) return;
96 fNeedHandshake = false;
97 stream.handshake(role);
99 std::streamsize read(char* s, std::streamsize n)
101 handshake(boost::asio::ssl::stream_base::server); // HTTPS servers read first
102 if (fUseSSL) return stream.read_some(boost::asio::buffer(s, n));
103 return stream.next_layer().read_some(boost::asio::buffer(s, n));
105 std::streamsize write(const char* s, std::streamsize n)
107 handshake(boost::asio::ssl::stream_base::client); // HTTPS clients write first
108 if (fUseSSL) return boost::asio::write(stream, boost::asio::buffer(s, n));
109 return boost::asio::write(stream.next_layer(), boost::asio::buffer(s, n));
111 bool connect(const std::string& server, const std::string& port)
113 using namespace boost::asio::ip;
114 tcp::resolver resolver(stream.get_io_service());
115 tcp::resolver::iterator endpoint_iterator;
116 #if BOOST_VERSION >= 104300
119 // The default query (flags address_configured) tries IPv6 if
120 // non-localhost IPv6 configured, and IPv4 if non-localhost IPv4
122 tcp::resolver::query query(server.c_str(), port.c_str());
123 endpoint_iterator = resolver.resolve(query);
124 #if BOOST_VERSION >= 104300
125 } catch (const boost::system::system_error&) {
126 // If we at first don't succeed, try blanket lookup (IPv4+IPv6 independent of configured interfaces)
127 tcp::resolver::query query(server.c_str(), port.c_str(), resolver_query_base::flags());
128 endpoint_iterator = resolver.resolve(query);
131 boost::system::error_code error = boost::asio::error::host_not_found;
132 tcp::resolver::iterator end;
133 while (error && endpoint_iterator != end)
135 stream.lowest_layer().close();
136 stream.lowest_layer().connect(*endpoint_iterator++, error);
146 boost::asio::ssl::stream<typename Protocol::socket>& stream;
149 std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
150 std::string HTTPError(int nStatus, bool keepalive,
151 bool headerOnly = false);
152 std::string HTTPReplyHeader(int nStatus, bool keepalive, size_t contentLength,
153 const char *contentType = "application/json");
154 std::string HTTPReply(int nStatus, const std::string& strMsg, bool keepalive,
155 bool headerOnly = false,
156 const char *contentType = "application/json");
157 bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
158 std::string& http_method, std::string& http_uri);
159 int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto);
160 int ReadHTTPHeaders(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet);
161 int ReadHTTPMessage(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet,
162 std::string& strMessageRet, int nProto, size_t max_size);
163 std::string JSONRPCRequest(const std::string& strMethod, const json_spirit::Array& params, const json_spirit::Value& id);
164 json_spirit::Object JSONRPCReplyObj(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id);
165 std::string JSONRPCReply(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id);
166 json_spirit::Object JSONRPCError(int code, const std::string& message);
168 #endif // BITCOIN_RPCPROTOCOL_H