Merge remote-tracking branch 'upstream/master' into Multicalls-patch

This commit is contained in:
mannol 2014-05-22 00:08:19 +02:00
commit 9db41e7582
15 changed files with 464 additions and 64 deletions

View File

@ -260,6 +260,7 @@ AC_LIBTOOL_WIN32_DLL
AC_PROG_LIBTOOL AC_PROG_LIBTOOL
WIN32=no WIN32=no
MACH=no
AC_CANONICAL_HOST AC_CANONICAL_HOST
case $host_os in case $host_os in
*mingw*) *mingw*)
@ -275,6 +276,9 @@ case $host_os in
CPPFLAGS="$CPPFLAGS -I/usr/local/include" CPPFLAGS="$CPPFLAGS -I/usr/local/include"
ADD_NACL_OBJECTS_TO_PKGCONFIG="no" ADD_NACL_OBJECTS_TO_PKGCONFIG="no"
;; ;;
darwin*)
MACH=yes
;;
esac esac
AM_CONDITIONAL(WIN32, test "x$WIN32" = "xyes") AM_CONDITIONAL(WIN32, test "x$WIN32" = "xyes")
@ -427,7 +431,7 @@ AC_C_BIGENDIAN
# Checks for library functions. # Checks for library functions.
AC_FUNC_FORK AC_FUNC_FORK
AC_CHECK_FUNCS([gettimeofday memset socket strchr malloc]) AC_CHECK_FUNCS([gettimeofday memset socket strchr malloc])
if test "x$WIN32" != "xyes"; then if (test "x$WIN32" != "xyes") && (test "x$MACH" != "xyes"); then
AC_CHECK_LIB(rt, clock_gettime, AC_CHECK_LIB(rt, clock_gettime,
[ [
RT_LIBS="-lrt" RT_LIBS="-lrt"

View File

@ -1,6 +1,6 @@
if BUILD_DHT_BOOTSTRAP_DAEMON if BUILD_DHT_BOOTSTRAP_DAEMON
noinst_PROGRAMS += tox_bootstrap_daemon bin_PROGRAMS += tox_bootstrap_daemon
tox_bootstrap_daemon_SOURCES = \ tox_bootstrap_daemon_SOURCES = \
../other/bootstrap_daemon/tox_bootstrap_daemon.c ../other/bootstrap_daemon/tox_bootstrap_daemon.c
@ -24,4 +24,4 @@ endif
EXTRA_DIST += \ EXTRA_DIST += \
$(top_srcdir)/other/bootstrap_daemon/conf \ $(top_srcdir)/other/bootstrap_daemon/conf \
$(top_srcdir)/other/bootstrap_daemon/tox_bootstrap_daemon.sh $(top_srcdir)/other/bootstrap_daemon/tox_bootstrap_daemon.sh

View File

@ -40,7 +40,7 @@ typedef void ( *MSICallback ) ( int32_t, void *arg );
* @brief Call type identifier. Also used as rtp callback prefix. * @brief Call type identifier. Also used as rtp callback prefix.
*/ */
typedef enum { typedef enum {
type_audio = 70, type_audio = 192,
type_video type_video
} MSICallType; } MSICallType;

View File

@ -500,7 +500,7 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
* @retval -1 Error occurred. * @retval -1 Error occurred.
* @retval 0 Success. * @retval 0 Success.
*/ */
int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t length ) int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
{ {
RTPSession *_session = object; RTPSession *_session = object;
RTPMessage *_msg; RTPMessage *_msg;
@ -570,11 +570,6 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l
return -1; return -1;
} }
/* Hopefully this goes well
* NOTE: Is this even used?
*/
memcpy(&_msg->from, &ip_port, sizeof(IP_Port));
/* Check if message came in late */ /* Check if message came in late */
if ( check_late_message(_session, _msg) < 0 ) { /* Not late */ if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
_session->rsequnum = _msg->header->sequnum; _session->rsequnum = _msg->header->sequnum;

View File

@ -75,7 +75,6 @@ typedef struct _RTPMessage {
uint8_t data[MAX_RTP_SIZE]; uint8_t data[MAX_RTP_SIZE];
uint32_t length; uint32_t length;
IP_Port from;
struct _RTPMessage *next; struct _RTPMessage *next;
} RTPMessage; } RTPMessage;

View File

@ -43,6 +43,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/TCP_client.c \ ../toxcore/TCP_client.c \
../toxcore/TCP_server.h \ ../toxcore/TCP_server.h \
../toxcore/TCP_server.c \ ../toxcore/TCP_server.c \
../toxcore/list.c \
../toxcore/list.h \
../toxcore/misc_tools.h ../toxcore/misc_tools.h
libtoxcore_la_CFLAGS = -I$(top_srcdir) \ libtoxcore_la_CFLAGS = -I$(top_srcdir) \

View File

@ -715,6 +715,25 @@ static int send_ping(Messenger *m, int32_t friendnumber)
return ret; return ret;
} }
static int send_relays(Messenger *m, int32_t friendnumber)
{
Node_format nodes[MAX_SHARED_RELAYS];
uint8_t data[1024];
int n, length;
n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS);
length = pack_nodes(data, sizeof(data), nodes, n);
int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length);
if (ret == 1)
m->friendlist[friendnumber].share_relays_lastsent = unix_time();
return ret;
}
static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length)
{ {
if (friend_not_valid(m, friendnumber)) if (friend_not_valid(m, friendnumber))
@ -1681,46 +1700,35 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
} }
static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port) static int handle_custom_user_packet(void *object, int friend_num, uint8_t *packet, uint16_t length)
{
uint32_t i;
for (i = 0; i < m->numonline_friends; ++i) {
if (ipport_equal(&m->online_friendlist[i].ip_port, &ip_port))
return m->online_friendlist[i].friend_num;
}
return -1;
}
static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{ {
Messenger *m = object; Messenger *m = object;
int32_t friend_num = friendnum_from_ip_port(m, source);
if (friend_num == -1) if (friend_not_valid(m, friend_num))
return 1; return 1;
if (m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function) if (m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function)
return m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function( return m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function(
m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].object, source, packet, length); m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length);
return 1; return 1;
} }
int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
void *object) int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object)
{ {
if (friend_not_valid(m, friendnumber)) if (friend_not_valid(m, friendnumber))
return -1; return -1;
if (byte < NET_PACKET_CUSTOM_RANGE_START || byte >= NET_PACKET_CUSTOM_RANGE_END) if (byte < PACKET_ID_LOSSY_RANGE_START)
return -1; return -1;
m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].function = cb; if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].object = object; return -1;
networking_registerhandler(m->net, byte, handle_custom_user_packet, m);
m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback;
m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object;
return 0; return 0;
} }
@ -1732,12 +1740,10 @@ int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, u
if (m->friendlist[friendnumber].status != FRIEND_ONLINE) if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
return -1; return -1;
IP_Port ip_port = get_friend_ipport(m, friendnumber); if (m->friendlist[friendnumber].crypt_connection_id == -1)
if (ip_port.port == 0)
return -1; return -1;
return sendpacket(m->net, ip_port, data, length); return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length);
} }
@ -1776,6 +1782,7 @@ static int handle_new_connections(void *object, New_Connection *n_c)
int id = accept_crypto_connection(m->net_crypto, n_c); int id = accept_crypto_connection(m->net_crypto, n_c);
connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id); connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id);
connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id); connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id);
connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friend_id);
m->friendlist[friend_id].crypt_connection_id = id; m->friendlist[friend_id].crypt_connection_id = id;
set_friend_status(m, friend_id, FRIEND_CONFIRMED); set_friend_status(m, friend_id, FRIEND_CONFIRMED);
return 0; return 0;
@ -2162,6 +2169,20 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
(*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata); (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata);
} }
case PACKET_ID_SHARE_RELAYS: {
Node_format nodes[MAX_SHARED_RELAYS];
int n;
if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1)
break;
int i;
for (i = 0; i < n; i++) {
add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id);
}
}
default: { default: {
break; break;
} }
@ -2187,8 +2208,8 @@ static int friend_new_connection(Messenger *m, int32_t friendnumber, uint8_t *re
m->friendlist[friendnumber].crypt_connection_id = id; m->friendlist[friendnumber].crypt_connection_id = id;
connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber); connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber);
connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber); connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber);
connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friendnumber);
return 0; return 0;
} }
/* TODO: Make this function not suck. */ /* TODO: Make this function not suck. */
@ -2276,6 +2297,10 @@ void do_friends(Messenger *m)
m->friendlist[i].crypt_connection_id = -1; m->friendlist[i].crypt_connection_id = -1;
set_friend_status(m, i, FRIEND_CONFIRMED); set_friend_status(m, i, FRIEND_CONFIRMED);
} }
if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) {
send_relays(m, i);
}
} }
} }
} }

