Fixed to be confilcts with the DHT and the messenger part.

This commit is contained in:
irungentoo 2013-07-07 11:54:25 -04:00
parent 2528ec148c
commit a632d960f8
6 changed files with 83 additions and 78 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6.0)
project(TOX C) project(TOX C)
set(core_sources set(core_sources
core/DHT.c #core/DHT.c
core/network.c) core/network.c)
set(test_sources set(test_sources

View File

@ -27,8 +27,65 @@
#include "DHT.h" #include "DHT.h"
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
uint32_t timestamp;
uint32_t last_pinged;
}Client_data;
//maximum number of clients stored per friend.
#define MAX_FRIEND_CLIENTS 8
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS];
}Friend;
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
}Node_format;
typedef struct
{
IP_Port ip_port;
uint32_t ping_id;
uint32_t timestamp;
}Pinged;
uint8_t self_client_id[CLIENT_ID_SIZE]; uint8_t self_client_id[CLIENT_ID_SIZE];
//TODO: Move these out of here and put them into the .c file.
//A list of the clients mathematically closest to ours.
#define LCLIENT_LIST 32
Client_data close_clientlist[LCLIENT_LIST];
//Hard maximum number of friends
#define MAX_FRIENDS 256
//Let's start with a static array for testing.
Friend friends_list[MAX_FRIENDS];
uint16_t num_friends;
//The list of ip ports along with the ping_id of what we sent them and a timestamp
#define LPING_ARRAY 128
Pinged pings[LPING_ARRAY];
#define LSEND_NODES_ARRAY LPING_ARRAY/2
Pinged send_nodes[LSEND_NODES_ARRAY];
//Compares client_id1 and client_id2 with client_id //Compares client_id1 and client_id2 with client_id
//return 0 if both are same distance //return 0 if both are same distance
//return 1 if client_id1 is closer. //return 1 if client_id1 is closer.
@ -593,7 +650,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)//tested
int addfriend(uint8_t * client_id) int DHT_addfriend(uint8_t * client_id)
{ {
//TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS
if(MAX_FRIENDS > num_friends) if(MAX_FRIENDS > num_friends)
@ -610,7 +667,7 @@ int addfriend(uint8_t * client_id)
int delfriend(uint8_t * client_id) int DHT_delfriend(uint8_t * client_id)
{ {
uint32_t i; uint32_t i;
for(i = 0; i < num_friends; i++) for(i = 0; i < num_friends; i++)
@ -630,7 +687,7 @@ int delfriend(uint8_t * client_id)
//TODO: Optimize this. //TODO: Optimize this.
IP_Port getfriendip(uint8_t * client_id) IP_Port DHT_getfriendip(uint8_t * client_id)
{ {
uint32_t i, j; uint32_t i, j;
IP_Port empty = {{{0}}, 0}; IP_Port empty = {{{0}}, 0};
@ -785,7 +842,7 @@ void doDHT()
void bootstrap(IP_Port ip_port) void DHT_bootstrap(IP_Port ip_port)
{ {
getnodes(ip_port, self_client_id); getnodes(ip_port, self_client_id);

View File

@ -34,49 +34,19 @@
//size of the client_id in bytes //size of the client_id in bytes
#define CLIENT_ID_SIZE 32 #define CLIENT_ID_SIZE 32
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
uint32_t timestamp;
uint32_t last_pinged;
}Client_data;
//maximum number of clients stored per friend.
#define MAX_FRIEND_CLIENTS 8
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS];
}Friend;
typedef struct
{
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
}Node_format;
typedef struct
{
IP_Port ip_port;
uint32_t ping_id;
uint32_t timestamp;
}Pinged;
//Add a new friend to the friends list //Add a new friend to the friends list
//client_id must be CLIENT_ID_SIZE bytes long. //client_id must be CLIENT_ID_SIZE bytes long.
//returns 0 if success //returns 0 if success
//returns 1 if failure (friends list is full) //returns 1 if failure (friends list is full)
int addfriend(uint8_t * client_id); int DHT_addfriend(uint8_t * client_id);
//Delete a friend from the friends list //Delete a friend from the friends list
//client_id must be CLIENT_ID_SIZE bytes long. //client_id must be CLIENT_ID_SIZE bytes long.
//returns 0 if success //returns 0 if success
//returns 1 if failure (client_id not in friends list) //returns 1 if failure (client_id not in friends list)
int delfriend(uint8_t * client_id); int DHT_delfriend(uint8_t * client_id);
//Get ip of friend //Get ip of friend
@ -86,7 +56,7 @@ int delfriend(uint8_t * client_id);
//returns ip if success //returns ip if success
//returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) //returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
//returns ip of 1 if friend is not in list. //returns ip of 1 if friend is not in list.
IP_Port getfriendip(uint8_t * client_id); IP_Port DHT_getfriendip(uint8_t * client_id);
//Run this function at least a couple times per second (It's the main loop) //Run this function at least a couple times per second (It's the main loop)
@ -99,7 +69,7 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
//Use this function to bootstrap the client //Use this function to bootstrap the client
//Sends a get nodes request to the given ip port //Sends a get nodes request to the given ip port
void bootstrap(IP_Port ip_port); void DHT_bootstrap(IP_Port ip_port);
//TODO: //TODO:
@ -112,27 +82,5 @@ void bootstrap(IP_Port ip_port);
extern uint8_t self_client_id[CLIENT_ID_SIZE]; extern uint8_t self_client_id[CLIENT_ID_SIZE];
//TODO: Move these out of here and put them into the .c file.
//A list of the clients mathematically closest to ours.
#define LCLIENT_LIST 32
Client_data close_clientlist[LCLIENT_LIST];
//Hard maximum number of friends
#define MAX_FRIENDS 256
//Let's start with a static array for testing.
Friend friends_list[MAX_FRIENDS];
uint16_t num_friends;
//The list of ip ports along with the ping_id of what we sent them and a timestamp
#define LPING_ARRAY 128
Pinged pings[LPING_ARRAY];
#define LSEND_NODES_ARRAY LPING_ARRAY/2
Pinged send_nodes[LSEND_NODES_ARRAY];
#endif #endif

View File

@ -90,7 +90,7 @@ int main(int argc, char *argv[])
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
addfriend(friend_id); DHT_addfriend(friend_id);
IP_Port friend_ip; IP_Port friend_ip;
int connection = -1; int connection = -1;
int inconnection = -1; int inconnection = -1;
@ -112,7 +112,7 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
bootstrap(bootstrap_ip_port); DHT_bootstrap(bootstrap_ip_port);
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
@ -146,21 +146,21 @@ int main(int argc, char *argv[])
} }
} }
} }
friend_ip = getfriendip((uint8_t *)friend_id); friend_ip = DHT_getfriendip(friend_id);
if(friend_ip.ip.i != 0) if(friend_ip.ip.i != 0)
{ {
if(connection == -1 && friendrequest == -1) if(connection == -1 && friendrequest == -1)
{ {
printf("Sending friend request to peer:"); printf("Sending friend request to peer:");
printip(friend_ip); printip(friend_ip);
friendrequest = send_friendrequest(friend_id, friend_ip, "Hello World", 12); friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
//connection = crypto_connect((uint8_t *)friend_id, friend_ip); //connection = crypto_connect((uint8_t *)friend_id, friend_ip);
//connection = new_connection(friend_ip); //connection = new_connection(friend_ip);
} }
if(check_friendrequest(friendrequest) == 1) if(check_friendrequest(friendrequest) == 1)
{ {
printf("Started connecting to friend:"); printf("Started connecting to friend:");
connection = crypto_connect((uint8_t *)friend_id, friend_ip); connection = crypto_connect(friend_id, friend_ip);
} }
} }
if(inconnection == -1) if(inconnection == -1)

