summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authornotsecure <notsecure@marek.ca>2014-05-20 07:08:03 -0400
committernotsecure <notsecure@marek.ca>2014-05-20 07:08:03 -0400
commitb034be4162ac232992d625413369d6305c2a5dd7 (patch)
treebd035716f08d3c5b57cd0c338e3be66fe2cac5cd /toxcore
parent35ae6cb90eb8e79d11ba5000337413d99da6b9f9 (diff)
use LIST in TCP_server
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_server.c22
-rw-r--r--toxcore/TCP_server.h3
-rw-r--r--toxcore/list.h5
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)
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
@@ -154,6 +146,10 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
154 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter; 146 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter;
155 TCP_server->accepted_connection_array[index].last_pinged = unix_time(); 147 TCP_server->accepted_connection_array[index].last_pinged = unix_time();
156 TCP_server->accepted_connection_array[index].ping_id = 0; 148 TCP_server->accepted_connection_array[index].ping_id = 0;
149
150 if (!list_add(&TCP_server->accepted_key_list, con->public_key, index))
151 return -1;
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.h b/toxcore/list.h
index d28a767f..299c010d 100644
--- a/toxcore/list.h
+++ b/toxcore/list.h
@@ -23,6 +23,9 @@
23 * 23 *
24 */ 24 */
25 25
26#ifndef LIST_H
27#define LIST_H
28
26#include <stdlib.h> 29#include <stdlib.h>
27#include <stdint.h> 30#include <stdint.h>
28#include <string.h> 31#include <string.h>
@@ -63,3 +66,5 @@ int list_add(LIST *list, void *data, int id);
63 * 0 : failure (element not found or id does not match 66 * 0 : failure (element not found or id does not match
64 */ 67 */
65int list_remove(LIST *list, void *data, int id); 68int list_remove(LIST *list, void *data, int id);
69
70#endif