summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Makefile.inc2
-rw-r--r--toxcore/TCP_server.c22
-rw-r--r--toxcore/TCP_server.h3
-rw-r--r--toxcore/list.c198
-rw-r--r--toxcore/list.h70
5 files changed, 286 insertions, 9 deletions
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 \
41 ../toxcore/TCP_client.c \ 41 ../toxcore/TCP_client.c \
42 ../toxcore/TCP_server.h \ 42 ../toxcore/TCP_server.h \
43 ../toxcore/TCP_server.c \ 43 ../toxcore/TCP_server.c \
44 ../toxcore/list.c \
45 ../toxcore/list.h \
44 ../toxcore/misc_tools.h 46 ../toxcore/misc_tools.h
45 47
46libtoxcore_la_CFLAGS = -I$(top_srcdir) \ 48libtoxcore_la_CFLAGS = -I$(top_srcdir) \
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index ceab5f10..a47f74e2 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)
99 */ 99 */
100static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key) 100static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key)
101{ 101{
102 //TODO optimize this function. 102 return list_find(&TCP_server->accepted_key_list, public_key);
103 uint32_t i;
104
105 for (i = 0; i < TCP_server->size_accepted_connections; ++i) {
106 if (memcmp(TCP_server->accepted_connection_array[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
107 return i;
108 }
109
110 return -1;
111} 103}
112 104
113 105
@@ -148,12 +140,16 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
148 return -1; 140 return -1;
149 } 141 }
150 142
143 if (!list_add(&TCP_server->accepted_key_list, con->public_key, index))
144 return -1;
145
151 memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection)); 146 memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection));
152 TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; 147 TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED;
153 ++TCP_server->num_accepted_connections; 148 ++TCP_server->num_accepted_connections;
154 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter; 149 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter;
155 TCP_server->accepted_connection_array[index].last_pinged = unix_time(); 150 TCP_server->accepted_connection_array[index].last_pinged = unix_time();
156 TCP_server->accepted_connection_array[index].ping_id = 0; 151 TCP_server->accepted_connection_array[index].ping_id = 0;
152
157 return index; 153 return index;
158} 154}
159 155
@@ -170,6 +166,9 @@ static int del_accepted(TCP_Server *TCP_server, int index)
170 if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) 166 if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS)
171 return -1; 167 return -1;
172 168
169 if (!list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index))
170 return -1;
171
173 memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection)); 172 memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection));
174 --TCP_server->num_accepted_connections; 173 --TCP_server->num_accepted_connections;
175 174
@@ -888,6 +887,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t
888 887
889 memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES); 888 memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES);
890 memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); 889 memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES);
890
891 list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES);
892
891 return temp; 893 return temp;
892} 894}
893 895
@@ -1040,6 +1042,8 @@ void kill_TCP_server(TCP_Server *TCP_server)
1040 set_callback_handle_recv_1(TCP_server->onion, NULL, NULL); 1042 set_callback_handle_recv_1(TCP_server->onion, NULL, NULL);
1041 } 1043 }
1042 1044
1045 list_free(&TCP_server->accepted_key_list);
1046
1043 free(TCP_server->socks_listening); 1047 free(TCP_server->socks_listening);
1044 free(TCP_server); 1048 free(TCP_server);
1045} 1049}
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 @@
25 25
26#include "crypto_core.h" 26#include "crypto_core.h"
27#include "onion.h" 27#include "onion.h"
28#include "list.h"
28 29
29#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__) 30#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__)
30#define MSG_NOSIGNAL 0 31#define MSG_NOSIGNAL 0
@@ -110,6 +111,8 @@ typedef struct {
110 uint32_t num_accepted_connections; 111 uint32_t num_accepted_connections;
111 112
112 uint64_t counter; 113 uint64_t counter;
114
115 LIST accepted_key_list;
113} TCP_Server; 116} TCP_Server;
114 117
115/* Create new TCP server instance. 118/* Create new TCP server instance.
diff --git a/toxcore/list.c b/toxcore/list.c
new file mode 100644
index 00000000..2904d15b
--- /dev/null
+++ b/toxcore/list.c
@@ -0,0 +1,198 @@
1/* list.h
2 *
3 * Simple struct with functions to create a list which associates ids with data
4 * -Allows for finding ids associated with data such as IPs or public keys in a short time
5 * -Should only be used if there are relatively few add/remove calls to the list
6 *
7 * Copyright (C) 2014 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#include "list.h"
27
28/* Basically, the elements in the list are placed in order so that they can be searched for easily
29 * -each element is seen as a big-endian integer when ordering them
30 * -the ids array is maintained so that each id always matches
31 * -the search algorithm cuts down the time to find the id associated with a piece of data
32 * at the cost of slow add/remove functions for large lists
33 * -Starts at 1/2 of the array, compares the element in the array with the data,
34 * then moves +/- 1/4 of the array depending on whether the value is greater or lower,
35 * then +- 1/8, etc, until the value is matched or its position where it should be in the array is found
36 * -some considerations since the array size is never perfect
37 */
38
39#define INDEX(i) (~i)
40
41/* Find data in list
42 *
43 * return value:
44 * >= 0 : id associated with data
45 * < 0 : no match, returns index (return value is INDEX(index)) where
46 * the data should be inserted
47 */
48static int find(LIST *list, void *data)
49{
50 //should work well, but could be improved
51 if (list->n == 0) {
52 return INDEX(0);
53 }
54
55 uint32_t i = list->n / 2; //current position in the array
56 uint32_t delta = i / 2; //how much we move in the array
57
58 int d = -1; //used to determine if closest match is found
59 //closest match is found if we move back to where we have already been
60
61 while (1) {
62 int r = memcmp(data, list->data + list->size * i, list->size);
63
64 if (r == 0) {
65 return list->ids[i];
66 }
67
68 if (r > 0) {
69 //data is greater
70 //move down
71 i += delta;
72
73 if (d == 0 || i == list->n) {
74 //reached bottom of list, or closest match
75 return INDEX(i);
76 }
77
78 delta = (delta) / 2;
79
80 if (delta == 0) {
81 delta = 1;
82 d = 1;
83 }
84 } else {
85 //data is smaller
86 if (d == 1 || i == 0) {
87 //reached top or list or closest match
88 return INDEX(i);
89 }
90
91 //move up
92 i -= delta;
93
94 delta = (delta) / 2;
95
96 if (delta == 0) {
97 delta = 1;
98 d = 0;
99 }
100 }
101 }
102}
103
104
105void list_init(LIST *list, uint32_t element_size)
106{
107 //set initial values
108 list->n = 0;
109 list->size = element_size;
110 list->data = NULL;
111 list->ids = NULL;
112}
113
114void list_free(LIST *list)
115{
116 //free both arrays
117 free(list->data);
118 free(list->ids);
119}
120
121int list_find(LIST *list, void *data)
122{
123 int r = find(list, data);
124
125 //return only -1 and positive values
126 if (r < 0) {
127 r = -1;
128 }
129
130 return r;
131}
132
133int list_add(LIST *list, void *data, int id)
134{
135 //find where the new element should be inserted
136 //see: return value of find()
137 int i = find(list, data);
138
139 if (i >= 0) {
140 //already in list
141 return 0;
142 }
143
144 i = ~i;
145
146 //increase the size of the arrays by one
147 void *p;
148
149 p = realloc(list->data, list->size * (list->n + 1));
150
151 if (!p) {
152 return 0;
153 } else {
154 list->data = p;
155 }
156
157 p = realloc(list->ids, sizeof(int) * (list->n + 1));
158
159 if (!p) {
160 return 0;
161 } else {
162 list->ids = p;
163 }
164
165 //insert data to element array
166 memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size);
167 memcpy(list->data + i * list->size, data, list->size);
168
169 //insert id to id array
170 memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int));
171 list->ids[i] = id;
172
173 //increase n
174 list->n++;
175
176 return 1;
177}
178
179int list_remove(LIST *list, void *data, int id)
180{
181 int i = find(list, data);
182
183 if (i < 0) {
184 return 0;
185 }
186
187 if (list->ids[i] != id) {
188 //this should never happen
189 return 0;
190 }
191
192 list->n--;
193
194 memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size);
195 memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));
196
197 return 1;
198}
diff --git a/toxcore/list.h b/toxcore/list.h
new file mode 100644
index 00000000..299c010d
--- /dev/null
+++ b/toxcore/list.h
@@ -0,0 +1,70 @@
1/* list.h
2 *
3 * Simple struct with functions to create a list which associates ids with data
4 * -Allows for finding ids associated with data such as IPs or public keys in a short time
5 * -Should only be used if there are relatively few add/remove calls to the list
6 *
7 * Copyright (C) 2014 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#ifndef LIST_H
27#define LIST_H
28
29#include <stdlib.h>
30#include <stdint.h>
31#include <string.h>
32
33typedef struct {
34 uint32_t n; //number of elements
35 uint32_t size; //size of the elements
36 void *data; //array of elements
37 int *ids; //array of element ids
38} LIST;
39
40/* Initialize a list, element_size is the size of the elements in the list */
41void list_init(LIST *list, uint32_t element_size);
42
43/* Free a list initiated with list_init */
44void list_free(LIST *list);
45
46/* Retrieve the id of an element in the list
47 *
48 * return value:
49 * >= 0 : id associated with data
50 * -1 : failure
51 */
52int list_find(LIST *list, void *data);
53
54/* Add an element with associated id to the list
55 *
56 * return value:
57 * 1 : success
58 * 0 : failure (data already in list)
59 */
60int list_add(LIST *list, void *data, int id);
61
62/* Remove element from the list
63 *
64 * return value:
65 * 1 : success
66 * 0 : failure (element not found or id does not match
67 */
68int list_remove(LIST *list, void *data, int id);
69
70#endif