mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge pull request #62 from Captainhat/master
More comments in core fixed
This commit is contained in:
commit
72ea431489
|
@ -28,23 +28,23 @@
|
|||
#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_secret_key[crypto_box_SECRETKEYBYTES];
|
||||
|
||||
|
||||
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
|
||||
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 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 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.
|
||||
//4 if the connection is timed out.
|
||||
uint16_t number; //Lossless_UDP connection number corresponding to this connection.
|
||||
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 sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
|
||||
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 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
|
||||
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||
4 if the connection is timed out. */
|
||||
uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
|
||||
|
||||
}Crypto_Connection;
|
||||
|
||||
|
@ -54,18 +54,18 @@ static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS];
|
|||
|
||||
#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];
|
||||
|
||||
#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];
|
||||
|
||||
//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
|
||||
//return -1 if there was a problem.
|
||||
//return length of encrypted data if everything was fine.
|
||||
/* 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
|
||||
return -1 if there was a problem.
|
||||
return length of encrypted data if everything was fine. */
|
||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
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 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);
|
||||
|
||||
//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)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
//unpad the encrypted message
|
||||
/* unpad the encrypted message */
|
||||
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, 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
|
||||
//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 length of plain data if everything was fine.
|
||||
/* 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
|
||||
return -1 if there was a problem(decryption failed)
|
||||
return length of plain data if everything was fine. */
|
||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * encrypted, uint32_t length, uint8_t * plain)
|
||||
{
|
||||
|
@ -107,24 +107,24 @@ 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 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,
|
||||
nonce, public_key, secret_key) == -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)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
//unpad the plain message
|
||||
/* unpad the plain message */
|
||||
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, 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)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -138,8 +138,8 @@ void increment_nonce(uint8_t * nonce)
|
|||
}
|
||||
}
|
||||
|
||||
//fill the given nonce with random bytes.
|
||||
//TODO: make this more optimized
|
||||
/* fill the given nonce with random bytes.
|
||||
TODO: make this more optimized */
|
||||
void random_nonce(uint8_t * nonce)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -149,9 +149,9 @@ void random_nonce(uint8_t * nonce)
|
|||
}
|
||||
}
|
||||
|
||||
//return 0 if there is no received data in the buffer
|
||||
//return -1 if the packet was discarded.
|
||||
//return length of received data if successful
|
||||
/* return 0 if there is no received data in the buffer
|
||||
return -1 if the packet was discarded.
|
||||
return length of received data if successful */
|
||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data)
|
||||
{
|
||||
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 1 if data was put into the queue
|
||||
/* return 0 if data could not be put in packet queue
|
||||
return 1 if data was put into the queue */
|
||||
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
//send a friend request to peer with public_key and ip_port.
|
||||
//Data represents the data we send with the friends request.
|
||||
//returns -1 on failure
|
||||
//returns a positive friend request id that can be used later to see if it was sent correctly on success.
|
||||
/* send a friend request to peer with public_key and ip_port.
|
||||
Data represents the data we send with the friends request.
|
||||
returns -1 on failure
|
||||
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)
|
||||
{
|
||||
if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES)
|
||||
|
@ -264,10 +264,10 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
|
|||
return -1;
|
||||
}
|
||||
|
||||
//return -1 if failure
|
||||
//return 0 if connection is still trying to send the request.
|
||||
//return 1 if sent correctly
|
||||
//return 2 if connection timed out
|
||||
/* return -1 if failure
|
||||
return 0 if connection is still trying to send the request.
|
||||
return 1 if sent correctly
|
||||
return 2 if connection timed out */
|
||||
int check_friendrequest(int friend_request)
|
||||
{
|
||||
if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS)
|
||||
|
@ -299,9 +299,9 @@ int check_friendrequest(int friend_request)
|
|||
return 0;
|
||||
}
|
||||
|
||||
//Send a crypto handshake packet containing an encrypted secret nonce and session 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
|
||||
/* Send a crypto handshake packet containing an encrypted secret nonce and session 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 */
|
||||
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];
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
//Extract secret nonce, session public key and public_key from a packet(data) with length length
|
||||
//return 1 if successful
|
||||
//return 0 if failure
|
||||
/* Extract secret nonce, session public key and public_key from a packet(data) with length length
|
||||
return 1 if successful
|
||||
return 0 if failure */
|
||||
int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
||||
uint8_t * session_key, uint8_t * data, uint16_t length)
|
||||
{
|
||||
|
@ -359,9 +359,9 @@ 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
|
||||
//in data if a friend request was sent to us and returns the length of the data.
|
||||
//return -1 if no valid friend requests.
|
||||
/* 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.
|
||||
return -1 if no valid friend requests. */
|
||||
int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -384,12 +384,12 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
|||
if(len1 != -1)
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -397,9 +397,9 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
|||
return -1;
|
||||
}
|
||||
|
||||
//get crypto connection id from public key of peer
|
||||
//return -1 if there are no connections like we are looking for
|
||||
//return id if it found it
|
||||
/* get crypto connection id from public key of peer
|
||||
return -1 if there are no connections like we are looking for
|
||||
return id if it found it */
|
||||
int getcryptconnection_id(uint8_t * public_key)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -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
|
||||
//returns -1 if failure
|
||||
//returns crypt_connection_id of the initialized connection if everything went well.
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -453,19 +453,19 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
|||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
return i;
|
||||
}
|
||||
return -1;//this should never happen.
|
||||
return -1; /* this should never happen. */
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//handle an incoming connection
|
||||
//return -1 if no crypto inbound 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
|
||||
//and the session public key for the connection in session_key
|
||||
//to accept it see: accept_crypto_inbound(...)
|
||||
//to refuse it just call kill_connection(...) on the connection id
|
||||
/* handle an incoming connection
|
||||
return -1 if no crypto inbound 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
|
||||
and the session public key for the connection in session_key
|
||||
to accept it see: accept_crypto_inbound(...)
|
||||
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)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -495,9 +495,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
|
|||
return -1;
|
||||
}
|
||||
|
||||
//kill a crypto connection
|
||||
//return 0 if killed successfully
|
||||
//return 1 if there was a problem.
|
||||
/* kill a crypto connection
|
||||
return 0 if killed successfully
|
||||
return 1 if there was a problem. */
|
||||
int crypto_kill(int crypt_connection_id)
|
||||
{
|
||||
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
|
||||
//return -1 if not successful
|
||||
//returns the crypt_connection_id if successful
|
||||
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||
return -1 if not 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)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -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);
|
||||
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));
|
||||
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 -1;//this should never happen.
|
||||
return -1; /* this should never happen. */
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//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.
|
||||
//4 if the connection is timed out and waiting to be killed
|
||||
/* 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.
|
||||
4 if the connection is timed out and waiting to be killed */
|
||||
int is_cryptoconnected(int crypt_connection_id)
|
||||
{
|
||||
if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
||||
|
@ -573,33 +573,33 @@ int is_cryptoconnected(int crypt_connection_id)
|
|||
}
|
||||
|
||||
|
||||
//Generate our public and private keys
|
||||
//Only call this function the first time the program starts.
|
||||
/* Generate our public and private keys
|
||||
Only call this function the first time the program starts. */
|
||||
void new_keys()
|
||||
{
|
||||
crypto_box_keypair(self_public_key,self_secret_key);
|
||||
}
|
||||
|
||||
//save the public and private keys to the keys array
|
||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
/* save the public and private keys to the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void save_keys(uint8_t * keys)
|
||||
{
|
||||
memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
|
||||
}
|
||||
|
||||
//load the public and private keys from the keys array
|
||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
/* load the public and private keys from the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void load_keys(uint8_t * keys)
|
||||
{
|
||||
memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
|
||||
}
|
||||
|
||||
//TODO: optimize this
|
||||
//adds an incoming connection to the incoming_connection list.
|
||||
//returns 0 if successful
|
||||
//returns 1 if failure
|
||||
/* TODO: optimize this
|
||||
adds an incoming connection to the incoming_connection list.
|
||||
returns 0 if successful
|
||||
returns 1 if failure */
|
||||
int new_incoming(int id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -614,8 +614,8 @@ int new_incoming(int id)
|
|||
return 1;
|
||||
}
|
||||
|
||||
//TODO: optimize this
|
||||
//handle all new incoming connections.
|
||||
/* TODO: optimize this
|
||||
handle all new incoming connections. */
|
||||
static void handle_incomings()
|
||||
{
|
||||
int income;
|
||||
|
@ -629,7 +629,7 @@ 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()
|
||||
{
|
||||
uint32_t i;
|
||||
|
@ -643,12 +643,12 @@ static void receive_crypto()
|
|||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint16_t len;
|
||||
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);
|
||||
|
||||
}
|
||||
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);
|
||||
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);
|
||||
increment_nonce(crypto_connections[i].sent_nonce);
|
||||
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));
|
||||
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)
|
||||
{
|
||||
//This should not happen
|
||||
//kill the connection if it does
|
||||
/* This should not happen
|
||||
kill the connection if it does */
|
||||
crypto_kill(crypto_connections[i].number);
|
||||
}
|
||||
|
||||
|
@ -689,28 +689,28 @@ static void receive_crypto()
|
|||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
//This should not happen
|
||||
//kill the connection if it does
|
||||
/* This should not happen
|
||||
kill the connection if it does */
|
||||
crypto_kill(crypto_connections[i].number);
|
||||
}
|
||||
}
|
||||
else if(id_packet(crypto_connections[i].number) != -1)
|
||||
{
|
||||
//This should not happen
|
||||
//kill the connection if it does
|
||||
/* This should not happen
|
||||
kill the connection if it does */
|
||||
crypto_kill(crypto_connections[i].number);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//run this to (re)initialize net_crypto
|
||||
//sets all the global connection variables to their default values.
|
||||
/* run this to (re)initialize net_crypto
|
||||
sets all the global connection variables to their default values. */
|
||||
void initNetCrypto()
|
||||
{
|
||||
memset(crypto_connections, 0 ,sizeof(crypto_connections));
|
||||
|
@ -740,12 +740,12 @@ static void killTimedout()
|
|||
}
|
||||
}
|
||||
|
||||
//main loop
|
||||
/* main loop */
|
||||
void doNetCrypto()
|
||||
{
|
||||
//TODO:check if friend requests were sent correctly
|
||||
//handle new incoming connections
|
||||
//handle friend requests
|
||||
/* TODO:check if friend requests were sent correctly
|
||||
handle new incoming connections
|
||||
handle friend requests */
|
||||
handle_incomings();
|
||||
receive_crypto();
|
||||
killTimedout();
|
||||
|
|
|
@ -26,111 +26,111 @@
|
|||
|
||||
#include "Lossless_UDP.h"
|
||||
|
||||
//Our public key.
|
||||
/* Our public key. */
|
||||
extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
||||
|
||||
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
||||
|
||||
//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
|
||||
//return -1 if there was a problem.
|
||||
//return length of encrypted data if everything was fine.
|
||||
/* 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
|
||||
return -1 if there was a problem.
|
||||
return length of encrypted data if everything was fine. */
|
||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * plain, uint32_t length, uint8_t * encrypted);
|
||||
|
||||
|
||||
//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
|
||||
//return -1 if there was a problem(decryption failed)
|
||||
//return length of plain data if everything was fine.
|
||||
/* 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
|
||||
return -1 if there was a problem(decryption failed)
|
||||
return length of plain data if everything was fine. */
|
||||
int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
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);
|
||||
|
||||
|
||||
//return 0 if there is no received data in the buffer
|
||||
//return -1 if the packet was discarded.
|
||||
//return length of received data if successful
|
||||
/* return 0 if there is no received data in the buffer
|
||||
return -1 if the packet was discarded.
|
||||
return length of received data if successful */
|
||||
int read_cryptpacket(int crypt_connection_id, uint8_t * data);
|
||||
|
||||
|
||||
//return 0 if data could not be put in packet queue
|
||||
//return 1 if data was put into the queue
|
||||
/* return 0 if data could not be put in packet queue
|
||||
return 1 if data was put into the queue */
|
||||
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.
|
||||
//Data represents the data we send with the friends request.
|
||||
//returns -1 on failure
|
||||
//returns a positive friend request id that can be used later to see if it was sent correctly on success.
|
||||
/* send a friend request to peer with public_key and ip_port.
|
||||
Data represents the data we send with the friends request.
|
||||
returns -1 on failure
|
||||
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);
|
||||
|
||||
|
||||
//return -1 if failure
|
||||
//return 0 if connection is still trying to send the request.
|
||||
//return 1 if sent correctly
|
||||
//return 2 if connection timed out
|
||||
/* return -1 if failure
|
||||
return 0 if connection is still trying to send the request.
|
||||
return 1 if sent correctly
|
||||
return 2 if connection timed out */
|
||||
int check_friendrequest(int friend_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.
|
||||
//return -1 if no valid friend requests.
|
||||
/* 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.
|
||||
return -1 if no valid friend requests. */
|
||||
int handle_friendrequest(uint8_t * public_key, uint8_t * data);
|
||||
|
||||
|
||||
//Start a secure connection with other peer who has public_key and ip_port
|
||||
//returns -1 if failure
|
||||
//returns crypt_connection_id of the initialized connection if everything went well.
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||
int crypto_connect(uint8_t * public_key, IP_Port ip_port);
|
||||
|
||||
|
||||
//kill a crypto connection
|
||||
//return 0 if killed successfully
|
||||
//return 1 if there was a problem.
|
||||
/* kill a crypto connection
|
||||
return 0 if killed successfully
|
||||
return 1 if there was a problem. */
|
||||
int crypto_kill(int crypt_connection_id);
|
||||
|
||||
//handle an incoming connection
|
||||
//return -1 if no crypto inbound 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
|
||||
//and the session public key for the connection in session_key
|
||||
//to accept it see: accept_crypto_inbound(...)
|
||||
//to refuse it just call kill_connection(...) on the connection id
|
||||
/* handle an incoming connection
|
||||
return -1 if no crypto inbound 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
|
||||
and the session public key for the connection in session_key
|
||||
to accept it see: accept_crypto_inbound(...)
|
||||
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);
|
||||
|
||||
|
||||
//accept an incoming connection using the parameters provided by crypto_inbound
|
||||
//return -1 if not successful
|
||||
//returns the crypt_connection_id if successful
|
||||
/* accept an incoming connection using the parameters provided by crypto_inbound
|
||||
return -1 if not 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);
|
||||
|
||||
//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.
|
||||
//4 if the connection is timed out and waiting to be killed
|
||||
/* 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.
|
||||
4 if the connection is timed out and waiting to be killed */
|
||||
int is_cryptoconnected(int crypt_connection_id);
|
||||
|
||||
|
||||
//Generate our public and private keys
|
||||
//Only call this function the first time the program starts.
|
||||
/* Generate our public and private keys
|
||||
Only call this function the first time the program starts. */
|
||||
void new_keys();
|
||||
|
||||
//save the public and private keys to the keys array
|
||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
/* save the public and private keys to the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void save_keys(uint8_t * keys);
|
||||
|
||||
//load the public and private keys from the keys array
|
||||
//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
/* load the public and private keys from the keys array
|
||||
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
||||
void load_keys(uint8_t * keys);
|
||||
|
||||
//run this to (re)initialize net_crypto
|
||||
//sets all the global connection variables to their default values.
|
||||
/* run this to (re)initialize net_crypto
|
||||
sets all the global connection variables to their default values. */
|
||||
void initNetCrypto();
|
||||
|
||||
//main loop
|
||||
/* main loop */
|
||||
void doNetCrypto();
|
||||
|
||||
|
||||
|
|
|
@ -25,12 +25,12 @@
|
|||
#include "network.h"
|
||||
|
||||
|
||||
//returns current UNIX time in microseconds (us).
|
||||
/* returns current UNIX time in microseconds (us). */
|
||||
uint64_t current_time()
|
||||
{
|
||||
uint64_t time;
|
||||
#ifdef WIN32
|
||||
//This probably works fine
|
||||
/* This probably works fine */
|
||||
FILETIME ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
time = ft.dwHighDateTime;
|
||||
|
@ -48,8 +48,8 @@ uint64_t current_time()
|
|||
|
||||
}
|
||||
|
||||
//return a random number
|
||||
//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary
|
||||
/* 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
|
||||
|
@ -60,11 +60,11 @@ uint32_t random_int()
|
|||
#endif
|
||||
}
|
||||
|
||||
//our UDP socket, a global variable.
|
||||
/* our UDP socket, a global variable. */
|
||||
static int sock;
|
||||
|
||||
//Basic network functions:
|
||||
//Function to send packet(data) of length length to ip_port
|
||||
/* Basic network functions:
|
||||
Function to send packet(data) of length length to ip_port */
|
||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||
{
|
||||
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
|
||||
//the packet data into data
|
||||
//the packet length into length.
|
||||
//dump all empty packets.
|
||||
/* 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;
|
||||
|
@ -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);
|
||||
if(*(int32_t *)length <= 0)
|
||||
{
|
||||
//nothing received
|
||||
//or empty packet
|
||||
/* nothing received
|
||||
or empty packet */
|
||||
return -1;
|
||||
}
|
||||
ip_port->ip = addr.ip;
|
||||
|
@ -97,12 +97,12 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
|||
|
||||
}
|
||||
|
||||
//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
|
||||
/* 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)
|
||||
{
|
||||
#ifdef WIN32
|
||||
|
@ -117,11 +117,11 @@ int init_networking(IP ip ,uint16_t port)
|
|||
#endif
|
||||
srand((uint32_t)current_time());
|
||||
|
||||
//initialize our socket
|
||||
/* 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
|
||||
/* 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)
|
||||
|
@ -134,28 +134,28 @@ int init_networking(IP ip ,uint16_t port)
|
|||
return -1;
|
||||
}*/
|
||||
|
||||
//Set socket nonblocking
|
||||
/* Set socket nonblocking */
|
||||
#ifdef WIN32
|
||||
//I think this works for windows
|
||||
/* I think this works for windows */
|
||||
u_long mode = 1;
|
||||
//ioctl(sock, FIONBIO, &mode);
|
||||
/* 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
|
||||
/* 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;
|
||||
|
||||
}
|
||||
|
||||
//function to cleanup networking stuff
|
||||
/* function to cleanup networking stuff */
|
||||
void shutdown_networking()
|
||||
{
|
||||
#ifdef WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,12 +34,12 @@
|
|||
|
||||
|
||||
|
||||
#ifdef WIN32 //Put win32 includes here
|
||||
#ifdef WIN32 /* Put win32 includes here */
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#undef VANILLA_NACL//make sure on windows we use libsodium
|
||||
#undef VANILLA_NACL /* make sure on windows we use libsodium */
|
||||
|
||||
#else //Linux includes
|
||||
|
||||
|
@ -53,12 +53,12 @@
|
|||
#endif
|
||||
|
||||
#ifndef VANILLA_NACL
|
||||
//we use libsodium by default
|
||||
/* 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.
|
||||
/* 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
|
||||
|
@ -77,7 +77,7 @@ typedef struct
|
|||
{
|
||||
IP ip;
|
||||
uint16_t port;
|
||||
//not used for anything right now
|
||||
/* not used for anything right now */
|
||||
uint16_t padding;
|
||||
}IP_Port;
|
||||
|
||||
|
@ -93,32 +93,32 @@ typedef struct
|
|||
}ADDR;
|
||||
|
||||
|
||||
//returns current time in milleseconds since the epoch.
|
||||
/* returns current time in milleseconds since the epoch. */
|
||||
uint64_t current_time();
|
||||
|
||||
//return a random number
|
||||
//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary
|
||||
/* return a random number
|
||||
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
||||
uint32_t random_int();
|
||||
|
||||
//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);
|
||||
|
||||
//Function to receive data, ip and port of sender is put into ip_port
|
||||
//the packet data into data
|
||||
//the packet length into length.
|
||||
/* Function to receive data, ip and port of sender is put into ip_port
|
||||
the packet data into data
|
||||
the packet length into length. */
|
||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
|
||||
|
||||
//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
|
||||
/* 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(doesn't do much right now)
|
||||
/* function to cleanup networking stuff(doesn't do much right now) */
|
||||
void shutdown_networking();
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user