From 0590d8325c4ae729fe3f1f96a4d4632ac9eefff4 Mon Sep 17 00:00:00 2001 From: Chris Hall Date: Wed, 14 Aug 2013 17:27:10 +1200 Subject: removing unnecessary function pointer _isset variables --- core/Messenger.c | 34 +++++++++++++--------------------- core/Messenger.h | 12 ++---------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 2721f8f6..45e8e3f9 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -75,9 +75,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) return -1; } -/* +/* * returns a uint16_t that represents the checksum of address of length len - * + * * TODO: Another checksum algorithm might be better. */ static uint16_t address_checksum(uint8_t *address, uint32_t len) @@ -94,7 +94,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) /* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] - * + * */ void getaddress(Messenger *m, uint8_t *address) { @@ -118,7 +118,7 @@ void getaddress(Messenger *m, uint8_t *address) * return FAERR_ALREADYSENT if friend request already sent or already a friend * return FAERR_UNKNOWN for unknown error * return FAERR_BADCHECKSUM if bad checksum in address - * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different + * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different * (the nospam for that friend was set to the new one) */ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) @@ -449,8 +449,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) m->friendlist[friendnumber].receives_read_receipts = yesno; } -/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); -static uint8_t friend_request_isset = 0; */ +/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ /* set the function that will be executed when a friend request is received. */ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) { @@ -461,55 +460,48 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_message = function; - m->friend_message_isset = 1; m->friend_message_userdata = userdata; } void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_action = function; - m->friend_action_isset = 1; m->friend_action_userdata = userdata; } void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_namechange = function; - m->friend_namechange_isset = 1; m->friend_namechange_userdata = userdata; } void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_statusmessagechange = function; - m->friend_statusmessagechange_isset = 1; m->friend_statuschange_userdata = userdata; } void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata) { m->friend_userstatuschange = function; - m->friend_userstatuschange_isset = 1; m->friend_userstatuschange_userdata = userdata; } void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata) { m->read_receipt = function; - m->read_receipt_isset = 1; m->read_receipt_userdata = userdata; } void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata) { m->friend_connectionstatuschange = function; - m->friend_connectionstatuschange_isset = 1; m->friend_connectionstatuschange_userdata = userdata; } static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) { - if (!m->friend_connectionstatuschange_isset) + if (!m->friend_connectionstatuschange) return; if (status == NOFRIEND) return; @@ -648,7 +640,7 @@ void doFriends(Messenger *m) case PACKET_ID_NICKNAME: { if (data_length >= MAX_NAME_LENGTH || data_length == 0) break; - if(m->friend_namechange_isset) + if(m->friend_namechange) m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); memcpy(m->friendlist[i].name, data, data_length); m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ @@ -659,7 +651,7 @@ void doFriends(Messenger *m) break; uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); - if (m->friend_statusmessagechange_isset) + if (m->friend_statusmessagechange) m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH), m->friend_statuschange_userdata); set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); @@ -670,7 +662,7 @@ void doFriends(Messenger *m) if (data_length != 1) break; USERSTATUS status = data[0]; - if (m->friend_userstatuschange_isset) + if (m->friend_userstatuschange) m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata); set_friend_userstatus(m, i, status); break; @@ -683,12 +675,12 @@ void doFriends(Messenger *m) if (m->friendlist[i].receives_read_receipts) { write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); } - if (m->friend_message_isset) + if (m->friend_message) (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); break; } case PACKET_ID_ACTION: { - if (m->friend_action_isset) + if (m->friend_action) (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); break; } @@ -698,7 +690,7 @@ void doFriends(Messenger *m) break; memcpy(&msgid, data, sizeof(msgid)); msgid = ntohl(msgid); - if (m->read_receipt_isset) + if (m->read_receipt) (*m->read_receipt)(m, i, msgid, m->read_receipt_userdata); break; } @@ -744,7 +736,7 @@ void doMessenger(Messenger *m) doNetCrypto(); doInbound(m); doFriends(m); - + timer_poll(); } diff --git a/core/Messenger.h b/core/Messenger.h index 9621176f..a2add19d 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -111,28 +111,20 @@ typedef struct Messenger { uint32_t numfriends; void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*); - uint8_t friend_message_isset; void* friend_message_userdata; void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*); - uint8_t friend_action_isset; void* friend_action_userdata; void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); - uint8_t friend_namechange_isset; void* friend_namechange_userdata; void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); - uint8_t friend_statusmessagechange_isset; void* friend_statusmessagechange_userdata; void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*); - uint8_t friend_userstatuschange_isset; void* friend_userstatuschange_userdata; void (*read_receipt)(struct Messenger *m, int, uint32_t, void*); - uint8_t read_receipt_isset; void* read_receipt_userdata; void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*); - uint8_t friend_statuschange_isset; void* friend_statuschange_userdata; void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*); - uint8_t friend_connectionstatuschange_isset; void* friend_connectionstatuschange_userdata; @@ -141,7 +133,7 @@ typedef struct Messenger { /* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] - * + * */ void getaddress(Messenger *m, uint8_t *address); @@ -157,7 +149,7 @@ void getaddress(Messenger *m, uint8_t *address); * return -4 if friend request already sent or already a friend * return -5 for unknown error * return -6 if bad checksum in address - * return -7 if the friend was already there but the nospam was different + * return -7 if the friend was already there but the nospam was different * (the nospam for that friend was set to the new one) */ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); -- cgit v1.2.3 From cf5ad56a4a26afc93da63a6064d49f6203c42d03 Mon Sep 17 00:00:00 2001 From: Chris Hall Date: Wed, 14 Aug 2013 17:35:15 +1200 Subject: fixing minor memory leak of friendlist --- core/Messenger.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/Messenger.c b/core/Messenger.c index 45e8e3f9..e2d83ada 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -572,7 +572,10 @@ Messenger * initMessenger(void) /* run this before closing shop */ void cleanupMessenger(Messenger *m){ - /* FIXME TODO it seems no one frees friendlist or all the elements status */ + /* FIXME TODO ideally cleanupMessenger will mirror initMessenger + * this requires the other modules to expose cleanup functions + */ + free(m->friendlist); free(m); } -- cgit v1.2.3 From 72ee99e84838e3b0c74e3d66defdcc3ab7f4f6f9 Mon Sep 17 00:00:00 2001 From: naxuroqa Date: Wed, 14 Aug 2013 10:44:25 +0200 Subject: rewrite of get_id function --- testing/nTox.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index cab2d158..ead4fd21 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -52,28 +52,15 @@ uint8_t num_requests = 0; void get_id(Messenger *m, char *data) { - char idstring0[200]; - char idstring1[FRIEND_ADDRESS_SIZE][5]; - char idstring2[FRIEND_ADDRESS_SIZE][5]; + sprintf(data, "[i] ID: "); + int offset = strlen(data); int i = 0; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); - for(i = 0; i < FRIEND_ADDRESS_SIZE; i++) + for(; i < FRIEND_ADDRESS_SIZE; i++) { - if (address[i] < (FRIEND_ADDRESS_SIZE / 2)) - strcpy(idstring1[i],"0"); - else - strcpy(idstring1[i], ""); - sprintf(idstring2[i], "%hhX",address[i]); + sprintf(data + 2*i + offset, "%02X ", address[i]); } - strcpy(idstring0,"[i] ID: "); - int j = 0; - for (j = 0; j < FRIEND_ADDRESS_SIZE; j++) { - strcat(idstring0,idstring1[j]); - strcat(idstring0,idstring2[j]); - } - - memcpy(data, idstring0, strlen(idstring0)); } void new_lines(char *line) -- cgit v1.2.3 From 3b42f489e495748b729958b3be18954eb27e75f7 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Wed, 14 Aug 2013 21:16:02 +1200 Subject: Have toxic handle new no-spam values from friend request --- testing/toxic/prompt.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index e1a7d75c..b1d6693f 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -138,6 +138,12 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) case FAERR_UNKNOWN: wprintw(self->window, "Undefined error when adding friend.\n"); break; + case FAERR_BADCHECKSUM: + wprintw(self->window, "Bad checksum in address.\n"); + break; + case FAERR_SETNEWNOSPAM: + wprintw(self->window, "Nospam was different.\n"); + break; default: wprintw(self->window, "Friend added as %d.\n", num); on_friendadded(num); -- cgit v1.2.3 From 1260aee9e1b61841459f83397f84b089d360819b Mon Sep 17 00:00:00 2001 From: Astonex Date: Wed, 14 Aug 2013 11:23:38 +0100 Subject: Update prompt.c Make id uppercase. --- testing/toxic/prompt.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index e1a7d75c..c0169c62 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -121,6 +121,11 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) } id_bin[i] = x; } + + for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { + id[i] = toupper(id[i]); + } + int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1); switch (num) { case FAERR_TOOLONG: -- cgit v1.2.3 From 4e21ac0d57df8d20dcdc4dd318d1e63166017e30 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Tue, 13 Aug 2013 09:34:46 +1200 Subject: prefer param over static Conflicts: testing/toxic/main.c --- testing/toxic/friendlist.c | 6 +++--- testing/toxic/main.c | 29 ++++++++++++++--------------- testing/toxic/prompt.c | 6 +++--- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 56061cf9..1a77440e 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -13,7 +13,7 @@ #include "windows.h" extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; -extern int add_window(ToxWindow w, int n); +extern int add_window(Messenger *m, ToxWindow w, int n); extern ToxWindow new_chat(Messenger *m, int friendnum); extern int active_window; @@ -54,7 +54,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num; - add_window(new_chat(m, num), i); + add_window(m, new_chat(m, num), i); active_window = i; break; } @@ -121,7 +121,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num_selected; friends[num_selected].chatwin = num_selected; - add_window(new_chat(m, num_selected), i); + add_window(m, new_chat(m, num_selected), i); active_window = i; break; } diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 40ff5d09..8ab2d393 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -40,8 +40,6 @@ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow* prompt; -static Messenger *m; - int w_num; int active_window; @@ -103,7 +101,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l } } -void on_friendadded(int friendnumber) +void on_friendadded(Messenger *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); } @@ -129,10 +127,10 @@ static void init_term() refresh(); } -static void init_tox() +static Messenger * init_tox() { /* Init core */ - m = initMessenger(); + Messenger *m = initMessenger(); /* Callbacks */ m_callback_friendrequest(m, on_request, NULL); @@ -147,6 +145,7 @@ static void init_tox() #else setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); #endif + return m; } #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ @@ -207,7 +206,7 @@ void init_window_status() WINDOW_STATUS[j] = -1; } -int add_window(ToxWindow w, int n) +int add_window(Messenger *m, ToxWindow w, int n) { if (w_num >= TOXWINDOWS_MAX_NUM) return -1; @@ -241,13 +240,13 @@ void del_window(ToxWindow *w, int f_num) refresh(); } -static void init_windows() +static void init_windows(Messenger *m) { w_num = 0; int n_prompt = 0; int n_friendslist = 1; - if (add_window(new_prompt(), n_prompt) == -1 - || add_window(new_friendlist(), n_friendslist) == -1) { + if (add_window(m, new_prompt(), n_prompt) == -1 + || add_window(m, new_friendlist(), n_friendslist) == -1) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); @@ -255,7 +254,7 @@ static void init_windows() prompt = &windows[n_prompt]; } -static void do_tox() +static void do_tox(Messenger *m) { static int conn_try = 0; static int conn_err = 0; @@ -279,7 +278,7 @@ static void do_tox() doMessenger(m); } -static void load_data(char *path) +static void load_data(Messenger *m, char *path) { FILE *fd; size_t len; @@ -451,12 +450,12 @@ int main(int argc, char *argv[]) } init_term(); - init_tox(); - init_windows(); + Messenger *m = init_tox(); + init_windows(m); init_window_status(); if(f_loadfromfile) - load_data(DATA_FILE); + load_data(m, DATA_FILE); free(DATA_FILE); if (f_flag == -1) { @@ -474,7 +473,7 @@ int main(int argc, char *argv[]) } while(true) { /* Update tox */ - do_tox(); + do_tox(m); /* Draw */ a = &windows[active_window]; diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index b1d6693f..c01ed710 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -15,7 +15,7 @@ uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX -extern void on_friendadded(int friendnumber); +extern void on_friendadded(Messenger *m, int friendnumber); static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; @@ -87,7 +87,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args) wprintw(self->window, "Failed to add friend.\n"); else { wprintw(self->window, "Friend accepted as: %d.\n", num); - on_friendadded(num); + on_friendadded(m, num); } } @@ -146,7 +146,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) break; default: wprintw(self->window, "Friend added as %d.\n", num); - on_friendadded(num); + on_friendadded(m, num); break; } } -- cgit v1.2.3 From 3f37c5580dc6cfd0920095f6d3c99aba035724ee Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 12:58:15 +1200 Subject: add header guard and includes to windows.h --- testing/toxic/windows.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 648243d0..4eb55a99 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -1,8 +1,13 @@ /* * Toxic -- Tox Curses Client */ +#ifndef _windows_h +#define _windows_h +#include +#include #include +#include "../../core/Messenger.h" #define TOXWINDOWS_MAX_NUM 32 #define MAX_FRIENDS_NUM 100 #define MAX_STR_SIZE 256 @@ -32,3 +37,6 @@ struct ToxWindow_ { WINDOW* window; }; + +#endif + -- cgit v1.2.3 From 75ce48146a7764b67a19d41e4da5d279d59b6ea1 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Tue, 13 Aug 2013 10:50:43 +1200 Subject: remove externs in favor of includes --- testing/toxic/chat.c | 10 +++--- testing/toxic/chat.h | 7 ++++ testing/toxic/friendlist.c | 25 +++++++------- testing/toxic/friendlist.h | 14 ++++++++ testing/toxic/main.c | 84 ++++++++++++++++++++++------------------------ testing/toxic/prompt.c | 6 ++-- testing/toxic/prompt.h | 14 ++++++++ 7 files changed, 98 insertions(+), 62 deletions(-) create mode 100644 testing/toxic/chat.h create mode 100644 testing/toxic/friendlist.h create mode 100644 testing/toxic/prompt.h diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 1b5e743d..6811569b 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -13,6 +13,8 @@ #include "../../core/network.h" #include "windows.h" +#include "friendlist.h" +#include "chat.h" #define CURS_Y_OFFSET 3 @@ -24,10 +26,8 @@ typedef struct { WINDOW* linewin; } ChatContext; -extern int active_window; +static delWindowFn *del_window; -extern void del_window(ToxWindow *w, int f_num); -extern void fix_name(uint8_t *name); void print_help(ChatContext *self); void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); @@ -320,7 +320,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) } else if (strcmp(ctx->line, "/close") == 0) { - active_window = 0; // Go to prompt screen int f_num = ctx->friendnum; delwin(ctx->linewin); del_window(self, f_num); @@ -371,8 +370,9 @@ void print_help(ChatContext *self) wattroff(self->history, COLOR_PAIR(2)); } -ToxWindow new_chat(Messenger *m, int friendnum) +ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn *f) { + del_window = f; ToxWindow ret; memset(&ret, 0, sizeof(ret)); diff --git a/testing/toxic/chat.h b/testing/toxic/chat.h new file mode 100644 index 00000000..dcd5b3ba --- /dev/null +++ b/testing/toxic/chat.h @@ -0,0 +1,7 @@ +#ifndef CHAT_H_6489PZ13 +#define CHAT_H_6489PZ13 + +typedef void (delWindowFn)(ToxWindow *w, int f_num); +ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn f); + +#endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 1a77440e..5b79fdef 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -10,13 +10,12 @@ #include "../../core/Messenger.h" #include "../../core/network.h" -#include "windows.h" +#include "friendlist.h" -extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; -extern int add_window(Messenger *m, ToxWindow w, int n); -extern ToxWindow new_chat(Messenger *m, int friendnum); - -extern int active_window; +static delWindowFn *del_window; +static setActiveWindowFn *set_active_window; +static addWindowFn *add_window; +static char * WINDOW_STATUS; typedef struct { uint8_t name[MAX_NAME_LENGTH]; @@ -54,8 +53,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num; - add_window(m, new_chat(m, num), i); - active_window = i; + add_window(m, new_chat(m, num, del_window), i); break; } } @@ -111,7 +109,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) int i; for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { if (WINDOW_STATUS[i] == num_selected) { - active_window = i; + set_active_window(i); break; } } @@ -121,8 +119,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num_selected; friends[num_selected].chatwin = num_selected; - add_window(m, new_chat(m, num_selected), i); - active_window = i; + add_window(m, new_chat(m, num_selected, del_window), i); break; } } @@ -169,7 +166,11 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m) } -ToxWindow new_friendlist() { +ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws) { + del_window = dw; + set_active_window = saw; + add_window = aw; + WINDOW_STATUS = ws; ToxWindow ret; memset(&ret, 0, sizeof(ret)); diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h new file mode 100644 index 00000000..e8b93aef --- /dev/null +++ b/testing/toxic/friendlist.h @@ -0,0 +1,14 @@ +#ifndef FRIENDLIST_H_53I41IM +#define FRIENDLIST_H_53I41IM + + +#include "windows.h" +#include "chat.h" +typedef void (setActiveWindowFn)(int ch); +typedef int (addWindowFn)(Messenger *m, ToxWindow w, int n); +ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws); +int friendlist_onFriendAdded(Messenger *m, int num); +void disable_chatwin(int f_num); +void fix_name(uint8_t *name); + +#endif /* end of include guard: FRIENDLIST_H_53I41IM */ diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 8ab2d393..a109311e 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -21,14 +21,9 @@ #include "configdir.h" #include "windows.h" +#include "prompt.h" +#include "friendlist.h" -extern ToxWindow new_prompt(); -extern ToxWindow new_friendlist(); - -extern int friendlist_onFriendAdded(Messenger *m, int num); -extern void disable_chatwin(int f_num); -extern int add_req(uint8_t *public_key); // XXX -extern unsigned char *hex_string_to_bin(char hex_string[]); /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; @@ -221,12 +216,14 @@ int add_window(Messenger *m, ToxWindow w, int n) windows[n] = w; w.onInit(&w, m); w_num++; + active_window = n; return n; } /* Deletes window w and cleans up */ void del_window(ToxWindow *w, int f_num) { + active_window = 0; // Go to prompt screen delwin(w->window); int i; for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { @@ -240,13 +237,48 @@ void del_window(ToxWindow *w, int f_num) refresh(); } +/* Shows next window when tab or back-tab is pressed */ +void set_active_window(int ch) +{ + int f_inf = 0; + int max = MAX_WINDOW_SLOTS-1; + if (ch == '\t') { + int i = (active_window + 1) % max; + while (true) { + if (WINDOW_STATUS[i] != -1) { + active_window = i; + return; + } + i = (i + 1) % max; + if (f_inf++ > max) { // infinite loop check + endwin(); + exit(2); + } + } + }else { + int i = active_window - 1; + if (i < 0) i = max; + while (true) { + if (WINDOW_STATUS[i] != -1) { + active_window = i; + return; + } + if (--i < 0) i = max; + if (f_inf++ > max) { + endwin(); + exit(2); + } + } + } +} + static void init_windows(Messenger *m) { w_num = 0; int n_prompt = 0; int n_friendslist = 1; - if (add_window(m, new_prompt(), n_prompt) == -1 - || add_window(m, new_friendlist(), n_friendslist) == -1) { + if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 + || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); @@ -378,40 +410,6 @@ void prepare_window(WINDOW *w) wresize(w, LINES-2, COLS); } -/* Shows next window when tab or back-tab is pressed */ -void set_active_window(int ch) -{ - int f_inf = 0; - int max = MAX_WINDOW_SLOTS-1; - if (ch == '\t') { - int i = (active_window + 1) % max; - while (true) { - if (WINDOW_STATUS[i] != -1) { - active_window = i; - return; - } - i = (i + 1) % max; - if (f_inf++ > max) { // infinite loop check - endwin(); - exit(2); - } - } - }else { - int i = active_window - 1; - if (i < 0) i = max; - while (true) { - if (WINDOW_STATUS[i] != -1) { - active_window = i; - return; - } - if (--i < 0) i = max; - if (f_inf++ > max) { - endwin(); - exit(2); - } - } - } -} int main(int argc, char *argv[]) { diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index c01ed710..bc4b8545 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -11,11 +11,12 @@ #include "../../core/network.h" #include "windows.h" +#include "prompt.h" uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX -extern void on_friendadded(Messenger *m, int friendnumber); +static friendAddedFn *on_friendadded; static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; @@ -428,8 +429,9 @@ static void prompt_onInit(ToxWindow *self, Messenger *m) wclrtoeol(self->window); } -ToxWindow new_prompt() +ToxWindow new_prompt(friendAddedFn *f) { + on_friendadded = f; ToxWindow ret; memset(&ret, 0, sizeof(ret)); ret.onKey = &prompt_onKey; diff --git a/testing/toxic/prompt.h b/testing/toxic/prompt.h new file mode 100644 index 00000000..6c5320ab --- /dev/null +++ b/testing/toxic/prompt.h @@ -0,0 +1,14 @@ +#ifndef PROMPT_H_UZYGWFFL +#define PROMPT_H_UZYGWFFL + +#include "windows.h" + +typedef void (friendAddedFn)(Messenger *m, int friendnumber); + +ToxWindow new_prompt(friendAddedFn *f); +int add_req(uint8_t *public_key); +unsigned char *hex_string_to_bin(char hex_string[]); + +#endif /* end of include guard: PROMPT_H_UZYGWFFL */ + + -- cgit v1.2.3 From 1edb7c1660884face7f5d97b8d6df57da5e54a17 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Tue, 13 Aug 2013 12:21:03 +1200 Subject: move windows functions to its own file --- testing/toxic/CMakeLists.txt | 1 + testing/toxic/main.c | 236 +------------------------------------------ testing/toxic/windows.c | 235 ++++++++++++++++++++++++++++++++++++++++++ testing/toxic/windows.h | 8 ++ 4 files changed, 248 insertions(+), 232 deletions(-) create mode 100644 testing/toxic/windows.c diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index 13b8692d..dd811b97 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt @@ -8,6 +8,7 @@ set(exe_name toxic) add_executable(${exe_name} main.c + windows.c prompt.c friendlist.c chat.c diff --git a/testing/toxic/main.c b/testing/toxic/main.c index a109311e..d7b251dc 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -24,84 +24,10 @@ #include "prompt.h" #include "friendlist.h" - -/* Holds status of chat windows */ -char WINDOW_STATUS[MAX_WINDOW_SLOTS]; - #ifndef TOXICVER #define TOXICVER "NOVER" //Use the -D flag to set this #endif -static ToxWindow windows[MAX_WINDOW_SLOTS]; -static ToxWindow* prompt; - -int w_num; -int active_window; - -/* CALLBACKS START */ -void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata) -{ - int n = add_req(public_key); - wprintw(prompt->window, "\nFriend request from:\n"); - - int i; - for (i = 0; i < KEY_SIZE_BYTES; ++i) { - wprintw(prompt->window, "%02x", public_key[i] & 0xff); - } - - wprintw(prompt->window, "\nWith the message: %s\n", data); - wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); - - for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { - if (windows[i].onFriendRequest != NULL) - windows[i].onFriendRequest(&windows[i], public_key, data, length); - } -} - -void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) -{ - int i; - for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { - if (windows[i].onMessage != NULL) - windows[i].onMessage(&windows[i], m, friendnumber, string, length); - } -} - -void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) -{ - int i; - for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { - if (windows[i].onAction != NULL) - windows[i].onAction(&windows[i], m, friendnumber, string, length); - } -} - -void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) -{ - wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); - int i; - for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { - if (windows[i].onNickChange != NULL) - windows[i].onNickChange(&windows[i], friendnumber, string, length); - } -} - -void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) -{ - wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); - int i; - for (i=0; i= TOXWINDOWS_MAX_NUM) - return -1; - - if (LINES < 2) - return -1; - - w.window = newwin(LINES - 2, COLS, 0, 0); - if (w.window == NULL) - return -1; - - windows[n] = w; - w.onInit(&w, m); - w_num++; - active_window = n; - return n; -} - -/* Deletes window w and cleans up */ -void del_window(ToxWindow *w, int f_num) -{ - active_window = 0; // Go to prompt screen - delwin(w->window); - int i; - for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { - if (WINDOW_STATUS[i] == f_num) { - WINDOW_STATUS[i] = -1; - disable_chatwin(f_num); - break; - } - } - clear(); - refresh(); -} - -/* Shows next window when tab or back-tab is pressed */ -void set_active_window(int ch) -{ - int f_inf = 0; - int max = MAX_WINDOW_SLOTS-1; - if (ch == '\t') { - int i = (active_window + 1) % max; - while (true) { - if (WINDOW_STATUS[i] != -1) { - active_window = i; - return; - } - i = (i + 1) % max; - if (f_inf++ > max) { // infinite loop check - endwin(); - exit(2); - } - } - }else { - int i = active_window - 1; - if (i < 0) i = max; - while (true) { - if (WINDOW_STATUS[i] != -1) { - active_window = i; - return; - } - if (--i < 0) i = max; - if (f_inf++ > max) { - endwin(); - exit(2); - } - } - } -} - -static void init_windows(Messenger *m) -{ - w_num = 0; - int n_prompt = 0; - int n_friendslist = 1; - if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 - || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) { - fprintf(stderr, "add_window() failed.\n"); - endwin(); - exit(1); - } - prompt = &windows[n_prompt]; -} - -static void do_tox(Messenger *m) +static void do_tox(Messenger *m, ToxWindow * prompt) { static int conn_try = 0; static int conn_err = 0; @@ -367,54 +196,8 @@ static void load_data(Messenger *m, char *path) fclose(fd); } -static void draw_bar() -{ - static int odd = 0; - int blinkrate = 30; - - attron(COLOR_PAIR(4)); - mvhline(LINES - 2, 0, '_', COLS); - attroff(COLOR_PAIR(4)); - - move(LINES - 1, 0); - - attron(COLOR_PAIR(4) | A_BOLD); - printw(" TOXIC " TOXICVER "|"); - attroff(COLOR_PAIR(4) | A_BOLD); - - int i; - for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { - if (WINDOW_STATUS[i] != -1) { - if (i == active_window) - attron(A_BOLD); - - odd = (odd+1) % blinkrate; - if (windows[i].blink && (odd < (blinkrate/2))) - attron(COLOR_PAIR(3)); - - printw(" %s", windows[i].title); - if (windows[i].blink && (odd < (blinkrate/2))) - attroff(COLOR_PAIR(3)); - - if (i == active_window) { - attroff(A_BOLD); - } - } - } - refresh(); -} - -void prepare_window(WINDOW *w) -{ - mvwin(w, 0, 0); - wresize(w, LINES-2, COLS); -} - - int main(int argc, char *argv[]) { - int ch; - ToxWindow* a; char *user_config_dir = get_user_config_dir(); char *DATA_FILE; int config_err = create_user_config_dir(user_config_dir); @@ -449,7 +232,7 @@ int main(int argc, char *argv[]) init_term(); Messenger *m = init_tox(); - init_windows(m); + ToxWindow * prompt = init_windows(m); init_window_status(); if(f_loadfromfile) @@ -471,21 +254,10 @@ int main(int argc, char *argv[]) } while(true) { /* Update tox */ - do_tox(m); + do_tox(m, prompt); /* Draw */ - a = &windows[active_window]; - prepare_window(a->window); - a->blink = false; - draw_bar(); - a->onDraw(a); - - /* Handle input */ - ch = getch(); - if (ch == '\t' || ch == KEY_BTAB) - set_active_window(ch); - else if (ch != ERR) - a->onKey(a, m, ch); + draw_active_window(m); } cleanupMessenger(m); return 0; diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c new file mode 100644 index 00000000..c3952432 --- /dev/null +++ b/testing/toxic/windows.c @@ -0,0 +1,235 @@ +#include "friendlist.h" +#include "prompt.h" +#include "windows.h" + +/* Holds status of chat windows */ +char WINDOW_STATUS[MAX_WINDOW_SLOTS]; + +static int w_num; +static ToxWindow windows[MAX_WINDOW_SLOTS]; +static Messenger *m; +int active_window; + +static ToxWindow* prompt; + +/* CALLBACKS START */ +void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata) +{ + int n = add_req(public_key); + wprintw(prompt->window, "\nFriend request from:\n"); + + int i; + for (i = 0; i < KEY_SIZE_BYTES; ++i) { + wprintw(prompt->window, "%02x", public_key[i] & 0xff); + } + + wprintw(prompt->window, "\nWith the message: %s\n", data); + wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); + + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { + if (windows[i].onFriendRequest != NULL) + windows[i].onFriendRequest(&windows[i], public_key, data, length); + } +} + +void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) +{ + int i; + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { + if (windows[i].onMessage != NULL) + windows[i].onMessage(&windows[i], m, friendnumber, string, length); + } +} + +void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) +{ + int i; + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { + if (windows[i].onAction != NULL) + windows[i].onAction(&windows[i], m, friendnumber, string, length); + } +} + +void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) +{ + wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); + int i; + for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { + if (windows[i].onNickChange != NULL) + windows[i].onNickChange(&windows[i], friendnumber, string, length); + } +} + +void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) +{ + wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); + int i; + for (i=0; i= TOXWINDOWS_MAX_NUM) + return -1; + + if (LINES < 2) + return -1; + + w.window = newwin(LINES - 2, COLS, 0, 0); + if (w.window == NULL) + return -1; + + windows[n] = w; + w.onInit(&w, m); + w_num++; + active_window = n; + return n; +} + +/* Deletes window w and cleans up */ +void del_window(ToxWindow *w, int f_num) +{ + active_window = 0; // Go to prompt screen + delwin(w->window); + int i; + for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { + if (WINDOW_STATUS[i] == f_num) { + WINDOW_STATUS[i] = -1; + disable_chatwin(f_num); + break; + } + } + clear(); + refresh(); +} + +/* Shows next window when tab or back-tab is pressed */ +void set_active_window(int ch) +{ + int f_inf = 0; + int max = MAX_WINDOW_SLOTS-1; + if (ch == '\t') { + int i = (active_window + 1) % max; + while (true) { + if (WINDOW_STATUS[i] != -1) { + active_window = i; + return; + } + i = (i + 1) % max; + if (f_inf++ > max) { // infinite loop check + endwin(); + exit(2); + } + } + }else { + int i = active_window - 1; + if (i < 0) i = max; + while (true) { + if (WINDOW_STATUS[i] != -1) { + active_window = i; + return; + } + if (--i < 0) i = max; + if (f_inf++ > max) { + endwin(); + exit(2); + } + } + } +} + +void init_window_status() +{ + /* Default window values decrement from -2 */ + int i; + for (i = 0; i < N_DEFAULT_WINS; ++i) + WINDOW_STATUS[i] = -(i+2); + + int j; + for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) + WINDOW_STATUS[j] = -1; +} + +ToxWindow * init_windows() +{ + w_num = 0; + int n_prompt = 0; + int n_friendslist = 1; + if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 + || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) { + fprintf(stderr, "add_window() failed.\n"); + endwin(); + exit(1); + } + prompt = &windows[n_prompt]; + return prompt; +} + +static void draw_bar() +{ + static int odd = 0; + int blinkrate = 30; + + attron(COLOR_PAIR(4)); + mvhline(LINES - 2, 0, '_', COLS); + attroff(COLOR_PAIR(4)); + + move(LINES - 1, 0); + + attron(COLOR_PAIR(4) | A_BOLD); + printw(" TOXIC " TOXICVER "|"); + attroff(COLOR_PAIR(4) | A_BOLD); + + int i; + for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { + if (WINDOW_STATUS[i] != -1) { + if (i == active_window) + attron(A_BOLD); + + odd = (odd+1) % blinkrate; + if (windows[i].blink && (odd < (blinkrate/2))) + attron(COLOR_PAIR(3)); + + printw(" %s", windows[i].title); + if (windows[i].blink && (odd < (blinkrate/2))) + attroff(COLOR_PAIR(3)); + + if (i == active_window) { + attroff(A_BOLD); + } + } + } + refresh(); +} + +void prepare_window(WINDOW *w) +{ + mvwin(w, 0, 0); + wresize(w, LINES-2, COLS); +} + +void draw_active_window(Messenger * m) +{ + + ToxWindow *a = &windows[active_window]; + prepare_window(a->window); + a->blink = false; + draw_bar(); + a->onDraw(a); + + /* Handle input */ + int ch = getch(); + if (ch == '\t' || ch == KEY_BTAB) + set_active_window(ch); + else if (ch != ERR) + a->onKey(a, m, ch); +} diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 4eb55a99..613fa3b3 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -38,5 +38,13 @@ struct ToxWindow_ { WINDOW* window; }; +void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata); +void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void init_window_status(); +ToxWindow * init_windows(); +void draw_active_window(Messenger * m); #endif -- cgit v1.2.3 From 4946c809d75ebec360d3f34b7518fb5f0948c925 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Tue, 13 Aug 2013 12:32:09 +1200 Subject: remove temporary callbacks from refactoring --- testing/toxic/chat.c | 5 +---- testing/toxic/chat.h | 3 +-- testing/toxic/friendlist.c | 13 ++++--------- testing/toxic/friendlist.h | 6 ++---- testing/toxic/windows.c | 2 +- testing/toxic/windows.h | 3 +++ 6 files changed, 12 insertions(+), 20 deletions(-) diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 6811569b..e4da9484 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -26,8 +26,6 @@ typedef struct { WINDOW* linewin; } ChatContext; -static delWindowFn *del_window; - void print_help(ChatContext *self); void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); @@ -370,9 +368,8 @@ void print_help(ChatContext *self) wattroff(self->history, COLOR_PAIR(2)); } -ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn *f) +ToxWindow new_chat(Messenger *m, int friendnum) { - del_window = f; ToxWindow ret; memset(&ret, 0, sizeof(ret)); diff --git a/testing/toxic/chat.h b/testing/toxic/chat.h index dcd5b3ba..7599d462 100644 --- a/testing/toxic/chat.h +++ b/testing/toxic/chat.h @@ -1,7 +1,6 @@ #ifndef CHAT_H_6489PZ13 #define CHAT_H_6489PZ13 -typedef void (delWindowFn)(ToxWindow *w, int f_num); -ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn f); +ToxWindow new_chat(Messenger *m, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 5b79fdef..e7504fbf 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -10,11 +10,9 @@ #include "../../core/Messenger.h" #include "../../core/network.h" +#include "windows.h" #include "friendlist.h" -static delWindowFn *del_window; -static setActiveWindowFn *set_active_window; -static addWindowFn *add_window; static char * WINDOW_STATUS; typedef struct { @@ -53,7 +51,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num; - add_window(m, new_chat(m, num, del_window), i); + add_window(m, new_chat(m, num), i); break; } } @@ -119,7 +117,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) if (WINDOW_STATUS[i] == -1) { WINDOW_STATUS[i] = num_selected; friends[num_selected].chatwin = num_selected; - add_window(m, new_chat(m, num_selected, del_window), i); + add_window(m, new_chat(m, num_selected), i); break; } } @@ -166,10 +164,7 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m) } -ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws) { - del_window = dw; - set_active_window = saw; - add_window = aw; +ToxWindow new_friendlist(char * ws) { WINDOW_STATUS = ws; ToxWindow ret; memset(&ret, 0, sizeof(ret)); diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h index e8b93aef..c3d8dac7 100644 --- a/testing/toxic/friendlist.h +++ b/testing/toxic/friendlist.h @@ -1,12 +1,10 @@ #ifndef FRIENDLIST_H_53I41IM #define FRIENDLIST_H_53I41IM - #include "windows.h" #include "chat.h" -typedef void (setActiveWindowFn)(int ch); -typedef int (addWindowFn)(Messenger *m, ToxWindow w, int n); -ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws); + +ToxWindow new_friendlist(char * ws); int friendlist_onFriendAdded(Messenger *m, int num); void disable_chatwin(int f_num); void fix_name(uint8_t *name); diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index c3952432..d19392ac 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -165,7 +165,7 @@ ToxWindow * init_windows() int n_prompt = 0; int n_friendslist = 1; if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 - || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) { + || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 613fa3b3..37f3e9da 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -46,5 +46,8 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l void init_window_status(); ToxWindow * init_windows(); void draw_active_window(Messenger * m); +int add_window(Messenger *m, ToxWindow w, int n); +void del_window(ToxWindow *w, int f_num); +void set_active_window(int ch); #endif -- cgit v1.2.3 From 4ddd91b580edc78755531b86955c8eb828ff2ad0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 08:56:29 -0400 Subject: Fixed small compile error on my end. --- testing/toxic/main.c | 3 --- testing/toxic/windows.h | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/testing/toxic/main.c b/testing/toxic/main.c index d7b251dc..063c1483 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -24,9 +24,6 @@ #include "prompt.h" #include "friendlist.h" -#ifndef TOXICVER -#define TOXICVER "NOVER" //Use the -D flag to set this -#endif static void init_term() { diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 37f3e9da..7d81e814 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -19,6 +19,10 @@ /* maximum window slots for WINDOW_STATUS array */ #define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM +#ifndef TOXICVER +#define TOXICVER "NOVER" //Use the -D flag to set this +#endif + typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { -- cgit v1.2.3 From 0b27a0cb355a15fddbce639e66d5af2c2106526a Mon Sep 17 00:00:00 2001 From: slvr Date: Wed, 14 Aug 2013 14:26:04 +0100 Subject: Incorrect constants: s/BOXZERO/ZERO/ --- core/net_crypto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index 07c43c40..0d8ad4c7 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -81,7 +81,7 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0) return -1; - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; + uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ @@ -114,7 +114,7 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0) return -1; - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; + uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ -- cgit v1.2.3 From ee34b51c24acfcee7c155b3b056a16dded8a27bc Mon Sep 17 00:00:00 2001 From: slvr Date: Wed, 14 Aug 2013 14:29:39 +0100 Subject: s/BOXZERO/ZERO/ --- core/net_crypto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index 0d8ad4c7..fd606335 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -139,7 +139,7 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) return -1; - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; + uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ @@ -165,7 +165,7 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) return -1; - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; + uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ -- cgit v1.2.3 From a854a730ecd3b92426914d9bc30713ade92d5727 Mon Sep 17 00:00:00 2001 From: slvr Date: Wed, 14 Aug 2013 14:46:29 +0100 Subject: Reduced redundant code, added new crypto test --- auto_tests/crypto_test.c | 38 +++++++++++++++++++++++++ core/net_crypto.c | 72 +++++++++++------------------------------------- 2 files changed, 54 insertions(+), 56 deletions(-) diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index 9ac81349..8b5397e4 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -197,6 +197,43 @@ START_TEST(test_endtoend) } END_TEST +START_TEST(test_large_data) +{ + unsigned char k[crypto_box_BEFORENMBYTES]; + + unsigned char n[crypto_box_NONCEBYTES]; + + unsigned char m1[MAX_DATA_SIZE - ENCRYPTION_PADDING]; + unsigned char c1[sizeof(m1) + ENCRYPTION_PADDING]; + unsigned char m1prime[sizeof(m1)]; + + unsigned char m2[MAX_DATA_SIZE]; + unsigned char c2[sizeof(m2) + ENCRYPTION_PADDING]; + + int c1len, c2len; + int m1plen; + + //Generate random messages + rand_bytes(m1, sizeof(m1)); + rand_bytes(m2, sizeof(m2)); + rand_bytes(n, crypto_box_NONCEBYTES); + + //Generate key + rand_bytes(k, crypto_box_BEFORENMBYTES); + + c1len = encrypt_data_fast(k, n, m1, sizeof(m1), c1); + c2len = encrypt_data_fast(k, n, m2, sizeof(m2), c2); + + ck_assert_msg(c1len == sizeof(m1) + ENCRYPTION_PADDING, "Could not encrypt max size"); + ck_assert_msg(c2len == -1, "incorrectly succeeded encrypting massive size"); + + m1plen = decrypt_data_fast(k, n, c1, c1len, m1prime); + + ck_assert_msg(m1plen == sizeof(m1), "decrypted text lengths differ"); + ck_assert_msg(memcmp(m1prime, m1, sizeof(m1)) == 0, "decrypted texts differ"); +} +END_TEST + #define DEFTESTCASE(NAME) \ TCase *NAME = tcase_create(#NAME); \ tcase_add_test(NAME, test_##NAME); \ @@ -209,6 +246,7 @@ Suite* crypto_suite(void) DEFTESTCASE(known); DEFTESTCASE(fast_known); DEFTESTCASE(endtoend); + DEFTESTCASE(large_data); return s; } diff --git a/core/net_crypto.c b/core/net_crypto.c index fd606335..ab18dd63 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -71,33 +71,6 @@ uint8_t crypto_iszero(uint8_t *mem, uint32_t length) return check; // We return zero if mem is made out of zeroes. } -/* encrypts plain of length length to encrypted of length + 16 using the - public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce - return -1 if there was a problem. - return length of encrypted data if everything was fine. */ -int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, - uint8_t *plain, uint32_t length, uint8_t *encrypted) -{ - if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0) - return -1; - - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; - uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES]; - - memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ - - crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); - - /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero - apparently memcmp should not be used so we do this instead:*/ - if(crypto_iszero(temp_encrypted, crypto_box_BOXZEROBYTES) != 0) - return -1; - - /* unpad the encrypted message */ - memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); - return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; -} - /* Precomputes the shared key from their public_key and our secret_key. This way we can avoid an expensive elliptic curve scalar multiply for each encrypt/decrypt operation. @@ -129,35 +102,6 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; } -/* decrypts encrypted of length length to plain of length length - 16 using the - public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce - return -1 if there was a problem(decryption failed) - return length of plain data if everything was fine. */ -int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, - uint8_t *encrypted, uint32_t length, uint8_t *plain) -{ - if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) - return -1; - - uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; - uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0}; - - memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ - - if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, - nonce, public_key, secret_key) == -1) - return -1; - - /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero - apparently memcmp should not be used so we do this instead:*/ - if(crypto_iszero(temp_plain, crypto_box_ZEROBYTES) != 0) - return -1; - - /* unpad the plain message */ - memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES); - return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; -} - /* Fast decrypt. Depends on enc_ley from encrypt_precompute. */ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain) @@ -184,6 +128,22 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce, return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; } +int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, + uint8_t *plain, uint32_t length, uint8_t *encrypted) +{ + uint8_t k[crypto_box_BEFORENMBYTES]; + encrypt_precompute(public_key, secret_key, k); + return encrypt_data_fast(k, nonce, plain, length, encrypted); +} + +int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, + uint8_t *encrypted, uint32_t length, uint8_t *plain) +{ + uint8_t k[crypto_box_BEFORENMBYTES]; + encrypt_precompute(public_key, secret_key, k); + return decrypt_data_fast(k, nonce, encrypted, length, plain); +} + /* increment the given nonce by 1 */ static void increment_nonce(uint8_t *nonce) { -- cgit v1.2.3 From 8eb8fb2ab5f703388be8987fd6b2cdf9dbc567e9 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 09:52:03 -0400 Subject: Messenger fix from pr #461. --- core/Messenger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Messenger.c b/core/Messenger.c index e2d83ada..690a81b1 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -746,7 +746,7 @@ void doMessenger(Messenger *m) /* returns the size of the messenger data (for saving) */ uint32_t Messenger_size(Messenger *m) { - return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends; } -- cgit v1.2.3 From 5909a13ba417733d14c3f88b79f7ec60faa8cbbf Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 10:26:02 -0400 Subject: Add friend now fails if it already is in DHT. --- core/DHT.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/DHT.c b/core/DHT.c index d359076d..5eae147c 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -633,6 +633,8 @@ static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length) int DHT_addfriend(uint8_t * client_id) { + if(friend_number(client_id) != -1) /*Is friend already in DHT?*/ + return 1; Friend * temp; temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); if (temp == NULL) -- cgit v1.2.3 From 2196fb8bc8117af1536db43cba5823f355077da9 Mon Sep 17 00:00:00 2001 From: Manuel Argüelles Date: Wed, 14 Aug 2013 10:40:26 -0500 Subject: Fix segfault when using -f in toxic Call strdup() to the data file argument string to avoid segmentation fault when it is later freed. --- testing/toxic/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 063c1483..ec33c72f 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -218,7 +218,7 @@ int main(int argc, char *argv[]) else if (argv[i][0] == '-') { if (argv[i][1] == 'f') { if (argv[i + 1] != NULL) - DATA_FILE = argv[i + 1]; + DATA_FILE = strdup(argv[i + 1]); else f_flag = -1; } else if (argv[i][1] == 'n') { -- cgit v1.2.3 From 566295037f1b742c872d3750ee73a92504bdcb6b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 12:35:55 -0400 Subject: Added logo in readme to repo. --- other/tox.png | Bin 0 -> 17922 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 other/tox.png diff --git a/other/tox.png b/other/tox.png new file mode 100644 index 00000000..e3d68696 Binary files /dev/null and b/other/tox.png differ -- cgit v1.2.3 From 95664357d42d4d71a093d011124b0b1be7c1e380 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 12:38:10 -0400 Subject: Logo is now hosted on the repo. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81836b4f..d4106910 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Project Tox](https://rbt.asia/boards/g/img/0352/79/1373823047559.png "Project Tox") +![Project Tox](https://raw.github.com/irungentoo/ProjectTox-Core/master/other/tox.png "Project Tox") Project Tox, _also known as Tox_, is a FOSS (Free and Open Source Software) instant messaging application aimed to replace Skype.
With the rise of governmental monitoring programs, Tox aims to be an easy to use, all-in-one communication platform (including audio, and videochats in the future) that ensures their users full privacy and secure message delivery.

-- cgit v1.2.3 From 1b2ac061ec37504adbe29d3d9a7b6d7ef3dbab9e Mon Sep 17 00:00:00 2001 From: naxuroqa Date: Wed, 14 Aug 2013 19:21:36 +0200 Subject: Fix for another segfault/leak case when using -f arg --- testing/toxic/main.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/testing/toxic/main.c b/testing/toxic/main.c index ec33c72f..53639eb2 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -196,17 +196,8 @@ static void load_data(Messenger *m, char *path) int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); - char *DATA_FILE; - int config_err = create_user_config_dir(user_config_dir); - if(config_err) { - DATA_FILE = "data"; - } else { - DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); - strcpy(DATA_FILE, user_config_dir); - strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, "data"); - } - free(user_config_dir); + char *DATA_FILE = NULL; + int config_err = 0; /* This is broken */ int f_loadfromfile = 1; @@ -227,6 +218,19 @@ int main(int argc, char *argv[]) } } + if (DATA_FILE == NULL ) { + config_err = create_user_config_dir(user_config_dir); + if (config_err) { + DATA_FILE = strdup("data"); + } else { + DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); + strcpy(DATA_FILE, user_config_dir); + strcat(DATA_FILE, CONFIGDIR); + strcat(DATA_FILE, "data"); + } + } + free(user_config_dir); + init_term(); Messenger *m = init_tox(); ToxWindow * prompt = init_windows(m); -- cgit v1.2.3 From aa114d2778f87180eaf946f2fd3113c53d2304bc Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 14 Aug 2013 16:02:21 -0400 Subject: small bug fixes --- testing/toxic/chat.c | 2 +- testing/toxic/main.c | 5 ++--- testing/toxic/prompt.c | 2 +- testing/toxic/windows.c | 5 +++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index e4da9484..d5edf956 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -314,7 +314,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } - wprintw(ctx->history, "Your ID: %s\n", id); + wprintw(ctx->history, "%s\n", id); } else if (strcmp(ctx->line, "/close") == 0) { diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 53639eb2..d1a4be5d 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -45,7 +45,7 @@ static void init_term() refresh(); } -static Messenger * init_tox() +static Messenger *init_tox() { /* Init core */ Messenger *m = initMessenger(); @@ -199,7 +199,6 @@ int main(int argc, char *argv[]) char *DATA_FILE = NULL; int config_err = 0; - /* This is broken */ int f_loadfromfile = 1; int f_flag = 0; int i = 0; @@ -233,7 +232,7 @@ int main(int argc, char *argv[]) init_term(); Messenger *m = init_tox(); - ToxWindow * prompt = init_windows(m); + ToxWindow *prompt = init_windows(m); init_window_status(); if(f_loadfromfile) diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index fb8a5090..b84773af 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -238,7 +238,7 @@ void cmd_myid(ToxWindow *self, Messenger *m, char **args) snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } - wprintw(self->window, "Your ID: %s\n", id); + wprintw(self->window, "%s\n", id); } void cmd_nick(ToxWindow *self, Messenger *m, char **args) diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index d19392ac..aab46e99 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -159,7 +159,7 @@ void init_window_status() WINDOW_STATUS[j] = -1; } -ToxWindow * init_windows() +ToxWindow *init_windows() { w_num = 0; int n_prompt = 0; @@ -170,6 +170,7 @@ ToxWindow * init_windows() endwin(); exit(1); } + active_window = n_prompt; prompt = &windows[n_prompt]; return prompt; } @@ -217,7 +218,7 @@ void prepare_window(WINDOW *w) wresize(w, LINES-2, COLS); } -void draw_active_window(Messenger * m) +void draw_active_window(Messenger *m) { ToxWindow *a = &windows[active_window]; -- cgit v1.2.3 From 4330bfbf871e769cf1599825bfb09ba56293d6a0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 14 Aug 2013 18:26:00 -0400 Subject: Metadata collection prevention part 1 of ???. Crypto requests packets are packets that can be routed by nodes in the DHT. In this pull request I have merged both Natping requests and friend requests into one common packet (Crypto request packets). Both these packets should now only be distinguishable by the size of the data in them to an outside observer for an example on how to send and recieve data with these packets see: friend_requests.c Note that these packets are prefixed with id 32 (friend request packets) which means this change is compatible with the currently running DHT bootstrap servers. Also changed small thing in DHT_test.c --- core/DHT.c | 55 +++++++++++++++++--------------------------------- core/friend_requests.c | 44 +++++++++++++--------------------------- core/net_crypto.c | 51 ++++++++++++++++++++++++++++++++++++++++------ core/net_crypto.h | 10 ++++----- testing/DHT_test.c | 4 ++-- 5 files changed, 84 insertions(+), 80 deletions(-) diff --git a/core/DHT.c b/core/DHT.c index 5eae147c..057ee9ce 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -932,49 +932,30 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) } /* Handle a received ping request for */ -static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length) +static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length) { - if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING - || length > MAX_DATA_SIZE + ENCRYPTION_PADDING) - return 1; - - /* check if request is for us. */ - if (id_equal(packet + 1, self_public_key)) { - uint8_t public_key[crypto_box_PUBLICKEYBYTES]; - uint8_t data[MAX_DATA_SIZE]; - - int len = handle_request(public_key, data, packet, length); - if (len != sizeof(uint64_t) + 1) - return 1; - - uint64_t ping_id; - memcpy(&ping_id, data + 1, sizeof(uint64_t)); + uint64_t ping_id; + memcpy(&ping_id, packet + 1, sizeof(uint64_t)); - int friendnumber = friend_number(public_key); - if (friendnumber == -1) - return 1; + int friendnumber = friend_number(source_pubkey); + if (friendnumber == -1) + return 1; - Friend * friend = &friends_list[friendnumber]; + Friend * friend = &friends_list[friendnumber]; - if (data[0] == 0) { - /* 1 is reply */ - send_NATping(public_key, ping_id, 1); - friend->recvNATping_timestamp = unix_time(); + if (packet[0] == 0) { + /* 1 is reply */ + send_NATping(source_pubkey, ping_id, 1); + friend->recvNATping_timestamp = unix_time(); + return 0; + } else if (packet[0] == 1) { + if (friend->NATping_id == ping_id) { + friend->NATping_id = ((uint64_t)random_int() << 32) + random_int(); + friend->hole_punching = 1; return 0; - } else if (data[0] == 1) { - if (friend->NATping_id == ping_id) { - friend->NATping_id = ((uint64_t)random_int() << 32) + random_int(); - friend->hole_punching = 1; - return 0; - } } - return 1; } - - /* if request is not for us, try routing it. */ - route_packet(packet + 1, packet, length); - - return 0; + return 1; } /* Get the most common ip in the ip_portlist @@ -1082,7 +1063,7 @@ void DHT_init(void) networking_registerhandler(1, &handle_ping_response); networking_registerhandler(2, &handle_getnodes); networking_registerhandler(3, &handle_sendnodes); - networking_registerhandler(254, &handle_NATping); + cryptopacket_registerhandler(254, &handle_NATping); } void doDHT(void) diff --git a/core/friend_requests.c b/core/friend_requests.c index 8276db29..ae19ebdd 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -123,39 +123,23 @@ static int request_received(uint8_t * client_id) } -static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t length) +static int friendreq_handlepacket(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length) { - if (packet[0] == 32) { - if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || - length > MAX_DATA_SIZE + ENCRYPTION_PADDING) - return 1; - if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. - if (handle_friendrequest_isset == 0) - return 1; - - uint8_t public_key[crypto_box_PUBLICKEYBYTES]; - uint8_t data[MAX_DATA_SIZE]; - int len = handle_request(public_key, data, packet, length); - if (len == -1) - return 1; - if (len <= sizeof(nospam)) - return 1; - if (request_received(public_key)) - return 1; - if (memcmp(data, &nospam, sizeof(nospam)) != 0) - return 1; - - addto_receivedlist(public_key); - (*handle_friendrequest)(public_key, data + 4, len - 4, handle_friendrequest_userdata); - } else { /* if request is not for us, try routing it. */ - if(route_packet(packet + 1, packet, length) == length) - return 0; - } - } - return 1; + if (handle_friendrequest_isset == 0) + return 1; + if (length <= sizeof(nospam)) + return 1; + if (request_received(source_pubkey)) + return 1; + if (memcmp(packet, &nospam, sizeof(nospam)) != 0) + return 1; + + addto_receivedlist(source_pubkey); + (*handle_friendrequest)(source_pubkey, packet + 4, length - 4, handle_friendrequest_userdata); + return 0; } void friendreq_init(void) { - networking_registerhandler(32, &friendreq_handlepacket); + cryptopacket_registerhandler(32, &friendreq_handlepacket); } diff --git a/core/net_crypto.c b/core/net_crypto.c index ab18dd63..376708ab 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -221,15 +221,18 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length) returns the length of the created packet on success */ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id) { - if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) + if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING) return -1; uint8_t nonce[crypto_box_NONCEBYTES]; + uint8_t temp[MAX_DATA_SIZE]; + memcpy(temp + 1, data, length); + temp[0] = request_id; random_nonce(nonce); - int len = encrypt_data(public_key, self_secret_key, nonce, data, length, + int len = encrypt_data(public_key, self_secret_key, nonce, temp, length, 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); if (len == -1) return -1; - packet[0] = request_id; + packet[0] = 32; memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); @@ -241,7 +244,7 @@ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t in data if a friend or ping request was sent to us and returns the length of the data. packet is the request packet and length is its length return -1 if not valid request. */ -int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length) +static int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length) { if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && @@ -249,16 +252,51 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); uint8_t nonce[crypto_box_NONCEBYTES]; + uint8_t temp[MAX_DATA_SIZE]; memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, - length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); - if(len1 == -1) + length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp); + if(len1 == -1 || len1 == 0) return -1; + request_id[0] = temp[0]; + --len1; + memcpy(data, temp + 1, len1); return len1; } else return -1; } +static cryptopacket_handler_callback cryptopackethandlers[256] = {0}; + +void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb) +{ + cryptopackethandlers[byte] = cb; +} + +static int cryptopacket_handle(IP_Port source, uint8_t * packet, uint32_t length) +{ + if (packet[0] == 32) { + if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || + length > MAX_DATA_SIZE + ENCRYPTION_PADDING) + return 1; + if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. + uint8_t public_key[crypto_box_PUBLICKEYBYTES]; + uint8_t data[MAX_DATA_SIZE]; + uint8_t number; + int len = handle_request(public_key, data, &number, packet, length); + if (len == -1 || len == 0) + return 1; + if (!cryptopackethandlers[number]) return 1; + cryptopackethandlers[number](source, public_key, data, len - 1); + + } else { /* if request is not for us, try routing it. */ + if(route_packet(packet + 1, packet, length) == length) + return 0; + } + } + return 1; +} + /* Send a crypto handshake packet containing an encrypted secret nonce and session public key to peer with connection_id and public_key the packet is encrypted with a random nonce which is sent in plain text with the packet */ @@ -579,6 +617,7 @@ void initNetCrypto(void) { memset(crypto_connections, 0 ,sizeof(crypto_connections)); memset(incoming_connections, -1 ,sizeof(incoming_connections)); + networking_registerhandler(32, &cryptopacket_handle); uint32_t i; for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) crypto_connections[i].number = ~0; diff --git a/core/net_crypto.h b/core/net_crypto.h index 135e099d..570b9373 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h @@ -25,6 +25,7 @@ #define NET_CRYPTO_H #include "Lossless_UDP.h" +#include "DHT.h" #ifdef __cplusplus extern "C" { @@ -88,11 +89,10 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); returns the length of the created packet on success */ int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); -/* puts the senders public key in the request in public_key, the data from the request - in data if a friend or ping request was sent to us and returns the length of the data. - packet is the request packet and length is its length - return -1 if not valid request. */ -int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); + +typedef int (*cryptopacket_handler_callback)(IP_Port ip_port, uint8_t * source_pubkey, uint8_t *data, uint32_t len); +/* Function to call when request beginning with byte is received */ +void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb); /* Start a secure connection with other peer who has public_key and ip_port returns -1 if failure diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 350093fd..c5b367cf 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -53,10 +53,10 @@ void print_clientlist() uint32_t i, j; IP_Port p_ip; printf("___________________CLOSE________________________________\n"); - for(i = 0; i < 4; i++) { + for(i = 0; i < 32; i++) { printf("ClientID: "); for(j = 0; j < 32; j++) { - printf("%hhX", close_clientlist[i].client_id[j]); + printf("%02hhX", close_clientlist[i].client_id[j]); } p_ip = close_clientlist[i].ip_port; printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); -- cgit v1.2.3 From e9f15d3879724da62fb2cfb8d8dceb01f1286ff9 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 12:59:16 +1200 Subject: refresh windows after resize --- testing/toxic/main.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/testing/toxic/main.c b/testing/toxic/main.c index d1a4be5d..aa025a1a 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef _win32 #include @@ -25,9 +26,17 @@ #include "friendlist.h" +void on_window_resize(int sig) +{ + endwin(); + refresh(); + clear(); +} + static void init_term() { /* Setup terminal */ + signal(SIGWINCH, on_window_resize); initscr(); cbreak(); keypad(stdscr, 1); -- cgit v1.2.3 From 93b34ef9e3b4ad44b9f387b112d168b2db55b8a3 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 14:55:57 +1200 Subject: move client data struct to header file --- core/DHT.c | 11 ----------- core/DHT.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/DHT.c b/core/DHT.c index 057ee9ce..cf0b396c 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -63,17 +63,6 @@ /*----------------------------------------------------------------------------------*/ -typedef struct { - uint8_t client_id[CLIENT_ID_SIZE]; - IP_Port ip_port; - uint64_t timestamp; - uint64_t last_pinged; - - /* Returned by this node. Either our friend or us */ - IP_Port ret_ip_port; - uint64_t ret_timestamp; -} Client_data; - typedef struct { uint8_t client_id[CLIENT_ID_SIZE]; Client_data client_list[MAX_FRIEND_CLIENTS]; diff --git a/core/DHT.h b/core/DHT.h index 0e05f132..bfbe8829 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -34,6 +34,17 @@ extern "C" { /* size of the client_id in bytes */ #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES +typedef struct { + uint8_t client_id[CLIENT_ID_SIZE]; + IP_Port ip_port; + uint64_t timestamp; + uint64_t last_pinged; + + /* Returned by this node. Either our friend or us */ + IP_Port ret_ip_port; + uint64_t ret_timestamp; +} Client_data; + /* Add a new friend to the friends list client_id must be CLIENT_ID_SIZE bytes long. returns 0 if success -- cgit v1.2.3 From 6b38b704a4a36e183817323f61505130df6f1dea Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 14:45:56 +1200 Subject: provide access to the close client list --- core/DHT.c | 6 ++++++ core/DHT.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/core/DHT.c b/core/DHT.c index cf0b396c..1d13aa73 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -104,6 +104,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; /*----------------------------------------------------------------------------------*/ + +Client_data * DHT_get_close_list(void) +{ + return close_clientlist; +} + /* Compares client_id1 and client_id2 with client_id * return 0 if both are same distance * return 1 if client_id1 is closer diff --git a/core/DHT.h b/core/DHT.h index bfbe8829..00a43d76 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -45,6 +45,8 @@ typedef struct { uint64_t ret_timestamp; } Client_data; +Client_data * DHT_get_close_list(void); + /* Add a new friend to the friends list client_id must be CLIENT_ID_SIZE bytes long. returns 0 if success -- cgit v1.2.3 From 1e190c97b848ac14eeca26df114e26fd13fd5035 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 14:48:02 +1200 Subject: add dhtstatus file --- testing/toxic/CMakeLists.txt | 1 + testing/toxic/dhtstatus.c | 89 ++++++++++++++++++++++++++++++++++++++++++++ testing/toxic/dhtstatus.h | 8 ++++ 3 files changed, 98 insertions(+) create mode 100644 testing/toxic/dhtstatus.c create mode 100644 testing/toxic/dhtstatus.h diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index dd811b97..9ee7020e 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${exe_name} windows.c prompt.c friendlist.c + dhtstatus.c chat.c configdir.c) diff --git a/testing/toxic/dhtstatus.c b/testing/toxic/dhtstatus.c new file mode 100644 index 00000000..66268900 --- /dev/null +++ b/testing/toxic/dhtstatus.c @@ -0,0 +1,89 @@ +#include "dhtstatus.h" +#include "string.h" +#include "../../core/network.h" +#include "../../core/DHT.h" + +typedef uint8_t ipbuf[3*4+3+1]; +static int num_selected = 0; + +static void printip(ipbuf buf, IP ip) +{ + sprintf((char*)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]); +} + +static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key) +{ + switch(key) { + case KEY_UP: + case 'k': + if (--num_selected < 0) + num_selected = CLIENT_ID_SIZE-1; + break; + + case KEY_DOWN: + case 'j': + num_selected = (num_selected+1) % CLIENT_ID_SIZE; + break; + + case '\n': + break; + + default: + break; + } +} + +static void dhtstatus_onDraw(ToxWindow *self) +{ + Client_data * close_clientlist = DHT_get_close_list(); + curs_set(0); + werase(self->window); + + uint64_t now = unix_time(); + uint32_t i, j; + ipbuf ipbuf; + wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now); + for(i = 0; i < CLIENT_ID_SIZE; i++) { + Client_data * client = close_clientlist + i; + if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); + wprintw(self->window,"[%02i] ", i); + uint16_t port = ntohs(client->ip_port.port); + if(port) { + for(j = 0; j < 32; j++) + wprintw(self->window, "%02hhx", client->client_id[j]); + + printip(ipbuf, client->ip_port.ip); + wprintw(self->window, " %15s %5u ", ipbuf, port); + wprintw(self->window, " %3llu ", now - client->timestamp); + wprintw(self->window, " %3llu ", now - client->last_pinged); + + port = ntohs(client->ret_ip_port.port); + if(port) { + printip(ipbuf, client->ret_ip_port.ip); + wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp); + } + } + wprintw(self->window, "\n"); + if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); + } + + wrefresh(self->window); +} + +static void dhtstatus_onInit(ToxWindow *self, Messenger *m) +{ + +} + +ToxWindow new_dhtstatus() +{ + ToxWindow ret; + memset(&ret, 0, sizeof(ret)); + + ret.onKey = &dhtstatus_onKey; + ret.onDraw = &dhtstatus_onDraw; + ret.onInit = &dhtstatus_onInit; + + strcpy(ret.title, "[dht status]"); + return ret; +} diff --git a/testing/toxic/dhtstatus.h b/testing/toxic/dhtstatus.h new file mode 100644 index 00000000..2b30e5a3 --- /dev/null +++ b/testing/toxic/dhtstatus.h @@ -0,0 +1,8 @@ +#ifndef _dhtstatus_h +#define _dhtstatus_h + +#include "windows.h" + +ToxWindow new_dhtstatus(); + +#endif -- cgit v1.2.3 From bf6c04b2cafe0a132aa54c6134217a0f4369a054 Mon Sep 17 00:00:00 2001 From: Aaron Lipinski Date: Sun, 11 Aug 2013 14:49:41 +1200 Subject: add dhtstatus window --- testing/toxic/windows.c | 5 ++++- testing/toxic/windows.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index aab46e99..6de50741 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -1,5 +1,6 @@ #include "friendlist.h" #include "prompt.h" +#include "dhtstatus.h" #include "windows.h" /* Holds status of chat windows */ @@ -164,8 +165,10 @@ ToxWindow *init_windows() w_num = 0; int n_prompt = 0; int n_friendslist = 1; + int n_dhtstatus = 2; if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 - || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1) { + || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 + || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { fprintf(stderr, "add_window() failed.\n"); endwin(); exit(1); diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 7d81e814..893ccf6f 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -14,7 +14,7 @@ #define KEY_SIZE_BYTES 32 /* number of permanent default windows */ -#define N_DEFAULT_WINS 2 +#define N_DEFAULT_WINS 3 /* maximum window slots for WINDOW_STATUS array */ #define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM -- cgit v1.2.3