/* the main loop that needs to be run at least 200 times per second. */ void doMessenger() { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while (receivepacket(&ip_port, data, &length) != -1) { #ifdef DEBUG /* if(rand() % 3 != 1) //simulate packet loss */ /* { */ if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) /* if packet is discarded */ printf("Received unhandled packet with length: %u\n", length); else printf("Received handled packet with length: %u\n", length); /* } */ printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); #else DHT_handlepacket(data, length, ip_port); LosslessUDP_handlepacket(data, length, ip_port); friendreq_handlepacket(data, length, ip_port); LANdiscovery_handlepacket(data, length, ip_port); #endif } doDHT(); doLossless_UDP(); doNetCrypto(); doInbound(); doFriends(); LANdiscovery(); }
int main(int argc, char *argv[]) { manage_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"); printf("Port: %u\n", PORT); //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); friendreq_handlepacket(data, length, ip_port); } c_sleep(1); } shutdown_networking(); return 0; }
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; }