Ran the code through: astyle --style=linux

This commit is contained in:
irungentoo 2013-07-27 08:43:36 -04:00
parent 1a6446266c
commit 14b43651c1
14 changed files with 737 additions and 749 deletions

View File

@ -23,8 +23,7 @@
#include "DHT.h"
typedef struct
{
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
uint32_t timestamp;
@ -32,13 +31,12 @@ typedef struct
IP_Port ret_ip_port;/* The ip_port returned by this node for the friend
(for nodes in friends_list) or us (for nodes in close_clientlist) */
uint32_t ret_timestamp;
}Client_data;
} Client_data;
/* maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8
typedef struct
{
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS];
uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
@ -49,21 +47,19 @@ typedef struct
uint32_t punching_timestamp;
uint64_t NATping_id;
uint32_t NATping_timestamp;
}Friend;
} Friend;
typedef struct
{
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
}Node_format;
} Node_format;
typedef struct
{
typedef struct {
IP_Port ip_port;
uint64_t ping_id;
uint32_t timestamp;
}Pinged;
} Pinged;
/* Our client id/public key */
uint8_t self_public_key[CLIENT_ID_SIZE];
@ -174,8 +170,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
num_nodes++;
}
else for(j = 0; j < MAX_SENT_NODES; ++j)
} else for(j = 0; j < MAX_SENT_NODES; ++j)
if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) {
memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE);
nodes_list[j].ip_port = close_clientlist[i].ip_port;
@ -210,7 +205,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
uint32_t i;
uint32_t temp_time = unix_time();
for(i = 0; i < length; ++i)
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ {
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) { /* if node is bad. */
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port;
list[i].timestamp = temp_time;
@ -704,12 +699,12 @@ void doDHTFriends()
for(i = 0; i < num_friends; ++i) {
uint32_t num_nodes = 0;
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ {
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) { /* if node is not dead. */
if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
friends_list[i].client_list[j].last_pinged = temp_time;
}
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ {
if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { /* if node is good. */
index[num_nodes] = j;
++num_nodes;
}
@ -918,8 +913,7 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
return 1;
}
/* if request is not for us, try routing it. */
else
if(route_packet(packet + 1, packet, length) == length)
else if(route_packet(packet + 1, packet, length) == length)
return 0;
return 0;
}
@ -998,8 +992,7 @@ static void doNAT()
send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
friends_list[i].NATping_timestamp = temp_time;
}
}
else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
} else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) {
IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2);
if(ip.i == 0)
continue;

View File

@ -30,79 +30,79 @@
extern "C" {
#endif
/* Current time, unix format */
/* Current time, unix format */
#define unix_time() ((uint32_t)time(NULL))
/* size of the client_id in bytes */
/* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
/* Add a new friend to the friends list
/* Add a new friend to the friends list
client_id must be CLIENT_ID_SIZE bytes long.
returns 0 if success
returns 1 if failure (friends list is full) */
int DHT_addfriend(uint8_t *client_id);
int DHT_addfriend(uint8_t *client_id);
/* Delete a friend from the friends list
/* Delete a friend from the friends list
client_id must be CLIENT_ID_SIZE bytes long.
returns 0 if success
returns 1 if failure (client_id not in friends list) */
int DHT_delfriend(uint8_t *client_id);
int DHT_delfriend(uint8_t *client_id);
/* Get ip of friend
/* Get ip of friend
client_id must be CLIENT_ID_SIZE bytes long.
ip must be 4 bytes long.
port must be 2 bytes long.
returns ip if success
returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
returns ip of 1 if friend is not in list. */
IP_Port DHT_getfriendip(uint8_t *client_id);
IP_Port DHT_getfriendip(uint8_t *client_id);
/* Run this function at least a couple times per second (It's the main loop) */
void doDHT();
/* Run this function at least a couple times per second (It's the main loop) */
void doDHT();
/* if we receive a DHT packet we call this function so it can be handled.
/* if we receive a DHT packet we call this function so it can be handled.
return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */
int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
/* Use this function to bootstrap the client
/* Use this function to bootstrap the client
Sends a get nodes request to the given node with ip port and public_key */
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
/* ROUTING FUNCTIONS */
/* ROUTING FUNCTIONS */
/* send the given packet to node with client_id
/* send the given packet to node with client_id
returns -1 if failure */
int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length);
int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length);
/* Send the following packet to everyone who tells us they are connected to friend_id
/* Send the following packet to everyone who tells us they are connected to friend_id
returns the number of nodes it sent the packet to */
int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length);
int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length);
/* NAT PUNCHING FUNCTIONS */
/* NAT PUNCHING FUNCTIONS */
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
ip_portlist must be at least MAX_FRIEND_CLIENTS big
returns the number of ips returned
returns -1 if no such friend*/
int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id);
int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id);
/* SAVE/LOAD functions */
/* SAVE/LOAD functions */
/* get the size of the DHT (for saving) */
uint32_t DHT_size();
/* get the size of the DHT (for saving) */
uint32_t DHT_size();
/* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t *data);
/* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t *data);
/* load the DHT from data of size size;
/* load the DHT from data of size size;
return -1 if failure
return 0 if success */
int DHT_load(uint8_t *data, uint32_t size);
int DHT_load(uint8_t *data, uint32_t size);
/* returns 0 if we are not connected to the DHT
/* returns 0 if we are not connected to the DHT
returns 1 if we are */
int DHT_isconnected();
int DHT_isconnected();
#ifdef __cplusplus
}

