From e680d885d725cbc27ddbbdabe79860d51816f0cb Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 9 Jul 2013 20:25:52 -0400 Subject: [PATCH] Delete friends function now works and some other changes. --- core/Messenger.c | 110 +++++++++++++++++++++---------- core/Messenger.h | 3 + testing/DHT_cryptosendfiletest.c | 2 +- testing/Messenger_test.c | 2 +- 4 files changed, 80 insertions(+), 37 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 57baa84c..7603a04f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -29,39 +29,96 @@ Friend friendlist[MAX_NUM_FRIENDS]; uint32_t numfriends; + +//return the friend id associated to that public key. +//return -1 if no such friend +int getfriend_id(uint8_t * client_id) +{ + uint32_t i; + for(i = 0; i < numfriends; i++) + { + if(friendlist[i].status > 0) + { + if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) + { + return i; + } + } + } + return -1; +} + + //add a friend //client_id is the client i of the friend //returns the friend number if success //return -1 if failure. int m_addfriend(uint8_t * client_id) { - - DHT_addfriend(client_id); - friendlist[numfriends].status = 1; - friendlist[numfriends].friend_request_id = -1; - memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE); - numfriends++; - - return numfriends - 1; + if(getfriend_id(client_id) != -1) + { + return -1; + } + uint32_t i; + for(i = 0; i < (numfriends + 1); i++) + { + if(friendlist[i].status == 0) + { + DHT_addfriend(client_id); + friendlist[i].status = 1; + friendlist[i].friend_request_id = -1; + memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); + numfriends++; + return i; + } + } + return -1; } int m_addfriend_norequest(uint8_t * client_id) { - DHT_addfriend(client_id); - friendlist[numfriends].status = 2; - friendlist[numfriends].friend_request_id = -1; - memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE); - numfriends++; - - return numfriends - 1; + if(getfriend_id(client_id) != -1) + { + return -1; + } + uint32_t i; + for(i = 0; i < (numfriends + 1); i++) + { + if(friendlist[i].status == 0) + { + DHT_addfriend(client_id); + friendlist[i].status = 2; + friendlist[i].friend_request_id = -1; + memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); + numfriends++; + return i; + } + } + return -1; } //remove a friend +//returns 0 if success +//return -1 if failure. int m_delfriend(int friendnumber) -{/* - TODO +{ + if(friendnumber >= numfriends || friendnumber < 0) + { + return -1; + } + DHT_delfriend(friendlist[friendnumber].client_id); -*/ + memset(&friendlist[friendnumber], 0, sizeof(Friend)); + uint32_t i; + for(i = numfriends; i != 0; i--) + { + if(friendlist[i].status != 0) + { + break; + } + } + numfriends = i; + return 0; } @@ -220,23 +277,6 @@ void doFriendRequest() } -//return the friend id associated to that public key. -//return -1 if no such friend -int getfriend_id(uint8_t * public_key) -{ - uint32_t i; - for(i = 0; i < numfriends; i++) - { - if(friendlist[i].status > 0) - { - if(memcmp(public_key, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) - { - return i; - } - } - } - return -1; -} void doInbound() { diff --git a/core/Messenger.h b/core/Messenger.h index 4e56b4fc..42ce20ca 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -23,6 +23,9 @@ int m_addfriend(uint8_t * client_id); //return -1 if failure. int m_addfriend_norequest(uint8_t * client_id); +//return the friend id associated to that client id. +//return -1 if no such friend +int getfriend_id(uint8_t * client_id); //remove a friend int m_delfriend(int friendnumber); diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index ad77a17d..39a928d2 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c @@ -7,7 +7,7 @@ * * NOTE: this program simulates 33% packet loss. * - * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/Linux/lib/amd64/* DHT_cryptosendfiletest.c + * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c * * * Command line arguments are the ip and port of a node (for bootstrapping). diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 8b19ffa5..ea32673d 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -7,7 +7,7 @@ * If it recieves a message from a friend it replies back. * * - * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../core/Messenger.c ../nacl/build/Linux/lib/amd64/* Messenger_test.c + * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../core/Messenger.c ../nacl/build/$HOSTNAME/lib/amd64/* Messenger_test.c * * * Command line arguments are the ip and port of a node (for bootstrapping).