From fe66fcc7e4053a7ec7e59d22a1e4847980c90d6a Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 16:42:09 -0400 Subject: [PATCH 1/8] list Simple struct with functions to create a list which associates ids with data --- toxcore/Makefile.inc | 2 + toxcore/list.c | 178 +++++++++++++++++++++++++++++++++++++++++++ toxcore/list.h | 61 +++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 toxcore/list.c create mode 100644 toxcore/list.h diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 7c1cf66e..4278457a 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -41,6 +41,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ ../toxcore/TCP_client.c \ ../toxcore/TCP_server.h \ ../toxcore/TCP_server.c \ + ../toxcore/list.c \ + ../toxcore/list.h \ ../toxcore/misc_tools.h libtoxcore_la_CFLAGS = -I$(top_srcdir) \ diff --git a/toxcore/list.c b/toxcore/list.c new file mode 100644 index 00000000..2ca9b175 --- /dev/null +++ b/toxcore/list.c @@ -0,0 +1,178 @@ +/* list.h + * + * Simple struct with functions to create a list which associates ids with data + * -Allows for finding ids associated with data such as IPs or public keys in a short time + * -Should only be used if there are relatively few add/remove calls to the list + * + * Copyright (C) 2014 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include "list.h" + +/* Basically, the elements in the list are placed in order so that they can be searched for easily + * -each element is seen as a big-endian integer when ordering them + * -the ids array is maintained so that each id always matches + * -the search algorithm cuts down the time to find the id associated with a piece of data + * at the cost of slow add/remove functions for large lists + * -Starts at 1/2 of the array, compares the element in the array with the data, + * then moves +/- 1/4 of the array depending on whether the value is greater or lower, + * then +- 1/8, etc, until the value is matched or its position where it should be in the array is found + * -some considerations since the array size is never perfect + */ + + #define INDEX(i) (-i -1) + +/* Find data in list + * + * return value: + * >= 0 : id associated with data + * < 0 : no match, returns index (return value is INDEX(index)) where + * the data should be inserted + */ +static int find(LIST *list, void *data) +{ + //should work well, but could be improved + if(list->n == 0) { + return INDEX(0); + } + + uint32_t i = list->n / 2; //current position in the array + uint32_t delta = i / 2; //how much we move in the array + + int d = -1; //used to determine if closest match is found + //closest match is found if we move back to where we have already been + + while(1) { + int r = memcmp(data, list->data + list->size * i, list->size); + if(r == 0) { + return list->ids[i]; + } + + if(r > 0) { + //data is greater + //move down + i += delta; + + if(d == 0 || i == list->n) { + //reached bottom of list, or closest match + return INDEX(i); + } + + delta = (delta) / 2; + if(delta == 0) + { + delta = 1; + d = 1; + } + } else { + //data is smaller + if(d == 1 || i == 0) { + //reached top or list or closest match + return INDEX(i); + } + + //move up + i -= delta; + + delta = (delta) / 2; + if(delta == 0) { + delta = 1; + d = 0; + } + } + } +} + + +void list_init(LIST *list, uint32_t element_size) +{ + //set initial values + list->n = 0; + list->size = element_size; + list->data = NULL; + list->ids = NULL; +} + +void list_free(LIST *list) +{ + //free both arrays + free(list->data); + free(list->ids); +} + +int list_find(LIST *list, void *data) +{ + int r = find(list, data); + //return only -1 and positive values + if(r < 0) { + r = -1; + } + + return r; +} + +int list_add(LIST *list, void *data, int id) +{ + //find where the new element should be inserted + //see: return value of find() + int i = find(list, data); + if(i >= 0) { + //already in list + return 0; + } + + i = -i - 1; + + //increase the size of the arrays by one + list->data = realloc(list->data, list->size * (list->n + 1)); + list->ids = realloc(list->ids, sizeof(int) * (list->n + 1)); + + //insert data to element array + memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size); + memcpy(list->data + i * list->size, data, list->size); + + //insert id to id array + memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); + list->ids[i] = id; + + //increase n + list->n++; + + return 1; +} + +void list_remove(LIST *list, int id) +{ + int i; + for(i = 0; i < list->n; i++) { + if(list->ids[i] == id) { + //decrease number of elements + list->n--; + + //move elements in both arrays down by one + memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); + memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); + + //return causes it to only remove the first element with the specified id + //(as opposed to all elements with that id if there are more than one - but there normally should not be) + //i--; + return; + } + } +} diff --git a/toxcore/list.h b/toxcore/list.h new file mode 100644 index 00000000..1fbea7ad --- /dev/null +++ b/toxcore/list.h @@ -0,0 +1,61 @@ +/* list.h + * + * Simple struct with functions to create a list which associates ids with data + * -Allows for finding ids associated with data such as IPs or public keys in a short time + * -Should only be used if there are relatively few add/remove calls to the list + * + * Copyright (C) 2014 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include +#include +#include + +typedef struct +{ + uint32_t n; //number of elements + uint32_t size; //size of the elements + void *data; //array of elements + int *ids; //array of element ids +}LIST; + +/* Initialize a list, element_size is the size of the elements in the list */ +void list_init(LIST *list, uint32_t element_size); + +/* Free a list initiated with list_init */ +void list_free(LIST *list); + +/* Retrieve the id of an element in the list + * + * return value: + * >= 0 : id associated with data + * -1 : failure + */ +int list_find(LIST *list, void *data); + +/* Add an element with associated id to the list + * + * return value: + * 1 : success + * 0 : failure (data already in list) + */ +int list_add(LIST *list, void *data, int id); + +/* Remove an element from the list */ +void list_remove(LIST *list, int id); From 410294da4882fa7e66e0acc8e323e00dcb2618f9 Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 17:53:29 -0400 Subject: [PATCH 2/8] style, failure check on realloc --- toxcore/list.c | 10 +++++++--- toxcore/list.h | 5 ++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/toxcore/list.c b/toxcore/list.c index 2ca9b175..2494016b 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -36,7 +36,7 @@ * -some considerations since the array size is never perfect */ - #define INDEX(i) (-i -1) +#define INDEX(i) (-i -1) /* Find data in list * @@ -75,8 +75,7 @@ static int find(LIST *list, void *data) } delta = (delta) / 2; - if(delta == 0) - { + if(delta == 0) { delta = 1; d = 1; } @@ -143,6 +142,11 @@ int list_add(LIST *list, void *data, int id) list->data = realloc(list->data, list->size * (list->n + 1)); list->ids = realloc(list->ids, sizeof(int) * (list->n + 1)); + if(!list->data || !list->ids) + { + return 0; + } + //insert data to element array memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size); memcpy(list->data + i * list->size, data, list->size); diff --git a/toxcore/list.h b/toxcore/list.h index 1fbea7ad..ac22435b 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -27,13 +27,12 @@ #include #include -typedef struct -{ +typedef struct { uint32_t n; //number of elements uint32_t size; //size of the elements void *data; //array of elements int *ids; //array of element ids -}LIST; +} LIST; /* Initialize a list, element_size is the size of the elements in the list */ void list_init(LIST *list, uint32_t element_size); From 9ae2fde0b9326f600571e3d0e30f442e51e68e1e Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 18:55:24 -0400 Subject: [PATCH 3/8] style (again) --- toxcore/list.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/toxcore/list.c b/toxcore/list.c index 2494016b..4d83c7be 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -48,7 +48,7 @@ static int find(LIST *list, void *data) { //should work well, but could be improved - if(list->n == 0) { + if (list->n == 0) { return INDEX(0); } @@ -58,30 +58,32 @@ static int find(LIST *list, void *data) int d = -1; //used to determine if closest match is found //closest match is found if we move back to where we have already been - while(1) { + while (1) { int r = memcmp(data, list->data + list->size * i, list->size); - if(r == 0) { + + if (r == 0) { return list->ids[i]; } - if(r > 0) { + if (r > 0) { //data is greater //move down i += delta; - if(d == 0 || i == list->n) { + if (d == 0 || i == list->n) { //reached bottom of list, or closest match return INDEX(i); } delta = (delta) / 2; - if(delta == 0) { + + if (delta == 0) { delta = 1; d = 1; } } else { //data is smaller - if(d == 1 || i == 0) { + if (d == 1 || i == 0) { //reached top or list or closest match return INDEX(i); } @@ -90,7 +92,8 @@ static int find(LIST *list, void *data) i -= delta; delta = (delta) / 2; - if(delta == 0) { + + if (delta == 0) { delta = 1; d = 0; } @@ -118,8 +121,9 @@ void list_free(LIST *list) int list_find(LIST *list, void *data) { int r = find(list, data); + //return only -1 and positive values - if(r < 0) { + if (r < 0) { r = -1; } @@ -131,7 +135,8 @@ int list_add(LIST *list, void *data, int id) //find where the new element should be inserted //see: return value of find() int i = find(list, data); - if(i >= 0) { + + if (i >= 0) { //already in list return 0; } @@ -142,8 +147,7 @@ int list_add(LIST *list, void *data, int id) list->data = realloc(list->data, list->size * (list->n + 1)); list->ids = realloc(list->ids, sizeof(int) * (list->n + 1)); - if(!list->data || !list->ids) - { + if (!list->data || !list->ids) { return 0; } @@ -164,8 +168,9 @@ int list_add(LIST *list, void *data, int id) void list_remove(LIST *list, int id) { int i; - for(i = 0; i < list->n; i++) { - if(list->ids[i] == id) { + + for (i = 0; i < list->n; i++) { + if (list->ids[i] == id) { //decrease number of elements list->n--; From 1810d1e20e0755e17e6be81a6ad4ea0d8d0d2e27 Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 19:07:47 -0400 Subject: [PATCH 4/8] proper realloc failure check --- toxcore/list.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/toxcore/list.c b/toxcore/list.c index 4d83c7be..6bd6230c 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -144,11 +144,22 @@ int list_add(LIST *list, void *data, int id) i = -i - 1; //increase the size of the arrays by one - list->data = realloc(list->data, list->size * (list->n + 1)); - list->ids = realloc(list->ids, sizeof(int) * (list->n + 1)); + void *p; - if (!list->data || !list->ids) { + p = realloc(list->data, list->size * (list->n + 1)); + + if (!p) { return 0; + } else { + list->data = p; + } + + p = realloc(list->ids, sizeof(int) * (list->n + 1)); + + if (!p) { + return 0; + } else { + list->ids = p; } //insert data to element array From d65ea9bc8dcb6cb0bab7617a8076bc3a189f2fb9 Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 20 May 2014 06:30:15 -0400 Subject: [PATCH 5/8] equivalent bitwise not --- toxcore/list.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toxcore/list.c b/toxcore/list.c index 6bd6230c..b5a3b032 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -36,7 +36,7 @@ * -some considerations since the array size is never perfect */ -#define INDEX(i) (-i -1) +#define INDEX(i) (~i) /* Find data in list * @@ -141,7 +141,7 @@ int list_add(LIST *list, void *data, int id) return 0; } - i = -i - 1; + i = ~i; //increase the size of the arrays by one void *p; From 35ae6cb90eb8e79d11ba5000337413d99da6b9f9 Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 20 May 2014 07:04:41 -0400 Subject: [PATCH 6/8] list takes data to remove faster --- toxcore/list.c | 30 ++++++++++++++---------------- toxcore/list.h | 9 +++++++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/toxcore/list.c b/toxcore/list.c index b5a3b032..2b21eec8 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -176,23 +176,21 @@ int list_add(LIST *list, void *data, int id) return 1; } -void list_remove(LIST *list, int id) +int list_remove(LIST *list, void *data, int id) { - int i; + int i = find(list, data); - for (i = 0; i < list->n; i++) { - if (list->ids[i] == id) { - //decrease number of elements - list->n--; - - //move elements in both arrays down by one - memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); - memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); - - //return causes it to only remove the first element with the specified id - //(as opposed to all elements with that id if there are more than one - but there normally should not be) - //i--; - return; - } + if (i < 0) { + return 0; } + + if (list->ids[i] != id) { + //this should never happen + return 0; + } + + memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); + memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); + + return 1; } diff --git a/toxcore/list.h b/toxcore/list.h index ac22435b..d28a767f 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -56,5 +56,10 @@ int list_find(LIST *list, void *data); */ int list_add(LIST *list, void *data, int id); -/* Remove an element from the list */ -void list_remove(LIST *list, int id); +/* Remove element from the list + * + * return value: + * 1 : success + * 0 : failure (element not found or id does not match + */ +int list_remove(LIST *list, void *data, int id); From b034be4162ac232992d625413369d6305c2a5dd7 Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 20 May 2014 07:08:03 -0400 Subject: [PATCH 7/8] use LIST in TCP_server --- toxcore/TCP_server.c | 22 +++++++++++++--------- toxcore/TCP_server.h | 3 +++ toxcore/list.h | 5 +++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index ceab5f10..fe912f94 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -99,15 +99,7 @@ static int realloc_connection(TCP_Server *TCP_server, uint32_t num) */ static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key) { - //TODO optimize this function. - uint32_t i; - - for (i = 0; i < TCP_server->size_accepted_connections; ++i) { - if (memcmp(TCP_server->accepted_connection_array[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) - return i; - } - - return -1; + return list_find(&TCP_server->accepted_key_list, public_key); } @@ -154,6 +146,10 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con) TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter; TCP_server->accepted_connection_array[index].last_pinged = unix_time(); TCP_server->accepted_connection_array[index].ping_id = 0; + + if (!list_add(&TCP_server->accepted_key_list, con->public_key, index)) + return -1; + return index; } @@ -170,6 +166,9 @@ static int del_accepted(TCP_Server *TCP_server, int index) if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) return -1; + if (!list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index)) + return -1; + memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection)); --TCP_server->num_accepted_connections; @@ -888,6 +887,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES); memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); + + list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES); + return temp; } @@ -1040,6 +1042,8 @@ void kill_TCP_server(TCP_Server *TCP_server) set_callback_handle_recv_1(TCP_server->onion, NULL, NULL); } + list_free(&TCP_server->accepted_key_list); + free(TCP_server->socks_listening); free(TCP_server); } diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h index fc8c234b..7fd4d976 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h @@ -25,6 +25,7 @@ #include "crypto_core.h" #include "onion.h" +#include "list.h" #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__) #define MSG_NOSIGNAL 0 @@ -110,6 +111,8 @@ typedef struct { uint32_t num_accepted_connections; uint64_t counter; + + LIST accepted_key_list; } TCP_Server; /* Create new TCP server instance. diff --git a/toxcore/list.h b/toxcore/list.h index d28a767f..299c010d 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -23,6 +23,9 @@ * */ +#ifndef LIST_H +#define LIST_H + #include #include #include @@ -63,3 +66,5 @@ int list_add(LIST *list, void *data, int id); * 0 : failure (element not found or id does not match */ int list_remove(LIST *list, void *data, int id); + +#endif From 6eae3d5ad7cb984314c6ff2b12756bbc4324af54 Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 20 May 2014 09:21:26 -0400 Subject: [PATCH 8/8] fixed some mistakes --- toxcore/TCP_server.c | 6 +++--- toxcore/list.c | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index fe912f94..a47f74e2 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -140,6 +140,9 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con) return -1; } + if (!list_add(&TCP_server->accepted_key_list, con->public_key, index)) + return -1; + memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection)); TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; ++TCP_server->num_accepted_connections; @@ -147,9 +150,6 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con) TCP_server->accepted_connection_array[index].last_pinged = unix_time(); TCP_server->accepted_connection_array[index].ping_id = 0; - if (!list_add(&TCP_server->accepted_key_list, con->public_key, index)) - return -1; - return index; } diff --git a/toxcore/list.c b/toxcore/list.c index 2b21eec8..2904d15b 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -189,6 +189,8 @@ int list_remove(LIST *list, void *data, int id) return 0; } + list->n--; + memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));