Merge branch 'master' of https://github.com/JamoBox/ProjectTox-Core into JamoBox-master

This commit is contained in:
irungentoo 2013-09-02 15:59:28 -04:00
commit a4bf25801d
16 changed files with 432 additions and 363 deletions

View File

@ -72,10 +72,11 @@ Client_data *DHT_get_close_list(DHT *dht)
return dht->close_clientlist;
}
/* Compares client_id1 and client_id2 with client_id
* return 0 if both are same distance
* return 1 if client_id1 is closer
* return 2 if client_id2 is closer
/* Compares client_id1 and client_id2 with client_id.
*
* return 0 if both are same distance.
* return 1 if client_id1 is closer.
* return 2 if client_id2 is closer.
*/
static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
{
@ -128,9 +129,9 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
/* Check if client with client_id is already in list of length length.
* If it is then set its corresponding timestamp to current time.
* If the id is already in the list with a different ip_port, update it.
* return True(1) or False(0)
* TODO: Maybe optimize this.
*
* TODO: maybe optimize this.
* return True(1) or False(0)
*/
static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
{
@ -156,7 +157,9 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id
}
/* Check if client with client_id is already in node format list of length length.
* return True(1) or False(0)
*
* return 1 if true.
* return 2 if false.
*/
static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
{
@ -170,7 +173,8 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien
return 0;
}
/* Returns the friend number from the client_id, or -1 if a failure occurs
/* return friend number from the client_id.
* return -1 if a failure occurs.
*/
static int friend_number(DHT *dht, uint8_t *client_id)
{
@ -274,9 +278,10 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list
return num_nodes;
}
/* Replace first bad (or empty) node with this one
* return 0 if successful
* return 1 if not (list contains no bad nodes)
/* Replace first bad (or empty) node with this one.
*
* return 0 if successful.
* return 1 if not (list contains no bad nodes).
*/
static int replace_bad( Client_data *list,
uint32_t length,
@ -302,8 +307,8 @@ static int replace_bad( Client_data *list,
return 1;
}
/*Sort the list. It will be sorted from furthest to closest.
* Turns list into data that quick sort can use and reverts it back.
/* Sort the list. It will be sorted from furthest to closest.
* Turns list into data that quick sort can use and reverts it back.
*/
static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
{
@ -350,7 +355,7 @@ static int replace_good( Client_data *list,
}
/* Attempt to add client with ip_port and client_id to the friends client list
* and close_clientlist
* and close_clientlist.
*/
void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
{
@ -361,7 +366,7 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
*/
if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
/* If we can't replace bad nodes we try replacing good ones */
/* If we can't replace bad nodes we try replacing good ones. */
replace_good( dht->close_clientlist,
LCLIENT_LIST,
client_id,
@ -801,7 +806,8 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
}
/* Send the given packet to node with client_id
* returns -1 if failure.
*
* return -1 if failure.
*/
int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
{
@ -815,11 +821,12 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
return -1;
}
/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist
* ip_portlist must be at least MAX_FRIEND_CLIENTS big
* returns the number of ips returned
* return 0 if we are connected to friend or if no ips were found.
* returns -1 if no such friend
/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist.
* ip_portlist must be at least MAX_FRIEND_CLIENTS big.
*
* return the number of ips returned.
* return 0 if we are connected to friend or if no ips were found.
* return -1 if no such friend.
*/
static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
{
@ -836,7 +843,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
client = &friend->client_list[i];
/* If ip is not zero and node is good */
/* If ip is not zero and node is good. */
if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
if (id_equal(client->client_id, friend->client_id))
@ -852,9 +859,9 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
/* 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.
*
* Only works if more than (MAX_FRIEND_CLIENTS / 2) return an ip for friend.
* return ip for friend.
* return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2).
*/
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
{
@ -878,7 +885,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
client = &friend->client_list[i];
/* If ip is not zero and node is good */
/* If ip is not zero and node is good. */
if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
@ -891,8 +898,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
}
/* Send the following packet to one random person who tells us they are connected to friend_id.
* returns the number of nodes it sent the packet to
*/
*
* return number of nodes the packet was sent to.
*/
static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
{
int num = friend_number(dht, friend_id);
@ -931,9 +939,10 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
/* 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
* return 0 if we are connected to friend or if no ips were found.
* returns -1 if no such friend
*
* return number of ips returned.
* return 0 if we are connected to friend or if no ips were found.
* return -1 if no such friend.
*/
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
{
@ -1014,7 +1023,8 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey,
/* Get the most common ip in the ip_portlist.
* Only return ip if it appears in list min_num or more.
* len must not be bigger than MAX_FRIEND_CLIENTS.
* return ip of 0 if failure.
*
* return ip of 0 if failure.
*/
static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
{
@ -1040,9 +1050,10 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
}
/* Return all the ports for one ip in a list.
* portlist must be at least len long
* where len is the length of ip_portlist
* returns the number of ports and puts the list of ports in portlist.
* portlist must be at least len long,
* where len is the length of ip_portlist.
*
* return number of ports and puts the list of ports in portlist.
*/
static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip)
{
@ -1068,7 +1079,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
uint32_t top = dht->friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS;
for (i = dht->friends_list[friend_num].punching_index; i != top; i++) {
/* TODO: improve port guessing algorithm */
/* TODO: Improve port guessing algorithm. */
uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
IP_Port pinging = {{ip, htons(port), 0}};
send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id);
@ -1086,7 +1097,7 @@ static void do_NAT(DHT *dht)
IP_Port ip_list[MAX_FRIEND_CLIENTS];
int num = friend_iplist(dht, ip_list, i);
/* If already connected or friend is not online don't try to hole punch */
/* If already connected or friend is not online don't try to hole punch. */
if (num < MAX_FRIEND_CLIENTS / 2)
continue;
@ -1124,8 +1135,9 @@ static void do_NAT(DHT *dht)
* If the list is full the nodes farthest from our client_id are replaced.
* The purpose of this list is to enable quick integration of new nodes into the
* network while preventing amplification attacks.
* return 0 if node was added.
* return -1 if node was not added.
*
* return 0 if node was added.
* return -1 if node was not added.
*/
int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
{
@ -1156,7 +1168,7 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port)
}
/* Ping all the valid nodes in the toping list every TIME_TOPING seconds.
* This function must be run at least once every TIME_TOPING seconds
* This function must be run at least once every TIME_TOPING seconds.
*/
static void do_toping(DHT *dht)
{
@ -1233,8 +1245,9 @@ void DHT_save(DHT *dht, uint8_t *data)
}
/* Load the DHT from data of size size.
* return -1 if failure.
* return 0 if success.
*
* return -1 if failure.
* return 0 if success.
*/
int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
{
@ -1278,8 +1291,8 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
return 0;
}
/* returns 0 if we are not connected to the DHT.
* returns 1 if we are.
/* return 0 if we are not connected to the DHT.
* return 1 if we are.
*/
int DHT_isconnected(DHT *dht)
{

View File

@ -104,16 +104,18 @@ typedef struct {
Client_data *DHT_get_close_list(DHT *dht);
/* 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).
* client_id must be CLIENT_ID_SIZE bytes long.
*
* return 0 if success.
* return 1 if failure (friends list is full).
*/
int DHT_addfriend(DHT *dht, 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).
* client_id must be CLIENT_ID_SIZE bytes long.
*
* return 0 if success.
* return 1 if failure (client_id not in friends list).
*/
int DHT_delfriend(DHT *dht, uint8_t *client_id);
@ -121,9 +123,10 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id);
* 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.
*
* return ip if success.
* return ip of 0 if failure (This means the friend is either offline or we have not found him yet).
* return ip of 1 if friend is not in list.
*/
IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id);
@ -141,27 +144,32 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key);
* If the list is full the nodes farthest from our client_id are replaced.
* The purpose of this list is to enable quick integration of new nodes into the
* network while preventing amplification attacks.
* return 0 if node was added.
* return -1 if node was not added.
*
* return 0 if node was added.
* return -1 if node was not added.
*/
int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port);
/* ROUTING FUNCTIONS */
/* Send the given packet to node with client_id.
returns -1 if failure. */
*
* return -1 if failure.
*/
int route_packet(DHT *dht, 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.
*
* return number of nodes it sent the packet to.
*/
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length);
/* 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.
* ip_portlist must be at least MAX_FRIEND_CLIENTS big.
*
* returns number of ips returned.
* returns -1 if no such friend.
*/
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id);
@ -180,13 +188,14 @@ DHT *new_DHT(Net_Crypto *c);
void kill_DHT(DHT *dht);
/* Load the DHT from data of size size.
*
* return -1 if failure.
* return 0 if success.
*/
int DHT_load(DHT *dht, uint8_t *data, uint32_t size);
/* returns 0 if we are not connected to the DHT
* returns 1 if we are
/* return 0 if we are not connected to the DHT.
* return 1 if we are.
*/
int DHT_isconnected(DHT *dht);

View File

@ -27,8 +27,9 @@
#ifdef __linux
/* Get the first working broadcast address that's not from "lo".
* returns higher than 0 on success.
* returns 0 on error.
*
* return higher than 0 on success.
* return 0 on error.
*/
static uint32_t get_broadcast(void)
{
@ -100,8 +101,8 @@ static IP broadcast_ip(void)
return ip;
}
/* return 0 if ip is a LAN ip.
* return -1 if it is not.
/* return 0 if ip is a LAN ip.
* return -1 if it is not.
*/
static int LAN_ip(IP ip)
{

View File

@ -28,7 +28,7 @@
#include "DHT.h"
/* used for get_broadcast(). */
/* Used for get_broadcast(). */
#ifdef __linux
#include <sys/ioctl.h>
#include <arpa/inet.h>

View File

@ -33,6 +33,7 @@
/*
* Get connection id from IP_Port.
*
* return -1 if there are no connections like we are looking for.
* return id if it found it.
*/
@ -87,8 +88,9 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source)
/*
* Initialize a new connection to ip_port
* Returns an integer corresponding to the connection id.
* Return -1 if it could not initialize the connectiont
*
* return an integer corresponding to the connection id.
* return -1 if it could not initialize the connectiont
* If there already was an existing connection to that ip_port return its number.
*/
int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
@ -141,8 +143,9 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
/*
* Initialize a new inbound connection from ip_port.
* return an integer corresponding to the connection id.
* return -1 if it could not initialize the connection.
*
* return an integer corresponding to the connection id.
* return -1 if it could not initialize the connection.
*/
static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
{
@ -179,10 +182,10 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
.last_sent = current_time(),
.send_counter = 127,
/* add randomness to timeout to prevent connections getting stuck in a loop. */
/* Add randomness to timeout to prevent connections getting stuck in a loop. */
.timeout = timeout,
/* if this connection isn't handled within the timeout kill it. */
/* If this connection isn't handled within the timeout kill it. */
.killat = current_time() + 1000000UL * timeout
};
@ -190,8 +193,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
}
/*
* return an integer corresponding to the next connection in our incoming connection list.
* return -1 if there are no new incoming connections in the list.
* return an integer corresponding to the next connection in our incoming connection list.
* return -1 if there are no new incoming connections in the list.
*/
int incoming_connection(Lossless_UDP *ludp)
{
@ -220,9 +223,8 @@ static void free_connections(Lossless_UDP *ludp)
return tox_array_pop(&ludp->connections, ludp->connections.len - i);
}
/*
* return -1 if it could not kill the connection.
* return 0 if killed successfully.
/* return -1 if it could not kill the connection.
* return 0 if killed successfully.
*/
int kill_connection(Lossless_UDP *ludp, int connection_id)
{
@ -243,8 +245,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
/*
* Kill connection in seconds.
* return -1 if it can not kill the connection.
* return 0 if it will kill it.
*
* return -1 if it can not kill the connection.
* return 0 if it will kill it.
*/
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
{
@ -262,11 +265,12 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
/*
* 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 waiting to be killed.
*
* return 0 if not.
* return 1 if attempting handshake.
* return 2 if handshake is done.
* return 3 if fully connected.
* return 4 if timed out and waiting to be killed.
*/
int is_connected(Lossless_UDP *ludp, int connection_id)
{
@ -276,7 +280,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
return 0;
}
/* return the ip_port of the corresponding connection. */
/* return the ip_port of the corresponding connection. */
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
{
if ((unsigned int)connection_id < ludp->connections.len)
@ -286,7 +290,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
return zero;
}
/* returns the number of packets in the queue waiting to be successfully sent. */
/* return the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
{
if ((unsigned int)connection_id >= ludp->connections.len)
@ -296,7 +300,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
return connection->sendbuff_packetnum - connection->successful_sent;
}
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
{
if ((unsigned int)connection_id >= ludp->connections.len)
@ -306,8 +310,8 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
return connection->recv_packetnum - connection->successful_read;
}
/* returns the id of the next packet in the queue.
* return -1 if no packet in queue.
/* return the id of the next packet in the queue.
* return -1 if no packet in queue.
*/
char id_packet(Lossless_UDP *ludp, int connection_id)
{
@ -322,8 +326,8 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
return -1;
}
/* return 0 if there is no received data in the buffer.
* return length of received packet if successful.
/* return 0 if there is no received data in the buffer.
* return length of received packet if successful.
*/
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
{
@ -340,9 +344,8 @@ int read_packet(Lossless_UDP *ludp, 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.
/* return 0 if data could not be put in packet queue.
* return 1 if data was put into the queue.
*/
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
{
@ -360,7 +363,7 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
/* Put the packet numbers the we are missing in requested and return the number. */
uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
{
/* don't request packets if the buffer is full. */
/* Don't request packets if the buffer is full. */
if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
return 0;
@ -448,7 +451,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack
return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
}
/* sends 1 data packet */
/* Sends 1 data packet. */
static int send_DATA(Lossless_UDP *ludp, int connection_id)
{
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@ -481,8 +484,8 @@ static int send_DATA(Lossless_UDP *ludp, int connection_id)
*/
/* return 0 if handled correctly.
* return 1 if packet is bad.
/* return 0 if handled correctly.
* return 1 if packet is bad.
*/
static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
@ -525,7 +528,9 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
return 0;
}
/* returns 1 if sync packet is valid 0 if not. */
/* return 1 if sync packet is valid.
* return 0 if not.
*/
static int SYNC_valid(uint32_t length)
{
if (length < 4 + 4 + 2)
@ -660,8 +665,9 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
/*
* Add a packet to the received buffer and set the recv_packetnum of the
* connection to its proper value.
* return 1 if data was too big.
* return 0 if not.
*
* return 1 if data was too big.
* return 0 if not.
*/
static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
{

View File

@ -55,18 +55,18 @@ typedef struct {
IP_Port ip_port;
/*
* return 0 if connection is dead.
* return 1 if attempting handshake.
* return 2 if handshake is done (we start sending SYNC packets).
* return 3 if we are sending SYNC packets and can send data.
* return 4 if the connection has timed out.
* return 0 if connection is dead.
* return 1 if attempting handshake.
* return 2 if handshake is done (we start sending SYNC packets).
* return 3 if we are sending SYNC packets and can send data.
* return 4 if the connection has timed out.
*/
uint8_t status;
/*
* return 0 if connection was not initiated by someone else.
* return 1 if incoming_connection() has returned.
* return 2 if it has not.
* return 0 if connection was not initiated by someone else.
* return 1 if incoming_connection() has returned.
* return 2 if it has not.
*/
uint8_t inbound;
@ -129,77 +129,75 @@ typedef struct {
/*
* Initialize a new connection to ip_port.
* return an integer corresponding to the connection id.
* return -1 if it could not initialize the connection.
* return number if there already was an existing connection to that ip_port.
*
* return an integer corresponding to the connection id.
* return -1 if it could not initialize the connection.
* return number if there already was an existing connection to that ip_port.
*/
int new_connection(Lossless_UDP *ludp, 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 .
*
* return -1 if there are no connections like we are looking for.
* return id if it found it .
*/
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);
/*
* return an integer corresponding to the next connection in our imcoming connection list.
* return -1 if there are no new incoming connections in the list.
/* return 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(Lossless_UDP *ludp);
/*
* return -1 if it could not kill the connection.
* return 0 if killed successfully.
/* return -1 if it could not kill the connection.
* return 0 if killed successfully.
*/
int kill_connection(Lossless_UDP *ludp, int connection_id);
/*
* Kill connection in seconds seconds.
* return -1 if it can not kill the connection.
* return 0 if it will kill it.
*
* return -1 if it can not kill the connection.
* return 0 if it will kill it.
*/
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);
/*
* returns the ip_port of the corresponding connection.
* return 0 if there is no such connection.
/* returns the ip_port of the corresponding connection.
* return 0 if there is no such connection.
*/
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);
/*
* returns the id of the next packet in the queue.
* return -1 if no packet in queue.
/* returns the id of the next packet in the queue.
* return -1 if no packet in queue.
*/
char id_packet(Lossless_UDP *ludp, int connection_id);
/*
* return 0 if there is no received data in the buffer.
* return length of received packet if successful.
/* return 0 if there is no received data in the buffer.
* return length of received packet if successful.
*/
int read_packet(Lossless_UDP *ludp, 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.
/* return 0 if data could not be put in packet queue.
* return 1 if data was put into the queue.
*/
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);
/* returns the number of packets in the queue waiting to be successfully sent. */
/* return number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
/*
* return the number of packets in the queue waiting to be successfully
* read with read_packet(...).
* return number of packets in the queue waiting to be successfully
* read with read_packet(...).
*/
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
/* Check if connection is connected:
* return 0 not.
* 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.
*
* return 0 not.
* 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(Lossless_UDP *ludp, int connection_id);

View File

@ -34,13 +34,9 @@ static uint8_t friend_not_valid(Messenger *m, int friendnumber)
return (unsigned int)friendnumber >= m->numfriends;
}
/* return 1 if we are online.
* return 0 if we are offline.
* static uint8_t online;
*/
/* Set the size of the friend list to numfriends.
* return -1 if realloc fails.
*
* return -1 if realloc fails.
*/
int realloc_friendlist(Messenger *m, uint32_t num)
{
@ -59,8 +55,8 @@ int realloc_friendlist(Messenger *m, uint32_t num)
return 0;
}
/* return the friend id associated to that public key.
* return -1 if no such friend.
/* return the friend id associated to that public key.
* return -1 if no such friend.
*/
int getfriend_id(Messenger *m, uint8_t *client_id)
{
@ -77,8 +73,9 @@ int getfriend_id(Messenger *m, 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.
*
* return 0 if success.
* return -1 if failure.
*/
int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
{
@ -92,10 +89,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
return -1;
}
/*
* return a uint16_t that represents the checksum of address of length len.
/* TODO: Another checksum algorithm might be better.
*
* TODO: Another checksum algorithm might be better.
* return a uint16_t that represents the checksum of address of length len.
*/
static uint16_t address_checksum(uint8_t *address, uint32_t len)
{
@ -110,10 +106,9 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
return check;
}
/*
* return FRIEND_ADDRESS_SIZE byte address to give to others.
* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
* return FRIEND_ADDRESS_SIZE byte address to give to others.
*/
void getaddress(Messenger *m, uint8_t *address)
{
@ -130,16 +125,17 @@ void getaddress(Messenger *m, uint8_t *address)
* Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
* TODO: add checksum.
* data is the data and length is the length.
* return the friend number if success.
* return FA_TOOLONG if message length is too long.
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return FAERR_OWNKEY if user's own key.
* return FAERR_ALREADYSENT if friend request already sent or already a friend.
* return FAERR_UNKNOWN for unknown error.
* return FAERR_BADCHECKSUM if bad checksum in address.
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return FAERR_NOMEM if increasing the friend list size fails.
*
* return the friend number if success.
* return FA_TOOLONG if message length is too long.
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return FAERR_OWNKEY if user's own key.
* return FAERR_ALREADYSENT if friend request already sent or already a friend.
* return FAERR_UNKNOWN for unknown error.
* return FAERR_BADCHECKSUM if bad checksum in address.
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return FAERR_NOMEM if increasing the friend list size fails.
*/
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
{
@ -247,6 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
}
/* Remove a friend.
*
* return 0 if success.
* return -1 if failure.
*/
@ -274,11 +271,11 @@ int m_delfriend(Messenger *m, int friendnumber)
return 0;
}
/* return FRIEND_ONLINE if friend is online.
* return FRIEND_CONFIRMED if friend is confirmed.
* return FRIEND_REQUESTED if the friend request was sent.
* return FRIEND_ADDED if the friend was added.
* return NOFRIEND if there is no friend with that number.
/* return FRIEND_ONLINE if friend is online.
* return FRIEND_CONFIRMED if friend is confirmed.
* return FRIEND_REQUESTED if the friend request was sent.
* return FRIEND_ADDED if the friend was added.
* return NOFRIEND if there is no friend with that number.
*/
int m_friendstatus(Messenger *m, int friendnumber)
{
@ -289,8 +286,9 @@ int m_friendstatus(Messenger *m, int friendnumber)
}
/* Send a text chat message to an online friend.
* return the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*
* return the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*/
uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
{
@ -322,8 +320,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
}
/* Send an action to an online friend.
* return 1 if packet was successfully put into the send queue.
* return 0 if it was not.
*
* return 1 if packet was successfully put into the send queue.
* return 0 if it was not.
*/
int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
{
@ -342,8 +341,9 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
}
/* Set the name of a friend.
* return 0 if success.
* return -1 if failure.
*
* return 0 if success.
* return -1 if failure.
*/
static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
{
@ -358,8 +358,9 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
* name must be a string of maximum MAX_NAME_LENGTH length.
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
* return 0 if success.
* return -1 if failure.
*
* return 0 if success.
* return -1 if failure.
*/
int setname(Messenger *m, uint8_t *name, uint16_t length)
{
@ -377,8 +378,9 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
}
/* Get our nickname and put it in name.
* Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
* return the length of the name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
*
* return the length of the name.
*/
uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
{
@ -395,9 +397,10 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
}
/* Get name of friendnumber and put it in name.
* Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
* return 0 if success.
* return -1 if failure.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
*
* return 0 if success.
* return -1 if failure.
*/
int getname(Messenger *m, int friendnumber, uint8_t *name)
{
@ -924,7 +927,7 @@ void doMessenger(Messenger *m)
LANdiscovery(m);
}
/* return the size of the messenger data (for saving) */
/* return size of the messenger data (for saving) */
uint32_t Messenger_size(Messenger *m)
{
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES

View File

@ -154,28 +154,27 @@ typedef struct Messenger {
} Messenger;
/*
* return FRIEND_ADDRESS_SIZE byte address to give to others.
* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
* return FRIEND_ADDRESS_SIZE byte address to give to others.
*/
void getaddress(Messenger *m, uint8_t *address);
/*
* Add a friend.
/* Add a friend.
* Set the data that will be sent along with friend request.
* address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
* data is the data and length is the length.
* returns the friend number if success.
* return -1 if message length is too long.
* return -2 if no message (message length must be >= 1 byte).
* return -3 if user's own key.
* return -4 if friend request already sent or already a friend.
* return -5 for unknown error.
* return -6 if bad checksum in address.
* return -7 if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return -8 if increasing the friend list size fails.
*
* return the friend number if success.
* return -1 if message length is too long.
* return -2 if no message (message length must be >= 1 byte).
* return -3 if user's own key.
* return -4 if friend request already sent or already a friend.
* return -5 for unknown error.
* return -6 if bad checksum in address.
* return -7 if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return -8 if increasing the friend list size fails.
*/
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
@ -186,13 +185,14 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
*/
int m_addfriend_norequest(Messenger *m, uint8_t *client_id);
/* return the friend id associated to that client id.
* return -1 if no such friend.
/* return the friend id associated to that client id.
* return -1 if no such friend.
*/
int getfriend_id(Messenger *m, 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
*/
@ -201,15 +201,16 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
/* Remove a friend. */
int m_delfriend(Messenger *m, 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.
/* 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(Messenger *m, int friendnumber);
/* Send a text chat message to an online friend.
*
* return the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*
@ -222,6 +223,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_
uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
/* Send an action to an online friend.
*
* return 1 if packet was successfully put into the send queue.
* return 0 if it was not.
*/
@ -231,8 +233,9 @@ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t lengt
* name must be a string of maximum MAX_NAME_LENGTH length.
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
* return 0 if success.
* return -1 if failure.
*
* return 0 if success.
* return -1 if failure.
*/
int setname(Messenger *m, uint8_t *name, uint16_t length);
@ -241,28 +244,30 @@ int setname(Messenger *m, uint8_t *name, uint16_t length);
* m - The messanger context to use.
* name - Pointer to a string for the name.
* nlen - The length of the string buffer.
* return length of the name.
* return 0 on error.
*
* return length of the name.
* return 0 on error.
*/
uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
/* Get name of friendnumber and put it in name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
* 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(Messenger *m, int friendnumber, uint8_t *name);
/* Set our user status.
* You are responsible for freeing status after.
* You are responsible for freeing status after.
*
* returns 0 on success.
* returns -1 on failure.
*/
int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
int m_set_userstatus(Messenger *m, USERSTATUS status);
/* return the length of friendnumber's status message,
* including null.
/* return the length of friendnumber's status message, including null.
* Pass it into malloc.
*/
int m_get_statusmessage_size(Messenger *m, int friendnumber);
@ -274,10 +279,10 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber);
int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
/* return one of USERSTATUS values.
* Values unknown to your application should be represented as USERSTATUS_NONE.
* As above, the self variant will return our own USERSTATUS.
* If friendnumber is invalid, this shall return USERSTATUS_INVALID.
/* return one of USERSTATUS values.
* Values unknown to your application should be represented as USERSTATUS_NONE.
* As above, the self variant will return our own USERSTATUS.
* If friendnumber is invalid, this shall return USERSTATUS_INVALID.
*/
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
USERSTATUS m_get_self_userstatus(Messenger *m);
@ -299,7 +304,7 @@ void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int,
void *userdata);
/* Set the function that will be executed when an action from a friend is received.
* function format is: function(int friendnumber, uint8_t * action, uint32_t length)
* Function format is: function(int friendnumber, uint8_t * action, uint32_t length)
*/
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata);
@ -311,7 +316,7 @@ void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uin
void *userdata);
/* Set the callback for status message changes.
* function(int friendnumber, uint8_t *newstatus, uint16_t length)
* Function(int friendnumber, uint8_t *newstatus, uint16_t length)
*
* You are not responsible for freeing newstatus
*/
@ -319,12 +324,12 @@ void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int,
void *userdata);
/* Set the callback for status type changes.
* function(int friendnumber, USERSTATUS kind)
* Function(int friendnumber, USERSTATUS kind)
*/
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata);
/* Set the callback for read receipts.
* function(int friendnumber, uint32_t receipt)
* Function(int friendnumber, uint32_t receipt)
*
* If you are keeping a record of returns from m_sendmessage,
* receipt might be one of those values, meaning the message
@ -348,8 +353,8 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
/* Run this at startup.
* returns allocated instance of Messenger on success.
* returns 0 if there are problems.
* return allocated instance of Messenger on success.
* return 0 if there are problems.
*/
Messenger *initMessenger(void);

View File

@ -25,9 +25,10 @@
/* Try to send a friend request to peer with public_key.
* data is the data in the request and length is the length.
* return -1 if failure.
* return 0 if it sent the friend request directly to the friend.
* return the number of peers it was routed through if it did not send it directly.
*
* return -1 if failure.
* return 0 if it sent the friend request directly to the friend.
* return the number of peers it was routed through if it did not send it directly.
*/
int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length)
{
@ -98,8 +99,9 @@ static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id)
}
/* Check if a friend request was already received.
* return 0 if it did not.
* return 1 if it did.
*
* return 0 if it did not.
* return 1 if it did.
*/
static int request_received(Friend_Requests *fr, uint8_t *client_id)
{

View File

@ -84,7 +84,7 @@ typedef struct tox_list {
struct tox_list *prev, *next;
} tox_list;
/* Returns a new tox_list_t. */
/* return new tox_list_t. */
static inline void tox_list_new(tox_list *lst)
{
lst->prev = lst->next = lst;
@ -196,10 +196,10 @@ static inline void tox_array_pop(tox_array *arr, uint32_t num)
* Quick Sort: Complexity O(nlogn)
* arr - the array to sort
* n - the sort index (should be called with n = length(arr))
* cmpfn - a function that compares two values of type type.
* cmpfn - a function that compares two values of type type.
* Must return -1, 0, 1 for a < b, a == b, and a > b respectively.
*/
/* Must be called in the header file. */
/* Must be called in the header file. */
#define declare_quick_sort(type) \
void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type));

View File

@ -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(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
{
@ -182,8 +182,8 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
return -1;
}
/* 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(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
{
@ -220,8 +220,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
* 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.
* return -1 on failure.
* return the length of the created packet on success.
*/
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
uint8_t *data, uint32_t length, uint8_t request_id)
@ -251,7 +251,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
/* 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.
*
* return -1 if not valid request.
*/
static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet,
uint16_t length)
@ -348,8 +349,9 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi
}
/* Extract secret nonce, session public key and public_key from a packet(data) with length length.
* return 1 if successful.
* return 0 if failure.
*
* return 1 if successful.
* return 0 if failure.
*/
static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce,
uint8_t *session_key, uint8_t *data, uint16_t length)
@ -381,8 +383,9 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s
}
/* 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.
*
* return -1 if there are no connections like we are looking for.
* return id if it found it.
*/
static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
{
@ -398,7 +401,8 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
}
/* Set the size of the friend list to numfriends.
* return -1 if realloc fails.
*
* return -1 if realloc fails.
*/
int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
{
@ -418,8 +422,9 @@ int realloc_cryptoconnection(Net_Crypto *c, uint32_t num)
}
/* 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.
*
* return -1 if failure.
* return crypt_connection_id of the initialized connection if everything went well.
*/
int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
{
@ -469,8 +474,9 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
}
/* 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.
*
* 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.
@ -507,8 +513,9 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
}
/* Kill a crypto connection.
* return 0 if killed successfully.
* return 1 if there was a problem.
*
* return 0 if killed successfully.
* return 1 if there was a problem.
*/
int crypto_kill(Net_Crypto *c, int crypt_connection_id)
{
@ -536,8 +543,9 @@ int crypto_kill(Net_Crypto *c, 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.
*
* return -1 if not successful.
* return the crypt_connection_id if successful.
*/
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
uint8_t *session_key)
@ -595,11 +603,11 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
return -1;
}
/* return 0 if no connection.
* return 1 we have sent a handshake.
* return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet).
* return 3 if the connection is established.
* return 4 if the connection is timed out and waiting to be killed.
/* return 0 if no connection.
* return 1 we have sent a handshake.
* return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet).
* return 3 if the connection is established.
* return 4 if the connection is timed out and waiting to be killed.
*/
int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
{
@ -633,9 +641,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys)
}
/* Adds an incoming connection to the incoming_connection list.
* returns 0 if successful
* returns 1 if failure.
* TODO: Optimize this.
*
* returns 0 if successful
* returns 1 if failure.
*/
static int new_incoming(Net_Crypto *c, int id)
{

View File

@ -80,7 +80,8 @@ typedef struct {
uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen);
/* Encrypts plain of length length to encrypted of length + 16 using the
* public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
* public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
*
* return -1 if there was a problem.
* return length of encrypted data if everything was fine.
*/
@ -89,7 +90,8 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
/* Decrypts encrypted of length length to plain of length length - 16 using the
* public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
* public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
*
* return -1 if there was a problem (decryption failed).
* return length of plain data if everything was fine.
*/
@ -113,14 +115,14 @@ int decrypt_data_fast(uint8_t *enc_key, 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(Net_Crypto *c, 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(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
@ -131,8 +133,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
* 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.
* return -1 on failure.
* return the length of the created packet on success.
*/
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
uint8_t *data, uint32_t length, uint8_t request_id);
@ -142,18 +144,21 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
void cryptopacket_registerhandler(Net_Crypto *c, uint8_t byte, cryptopacket_handler_callback cb, void *object);
/* 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.
*
* return -1 if failure.
* return crypt_connection_id of the initialized connection if everything went well.
*/
int crypto_connect(Net_Crypto *c, 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(Net_Crypto *c, 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.
*
@ -165,17 +170,18 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id);
int crypto_inbound(Net_Crypto *c, 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.
* return crypt_connection_id if successful.
*/
int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce,
uint8_t *session_key);
/* return 0 if no connection.
* return 1 we have sent a handshake
* return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet).
* return 3 if the connection is established.
* return 4 if the connection is timed out and waiting to be killed.
/* return 0 if no connection.
* return 1 we have sent a handshake
* return 2 if connexion is not confirmed yet (we have received a handshake but no empty data packet).
* return 3 if the connection is established.
* return 4 if the connection is timed out and waiting to be killed.
*/
int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id);

View File

@ -23,7 +23,7 @@
#include "network.h"
/* return current UNIX time in microseconds (us). */
/* return current UNIX time in microseconds (us). */
uint64_t current_time(void)
{
uint64_t time;
@ -44,7 +44,7 @@ uint64_t current_time(void)
#endif
}
/* 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(void)
@ -153,8 +153,8 @@ static void at_shutdown(void)
* 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 Networking_Core object if no problems
* returns NULL if there are problems.
* return Networking_Core object if no problems
* return NULL if there are problems.
*/
Networking_Core *new_networking(IP ip, uint16_t port)
{

View File

@ -128,10 +128,10 @@ typedef struct {
} Networking_Core;
/* return current time in milleseconds since the epoch. */
/* return current time in milleseconds since the epoch. */
uint64_t current_time(void);
/* 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(void);
@ -153,12 +153,12 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
void networking_poll(Networking_Core *net);
/* 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).
* 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.
* return 0 if no problems.
* return -1 if there were problems.
*/
Networking_Core *new_networking(IP ip, uint16_t port);

View File

@ -38,16 +38,17 @@ void tox_getaddress(void *tox, uint8_t *address)
* Set the data that will be sent along with friend request.
* address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
* data is the data and length is the length.
* returns the friend number if success.
* return FA_TOOLONG if message length is too long.
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return FAERR_OWNKEY if user's own key.
* return FAERR_ALREADYSENT if friend request already sent or already a friend.
* return FAERR_UNKNOWN for unknown error.
* return FAERR_BADCHECKSUM if bad checksum in address.
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return FAERR_NOMEM if increasing the friend list size fails.
*
* return the friend number if success.
* return FA_TOOLONG if message length is too long.
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return FAERR_OWNKEY if user's own key.
* return FAERR_ALREADYSENT if friend request already sent or already a friend.
* return FAERR_UNKNOWN for unknown error.
* return FAERR_BADCHECKSUM if bad checksum in address.
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return FAERR_NOMEM if increasing the friend list size fails.
*/
int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
{
@ -56,7 +57,8 @@ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
}
/* Add a friend without sending a friendrequest.
* returns the friend number if success.
*
* return the friend number if success.
* return -1 if failure.
*/
int tox_addfriend_norequest(void *tox, uint8_t *client_id)
@ -65,8 +67,8 @@ int tox_addfriend_norequest(void *tox, uint8_t *client_id)
return m_addfriend_norequest(m, client_id);
}
/* return the friend id associated to that client id.
* return -1 if no such friend.
/* return the friend id associated to that client id.
* return -1 if no such friend.
*/
int tox_getfriend_id(void *tox, uint8_t *client_id)
{
@ -75,7 +77,8 @@ int tox_getfriend_id(void *tox, 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.
* Make sure that client_id is of size CLIENT_ID_SIZE.
*
* return 0 if success.
* return -1 if failure.
*/
@ -92,11 +95,11 @@ int tox_delfriend(void *tox, int friendnumber)
return m_delfriend(m, 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.
/* 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 tox_friendstatus(void *tox, int friendnumber)
{
@ -105,7 +108,7 @@ int tox_friendstatus(void *tox, int friendnumber)
}
/* Send a text chat message to an online friend.
* returns the message id if packet was successfully put into the send queue.
* return the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*
* You will want to retain the return value, it will be passed to your read receipt callback
@ -126,7 +129,7 @@ uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uin
}
/* Send an action to an online friend.
* returns 1 if packet was successfully put into the send queue.
* return 1 if packet was successfully put into the send queue.
* return 0 if it was not.
*/
int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length)
@ -139,8 +142,9 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length
* name must be a string of maximum MAX_NAME_LENGTH length.
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
* return 0 if success.
* return -1 if failure.
*
* return 0 if success.
* return -1 if failure.
*/
int tox_setname(void *tox, uint8_t *name, uint16_t length)
{
@ -152,8 +156,9 @@ int tox_setname(void *tox, uint8_t *name, uint16_t length)
* m - The messanger context to use.
* name - Pointer to a string for the name.
* nlen - The length of the string buffer.
* return length of the name.
* return 0 on error.
*
* return length of the name.
* return 0 on error.
*/
uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
{
@ -162,7 +167,8 @@ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
}
/* Get name of friendnumber and put it in name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
* 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.
*/
@ -172,9 +178,11 @@ int tox_getname(void *tox, int friendnumber, uint8_t *name)
return getname(m, friendnumber, name);
}
/* set our user status
you are responsible for freeing status after
returns 0 on success, -1 on failure */
/* Set our user status;
* you are responsible for freeing status after.
*
* return 0 on success, -1 on failure.
*/
int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length)
{
Messenger *m = tox;
@ -187,8 +195,8 @@ int tox_set_userstatus(void *tox, USERSTATUS status)
return m_set_userstatus(m, status);
}
/* return the length of friendnumber's status message, including null.
* Pass it into malloc.
/* return the length of friendnumber's status message, including null.
* Pass it into malloc.
*/
int tox_get_statusmessage_size(void *tox, int friendnumber)
{
@ -197,8 +205,8 @@ int tox_get_statusmessage_size(void *tox, int friendnumber)
}
/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
* Get the size you need to allocate from m_get_statusmessage_size.
* The self variant will copy our own status message.
* Get the size you need to allocate from m_get_statusmessage_size.
* The self variant will copy our own status message.
*/
int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen)
{
@ -317,6 +325,7 @@ void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int,
/* Set the callback for connection status changes.
* function(int friendnumber, uint8_t status)
*
* Status:
* 0 -- friend went offline after being previously online
* 1 -- friend went online
@ -332,7 +341,7 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i
}
/* Use this function to bootstrap the client.
* Sends a get nodes request to the given node with ip port and public_key.
* Sends a get nodes request to the given node with ip port and public_key.
*/
void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
{
@ -340,8 +349,8 @@ void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
DHT_bootstrap(m->dht, ip_port, public_key);
}
/* returns 0 if we are not connected to the DHT.
* returns 1 if we are.
/* return 0 if we are not connected to the DHT.
* return 1 if we are.
*/
int tox_isconnected(void *tox)
{
@ -350,8 +359,9 @@ int tox_isconnected(void *tox)
}
/* Run this at startup.
* returns allocated instance of tox on success.
* returns 0 if there are problems.
*
* return allocated instance of tox on success.
* return 0 if there are problems.
*/
void *tox_new(void)
{
@ -376,7 +386,7 @@ void tox_do(void *tox)
/* SAVING AND LOADING FUNCTIONS: */
/* returns the size of the messenger data (for saving). */
/* return size of the messenger data (for saving). */
uint32_t tox_size(void *tox)
{
Messenger *m = tox;

View File

@ -86,7 +86,7 @@ TOX_USERSTATUS;
typedef void Tox;
/* returns FRIEND_ADDRESS_SIZE byte address to give to others.
/* return FRIEND_ADDRESS_SIZE byte address to give to others.
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*/
void tox_getaddress(Tox *tox, uint8_t *address);
@ -95,32 +95,33 @@ void tox_getaddress(Tox *tox, uint8_t *address);
* Set the data that will be sent along with friend request.
* address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
* data is the data and length is the length.
* returns the friend number if success.
* return TOX_FA_TOOLONG if message length is too long.
* return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return TOX_FAERR_OWNKEY if user's own key.
* return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend.
* return TOX_FAERR_UNKNOWN for unknown error.
* return TOX_FAERR_BADCHECKSUM if bad checksum in address.
* return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return TOX_FAERR_NOMEM if increasing the friend list size fails.
*
* return the friend number if success.
* return TOX_FA_TOOLONG if message length is too long.
* return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return TOX_FAERR_OWNKEY if user's own key.
* return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend.
* return TOX_FAERR_UNKNOWN for unknown error.
* return TOX_FAERR_BADCHECKSUM if bad checksum in address.
* return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return TOX_FAERR_NOMEM if increasing the friend list size fails.
*/
int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length);
/* Add a friend without sending a friendrequest.
* returns the friend number if success.
* return the friend number if success.
* return -1 if failure.
*/
int tox_addfriend_norequest(Tox *tox, 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 tox_getfriend_id(Tox *tox, 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.
* Make sure that client_id is of size CLIENT_ID_SIZE.
* return 0 if success.
* return -1 if failure.
*/
@ -138,8 +139,9 @@ int tox_delfriend(Tox *tox, int friendnumber);
int tox_friendstatus(Tox *tox, int friendnumber);
/* Send a text chat message to an online friend.
* returns the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*
* return the message id if packet was successfully put into the send queue.
* return 0 if it was not.
*
* You will want to retain the return value, it will be passed to your read receipt callback
* if one is received.
@ -150,7 +152,8 @@ uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t
uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
/* Send an action to an online friend.
* returns 1 if packet was successfully put into the send queue.
*
* return 1 if packet was successfully put into the send queue.
* return 0 if it was not.
*/
int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length);
@ -160,8 +163,8 @@ int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length)
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
*
* return 0 if success.
* return -1 if failure.
* return 0 if success.
* return -1 if failure.
*/
int tox_setname(Tox *tox, uint8_t *name, uint16_t length);
@ -171,12 +174,13 @@ int tox_setname(Tox *tox, uint8_t *name, uint16_t length);
* name - Pointer to a string for the name.
* nlen - The length of the string buffer.
*
* returns Return the length of the name, 0 on error.
* return length of name.
* return 0 on error.
*/
uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen);
/* Get name of friendnumber and put it in name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
* 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.
@ -184,29 +188,30 @@ uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen);
int tox_getname(Tox *tox, int friendnumber, uint8_t *name);
/* Set our user status.
* You are responsible for freeing status after.
* You are responsible for freeing status after.
*
* returns 0 on success.
* returns -1 on failure.
*/
int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length);
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status);
/* return the length of friendnumber's status message, including null.
* Pass it into malloc
/* return the length of friendnumber's status message, including null.
* Pass it into malloc
*/
int tox_get_statusmessage_size(Tox *tox, int friendnumber);
/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
* Get the size you need to allocate from m_get_statusmessage_size.
* The self variant will copy our own status message.
* Get the size you need to allocate from m_get_statusmessage_size.
* The self variant will copy our own status message.
*/
int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen);
int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen);
/* return one of USERSTATUS values.
* Values unknown to your application should be represented as USERSTATUS_NONE.
* As above, the self variant will return our own USERSTATUS.
* If friendnumber is invalid, this shall return USERSTATUS_INVALID.
/* return one of USERSTATUS values.
* Values unknown to your application should be represented as USERSTATUS_NONE.
* As above, the self variant will return our own USERSTATUS.
* If friendnumber is invalid, this shall return USERSTATUS_INVALID.
*/
TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber);
TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox);
@ -264,6 +269,7 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
/* Set the callback for connection status changes.
* function(int friendnumber, uint8_t status)
*
* Status:
* 0 -- friend went offline after being previously online
* 1 -- friend went online
@ -275,18 +281,19 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata);
/* Use this function to bootstrap the client.
* Sends a get nodes request to the given node with ip port and public_key.
* Sends a get nodes request to the given node with ip port and public_key.
*/
void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
/* returns 0 if we are not connected to the DHT.
* returns 1 if we are.
/* return 0 if we are not connected to the DHT.
* return 1 if we are.
*/
int tox_isconnected(Tox *tox);
/* Run this at startup.
* returns allocated instance of tox on success.
* returns 0 if there are problems.
*
* return allocated instance of tox on success.
* return 0 if there are problems.
*/
Tox *tox_new(void);
@ -299,7 +306,7 @@ void tox_do(Tox *tox);
/* SAVING AND LOADING FUNCTIONS: */
/* returns the size of the messenger data (for saving). */
/* return size of messenger data (for saving). */
uint32_t tox_size(Tox *tox);
/* Save the messenger in data (must be allocated memory of size Messenger_size()). */