Skip to content

Commit

Permalink
Updating toxcore to 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
dvor committed Mar 6, 2017
1 parent 9708030 commit 3a83142
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 201 deletions.
2 changes: 1 addition & 1 deletion toxcore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Pod::Spec.new do |s|
s.name = "toxcore"
s.version = "0.1.5"
s.version = "0.1.6"
s.summary = "Cocoapods wrapper for toxcore"
s.homepage = "https://github.com/Antidote-for-Tox/toxcore"
s.license = 'GPLv3'
Expand Down
1 change: 1 addition & 0 deletions toxcore/toxav/bwcontroller.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "../toxcore/util.h"

#include <assert.h>
#include <errno.h>

#define BWC_PACKET_ID 196
#define BWC_SEND_INTERVAL_MS 1000
Expand Down
1 change: 1 addition & 0 deletions toxcore/toxav/rtp.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../toxcore/util.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>


Expand Down
1 change: 1 addition & 0 deletions toxcore/toxav/toxav.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "../toxcore/util.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

Expand Down
92 changes: 59 additions & 33 deletions toxcore/toxcore/DHT.m
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,12 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le
if (ip_port.ip.family == AF_INET) {

if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
char ip_str[IP_NTOA_LEN];
LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", i,
ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port),
ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
ip_ntoa(&list[i].assoc4.ip_port.ip, ip_str, sizeof(ip_str)),
ntohs(list[i].assoc4.ip_port.port),
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)),
ntohs(ip_port.port));
}

if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) {
Expand All @@ -552,9 +555,12 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le
} else if (ip_port.ip.family == AF_INET6) {

if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
char ip_str[IP_NTOA_LEN];
LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", i,
ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port),
ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
ip_ntoa(&list[i].assoc6.ip_port.ip, ip_str, sizeof(ip_str)),
ntohs(list[i].assoc6.ip_port.port),
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)),
ntohs(ip_port.port));
}

if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) {
Expand Down Expand Up @@ -785,12 +791,20 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node
return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good);
}

static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE];
typedef struct {
const uint8_t *base_public_key;
Client_data entry;
} Cmp_data;

static int cmp_dht_entry(const void *a, const void *b)
{
Client_data entry1, entry2;
memcpy(&entry1, a, sizeof(Client_data));
memcpy(&entry2, b, sizeof(Client_data));
Cmp_data cmp1, cmp2;
memcpy(&cmp1, a, sizeof(Cmp_data));
memcpy(&cmp2, b, sizeof(Cmp_data));
Client_data entry1 = cmp1.entry;
Client_data entry2 = cmp2.entry;
const uint8_t *cmp_public_key = cmp1.base_public_key;

int t1 = is_timeout(entry1.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry1.assoc6.timestamp, BAD_NODE_TIMEOUT);
int t2 = is_timeout(entry2.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry2.assoc6.timestamp, BAD_NODE_TIMEOUT);

Expand Down Expand Up @@ -851,8 +865,20 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *publ

static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key)
{
memcpy(cmp_public_key, comp_public_key, CRYPTO_PUBLIC_KEY_SIZE);
qsort(list, length, sizeof(Client_data), cmp_dht_entry);
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function cmp_dht_entry can use it as the base of comparison.
Cmp_data cmp_list[length];

for (uint32_t i = 0; i < length; i++) {
cmp_list[i].base_public_key = comp_public_key;
cmp_list[i].entry = list[i];
}

qsort(cmp_list, length, sizeof(Cmp_data), cmp_dht_entry);

for (uint32_t i = 0; i < length; i++) {
list[i] = cmp_list[i].entry;
}
}

/* Replace a first bad (or empty) node with this one
Expand Down Expand Up @@ -2533,36 +2559,36 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack
{
DHT *dht = (DHT *)object;

if (packet[0] == NET_PACKET_CRYPTO) {
if (length <= CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + 1 + CRYPTO_MAC_SIZE ||
length > MAX_CRYPTO_REQUEST_SIZE + CRYPTO_MAC_SIZE) {
return 1;
}
assert(packet[0] == NET_PACKET_CRYPTO);

if (public_key_cmp(packet + 1, dht->self_public_key) == 0) { // Check if request is for us.
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t data[MAX_CRYPTO_REQUEST_SIZE];
uint8_t number;
int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length);
if (length <= CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + 1 + CRYPTO_MAC_SIZE ||
length > MAX_CRYPTO_REQUEST_SIZE + CRYPTO_MAC_SIZE) {
return 1;
}

if (len == -1 || len == 0) {
return 1;
}
if (public_key_cmp(packet + 1, dht->self_public_key) == 0) { // Check if request is for us.
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t data[MAX_CRYPTO_REQUEST_SIZE];
uint8_t number;
int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length);

if (!dht->cryptopackethandlers[number].function) {
return 1;
}
if (len == -1 || len == 0) {
return 1;
}

return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key,
data, len, userdata);
if (!dht->cryptopackethandlers[number].function) {
return 1;
}

/* If request is not for us, try routing it. */
int retval = route_packet(dht, packet + 1, packet, length);
return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key,
data, len, userdata);
}

