typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_vector_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type const& call(Args const&... args) { decayed_key_t new_keys(getKey(args)...); auto&& it(cache.find(new_keys)); if (it == cache.end()) // not cached { if (cache.size() == LRU_MAX) // cache is full, time to overwrite old objects { if (internal_it == internal_cache.end()) { assert(cache_it == cache.end()); internal_it = internal_cache.begin(); cache_it = cache.begin(); } *internal_it = fptr(args...); cache.erase(cache_it); cache.insert(cache_it, std::pair<key_t, internal_cache_it_t>(new_keys, internal_it)); ++cache_it; return *internal_it++; } else { // Maybe emplace_back internal_cache.push_back(fptr(args...)); cache[new_keys] = internal_cache.end(); return internal_cache.back(); } } else // was cached return *it->second; };
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return 0; new_keys(); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if (init_networking(ip, PORT) == -1) return 0; DHT_init(); LosslessUDP_init(); friendreq_init(); LANdiscovery_init(); set_nospam(random_int()); send_LANdiscovery(htons(PORT)); timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL); return m; }
void manage_keys(DHT *dht) { const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint8_t keys[KEYS_SIZE]; FILE *keys_file = fopen("key", "r"); if (keys_file != NULL) { /* If file was opened successfully -- load keys, otherwise save new keys */ size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); if (read_size != KEYS_SIZE) { printf("Error while reading the key file\nExiting.\n"); exit(1); } load_keys(dht->c, keys); printf("Keys loaded successfully.\n"); } else { new_keys(dht->c); save_keys(dht->c, keys); keys_file = fopen("key", "w"); if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { printf("Error while writing the key file.\nExiting.\n"); exit(1); } printf("Keys saved successfully.\n"); } fclose(keys_file); }
void Position::makeTowerKeys (const unsigned int tower_type) { #ifdef USE_POSITION_KEY std::vector<unsigned long long> new_keys(squares); for (unsigned int i = 0; i< squares; i++) { new_keys[i] = rk.rand<unsigned long long>(); } tower_keys[tower_type] = new_keys; #endif }
int main(int argc, char *argv[]) { new_keys(); printf("Public key: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } printf("\n"); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.i = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(bootstrap_info, bootstrap_key); free(bootstrap_key); } IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; int is_waiting_for_dht_connection = 1; while(1) { if (is_waiting_for_dht_connection && DHT_isconnected()) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { DHT_handlepacket(data, length, ip_port); } c_sleep(1); } shutdown_networking(); return 0; }
/* run this at startup */ int initMessenger() { new_keys(); m_set_userstatus((uint8_t*)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if(init_networking(ip,PORT) == -1) return -1; return 0; }
typename std::enable_if<(sizeof...(Args), std::is_same<cache_t, cache_map_t>::value) && tp == timeout_policy_t::NEVER, std::remove_const_t<std::decay_t<R>>>::type const& call(Args const&... args) { decayed_key_t new_keys(getKey(args)...); auto&& it(cache.find(new_keys)); if (it == cache.end()) //not cached { auto&& pos(cache.insert(std::pair<key_t, R>(std::move(new_keys), fptr(args...)))); return pos.first->second; } else //was cached return it->second; };
/* run this at startup */ int initMessenger(void) { new_keys(); m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if(init_networking(ip,PORT) == -1) return -1; DHT_init(); LosslessUDP_init(); friendreq_init(); LANdiscovery_init(); return 0; }
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
int manage_keys(DHT *dht, char *keys_file_path) { const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint8_t keys[KEYS_SIZE]; FILE *keys_file; // Check if file exits, proceed to open and load keys keys_file = fopen(keys_file_path, "r"); if (keys_file != NULL) { size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); if (read_size != KEYS_SIZE) { return 0; } load_keys(dht->c, keys); } else { // Otherwise save new keys new_keys(dht->c); save_keys(dht->c, keys); keys_file = fopen(keys_file_path, "w"); size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); if (write_size != KEYS_SIZE) { return 0; } } fclose(keys_file); // We want our DHT public key to be the same as our internal one since this is a bootstrap server memcpy(dht->self_public_key, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); memcpy(dht->self_secret_key, dht->c->self_secret_key, crypto_box_SECRETKEYBYTES); return 1; }
/* Run this to (re)initialize net_crypto. * Sets all the global connection variables to their default values. */ Net_Crypto *new_net_crypto(Networking_Core *net) { unix_time_update(); if (net == NULL) return NULL; Net_Crypto *temp = calloc(1, sizeof(Net_Crypto)); if (temp == NULL) return NULL; temp->lossless_udp = new_lossless_udp(net); if (temp->lossless_udp == NULL) { free(temp); return NULL; } new_keys(temp); return temp; }
void manage_keys(DHT *dht, char *keys_file) { const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint8_t keys[KEYS_SIZE]; struct stat existence; FILE *keysf; /* Check if file exits, proceed to open and load keys */ if (stat(keys_file, &existence) >= 0) { keysf = fopen(keys_file, "r"); size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf); if (read_size != KEYS_SIZE) { printf("Error while reading the key file\nExiting.\n"); exit(1); } else { printf("Keys loaded successfully\n"); } load_keys(dht->c, keys); } else { /* otherwise save new keys */ /* Silly work-around to ignore any errors coming from new_keys() */ new_keys(dht->c); save_keys(dht->c, keys); keysf = fopen(keys_file, "w"); if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) { printf("Error while writing the key file.\nExiting.\n"); exit(1); } else { printf("Keys saved successfully\n"); } } fclose(keysf); }
int main(int argc, char *argv[]) { //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port public_key\n", argv[0]); exit(0); } new_keys(); printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } char temp_id[128]; printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); if(scanf("%s", temp_id) != 1) exit(0); DHT_addfriend(hex_string_to_bin(temp_id)); /* initialize networking */ /* bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); /* bootstrap_ip_port.ip.c[0] = 127; * bootstrap_ip_port.ip.c[1] = 0; * bootstrap_ip_port.ip.c[2] = 0; * bootstrap_ip_port.ip.c[3] = 1; */ bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while(1) { doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } shutdown_networking(); return 0; }
int main(int argc, char *argv[]) { if (argc < 4) { printf("usage %s ip port filename(of file to send)\n", argv[0]); exit(0); } new_keys(); printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } printf("\n"); memcpy(self_client_id, self_public_key, 32); char temp_id[128]; printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n"); scanf("%s", temp_id); uint8_t friend_id[32]; memcpy(friend_id, hex_string_to_bin(temp_id), 32); /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ DHT_addfriend(friend_id); IP_Port friend_ip; int connection = -1; int inconnection = -1; uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES]; int friendrequest = -1; uint8_t request_data[512]; /* initialize networking * bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); initNetCrypto(); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; uint8_t buffer1[128]; int read1 = 0; uint8_t buffer2[128]; int read2 = 0; FILE *file1 = fopen(argv[3], "rb"); if ( file1==NULL ){printf("Error opening file.\n");return 1;} FILE *file2 = fopen("received.txt", "wb"); if ( file2==NULL ){return 1;} read1 = fread(buffer1, 1, 128, file1); while(1) { while(receivepacket(&ip_port, data, &length) != -1) { if(rand() % 3 != 1) { /* simulate packet loss */ if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { /* if packet is not recognized */ printf("Received unhandled packet with length: %u\n", length); } else { printf("Received handled packet with length: %u\n", length); } } } friend_ip = DHT_getfriendip(friend_id); if(friend_ip.ip.i != 0) { if(connection == -1 && friendrequest == -1) { printf("Sending friend request to peer:"); printip(friend_ip); friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ /* connection = new_connection(friend_ip); */ } if(check_friendrequest(friendrequest) == 1) { printf("Started connecting to friend:"); connection = crypto_connect(friend_id, friend_ip); } } if(inconnection == -1) { uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES]; inconnection = crypto_inbound(public_key, secret_nonce, session_key); inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); /* inconnection = incoming_connection(); */ if(inconnection != -1) { printf("Someone connected to us:\n"); /* printip(connection_ip(inconnection)); */ } } if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { printf("RECIEVED FRIEND REQUEST: %s\n", request_data); } /* if someone connected to us write what he sends to a file * also send him our file. */ if(inconnection != -1) { if(write_cryptpacket(inconnection, buffer1, read1)) { printf("Wrote data1.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(inconnection, buffer2); if(read2 != 0) { printf("Received data1.\n"); if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error1\n"); } if(read2 < 128) { printf("Closed file1 %u\n", read2); fclose(file2); } } /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(inconnection) == 4) { crypto_kill(inconnection); } } /* if we are connected to a friend send him data from the file. * also put what he sends us in a file. */ if(is_cryptoconnected(connection) >= 3) { if(write_cryptpacket(0, buffer1, read1)) { printf("Wrote data2.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(0, buffer2); if(read2 != 0) { printf("Received data2.\n"); if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error2\n"); } if(read2 < 128) { printf("Closed file2 %u\n", read2); fclose(file2); } } /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(connection) == 4) { crypto_kill(connection); } } doDHT(); doLossless_UDP(); doNetCrypto(); /*print_clientlist(); *print_friendlist(); *c_sleep(300); */ c_sleep(1); } shutdown_networking(); return 0; }
void test_map (void) { std::cerr << "************* MAP: SETTING KEY:VALUE PAIRS*************\n"; knowledge::KnowledgeBase knowledge; std::vector <std::string> keys; containers::Map map ("test_map", knowledge); map.set ("name", "Rob Roy"); map.set ("occupation", "Lord of the MacGregors"); map.set ("age", madara::knowledge::KnowledgeRecord::Integer (63)); map.set ("wives", madara::knowledge::KnowledgeRecord::Integer (1)); size_t size = map.size (); map.keys (keys); std::cerr << "\nMap results:\n"; for (std::vector <std::string>::iterator i = keys.begin (); i != keys.end (); ++i) std::cerr << " " << *i << "=" << map[*i] << "\n"; std::cerr << "\n"; if (size != 4) std::cerr << "FAIL. Map.size != 4\n"; else std::cerr << "SUCCESS. Map.size == 4\n"; if (keys.size () != 4) std::cerr << "FAIL. Map.keys ().size != 4\n"; else std::cerr << "SUCCESS. Map.keys ().size == 4\n"; if (map["name"].to_string () == "Rob Roy" && map["occupation"].to_string () == "Lord of the MacGregors" && map["age"].to_integer () == 63 && map["wives"].to_integer () == 1) std::cerr << "SUCCESS. map values were set and retrieved.\n"; else std::cerr << "FAIL. map values were not set and retrieved.\n"; std::cerr << "************* MAP: COPYING MAP FROM KB*************\n"; containers::Map copy ("test_map", knowledge); size = map.size (); map.keys (keys); if (size != 4) std::cerr << "FAIL. Map Copy.size != 4\n"; else std::cerr << "SUCCESS. Map Copy.size == 4\n"; if (keys.size () != 4) std::cerr << "FAIL. Map Copy.keys ().size != 4\n"; else std::cerr << "SUCCESS. Map Copy.keys ().size == 4\n"; if (copy["name"].to_string () == "Rob Roy" && copy["occupation"].to_string () == "Lord of the MacGregors" && copy["age"].to_integer () == 63 && copy["wives"].to_integer () == 1) std::cerr << "SUCCESS. map copy values were set and retrieved.\n"; else std::cerr << "FAIL. map copy values were not set and retrieved.\n"; std::cerr << "************* MAP: SYNCING KEYS*************\n"; knowledge.set ("test_map.wife", "Mary Helen MacGregor"); std::vector <std::string> new_keys (map.sync_keys ()); if (map.exists ("wife") && !copy.exists ("wife") && new_keys.size () == 1 && new_keys[0] == "wife") std::cerr << "SUCCESS. map.sync_keys () worked.\n"; else { std::cerr << "FAIL. map.sync_keys () did not work.\n"; std::cerr << " new_keys[0] = " << new_keys[0] << ", should be 'wife'\n"; std::cerr << " new_keys.size() = " << new_keys.size () << ", should be 1\n"; } if (map.get_name () == "test_map" && copy.get_name () == "test_map") std::cerr << "SUCCESS. map.name () returned test_map.\n"; else std::cerr << "FAIL. map.name () did not return test_map.\n"; knowledge.print (); // test deleting map entries std::cerr << "************* MAP: CLEARING KEYS*************\n"; knowledge::VariableReferences before_delete = knowledge.save_modifieds (); map.clear (true); knowledge::VariableReferences after_delete = knowledge.save_modifieds (); int size_diff = int (before_delete.size () - after_delete.size ()); if (before_delete.size () > after_delete.size ()) { std::cerr << "SUCCESS. map.clear () removed " << size_diff << " elements from modifieds list.\n"; } else { std::cerr << "FAIL. map.clear () has a non-positive size difference of " << size_diff << ".\n"; } }