Anoncoin  0.9.4
P2P Digital Currency
rpcprotocol.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2013-2014 The Anoncoin Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef _ANONCOINRPC_PROTOCOL_H_
8 #define _ANONCOINRPC_PROTOCOL_H_
9 
10 #include <list>
11 #include <map>
12 #include <stdint.h>
13 #include <string>
14 #include <boost/iostreams/concepts.hpp>
15 #include <boost/iostreams/stream.hpp>
16 #include <boost/asio.hpp>
17 #include <boost/asio/ssl.hpp>
18 
19 #include "json/json_spirit_reader_template.h"
20 #include "json/json_spirit_utils.h"
21 #include "json/json_spirit_writer_template.h"
22 
23 // HTTP status codes
25 {
26  HTTP_OK = 200,
32 };
33 
34 // Anoncoin RPC error codes
36 {
37  // Standard JSON-RPC 2.0 errors
42  RPC_PARSE_ERROR = -32700,
43 
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_TRANSACTION_ERROR = -25, // General error during transaction submission
54  RPC_TRANSACTION_REJECTED = -26, // Transaction was rejected by network rules
55  RPC_TRANSACTION_ALREADY_IN_CHAIN= -27, // Transaction already in chain
56 
57  // P2P client errors
58  RPC_CLIENT_NOT_CONNECTED = -9, // Anoncoin is not connected
59  RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, // Still downloading initial blocks
60  RPC_CLIENT_NODE_ALREADY_ADDED = -23, // Node is already added
61  RPC_CLIENT_NODE_NOT_ADDED = -24, // Node has not been added before
62 
63  // Wallet errors
64  RPC_WALLET_ERROR = -4, // Unspecified problem with wallet (key not found etc.)
65  RPC_WALLET_INSUFFICIENT_FUNDS = -6, // Not enough funds in wallet or account
66  RPC_WALLET_INVALID_ACCOUNT_NAME = -11, // Invalid account name
67  RPC_WALLET_KEYPOOL_RAN_OUT = -12, // Keypool ran out, call keypoolrefill first
68  RPC_WALLET_UNLOCK_NEEDED = -13, // Enter the wallet passphrase with walletpassphrase first
69  RPC_WALLET_PASSPHRASE_INCORRECT = -14, // The wallet passphrase entered was incorrect
70  RPC_WALLET_WRONG_ENC_STATE = -15, // Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
71  RPC_WALLET_ENCRYPTION_FAILED = -16, // Failed to encrypt the wallet
72  RPC_WALLET_ALREADY_UNLOCKED = -17, // Wallet is already unlocked
73 };
74 
75 //
76 // IOStream device that speaks SSL but can also speak non-SSL
77 //
78 template <typename Protocol>
79 class SSLIOStreamDevice : public boost::iostreams::device<boost::iostreams::bidirectional> {
80 public:
81  SSLIOStreamDevice(boost::asio::ssl::stream<typename Protocol::socket> &streamIn, bool fUseSSLIn) : stream(streamIn)
82  {
83  fUseSSL = fUseSSLIn;
84  fNeedHandshake = fUseSSLIn;
85  }
86 
87  void handshake(boost::asio::ssl::stream_base::handshake_type role)
88  {
89  if (!fNeedHandshake) return;
90  fNeedHandshake = false;
91  stream.handshake(role);
92  }
93  std::streamsize read(char* s, std::streamsize n)
94  {
95  handshake(boost::asio::ssl::stream_base::server); // HTTPS servers read first
96  if (fUseSSL) return stream.read_some(boost::asio::buffer(s, n));
97  return stream.next_layer().read_some(boost::asio::buffer(s, n));
98  }
99  std::streamsize write(const char* s, std::streamsize n)
100  {
101  handshake(boost::asio::ssl::stream_base::client); // HTTPS clients write first
102  if (fUseSSL) return boost::asio::write(stream, boost::asio::buffer(s, n));
103  return boost::asio::write(stream.next_layer(), boost::asio::buffer(s, n));
104  }
105  bool connect(const std::string& server, const std::string& port)
106  {
107  boost::asio::ip::tcp::resolver resolver(stream.get_io_service());
108  boost::asio::ip::tcp::resolver::query query(server.c_str(), port.c_str());
109  boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
110  boost::asio::ip::tcp::resolver::iterator end;
111  boost::system::error_code error = boost::asio::error::host_not_found;
112  while (error && endpoint_iterator != end)
113  {
114  stream.lowest_layer().close();
115  stream.lowest_layer().connect(*endpoint_iterator++, error);
116  }
117  if (error)
118  return false;
119  return true;
120  }
121 
122 private:
124  bool fUseSSL;
125  boost::asio::ssl::stream<typename Protocol::socket>& stream;
126 };
127 
128 std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
129 std::string HTTPReply(int nStatus, const std::string& strMsg, bool keepalive);
130 bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
131  std::string& http_method, std::string& http_uri);
132 int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto);
133 int ReadHTTPHeaders(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet);
134 int ReadHTTPMessage(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet,
135  std::string& strMessageRet, int nProto);
136 std::string JSONRPCRequest(const std::string& strMethod, const json_spirit::Array& params, const json_spirit::Value& id);
137 json_spirit::Object JSONRPCReplyObj(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id);
138 std::string JSONRPCReply(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id);
139 json_spirit::Object JSONRPCError(int code, const std::string& message);
140 
141 #endif
std::streamsize read(char *s, std::streamsize n)
Definition: rpcprotocol.h:93
int ReadHTTPHeaders(std::basic_istream< char > &stream, std::map< std::string, std::string > &mapHeadersRet)
json_spirit::Object JSONRPCReplyObj(const json_spirit::Value &result, const json_spirit::Value &error, const json_spirit::Value &id)
RPCErrorCode
Definition: rpcprotocol.h:35
int ReadHTTPMessage(std::basic_istream< char > &stream, std::map< std::string, std::string > &mapHeadersRet, std::string &strMessageRet, int nProto)
bool ReadHTTPRequestLine(std::basic_istream< char > &stream, int &proto, std::string &http_method, std::string &http_uri)
std::string JSONRPCRequest(const std::string &strMethod, const json_spirit::Array &params, const json_spirit::Value &id)
std::string HTTPPost(const std::string &strMsg, const std::map< std::string, std::string > &mapRequestHeaders)
std::streamsize write(const char *s, std::streamsize n)
Definition: rpcprotocol.h:99
std::string JSONRPCReply(const json_spirit::Value &result, const json_spirit::Value &error, const json_spirit::Value &id)
std::string HTTPReply(int nStatus, const std::string &strMsg, bool keepalive)
HTTPStatusCode
Definition: rpcprotocol.h:24
boost::asio::ssl::stream< typename Protocol::socket > & stream
Definition: rpcprotocol.h:125
json_spirit::Object JSONRPCError(int code, const std::string &message)
void handshake(boost::asio::ssl::stream_base::handshake_type role)
Definition: rpcprotocol.h:87
SSLIOStreamDevice(boost::asio::ssl::stream< typename Protocol::socket > &streamIn, bool fUseSSLIn)
Definition: rpcprotocol.h:81
int ReadHTTPStatus(std::basic_istream< char > &stream, int &proto)
bool connect(const std::string &server, const std::string &port)
Definition: rpcprotocol.h:105