Skip to content

Commit

Permalink
UDP can now be disabled.
Browse files Browse the repository at this point in the history
new_messenger() now takes an options struct as an argument.
  • Loading branch information
irungentoo committed Aug 14, 2014
1 parent dbab15c commit 1298932
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
4 changes: 3 additions & 1 deletion auto_tests/messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ int main(int argc, char *argv[])
bad_id = hex_string_to_bin(bad_id_str);

/* IPv6 status from global define */
m = new_messenger(TOX_ENABLE_IPV6_DEFAULT);
Messenger_Options options = {0};
options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
m = new_messenger(&options);

/* setup a default friend and friendnum */
if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)
Expand Down
4 changes: 3 additions & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ int main(int argc, char *argv[])
exit(0);
}

m = new_messenger(ipv6enabled);
Messenger_Options options = {0};
options.ipv6enabled = ipv6enabled;
m = new_messenger(&options);

if ( !m ) {
fputs("Failed to allocate messenger datastructure\n", stderr);
Expand Down
27 changes: 20 additions & 7 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1827,16 +1827,21 @@ static int handle_new_connections(void *object, New_Connection *n_c)


/* Run this at startup. */
Messenger *new_messenger(uint8_t ipv6enabled)
Messenger *new_messenger(Messenger_Options *options)
{
Messenger *m = calloc(1, sizeof(Messenger));

if ( ! m )
return NULL;

IP ip;
ip_init(&ip, ipv6enabled);
m->net = new_networking(ip, TOX_PORT_DEFAULT);
if (options->udp_disabled) {
/* this is the easiest way to completely disable UDP without changing too much code. */
m->net = calloc(1, sizeof(Networking_Core));
} else {
IP ip;
ip_init(&ip, options->ipv6enabled);
m->net = new_networking(ip, TOX_PORT_DEFAULT);
}

if (m->net == NULL) {
free(m);
Expand All @@ -1850,7 +1855,12 @@ Messenger *new_messenger(uint8_t ipv6enabled)
free(m);
return NULL;
}
m->net_crypto = new_net_crypto(m->dht, 0);

if (options->proxy_enabled) {
m->net_crypto = new_net_crypto(m->dht, &options->proxy_info);
} else {
m->net_crypto = new_net_crypto(m->dht, 0);
}

if (m->net_crypto == NULL) {
kill_networking(m->net);
Expand All @@ -1876,6 +1886,7 @@ Messenger *new_messenger(uint8_t ipv6enabled)
return NULL;
}

m->options = *options;
friendreq_init(&(m->fr), m->onion_c);
LANdiscovery_init(m->dht);
set_nospam(&(m->fr), random_int());
Expand Down Expand Up @@ -2385,9 +2396,11 @@ void do_messenger(Messenger *m)
{
unix_time_update();

networking_poll(m->net);
if (!m->options.udp_disabled) {
networking_poll(m->net);
do_DHT(m->dht);
}

do_DHT(m->dht);
do_net_crypto(m->net_crypto);
do_onion_client(m->onion_c);
do_friends(m);
Expand Down
12 changes: 10 additions & 2 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
#define PACKET_ID_LOSSLESS_RANGE_START 160
#define PACKET_ID_LOSSLESS_RANGE_SIZE 32

typedef struct {
uint8_t ipv6enabled;
uint8_t udp_disabled;
uint8_t proxy_enabled;
TCP_Proxy_Info proxy_info;
} Messenger_Options;

/* Status definitions. */
enum {
NOFRIEND,
Expand Down Expand Up @@ -252,6 +259,7 @@ typedef struct Messenger {
void (*msi_packet)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
void *msi_packet_userdata;

Messenger_Options options;
} Messenger;

/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
Expand Down Expand Up @@ -740,12 +748,12 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
* return allocated instance of Messenger on success.
* return 0 if there are problems.
*/
Messenger *new_messenger(uint8_t ipv6enabled);
Messenger *new_messenger(Messenger_Options *options);

/* Run this before closing shop
* Free all datastructures.
*/
void kill_messenger(Messenger *M);
void kill_messenger(Messenger *m);

/* The main loop that needs to be run at least 20 times per second. */
void do_messenger(Messenger *m);
Expand Down
10 changes: 9 additions & 1 deletion toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ uint64_t current_time_monotonic(void)
*/
int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint32_t length)
{
if (net->family == 0) /* Socket not initialized */
return -1;

/* socket AF_INET, but target IP NOT: can't send */
if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET))
return -1;
Expand Down Expand Up @@ -400,6 +403,9 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl

void networking_poll(Networking_Core *net)
{
if (net->family == 0) /* Socket not initialized */
return;

unix_time_update();

IP_Port ip_port;
Expand Down Expand Up @@ -628,7 +634,9 @@ Networking_Core *new_networking(IP ip, uint16_t port)
/* Function to cleanup networking stuff. */
void kill_networking(Networking_Core *net)
{
kill_sock(net->sock);
if (net->family != 0) /* Socket not initialized */
kill_sock(net->sock);

free(net);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,9 @@ uint32_t tox_do_interval(Tox *tox)
Tox *tox_new(uint8_t ipv6enabled)
{
LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL);
return new_messenger(ipv6enabled);
Messenger_Options options = {0};
options.ipv6enabled = ipv6enabled;
return new_messenger(&options);
}

/* Run this before closing shop.
Expand Down

0 comments on commit 1298932

Please sign in to comment.