use LIST in TCP_server

This commit is contained in:
notsecure 2014-05-20 07:08:03 -04:00
parent 35ae6cb90e
commit b034be4162
3 changed files with 21 additions and 9 deletions

View File

@ -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) static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key)
{ {
//TODO optimize this function. return list_find(&TCP_server->accepted_key_list, public_key);
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;
} }
@ -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].identifier = ++TCP_server->counter;
TCP_server->accepted_connection_array[index].last_pinged = unix_time(); TCP_server->accepted_connection_array[index].last_pinged = unix_time();
TCP_server->accepted_connection_array[index].ping_id = 0; 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; 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) if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS)
return -1; 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)); memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection));
--TCP_server->num_accepted_connections; --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->public_key, public_key, crypto_box_PUBLICKEYBYTES);
memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES);
list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES);
return temp; return temp;
} }
@ -1040,6 +1042,8 @@ void kill_TCP_server(TCP_Server *TCP_server)
set_callback_handle_recv_1(TCP_server->onion, NULL, NULL); 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->socks_listening);
free(TCP_server); free(TCP_server);
} }

View File

@ -25,6 +25,7 @@
#include "crypto_core.h" #include "crypto_core.h"
#include "onion.h" #include "onion.h"
#include "list.h"
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__) #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__)
#define MSG_NOSIGNAL 0 #define MSG_NOSIGNAL 0
@ -110,6 +111,8 @@ typedef struct {
uint32_t num_accepted_connections; uint32_t num_accepted_connections;
uint64_t counter; uint64_t counter;
LIST accepted_key_list;
} TCP_Server; } TCP_Server;
/* Create new TCP server instance. /* Create new TCP server instance.

View File

@ -23,6 +23,9 @@
* *
*/ */
#ifndef LIST_H
#define LIST_H
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -63,3 +66,5 @@ int list_add(LIST *list, void *data, int id);
* 0 : failure (element not found or id does not match * 0 : failure (element not found or id does not match
*/ */
int list_remove(LIST *list, void *data, int id); int list_remove(LIST *list, void *data, int id);
#endif