From f581e20233d2f67a1b0afc974900e258113bb230 Mon Sep 17 00:00:00 2001 From: Bahkuh Date: Mon, 21 Oct 2013 08:00:27 +0200 Subject: This squash should work now. --- toxcore/net_crypto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'toxcore/net_crypto.c') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b7e3ced7..14831868 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -152,7 +152,7 @@ void random_nonce(uint8_t *nonce) static uint8_t base_nonce[crypto_box_NONCEBYTES]; static uint8_t nonce_set = 0; -/*Gives a nonce guaranteed to be different from previous ones.*/ +/* Gives a nonce guaranteed to be different from previous ones.*/ void new_nonce(uint8_t *nonce) { if (nonce_set == 0) { @@ -240,7 +240,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin return 1; } -/* Ceate a request to peer. +/* Create a request to peer. * send_public_key and send_secret_key are the pub/secret keys of the sender. * recv_public_key is public key of reciever. * packet must be an array of MAX_DATA_SIZE big. -- cgit v1.2.3 From a67b4f8c6d85b5d77cbd8b04a1f7a90a4470947b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 23 Oct 2013 14:32:09 -0400 Subject: Code cleanups. --- toxcore/Messenger.c | 10 ++++----- toxcore/net_crypto.c | 60 +++++++++++++++++++++++----------------------------- toxcore/net_crypto.h | 6 ++++++ toxcore/ping.c | 18 ++++++++++++++++ toxcore/ping.h | 17 +++++++++++++++ toxcore/util.c | 17 +++++++++++++++ toxcore/util.h | 17 +++++++++++++++ 7 files changed, 107 insertions(+), 38 deletions(-) (limited to 'toxcore/net_crypto.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 2e8efeac..eb379c4c 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -714,7 +714,7 @@ IP_Port get_friend_ipport(Messenger *m, int friendnumber) int crypt_id = m->friendlist[friendnumber].crypt_connection_id; - if (is_cryptoconnected(m->net_crypto, crypt_id) != 3) + if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED) return zero; return connection_ip(m->net_crypto->lossless_udp, m->net_crypto->crypto_connections[crypt_id].number); @@ -1454,13 +1454,13 @@ void doFriends(Messenger *m) int friendok = DHT_getfriendip(m->dht, m->friendlist[i].client_id, &friendip); switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { - case 0: + case CRYPTO_CONN_NO_CONNECTION: if (friendok == 1) m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); break; - case 3: /* Connection is established. */ + case CRYPTO_CONN_ESTABLISHED: /* Connection is established. */ set_friend_status(m, i, FRIEND_ONLINE); m->friendlist[i].name_sent = 0; m->friendlist[i].userstatus_sent = 0; @@ -1468,7 +1468,7 @@ void doFriends(Messenger *m) m->friendlist[i].ping_lastrecv = temp_time; break; - case 4: + case CRYPTO_CONN_TIMED_OUT: crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); m->friendlist[i].crypt_connection_id = -1; break; @@ -1706,7 +1706,7 @@ void doFriends(Messenger *m) } } else { if (is_cryptoconnected(m->net_crypto, - m->friendlist[i].crypt_connection_id) == 4) { /* If the connection timed out, kill it. */ + m->friendlist[i].crypt_connection_id) == CRYPTO_CONN_TIMED_OUT) { /* If the connection timed out, kill it. */ crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); m->friendlist[i].crypt_connection_id = -1; set_friend_status(m, i, FRIEND_CONFIRMED); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 14831868..a9aa77f9 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -30,12 +30,6 @@ #include "net_crypto.h" -#define CONN_NO_CONNECTION 0 -#define CONN_HANDSHAKE_SENT 1 -#define CONN_NOT_CONFIRMED 2 -#define CONN_ESTABLISHED 3 -#define CONN_TIMED_OUT 4 - static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) { return (uint32_t)crypt_connection_id >= c->crypto_connections_length; @@ -174,7 +168,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) if (crypt_connection_id_not_valid(c, crypt_connection_id)) return 0; - if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) + if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) return 0; uint8_t temp_data[MAX_DATA_SIZE]; @@ -220,7 +214,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) return 0; - if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) + if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) return 0; uint8_t temp_data[MAX_DATA_SIZE]; @@ -420,7 +414,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) uint32_t i; for (i = 0; i < c->crypto_connections_length; ++i) { - if (c->crypto_connections[i].status != CONN_NO_CONNECTION) + if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) return i; } @@ -474,14 +468,14 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) c->crypto_connections[c->crypto_connections_length].number = ~0; for (i = 0; i <= c->crypto_connections_length; ++i) { - if (c->crypto_connections[i].status == CONN_NO_CONNECTION) { + if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) { int id_new = new_connection(c->lossless_udp, ip_port); if (id_new == -1) return -1; c->crypto_connections[i].number = id_new; - c->crypto_connections[i].status = CONN_HANDSHAKE_SENT; + c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT; random_nonce(c->crypto_connections[i].recv_nonce); memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); @@ -550,15 +544,15 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) if (crypt_connection_id_not_valid(c, crypt_connection_id)) return 1; - if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { - c->crypto_connections[crypt_connection_id].status = CONN_NO_CONNECTION; + if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_NO_CONNECTION) { + c->crypto_connections[crypt_connection_id].status = CRYPTO_CONN_NO_CONNECTION; kill_connection(c->lossless_udp, c->crypto_connections[crypt_connection_id].number); memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); c->crypto_connections[crypt_connection_id].number = ~0; uint32_t i; for (i = c->crypto_connections_length; i != 0; --i) { - if (c->crypto_connections[i - 1].status != CONN_NO_CONNECTION) + if (c->crypto_connections[i - 1].status != CRYPTO_CONN_NO_CONNECTION) break; } @@ -598,9 +592,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, c->crypto_connections[c->crypto_connections_length].number = ~0; for (i = 0; i <= c->crypto_connections_length; ++i) { - if (c->crypto_connections[i].status == CONN_NO_CONNECTION) { + if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) { c->crypto_connections[i].number = connection_id; - c->crypto_connections[i].status = CONN_NOT_CONFIRMED; + c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; random_nonce(c->crypto_connections[i].recv_nonce); memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); @@ -621,9 +615,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, c->crypto_connections[i].sessionsecret_key, c->crypto_connections[i].shared_key); c->crypto_connections[i].status = - CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ + CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); - c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ + c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ return i; } @@ -645,7 +639,7 @@ int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) if ((unsigned int)crypt_connection_id < c->crypto_connections_length) return c->crypto_connections[crypt_connection_id].status; - return CONN_NO_CONNECTION; + return CRYPTO_CONN_NO_CONNECTION; } void new_keys(Net_Crypto *c) @@ -678,10 +672,10 @@ static void receive_crypto(Net_Crypto *c) uint64_t temp_time = unix_time(); for (i = 0; i < c->crypto_connections_length; ++i) { - if (c->crypto_connections[i].status == CONN_NO_CONNECTION) + if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) continue; - if (c->crypto_connections[i].status == CONN_HANDSHAKE_SENT) { + if (c->crypto_connections[i].status == CRYPTO_CONN_HANDSHAKE_SENT) { uint8_t temp_data[MAX_DATA_SIZE]; uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; @@ -701,25 +695,25 @@ static void receive_crypto(Net_Crypto *c) c->crypto_connections[i].sessionsecret_key, c->crypto_connections[i].shared_key); c->crypto_connections[i].status = - CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ + CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); - c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ + c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ } else { /* This should not happen, timeout the connection if it does. */ - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } else { /* This should not happen, timeout the connection if it does. */ - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { /* This should not happen, timeout the connection if it does. */ - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } - if (c->crypto_connections[i].status == CONN_NOT_CONFIRMED) { + if (c->crypto_connections[i].status == CRYPTO_CONN_NOT_CONFIRMED) { if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 3) { uint8_t temp_data[MAX_DATA_SIZE]; uint8_t data[MAX_DATA_SIZE]; @@ -734,22 +728,22 @@ static void receive_crypto(Net_Crypto *c) encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, c->crypto_connections[i].sessionsecret_key, c->crypto_connections[i].shared_key); - c->crypto_connections[i].status = CONN_ESTABLISHED; + c->crypto_connections[i].status = CRYPTO_CONN_ESTABLISHED; c->crypto_connections[i].timeout = ~0; /* Connection is accepted. */ confirm_connection(c->lossless_udp, c->crypto_connections[i].number); } else { /* This should not happen, timeout the connection if it does. */ - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { /* This should not happen, timeout the connection if it does. */ - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } if (temp_time > c->crypto_connections[i].timeout) { - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } } @@ -788,9 +782,9 @@ static void kill_timedout(Net_Crypto *c) uint32_t i; for (i = 0; i < c->crypto_connections_length; ++i) { - if (c->crypto_connections[i].status != CONN_NO_CONNECTION + if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION && is_connected(c->lossless_udp, c->crypto_connections[i].number) == LUDP_TIMED_OUT) - c->crypto_connections[i].status = CONN_TIMED_OUT; + c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; } } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index f0141f52..0de66e98 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -30,6 +30,12 @@ #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ #define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2) +#define CRYPTO_CONN_NO_CONNECTION 0 +#define CRYPTO_CONN_HANDSHAKE_SENT 1 +#define CRYPTO_CONN_NOT_CONFIRMED 2 +#define CRYPTO_CONN_ESTABLISHED 3 +#define CRYPTO_CONN_TIMED_OUT 4 + typedef struct { uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */ uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */ diff --git a/toxcore/ping.c b/toxcore/ping.c index b9940ce1..2d0a4545 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -3,6 +3,24 @@ * * This file is donated to the Tox Project. * Copyright 2013 plutooo + * + * Copyright (C) 2013 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 . + * */ #ifdef HAVE_CONFIG_H diff --git a/toxcore/ping.h b/toxcore/ping.h index fabb1afd..32742401 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -3,6 +3,23 @@ * * This file is donated to the Tox Project. * Copyright 2013 plutooo + * + * Copyright (C) 2013 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 . */ #ifndef __PING_H__ #define __PING_H__ diff --git a/toxcore/util.c b/toxcore/util.c index a1de6392..576972b4 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -3,6 +3,23 @@ * * This file is donated to the Tox Project. * Copyright 2013 plutooo + * + * Copyright (C) 2013 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 . */ #ifdef HAVE_CONFIG_H diff --git a/toxcore/util.h b/toxcore/util.h index 13ab4792..f7b30693 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -3,6 +3,23 @@ * * This file is donated to the Tox Project. * Copyright 2013 plutooo + * + * Copyright (C) 2013 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 . */ #ifndef __UTIL_H__ -- cgit v1.2.3