]> Git Repo - VerusCoin.git/blob - src/rpcprotocol.h
Update comments in src/rpc* to be doxygen compatible
[VerusCoin.git] / src / rpcprotocol.h
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_RPCPROTOCOL_H
7 #define BITCOIN_RPCPROTOCOL_H
8
9 #include <list>
10 #include <map>
11 #include <stdint.h>
12 #include <string>
13 #include <boost/iostreams/concepts.hpp>
14 #include <boost/iostreams/stream.hpp>
15 #include <boost/asio.hpp>
16 #include <boost/asio/ssl.hpp>
17
18 #include "json/json_spirit_reader_template.h"
19 #include "json/json_spirit_utils.h"
20 #include "json/json_spirit_writer_template.h"
21
22 //! HTTP status codes
23 enum HTTPStatusCode
24 {
25     HTTP_OK                    = 200,
26     HTTP_BAD_REQUEST           = 400,
27     HTTP_UNAUTHORIZED          = 401,
28     HTTP_FORBIDDEN             = 403,
29     HTTP_NOT_FOUND             = 404,
30     HTTP_INTERNAL_SERVER_ERROR = 500,
31 };
32
33 //! Bitcoin RPC error codes
34 enum RPCErrorCode
35 {
36     //! Standard JSON-RPC 2.0 errors
37     RPC_INVALID_REQUEST  = -32600,
38     RPC_METHOD_NOT_FOUND = -32601,
39     RPC_INVALID_PARAMS   = -32602,
40     RPC_INTERNAL_ERROR   = -32603,
41     RPC_PARSE_ERROR      = -32700,
42
43     //! General application defined errors
44     RPC_MISC_ERROR                  = -1,  //! std::exception thrown in command handling
45     RPC_FORBIDDEN_BY_SAFE_MODE      = -2,  //! Server is in safe mode, and command is not allowed in safe mode
46     RPC_TYPE_ERROR                  = -3,  //! Unexpected type was passed as parameter
47     RPC_INVALID_ADDRESS_OR_KEY      = -5,  //! Invalid address or key
48     RPC_OUT_OF_MEMORY               = -7,  //! Ran out of memory during operation
49     RPC_INVALID_PARAMETER           = -8,  //! Invalid, missing or duplicate parameter
50     RPC_DATABASE_ERROR              = -20, //! Database error
51     RPC_DESERIALIZATION_ERROR       = -22, //! Error parsing or validating structure in raw format
52     RPC_VERIFY_ERROR                = -25, //! General error during transaction or block submission
53     RPC_VERIFY_REJECTED             = -26, //! Transaction or block was rejected by network rules
54     RPC_VERIFY_ALREADY_IN_CHAIN     = -27, //! Transaction already in chain
55     RPC_IN_WARMUP                   = -28, //! Client still warming up
56
57     //! Aliases for backward compatibility
58     RPC_TRANSACTION_ERROR           = RPC_VERIFY_ERROR,
59     RPC_TRANSACTION_REJECTED        = RPC_VERIFY_REJECTED,
60     RPC_TRANSACTION_ALREADY_IN_CHAIN= RPC_VERIFY_ALREADY_IN_CHAIN,
61
62     //! P2P client errors
63     RPC_CLIENT_NOT_CONNECTED        = -9,  //! Bitcoin is not connected
64     RPC_CLIENT_IN_INITIAL_DOWNLOAD  = -10, //! Still downloading initial blocks
65     RPC_CLIENT_NODE_ALREADY_ADDED   = -23, //! Node is already added
66     RPC_CLIENT_NODE_NOT_ADDED       = -24, //! Node has not been added before
67
68     //! Wallet errors
69     RPC_WALLET_ERROR                = -4,  //! Unspecified problem with wallet (key not found etc.)
70     RPC_WALLET_INSUFFICIENT_FUNDS   = -6,  //! Not enough funds in wallet or account
71     RPC_WALLET_INVALID_ACCOUNT_NAME = -11, //! Invalid account name
72     RPC_WALLET_KEYPOOL_RAN_OUT      = -12, //! Keypool ran out, call keypoolrefill first
73     RPC_WALLET_UNLOCK_NEEDED        = -13, //! Enter the wallet passphrase with walletpassphrase first
74     RPC_WALLET_PASSPHRASE_INCORRECT = -14, //! The wallet passphrase entered was incorrect
75     RPC_WALLET_WRONG_ENC_STATE      = -15, //! Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
76     RPC_WALLET_ENCRYPTION_FAILED    = -16, //! Failed to encrypt the wallet
77     RPC_WALLET_ALREADY_UNLOCKED     = -17, //! Wallet is already unlocked
78 };
79
80 /**
81  * IOStream device that speaks SSL but can also speak non-SSL
82  */
83 template <typename Protocol>
84 class SSLIOStreamDevice : public boost::iostreams::device<boost::iostreams::bidirectional> {
85 public:
86     SSLIOStreamDevice(boost::asio::ssl::stream<typename Protocol::socket> &streamIn, bool fUseSSLIn) : stream(streamIn)
87     {
88         fUseSSL = fUseSSLIn;
89         fNeedHandshake = fUseSSLIn;
90     }
91
92     void handshake(boost::asio::ssl::stream_base::handshake_type role)
93     {
94         if (!fNeedHandshake) return;
95         fNeedHandshake = false;
96         stream.handshake(role);
97     }
98     std::streamsize read(char* s, std::streamsize n)
99     {
100         handshake(boost::asio::ssl::stream_base::server); // HTTPS servers read first
101         if (fUseSSL) return stream.read_some(boost::asio::buffer(s, n));
102         return stream.next_layer().read_some(boost::asio::buffer(s, n));
103     }
104     std::streamsize write(const char* s, std::streamsize n)
105     {
106         handshake(boost::asio::ssl::stream_base::client); // HTTPS clients write first
107         if (fUseSSL) return boost::asio::write(stream, boost::asio::buffer(s, n));
108         return boost::asio::write(stream.next_layer(), boost::asio::buffer(s, n));
109     }
110     bool connect(const std::string& server, const std::string& port)
111     {
112         using namespace boost::asio::ip;
113         tcp::resolver resolver(stream.get_io_service());
114         tcp::resolver::iterator endpoint_iterator;
115 #if BOOST_VERSION >= 104300
116         try {
117 #endif
118             // The default query (flags address_configured) tries IPv6 if
119             // non-localhost IPv6 configured, and IPv4 if non-localhost IPv4
120             // configured.
121             tcp::resolver::query query(server.c_str(), port.c_str());
122             endpoint_iterator = resolver.resolve(query);
123 #if BOOST_VERSION >= 104300
124         } catch(boost::system::system_error &e)
125         {
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);
129         }
130 #endif
131         boost::system::error_code error = boost::asio::error::host_not_found;
132         tcp::resolver::iterator end;
133         while (error && endpoint_iterator != end)
134         {
135             stream.lowest_layer().close();
136             stream.lowest_layer().connect(*endpoint_iterator++, error);
137         }
138         if (error)
139             return false;
140         return true;
141     }
142
143 private:
144     bool fNeedHandshake;
145     bool fUseSSL;
146     boost::asio::ssl::stream<typename Protocol::socket>& stream;
147 };
148
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);
167
168 #endif // BITCOIN_RPCPROTOCOL_H
This page took 0.032916 seconds and 4 git commands to generate.