View File

@ -48,7 +48,7 @@ int main(int argc, char *argv[])
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]); 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); exit(0);
} }
addfriend(argv[3]); DHT_addfriend((uint8_t *)argv[3]);
IP_Port friend_ip; IP_Port friend_ip;
int connection = -1; int connection = -1;
int inconnection = -1; int inconnection = -1;
@ -66,15 +66,15 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
bootstrap(bootstrap_ip_port); DHT_bootstrap(bootstrap_ip_port);
IP_Port ip_port; IP_Port ip_port;
char data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
char buffer1[128]; uint8_t buffer1[128];
int read1 = 0; int read1 = 0;
char buffer2[128]; uint8_t buffer2[128];
int read2 = 0; int read2 = 0;
FILE *file1 = fopen(argv[4], "rb"); FILE *file1 = fopen(argv[4], "rb");
if ( file1==NULL ){printf("Error opening file.\n");return 1;} if ( file1==NULL ){printf("Error opening file.\n");return 1;}
@ -100,7 +100,7 @@ int main(int argc, char *argv[])
} }
} }
} }
friend_ip = getfriendip(argv[3]); friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
if(friend_ip.ip.i != 0) if(friend_ip.ip.i != 0)
{ {
if(connection == -1) if(connection == -1)

View File

@ -1,13 +1,13 @@
/* DHT test /* DHT test
* A file with a main that runs our DHT for testing. * A file with a main that runs our DHT for testing.
* *
* Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c DHT_test.c * Compile with: gcc -O2 -Wall -o test ../core/network.c DHT_test.c
* *
* Command line arguments are the ip and port of a node and the client_id (32 bytes) of the friend you want to find the ip_port of * Command line arguments are the ip and port of a node and the client_id (32 bytes) of the friend you want to find the ip_port of
* EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef
*/ */
#include "../core/network.h" #include "../core/network.h"
#include "../core/DHT.h" #include "../core/DHT.c"
#include <string.h> #include <string.h>
@ -58,7 +58,7 @@ void print_friendlist()
{ {
printf("%c", friends_list[k].client_id[j]); printf("%c", friends_list[k].client_id[j]);
} }
p_ip = getfriendip(friends_list[k].client_id); p_ip = DHT_getfriendip(friends_list[k].client_id);
printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
printf("\nCLIENTS IN LIST:\n\n"); printf("\nCLIENTS IN LIST:\n\n");
@ -102,7 +102,7 @@ int main(int argc, char *argv[])
printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]);
exit(0); exit(0);
} }
addfriend((uint8_t *)argv[3]); DHT_addfriend((uint8_t *)argv[3]);
//initialize networking //initialize networking
//bind to ip 0.0.0.0:PORT //bind to ip 0.0.0.0:PORT
@ -122,7 +122,7 @@ int main(int argc, char *argv[])
//bootstrap_ip_port.ip.c[2] = 0; //bootstrap_ip_port.ip.c[2] = 0;
//bootstrap_ip_port.ip.c[3] = 1; //bootstrap_ip_port.ip.c[3] = 1;
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
bootstrap(bootstrap_ip_port); DHT_bootstrap(bootstrap_ip_port);
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];