size_t detail::process_istream::readsome( char* buf, size_t len ) { int bytesRead = proc.read_some(buf, len, chan); if (bytesRead < 0) FC_THROW("EOF"); else return bytesRead; }
bool generate_keys( char* pubkey, fc::vector<char>& privkey, uint32_t key_size, uint32_t pe ) { static bool init = true; if( init ) { ERR_load_crypto_strings(); init = false; } RSA* rsa = RSA_generate_key( key_size, pe, NULL, NULL ); BN_bn2bin( rsa->n, (unsigned char*)pubkey ); BIO *mem = BIO_new(BIO_s_mem()); int e = PEM_write_bio_RSAPrivateKey(mem, rsa, NULL, NULL, 0, NULL, NULL ); if( e != 1 ) { BIO_free(mem); RSA_free(rsa); FC_THROW(generic_exception("Error writing PrivateKey") ); } char* dat; uint32_t l = BIO_get_mem_data( mem, &dat ); privkey.resize(l); memcpy( &privkey.front(), dat, l ); BIO_free(mem); RSA_free(rsa); return true; }
/** * Blocks until the result code of the process has been returned. */ int process::result() { if (!my->return_code && !my->return_signal) { // we don't have any cached exit status, so wait and obtain the values now my->sshc->my->call_ssh2_function(boost::bind(libssh2_channel_wait_eof, my->chan)); my->sshc->my->call_ssh2_function_throw(boost::bind(libssh2_channel_wait_closed, my->chan), "Error waiting on socket to close: ${message}"); char* exit_signal; char* error_message; libssh2_channel_get_exit_signal(my->chan, &exit_signal, NULL, &error_message, NULL, NULL, NULL); if (exit_signal) { // process terminated with a signal my->return_signal = exit_signal; libssh2_free(my->chan->session, exit_signal); if (error_message) { my->return_signal_message = error_message; libssh2_free(my->chan->session, error_message); } } else my->return_code = libssh2_channel_get_exit_status(my->chan); } if (my->return_signal) FC_THROW("process terminated with signal ${signal}: ${signal_message}", ("signal", *my->return_signal) ("signal_message", my->return_signal_message ? *my->return_signal_message : "")); else return *my->return_code; }
fc::variants parse_recognized_interactive_command( fc::buffered_istream& argument_stream, const rpc_server::method_data& method_data) { fc::variants arguments; for (unsigned i = 0; i < method_data.parameters.size(); ++i) { try { arguments.push_back(_self->parse_argument_of_known_type(argument_stream, method_data, i)); } catch( const fc::eof_exception& e ) { if (method_data.parameters[i].classification != rpc_server::required_positional) return arguments; else FC_THROW("Missing argument ${argument_number} of command \"${command}\"", ("argument_number", i + 1)("command", method_data.name)("cause",e.to_detail_string()) ); } catch( fc::parse_error_exception& e ) { FC_RETHROW_EXCEPTION(e, error, "Error parsing argument ${argument_number} of command \"${command}\": ${detail}", ("argument_number", i + 1)("command", method_data.name)("detail", e.get_log())); } if (method_data.parameters[i].classification == rpc_server::optional_named) break; } return arguments; }
wasm_interface_impl(wasm_interface::vm_type vm) { if(vm == wasm_interface::vm_type::wavm) runtime_interface = std::make_unique<webassembly::wavm::wavm_runtime>(); else if(vm == wasm_interface::vm_type::binaryen) runtime_interface = std::make_unique<webassembly::binaryen::binaryen_runtime>(); else FC_THROW("wasm_interface_impl fall through"); }
bool sign_data( const fc::vector<char>& key, uint32_t key_size, uint32_t pe, const sha1& digest, char* sig ) { RSA* priv = get_priv( key,key_size,pe); if( !priv ) { generic_exception g(fc::generic_exception("Error loading private key: " + fc::string(ERR_error_string( ERR_get_error(),NULL))) ); FC_THROW(g); } uint32_t slen = 0; if( 1 != RSA_sign( NID_sha1, (uint8_t*)digest.data(), sizeof(digest), (unsigned char*)sig, &slen, priv ) ) { RSA_free(priv); generic_exception g(fc::generic_exception("Error signing data: " + fc::string(ERR_error_string( ERR_get_error(),NULL))) ); FC_THROW(g); } RSA_free(priv); return true; }
void rand_bytes(char* buf, int count) { static int init = init_openssl(); (void)init; int result = RAND_bytes((unsigned char*)buf, count); if (result != 1) FC_THROW("Error calling OpenSSL's RAND_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); }
fc::path find_executable_in_path( const fc::string name ) { try { return fc::string(bp::find_executable_in_path( std::string(name), "" )); } catch (...) { const char* p = std::getenv("PATH"); FC_THROW( "Unable to find executable ${exe} in path.", ("exe", name) ("inner", fc::except_str() ) ("PATH", fc::string(p!=nullptr?p:"") ) ); } return fc::path(); }
inline void TConnectionProcessor::TOutboxQueue::sendAuthMsg(const TRequestMessage& auth_msg, const TRecipientPublicKey& to, const fc::ecc::private_key& from) { if(isMailConnected()) { App->send_contact_request(auth_msg, to, from); return; } FC_THROW("No connection to execute send_contact_request"); }
inline void TConnectionProcessor::TOutboxQueue::sendMail(const TPhysicalMailMessage& email, const TRecipientPublicKey& to, const fc::ecc::private_key& from) { if(isMailConnected()) { App->send_email(email, to, from); return; } FC_THROW("No connection to execute send_email"); }
int detail::process_impl::write_some( const char* data, size_t len, int stream_id ) { if( !sshc->my->session ) { FC_THROW( "Session closed" ); } int rc; const char* buf = data; size_t buflen = len; do { rc = sshc->my->call_ssh2_function_throw(boost::bind(libssh2_channel_write_ex, chan, stream_id, buf, buflen), "write failed: ${message}"); if( rc > 0 ) { buf += rc; buflen -= rc; return buf-data; } else if( rc == 0 ) { if( libssh2_channel_eof( chan ) ) { FC_THROW( "EOF" ); //return -1; // eof } } } while( rc >= 0 && buflen); return buf-data; }
void tcp_socket::bind(const fc::ip::endpoint& local_endpoint) { try { my->_sock.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(local_endpoint.get_address()), local_endpoint.port())); } catch (const std::exception& except) { elog("Exception binding outgoing connection to desired local endpoint ${endpoint}: ${what}", ("endpoint", local_endpoint)("what", except.what())); FC_THROW("error binding to ${endpoint}: ${what}", ("endpoint", local_endpoint)("what", except.what())); } }
void rand_pseudo_bytes(char* buf, int count) { static int init = init_openssl(); (void)init; // RAND_pseudo_bytes is deprecated in favor of RAND_bytes as of OpenSSL 1.1.0 #if OPENSSL_VERSION_NUMBER < 0x10100000L int result = RAND_pseudo_bytes((unsigned char*)buf, count); if (result == -1) FC_THROW("Error calling OpenSSL's RAND_pseudo_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); #else rand_bytes(buf, count); #endif }
bool public_encrypt( const char* key, uint32_t key_size, uint32_t pe, const fc::vector<char>& in, fc::vector<char>& out ) { RSA* pub = get_pub( key,key_size/8,pe); out.resize(RSA_size(pub)); int rtn = RSA_public_encrypt( in.size(), (unsigned char*)&in.front(), (unsigned char*)&out.front(), pub, RSA_PKCS1_OAEP_PADDING ); RSA_free(pub); if( rtn >= 0 ) { out.resize(rtn); return true; } out.resize(0); FC_THROW( fc::generic_exception( ERR_error_string( ERR_get_error(), NULL ) ) ); return false; }
virtual void handle_message(const message& message_to_process) override { // not a transaction, not a block FC_THROW( "Invalid Message Type" ); }
void gntp_notifier_impl::send_gntp_message(const std::string& message) { std::shared_ptr<boost::asio::ip::tcp::socket> sock(new boost::asio::ip::tcp::socket(asio::default_io_service())); bool connected = false; if (endpoint) { // we've successfully connected before, connect to the same endpoint that worked last time try { asio::tcp::connect(*sock, *endpoint); connected = true; } catch (exception& er) { ilog("Failed to connect to GNTP service using an endpoint that previously worked: ${error_report}", ("error_report", er.to_detail_string())); sock->close(); // clear the cached endpoint and fall through to the full connection procedure endpoint = optional<boost::asio::ip::tcp::endpoint>(); } catch (...) { ilog("Failed to connect to GNTP service using an endpoint that previously worked"); sock->close(); // clear the cached endpoint and fall through to the full connection procedure endpoint = optional<boost::asio::ip::tcp::endpoint>(); } } if (!connected) { // do the full connection procedure auto eps = asio::tcp::resolve(hostname, boost::lexical_cast<std::string>(port)); if (eps.size() == 0) FC_THROW("Unable to resolve host '${host}'", ("host", hostname)); for (uint32_t i = 0; i < eps.size(); ++i) { try { boost::system::error_code ec; ilog("Attempting to connect to GNTP srvice"); asio::tcp::connect(*sock, eps[i]); endpoint = eps[i]; connected = true; break; } catch (const exception& er) { ilog("Failed to connect to GNTP service: ${error_reprot}", ("error_report", er.to_detail_string()) ); sock->close(); } catch (...) { ilog("Failed to connect to GNTP service"); sock->close(); } } } if (!connected) FC_THROW("Unable to connect to any resolved endpoint for ${host}:${port}", ("host", hostname)("port", port)); try { asio::ostream<boost::asio::ip::tcp::socket> write_stream(sock); write_stream.write(message.c_str(), message.size()); write_stream.flush(); write_stream.close(); } catch (exception& er) { FC_RETHROW_EXCEPTION(er, warn, "Caught an exception while sending data to GNTP service"); } catch (...) { FC_THROW("Caught an exception while sending data to GNTP service"); } }
/** * This method dumps the state of the blockchain in a semi-human readable form for the * purpose of tracking down funds and mismatches in currency allocation */ void database::debug_dump() { const auto& db = *this; const asset_dynamic_data_object& core_asset_data = db.get_core_asset().dynamic_asset_data_id(db); const auto& balance_index = db.get_index_type<account_balance_index>().indices(); const auto& statistics_index = db.get_index_type<account_stats_index>().indices(); const auto& bids = db.get_index_type<collateral_bid_index>().indices(); const auto& settle_index = db.get_index_type<force_settlement_index>().indices(); map<asset_id_type,share_type> total_balances; map<asset_id_type,share_type> total_debts; share_type core_in_orders; share_type reported_core_in_orders; for( const account_balance_object& a : balance_index ) { // idump(("balance")(a)); total_balances[a.asset_type] += a.balance; } for( const force_settlement_object& s : settle_index ) { total_balances[s.balance.asset_id] += s.balance.amount; } for( const vesting_balance_object& vbo : db.get_index_type< vesting_balance_index >().indices() ) total_balances[ vbo.balance.asset_id ] += vbo.balance.amount; for( const fba_accumulator_object& fba : db.get_index_type< simple_index< fba_accumulator_object > >() ) total_balances[ asset_id_type() ] += fba.accumulated_fba_fees; for( const account_statistics_object& s : statistics_index ) { // idump(("statistics")(s)); reported_core_in_orders += s.total_core_in_orders; } for( const collateral_bid_object& b : bids ) total_balances[b.inv_swan_price.base.asset_id] += b.inv_swan_price.base.amount; for( const limit_order_object& o : db.get_index_type<limit_order_index>().indices() ) { // idump(("limit_order")(o)); auto for_sale = o.amount_for_sale(); if( for_sale.asset_id == asset_id_type() ) core_in_orders += for_sale.amount; total_balances[for_sale.asset_id] += for_sale.amount; } for( const call_order_object& o : db.get_index_type<call_order_index>().indices() ) { // idump(("call_order")(o)); auto col = o.get_collateral(); if( col.asset_id == asset_id_type() ) core_in_orders += col.amount; total_balances[col.asset_id] += col.amount; total_debts[o.get_debt().asset_id] += o.get_debt().amount; } for( const asset_object& asset_obj : db.get_index_type<asset_index>().indices() ) { total_balances[asset_obj.id] += asset_obj.dynamic_asset_data_id(db).accumulated_fees; total_balances[asset_id_type()] += asset_obj.dynamic_asset_data_id(db).fee_pool; // edump((total_balances[asset_obj.id])(asset_obj.dynamic_asset_data_id(db).current_supply ) ); } if( total_balances[asset_id_type()].value != core_asset_data.current_supply.value ) { FC_THROW( "computed balance of CORE mismatch", ("computed value",total_balances[asset_id_type()].value) ("current supply",core_asset_data.current_supply.value) ); } /* const auto& vbidx = db.get_index_type<simple_index<vesting_balance_object>>(); for( const auto& s : vbidx ) { // idump(("vesting_balance")(s)); } */ }
void api_generator::load_type_map(const fc::variants& json_type_map) { for (const fc::variant& json_type_variant : json_type_map) { fc::variant_object json_type = json_type_variant.get_object(); FC_ASSERT(json_type.contains("type_name"), "type map entry is missing a \"type_name\" attribute"); std::string json_type_name = json_type["type_name"].as_string(); type_mapping_ptr mapping; if (json_type.contains("cpp_return_type")) { std::string parameter_type; if (json_type.contains("cpp_parameter_type")) parameter_type = json_type["cpp_parameter_type"].as_string(); std::string return_type = json_type["cpp_return_type"].as_string(); fundamental_type_mapping_ptr fundamental_mapping = std::make_shared<fundamental_type_mapping>(json_type_name, parameter_type, return_type); mapping = fundamental_mapping; } else if (json_type.contains("container_type")) { std::string container_type_string = json_type["container_type"].as_string(); if (container_type_string == "array") { FC_ASSERT(json_type.contains("contained_type"), "arrays must specify a \"contained_type\""); std::string contained_type_name = json_type["contained_type"].as_string(); type_mapping_ptr contained_type = lookup_type_mapping(contained_type_name); sequence_type_mapping_ptr sequence_mapping = std::make_shared<sequence_type_mapping>(json_type_name, contained_type); mapping = sequence_mapping; } else if (container_type_string == "dictionary") { FC_ASSERT(json_type.contains("key_type"), "dictionaries must specify a \"key_type\""); FC_ASSERT(json_type.contains("value_type"), "dictionaries must specify a \"value_type\""); std::string key_type_name = json_type["key_type"].as_string(); std::string value_type_name = json_type["value_type"].as_string(); type_mapping_ptr key_type = lookup_type_mapping(key_type_name); type_mapping_ptr value_type = lookup_type_mapping(value_type_name); dictionary_type_mapping_ptr dictionary_mapping = std::make_shared<dictionary_type_mapping>(json_type_name, key_type, value_type); mapping = dictionary_mapping; } else FC_THROW("invalid container_type for type ${type}", ("type", json_type_name)); } else FC_THROW("malformed type map entry for type ${type}", ("type", json_type_name)); if (json_type.contains("to_variant_function")) mapping->set_to_variant_function(json_type["to_variant_function"].as_string()); if (json_type.contains("from_variant_function")) mapping->set_from_variant_function(json_type["from_variant_function"].as_string()); if (json_type.contains("obscure_in_log_files") && json_type["obscure_in_log_files"].as_bool()) mapping->set_obscure_in_log_files(); FC_ASSERT(_type_map.find(json_type_name) == _type_map.end(), "Error, type ${type_name} is already registered", ("type_name", json_type_name)); _type_map.insert(type_map_type::value_type(json_type_name, mapping)); if (json_type.contains("cpp_include_file")) _include_files.insert(json_type["cpp_include_file"].as_string()); } }