From 60f333328cea995585e7199b1e9fc93dfae53428 Mon Sep 17 00:00:00 2001 From: Konstantin Kowalski Date: Tue, 6 Aug 2013 13:52:05 -0400 Subject: [PATCH 1/5] Added a linked list implementation. --- testing/misc_tools.c | 12 ++++- testing/misc_tools.h | 111 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/testing/misc_tools.c b/testing/misc_tools.c index aa546a88f..fa7f42c9e 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -25,7 +25,11 @@ #include #include -#include /* for sscanf */ +#include + +#ifdef DEBUG +#include +#endif // DEBUG /* TODO: rewrite */ unsigned char * hex_string_to_bin(char hex_string[]) @@ -39,3 +43,9 @@ unsigned char * hex_string_to_bin(char hex_string[]) return val; } + + + + + + diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 5079e15d0..4e09d40d1 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h @@ -25,5 +25,114 @@ #define MISC_TOOLS_H unsigned char * hex_string_to_bin(char hex_string[]); - + + + + + +/************************Linked List*********************** + * This is a simple linked list implementation, very similar + * to Linux kernel's /include/linux/list.h (which we can't + * use because Tox is GPLv3 and Linux is GPLv2.) + * + * TODO: Make the lists easier to use with some sweat pre- + * processor syntactic sugar. + **********************************************************/ + +/* Example usage + +This sample program makes a new struct which contains a +character and a tox_list_t. It then prompts a user for +input until he enters q or e. It then adds each character +to the list, and uses a special for loop to print them. +It then removes all the 'z' characters, and prints the list +again. + +//Notice that the data to be put in the list *contains* tox_list_t; +//usually, this is the other way around! +typedef struct tox_string { + char c; + tox_list_t tox_lst; //Notice that tox_lst is *NOT* a pointer. +} tox_string_t; + +int main() +{ + tox_list_t head; + tox_list_new(&head); //initialize head + + //input a new character, until user enters q or e + char c = '\0'; + while (c != 'q' && c != 'e') { + scanf("%c", &c); + tox_string_t* tmp = malloc(sizeof(tox_string_t)); + tmp->c = c; + tox_list_add(&head, &tmp->tox_lst); //add it to the list + } + +TOX_LIST_FOR_EACH() takes a struct tox_list and a name for a temporary pointer to use in the loop. + +TOX_LIST_GET_VALUE() uses magic to return an instance of a structure that contains tox_list_t. +You have to give it a temporary tox_string_t, name of tox_list_t member inside our structure (tox_lst), +and the type of structure to return. + + TOX_LIST_FOR_EACH(head, tmp) + printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c); + + TOX_LIST_FOR_EACH(head, tmp) { + if (TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c == 'z') { + //If you delete tmp, you have to quit the loop, or it will go on infinitly. + //This will be fixed later on. + tox_list_remove(tmp); + break; + } + } + + printf("\n"); + TOX_LIST_FOR_EACH(head, tmp) + printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c); + + + return 0; +} +*/ + +#define MEMBER_OFFSET(var_name_in_parent, parent_type) \ + (&(((parent_type*)0)->var_name_in_parent)) + +#define GET_PARENT(var, var_name_in_parent, parent_type) \ + (*((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))) + +#define TOX_LIST_FOR_EACH(lst, tmp_name) \ + for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) + +#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) + +typedef struct tox_list { + struct tox_list *prev, *next; +} tox_list_t; + +/* Returns a new tox_list_t. */ +static inline void tox_list_new(tox_list_t* lst) { + lst->prev = lst->next = lst; +} + +/* Inserts a new tox_lst after lst and returns it. */ +static inline void tox_list_add(tox_list_t* lst, tox_list_t* new_lst) { + tox_list_new(new_lst); + + new_lst->next = lst->next; + new_lst->next->prev = new_lst; + + lst->next = new_lst; + new_lst->prev = lst; +} + +static inline void tox_list_remove(tox_list_t* lst) { +#ifdef DEBUG /* TODO: check how debugging is done in Tox. */ + assert(lst->next != lst && lst->prev != lst); +#endif + lst->prev->next = lst->next; + lst->next->prev = lst->prev; +} + #endif // MISC_TOOLS_H From f603342acfdc4196a91e0f2356f2ffb286b02d81 Mon Sep 17 00:00:00 2001 From: Konstantin Kowalski Date: Tue, 6 Aug 2013 16:47:15 -0400 Subject: [PATCH 2/5] Added ERROR() and WARNING() for debugging. --- testing/misc_tools.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 4e09d40d1..eb09693e0 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h @@ -26,9 +26,40 @@ unsigned char * hex_string_to_bin(char hex_string[]); +/* WARNING(msg) takes a printf()-styled string and prints it + * with some additional details. + * ERROR(exit_status, msg) does the same thing as WARNING(), but + * also exits the program with the given exit status. + * Examples: + * WARNING(""); + * int exit_status = 2; + * ERROR(exit_status, "exiting with status %i", exit_status); + */ +#ifdef DEBUG + #include + #include + #include + #define DEBUG_PRINT(str, ...) do { \ + char msg[1000]; \ + sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ + fprintf(stderr, msg, __VA_ARGS__); \ + } while (0) + #define WARNING(...) do { \ + fprintf(stderr, "warning in "); \ + DEBUG_PRINT(__VA_ARGS__, ' '); \ + } while (0) + #define ERROR(exit_status, ...) do { \ + fprintf(stderr, "error in "); \ + DEBUG_PRINT(__VA_ARGS__, ' '); \ + exit(exit_status); \ + } while (0) +#else + #define WARNING(...) + #define ERROR(...) +#endif // DEBUG /************************Linked List*********************** * This is a simple linked list implementation, very similar From 7ddeb2bf6da9e68d8963ff703ec85284f7ed73ba Mon Sep 17 00:00:00 2001 From: Nick ODell Date: Tue, 6 Aug 2013 16:55:11 -0600 Subject: [PATCH 3/5] Fix bug where we could run past the end of the friends list --- core/Messenger.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 9fa643bba..5532c9cc6 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -116,7 +116,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) return FAERR_ALREADYSENT; uint32_t i; - for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ + for (i = 0; i < numfriends && i < MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_ADDED; @@ -141,7 +141,7 @@ int m_addfriend_norequest(uint8_t * client_id) if (getfriend_id(client_id) != -1) return -1; uint32_t i; - for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ + for (i = 0; i < numfriends && i < MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_REQUESTED; From eb546db56b7d30c970173d4a4d02672d3dc24a3a Mon Sep 17 00:00:00 2001 From: Nick ODell Date: Tue, 6 Aug 2013 17:07:45 -0600 Subject: [PATCH 4/5] Fix segfault when no broadcast device is found --- core/LAN_discovery.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 559536855..26b3930c7 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c @@ -70,6 +70,10 @@ static uint32_t get_broadcast(void) } } close(sock); + if(sock_holder == NULL) { + perror("[!] no broadcast device found"); + return 0; + } return sock_holder->sin_addr.s_addr; } From 34f531553a6ec39ba55282dbf5b29e9a401d0880 Mon Sep 17 00:00:00 2001 From: Nick ODell Date: Tue, 6 Aug 2013 17:15:09 -0600 Subject: [PATCH 5/5] Fix bug where file handle wasn't closed on success --- testing/nTox.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/nTox.c b/testing/nTox.c index 63d0c32ea..ecdba1e37 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -410,6 +410,8 @@ void load_key(char *path) } } + if(fclose(data_file) < 0) + perror("[!] fclose failed"); return; FILE_ERROR: