mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
TCP_connection started.
The plan is to move some of the TCP stuff from net_crypto into it.
This commit is contained in:
parent
cf436fde12
commit
3e9c4e80f0
|
@ -46,6 +46,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
|
|||
../toxcore/TCP_client.c \
|
||||
../toxcore/TCP_server.h \
|
||||
../toxcore/TCP_server.c \
|
||||
../toxcore/TCP_connection.h \
|
||||
../toxcore/TCP_connection.c \
|
||||
../toxcore/list.c \
|
||||
../toxcore/list.h \
|
||||
../toxcore/misc_tools.h
|
||||
|
|
205
toxcore/TCP_connection.c
Normal file
205
toxcore/TCP_connection.c
Normal file
|
@ -0,0 +1,205 @@
|
|||
/* TCP_connection.c
|
||||
*
|
||||
* Handles TCP relay connections between two Tox clients.
|
||||
*
|
||||
* Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "TCP_connection.h"
|
||||
|
||||
/* Set the size of the array to num.
|
||||
*
|
||||
* return -1 if realloc fails.
|
||||
* return 0 if it succeeds.
|
||||
*/
|
||||
#define realloc_tox_array(array, element_size, num, temp_pointer) (num ? (temp_pointer = realloc(array, num * element_size), temp_pointer ? (array = temp_pointer, 0) : (-1) ) : (free(array), array = NULL, 0))
|
||||
|
||||
|
||||
/* return 1 if the connections_number is not valid.
|
||||
* return 0 if the connections_number is valid.
|
||||
*/
|
||||
static _Bool connections_number_not_valid(const TCP_Connections *tcp_c, int connections_number)
|
||||
{
|
||||
if ((unsigned int)connections_number >= tcp_c->connections_length)
|
||||
return 1;
|
||||
|
||||
if (tcp_c->connections == NULL)
|
||||
return 1;
|
||||
|
||||
if (tcp_c->connections[connections_number].status == TCP_CONN_STATUS_NONE)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* return 1 if the tcp_connections_number is not valid.
|
||||
* return 0 if the tcp_connections_number is valid.
|
||||
*/
|
||||
static _Bool tcp_connections_number_not_valid(const TCP_Connections *tcp_c, int tcp_connections_number)
|
||||
{
|
||||
if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length)
|
||||
return 1;
|
||||
|
||||
if (tcp_c->tcp_connections == NULL)
|
||||
return 1;
|
||||
|
||||
if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_STATUS_NONE)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Create a new empty connection.
|
||||
*
|
||||
* return -1 on failure.
|
||||
* return connections_number on success.
|
||||
*/
|
||||
static int create_connection(TCP_Connections *tcp_c)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < tcp_c->connections_length; ++i) {
|
||||
if (tcp_c->connections[i].status == TCP_CONN_STATUS_NONE)
|
||||
return i;
|
||||
}
|
||||
|
||||
int id = -1;
|
||||
|
||||
TCP_Connection_to *temp_pointer;
|
||||
|
||||
if (realloc_tox_array(tcp_c->connections, sizeof(TCP_Connection_to), tcp_c->connections_length + 1,
|
||||
temp_pointer) == 0) {
|
||||
id = tcp_c->connections_length;
|
||||
++tcp_c->connections_length;
|
||||
memset(&(tcp_c->connections[id]), 0, sizeof(TCP_Connection_to));
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Create a new empty tcp connection.
|
||||
*
|
||||
* return -1 on failure.
|
||||
* return tcp_connections_number on success.
|
||||
*/
|
||||
static int create_tcp_connection(TCP_Connections *tcp_c)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
|
||||
if (tcp_c->tcp_connections[i].status == TCP_CONN_STATUS_NONE)
|
||||
return i;
|
||||
}
|
||||
|
||||
int id = -1;
|
||||
|
||||
TCP_con *temp_pointer;
|
||||
|
||||
if (realloc_tox_array(tcp_c->tcp_connections, sizeof(TCP_con), tcp_c->tcp_connections_length + 1, temp_pointer) == 0) {
|
||||
id = tcp_c->tcp_connections_length;
|
||||
++tcp_c->tcp_connections_length;
|
||||
memset(&(tcp_c->tcp_connections[id]), 0, sizeof(TCP_con));
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Wipe a connection.
|
||||
*
|
||||
* return -1 on failure.
|
||||
* return 0 on success.
|
||||
*/
|
||||
static int wipe_connection(TCP_Connections *tcp_c, int connections_number)
|
||||
{
|
||||
if (connections_number_not_valid(tcp_c, connections_number))
|
||||
return -1;
|
||||
|
||||
uint32_t i;
|
||||
memset(&(tcp_c->connections[connections_number]), 0 , sizeof(TCP_Connection_to));
|
||||
|
||||
for (i = tcp_c->connections_length; i != 0; --i) {
|
||||
if (tcp_c->connections[i - 1].status != TCP_CONN_STATUS_NONE)
|
||||
break;
|
||||
}
|
||||
|
||||
if (tcp_c->connections_length != i) {
|
||||
tcp_c->connections_length = i;
|
||||
TCP_Connection_to *temp_pointer;
|
||||
realloc_tox_array(tcp_c->connections, sizeof(TCP_Connection_to), tcp_c->connections_length, temp_pointer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Wipe a connection.
|
||||
*
|
||||
* return -1 on failure.
|
||||
* return 0 on success.
|
||||
*/
|
||||
static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_number)
|
||||
{
|
||||
if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number))
|
||||
return -1;
|
||||
|
||||
uint32_t i;
|
||||
memset(&(tcp_c->tcp_connections[tcp_connections_number]), 0 , sizeof(TCP_con));
|
||||
|
||||
for (i = tcp_c->tcp_connections_length; i != 0; --i) {
|
||||
if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_STATUS_NONE)
|
||||
break;
|
||||
}
|
||||
|
||||
if (tcp_c->tcp_connections_length != i) {
|
||||
tcp_c->tcp_connections_length = i;
|
||||
TCP_con *temp_pointer;
|
||||
realloc_tox_array(tcp_c->tcp_connections, sizeof(TCP_con), tcp_c->tcp_connections_length, temp_pointer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
TCP_Connections *new_tcp_connections(DHT *dht)
|
||||
{
|
||||
if (dht == NULL)
|
||||
return NULL;
|
||||
|
||||
TCP_Connections *temp = calloc(1, sizeof(TCP_Connections));
|
||||
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
|
||||
temp->dht = dht;
|
||||
return temp;
|
||||
}
|
||||
|
||||
void do_tcp_connections(TCP_Connections *tcp_c)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void kill_tcp_connections(TCP_Connections *tcp_c)
|
||||
{
|
||||
free(tcp_c);
|
||||
}
|
||||
|
||||
|
61
toxcore/TCP_connection.h
Normal file
61
toxcore/TCP_connection.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/* TCP_connection.h
|
||||
*
|
||||
* Handles TCP relay connections between two Tox clients.
|
||||
*
|
||||
* Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TCP_CONNECTION_H
|
||||
#define TCP_CONNECTION_H
|
||||
|
||||
#include "TCP_client.h"
|
||||
|
||||
#define TCP_CONN_STATUS_NONE 0
|
||||
|
||||
typedef struct {
|
||||
uint8_t status;
|
||||
uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */
|
||||
} TCP_Connection_to;
|
||||
|
||||
typedef struct {
|
||||
uint8_t status;
|
||||
TCP_Client_Connection *connection;
|
||||
uint32_t lock_count;
|
||||
} TCP_con;
|
||||
|
||||
typedef struct {
|
||||
DHT *dht;
|
||||
|
||||
TCP_Connection_to *connections;
|
||||
uint32_t connections_length; /* Length of connections array. */
|
||||
|
||||
TCP_con *tcp_connections;
|
||||
uint32_t tcp_connections_length; /* Length of tcp_connections array. */
|
||||
|
||||
|
||||
} TCP_Connections;
|
||||
|
||||
|
||||
|
||||
TCP_Connections *new_tcp_connections(DHT *dht);
|
||||
void do_tcp_connections(TCP_Connections *tcp_c);
|
||||
void kill_tcp_connections(TCP_Connections *tcp_c);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user