Merge branch 'master' of https://github.com/sometwo/ProjectTox-Core into sometwo-master

Conflicts:
	core/Messenger.c
This commit is contained in:
irungentoo 2013-07-20 07:55:21 -04:00
commit 11e94066f7
6 changed files with 403 additions and 413 deletions

View File

@ -35,13 +35,13 @@ typedef struct
uint32_t timestamp; uint32_t timestamp;
uint32_t last_pinged; uint32_t last_pinged;
}Client_data; }Client_data;
//maximum number of clients stored per friend. /* maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8 #define MAX_FRIEND_CLIENTS 8
typedef struct typedef struct
{ {
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS]; Client_data client_list[MAX_FRIEND_CLIENTS];
uint32_t lastgetnode;//time at which the last get_nodes request was sent. uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
}Friend; }Friend;
@ -60,12 +60,12 @@ typedef struct
}Pinged; }Pinged;
//Our client id/public key /* Our client id/public key */
uint8_t self_public_key[CLIENT_ID_SIZE]; uint8_t self_public_key[CLIENT_ID_SIZE];
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
//TODO: Move these out of here and put them into the .c file. /* TODO: Move these out of here and put them into the .c file.
//A list of the clients mathematically closest to ours. A list of the clients mathematically closest to ours. */
#define LCLIENT_LIST 32 #define LCLIENT_LIST 32
static Client_data close_clientlist[LCLIENT_LIST]; static Client_data close_clientlist[LCLIENT_LIST];
@ -74,7 +74,7 @@ static Client_data close_clientlist[LCLIENT_LIST];
static Friend * friends_list; static Friend * friends_list;
static uint16_t num_friends; static uint16_t num_friends;
//The list of ip ports along with the ping_id of what we sent them and a timestamp /* The list of ip ports along with the ping_id of what we sent them and a timestamp */
#define LPING_ARRAY 128 #define LPING_ARRAY 128
static Pinged pings[LPING_ARRAY]; static Pinged pings[LPING_ARRAY];
@ -84,11 +84,11 @@ static Pinged pings[LPING_ARRAY];
static Pinged send_nodes[LSEND_NODES_ARRAY]; static 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
//return 2 if client_id2 is closer. return 2 if client_id2 is closer */
int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */
{ {
uint32_t i; uint32_t i;
for(i = 0; i < CLIENT_ID_SIZE; ++i) for(i = 0; i < CLIENT_ID_SIZE; ++i)
@ -108,11 +108,11 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)/
} }
//check if client with client_id is already in list of length length. /* check if client with client_id is already in list of length length.
//if it is set it's corresponding timestamp to current time. if it is set it's corresponding timestamp to current time.
//if the id is already in the list with a different ip_port, update it. if the id is already in the list with a different ip_port, update it.
//return True(1) or False(0) return True(1) or False(0)
//TODO: maybe optimize this. TODO: maybe optimize this. */
int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
{ {
uint32_t i; uint32_t i;
@ -122,7 +122,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
{ {
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
{ {
//Refresh the client timestamp. /* Refresh the client timestamp. */
list[i].timestamp = temp_time; list[i].timestamp = temp_time;
list[i].ip_port.ip.i = ip_port.ip.i; list[i].ip_port.ip.i = ip_port.ip.i;
list[i].ip_port.port = ip_port.port; list[i].ip_port.port = ip_port.port;
@ -133,8 +133,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
} }
//check if client with client_id is already in node format list of length length. /* check if client with client_id is already in node format list of length length.
//return True(1) or False(0) return True(1) or False(0) */
int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
{ {
uint32_t i; uint32_t i;
@ -152,15 +152,15 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
//the number of seconds for a non responsive node to become bad. /* the number of seconds for a non responsive node to become bad. */
#define BAD_NODE_TIMEOUT 130 #define BAD_NODE_TIMEOUT 130
//The max number of nodes to send with send nodes. /* the max number of nodes to send with send nodes. */
#define MAX_SENT_NODES 8 #define MAX_SENT_NODES 8
//Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
//put them in the nodes_list and return how many were found. put them in the nodes_list and return how many were found.
//TODO: Make this function much more efficient. TODO: Make this function much more efficient. */
int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
{ {
uint32_t i, j, k; uint32_t i, j, k;
@ -170,7 +170,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
{ {
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
!client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id))
//if node is good and not already in list. /* if node is good and not already in list. */
{ {
if(num_nodes < MAX_SENT_NODES) if(num_nodes < MAX_SENT_NODES)
{ {
@ -196,7 +196,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
{ {
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
!client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id))
//if node is good and not already in list. /* if node is good and not already in list. */
{ {
if(num_nodes < MAX_SENT_NODES) if(num_nodes < MAX_SENT_NODES)
{ {
@ -223,16 +223,16 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
//replace first bad (or empty) node with this one /* replace first bad (or empty) node with this one
//return 0 if successful return 0 if successful
//return 1 if not (list contains no bad nodes) return 1 if not (list contains no bad nodes) */
int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)//tested int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */
{ {
uint32_t i; uint32_t i;
uint32_t temp_time = unix_time(); uint32_t temp_time = unix_time();
for(i = 0; i < length; ++i) for(i = 0; i < length; ++i)
{ {
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time)//if node is bad. if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */
{ {
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port; list[i].ip_port = ip_port;
@ -244,7 +244,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
} }
//replace the first good node that is further to the comp_client_id than that of the client_id in the list /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id)
{ {
uint32_t i; uint32_t i;
@ -264,18 +264,18 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
} }
//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */
void addto_lists(IP_Port ip_port, uint8_t * client_id) void addto_lists(IP_Port ip_port, uint8_t * client_id)
{ {
uint32_t i; uint32_t i;
//NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port))
{ {
if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port))
{ {
//if we can't replace bad nodes we try replacing good ones /* if we can't replace bad nodes we try replacing good ones */
replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
} }
@ -287,7 +287,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port))
{ {
//if we can't replace bad nodes we try replacing good ones /* if we can't replace bad nodes we try replacing good ones. */
replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id);
} }
} }
@ -295,13 +295,13 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
} }
//ping timeout in seconds /* ping timeout in seconds */
#define PING_TIMEOUT 5 #define PING_TIMEOUT 5
//check if we are currently pinging an ip_port and/or a ping_id /* check if we are currently pinging an ip_port and/or a ping_id
//Variables with values of zero will not be checked. variables with values of zero will not be checked.
//if we are already, return 1 if we are already, return 1
//else return 0 else return 0
//TODO: Maybe optimize this TODO: optimize this */
int is_pinging(IP_Port ip_port, uint64_t ping_id) int is_pinging(IP_Port ip_port, uint64_t ping_id)
{ {
uint32_t i; uint32_t i;
@ -341,7 +341,7 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
} }
//Same as last function but for get_node requests. /* Same as last function but for get_node requests. */
int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
{ {
uint32_t i; uint32_t i;
@ -381,10 +381,10 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
} }
//Add a new ping request to the list of ping requests /* Add a new ping request to the list of ping requests
//returns the ping_id to put in the ping request returns the ping_id to put in the ping request
//returns 0 if problem. returns 0 if problem.
//TODO: Maybe optimize this TODO: optimize this */
uint64_t add_pinging(IP_Port ip_port) uint64_t add_pinging(IP_Port ip_port)
{ {
uint32_t i, j; uint32_t i, j;
@ -408,7 +408,7 @@ uint64_t add_pinging(IP_Port ip_port)
} }
//Same but for get node requests /* Same but for get node requests */
uint64_t add_gettingnodes(IP_Port ip_port) uint64_t add_gettingnodes(IP_Port ip_port)
{ {
uint32_t i, j; uint32_t i, j;
@ -434,11 +434,11 @@ uint64_t add_gettingnodes(IP_Port ip_port)
//send a ping request /* send a ping request
//Ping request only works if none has been sent to that ip/port in the last 5 seconds. Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
static int pingreq(IP_Port ip_port, uint8_t * public_key) static int pingreq(IP_Port ip_port, uint8_t * public_key)
{ {
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
{ {
return 1; return 1;
} }
@ -474,10 +474,10 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
} }
//send a ping response /* send a ping response */
static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
{ {
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
{ {
return 1; return 1;
} }
@ -501,10 +501,10 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
} }
//send a getnodes request /* send a getnodes request */
static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
{ {
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
{ {
return 1; return 1;
} }
@ -545,10 +545,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
} }
//send a send nodes response /* send a send nodes response */
static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
{ {
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
{ {
return 1; return 1;
} }
@ -591,10 +591,9 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
/* Packet handling functions
//Packet handling functions One to handle each types of packets we receive
//One to handle each types of packets we receive return 0 if handled correctly, 1 if packet is bad. */
//return 0 if handled correctly, 1 if packet is bad.
int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
{ {
uint64_t ping_id; uint64_t ping_id;
@ -602,7 +601,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
{ {
return 1; return 1;
} }
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
{ {
return 1; return 1;
} }
@ -620,7 +619,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
pingres(source, packet + 1, ping_id); pingres(source, packet + 1, ping_id);
pingreq(source, packet + 1);//TODO: make this smarter? pingreq(source, packet + 1); /* TODO: make this smarter? */
return 0; return 0;
@ -633,7 +632,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
{ {
return 1; return 1;
} }
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
{ {
return 1; return 1;
} }
@ -664,7 +663,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
{ {
return 1; return 1;
} }
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
{ {
return 1; return 1;
} }
@ -684,7 +683,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
memcpy(&ping_id, plain, sizeof(ping_id)); memcpy(&ping_id, plain, sizeof(ping_id));
sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
pingreq(source, packet + 1);//TODO: make this smarter? pingreq(source, packet + 1); /* TODO: make this smarter? */
return 0; return 0;
@ -737,9 +736,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
} }
//END of packet handling functions /* END of packet handling functions */
int DHT_addfriend(uint8_t * client_id) int DHT_addfriend(uint8_t * client_id)
{ {
@ -766,15 +763,13 @@ int DHT_addfriend(uint8_t * client_id)
int DHT_delfriend(uint8_t * client_id) int DHT_delfriend(uint8_t * client_id)
{ {
uint32_t i; uint32_t i;
Friend * temp; Friend * temp;
for(i = 0; i < num_friends; ++i) for(i = 0; i < num_friends; ++i)
{ {
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
{ {
--num_friends; --num_friends;
if(num_friends != i) if(num_friends != i)
@ -795,7 +790,7 @@ int DHT_delfriend(uint8_t * client_id)
//TODO: Optimize this. /* TODO: Optimize this. */
IP_Port DHT_getfriendip(uint8_t * client_id) IP_Port DHT_getfriendip(uint8_t * client_id)
{ {
uint32_t i, j; uint32_t i, j;
@ -803,7 +798,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
uint32_t temp_time = unix_time(); uint32_t temp_time = unix_time();
for(i = 0; i < num_friends; ++i) for(i = 0; i < num_friends; ++i)
{ {
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
{ {
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
{ {
@ -825,8 +820,6 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{ {
switch (packet[0]) { switch (packet[0]) {
@ -851,17 +844,17 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
} }
//The timeout after which a node is discarded completely. /* The timeout after which a node is discarded completely. */
#define Kill_NODE_TIMEOUT 300 #define Kill_NODE_TIMEOUT 300
//ping interval in seconds for each node in our lists. /* ping interval in seconds for each node in our lists. */
#define PING_INTERVAL 60 #define PING_INTERVAL 60
//ping interval in seconds for each random sending of a get nodes request. /* ping interval in seconds for each random sending of a get nodes request. */
#define GET_NODE_INTERVAL 10 #define GET_NODE_INTERVAL 10
//Ping each client in the "friends" list every 60 seconds. /* Ping each client in the "friends" list every 60 seconds.
//Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */
void doDHTFriends() void doDHTFriends()
@ -876,14 +869,14 @@ void doDHTFriends()
{ {
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
{ {
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */
{ {
if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
{ {
pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
friends_list[i].client_list[j].last_pinged = temp_time; friends_list[i].client_list[j].last_pinged = temp_time;
} }
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */
{ {
index[num_nodes] = j; index[num_nodes] = j;
++num_nodes; ++num_nodes;
@ -903,9 +896,9 @@ void doDHTFriends()
static uint32_t close_lastgetnodes; static uint32_t close_lastgetnodes;
//Ping each client in the close nodes list every 60 seconds. /* Ping each client in the close nodes list every 60 seconds.
//Send a get nodes request every 20 seconds to a random good node in the list. Send a get nodes request every 20 seconds to a random good node in the list. */
void doClose()//tested void doClose() /* tested */
{ {
uint32_t i; uint32_t i;
uint32_t temp_time = unix_time(); uint32_t temp_time = unix_time();
@ -915,14 +908,14 @@ void doClose()//tested
for(i = 0; i < LCLIENT_LIST; ++i) for(i = 0; i < LCLIENT_LIST; ++i)
{ {
if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */
{ {
if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
{ {
pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
close_clientlist[i].last_pinged = temp_time; close_clientlist[i].last_pinged = temp_time;
} }
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */
{ {
index[num_nodes] = i; index[num_nodes] = i;
++num_nodes; ++num_nodes;
@ -951,31 +944,28 @@ void doDHT()
void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
{ {
getnodes(ip_port, public_key, self_public_key); getnodes(ip_port, public_key, self_public_key);
} }
//get the size of the DHT (for saving) /* get the size of the DHT (for saving) */
uint32_t DHT_size() uint32_t DHT_size()
{ {
return sizeof(close_clientlist) + sizeof(Friend) * num_friends; return sizeof(close_clientlist) + sizeof(Friend) * num_friends;
} }
//save the DHT in data where data is an array of size DHT_size() /* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t * data) void DHT_save(uint8_t * data)
{ {
memcpy(data, close_clientlist, sizeof(close_clientlist)); memcpy(data, close_clientlist, sizeof(close_clientlist));
memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends); memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends);
} }
//load the DHT from data of size size; /* load the DHT from data of size size;
//return -1 if failure return -1 if failure
//return 0 if success return 0 if success */
int DHT_load(uint8_t * data, uint32_t size) int DHT_load(uint8_t * data, uint32_t size)
{ {
if(size < sizeof(close_clientlist)) if(size < sizeof(close_clientlist))
@ -987,7 +977,7 @@ int DHT_load(uint8_t * data, uint32_t size)
return -1; return -1;
} }
uint32_t i, j; uint32_t i, j;
//uint32_t temp_time = unix_time(); /* uint32_t temp_time = unix_time(); */
uint16_t temp; uint16_t temp;
temp = (size - sizeof(close_clientlist))/sizeof(Friend); temp = (size - sizeof(close_clientlist))/sizeof(Friend);
@ -1021,8 +1011,8 @@ int DHT_load(uint8_t * data, uint32_t size)
return 0; return 0;
} }
//returns 0 if we are not connected to the DHT /* returns 0 if we are not connected to the DHT
//returns 1 if we are returns 1 if we are */
int DHT_isconnected() int DHT_isconnected()
{ {
uint32_t i; uint32_t i;

View File

@ -28,66 +28,66 @@
#include "net_crypto.h" #include "net_crypto.h"
//Current time, unix format /* Current time, unix format */
#define unix_time() ((uint32_t)time(NULL)) #define unix_time() ((uint32_t)time(NULL))
//size of the client_id in bytes /* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
//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 DHT_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 DHT_delfriend(uint8_t * client_id); int DHT_delfriend(uint8_t * client_id);
//Get ip of friend /* Get ip of friend
//client_id must be CLIENT_ID_SIZE bytes long. client_id must be CLIENT_ID_SIZE bytes long.
//ip must be 4 bytes long. ip must be 4 bytes long.
//port must be 2 bytes long. port must be 2 bytes long.
//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 DHT_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) */
void doDHT(); void doDHT();
//if we receive a DHT packet we call this function so it can be handled. /* if we receive a DHT packet we call this function so it can be handled.
//Return 0 if packet is handled correctly. return 0 if packet is handled correctly.
//return 1 if it didn't handle the packet or if the packet was shit. return 1 if it didn't handle the packet or if the packet was shit. */
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); 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 node with ip port and public_key Sends a get nodes request to the given node with ip port and public_key */
void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key);
//SAVE/LOAD functions /* SAVE/LOAD functions */
//get the size of the DHT (for saving) /* get the size of the DHT (for saving) */
uint32_t DHT_size(); uint32_t DHT_size();
//save the DHT in data where data is an array of size DHT_size() /* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t * data); void DHT_save(uint8_t * data);
//load the DHT from data of size size; /* load the DHT from data of size size;
//return -1 if failure return -1 if failure
//return 0 if success return 0 if success */
int DHT_load(uint8_t * data, uint32_t size); int DHT_load(uint8_t * data, uint32_t size);
//returns 0 if we are not connected to the DHT /* returns 0 if we are not connected to the DHT
//returns 1 if we are returns 1 if we are */
int DHT_isconnected(); int DHT_isconnected();
#endif #endif

View File

@ -21,29 +21,29 @@
along with Tox. If not, see <http://www.gnu.org/licenses/>. along with Tox. If not, see <http://www.gnu.org/licenses/>.
*/ */
//TODO: clean this file a bit. /* TODO: clean this file a bit.
//There are a couple of useless variables to get rid of. There are a couple of useless variables to get rid of. */
#include "Lossless_UDP.h" #include "Lossless_UDP.h"
//maximum data packets in sent and receive queues. /* maximum data packets in sent and receive queues. */
#define MAX_QUEUE_NUM 16 #define MAX_QUEUE_NUM 16
//maximum length of the data in the data packets /* maximum length of the data in the data packets */
//#define MAX_DATA_SIZE 1024 //defined in Lossless_UDP.h /* #define MAX_DATA_SIZE 1024 */ /* defined in Lossless_UDP.h */
//maximum number of data packets in the buffer /* maximum number of data packets in the buffer */
#define BUFFER_PACKET_NUM (16-1) #define BUFFER_PACKET_NUM (16-1)
//Lossless UDP connection timeout. /* Lossless UDP connection timeout.
//timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */
#define CONNEXION_TIMEOUT 5 #define CONNEXION_TIMEOUT 5
//initial amount of sync/hanshake packets to send per second. /* initial amount of sync/hanshake packets to send per second. */
#define SYNC_RATE 2 #define SYNC_RATE 2
//initial send rate of data. /* initial send rate of data. */
#define DATA_SYNC_RATE 30 #define DATA_SYNC_RATE 30
typedef struct typedef struct
@ -55,37 +55,37 @@ typedef struct
typedef struct typedef struct
{ {
IP_Port ip_port; IP_Port ip_port;
uint8_t status;//0 if connection is dead, 1 if attempting handshake, uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
//2 if handshake is done (we start sending SYNC packets) 2 if handshake is done (we start sending SYNC packets)
//3 if we are sending SYNC packets and can send data 3 if we are sending SYNC packets and can send data
//4 if the connection has timed out. 4 if the connection has timed out. */
uint8_t inbound; //1 or 2 if connection was initiated by someone else, 0 if not. uint8_t inbound; /* 1 or 2 if connection was initiated by someone else, 0 if not.
//2 if incoming_connection() has not returned it yet, 1 if it has. 2 if incoming_connection() has not returned it yet, 1 if it has. */
uint16_t SYNC_rate;//current SYNC packet send rate packets per second. uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */
uint16_t data_rate;//current data packet send rate packets per second. uint16_t data_rate; /* current data packet send rate packets per second. */
uint64_t last_SYNC; //time at which our last SYNC packet was sent. uint64_t last_SYNC; /* time at which our last SYNC packet was sent. */
uint64_t last_sent; //time at which our last data or handshake packet was sent. uint64_t last_sent; /* time at which our last data or handshake packet was sent. */
uint64_t last_recvSYNC; //time at which we last received a SYNC packet from the other uint64_t last_recvSYNC; /* time at which we last received a SYNC packet from the other */
uint64_t last_recvdata; //time at which we last received a DATA packet from the other uint64_t last_recvdata; /* time at which we last received a DATA packet from the other */
uint64_t killat; //time at which to kill the connection uint64_t killat; /* time at which to kill the connection */
Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
Data recvbuffer[MAX_QUEUE_NUM];//packet receive buffer. Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
uint32_t handshake_id1; uint32_t handshake_id1;
uint32_t handshake_id2; uint32_t handshake_id2;
uint32_t recv_packetnum; //number of data packets received (also used as handshake_id1) uint32_t recv_packetnum; /* number of data packets received (also used as handshake_id1) */
uint32_t orecv_packetnum; //number of packets received by the other peer uint32_t orecv_packetnum; /* number of packets received by the other peer */
uint32_t sent_packetnum; //number of data packets sent uint32_t sent_packetnum; /* number of data packets sent */
uint32_t osent_packetnum; //number of packets sent by the other peer. uint32_t osent_packetnum; /* number of packets sent by the other peer. */
uint32_t sendbuff_packetnum; //number of latest packet written onto the sendbuffer uint32_t sendbuff_packetnum; /* number of latest packet written onto the sendbuffer */
uint32_t successful_sent;//we know all packets before that number were successfully sent uint32_t successful_sent; /* we know all packets before that number were successfully sent */
uint32_t successful_read;//packet number of last packet read with the read_packet function uint32_t successful_read; /* packet number of last packet read with the read_packet function */
uint32_t req_packets[BUFFER_PACKET_NUM]; //list of currently requested packet numbers(by the other person) uint32_t req_packets[BUFFER_PACKET_NUM]; /* list of currently requested packet numbers(by the other person) */
uint16_t num_req_paquets; //total number of currently requested packets(by the other person) uint16_t num_req_paquets; /* total number of currently requested packets(by the other person) */
uint8_t recv_counter; uint8_t recv_counter;
uint8_t send_counter; uint8_t send_counter;
uint8_t timeout; //connection timeout in seconds. uint8_t timeout; /* connection timeout in seconds. */
}Connection; }Connection;
@ -93,13 +93,13 @@ typedef struct
static Connection connections[MAX_CONNECTIONS]; static Connection connections[MAX_CONNECTIONS];
//static uint32_t numconnections; /* static uint32_t numconnections; */
//Functions /* Functions */
//get connection id from IP_Port /* get connection id from IP_Port
//return -1 if there are no connections like we are looking for return -1 if there are no connections like we are looking for
//return id if it found it return id if it found it */
int getconnection_id(IP_Port ip_port) int getconnection_id(IP_Port ip_port)
{ {
uint32_t i; uint32_t i;
@ -114,13 +114,13 @@ int getconnection_id(IP_Port ip_port)
return -1; return -1;
} }
//table of random numbers used below. /* table of random numbers used below. */
static uint32_t randtable[6][256]; static uint32_t randtable[6][256];
//generate a handshake_id which depends on the ip_port. /* generate a handshake_id which depends on the ip_port.
//this function will always give one unique handshake_id per ip_port. this function will always give one unique handshake_id per ip_port.
//TODO: make this better TODO: make this better */
uint32_t handshake_id(IP_Port source) uint32_t handshake_id(IP_Port source)
{ {
uint32_t id = 0, i; uint32_t id = 0, i;
@ -132,14 +132,14 @@ uint32_t handshake_id(IP_Port source)
} }
id ^= randtable[i][((uint8_t *)&source)[i]]; id ^= randtable[i][((uint8_t *)&source)[i]];
} }
if(id == 0)//id can't be zero if(id == 0) /* id can't be zero */
{ {
id = 1; id = 1;
} }
return id; return id;
} }
//change the hnshake id associated with that ip_port /* change the hnshake id associated with that ip_port
//TODO: make this better TODO: make this better */
void change_handshake(IP_Port source) void change_handshake(IP_Port source)
{ {
uint8_t rand = random_int() % 4; uint8_t rand = random_int() % 4;
@ -147,10 +147,10 @@ void change_handshake(IP_Port source)
} }
//initialize a new connection to ip_port /* initialize a new connection to ip_port
//returns an integer corresponding to the connection id. returns an integer corresponding to the connection id.
//return -1 if it could not initialize the connection. return -1 if it could not initialize the connection.
//if there already was an existing connection to that ip_port return its number. if there already was an existing connection to that ip_port return its number. */
int new_connection(IP_Port ip_port) int new_connection(IP_Port ip_port)
{ {
int connect = getconnection_id(ip_port); int connect = getconnection_id(ip_port);
@ -177,7 +177,7 @@ int new_connection(IP_Port ip_port)
connections[i].last_sent = current_time(); connections[i].last_sent = current_time();
connections[i].killat = ~0; connections[i].killat = ~0;
connections[i].send_counter = 0; connections[i].send_counter = 0;
//add randomness to timeout to prevent connections getting stuck in a loop. /* add randomness to timeout to prevent connections getting stuck in a loop. */
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
return i; return i;
} }
@ -185,9 +185,9 @@ int new_connection(IP_Port ip_port)
return -1; return -1;
} }
//initialize a new inbound connection from ip_port /* initialize a new inbound connection from ip_port
//returns an integer corresponding to the connection id. returns an integer corresponding to the connection id.
//return -1 if it could not initialize the connection. return -1 if it could not initialize the connection. */
int new_inconnection(IP_Port ip_port) int new_inconnection(IP_Port ip_port)
{ {
if(getconnection_id(ip_port) != -1) if(getconnection_id(ip_port) != -1)
@ -207,9 +207,9 @@ int new_inconnection(IP_Port ip_port)
connections[i].data_rate = DATA_SYNC_RATE; connections[i].data_rate = DATA_SYNC_RATE;
connections[i].last_recvSYNC = current_time(); connections[i].last_recvSYNC = current_time();
connections[i].last_sent = current_time(); connections[i].last_sent = current_time();
//add randomness to timeout to prevent connections getting stuck in a loop. /* add randomness to timeout to prevent connections getting stuck in a loop. */
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
//if this connection isn't handled within the timeout kill it. /* if this connection isn't handled within the timeout kill it. */
connections[i].killat = current_time() + 1000000UL*connections[i].timeout; connections[i].killat = current_time() + 1000000UL*connections[i].timeout;
connections[i].send_counter = 127; connections[i].send_counter = 127;
return i; return i;
@ -218,8 +218,8 @@ int new_inconnection(IP_Port ip_port)
return -1; return -1;
} }
//returns an integer corresponding to the next connection in our incoming connection list /* returns an integer corresponding to the next connection in our incoming connection list
//return -1 if there are no new incoming connections in the list. return -1 if there are no new incoming connections in the list. */
int incoming_connection() int incoming_connection()
{ {
uint32_t i; uint32_t i;
@ -234,8 +234,8 @@ int incoming_connection()
return -1; return -1;
} }
//return -1 if it could not kill the connection. /* return -1 if it could not kill the connection.
//return 0 if killed successfully return 0 if killed successfully */
int kill_connection(int connection_id) int kill_connection(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
@ -250,9 +250,9 @@ int kill_connection(int connection_id)
return -1; return -1;
} }
//kill connection in seconds seconds. /* kill connection in seconds seconds.
//return -1 if it can not kill the connection. return -1 if it can not kill the connection.
//return 0 if it will kill it return 0 if it will kill it */
int kill_connection_in(int connection_id, uint32_t seconds) int kill_connection_in(int connection_id, uint32_t seconds)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
@ -266,12 +266,12 @@ int kill_connection_in(int connection_id, uint32_t seconds)
return -1; return -1;
} }
//check if connection is connected /* check if connection is connected
//return 0 no. return 0 no.
//return 1 if attempting handshake return 1 if attempting handshake
//return 2 if handshake is done return 2 if handshake is done
//return 3 if fully connected return 3 if fully connected
//return 4 if timed out and waiting to be killed return 4 if timed out and waiting to be killed */
int is_connected(int connection_id) int is_connected(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
@ -281,7 +281,7 @@ int is_connected(int connection_id)
return 0; return 0;
} }
//returns the ip_port of the corresponding connection. /* returns the ip_port of the corresponding connection. */
IP_Port connection_ip(int connection_id) IP_Port connection_ip(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
@ -292,20 +292,20 @@ IP_Port connection_ip(int connection_id)
return zero; return zero;
} }
//returns the number of packets in the queue waiting to be successfully sent. /* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id) uint32_t sendqueue(int connection_id)
{ {
return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent;
} }
//returns the number of packets in the queue waiting to be successfully read with read_packet(...) /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id) uint32_t recvqueue(int connection_id)
{ {
return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; return connections[connection_id].recv_packetnum - connections[connection_id].successful_read;
} }
//returns the id of the next packet in the queue /* returns the id of the next packet in the queue
//return -1 if no packet in queue return -1 if no packet in queue */
char id_packet(int connection_id) char id_packet(int connection_id)
{ {
if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
@ -314,8 +314,8 @@ char id_packet(int connection_id)
} }
return -1; return -1;
} }
//return 0 if there is no received data in the buffer. /* return 0 if there is no received data in the buffer.
//return length of received packet if successful return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data) int read_packet(int connection_id, uint8_t * data)
{ {
if(recvqueue(connection_id) != 0) if(recvqueue(connection_id) != 0)
@ -330,8 +330,8 @@ int read_packet(int connection_id, uint8_t * data)
return 0; return 0;
} }
//return 0 if data could not be put in packet queue /* return 0 if data could not be put in packet queue
//return 1 if data was put into the queue return 1 if data was put into the queue */
int write_packet(int connection_id, uint8_t * data, uint32_t length) int write_packet(int connection_id, uint8_t * data, uint32_t length)
{ {
if(length > MAX_DATA_SIZE) if(length > MAX_DATA_SIZE)
@ -356,13 +356,13 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length)
//put the packet numbers the we are missing in requested and return the number /* put the packet numbers the we are missing in requested and return the number */
uint32_t missing_packets(int connection_id, uint32_t * requested) uint32_t missing_packets(int connection_id, uint32_t * requested)
{ {
uint32_t number = 0; uint32_t number = 0;
uint32_t i; uint32_t i;
uint32_t temp; uint32_t temp;
if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1))//don't request packets if the buffer is full. if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
{ {
return 0; return 0;
} }
@ -383,9 +383,9 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
} }
//Packet sending functions /* Packet sending functions
//One per packet type. One per packet type.
//see docs/Lossless_UDP.txt for more information. see docs/Lossless_UDP.txt for more information. */
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
@ -444,7 +444,7 @@ int send_data_packet(uint32_t connection_id, uint32_t packet_num)
1 + 4 + connections[connection_id].sendbuffer[index].size); 1 + 4 + connections[connection_id].sendbuffer[index].size);
} }
//sends 1 data packet /* sends 1 data packet */
int send_DATA(uint32_t connection_id) int send_DATA(uint32_t connection_id)
{ {
int ret; int ret;
@ -466,13 +466,13 @@ int send_DATA(uint32_t connection_id)
return 0; return 0;
} }
//END of packet sending functions /* END of packet sending functions */
//Packet handling functions /* Packet handling functions
//One to handle each type of packets we receive One to handle each type of packets we receive
//return 0 if handled correctly, 1 if packet is bad. return 0 if handled correctly, 1 if packet is bad. */
int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
{ {
if(length != (1 + 4 + 4)) if(length != (1 + 4 + 4))
@ -496,11 +496,11 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
{ {
return 1; return 1;
} }
if(handshake_id2 == connections[connection].handshake_id1)//if handshake_id2 is what we sent previously as handshake_id1 if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */
{ {
connections[connection].status = 2; connections[connection].status = 2;
//NOTE:is this necessary? /* NOTE: is this necessary?
//connections[connection].handshake_id2 = handshake_id1; connections[connection].handshake_id2 = handshake_id1; */
connections[connection].orecv_packetnum = handshake_id2; connections[connection].orecv_packetnum = handshake_id2;
connections[connection].osent_packetnum = handshake_id1; connections[connection].osent_packetnum = handshake_id1;
connections[connection].recv_packetnum = handshake_id1; connections[connection].recv_packetnum = handshake_id1;
@ -510,8 +510,8 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
} }
//returns 1 if sync packet is valid /* returns 1 if sync packet is valid
//0 if not. 0 if not. */
int SYNC_valid(uint32_t length) int SYNC_valid(uint32_t length)
{ {
if(length < 4 + 4 + 2) if(length < 4 + 4 + 2)
@ -526,7 +526,7 @@ int SYNC_valid(uint32_t length)
return 1; return 1;
} }
//case 1: /* case 1: */
int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
{ {
if(handshake_id(source) == recv_packetnum) if(handshake_id(source) == recv_packetnum)
@ -548,11 +548,11 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
return -1; return -1;
} }
//case 2: /* case 2: */
int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
{ {
if(recv_packetnum == connections[connection_id].orecv_packetnum) if(recv_packetnum == connections[connection_id].orecv_packetnum)
//&& sent_packetnum == connections[connection_id].osent_packetnum) /* && sent_packetnum == connections[connection_id].osent_packetnum) */
{ {
connections[connection_id].status = 3; connections[connection_id].status = 3;
connections[connection_id].recv_counter = counter; connections[connection_id].recv_counter = counter;
@ -562,17 +562,17 @@ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
} }
return 1; return 1;
} }
//case 3: /* case 3: */
int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
uint16_t number) uint16_t number)
{ {
uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); uint8_t comp_counter = (counter - connections[connection_id].recv_counter );
uint32_t i, temp; uint32_t i, temp;
//uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); /* 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_2 = (sent_packetnum - connections[connection_id].successful_read); */
uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_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 && comp_counter != 0) //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].orecv_packetnum = recv_packetnum;
connections[connection_id].osent_packetnum = sent_packetnum; connections[connection_id].osent_packetnum = sent_packetnum;
@ -629,8 +629,8 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
return 0; return 0;
} }
//add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value.
//return 1 if data was too big, 0 if not. return 1 if data was too big, 0 if not. */
int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size)
{ {
if(size > MAX_DATA_SIZE) if(size > MAX_DATA_SIZE)
@ -679,7 +679,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
return 1; return 1;
} }
if(connections[connection].status != 3)//Drop the data packet if connection is not connected. if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
{ {
return 1; return 1;
} }
@ -698,7 +698,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
} }
//END of packet handling functions /* END of packet handling functions */
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
@ -723,8 +723,8 @@ int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
} }
//Send handshake requests /* Send handshake requests
//handshake packets are sent at the same rate as SYNC packets handshake packets are sent at the same rate as SYNC packets */
void doNew() void doNew()
{ {
uint32_t i; uint32_t i;
@ -740,11 +740,11 @@ void doNew()
} }
} }
//kill all timed out connections /* kill all timed out connections */
if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
connections[i].status != 4) connections[i].status != 4)
{ {
//kill_connection(i); /* kill_connection(i); */
connections[i].status = 4; connections[i].status = 4;
} }
if(connections[i].status > 0 && connections[i].killat < temp_time) if(connections[i].status > 0 && connections[i].killat < temp_time)
@ -792,8 +792,8 @@ void doData()
} }
} }
//TODO: flow control. /* TODO: flow control.
//automatically adjusts send rates of packets for optimal transmission. automatically adjusts send rates of packets for optimal transmission. */
#define MAX_SYNC_RATE 10 #define MAX_SYNC_RATE 10
@ -824,8 +824,8 @@ void adjustRates()
} }
} }
} }
//Call this function a couple times per second /* Call this function a couple times per second
//It's the main loop. It's the main loop. */
void doLossless_UDP() void doLossless_UDP()
{ {
doNew(); doNew();

View File

@ -28,82 +28,82 @@
#include "network.h" #include "network.h"
//maximum length of the data in the data packets /* maximum length of the data in the data packets */
#define MAX_DATA_SIZE 1024 #define MAX_DATA_SIZE 1024
//Functions /* Functions */
//initialize a new connection to ip_port /* initialize a new connection to ip_port
//returns an integer corresponding to the connection id. returns an integer corresponding to the connection id.
//return -1 if it could not initialize the connection. return -1 if it could not initialize the connection.
//if there already was an existing connection to that ip_port return its number. if there already was an existing connection to that ip_port return its number. */
int new_connection(IP_Port ip_port); int new_connection(IP_Port ip_port);
//get connection id from IP_Port /* get connection id from IP_Port
//return -1 if there are no connections like we are looking for return -1 if there are no connections like we are looking for
//return id if it found it return id if it found it */
int getconnection_id(IP_Port ip_port); int getconnection_id(IP_Port ip_port);
//returns an integer corresponding to the next connection in our imcoming connection list /* returns an integer corresponding to the next connection in our imcoming connection list
//return -1 if there are no new incoming connections in the list. return -1 if there are no new incoming connections in the list. */
int incoming_connection(); int incoming_connection();
//return -1 if it could not kill the connection. /* return -1 if it could not kill the connection.
//return 0 if killed successfully return 0 if killed successfully */
int kill_connection(int connection_id); int kill_connection(int connection_id);
//kill connection in seconds seconds. /* kill connection in seconds seconds.
//return -1 if it can not kill the connection. return -1 if it can not kill the connection.
//return 0 if it will kill it return 0 if it will kill it */
int kill_connection_in(int connection_id, uint32_t seconds); int kill_connection_in(int connection_id, uint32_t seconds);
//returns the ip_port of the corresponding connection. /* returns the ip_port of the corresponding connection.
//return 0 if there is no such connection. return 0 if there is no such connection. */
IP_Port connection_ip(int connection_id); IP_Port connection_ip(int connection_id);
//returns the id of the next packet in the queue /* returns the id of the next packet in the queue
//return -1 if no packet in queue return -1 if no packet in queue */
char id_packet(int connection_id); char id_packet(int connection_id);
//return 0 if there is no received data in the buffer. /* return 0 if there is no received data in the buffer.
//return length of received packet if successful return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data); int read_packet(int connection_id, uint8_t * data);
//return 0 if data could not be put in packet queue /* return 0 if data could not be put in packet queue
//return 1 if data was put into the queue return 1 if data was put into the queue */
int write_packet(int connection_id, uint8_t * data, uint32_t length); int write_packet(int connection_id, uint8_t * data, uint32_t length);
//returns the number of packets in the queue waiting to be successfully sent. /* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id); uint32_t sendqueue(int connection_id);
//returns the number of packets in the queue waiting to be successfully read with read_packet(...) /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id); uint32_t recvqueue(int connection_id);
//check if connection is connected /* check if connection is connected
//return 0 no. return 0 no.
//return 1 if attempting handshake return 1 if attempting handshake
//return 2 if handshake is done return 2 if handshake is done
//return 3 if fully connected return 3 if fully connected
//return 4 if timed out and wating to be killed return 4 if timed out and wating to be killed */
int is_connected(int connection_id); int is_connected(int connection_id);
//Call this function a couple times per second /* Call this function a couple times per second
//It's the main loop. It's the main loop. */
void doLossless_UDP(); void doLossless_UDP();
//if we receive a Lossless_UDP packet we call this function so it can be handled. /* if we receive a Lossless_UDP packet we call this function so it can be handled.
//Return 0 if packet is handled correctly. return 0 if packet is handled correctly.
//return 1 if it didn't handle the packet or if the packet was shit. return 1 if it didn't handle the packet or if the packet was shit. */
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
#endif #endif

View File

@ -29,15 +29,15 @@ typedef struct
{ {
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
int crypt_connection_id; int crypt_connection_id;
int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
uint8_t status;//0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
uint8_t name_sent;//0 if we didn't send our name to this friend 1 if we have. uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
uint8_t *userstatus; uint8_t *userstatus;
uint16_t userstatus_length; uint16_t userstatus_length;
uint8_t userstatus_sent; uint8_t userstatus_sent;
uint16_t info_size; //length of the info uint16_t info_size; /* length of the info */
}Friend; }Friend;
@ -54,13 +54,13 @@ static Friend friendlist[MAX_NUM_FRIENDS];
static uint32_t numfriends; static uint32_t numfriends;
//1 if we are online /* 1 if we are online
//0 if we are offline 0 if we are offline
//static uint8_t online; static uint8_t online; */
//return the friend id associated to that public key. /* return the friend id associated to that public key.
//return -1 if no such friend return -1 if no such friend */
int getfriend_id(uint8_t * client_id) int getfriend_id(uint8_t * client_id)
{ {
uint32_t i; uint32_t i;
@ -78,10 +78,10 @@ int getfriend_id(uint8_t * client_id)
} }
//copies the public key associated to that friend id into client_id buffer. /* copies the public key associated to that friend id into client_id buffer.
//make sure that client_id is of size CLIENT_ID_SIZE. make sure that client_id is of size CLIENT_ID_SIZE.
//returns 0 if success return 0 if success
//return -1 if failure. return -1 if failure. */
int getclient_id(int friend_id, uint8_t * client_id) int getclient_id(int friend_id, uint8_t * client_id)
{ {
if(friend_id >= numfriends || friend_id < 0) if(friend_id >= numfriends || friend_id < 0)
@ -98,12 +98,12 @@ int getclient_id(int friend_id, uint8_t * client_id)
} }
//add a friend /* add a friend
//set the data that will be sent along with friend request set the data that will be sent along with friend request
//client_id is the client id of the friend client_id is the client id of the friend
//data is the data and length is the length data is the data and length is the length
//returns the friend number if success returns the friend number if success
//return -1 if failure. return -1 if failure. */
int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
{ {
if(length == 0 || length >= if(length == 0 || length >=
@ -166,9 +166,9 @@ int m_addfriend_norequest(uint8_t * client_id)
return -1; return -1;
} }
//remove a friend /* remove a friend
//returns 0 if success return 0 if success
//return -1 if failure. return -1 if failure */
int m_delfriend(int friendnumber) int m_delfriend(int friendnumber)
{ {
if(friendnumber >= numfriends || friendnumber < 0) if(friendnumber >= numfriends || friendnumber < 0)
@ -193,11 +193,11 @@ int m_delfriend(int friendnumber)
} }
//return 4 if friend is online /* return 4 if friend is online
//return 3 if friend is confirmed return 3 if friend is confirmed
//return 2 if the friend request was sent return 2 if the friend request was sent
//return 1 if the friend was added return 1 if the friend was added
//return 0 if there is no friend with that number. return 0 if there is no friend with that number */
int m_friendstatus(int friendnumber) int m_friendstatus(int friendnumber)
{ {
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
@ -208,9 +208,9 @@ int m_friendstatus(int friendnumber)
} }
//send a text chat message to an online friend. /* send a text chat message to an online friend
//returns 1 if packet was successfully put into the send queue return 1 if packet was successfully put into the send queue
//return 0 if it was not. return 0 if it was not */
int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
{ {
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
@ -218,7 +218,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
return 0; return 0;
} }
if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
//this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
{ {
return 0; return 0;
} }
@ -228,7 +228,7 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
} }
//send a name packet to friendnumber /* send a name packet to friendnumber */
static int m_sendname(int friendnumber, uint8_t * name) static int m_sendname(int friendnumber, uint8_t * name)
{ {
uint8_t temp[MAX_NAME_LENGTH + 1]; uint8_t temp[MAX_NAME_LENGTH + 1];
@ -237,9 +237,9 @@ static int m_sendname(int friendnumber, uint8_t * name)
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1);
} }
//set the name of a friend /* set the name of a friend
//return 0 if success return 0 if success
//return -1 if failure return -1 if failure */
static int setfriendname(int friendnumber, uint8_t * name) static int setfriendname(int friendnumber, uint8_t * name)
{ {
@ -252,10 +252,10 @@ static int setfriendname(int friendnumber, uint8_t * name)
} }
//Set our nickname /* Set our nickname
//name must be a string of maximum MAX_NAME_LENGTH length. name must be a string of maximum MAX_NAME_LENGTH length.
//return 0 if success return 0 if success
//return -1 if failure return -1 if failure */
int setname(uint8_t * name, uint16_t length) int setname(uint8_t * name, uint16_t length)
{ {
if(length > MAX_NAME_LENGTH) if(length > MAX_NAME_LENGTH)
@ -271,11 +271,11 @@ int setname(uint8_t * name, uint16_t length)
return 0; return 0;
} }
//get name of friendnumber /* get name of friendnumber
//put it in name put it in name
//name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
//return 0 if success return 0 if success
//return -1 if failure return -1 if failure */
int getname(int friendnumber, uint8_t * name) int getname(int friendnumber, uint8_t * name)
{ {
if(friendnumber >= numfriends || friendnumber < 0) if(friendnumber >= numfriends || friendnumber < 0)
@ -306,8 +306,8 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
return 0; return 0;
} }
// return the size of friendnumber's user status /* return the size of friendnumber's user status
// guaranteed to be at most MAX_USERSTATUS_LENGTH guaranteed to be at most MAX_USERSTATUS_LENGTH */
int m_get_userstatus_size(int friendnumber) int m_get_userstatus_size(int friendnumber)
{ {
if(friendnumber >= numfriends || friendnumber < 0) if(friendnumber >= numfriends || friendnumber < 0)
@ -317,8 +317,8 @@ int m_get_userstatus_size(int friendnumber)
return friendlist[friendnumber].userstatus_length; return friendlist[friendnumber].userstatus_length;
} }
// copy the user status of friendnumber into buf, truncating if needed to maxlen /* copy the user status of friendnumber into buf, truncating if needed to maxlen
// bytes, use m_get_userstatus_size to find out how much you need to allocate bytes, use m_get_userstatus_size to find out how much you need to allocate */
int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
{ {
if(friendnumber >= numfriends || friendnumber < 0) if(friendnumber >= numfriends || friendnumber < 0)
@ -357,7 +357,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; static uint8_t friend_request_isset = 0;
//set the function that will be executed when a friend request is received. /* set the function that will be executed when a friend request is received. */
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
{ {
friend_request = function; friend_request = function;
@ -368,7 +368,7 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
static void (*friend_message)(int, uint8_t *, uint16_t); static void (*friend_message)(int, uint8_t *, uint16_t);
static uint8_t friend_message_isset = 0; static uint8_t friend_message_isset = 0;
//set the function that will be executed when a message from a friend is received. /* set the function that will be executed when a message from a friend is received. */
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
{ {
friend_message = function; friend_message = function;
@ -393,7 +393,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
} }
#define PORT 33445 #define PORT 33445
//run this at startup /* run this at startup */
void initMessenger() void initMessenger()
{ {
new_keys(); new_keys();
@ -405,7 +405,7 @@ void initMessenger()
} }
static void doFriends() static void doFriends()
{//TODO: add incoming connections and some other stuff. {/* TODO: add incoming connections and some other stuff. */
uint32_t i; uint32_t i;
int len; int len;
uint8_t temp[MAX_DATA_SIZE]; uint8_t temp[MAX_DATA_SIZE];
@ -415,7 +415,7 @@ static void doFriends()
{ {
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
int request = check_friendrequest(friendlist[i].friend_request_id); int request = check_friendrequest(friendlist[i].friend_request_id);
//printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); /* printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); */
if(friendip.ip.i > 1 && request == -1) if(friendip.ip.i > 1 && request == -1)
{ {
friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id,
@ -423,9 +423,9 @@ static void doFriends()
friendlist[i].status = 2; friendlist[i].status = 2;
} }
} }
if(friendlist[i].status == 2 || friendlist[i].status == 3) //friend is not online if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */
{ {
check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request check_friendrequest(friendlist[i].friend_request_id); /* for now this is used to kill the friend request */
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) switch(is_cryptoconnected(friendlist[i].crypt_connection_id))
{ {
@ -433,7 +433,7 @@ static void doFriends()
if (friendip.ip.i > 1) if (friendip.ip.i > 1)
friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
break; break;
case 3: // Connection is established case 3: /* Connection is established */
friendlist[i].status = 4; friendlist[i].status = 4;
break; break;
case 4: case 4:
@ -444,7 +444,7 @@ static void doFriends()
break; break;
} }
} }
while(friendlist[i].status == 4) //friend is online while(friendlist[i].status == 4) /* friend is online */
{ {
if(friendlist[i].name_sent == 0) if(friendlist[i].name_sent == 0)
{ {
@ -468,10 +468,10 @@ static void doFriends()
if (len != MAX_NAME_LENGTH + 1) break; if (len != MAX_NAME_LENGTH + 1) break;
if(friend_namechange_isset) if(friend_namechange_isset)
{ {
friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */
} }
memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present. friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */
break; break;
} }
case PACKET_ID_USERSTATUS: { case PACKET_ID_USERSTATUS: {
@ -496,7 +496,7 @@ static void doFriends()
} }
else else
{ {
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */
{ {
crypto_kill(friendlist[i].crypt_connection_id); crypto_kill(friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1; friendlist[i].crypt_connection_id = -1;
@ -545,7 +545,7 @@ static void doInbound()
} }
} }
//the main loop that needs to be run at least 200 times per second. /* the main loop that needs to be run at least 200 times per second. */
void doMessenger() void doMessenger()
{ {
IP_Port ip_port; IP_Port ip_port;
@ -554,18 +554,18 @@ void doMessenger()
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1)
{ {
#ifdef DEBUG #ifdef DEBUG
//if(rand() % 3 != 1)//simulate packet loss /* if(rand() % 3 != 1) //simulate packet loss */
//{ /* { */
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
{ {
//if packet is discarded /* if packet is discarded */
printf("Received unhandled packet with length: %u\n", length); printf("Received unhandled packet with length: %u\n", length);
} }
else else
{ {
printf("Received handled packet with length: %u\n", length); 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); printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
#else #else
DHT_handlepacket(data, length, ip_port); DHT_handlepacket(data, length, ip_port);
@ -581,14 +581,14 @@ void doMessenger()
doFriends(); doFriends();
} }
//returns the size of the messenger data (for saving) /* returns the size of the messenger data (for saving) */
uint32_t Messenger_size() uint32_t Messenger_size()
{ {
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
+ sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
} }
//save the messenger in data of size Messenger_size() /* save the messenger in data of size Messenger_size() */
void Messenger_save(uint8_t * data) void Messenger_save(uint8_t * data)
{ {
save_keys(data); save_keys(data);
@ -604,7 +604,7 @@ void Messenger_save(uint8_t * data)
memcpy(data, friendlist, sizeof(Friend) * numfriends); memcpy(data, friendlist, sizeof(Friend) * numfriends);
} }
//load the messenger from data of size length. /* load the messenger from data of size length. */
int Messenger_load(uint8_t * data, uint32_t length) int Messenger_load(uint8_t * data, uint32_t length)
{ {
if(length == ~0) if(length == ~0)
@ -651,7 +651,7 @@ int Messenger_load(uint8_t * data, uint32_t length)
{ {
int fnum = m_addfriend_norequest(temp[i].client_id); int fnum = m_addfriend_norequest(temp[i].client_id);
setfriendname(fnum, temp[i].name); setfriendname(fnum, temp[i].name);
//set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
} }
} }
free(temp); free(temp);

View File

@ -37,113 +37,113 @@
#define PACKET_ID_USERSTATUS 49 #define PACKET_ID_USERSTATUS 49
#define PACKET_ID_MESSAGE 64 #define PACKET_ID_MESSAGE 64
// don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased /* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
// to an absurdly large number later to an absurdly large number later */
//add a friend /* add a friend
//set the data that will be sent along with friend request set the data that will be sent along with friend request
//client_id is the client id of the friend client_id is the client id of the friend
//data is the data and length is the length data is the data and length is the length
//returns the friend number if success returns the friend number if success
//return -1 if failure. return -1 if failure. */
int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length);
//add a friend without sending a friendrequest. /* add a friend without sending a friendrequest.
//returns the friend number if success returns the friend number if success
//return -1 if failure. return -1 if failure. */
int m_addfriend_norequest(uint8_t * client_id); int m_addfriend_norequest(uint8_t * client_id);
//return the friend id associated to that client id. /* return the friend id associated to that client id.
//return -1 if no such friend return -1 if no such friend */
int getfriend_id(uint8_t * client_id); int getfriend_id(uint8_t * client_id);
//copies the public key associated to that friend id into client_id buffer. /* copies the public key associated to that friend id into client_id buffer.
//make sure that client_id is of size CLIENT_ID_SIZE. make sure that client_id is of size CLIENT_ID_SIZE.
//returns 0 if success return 0 if success
//return -1 if failure. return -1 if failure */
int getclient_id(int friend_id, uint8_t * client_id); int getclient_id(int friend_id, uint8_t * client_id);
//remove a friend /* remove a friend */
int m_delfriend(int friendnumber); int m_delfriend(int friendnumber);
//return 4 if friend is online /* return 4 if friend is online
//return 3 if friend is confirmed return 3 if friend is confirmed
//return 2 if the friend request was sent return 2 if the friend request was sent
//return 1 if the friend was added return 1 if the friend was added
//return 0 if there is no friend with that number. return 0 if there is no friend with that number */
int m_friendstatus(int friendnumber); int m_friendstatus(int friendnumber);
//send a text chat message to an online friend. /* send a text chat message to an online friend
//returns 1 if packet was successfully put into the send queue returns 1 if packet was successfully put into the send queue
//return 0 if it was not. return 0 if it was not */
int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length);
//Set our nickname /* Set our nickname
//name must be a string of maximum MAX_NAME_LENGTH length. name must be a string of maximum MAX_NAME_LENGTH length.
//return 0 if success return 0 if success
//return -1 if failure return -1 if failure */
int setname(uint8_t * name, uint16_t length); int setname(uint8_t * name, uint16_t length);
//get name of friendnumber /* get name of friendnumber
//put it in name put it in name
//name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
//return 0 if success return 0 if success
//return -1 if failure return -1 if failure */
int getname(int friendnumber, uint8_t * name); int getname(int friendnumber, uint8_t * name);
// set our user status /* set our user status
// you are responsible for freeing status after you are responsible for freeing status after
// returns 0 on success, -1 on failure returns 0 on success, -1 on failure */
int m_set_userstatus(uint8_t *status, uint16_t length); int m_set_userstatus(uint8_t *status, uint16_t length);
// return the length of friendnumber's user status, /* return the length of friendnumber's user status,
// including null including null
// pass it into malloc pass it into malloc */
int m_get_userstatus_size(int friendnumber); int m_get_userstatus_size(int friendnumber);
// copy friendnumber's userstatus into buf, truncating if size is over maxlen /* copy friendnumber's userstatus into buf, truncating if size is over maxlen
// get the size you need to allocate from m_get_userstatus_size get the size you need to allocate from m_get_userstatus_size */
int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen);
//set the function that will be executed when a friend request is received. /* set the function that will be executed when a friend request is received.
//function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
//set the function that will be executed when a message from a friend is received. /* set the function that will be executed when a message from a friend is received.
//function format is: function(int friendnumber, uint8_t * message, uint32_t length) function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
// set the callback for name changes /* set the callback for name changes
// function(int friendnumber, uint8_t *newname, uint16_t length) function(int friendnumber, uint8_t *newname, uint16_t length)
// you are not responsible for freeing newname you are not responsible for freeing newname */
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
// set the callback for user status changes /* set the callback for user status changes
// function(int friendnumber, uint8_t *newstatus, uint16_t length) function(int friendnumber, uint8_t *newstatus, uint16_t length)
// you are not responsible for freeing newstatus you are not responsible for freeing newstatus */
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
//run this at startup /* run this at startup */
void initMessenger(); void initMessenger();
//the main loop that needs to be run at least 200 times per second. /* the main loop that needs to be run at least 200 times per second */
void doMessenger(); void doMessenger();
//SAVING AND LOADING FUNCTIONS: /* SAVING AND LOADING FUNCTIONS: */
//returns the size of the messenger data (for saving) /* returns the size of the messenger data (for saving) */
uint32_t Messenger_size(); uint32_t Messenger_size();
//save the messenger in data (must be allocated memory of size Messenger_size()) /* save the messenger in data (must be allocated memory of size Messenger_size()) */
void Messenger_save(uint8_t * data); void Messenger_save(uint8_t * data);
//load the messenger from data of size length. /* load the messenger from data of size length */
int Messenger_load(uint8_t * data, uint32_t length); int Messenger_load(uint8_t * data, uint32_t length);
#endif #endif