void c_peering_udp::send_data_udp(const char * data, size_t data_size, int udp_socket, c_haship_addr src_hip, c_haship_addr dst_hip, int ttl, antinet_crypto::t_crypto_nonce nonce_used) { _info("Send to peer (tunneled data) data: " << string_as_dbg(data,data_size).get() ); // TODO .get trivialserialize::generator gen(data_size + 50); gen.push_byte_u( c_protocol::current_version ); gen.push_byte_u( c_protocol::e_proto_cmd_tunneled_data ); gen.push_bytes_n( g_ipv6_rfc::length_of_addr , to_binary_string(src_hip) ); gen.push_bytes_n( g_ipv6_rfc::length_of_addr , to_binary_string(dst_hip) ); gen.push_byte_u( ttl ); gen.push_bytes_n( crypto_box_NONCEBYTES , nonce_used.get().to_binary() ); // TODO avoid conversion/copy gen.push_varstring( std::string(data, data+data_size) ); // TODO view_string /* // TODONOW turn off this crypto (unless leave here for peer-to-peer auth only) static unsigned char generated_shared_key[crypto_generichash_BYTES] = {43, 124, 179, 100, 186, 41, 101, 94, 81, 131, 17, 198, 11, 53, 71, 210, 232, 187, 135, 116, 6, 195, 175, 233, 194, 218, 13, 180, 63, 64, 3, 11}; static unsigned char nonce[crypto_aead_chacha20poly1305_NPUBBYTES] = {148, 231, 240, 47, 172, 96, 246, 79}; static unsigned char additional_data[] = {1, 2, 3}; static unsigned long long additional_data_len = 3; // TODO // TODO randomize this data XXX use real crypto data const int header_size = c_protocol::version_size + c_protocol::cmd_size + c_protocol::ttl_size ; // [protocol] TODO pack this into protocol class --r // TODO allocate buffer outside std::unique_ptr<unsigned char[]> protomsg(new unsigned char[data_size + crypto_aead_chacha20poly1305_ABYTES + header_size ]); // plus the headers // long long? or u64? --r assert size sizeof(long long .... == ... // why? --r: assert(crypto_aead_chacha20poly1305_KEYBYTES <= crypto_generichash_BYTES); // write headers: protomsg.get()[0] = c_protocol::current_version; protomsg.get()[1] = c_protocol::e_proto_cmd_tunneled_data; // this are tunneled data assert( (ttl >= 0) && (ttl <= c_protocol::ttl_max_value_ever) ); protomsg.get()[2] = ttl; // ...this is their route ttl unsigned char * ciphertext_buf = protomsg.get() + header_size; // just-pointer to part of protomsg where to write the message! unsigned long long ciphertext_buf_len = 0; // encryption will write here the resulting size crypto_aead_chacha20poly1305_encrypt(ciphertext_buf, &ciphertext_buf_len, reinterpret_cast<const unsigned char *>(data), data_size, additional_data, additional_data_len, NULL, nonce, generated_shared_key); unsigned long long protomsg_len = ciphertext_buf_len + header_size; // the output of crypto, plus the header in front // TODO asserts!!! */ string protomsg = gen.str(); // TODO view_string this->send_data_RAW_udp(protomsg.c_str(), protomsg.size(), udp_socket); }
int main() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, 20); int ival = dis(gen); std::cout << ival << " -> " << to_binary_string(ival) << std::endl; }
/// Convert the content of a buffer to a binary string in the form: /// \verbatim <<I1, I2, ..., In>> \endverbatim where <tt>Ik</tt> is /// unsigned integer less than 256. inline std::string to_binary_string(const char* a, size_t sz) { std::stringstream oss; to_binary_string(oss, a, sz); return oss.str(); }