if ((unsigned int)retval == length) {
return 0;
}
/* If request is not for us, try routing it. */
int retval = route_packet(dht, packet + 1, packet, length);

if ((unsigned int)retval == length) {
return 0;
}

return 1;
Expand Down
59 changes: 42 additions & 17 deletions toxcore/toxcore/LAN_discovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
#define MAX_INTERFACES 16


/* TODO: multiple threads might concurrently try to set these, and it isn't clear that this couldn't lead to undesirable
* behaviour. Consider storing the data in per-instance variables instead. */
static int broadcast_count = -1;
static IP_Port broadcast_ip_port[MAX_INTERFACES];
static IP_Port broadcast_ip_ports[MAX_INTERFACES];

#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)

#include <iphlpapi.h>

static void fetch_broadcast_info(uint16_t port)
{
broadcast_count = 0;

IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO);

Expand All @@ -66,6 +66,13 @@ static void fetch_broadcast_info(uint16_t port)
}
}

/* We copy these to the static variables broadcast_* only at the end of fetch_broadcast_info().
* The intention is to ensure that even if multiple threads enter fetch_broadcast_info() concurrently, only valid
* interfaces will be set to be broadcast to.
* */
int count = 0;
IP_Port ip_ports[MAX_INTERFACES];

int ret;

if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
Expand All @@ -77,16 +84,16 @@ static void fetch_broadcast_info(uint16_t port)
if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
if (gateway.family == AF_INET && subnet_mask.family == AF_INET) {
IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
IP_Port *ip_port = &ip_ports[count];
ip_port->ip.family = AF_INET;
uint32_t gateway_ip = ntohl(gateway.ip4.uint32), subnet_ip = ntohl(subnet_mask.ip4.uint32);
uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1;
ip_port->ip.ip4.uint32 = htonl(broadcast_ip);
ip_port->port = port;
broadcast_count++;
count++;

if (broadcast_count >= MAX_INTERFACES) {
return;
if (count >= MAX_INTERFACES) {
break;
}
}
}
Expand All @@ -98,6 +105,12 @@ static void fetch_broadcast_info(uint16_t port)
if (pAdapterInfo) {
free(pAdapterInfo);
}

broadcast_count = count;

for (uint32_t i = 0; i < count; i++) {
broadcast_ip_ports[i] = ip_ports[i];
}
}

#elif defined(__linux__)
Expand All @@ -109,7 +122,7 @@ static void fetch_broadcast_info(uint16_t port)
* Definitely won't work like this on Windows...
*/
broadcast_count = 0;
sock_t sock = 0;
Socket sock = 0;

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
Expand All @@ -128,14 +141,21 @@ static void fetch_broadcast_info(uint16_t port)
return;
}

/* We copy these to the static variables broadcast_* only at the end of fetch_broadcast_info().
* The intention is to ensure that even if multiple threads enter fetch_broadcast_info() concurrently, only valid
* interfaces will be set to be broadcast to.
* */
int count = 0;
IP_Port ip_ports[MAX_INTERFACES];

/* ifconf.ifc_len is set by the ioctl() to the actual length used;
* on usage of the complete array the call should be repeated with
* a larger array, not done (640kB and 16 interfaces shall be
* enough, for everybody!)
*/
int i, count = ifconf.ifc_len / sizeof(struct ifreq);
int i, n = ifconf.ifc_len / sizeof(struct ifreq);

for (i = 0; i < count; i++) {
for (i = 0; i < n; i++) {
/* there are interfaces with are incapable of broadcast */
if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
continue;
Expand All @@ -148,24 +168,29 @@ static void fetch_broadcast_info(uint16_t port)

struct sockaddr_in *sock4 = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;

if (broadcast_count >= MAX_INTERFACES) {
close(sock);
return;
if (count >= MAX_INTERFACES) {
break;
}

IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
IP_Port *ip_port = &ip_ports[count];
ip_port->ip.family = AF_INET;
ip_port->ip.ip4.in_addr = sock4->sin_addr;
get_ip4(&ip_port->ip.ip4, &sock4->sin_addr);

if (ip_port->ip.ip4.uint32 == 0) {
continue;
}

ip_port->port = port;
broadcast_count++;
count++;
}

close(sock);

broadcast_count = count;

for (uint32_t i = 0; i < count; i++) {
broadcast_ip_ports[i] = ip_ports[i];
}
}

#else // TODO(irungentoo): Other platforms?
Expand Down Expand Up @@ -196,7 +221,7 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8
int i;

for (i = 0; i < broadcast_count; i++) {
sendpacket(net, broadcast_ip_port[i], data, length);
sendpacket(net, broadcast_ip_ports[i], data, length);
}

return 1;
Expand Down
2 changes: 2 additions & 0 deletions toxcore/toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ struct Messenger {
Friend *friendlist;
uint32_t numfriends;

time_t lastdump;

uint8_t has_added_relays; // If the first connection has occurred in do_messenger
Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config

Expand Down
Loading

0 comments on commit 3a83142

Please sign in to comment.