Just cleaning up as I read, no behaviour changes

This commit is contained in:
Jeffail 2013-07-15 15:42:50 +01:00
parent 7674e65726
commit 859f31c160
3 changed files with 21 additions and 28 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
//nacl build //nacl build
nacl/build/ nacl/build/
build

View File

@ -126,6 +126,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
{ {
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
} }
for(j = 0; j < CLIENT_ID_SIZE; j++) for(j = 0; j < CLIENT_ID_SIZE; j++)
{ {
@ -768,7 +769,7 @@ int DHT_addfriend(uint8_t * client_id)
{ {
temp = malloc(sizeof(Friend)); temp = malloc(sizeof(Friend));
} }
if(num_friends > 0) else
{ {
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
} }
@ -782,8 +783,6 @@ int DHT_addfriend(uint8_t * client_id)
memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
num_friends++; num_friends++;
return 0; return 0;
} }

View File

@ -33,21 +33,15 @@ typedef struct
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
uint16_t info_size; //length of the info uint16_t info_size; //length of the info
}Friend; }Friend;
#define MAX_NUM_FRIENDS 256 #define MAX_NUM_FRIENDS 256
static Friend friendlist[MAX_NUM_FRIENDS]; static Friend friendlist[MAX_NUM_FRIENDS];
static uint32_t numfriends; static uint32_t numfriends;
//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)
@ -66,6 +60,7 @@ int getfriend_id(uint8_t * client_id)
return -1; return -1;
} }
//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 //returns 0 if success
@ -94,8 +89,8 @@ int getclient_id(int friend_id, uint8_t * client_id)
//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 > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES if(length == 0 || length >=
- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES) (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES))
{ {
return -1; return -1;
} }
@ -105,7 +100,7 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
return -1; return -1;
} }
uint32_t i; uint32_t i;
for(i = 0; i < (numfriends + 1); i++) for(i = 0; i <= numfriends; i++)
{ {
if(friendlist[i].status == 0) if(friendlist[i].status == 0)
{ {
@ -131,7 +126,7 @@ int m_addfriend_norequest(uint8_t * client_id)
return -1; return -1;
} }
uint32_t i; uint32_t i;
for(i = 0; i < (numfriends + 1); i++) for(i = 0; i <= numfriends; i++)
{ {
if(friendlist[i].status == 0) if(friendlist[i].status == 0)
{ {
@ -205,7 +200,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
temp[0] = 64; temp[0] = 64;
memcpy(temp + 1, message, length); memcpy(temp + 1, message, length);
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
} }
@ -236,7 +230,6 @@ void initMessenger()
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
} }
static void doFriends() static void doFriends()
@ -261,19 +254,21 @@ static void doFriends()
if(friendlist[i].status == 2 || friendlist[i].status == 3) if(friendlist[i].status == 2 || friendlist[i].status == 3)
{ {
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);
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 0 && friendip.ip.i > 1) switch(is_cryptoconnected(friendlist[i].crypt_connection_id))
{ {
case 0:
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;
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 3)//if connection is established. case 3: // Connection is established
{
friendlist[i].status = 4; friendlist[i].status = 4;
} break;
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) case 4:
{
crypto_kill(friendlist[i].crypt_connection_id); crypto_kill(friendlist[i].crypt_connection_id);
break;
default:
break;
} }
} }
while(friendlist[i].status == 4) while(friendlist[i].status == 4)
@ -308,9 +303,7 @@ static void doFriendRequest()
if(len >= 0) if(len >= 0)
{ {
(*friend_request)(public_key, temp, len); (*friend_request)(public_key, temp, len);
} }
} }