View File

@ -41,6 +41,7 @@
/* NOTE: Packet ids below 16 must never be used. */ /* NOTE: Packet ids below 16 must never be used. */
#define PACKET_ID_ALIVE 16 #define PACKET_ID_ALIVE 16
#define PACKET_ID_SHARE_RELAYS 17
#define PACKET_ID_NICKNAME 48 #define PACKET_ID_NICKNAME 48
#define PACKET_ID_STATUSMESSAGE 49 #define PACKET_ID_STATUSMESSAGE 49
#define PACKET_ID_USERSTATUS 50 #define PACKET_ID_USERSTATUS 50
@ -59,6 +60,9 @@
/* Max number of groups we can invite someone at the same time to. */ /* Max number of groups we can invite someone at the same time to. */
#define MAX_INVITED_GROUPS 64 #define MAX_INVITED_GROUPS 64
/* Max number of tcp relays sent to friends */
#define MAX_SHARED_RELAYS 16
/* Status definitions. */ /* Status definitions. */
enum { enum {
NOFRIEND, NOFRIEND,
@ -88,9 +92,13 @@ enum {
/* Interval between the sending of ping packets. */ /* Interval between the sending of ping packets. */
#define FRIEND_PING_INTERVAL 5 #define FRIEND_PING_INTERVAL 5
/* Interval between the sending of tcp relay information */
#define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60)
/* If no packets are received from friend in this time interval, kill the connection. */ /* If no packets are received from friend in this time interval, kill the connection. */
#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2) #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2)
/* USERSTATUS - /* USERSTATUS -
* Represents userstatuses someone can have. * Represents userstatuses someone can have.
*/ */
@ -153,12 +161,16 @@ typedef struct {
uint32_t friendrequest_nospam; // The nospam number used in the friend request. uint32_t friendrequest_nospam; // The nospam number used in the friend request.
uint64_t ping_lastrecv; uint64_t ping_lastrecv;
uint64_t ping_lastsent; uint64_t ping_lastsent;
uint64_t share_relays_lastsent;
struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES];
struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES];
int invited_groups[MAX_INVITED_GROUPS]; int invited_groups[MAX_INVITED_GROUPS];
uint16_t invited_groups_num; uint16_t invited_groups_num;
Packet_Handles packethandlers[TOTAL_USERPACKETS]; struct {
int (*function)(void *object, uint8_t *data, uint32_t len);
void *object;
} packethandlers[PACKET_ID_LOSSY_RANGE_SIZE];
} Friend; } Friend;
typedef struct { typedef struct {
@ -688,8 +700,8 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
* return -1 on failure. * return -1 on failure.
* return 0 on success. * return 0 on success.
*/ */
int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
void *object); int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object);
/* High level function to send custom user packets. /* High level function to send custom user packets.
* *

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;
} }
@ -148,12 +140,16 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
return -1; 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)); memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection));
TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED;
++TCP_server->num_accepted_connections; ++TCP_server->num_accepted_connections;
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;
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.

198
toxcore/list.c Normal file
View File

@ -0,0 +1,198 @@
/* 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 <http://www.gnu.org/licenses/>.
*
*/
#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)
/* 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;
//increase the size of the arrays by one
void *p;
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
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;
}
int list_remove(LIST *list, void *data, int id)
{
int i = find(list, data);
if (i < 0) {
return 0;
}
if (list->ids[i] != id) {
//this should never happen
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));
return 1;
}

70
toxcore/list.h Normal file
View File

@ -0,0 +1,70 @@
/* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
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 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);
#endif

View File

@ -1079,7 +1079,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin
} else if (real_data[0] == PACKET_ID_KILL) { } else if (real_data[0] == PACKET_ID_KILL) {
conn->killed = 1; conn->killed = 1;
return 0; return 0;
} else { } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) {
Packet_Data dt; Packet_Data dt;
dt.time = current_time_monotonic(); dt.time = current_time_monotonic();
dt.length = real_length; dt.length = real_length;
@ -1096,6 +1096,16 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin
/* Packet counter. */ /* Packet counter. */
++conn->packet_counter; ++conn->packet_counter;
} else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START &&
real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
if (conn->connection_lossy_data_callback)
conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object,
conn->connection_lossy_data_callback_id, real_data, real_length);
set_buffer_end(&conn->recv_array, num);
} else {
return -1;
} }
if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) {
@ -2050,6 +2060,28 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
return 0; return 0;
} }
/* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
*
* The set function should return -1 on failure and 0 on success.
* Object and id will be passed to this function untouched.
*
* return -1 on failure.
* return 0 on success.
*/
int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
if (conn == 0)
return -1;
conn->connection_lossy_data_callback = connection_lossy_data_callback;
conn->connection_lossy_data_callback_object = object;
conn->connection_lossy_data_callback_id = id;
return 0;
}
/* Get the crypto connection id from the ip_port. /* Get the crypto connection id from the ip_port.
* *
* return -1 on failure. * return -1 on failure.
@ -2211,8 +2243,12 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
/* return -1 if data could not be put in packet queue. /* Sends a lossless cryptopacket.
* return positive packet number if data was put into the queue. *
* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue.
*
* The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
*/ */
int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
{ {
@ -2222,6 +2258,9 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
if (data[0] < CRYPTO_RESERVED_PACKETS) if (data[0] < CRYPTO_RESERVED_PACKETS)
return -1; return -1;
if (data[0] >= PACKET_ID_LOSSY_RANGE_START)
return -1;
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
if (conn == 0) if (conn == 0)
@ -2243,6 +2282,31 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
return ret; return ret;
} }
/* return -1 on failure.
* return 0 on success.
*
* Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
return -1;
if (data[0] < PACKET_ID_LOSSY_RANGE_START)
return -1;
if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
return -1;
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
if (conn == 0)
return -1;
return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data,
length);
}
/* Kill a crypto connection. /* Kill a crypto connection.
* *
* return -1 on failure. * return -1 on failure.

View File

@ -69,6 +69,10 @@
#define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */ #define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */
#define STATUS_TCP_ONLINE 3 #define STATUS_TCP_ONLINE 3
/* All packets starting with a byte in this range are considered lossy packets. */
#define PACKET_ID_LOSSY_RANGE_START 192
#define PACKET_ID_LOSSY_RANGE_SIZE 63
typedef struct { typedef struct {
uint64_t time; uint64_t time;
uint16_t length; uint16_t length;
@ -119,6 +123,10 @@ typedef struct {
void *connection_data_callback_object; void *connection_data_callback_object;
int connection_data_callback_id; int connection_data_callback_id;
int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length);
void *connection_lossy_data_callback_object;
int connection_lossy_data_callback_id;
uint64_t last_request_packet_sent; uint64_t last_request_packet_sent;
uint32_t packet_counter; uint32_t packet_counter;
@ -231,7 +239,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port);
int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object,
int id, uint8_t status), void *object, int id); int id, uint8_t status), void *object, int id);
/* Set function to be called when connection with crypt_connection_id receives a data packet of length. /* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length.
* *
* The set function should return -1 on failure and 0 on success. * The set function should return -1 on failure and 0 on success.
* Object and id will be passed to this function untouched. * Object and id will be passed to this function untouched.
@ -243,16 +251,38 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
int id, uint8_t *data, uint16_t length), void *object, int id); int id, uint8_t *data, uint16_t length), void *object, int id);
/* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
*
* The set function should return -1 on failure and 0 on success.
* Object and id will be passed to this function untouched.
*
* return -1 on failure.
* return 0 on success.
*/
int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id);
/* returns the number of packet slots left in the sendbuffer. /* returns the number of packet slots left in the sendbuffer.
* return 0 if failure. * return 0 if failure.
*/ */
uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id); uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id);
/* return -1 if data could not be put in packet queue. /* Sends a lossless cryptopacket.
* return positive packet number if data was put into the queue. *
* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue.
*
* The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
*/ */
int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
/* return -1 on failure.
* return 0 on success.
*
* Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
/* Add a tcp relay, associating it to a crypt_connection_id. /* Add a tcp relay, associating it to a crypt_connection_id.
* *
* return 0 if it was added. * return 0 if it was added.

View File

@ -122,12 +122,6 @@ typedef int sock_t;
#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */
#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */
/* Range of ids that custom user packets can use. */
#define NET_PACKET_CUSTOM_RANGE_START 64
#define NET_PACKET_CUSTOM_RANGE_END 96
#define TOTAL_USERPACKETS (NET_PACKET_CUSTOM_RANGE_END - NET_PACKET_CUSTOM_RANGE_START)
/* See: docs/Prevent_Tracking.txt and onion.{c, h} */ /* See: docs/Prevent_Tracking.txt and onion.{c, h} */
#define NET_PACKET_ONION_SEND_INITIAL 128 #define NET_PACKET_ONION_SEND_INITIAL 128
#define NET_PACKET_ONION_SEND_1 129 #define NET_PACKET_ONION_SEND_1 129