summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c23
-rw-r--r--core/Messenger.c11
-rw-r--r--core/Messenger.h2
3 files changed, 33 insertions, 3 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 1d13aa73..bcaaf6d8 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -319,6 +319,28 @@ static int replace_bad( Client_data * list,
319 319
320 return 1; 320 return 1;
321} 321}
322/*Sort the list. It will be sorted from furthest to closest.
323 TODO: this is innefficient and needs to be optimized.*/
324static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
325{
326 if(length == 0)
327 return;
328 uint32_t i, count;
329 while(1) {
330 count = 0;
331 for(i = 0; i < (length - 1); ++i) {
332 if(id_closest(comp_client_id, list[i].client_id, list[i + 1].client_id) == 1) {
333 Client_data temp = list[i + 1];
334 list[i + 1] = list[i];
335 list[i] = temp;
336 ++count;
337 }
338 }
339 if(count == 0)
340 return;
341 }
342}
343
322 344
323/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ 345/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
324static int replace_good( Client_data * list, 346static int replace_good( Client_data * list,
@@ -329,6 +351,7 @@ static int replace_good( Client_data * list,
329{ 351{
330 uint32_t i; 352 uint32_t i;
331 uint64_t temp_time = unix_time(); 353 uint64_t temp_time = unix_time();
354 sort_list(list, length, comp_client_id);
332 355
333 for(i = 0; i < length; ++i) 356 for(i = 0; i < length; ++i)
334 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { 357 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
diff --git a/core/Messenger.c b/core/Messenger.c
index 690a81b1..bed59d4d 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -120,6 +120,7 @@ void getaddress(Messenger *m, uint8_t *address)
120 * return FAERR_BADCHECKSUM if bad checksum in address 120 * return FAERR_BADCHECKSUM if bad checksum in address
121 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different 121 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different
122 * (the nospam for that friend was set to the new one) 122 * (the nospam for that friend was set to the new one)
123 * return FAERR_NOMEM if increasing the friend list size fails
123 */ 124 */
124int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 125int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
125{ 126{
@@ -148,7 +149,8 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
148 } 149 }
149 150
150 /* resize the friend list if necessary */ 151 /* resize the friend list if necessary */
151 realloc_friendlist(m, m->numfriends + 1); 152 if (realloc_friendlist(m, m->numfriends + 1) != 0)
153 return FAERR_NOMEM;
152 154
153 uint32_t i; 155 uint32_t i;
154 for (i = 0; i <= m->numfriends; ++i) { 156 for (i = 0; i <= m->numfriends; ++i) {
@@ -180,7 +182,8 @@ int m_addfriend_norequest(Messenger *m, uint8_t * client_id)
180 return -1; 182 return -1;
181 183
182 /* resize the friend list if necessary */ 184 /* resize the friend list if necessary */
183 realloc_friendlist(m, m->numfriends + 1); 185 if (realloc_friendlist(m, m->numfriends + 1) != 0)
186 return FAERR_NOMEM;
184 187
185 uint32_t i; 188 uint32_t i;
186 for (i = 0; i <= m->numfriends; ++i) { 189 for (i = 0; i <= m->numfriends; ++i) {
@@ -221,7 +224,9 @@ int m_delfriend(Messenger *m, int friendnumber)
221 break; 224 break;
222 } 225 }
223 m->numfriends = i; 226 m->numfriends = i;
224 realloc_friendlist(m, m->numfriends + 1); 227
228 if (realloc_friendlist(m, m->numfriends + 1) != 0)
229 return FAERR_NOMEM;
225 230
226 return 0; 231 return 0;
227} 232}
diff --git a/core/Messenger.h b/core/Messenger.h
index a2add19d..e12b3fc8 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -63,6 +63,7 @@ extern "C" {
63#define FAERR_UNKNOWN -5 63#define FAERR_UNKNOWN -5
64#define FAERR_BADCHECKSUM -6 64#define FAERR_BADCHECKSUM -6
65#define FAERR_SETNEWNOSPAM -7 65#define FAERR_SETNEWNOSPAM -7
66#define FAERR_NOMEM -8
66 67
67/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased 68/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased
68 to an absurdly large number later */ 69 to an absurdly large number later */
@@ -151,6 +152,7 @@ void getaddress(Messenger *m, uint8_t *address);
151 * return -6 if bad checksum in address 152 * return -6 if bad checksum in address
152 * return -7 if the friend was already there but the nospam was different 153 * return -7 if the friend was already there but the nospam was different
153 * (the nospam for that friend was set to the new one) 154 * (the nospam for that friend was set to the new one)
155 * return -8 if increasing the friend list size fails
154 */ 156 */
155int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); 157int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
156 158