mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Fixed comments in other files as per request of jvrv
This commit is contained in:
parent
e02620c7be
commit
5101ef756a
|
@ -28,23 +28,23 @@
|
||||||
#include "net_crypto.h"
|
#include "net_crypto.h"
|
||||||
|
|
||||||
|
|
||||||
//Our public and secret keys.
|
/* Our public and secret keys. */
|
||||||
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||||
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];//the real public key of the peer.
|
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
|
uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
|
||||||
uint8_t sent_nonce[crypto_box_NONCEBYTES];//nonce of sent packets.
|
uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
|
||||||
uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES];//our public key for this session.
|
uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */
|
||||||
uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];//our private key for this session.
|
uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];our private key for this session.
|
||||||
uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES];//The public key of the peer.
|
uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
|
||||||
uint8_t status;//0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
uint8_t status;/* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||||
//(we have received a handshake but no empty data packet), 3 if the connection is established.
|
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||||
//4 if the connection is timed out.
|
4 if the connection is timed out. */
|
||||||
uint16_t number; //Lossless_UDP connection number corresponding to this connection.
|
uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
|
||||||
|
|
||||||
}Crypto_Connection;
|
}Crypto_Connection;
|
||||||
|
|
||||||
|
@ -54,18 +54,18 @@ static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS];
|
||||||
|
|
||||||
#define MAX_FRIEND_REQUESTS 32
|
#define MAX_FRIEND_REQUESTS 32
|
||||||
|
|
||||||
//keeps track of the connection numbers for friends request so we can check later if they were sent
|
/* keeps track of the connection numbers for friends request so we can check later if they were sent */
|
||||||
static int outbound_friendrequests[MAX_FRIEND_REQUESTS];
|
static int outbound_friendrequests[MAX_FRIEND_REQUESTS];
|
||||||
|
|
||||||
#define MAX_INCOMING 64
|
#define MAX_INCOMING 64
|
||||||
|
|
||||||
//keeps track of the connection numbers for friends request so we can check later if they were sent
|
/* keeps track of the connection numbers for friends request so we can check later if they were sent */
|
||||||
static int incoming_connections[MAX_INCOMING];
|
static int incoming_connections[MAX_INCOMING];
|
||||||
|
|
||||||
//encrypts plain of length length to encrypted of length + 16 using the
|
/* encrypts plain of length length to encrypted of length + 16 using the
|
||||||
//public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
||||||
//return -1 if there was a problem.
|
return -1 if there was a problem.
|
||||||
//return length of encrypted data if everything was fine.
|
return length of encrypted data if everything was fine. */
|
||||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t * plain, uint32_t length, uint8_t * encrypted)
|
uint8_t * plain, uint32_t length, uint8_t * encrypted)
|
||||||
{
|
{
|
||||||
|
@ -78,24 +78,24 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
|
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
|
||||||
uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0};
|
uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0};
|
||||||
|
|
||||||
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);//pad the message with 32 0 bytes.
|
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
|
||||||
|
|
||||||
crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
|
crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
|
||||||
|
|
||||||
//if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero
|
/* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */
|
||||||
if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
|
if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//unpad the encrypted message
|
/* unpad the encrypted message */
|
||||||
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
|
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
|
||||||
return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
|
return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
|
||||||
}
|
}
|
||||||
|
|
||||||
//decrypts encrypted of length length to plain of length length - 16 using the
|
/* decrypts encrypted of length length to plain of length length - 16 using the
|
||||||
//public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
||||||
//return -1 if there was a problem(decryption failed)
|
return -1 if there was a problem(decryption failed)
|
||||||
//return length of plain data if everything was fine.
|
return length of plain data if everything was fine. */
|
||||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t * encrypted, uint32_t length, uint8_t * plain)
|
uint8_t * encrypted, uint32_t length, uint8_t * plain)
|
||||||
{
|
{
|
||||||
|
@ -107,30 +107,30 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
|
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
|
||||||
uint8_t zeroes[crypto_box_ZEROBYTES] = {0};
|
uint8_t zeroes[crypto_box_ZEROBYTES] = {0};
|
||||||
|
|
||||||
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);//pad the message with 16 0 bytes.
|
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
|
||||||
|
|
||||||
if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
|
if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
|
||||||
nonce, public_key, secret_key) == -1)
|
nonce, public_key, secret_key) == -1)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero
|
/* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */
|
||||||
if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
|
if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
//unpad the plain message
|
/* unpad the plain message */
|
||||||
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES);
|
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES);
|
||||||
return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
|
return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
|
||||||
}
|
}
|
||||||
|
|
||||||
//increment the given nonce by 1
|
/* increment the given nonce by 1 */
|
||||||
void increment_nonce(uint8_t * nonce)
|
void increment_nonce(uint8_t * nonce)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < crypto_box_NONCEBYTES; i++)
|
for(i = 0; i < crypto_box_NONCEBYTES; ++i)
|
||||||
{
|
{
|
||||||
nonce[i]++;
|
++nonce[i];
|
||||||
if(nonce[i] != 0)
|
if(nonce[i] != 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
@ -138,20 +138,20 @@ void increment_nonce(uint8_t * nonce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fill the given nonce with random bytes.
|
/* fill the given nonce with random bytes.
|
||||||
//TODO: make this more optimized
|
TODO: make this more optimized */
|
||||||
void random_nonce(uint8_t * nonce)
|
void random_nonce(uint8_t * nonce)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < crypto_box_NONCEBYTES; i++)
|
for(i = 0; i < crypto_box_NONCEBYTES; ++i)
|
||||||
{
|
{
|
||||||
nonce[i] = random_int() % 256;
|
nonce[i] = random_int() % 256;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//return 0 if there is no received data in the buffer
|
/* return 0 if there is no received data in the buffer
|
||||||
//return -1 if the packet was discarded.
|
return -1 if the packet was discarded.
|
||||||
//return length of received data if successful
|
return length of received data if successful */
|
||||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data)
|
int read_cryptpacket(int crypt_connection_id, uint8_t * data)
|
||||||
{
|
{
|
||||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||||
|
@ -184,8 +184,8 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//return 0 if data could not be put in packet queue
|
/* return 0 if data could not be put in packet queue
|
||||||
//return 1 if data was put into the queue
|
return 1 if data was put into the queue */
|
||||||
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
||||||
{
|
{
|
||||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||||
|
@ -217,10 +217,10 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//send a friend request to peer with public_key and ip_port.
|
/* send a friend request to peer with public_key and ip_port.
|
||||||
//Data represents the data we send with the friends request.
|
Data represents the data we send with the friends request.
|
||||||
//returns -1 on failure
|
returns -1 on failure
|
||||||
//returns a positive friend request id that can be used later to see if it was sent correctly on success.
|
returns a positive friend request id that can be used later to see if it was sent correctly on success. */
|
||||||
int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length)
|
int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||||
{
|
{
|
||||||
if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES)
|
if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES)
|
||||||
|
@ -228,7 +228,7 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_FRIEND_REQUESTS; i++)
|
for(i = 0; i < MAX_FRIEND_REQUESTS; ++i)
|
||||||
{
|
{
|
||||||
if(outbound_friendrequests[i] == -1)
|
if(outbound_friendrequests[i] == -1)
|
||||||
{
|
{
|
||||||
|
@ -264,10 +264,10 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return -1 if failure
|
/* return -1 if failure
|
||||||
//return 0 if connection is still trying to send the request.
|
return 0 if connection is still trying to send the request.
|
||||||
//return 1 if sent correctly
|
return 1 if sent correctly
|
||||||
//return 2 if connection timed out
|
return 2 if connection timed out */
|
||||||
int check_friendrequest(int friend_request)
|
int check_friendrequest(int friend_request)
|
||||||
{
|
{
|
||||||
if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS)
|
if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS)
|
||||||
|
@ -299,9 +299,9 @@ int check_friendrequest(int friend_request)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Send a crypto handshake packet containing an encrypted secret nonce and session public key
|
/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
|
||||||
//to peer with connection_id and public_key
|
to peer with connection_id and public_key
|
||||||
//the packet is encrypted with a random nonce which is sent in plain text with the packet
|
the packet is encrypted with a random nonce which is sent in plain text with the packet */
|
||||||
int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||||
{
|
{
|
||||||
uint8_t temp_data[MAX_DATA_SIZE];
|
uint8_t temp_data[MAX_DATA_SIZE];
|
||||||
|
@ -324,9 +324,9 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
|
||||||
return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Extract secret nonce, session public key and public_key from a packet(data) with length length
|
/* Extract secret nonce, session public key and public_key from a packet(data) with length length
|
||||||
//return 1 if successful
|
return 1 if successful
|
||||||
//return 0 if failure
|
return 0 if failure */
|
||||||
int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
||||||
uint8_t * session_key, uint8_t * data, uint16_t length)
|
uint8_t * session_key, uint8_t * data, uint16_t length)
|
||||||
{
|
{
|
||||||
|
@ -359,13 +359,13 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//puts the public key of the friend if public_key, the data from the request
|
/* puts the public key of the friend if public_key, the data from the request
|
||||||
//in data if a friend request was sent to us and returns the length of the data.
|
in data if a friend request was sent to us and returns the length of the data.
|
||||||
//return -1 if no valid friend requests.
|
return -1 if no valid friend requests. */
|
||||||
int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_INCOMING; i++)
|
for(i = 0; i < MAX_INCOMING; ++i)
|
||||||
{
|
{
|
||||||
if(incoming_connections[i] != -1)
|
if(incoming_connections[i] != -1)
|
||||||
{
|
{
|
||||||
|
@ -384,12 +384,12 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
||||||
if(len1 != -1)
|
if(len1 != -1)
|
||||||
{
|
{
|
||||||
kill_connection(incoming_connections[i]);
|
kill_connection(incoming_connections[i]);
|
||||||
//kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds
|
/* kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds */
|
||||||
incoming_connections[i] = -1;
|
incoming_connections[i] = -1;
|
||||||
return len1;
|
return len1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
kill_connection(incoming_connections[i]); //conection is useless now, kill it.
|
kill_connection(incoming_connections[i]); /* conection is useless now, kill it. */
|
||||||
incoming_connections[i] = -1;
|
incoming_connections[i] = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -397,13 +397,13 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get crypto connection id from public key of peer
|
/* get crypto connection id from public key of peer
|
||||||
//return -1 if there are no connections like we are looking for
|
return -1 if there are no connections like we are looking for
|
||||||
//return id if it found it
|
return id if it found it */
|
||||||
int getcryptconnection_id(uint8_t * public_key)
|
int getcryptconnection_id(uint8_t * public_key)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
if(crypto_connections[i].status > 0)
|
if(crypto_connections[i].status > 0)
|
||||||
{
|
{
|
||||||
|
@ -417,9 +417,9 @@ int getcryptconnection_id(uint8_t * public_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Start a secure connection with other peer who has public_key and ip_port
|
/* Start a secure connection with other peer who has public_key and ip_port
|
||||||
//returns -1 if failure
|
returns -1 if failure
|
||||||
//returns crypt_connection_id of the initialized connection if everything went well.
|
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -432,7 +432,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
if(crypto_connections[i].status == 0)
|
if(crypto_connections[i].status == 0)
|
||||||
{
|
{
|
||||||
|
@ -453,23 +453,23 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
||||||
increment_nonce(crypto_connections[i].recv_nonce);
|
increment_nonce(crypto_connections[i].recv_nonce);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;//this should never happen.
|
return -1; /* this should never happen. */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle an incoming connection
|
/* handle an incoming connection
|
||||||
//return -1 if no crypto inbound connection
|
return -1 if no crypto inbound connection
|
||||||
//return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
|
return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
|
||||||
//Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
|
Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
|
||||||
//and the session public key for the connection in session_key
|
and the session public key for the connection in session_key
|
||||||
//to accept it see: accept_crypto_inbound(...)
|
to accept it see: accept_crypto_inbound(...)
|
||||||
//to refuse it just call kill_connection(...) on the connection id
|
to refuse it just call kill_connection(...) on the connection id */
|
||||||
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_INCOMING; i++)
|
for(i = 0; i < MAX_INCOMING; ++i)
|
||||||
{
|
{
|
||||||
if(incoming_connections[i] != -1)
|
if(incoming_connections[i] != -1)
|
||||||
{
|
{
|
||||||
|
@ -486,7 +486,7 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
|
||||||
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
|
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
|
||||||
{
|
{
|
||||||
int connection_id = incoming_connections[i];
|
int connection_id = incoming_connections[i];
|
||||||
incoming_connections[i] = -1;//remove this connection from the incoming connection list.
|
incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
|
||||||
return connection_id;
|
return connection_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -495,9 +495,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//kill a crypto connection
|
/* kill a crypto connection
|
||||||
//return 0 if killed successfully
|
return 0 if killed successfully
|
||||||
//return 1 if there was a problem.
|
return 1 if there was a problem. */
|
||||||
int crypto_kill(int crypt_connection_id)
|
int crypto_kill(int crypt_connection_id)
|
||||||
{
|
{
|
||||||
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||||
|
@ -515,9 +515,9 @@ int crypto_kill(int crypt_connection_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//accept an incoming connection using the parameters provided by crypto_inbound
|
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||||
//return -1 if not successful
|
return -1 if not successful
|
||||||
//returns the crypt_connection_id if successful
|
returns the crypt_connection_id if successful */
|
||||||
int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -530,7 +530,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}*/
|
}*/
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
if(crypto_connections[i].status == 0)
|
if(crypto_connections[i].status == 0)
|
||||||
{
|
{
|
||||||
|
@ -549,20 +549,20 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
|
||||||
{
|
{
|
||||||
increment_nonce(crypto_connections[i].recv_nonce);
|
increment_nonce(crypto_connections[i].recv_nonce);
|
||||||
uint32_t zero = 0;
|
uint32_t zero = 0;
|
||||||
crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work
|
crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||||
write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
|
write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
|
||||||
crypto_connections[i].status = 2;//set it to its proper value right after.
|
crypto_connections[i].status = 2; /* set it to its proper value right after. */
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;//this should never happen.
|
return -1; /* this should never happen. */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||||
//(we have received a handshake but no empty data packet), 3 if the connection is established.
|
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||||
//4 if the connection is timed out and waiting to be killed
|
4 if the connection is timed out and waiting to be killed */
|
||||||
int is_cryptoconnected(int crypt_connection_id)
|
int is_cryptoconnected(int crypt_connection_id)
|
||||||
{
|
{
|
||||||
if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
||||||
|
@ -573,37 +573,37 @@ int is_cryptoconnected(int crypt_connection_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Generate our public and private keys
|
/* Generate our public and private keys
|
||||||
//Only call this function the first time the program starts.
|
Only call this function the first time the program starts. */
|
||||||
void new_keys()
|
void new_keys()
|
||||||
{
|
{
|
||||||
crypto_box_keypair(self_public_key,self_secret_key);
|
crypto_box_keypair(self_public_key,self_secret_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
//save the public and private keys to the keys array
|
/* save the public and private keys to the keys array
|
||||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||||
void save_keys(uint8_t * keys)
|
void save_keys(uint8_t * keys)
|
||||||
{
|
{
|
||||||
memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
|
memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||||
memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
|
memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
//load the public and private keys from the keys array
|
/* load the public and private keys from the keys array
|
||||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||||
void load_keys(uint8_t * keys)
|
void load_keys(uint8_t * keys)
|
||||||
{
|
{
|
||||||
memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
|
memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
|
||||||
memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
|
memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: optimize this
|
/* TODO: optimize this
|
||||||
//adds an incoming connection to the incoming_connection list.
|
adds an incoming connection to the incoming_connection list.
|
||||||
//returns 0 if successful
|
returns 0 if successful
|
||||||
//returns 1 if failure
|
returns 1 if failure */
|
||||||
int new_incoming(int id)
|
int new_incoming(int id)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_INCOMING; i++)
|
for(i = 0; i < MAX_INCOMING; ++i)
|
||||||
{
|
{
|
||||||
if(incoming_connections[i] == -1)
|
if(incoming_connections[i] == -1)
|
||||||
{
|
{
|
||||||
|
@ -614,8 +614,8 @@ int new_incoming(int id)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: optimize this
|
/* TODO: optimize this
|
||||||
//handle all new incoming connections.
|
handle all new incoming connections. */
|
||||||
static void handle_incomings()
|
static void handle_incomings()
|
||||||
{
|
{
|
||||||
int income;
|
int income;
|
||||||
|
@ -629,11 +629,11 @@ static void handle_incomings()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle received packets for not yet established crypto connections.
|
/* handle received packets for not yet established crypto connections. */
|
||||||
static void receive_crypto()
|
static void receive_crypto()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
if(crypto_connections[i].status == 1)
|
if(crypto_connections[i].status == 1)
|
||||||
{
|
{
|
||||||
|
@ -643,12 +643,12 @@ static void receive_crypto()
|
||||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
if(id_packet(crypto_connections[i].number) == 1)
|
if(id_packet(crypto_connections[i].number) == 1)
|
||||||
//if the packet is a friend request drop it (because we are already friends)
|
/* if the packet is a friend request drop it (because we are already friends) */
|
||||||
{
|
{
|
||||||
len = read_packet(crypto_connections[i].number, temp_data);
|
len = read_packet(crypto_connections[i].number, temp_data);
|
||||||
|
|
||||||
}
|
}
|
||||||
if(id_packet(crypto_connections[i].number) == 2)//handle handshake packet.
|
if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */
|
||||||
{
|
{
|
||||||
len = read_packet(crypto_connections[i].number, temp_data);
|
len = read_packet(crypto_connections[i].number, temp_data);
|
||||||
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
|
if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
|
||||||
|
@ -659,16 +659,16 @@ static void receive_crypto()
|
||||||
memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
|
memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
|
||||||
increment_nonce(crypto_connections[i].sent_nonce);
|
increment_nonce(crypto_connections[i].sent_nonce);
|
||||||
uint32_t zero = 0;
|
uint32_t zero = 0;
|
||||||
crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work
|
crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
|
||||||
write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
|
write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
|
||||||
crypto_connections[i].status = 2;//set it to its proper value right after.
|
crypto_connections[i].status = 2; /* set it to its proper value right after. */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(id_packet(crypto_connections[i].number) != -1)
|
else if(id_packet(crypto_connections[i].number) != -1)
|
||||||
{
|
{
|
||||||
//This should not happen
|
/* This should not happen
|
||||||
//kill the connection if it does
|
kill the connection if it does */
|
||||||
crypto_kill(crypto_connections[i].number);
|
crypto_kill(crypto_connections[i].number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,35 +689,35 @@ static void receive_crypto()
|
||||||
increment_nonce(crypto_connections[i].recv_nonce);
|
increment_nonce(crypto_connections[i].recv_nonce);
|
||||||
crypto_connections[i].status = 3;
|
crypto_connections[i].status = 3;
|
||||||
|
|
||||||
//connection is accepted so we disable the auto kill by setting it to about 1 month from now.
|
/* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */
|
||||||
kill_connection_in(crypto_connections[i].number, 3000000);
|
kill_connection_in(crypto_connections[i].number, 3000000);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//This should not happen
|
/* This should not happen
|
||||||
//kill the connection if it does
|
kill the connection if it does */
|
||||||
crypto_kill(crypto_connections[i].number);
|
crypto_kill(crypto_connections[i].number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(id_packet(crypto_connections[i].number) != -1)
|
else if(id_packet(crypto_connections[i].number) != -1)
|
||||||
{
|
{
|
||||||
//This should not happen
|
/* This should not happen
|
||||||
//kill the connection if it does
|
kill the connection if it does */
|
||||||
crypto_kill(crypto_connections[i].number);
|
crypto_kill(crypto_connections[i].number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//run this to (re)initialize net_crypto
|
/* run this to (re)initialize net_crypto
|
||||||
//sets all the global connection variables to their default values.
|
sets all the global connection variables to their default values. */
|
||||||
void initNetCrypto()
|
void initNetCrypto()
|
||||||
{
|
{
|
||||||
memset(crypto_connections, 0 ,sizeof(crypto_connections));
|
memset(crypto_connections, 0 ,sizeof(crypto_connections));
|
||||||
memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests));
|
memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests));
|
||||||
memset(incoming_connections, -1 ,sizeof(incoming_connections));
|
memset(incoming_connections, -1 ,sizeof(incoming_connections));
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
crypto_connections[i].number = ~0;
|
crypto_connections[i].number = ~0;
|
||||||
}
|
}
|
||||||
|
@ -726,7 +726,7 @@ void initNetCrypto()
|
||||||
static void killTimedout()
|
static void killTimedout()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||||
{
|
{
|
||||||
if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
|
if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
|
||||||
{
|
{
|
||||||
|
@ -740,12 +740,12 @@ static void killTimedout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//main loop
|
/* main loop */
|
||||||
void doNetCrypto()
|
void doNetCrypto()
|
||||||
{
|
{
|
||||||
//TODO:check if friend requests were sent correctly
|
/* TODO:check if friend requests were sent correctly
|
||||||
//handle new incoming connections
|
handle new incoming connections
|
||||||
//handle friend requests
|
handle friend requests */
|
||||||
handle_incomings();
|
handle_incomings();
|
||||||
receive_crypto();
|
receive_crypto();
|
||||||
killTimedout();
|
killTimedout();
|
||||||
|
|
|
@ -26,111 +26,111 @@
|
||||||
|
|
||||||
#include "Lossless_UDP.h"
|
#include "Lossless_UDP.h"
|
||||||
|
|
||||||
//Our public key.
|
/* Our public key. */
|
||||||
extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||||
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
||||||
|
|
||||||
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
||||||
|
|
||||||
//encrypts plain of length length to encrypted of length + 16 using the
|
/* encrypts plain of length length to encrypted of length + 16 using the
|
||||||
//public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
||||||
//return -1 if there was a problem.
|
return -1 if there was a problem.
|
||||||
//return length of encrypted data if everything was fine.
|
return length of encrypted data if everything was fine. */
|
||||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t * plain, uint32_t length, uint8_t * encrypted);
|
uint8_t * plain, uint32_t length, uint8_t * encrypted);
|
||||||
|
|
||||||
|
|
||||||
//decrypts encrypted of length length to plain of length length - 16 using the
|
/* decrypts encrypted of length length to plain of length length - 16 using the
|
||||||
//public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
||||||
//return -1 if there was a problem(decryption failed)
|
return -1 if there was a problem(decryption failed)
|
||||||
//return length of plain data if everything was fine.
|
return length of plain data if everything was fine. */
|
||||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||||
uint8_t * encrypted, uint32_t length, uint8_t * plain);
|
uint8_t * encrypted, uint32_t length, uint8_t * plain);
|
||||||
|
|
||||||
|
|
||||||
//fill the given nonce with random bytes.
|
/* fill the given nonce with random bytes. */
|
||||||
void random_nonce(uint8_t * nonce);
|
void random_nonce(uint8_t * nonce);
|
||||||
|
|
||||||
|
|
||||||
//return 0 if there is no received data in the buffer
|
/* return 0 if there is no received data in the buffer
|
||||||
//return -1 if the packet was discarded.
|
return -1 if the packet was discarded.
|
||||||
//return length of received data if successful
|
return length of received data if successful */
|
||||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data);
|
int read_cryptpacket(int crypt_connection_id, uint8_t * data);
|
||||||
|
|
||||||
|
|
||||||
//return 0 if data could not be put in packet queue
|
/* return 0 if data could not be put in packet queue
|
||||||
//return 1 if data was put into the queue
|
return 1 if data was put into the queue */
|
||||||
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
|
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
|
||||||
|
|
||||||
//send a friend request to peer with public_key and ip_port.
|
/* send a friend request to peer with public_key and ip_port.
|
||||||
//Data represents the data we send with the friends request.
|
Data represents the data we send with the friends request.
|
||||||
//returns -1 on failure
|
returns -1 on failure
|
||||||
//returns a positive friend request id that can be used later to see if it was sent correctly on success.
|
returns a positive friend request id that can be used later to see if it was sent correctly on success. */
|
||||||
int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length);
|
int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length);
|
||||||
|
|
||||||
|
|
||||||
//return -1 if failure
|
/* return -1 if failure
|
||||||
//return 0 if connection is still trying to send the request.
|
return 0 if connection is still trying to send the request.
|
||||||
//return 1 if sent correctly
|
return 1 if sent correctly
|
||||||
//return 2 if connection timed out
|
return 2 if connection timed out */
|
||||||
int check_friendrequest(int friend_request);
|
int check_friendrequest(int friend_request);
|
||||||
|
|
||||||
|
|
||||||
//puts the public key of the friend if public_key, the data from the request
|
/* puts the public key of the friend if public_key, the data from the request
|
||||||
//in data if a friend request was sent to us and returns the length of the data.
|
in data if a friend request was sent to us and returns the length of the data.
|
||||||
//return -1 if no valid friend requests.
|
return -1 if no valid friend requests. */
|
||||||
int handle_friendrequest(uint8_t * public_key, uint8_t * data);
|
int handle_friendrequest(uint8_t * public_key, uint8_t * data);
|
||||||
|
|
||||||
|
|
||||||
//Start a secure connection with other peer who has public_key and ip_port
|
/* Start a secure connection with other peer who has public_key and ip_port
|
||||||
//returns -1 if failure
|
returns -1 if failure
|
||||||
//returns crypt_connection_id of the initialized connection if everything went well.
|
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port);
|
int crypto_connect(uint8_t * public_key, IP_Port ip_port);
|
||||||
|
|
||||||
|
|
||||||
//kill a crypto connection
|
/* kill a crypto connection
|
||||||
//return 0 if killed successfully
|
return 0 if killed successfully
|
||||||
//return 1 if there was a problem.
|
return 1 if there was a problem. */
|
||||||
int crypto_kill(int crypt_connection_id);
|
int crypto_kill(int crypt_connection_id);
|
||||||
|
|
||||||
//handle an incoming connection
|
/* handle an incoming connection
|
||||||
//return -1 if no crypto inbound connection
|
return -1 if no crypto inbound connection
|
||||||
//return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
|
return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
|
||||||
//Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
|
Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
|
||||||
//and the session public key for the connection in session_key
|
and the session public key for the connection in session_key
|
||||||
//to accept it see: accept_crypto_inbound(...)
|
to accept it see: accept_crypto_inbound(...)
|
||||||
//to refuse it just call kill_connection(...) on the connection id
|
to refuse it just call kill_connection(...) on the connection id */
|
||||||
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
||||||
|
|
||||||
|
|
||||||
//accept an incoming connection using the parameters provided by crypto_inbound
|
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||||
//return -1 if not successful
|
return -1 if not successful
|
||||||
//returns the crypt_connection_id if successful
|
returns the crypt_connection_id if successful */
|
||||||
int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
|
||||||
|
|
||||||
//return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||||
//(we have received a handshake but no empty data packet), 3 if the connection is established.
|
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||||
//4 if the connection is timed out and waiting to be killed
|
4 if the connection is timed out and waiting to be killed */
|
||||||
int is_cryptoconnected(int crypt_connection_id);
|
int is_cryptoconnected(int crypt_connection_id);
|
||||||
|
|
||||||
|
|
||||||
//Generate our public and private keys
|
/* Generate our public and private keys
|
||||||
//Only call this function the first time the program starts.
|
Only call this function the first time the program starts. */
|
||||||
void new_keys();
|
void new_keys();
|
||||||
|
|
||||||
//save the public and private keys to the keys array
|
/* save the public and private keys to the keys array
|
||||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||||
void save_keys(uint8_t * keys);
|
void save_keys(uint8_t * keys);
|
||||||
|
|
||||||
//load the public and private keys from the keys array
|
/* load the public and private keys from the keys array
|
||||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||||
void load_keys(uint8_t * keys);
|
void load_keys(uint8_t * keys);
|
||||||
|
|
||||||
//run this to (re)initialize net_crypto
|
/* run this to (re)initialize net_crypto
|
||||||
//sets all the global connection variables to their default values.
|
sets all the global connection variables to their default values. */
|
||||||
void initNetCrypto();
|
void initNetCrypto();
|
||||||
|
|
||||||
//main loop
|
/* main loop */
|
||||||
void doNetCrypto();
|
void doNetCrypto();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,12 +25,12 @@
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
|
||||||
|
|
||||||
//returns current UNIX time in microseconds (us).
|
/* returns current UNIX time in microseconds (us). */
|
||||||
uint64_t current_time()
|
uint64_t current_time()
|
||||||
{
|
{
|
||||||
uint64_t time;
|
uint64_t time;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
//This probably works fine
|
/* This probably works fine */
|
||||||
FILETIME ft;
|
FILETIME ft;
|
||||||
GetSystemTimeAsFileTime(&ft);
|
GetSystemTimeAsFileTime(&ft);
|
||||||
time = ft.dwHighDateTime;
|
time = ft.dwHighDateTime;
|
||||||
|
@ -48,8 +48,8 @@ uint64_t current_time()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//return a random number
|
/* return a random number
|
||||||
//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary
|
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
||||||
uint32_t random_int()
|
uint32_t random_int()
|
||||||
{
|
{
|
||||||
#ifndef VANILLA_NACL
|
#ifndef VANILLA_NACL
|
||||||
|
@ -60,11 +60,11 @@ uint32_t random_int()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//our UDP socket, a global variable.
|
/* our UDP socket, a global variable. */
|
||||||
static int sock;
|
static int sock;
|
||||||
|
|
||||||
//Basic network functions:
|
/* Basic network functions:
|
||||||
//Function to send packet(data) of length length to ip_port
|
Function to send packet(data) of length length to ip_port */
|
||||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||||
{
|
{
|
||||||
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
||||||
|
@ -72,10 +72,10 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Function to receive data, ip and port of sender is put into ip_port
|
/* Function to receive data, ip and port of sender is put into ip_port
|
||||||
//the packet data into data
|
the packet data into data
|
||||||
//the packet length into length.
|
the packet length into length.
|
||||||
//dump all empty packets.
|
dump all empty packets. */
|
||||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
||||||
{
|
{
|
||||||
ADDR addr;
|
ADDR addr;
|
||||||
|
@ -87,8 +87,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
||||||
(*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
(*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
if(*(int32_t *)length <= 0)
|
if(*(int32_t *)length <= 0)
|
||||||
{
|
{
|
||||||
//nothing received
|
/* nothing received
|
||||||
//or empty packet
|
or empty packet */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ip_port->ip = addr.ip;
|
ip_port->ip = addr.ip;
|
||||||
|
@ -97,12 +97,12 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//initialize networking
|
/* initialize networking
|
||||||
//bind to ip and port
|
bind to ip and port
|
||||||
//ip must be in network order EX: 127.0.0.1 = (7F000001)
|
ip must be in network order EX: 127.0.0.1 = (7F000001)
|
||||||
//port is in host byte order (this means don't worry about it)
|
port is in host byte order (this means don't worry about it)
|
||||||
//returns 0 if no problems
|
returns 0 if no problems
|
||||||
//TODO: add something to check if there are errors
|
TODO: add something to check if there are errors */
|
||||||
int init_networking(IP ip ,uint16_t port)
|
int init_networking(IP ip ,uint16_t port)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -117,11 +117,11 @@ int init_networking(IP ip ,uint16_t port)
|
||||||
#endif
|
#endif
|
||||||
srand((uint32_t)current_time());
|
srand((uint32_t)current_time());
|
||||||
|
|
||||||
//initialize our socket
|
/* initialize our socket */
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
//Functions to increase the size of the send and receive UDP buffers
|
/* Functions to increase the size of the send and receive UDP buffers
|
||||||
//NOTE: uncomment if necessary
|
NOTE: uncomment if necessary */
|
||||||
/*
|
/*
|
||||||
int n = 1024 * 1024 * 2;
|
int n = 1024 * 1024 * 2;
|
||||||
if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
|
if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
|
||||||
|
@ -134,28 +134,28 @@ int init_networking(IP ip ,uint16_t port)
|
||||||
return -1;
|
return -1;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//Set socket nonblocking
|
/* Set socket nonblocking */
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
//I think this works for windows
|
/* I think this works for windows */
|
||||||
u_long mode = 1;
|
u_long mode = 1;
|
||||||
//ioctl(sock, FIONBIO, &mode);
|
/* ioctl(sock, FIONBIO, &mode); */
|
||||||
ioctlsocket(sock, FIONBIO, &mode);
|
ioctlsocket(sock, FIONBIO, &mode);
|
||||||
#else
|
#else
|
||||||
fcntl(sock, F_SETFL, O_NONBLOCK, 1);
|
fcntl(sock, F_SETFL, O_NONBLOCK, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Bind our socket to port PORT and address 0.0.0.0
|
/* Bind our socket to port PORT and address 0.0.0.0 */
|
||||||
ADDR addr = {AF_INET, htons(port), ip};
|
ADDR addr = {AF_INET, htons(port), ip};
|
||||||
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//function to cleanup networking stuff
|
/* function to cleanup networking stuff */
|
||||||
void shutdown_networking()
|
void shutdown_networking()
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
215
core/network.h
215
core/network.h
|
@ -1,6 +1,6 @@
|
||||||
/* network.h
|
/* network.h
|
||||||
*
|
*
|
||||||
* Datatypes, functions and includes for the core networking.
|
* Functions for the core networking.
|
||||||
*
|
*
|
||||||
|
|
||||||
Copyright (C) 2013 Tox project All Rights Reserved.
|
Copyright (C) 2013 Tox project All Rights Reserved.
|
||||||
|
@ -21,104 +21,141 @@
|
||||||
along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef NETWORK_H
|
#include "network.h"
|
||||||
#define NETWORK_H
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
/* returns current UNIX time in microseconds (us). */
|
||||||
#ifdef WIN32 //Put win32 includes here
|
uint64_t current_time()
|
||||||
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#undef VANILLA_NACL//make sure on windows we use libsodium
|
|
||||||
|
|
||||||
#else //Linux includes
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef VANILLA_NACL
|
|
||||||
//we use libsodium by default
|
|
||||||
#include <sodium.h>
|
|
||||||
#else
|
|
||||||
|
|
||||||
//TODO: Including stuff like this is bad. This needs fixing.
|
|
||||||
//We keep support for the original NaCl for now.
|
|
||||||
#include "../nacl/build/Linux/include/amd64/crypto_box.h"
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define MAX_UDP_PACKET_SIZE 65507
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
{
|
||||||
uint8_t c[4];
|
uint64_t time;
|
||||||
uint16_t s[2];
|
#ifdef WIN32
|
||||||
uint32_t i;
|
/* This probably works fine */
|
||||||
}IP;
|
FILETIME ft;
|
||||||
|
GetSystemTimeAsFileTime(&ft);
|
||||||
typedef struct
|
time = ft.dwHighDateTime;
|
||||||
{
|
time <<=32;
|
||||||
IP ip;
|
time |= ft.dwLowDateTime;
|
||||||
uint16_t port;
|
time -= 116444736000000000UL;
|
||||||
//not used for anything right now
|
return time/10;
|
||||||
uint16_t padding;
|
#else
|
||||||
}IP_Port;
|
struct timeval a;
|
||||||
|
gettimeofday(&a, NULL);
|
||||||
typedef struct
|
time = 1000000UL*a.tv_sec + a.tv_usec;
|
||||||
{
|
return time;
|
||||||
int16_t family;
|
|
||||||
uint16_t port;
|
|
||||||
IP ip;
|
|
||||||
uint8_t zeroes[8];
|
|
||||||
#ifdef ENABLE_IPV6
|
|
||||||
uint8_t zeroes2[12];
|
|
||||||
#endif
|
#endif
|
||||||
}ADDR;
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return a random number
|
||||||
|
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
||||||
|
uint32_t random_int()
|
||||||
|
{
|
||||||
|
#ifndef VANILLA_NACL
|
||||||
|
/* NOTE: this function comes from libsodium */
|
||||||
|
return randombytes_random();
|
||||||
|
#else
|
||||||
|
return random();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
//returns current time in milleseconds since the epoch.
|
/* our UDP socket, a global variable. */
|
||||||
uint64_t current_time();
|
static int sock;
|
||||||
|
|
||||||
//return a random number
|
/* Basic network functions:
|
||||||
//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary
|
Function to send packet(data) of length length to ip_port */
|
||||||
uint32_t random_int();
|
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||||
|
{
|
||||||
|
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
||||||
|
return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//Basic network functions:
|
/* Function to receive data, ip and port of sender is put into ip_port
|
||||||
|
the packet data into data
|
||||||
|
the packet length into length.
|
||||||
|
dump all empty packets. */
|
||||||
|
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
||||||
|
{
|
||||||
|
ADDR addr;
|
||||||
|
#ifdef WIN32
|
||||||
|
int addrlen = sizeof(addr);
|
||||||
|
#else
|
||||||
|
uint32_t addrlen = sizeof(addr);
|
||||||
|
#endif
|
||||||
|
(*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
|
if(*(int32_t *)length <= 0)
|
||||||
|
{
|
||||||
|
/* nothing received
|
||||||
|
or empty packet */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ip_port->ip = addr.ip;
|
||||||
|
ip_port->port = addr.port;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//Function to send packet(data) of length length to ip_port
|
/* initialize networking
|
||||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length);
|
bind to ip and port
|
||||||
|
ip must be in network order EX: 127.0.0.1 = (7F000001)
|
||||||
|
port is in host byte order (this means don't worry about it)
|
||||||
|
returns 0 if no problems
|
||||||
|
TODO: add something to check if there are errors */
|
||||||
|
int init_networking(IP ip ,uint16_t port)
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
WSADATA wsaData;
|
||||||
|
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
srandom((uint32_t)current_time());
|
||||||
|
#endif
|
||||||
|
srand((uint32_t)current_time());
|
||||||
|
|
||||||
|
/* initialize our socket */
|
||||||
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
|
/* Functions to increase the size of the send and receive UDP buffers
|
||||||
|
NOTE: uncomment if necessary
|
||||||
|
|
||||||
|
int n = 1024 * 1024 * 2;
|
||||||
|
if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
//Function to receive data, ip and port of sender is put into ip_port
|
if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1)
|
||||||
//the packet data into data
|
{
|
||||||
//the packet length into length.
|
return -1;
|
||||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
|
}*/
|
||||||
|
|
||||||
|
/*Set socket nonblocking */
|
||||||
|
#ifdef WIN32
|
||||||
|
/* I think this works for windows */
|
||||||
|
u_long mode = 1;
|
||||||
|
/* ioctl(sock, FIONBIO, &mode); */
|
||||||
|
ioctlsocket(sock, FIONBIO, &mode);
|
||||||
|
#else
|
||||||
|
fcntl(sock, F_SETFL, O_NONBLOCK, 1);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Bind our socket to port PORT and address 0.0.0.0 */
|
||||||
|
ADDR addr = {AF_INET, htons(port), ip};
|
||||||
|
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
||||||
|
return 0;
|
||||||
|
|
||||||
//initialize networking
|
}
|
||||||
//bind to ip and port
|
|
||||||
//ip must be in network order EX: 127.0.0.1 = (7F000001)
|
|
||||||
//port is in host byte order (this means don't worry about it)
|
|
||||||
//returns 0 if no problems
|
|
||||||
//TODO: add something to check if there are errors
|
|
||||||
int init_networking(IP ip ,uint16_t port);
|
|
||||||
|
|
||||||
|
/* function to cleanup networking stuff */
|
||||||
//function to cleanup networking stuff(doesn't do much right now)
|
void shutdown_networking()
|
||||||
void shutdown_networking();
|
{
|
||||||
#endif
|
#ifdef WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user