mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Fixed small astyle problems.
This commit is contained in:
parent
14b43651c1
commit
4edf2207fe
108
core/DHT.h
108
core/DHT.h
|
@ -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
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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.
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* ROUTING FUNCTIONS */
|
||||
/* ROUTING FUNCTIONS */
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* NAT PUNCHING FUNCTIONS */
|
||||
/* NAT PUNCHING FUNCTIONS */
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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;
|
||||
return -1 if failure
|
||||
return 0 if success */
|
||||
int DHT_load(uint8_t *data, uint32_t 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);
|
||||
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
returns 1 if we are */
|
||||
int DHT_isconnected();
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
returns 1 if we are */
|
||||
int DHT_isconnected();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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();
|
||||
/* 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();
|
||||
|
||||
/* return -1 if it could not kill the connection.
|
||||
return 0 if killed successfully */
|
||||
int kill_connection(int connection_id);
|
||||
/* return -1 if it could not kill the connection.
|
||||
return 0 if killed successfully */
|
||||
int kill_connection(int connection_id);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* returns the ip_port of the corresponding connection.
|
||||
return 0 if there is no such connection. */
|
||||
IP_Port connection_ip(int connection_id);
|
||||
/* returns the ip_port of the corresponding connection.
|
||||
return 0 if there is no such connection. */
|
||||
IP_Port connection_ip(int connection_id);
|
||||
|
||||
/* returns the id of the next packet in the queue
|
||||
return -1 if no packet in queue */
|
||||
char id_packet(int connection_id);
|
||||
/* returns the id of the next packet in the queue
|
||||
return -1 if no packet in queue */
|
||||
char id_packet(int connection_id);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
/* Call this function a couple times per second
|
||||
It's the main loop. */
|
||||
void doLossless_UDP();
|
||||
/* Call this function a couple times per second
|
||||
It's the main loop. */
|
||||
void doLossless_UDP();
|
||||
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
164
core/Messenger.h
164
core/Messenger.h
|
@ -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
|
||||
to an absurdly large number later */
|
||||
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
||||
to an absurdly large number later */
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* return the friend id associated to that client id.
|
||||
return -1 if no such friend */
|
||||
int getfriend_id(uint8_t *client_id);
|
||||
/* return the friend id associated to that client id.
|
||||
return -1 if no such friend */
|
||||
int getfriend_id(uint8_t *client_id);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* remove a friend */
|
||||
int m_delfriend(int friendnumber);
|
||||
/* remove a friend */
|
||||
int m_delfriend(int friendnumber);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* return the length of friendnumber's user status,
|
||||
including null
|
||||
pass it into malloc */
|
||||
int m_get_userstatus_size(int friendnumber);
|
||||
/* return the length of friendnumber's user status,
|
||||
including null
|
||||
pass it into malloc */
|
||||
int m_get_userstatus_size(int friendnumber);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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));
|
||||
/* 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));
|
||||
|
||||
/* 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));
|
||||
/* 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));
|
||||
|
||||
/* 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));
|
||||
/* 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));
|
||||
|
||||
/* 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));
|
||||
/* 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));
|
||||
|
||||
/* run this at startup
|
||||
returns 0 if no connection problems
|
||||
returns -1 if there are problems */
|
||||
int initMessenger();
|
||||
/* run this at startup
|
||||
returns 0 if no connection problems
|
||||
returns -1 if there are problems */
|
||||
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
|
||||
}
|
||||
|
|
|
@ -31,18 +31,18 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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));
|
||||
/* 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));
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -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
|
||||
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);
|
||||
/* 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. */
|
||||
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
||||
uint8_t *encrypted, uint32_t length, uint8_t *plain);
|
||||
/* 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. */
|
||||
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 -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 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 */
|
||||
int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
/* 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. */
|
||||
int crypto_kill(int crypt_connection_id);
|
||||
/* 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 */
|
||||
int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key);
|
||||
/* 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 */
|
||||
int accept_crypto_inbound(int connection_id, 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 */
|
||||
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 */
|
||||
int is_cryptoconnected(int crypt_connection_id);
|
||||
/* 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. */
|
||||
void new_keys();
|
||||
/* 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 */
|
||||
void save_keys(uint8_t * keys);
|
||||
/* 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 */
|
||||
void load_keys(uint8_t * keys);
|
||||
/* 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. */
|
||||
void initNetCrypto();
|
||||
/* run this to (re)initialize net_crypto
|
||||
sets all the global connection variables to their default values. */
|
||||
void initNetCrypto();
|
||||
|
||||
/* main loop */
|
||||
void doNetCrypto();
|
||||
/* main loop */
|
||||
void doNetCrypto();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -69,63 +69,63 @@ extern "C" {
|
|||
|
||||
#define MAX_UDP_PACKET_SIZE 65507
|
||||
|
||||
typedef union {
|
||||
uint8_t c[4];
|
||||
uint16_t s[2];
|
||||
uint32_t i;
|
||||
} IP;
|
||||
typedef union {
|
||||
uint8_t c[4];
|
||||
uint16_t s[2];
|
||||
uint32_t i;
|
||||
} IP;
|
||||
|
||||
typedef struct {
|
||||
IP ip;
|
||||
uint16_t port;
|
||||
/* not used for anything right now */
|
||||
uint16_t padding;
|
||||
} IP_Port;
|
||||
typedef struct {
|
||||
IP ip;
|
||||
uint16_t port;
|
||||
/* not used for anything right now */
|
||||
uint16_t padding;
|
||||
} IP_Port;
|
||||
|
||||
typedef struct {
|
||||
int16_t family;
|
||||
uint16_t port;
|
||||
IP ip;
|
||||
uint8_t zeroes[8];
|
||||
typedef struct {
|
||||
int16_t family;
|
||||
uint16_t port;
|
||||
IP ip;
|
||||
uint8_t zeroes[8];
|
||||
#ifdef ENABLE_IPV6
|
||||
uint8_t zeroes2[12];
|
||||
uint8_t zeroes2[12];
|
||||
#endif
|
||||
} ADDR;
|
||||
} 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
|
||||
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
||||
uint32_t random_int();
|
||||
/* 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 */
|
||||
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
|
||||
the packet data into data
|
||||
the packet length into length. */
|
||||
int receivepacket(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. */
|
||||
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
|
||||
returns -1 if there were problems */
|
||||
int init_networking(IP ip, uint16_t port);
|
||||
/* 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);
|
||||
|
||||
/* 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
|
||||
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);
|
||||
/* 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user