From 2e0c3bb5934a8db2be6a07e8f1f0511a36bfd86e Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 30 Jun 2013 17:19:15 -0400 Subject: [PATCH] Added a new test program. Fixed some stuff in Lossless UDP. --- README.md | 9 +- core/Lossless_UDP.c | 21 ++-- testing/DHT_sendfiletest.c | 178 ++++++++++++++++++++++++++++++ testing/Lossless_UDP_testserver.c | 1 - 4 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 testing/DHT_sendfiletest.c diff --git a/README.md b/README.md index 76f634de..d2dbb644 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,10 @@ Your client stores the id of the peers along with their public keys used to init ## Roadmap: 1. Get our DHT working perfectly.(Done, needs large scale testing though.) -2. Reliable connection (See Lossless_UDP protocol) to other peers according to client id. -3. Encrypted message sending with RSA (NOTE: We have not decided on the encryption yet. This was just a quick guess.) -4. Encrypted message sending with AES (encryption done) -5. Reliable encrypted sending of data larger than the maximum packet size. -... +2. Reliable connection (See Lossless_UDP protocol) to other peers according to client id. (Done, see DHT_sendfiletest.c for an example) +3. Encryption. (this is where we are now) +4. Optimize for streaming media. +5. ## TODO: diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index b92e6806..482d88b1 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -28,22 +28,22 @@ //maximum data packets in sent and recieve queues. -#define MAX_QUEUE_NUM 32 +#define MAX_QUEUE_NUM 16 //maximum length of the data in the data packets #define MAX_DATA_SIZE 1024 //maximum number of data packets in the buffer -#define BUFFER_PACKET_NUM MAX_QUEUE_NUM +#define BUFFER_PACKET_NUM (16-1) //Lossless UDP connection timeout. #define CONNEXION_TIMEOUT 10 //initial amount of sync/hanshake packets to send per second. -#define SYNC_RATE 5 +#define SYNC_RATE 50 //initial send rate of sync packets when data is being sent/recieved. -#define DATA_SYNC_RATE 20 +#define DATA_SYNC_RATE 200 typedef struct { @@ -242,7 +242,7 @@ int is_connected(int connection_id) //returns the ip_port of the corresponding connection. IP_Port connection_ip(int connection_id) { - if(connection_id > 0 && connection_id < MAX_CONNECTIONS) + if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) { return connections[connection_id].ip_port; } @@ -290,7 +290,7 @@ int write_packet(int connection_id, char * data, uint32_t length) { return 0; } - if(sendqueue(connection_id) < MAX_QUEUE_NUM) + if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; memcpy(connections[connection_id].sendbuffer[index].data, data, length); @@ -309,6 +309,11 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) { uint32_t number = 0; uint32_t i; + + if(recvqueue(connection_id) >= BUFFER_PACKET_NUM)//don't request packets if the buffer is full. + { + return 0; + } for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) { if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) @@ -500,9 +505,11 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui uint16_t number) { uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); + //uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); + //uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); - if(comp_1 < BUFFER_PACKET_NUM && comp_2 < BUFFER_PACKET_NUM && comp_counter < 10) //packet valid + if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) //packet valid { connections[connection_id].orecv_packetnum = recv_packetnum; connections[connection_id].osent_packetnum = sent_packetnum; diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c new file mode 100644 index 00000000..fd4fd29f --- /dev/null +++ b/testing/DHT_sendfiletest.c @@ -0,0 +1,178 @@ +/* DHT sendfiletest + * + * Sends the data from a file to another client. + * Receives the file data that that client sends us. + * + * NOTE: this program simulates 33% packet loss. + * + * Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c + * + * Command line arguments are the ip and port of a node (for bootstrapping), the + * client_id (32 bytes) of the friend you want to send the data in filename to and + * the client_id this node will take. + * + * Saves all received data to: received.txt + * + * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg + */ +#include "../core/network.h" +#include "../core/DHT.h" +#include "../core/Lossless_UDP.h" + +#include + +//Sleep function (x = milliseconds) +#ifdef WIN32 + +#define c_sleep(x) Sleep(1*x) + +#else +#include +#include +#define c_sleep(x) usleep(1000*x) + +#endif + +#define PORT 33445 + +void printip(IP_Port ip_port) +{ + printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); +} + +int main(int argc, char *argv[]) +{ + //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); + + if (argc < 6) { + printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]); + exit(0); + } + addfriend(argv[3]); + IP_Port friend_ip; + int connection = -1; + int inconnection = -1; + + //initialize networking + //bind to ip 0.0.0.0:PORT + IP ip; + ip.i = 0; + init_networking(ip, PORT); + + memcpy(self_client_id, argv[5], 32); + + + perror("Initialization"); + IP_Port bootstrap_ip_port; + bootstrap_ip_port.port = htons(atoi(argv[2])); + bootstrap_ip_port.ip.i = inet_addr(argv[1]); + bootstrap(bootstrap_ip_port); + + IP_Port ip_port; + char data[MAX_UDP_PACKET_SIZE]; + uint32_t length; + + char buffer1[128]; + int read1 = 0; + char buffer2[128]; + int read2 = 0; + FILE *file1 = fopen(argv[4], "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(recievepacket(&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 = getfriendip(argv[3]); + if(friend_ip.ip.i != 0) + { + if(connection == -1) + { + printf("Started connecting to friend:"); + printip(friend_ip); + connection = new_connection(friend_ip); + } + } + if(inconnection == -1) + { + inconnection = incoming_connection(); + if(inconnection != -1) + { + printf("Someone connected to us:"); + printip(connection_ip(inconnection)); + } + } + //if someone connected to us write what he sends to a file + //also send him our file. + if(inconnection != -1) + { + if(write_packet(inconnection, buffer1, read1)) + { + printf("Wrote data.\n"); + read1 = fread(buffer1, 1, 128, file1); + } + read2 = read_packet(inconnection, buffer2); + if(read2 != 0) + { + printf("Received data.\n"); + if(!fwrite(buffer2, read2, 1, file2)) + { + printf("file write error\n"); + } + if(read2 < 128) + { + fclose(file2); + } + } + } + //if we are connected to a friend send him data from the file. + //also put what he sends us in a file. + if(is_connected(connection) == 3) + { + if(write_packet(0, buffer1, read1)) + { + printf("Wrote data.\n"); + read1 = fread(buffer1, 1, 128, file1); + } + read2 = read_packet(0, buffer2); + if(read2 != 0) + { + printf("Received data.\n"); + if(!fwrite(buffer2, read2, 1, file2)) + { + printf("file write error\n"); + } + if(read2 < 128) + { + fclose(file2); + } + } + } + doDHT(); + doLossless_UDP(); + //print_clientlist(); + //print_friendlist(); + //c_sleep(300); + c_sleep(1); + } + + shutdown_networking(); + return 0; +} \ No newline at end of file diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 00e537ac..8357c2ef 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -174,7 +174,6 @@ int main(int argc, char *argv[]) if(is_connected(connection) >= 2) { read = read_packet(connection, buffer); - if(read != 0) { // printf("Recieved data.\n");