View File

@ -32,14 +32,14 @@
extern "C" {
#endif
/*Send a LAN discovery pcaket to the broadcast address with port port*/
int send_LANdiscovery(uint16_t port);
/*Send a LAN discovery pcaket to the broadcast address with port port*/
int send_LANdiscovery(uint16_t port);
/* if we receive a packet we call this function so it can be handled.
/* if we receive a packet we call this function so it can be handled.
return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);

View File

@ -47,7 +47,7 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION
typedef struct {
uint8_t data[MAX_DATA_SIZE];
uint16_t size;
}Data;
} Data;
typedef struct {
IP_Port ip_port;
@ -82,7 +82,7 @@ typedef struct {
uint8_t recv_counter;
uint8_t send_counter;
uint8_t timeout; /* connection timeout in seconds. */
}Connection;
} Connection;
#define MAX_CONNECTIONS 256
@ -95,7 +95,8 @@ static Connection connections[MAX_CONNECTIONS];
/* get connection id from IP_Port
return -1 if there are no connections like we are looking for
return id if it found it */
int getconnection_id(IP_Port ip_port) {
int getconnection_id(IP_Port ip_port)
{
uint32_t i;
for (i = 0; i < MAX_CONNECTIONS; ++i) {
if (connections[i].ip_port.ip.i == ip_port.ip.i &&
@ -684,8 +685,7 @@ void adjustRates()
if (sendqueue(i) != 0) {
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
connections[i].SYNC_rate = MAX_SYNC_RATE;
}
else if (connections[i].last_recvdata + 1000000UL > temp_time)
} else if (connections[i].last_recvdata + 1000000UL > temp_time)
connections[i].SYNC_rate = MAX_SYNC_RATE;
else
connections[i].SYNC_rate = SYNC_RATE;

View File

@ -30,74 +30,74 @@
extern "C" {
#endif
/* maximum length of the data in the data packets */
/* maximum length of the data in the data packets */
#define MAX_DATA_SIZE 1024
/* Functions */
/* Functions */
/* initialize a new connection to ip_port
/* initialize a new connection to ip_port
returns an integer corresponding to the connection id.
return -1 if it could not initialize the connection.
if there already was an existing connection to that ip_port return its number. */
int new_connection(IP_Port ip_port);
int new_connection(IP_Port ip_port);
/* get connection id from IP_Port
/* get connection id from IP_Port
return -1 if there are no connections like we are looking for
return id if it found it */
int getconnection_id(IP_Port ip_port);
int getconnection_id(IP_Port ip_port);
/* returns an integer corresponding to the next connection in our imcoming connection list
/* returns an integer corresponding to the next connection in our imcoming connection list
return -1 if there are no new incoming connections in the list. */
int incoming_connection();
int incoming_connection();
/* return -1 if it could not kill the connection.
/* return -1 if it could not kill the connection.
return 0 if killed successfully */
int kill_connection(int connection_id);
int kill_connection(int connection_id);
/* kill connection in seconds seconds.
/* kill connection in seconds seconds.
return -1 if it can not kill the connection.
return 0 if it will kill it */
int kill_connection_in(int connection_id, uint32_t seconds);
int kill_connection_in(int connection_id, uint32_t seconds);
/* returns the ip_port of the corresponding connection.
/* returns the ip_port of the corresponding connection.
return 0 if there is no such connection. */
IP_Port connection_ip(int connection_id);
IP_Port connection_ip(int connection_id);
/* returns the id of the next packet in the queue
/* returns the id of the next packet in the queue
return -1 if no packet in queue */
char id_packet(int connection_id);
char id_packet(int connection_id);
/* return 0 if there is no received data in the buffer.
/* return 0 if there is no received data in the buffer.
return length of received packet if successful */
int read_packet(int connection_id, uint8_t *data);
int read_packet(int 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 */
int write_packet(int connection_id, uint8_t *data, uint32_t length);
int write_packet(int connection_id, uint8_t *data, uint32_t length);
/* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id);
/* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id);
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id);
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id);
/* check if connection is connected
/* check if connection is connected
return 0 no.
return 1 if attempting handshake
return 2 if handshake is done
return 3 if fully connected
return 4 if timed out and wating to be killed */
int is_connected(int connection_id);
int is_connected(int connection_id);
/* Call this function a couple times per second
/* Call this function a couple times per second
It's the main loop. */
void doLossless_UDP();
void doLossless_UDP();
/* if we receive a Lossless_UDP packet we call this function so it can be handled.
/* if we receive a Lossless_UDP packet we call this function so it can be handled.
return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
#ifdef __cplusplus
}

View File

@ -349,7 +349,8 @@ int initMessenger()
//TODO: make this function not suck.
static void doFriends()
{/* TODO: add incoming connections and some other stuff. */
{
/* TODO: add incoming connections and some other stuff. */
uint32_t i;
int len;
uint8_t temp[MAX_DATA_SIZE];
@ -421,8 +422,7 @@ static void doFriends()
break;
}
}
}
else {
} else {
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
crypto_kill(friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1;

View File

@ -42,111 +42,111 @@ extern "C" {
#define PACKET_ID_USERSTATUS 49
#define PACKET_ID_MESSAGE 64
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
to an absurdly large number later */
/* add a friend
/* add a friend
set the data that will be sent along with friend request
client_id is the client id of the friend
data is the data and length is the length
returns the friend number if success
return -1 if failure. */
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
/* add a friend without sending a friendrequest.
/* add a friend without sending a friendrequest.
returns the friend number if success
return -1 if failure. */
int m_addfriend_norequest(uint8_t *client_id);
int m_addfriend_norequest(uint8_t *client_id);
/* return the friend id associated to that client id.
/* return the friend id associated to that client id.
return -1 if no such friend */
int getfriend_id(uint8_t *client_id);
int getfriend_id(uint8_t *client_id);
/* copies the public key associated to that friend id into client_id buffer.
/* copies the public key associated to that friend id into client_id buffer.
make sure that client_id is of size CLIENT_ID_SIZE.
return 0 if success
return -1 if failure */
int getclient_id(int friend_id, uint8_t *client_id);
int getclient_id(int friend_id, uint8_t *client_id);
/* remove a friend */
int m_delfriend(int friendnumber);
/* remove a friend */
int m_delfriend(int friendnumber);
/* return 4 if friend is online
/* return 4 if friend is online
return 3 if friend is confirmed
return 2 if the friend request was sent
return 1 if the friend was added
return 0 if there is no friend with that number */
int m_friendstatus(int friendnumber);
int m_friendstatus(int friendnumber);
/* send a text chat message to an online friend
/* send a text chat message to an online friend
returns 1 if packet was successfully put into the send queue
return 0 if it was not */
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
/* Set our nickname
/* Set our nickname
name must be a string of maximum MAX_NAME_LENGTH length.
return 0 if success
return -1 if failure */
int setname(uint8_t *name, uint16_t length);
int setname(uint8_t *name, uint16_t length);
/* get name of friendnumber
/* get name of friendnumber
put it in name
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
return 0 if success
return -1 if failure */
int getname(int friendnumber, uint8_t *name);
int getname(int friendnumber, uint8_t *name);
/* set our user status
/* set our user status
you are responsible for freeing status after
returns 0 on success, -1 on failure */
int m_set_userstatus(uint8_t *status, uint16_t length);
int m_set_userstatus(uint8_t *status, uint16_t length);
/* return the length of friendnumber's user status,
/* return the length of friendnumber's user status,
including null
pass it into malloc */
int m_get_userstatus_size(int friendnumber);
int m_get_userstatus_size(int friendnumber);
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
get the size you need to allocate from m_get_userstatus_size */
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
/* set the function that will be executed when a friend request is received.
/* set the function that will be executed when a friend request is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
/* set the function that will be executed when a message from a friend is received.
/* set the function that will be executed when a message from a friend is received.
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
/* set the callback for name changes
/* set the callback for name changes
function(int friendnumber, uint8_t *newname, uint16_t length)
you are not responsible for freeing newname */
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
/* set the callback for user status changes
/* set the callback for user status changes
function(int friendnumber, uint8_t *newstatus, uint16_t length)
you are not responsible for freeing newstatus */
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
/* run this at startup
/* run this at startup
returns 0 if no connection problems
returns -1 if there are problems */
int initMessenger();
int initMessenger();
/* the main loop that needs to be run at least 200 times per second */
void doMessenger();
/* the main loop that needs to be run at least 200 times per second */
void doMessenger();
/* SAVING AND LOADING FUNCTIONS: */
/* SAVING AND LOADING FUNCTIONS: */
/* returns the size of the messenger data (for saving) */
uint32_t Messenger_size();
/* returns the size of the messenger data (for saving) */
uint32_t Messenger_size();
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
void Messenger_save(uint8_t *data);
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
void Messenger_save(uint8_t *data);
/* load the messenger from data of size length */
int Messenger_load(uint8_t *data, uint32_t length);
/* load the messenger from data of size length */
int Messenger_load(uint8_t *data, uint32_t length);
#ifdef __cplusplus
}

View File

@ -61,7 +61,8 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
static uint8_t handle_friendrequest_isset = 0;
/* set the function that will be executed when a friend request is received. */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) {
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
{
handle_friendrequest = function;
handle_friendrequest_isset = 1;
}
@ -76,7 +77,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES]
static uint16_t recieved_requests_index;
/*Add to list of recieved friend requests*/
static void addto_recievedlist(uint8_t * client_id) {
static void addto_recievedlist(uint8_t * client_id)
{
if (recieved_requests_index >= MAX_RECIEVED_STORED)
recieved_requests_index = 0;
@ -86,7 +88,8 @@ static void addto_recievedlist(uint8_t * client_id) {
/* Check if a friend request was already recieved
return 0 if not, 1 if we did */
static int request_recieved(uint8_t * client_id) {
static int request_recieved(uint8_t * client_id)
{
uint32_t i;
for (i = 0; i < MAX_RECIEVED_STORED; ++i) {
@ -98,7 +101,8 @@ static int request_recieved(uint8_t * client_id) {
}
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) {
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{
if (packet[0] == 32) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@ -118,8 +122,7 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) {
addto_recievedlist(public_key);
(*handle_friendrequest)(public_key, data, len);
}
else {/* if request is not for us, try routing it. */
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;
}

View File

@ -31,18 +31,18 @@
extern "C" {
#endif
/* Try to send a friendrequest to peer with public_key
/* Try to send a friendrequest to peer with public_key
data is the data in the request and length is the length. */
int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
/* set the function that will be executed when a friend request for us is received.
/* set the function that will be executed when a friend request for us is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
/* if we receive a packet we call this function so it can be handled.
/* if we receive a packet we call this function so it can be handled.
return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */
int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
#ifdef __cplusplus
}

View File

@ -211,8 +211,7 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
length <= MAX_DATA_SIZE + ENCRYPTION_PADDING &&
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
{
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
uint8_t nonce[crypto_box_NONCEBYTES];
memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
@ -221,8 +220,7 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
if(len1 == -1)
return -1;
return len1;
}
else
} else
return -1;
}
@ -257,8 +255,7 @@ int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
{
int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
+ crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad)
{
+ crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) {
return 0;
}
if (data[0] != 2)
@ -317,8 +314,7 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
crypto_connections[i].sessionpublic_key) == 1)
{
crypto_connections[i].sessionpublic_key) == 1) {
increment_nonce(crypto_connections[i].recv_nonce);
return i;
}
@ -401,8 +397,7 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
crypto_connections[i].sessionpublic_key) == 1)
{
crypto_connections[i].sessionpublic_key) == 1) {
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 */
@ -504,8 +499,7 @@ static void receive_crypto()
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
} else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does
crypto_kill(crypto_connections[i].number);
}
@ -524,11 +518,9 @@ static void receive_crypto()
/* 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
} else
crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does
}
else if(id_packet(crypto_connections[i].number) != -1)
} else if(id_packet(crypto_connections[i].number) != -1)
/* This should not happen
kill the connection if it does */
crypto_kill(crypto_connections[i].number);

View File

@ -30,102 +30,102 @@
extern "C" {
#endif
/* Our public key. */
extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
/* 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
/* 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,
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
/* 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,
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. */
void random_nonce(uint8_t *nonce);
/* 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 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);
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 */
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);
/* create a request to peer with public_key.
/* create a request to peer with public_key.
packet must be an array of MAX_DATA_SIZE big.
Data represents the data we send with the request with length being the length of the data.
request_id is the id of the request (32 = friend request, 254 = ping request)
returns -1 on failure
returns the length of the created packet on success */
int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id);
int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id);
/* puts the senders public key in the request in public_key, the data from the request
/* puts the senders public key in the request in public_key, the data from the request
in data if a friend or ping request was sent to us and returns the length of the data.
packet is the request packet and length is its length
return -1 if not valid request. */
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length);
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length);
/* 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 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 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 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);
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
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.
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. */
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 */
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 */
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. */
void initNetCrypto();
void initNetCrypto();
/* main loop */
void doNetCrypto();
/* main loop */
void doNetCrypto();
#ifdef __cplusplus
}

View File

@ -27,7 +27,7 @@
uint64_t current_time()
{
uint64_t time;
#ifdef WIN32
#ifdef WIN32
/* This probably works fine */
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
@ -36,24 +36,24 @@ uint64_t current_time()
time |= ft.dwLowDateTime;
time -= 116444736000000000UL;
return time/10;
#else
#else
struct timeval a;
gettimeofday(&a, NULL);
time = 1000000UL*a.tv_sec + a.tv_usec;
return time;
#endif
#endif
}
/* 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
#ifndef VANILLA_NACL
//NOTE: this function comes from libsodium
return randombytes_random();
#else
#else
return random();
#endif
#endif
}
/* our UDP socket, a global variable. */
@ -74,11 +74,11 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
{
ADDR addr;
#ifdef WIN32
#ifdef WIN32
int addrlen = sizeof(addr);
#else
#else
uint32_t addrlen = sizeof(addr);
#endif
#endif
(*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen);
if (*(int32_t*)length <= 0)
return -1; /* nothing received or empty packet */
@ -96,26 +96,26 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
returns -1 if there are problems */
int init_networking(IP ip, uint16_t port)
{
#ifdef WIN32
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
return -1;
#else
#else
srandom((uint32_t)current_time());
#endif
#endif
srand((uint32_t)current_time());
/* initialize our socket */
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Check for socket error */
#ifdef WIN32
#ifdef WIN32
if (sock == INVALID_SOCKET) /* MSDN recommends this */
return -1;
#else
#else
if (sock < 0)
return -1;
#endif
#endif
/* Functions to increase the size of the send and receive UDP buffers
NOTE: uncomment if necessary */
@ -135,14 +135,14 @@ int init_networking(IP ip, uint16_t port)
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast));
/* Set socket nonblocking */
#ifdef WIN32
#ifdef WIN32
/* I think this works for windows */
u_long mode = 1;
/* ioctl(sock, FIONBIO, &mode); */
ioctlsocket(sock, FIONBIO, &mode);
#else
#else
fcntl(sock, F_SETFL, O_NONBLOCK, 1);
#endif
#endif
/* Bind our socket to port PORT and address 0.0.0.0 */
ADDR addr = {AF_INET, htons(port), ip};
@ -155,12 +155,12 @@ int init_networking(IP ip, uint16_t port)
/* function to cleanup networking stuff */
void shutdown_networking()
{
#ifdef WIN32
#ifdef WIN32
closesocket(sock);
WSACleanup();
#else
#else
close(sock);
#endif
#endif
return;
}

View File

@ -69,63 +69,63 @@ extern "C" {
#define MAX_UDP_PACKET_SIZE 65507
typedef union {
typedef union {
uint8_t c[4];
uint16_t s[2];
uint32_t i;
} IP;
} IP;
typedef struct {
typedef struct {
IP ip;
uint16_t port;
/* not used for anything right now */
uint16_t padding;
} IP_Port;
} IP_Port;
typedef struct {
typedef struct {
int16_t family;
uint16_t port;
IP ip;
uint8_t zeroes[8];
#ifdef ENABLE_IPV6
#ifdef ENABLE_IPV6
uint8_t zeroes2[12];
#endif
} ADDR;
#endif
} ADDR;
/* returns current time in milleseconds since the epoch. */
uint64_t current_time();
/* returns current time in milleseconds since the epoch. */
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 */
uint32_t random_int();
uint32_t random_int();
/* Basic network functions: */
/* 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);
/* 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
/* 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);
int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
/* initialize networking
/* 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
returns -1 if there were problems */
int init_networking(IP ip, uint16_t port);
int init_networking(IP ip, uint16_t port);
/* function to cleanup networking stuff(doesn't do much right now) */
void shutdown_networking();
/* function to cleanup networking stuff(doesn't do much right now) */
void shutdown_networking();
/* resolves provided address to a binary data in network byte order
/* resolves provided address to a binary data in network byte order
address is ASCII null terminated string
address should represent IPv4, IPv6 or a hostname
on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
on failure returns -1 */
int resolve_addr(char *address);
int resolve_addr(char *address);
#ifdef __cplusplus
}