Formatting.

Many stylistic changes, mostly formatting code more closely to the
coding style.
This commit is contained in:
SilentSand 2013-07-26 04:02:17 -04:00
parent b9169ff1b2
commit 59b34e423b
20 changed files with 335 additions and 701 deletions

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,6 @@
* *
*/ */
#ifndef DHT_H #ifndef DHT_H
#define DHT_H #define DHT_H
@ -37,8 +36,6 @@ extern "C" {
/* size of the client_id in bytes */ /* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
/* Add a new friend to the friends list /* Add a new friend to the friends list
client_id must be CLIENT_ID_SIZE bytes long. client_id must be CLIENT_ID_SIZE bytes long.
returns 0 if success returns 0 if success
@ -51,7 +48,6 @@ int DHT_addfriend(uint8_t * client_id);
returns 1 if failure (client_id not in friends list) */ returns 1 if failure (client_id not in friends list) */
int DHT_delfriend(uint8_t * client_id); int DHT_delfriend(uint8_t * client_id);
/* Get ip of friend /* Get ip of friend
client_id must be CLIENT_ID_SIZE bytes long. client_id must be CLIENT_ID_SIZE bytes long.
ip must be 4 bytes long. ip must be 4 bytes long.
@ -61,7 +57,6 @@ int DHT_delfriend(uint8_t * client_id);
returns ip of 1 if friend is not in list. */ returns ip of 1 if friend is not in list. */
IP_Port DHT_getfriendip(uint8_t * client_id); IP_Port DHT_getfriendip(uint8_t * client_id);
/* Run this function at least a couple times per second (It's the main loop) */ /* Run this function at least a couple times per second (It's the main loop) */
void doDHT(); void doDHT();
@ -74,8 +69,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
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 DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key);
/* ROUTING FUNCTIONS */ /* ROUTING FUNCTIONS */
/* send the given packet to node with client_id /* send the given packet to node with client_id
@ -86,8 +79,6 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length);
returns the number of nodes it sent the packet to */ returns the number of nodes it sent the packet to */
int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
/* NAT PUNCHING FUNCTIONS */ /* NAT PUNCHING FUNCTIONS */
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
@ -96,8 +87,6 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
returns -1 if no such friend*/ returns -1 if no such friend*/
int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id);
/* SAVE/LOAD functions */ /* SAVE/LOAD functions */
/* get the size of the DHT (for saving) */ /* get the size of the DHT (for saving) */

View File

@ -25,8 +25,6 @@
There are a couple of useless variables to get rid of. */ There are a couple of useless variables to get rid of. */
#include "Lossless_UDP.h" #include "Lossless_UDP.h"
/* maximum data packets in sent and receive queues. */ /* maximum data packets in sent and receive queues. */
#define MAX_QUEUE_NUM 16 #define MAX_QUEUE_NUM 16
@ -88,7 +86,6 @@ typedef struct
uint8_t timeout; /* connection timeout in seconds. */ uint8_t timeout; /* connection timeout in seconds. */
}Connection; }Connection;
#define MAX_CONNECTIONS 256 #define MAX_CONNECTIONS 256
static Connection connections[MAX_CONNECTIONS]; static Connection connections[MAX_CONNECTIONS];
@ -117,7 +114,6 @@ int getconnection_id(IP_Port ip_port)
/* table of random numbers used below. */ /* table of random numbers used below. */
static uint32_t randtable[6][256]; static uint32_t randtable[6][256];
/* generate a handshake_id which depends on the ip_port. /* generate a handshake_id which depends on the ip_port.
this function will always give one unique handshake_id per ip_port. this function will always give one unique handshake_id per ip_port.
TODO: make this better */ TODO: make this better */
@ -138,6 +134,7 @@ uint32_t handshake_id(IP_Port source)
} }
return id; return id;
} }
/* change the hnshake id associated with that ip_port /* change the hnshake id associated with that ip_port
TODO: make this better */ TODO: make this better */
void change_handshake(IP_Port source) void change_handshake(IP_Port source)
@ -146,7 +143,6 @@ void change_handshake(IP_Port source)
randtable[rand][((uint8_t *)&source)[rand]] = random_int(); randtable[rand][((uint8_t *)&source)[rand]] = random_int();
} }
/* initialize a new connection to ip_port /* initialize a new connection to ip_port
returns an integer corresponding to the connection id. returns an integer corresponding to the connection id.
return -1 if it could not initialize the connection. return -1 if it could not initialize the connection.
@ -314,6 +310,7 @@ char id_packet(int connection_id)
} }
return -1; return -1;
} }
/* return 0 if there is no received data in the buffer. /* return 0 if there is no received data in the buffer.
return length of received packet if successful */ return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data) int read_packet(int connection_id, uint8_t * data)
@ -353,9 +350,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length)
return 0; return 0;
} }
/* put the packet numbers the we are missing in requested and return the number */ /* put the packet numbers the we are missing in requested and return the number */
uint32_t missing_packets(int connection_id, uint32_t * requested) uint32_t missing_packets(int connection_id, uint32_t * requested)
{ {
@ -386,8 +380,6 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
/* Packet sending functions /* Packet sending functions
One per packet type. One per packet type.
see docs/Lossless_UDP.txt for more information. */ see docs/Lossless_UDP.txt for more information. */
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
{ {
uint8_t packet[1 + 4 + 4]; uint8_t packet[1 + 4 + 4];
@ -402,7 +394,6 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i
} }
int send_SYNC(uint32_t connection_id) int send_SYNC(uint32_t connection_id)
{ {
@ -468,8 +459,6 @@ int send_DATA(uint32_t connection_id)
/* END of packet sending functions */ /* END of packet sending functions */
/* Packet handling functions /* Packet handling functions
One to handle each type of packets we receive One to handle each type of packets we receive
return 0 if handled correctly, 1 if packet is bad. */ return 0 if handled correctly, 1 if packet is bad. */
@ -700,7 +689,6 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
/* END of packet handling functions */ /* END of packet handling functions */
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{ {
@ -828,6 +816,7 @@ void adjustRates()
} }
} }
} }
/* Call this function a couple times per second /* Call this function a couple times per second
It's the main loop. */ It's the main loop. */
void doLossless_UDP() void doLossless_UDP()

View File

@ -33,8 +33,6 @@ extern "C" {
/* maximum length of the data in the data packets */ /* maximum length of the data in the data packets */
#define MAX_DATA_SIZE 1024 #define MAX_DATA_SIZE 1024
/* Functions */ /* Functions */
/* initialize a new connection to ip_port /* initialize a new connection to ip_port
@ -52,7 +50,6 @@ int getconnection_id(IP_Port ip_port);
return -1 if there are no new incoming connections in the list. */ return -1 if there are no new incoming connections in the list. */
int incoming_connection(); int incoming_connection();
/* return -1 if it could not kill the connection. /* return -1 if it could not kill the connection.
return 0 if killed successfully */ return 0 if killed successfully */
int kill_connection(int connection_id); int kill_connection(int connection_id);
@ -74,21 +71,16 @@ char id_packet(int connection_id);
return length of received packet if successful */ return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data); int read_packet(int connection_id, uint8_t * data);
/* return 0 if data could not be put in packet queue /* return 0 if data could not be put in packet queue
return 1 if data was put into the queue */ return 1 if data was put into the queue */
int write_packet(int connection_id, uint8_t * data, uint32_t length); int write_packet(int connection_id, uint8_t * data, uint32_t length);
/* returns the number of packets in the queue waiting to be successfully sent. */ /* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id); uint32_t sendqueue(int connection_id);
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id); uint32_t recvqueue(int connection_id);
/* check if connection is connected /* check if connection is connected
return 0 no. return 0 no.
return 1 if attempting handshake return 1 if attempting handshake
@ -97,7 +89,6 @@ uint32_t recvqueue(int connection_id);
return 4 if timed out and wating to be killed */ return 4 if timed out and wating to be killed */
int is_connected(int connection_id); int is_connected(int connection_id);
/* Call this function a couple times per second /* Call this function a couple times per second
It's the main loop. */ It's the main loop. */
void doLossless_UDP(); void doLossless_UDP();

View File

@ -39,8 +39,6 @@ typedef struct
uint16_t info_size; /* length of the info */ uint16_t info_size; /* length of the info */
}Friend; }Friend;
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
static uint8_t self_name[MAX_NAME_LENGTH]; static uint8_t self_name[MAX_NAME_LENGTH];
@ -57,7 +55,6 @@ static uint32_t numfriends;
0 if we are offline 0 if we are offline
static uint8_t online; */ static uint8_t online; */
/* return the friend id associated to that public key. /* return the friend id associated to that public key.
return -1 if no such friend */ return -1 if no such friend */
int getfriend_id(uint8_t * client_id) int getfriend_id(uint8_t * client_id)
@ -76,7 +73,6 @@ int getfriend_id(uint8_t * client_id)
return -1; return -1;
} }
/* copies the public key associated to that friend id into client_id buffer. /* copies the public key associated to that friend id into client_id buffer.
make sure that client_id is of size CLIENT_ID_SIZE. make sure that client_id is of size CLIENT_ID_SIZE.
return 0 if success return 0 if success
@ -96,7 +92,6 @@ int getclient_id(int friend_id, uint8_t * client_id)
return -1; return -1;
} }
/* add a friend /* add a friend
set the data that will be sent along with friend request set the data that will be sent along with friend request
client_id is the client id of the friend client_id is the client id of the friend
@ -191,7 +186,6 @@ int m_delfriend(int friendnumber)
return 0; return 0;
} }
/* return 4 if friend is online /* return 4 if friend is online
return 3 if friend is confirmed return 3 if friend is confirmed
return 2 if the friend request was sent return 2 if the friend request was sent
@ -206,7 +200,6 @@ int m_friendstatus(int friendnumber)
return friendlist[friendnumber].status; return friendlist[friendnumber].status;
} }
/* send a text chat message to an online friend /* send a text chat message to an online friend
return 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 */ return 0 if it was not */
@ -239,7 +232,6 @@ static int m_sendname(int friendnumber, uint8_t * name)
/* set the name of a friend /* set the name of a friend
return 0 if success return 0 if success
return -1 if failure */ return -1 if failure */
static int setfriendname(int friendnumber, uint8_t * name) static int setfriendname(int friendnumber, uint8_t * name)
{ {
if(friendnumber >= numfriends || friendnumber < 0) if(friendnumber >= numfriends || friendnumber < 0)
@ -250,7 +242,6 @@ static int setfriendname(int friendnumber, uint8_t * name)
return 0; return 0;
} }
/* Set our nickname /* Set our nickname
name must be a string of maximum MAX_NAME_LENGTH length. name must be a string of maximum MAX_NAME_LENGTH length.
return 0 if success return 0 if success
@ -352,6 +343,7 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
friendlist[friendnumber].userstatus_length = length; friendlist[friendnumber].userstatus_length = length;
return 0; return 0;
} }
/* /*
static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; static uint8_t friend_request_isset = 0;
@ -362,7 +354,6 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
callback_friendrequest(function); callback_friendrequest(function);
} }
static void (*friend_message)(int, uint8_t *, uint16_t); static void (*friend_message)(int, uint8_t *, uint16_t);
static uint8_t friend_message_isset = 0; static uint8_t friend_message_isset = 0;
@ -373,7 +364,6 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
friend_message_isset = 1; friend_message_isset = 1;
} }
static void (*friend_namechange)(int, uint8_t *, uint16_t); static void (*friend_namechange)(int, uint8_t *, uint16_t);
static uint8_t friend_namechange_isset = 0; static uint8_t friend_namechange_isset = 0;
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
@ -520,8 +510,6 @@ static void doFriends()
} }
} }
static void doInbound() static void doInbound()
{ {
uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t secret_nonce[crypto_box_NONCEBYTES];

View File

@ -23,7 +23,6 @@
* *
*/ */
#ifndef MESSENGER_H #ifndef MESSENGER_H
#define MESSENGER_H #define MESSENGER_H
@ -79,7 +78,6 @@ int m_delfriend(int friendnumber);
return 0 if there is no friend with that number */ return 0 if there is no friend with that number */
int m_friendstatus(int friendnumber); int m_friendstatus(int friendnumber);
/* send a text chat message to an online friend /* send a text chat message to an online friend
returns 1 if packet was successfully put into the send queue returns 1 if packet was successfully put into the send queue
return 0 if it was not */ return 0 if it was not */
@ -91,7 +89,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length);
return -1 if failure */ return -1 if failure */
int setname(uint8_t * name, uint16_t length); int setname(uint8_t * name, uint16_t length);
/* get name of friendnumber /* get name of friendnumber
put it in name 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.
@ -117,7 +114,6 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen);
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
/* set the function that will be executed when a message from a friend is received. /* set the function that will be executed when a message from a friend is received.
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
@ -137,11 +133,9 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
returns -1 if there are problems */ returns -1 if there are problems */
int initMessenger(); int initMessenger();
/* the main loop that needs to be run at least 200 times per second */ /* the main loop that needs to be run at least 200 times per second */
void doMessenger(); void doMessenger();
/* SAVING AND LOADING FUNCTIONS: */ /* SAVING AND LOADING FUNCTIONS: */
/* returns the size of the messenger data (for saving) */ /* returns the size of the messenger data (for saving) */

View File

@ -25,7 +25,6 @@
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
/* Try to send a friendrequest to peer with public_key /* Try to send a friendrequest to peer with public_key
data is the data in the request and length is the length. data is the data in the request and length is the length.
return -1 if failure. return -1 if failure.
@ -61,7 +60,6 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
return num; return num;
} }
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
static uint8_t handle_friendrequest_isset = 0; static uint8_t handle_friendrequest_isset = 0;
@ -72,7 +70,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
handle_friendrequest_isset = 1; handle_friendrequest_isset = 1;
} }
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{ {

View File

@ -21,11 +21,9 @@
* *
*/ */
#ifndef FRIEND_REQUESTS_H #ifndef FRIEND_REQUESTS_H
#define FRIEND_REQUESTS_H #define FRIEND_REQUESTS_H
#include "DHT.h" #include "DHT.h"
#include "net_crypto.h" #include "net_crypto.h"
@ -37,7 +35,6 @@ extern "C" {
data is the data in the request and length is the length. */ data is the data in the request and length is the length. */
int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length);
/* set the function that will be executed when a friend request for us is received. /* set the function that will be executed when a friend request for us is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
@ -47,8 +44,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
return 1 if it didn't handle the packet or if the packet was shit. */ return 1 if it didn't handle the packet or if the packet was shit. */
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -26,12 +26,10 @@
#include "net_crypto.h" #include "net_crypto.h"
/* Our public and secret keys. */ /* Our public and secret keys. */
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
typedef struct typedef struct
{ {
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
@ -177,7 +175,6 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data)
return -1; return -1;
} }
/* return 0 if data could not be put in packet queue /* return 0 if data could not be put in packet queue
return 1 if data was put into the queue */ return 1 if data was put into the queue */
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
@ -267,7 +264,6 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1
} }
} }
/* Send a crypto handshake packet containing an encrypted secret nonce and session public key /* Send a crypto handshake packet containing an encrypted secret nonce and session public key
to peer with connection_id and public_key to peer with connection_id and public_key
the packet is encrypted with a random nonce which is sent in plain text with the packet */ the packet is encrypted with a random nonce which is sent in plain text with the packet */
@ -327,9 +323,6 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
return 1; return 1;
} }
/* get crypto connection id from public key of peer /* get crypto connection id from public key of peer
return -1 if there are no connections like we are looking for return -1 if there are no connections like we are looking for
return id if it found it */ return id if it found it */
@ -349,7 +342,6 @@ int getcryptconnection_id(uint8_t * public_key)
return -1; return -1;
} }
/* Start a secure connection with other peer who has public_key and ip_port /* Start a secure connection with other peer who has public_key and ip_port
returns -1 if failure returns -1 if failure
returns crypt_connection_id of the initialized connection if everything went well. */ returns crypt_connection_id of the initialized connection if everything went well. */
@ -447,7 +439,6 @@ int crypto_kill(int crypt_connection_id)
return 1; return 1;
} }
/* accept an incoming connection using the parameters provided by crypto_inbound /* accept an incoming connection using the parameters provided by crypto_inbound
return -1 if not successful return -1 if not successful
returns the crypt_connection_id if successful */ returns the crypt_connection_id if successful */
@ -505,7 +496,6 @@ int is_cryptoconnected(int crypt_connection_id)
return 0; return 0;
} }
/* Generate our public and private keys /* Generate our public and private keys
Only call this function the first time the program starts. */ Only call this function the first time the program starts. */
void new_keys() void new_keys()

View File

@ -55,13 +55,11 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
/* fill the given nonce with random bytes. */ /* fill the given nonce with random bytes. */
void random_nonce(uint8_t * nonce); void random_nonce(uint8_t * nonce);
/* return 0 if there is no received data in the buffer /* return 0 if there is no received data in the buffer
return -1 if the packet was discarded. return -1 if the packet was discarded.
return length of received data if successful */ return length of received data if successful */
int read_cryptpacket(int crypt_connection_id, uint8_t * data); int read_cryptpacket(int crypt_connection_id, uint8_t * data);
/* return 0 if data could not be put in packet queue /* return 0 if data could not be put in packet queue
return 1 if data was put into the queue */ return 1 if data was put into the queue */
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
@ -74,20 +72,17 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
returns the length of the created packet on success */ returns the length of the created packet on success */
int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id);
/* puts the senders public key in the request in public_key, the data from the request /* puts the senders public key in the request in public_key, the data from the request
in data if a friend or ping request was sent to us and returns the length of the data. 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 packet is the request packet and length is its length
return -1 if not valid request. */ return -1 if not valid request. */
int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length);
/* Start a secure connection with other peer who has public_key and ip_port /* Start a secure connection with other peer who has public_key and ip_port
returns -1 if failure returns -1 if failure
returns crypt_connection_id of the initialized connection if everything went well. */ returns crypt_connection_id of the initialized connection if everything went well. */
int crypto_connect(uint8_t * public_key, IP_Port ip_port); int crypto_connect(uint8_t * public_key, IP_Port ip_port);
/* kill a crypto connection /* kill a crypto connection
return 0 if killed successfully return 0 if killed successfully
return 1 if there was a problem. */ return 1 if there was a problem. */
@ -102,7 +97,6 @@ int crypto_kill(int crypt_connection_id);
to refuse it just call kill_connection(...) on the connection id */ to refuse it just call kill_connection(...) on the connection id */
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
/* accept an incoming connection using the parameters provided by crypto_inbound /* accept an incoming connection using the parameters provided by crypto_inbound
return -1 if not successful return -1 if not successful
returns the crypt_connection_id if successful */ returns the crypt_connection_id if successful */

View File

@ -23,7 +23,6 @@
#include "network.h" #include "network.h"
/* returns current UNIX time in microseconds (us). */ /* returns current UNIX time in microseconds (us). */
uint64_t current_time() uint64_t current_time()
{ {
@ -128,7 +127,6 @@ int init_networking(IP ip, uint16_t port)
return -1; return -1;
#endif #endif
/* Functions to increase the size of the send and receive UDP buffers /* Functions to increase the size of the send and receive UDP buffers
NOTE: uncomment if necessary */ NOTE: uncomment if necessary */
/* /*
@ -147,7 +145,6 @@ int init_networking(IP ip, uint16_t port)
int broadcast = 1; int broadcast = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast));
/* Set socket nonblocking */ /* Set socket nonblocking */
#ifdef WIN32 #ifdef WIN32
/* I think this works for windows */ /* I think this works for windows */

View File

@ -21,7 +21,6 @@
* *
*/ */
#ifndef NETWORK_H #ifndef NETWORK_H
#define NETWORK_H #define NETWORK_H
@ -31,8 +30,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#ifdef WIN32 /* Put win32 includes here */ #ifdef WIN32 /* Put win32 includes here */
//Windows XP //Windows XP
#define WINVER 0x0501 #define WINVER 0x0501
@ -97,7 +94,6 @@ typedef struct
#endif #endif
}ADDR; }ADDR;
/* returns current time in milleseconds since the epoch. */ /* returns current time in milleseconds since the epoch. */
uint64_t current_time(); uint64_t current_time();

View File

@ -41,7 +41,7 @@
#include <string.h> #include <string.h>
//Sleep function (x = milliseconds) /* Sleep function (x = milliseconds) */
#ifdef WIN32 #ifdef WIN32
#define c_sleep(x) Sleep(1*x) #define c_sleep(x) Sleep(1*x)
@ -60,16 +60,14 @@ void printip(IP_Port ip_port)
printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
} }
/* horrible function from one of my first C programs.
//horrible function from one of my first C programs. *only here because I was too lazy to write a proper one. */
//only here because I was too lazy to write a proper one.
unsigned char * hex_string_to_bin(char hex_string[]) unsigned char * hex_string_to_bin(char hex_string[])
{ {
unsigned char * val = malloc(strlen(hex_string)); unsigned char * val = malloc(strlen(hex_string));
char * pos = hex_string; char * pos = hex_string;
int i=0; int i=0;
while(i < strlen(hex_string)) while(i < strlen(hex_string)) {
{
sscanf(pos,"%2hhx",&val[i]); sscanf(pos,"%2hhx",&val[i]);
pos+=2; pos+=2;
i++; i++;
@ -88,8 +86,7 @@ int main(int argc, char *argv[])
new_keys(); new_keys();
printf("OUR ID: "); printf("OUR ID: ");
uint32_t i; uint32_t i;
for(i = 0; i < 32; i++) for(i = 0; i < 32; i++) {
{
if(self_public_key[i] < 16) if(self_public_key[i] < 16)
printf("0"); printf("0");
printf("%hhX",self_public_key[i]); printf("%hhX",self_public_key[i]);
@ -105,8 +102,7 @@ int main(int argc, char *argv[])
uint8_t friend_id[32]; uint8_t friend_id[32];
memcpy(friend_id, hex_string_to_bin(temp_id), 32); memcpy(friend_id, hex_string_to_bin(temp_id), 32);
/* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
DHT_addfriend(friend_id); DHT_addfriend(friend_id);
@ -118,15 +114,13 @@ int main(int argc, char *argv[])
int friendrequest = -1; int friendrequest = -1;
uint8_t request_data[512]; uint8_t request_data[512];
//initialize networking /* initialize networking
//bind to ip 0.0.0.0:PORT * bind to ip 0.0.0.0:PORT */
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
initNetCrypto(); initNetCrypto();
perror("Initialization"); perror("Initialization");
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
@ -147,122 +141,99 @@ int main(int argc, char *argv[])
if ( file2==NULL ){return 1;} if ( file2==NULL ){return 1;}
read1 = fread(buffer1, 1, 128, file1); read1 = fread(buffer1, 1, 128, file1);
while(1) while(1) {
{ while(receivepacket(&ip_port, data, &length) != -1) {
if(rand() % 3 != 1) { /* simulate packet loss */
while(receivepacket(&ip_port, data, &length) != -1) if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
{ /* if packet is not recognized */
if(rand() % 3 != 1)//simulate packet loss
{
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
{
//if packet is not recognized
printf("Received unhandled packet with length: %u\n", length); printf("Received unhandled packet with length: %u\n", length);
} } else {
else
{
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
} }
} }
friend_ip = DHT_getfriendip(friend_id); friend_ip = DHT_getfriendip(friend_id);
if(friend_ip.ip.i != 0) if(friend_ip.ip.i != 0) {
{ if(connection == -1 && friendrequest == -1) {
if(connection == -1 && friendrequest == -1)
{
printf("Sending friend request to peer:"); printf("Sending friend request to peer:");
printip(friend_ip); printip(friend_ip);
friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
//connection = crypto_connect((uint8_t *)friend_id, friend_ip); /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
//connection = new_connection(friend_ip); /* connection = new_connection(friend_ip); */
} }
if(check_friendrequest(friendrequest) == 1) if(check_friendrequest(friendrequest) == 1) {
{
printf("Started connecting to friend:"); printf("Started connecting to friend:");
connection = crypto_connect(friend_id, friend_ip); connection = crypto_connect(friend_id, friend_ip);
} }
} }
if(inconnection == -1) if(inconnection == -1) {
{
uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t secret_nonce[crypto_box_NONCEBYTES];
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t session_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES];
inconnection = crypto_inbound(public_key, secret_nonce, session_key); inconnection = crypto_inbound(public_key, secret_nonce, session_key);
inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
//inconnection = incoming_connection(); /* inconnection = incoming_connection(); */
if(inconnection != -1) if(inconnection != -1) {
{
printf("Someone connected to us:\n"); printf("Someone connected to us:\n");
// printip(connection_ip(inconnection)); /* printip(connection_ip(inconnection)); */
} }
} }
if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
{
printf("RECIEVED FRIEND REQUEST: %s\n", request_data); printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
} }
//if someone connected to us write what he sends to a file /* if someone connected to us write what he sends to a file
//also send him our file. * also send him our file. */
if(inconnection != -1) if(inconnection != -1) {
{ if(write_cryptpacket(inconnection, buffer1, read1)) {
if(write_cryptpacket(inconnection, buffer1, read1))
{
printf("Wrote data1.\n"); printf("Wrote data1.\n");
read1 = fread(buffer1, 1, 128, file1); read1 = fread(buffer1, 1, 128, file1);
} }
read2 = read_cryptpacket(inconnection, buffer2); read2 = read_cryptpacket(inconnection, buffer2);
if(read2 != 0) if(read2 != 0) {
{
printf("Received data1.\n"); printf("Received data1.\n");
if(!fwrite(buffer2, read2, 1, file2)) if(!fwrite(buffer2, read2, 1, file2)) {
{
printf("file write error1\n"); printf("file write error1\n");
} }
if(read2 < 128) if(read2 < 128) {
{
printf("Closed file1 %u\n", read2); printf("Closed file1 %u\n", read2);
fclose(file2); fclose(file2);
} }
} }
else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. /* if buffer is empty and the connection timed out. */
{ else if(is_cryptoconnected(inconnection) == 4) {
crypto_kill(inconnection); crypto_kill(inconnection);
} }
} }
//if we are connected to a friend send him data from the file. /* if we are connected to a friend send him data from the file.
//also put what he sends us in a file. * also put what he sends us in a file. */
if(is_cryptoconnected(connection) >= 3) if(is_cryptoconnected(connection) >= 3) {
{ if(write_cryptpacket(0, buffer1, read1)) {
if(write_cryptpacket(0, buffer1, read1))
{
printf("Wrote data2.\n"); printf("Wrote data2.\n");
read1 = fread(buffer1, 1, 128, file1); read1 = fread(buffer1, 1, 128, file1);
} }
read2 = read_cryptpacket(0, buffer2); read2 = read_cryptpacket(0, buffer2);
if(read2 != 0) if(read2 != 0) {
{
printf("Received data2.\n"); printf("Received data2.\n");
if(!fwrite(buffer2, read2, 1, file2)) if(!fwrite(buffer2, read2, 1, file2)) {
{
printf("file write error2\n"); printf("file write error2\n");
} }
if(read2 < 128) if(read2 < 128) {
{
printf("Closed file2 %u\n", read2); printf("Closed file2 %u\n", read2);
fclose(file2); fclose(file2);
} }
} }
else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. /* if buffer is empty and the connection timed out. */
{ else if(is_cryptoconnected(connection) == 4) {
crypto_kill(connection); crypto_kill(connection);
} }
} }
doDHT(); doDHT();
doLossless_UDP(); doLossless_UDP();
doNetCrypto(); doNetCrypto();
//print_clientlist(); /*print_clientlist();
//print_friendlist(); *print_friendlist();
//c_sleep(300); *c_sleep(300); */
c_sleep(1); c_sleep(1);
} }

View File

@ -33,6 +33,7 @@
* along with Tox. If not, see <http://www.gnu.org/licenses/>. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
#include "../core/network.h" #include "../core/network.h"
#include "../core/DHT.h" #include "../core/DHT.h"
#include "../core/Lossless_UDP.h" #include "../core/Lossless_UDP.h"
@ -100,68 +101,53 @@ int main(int argc, char *argv[])
if ( file2==NULL ){return 1;} if ( file2==NULL ){return 1;}
read1 = fread(buffer1, 1, 128, file1); read1 = fread(buffer1, 1, 128, file1);
while(1) while(1) {
{
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1) {
{ if(rand() % 3 != 1) { /* simulate packet loss */
if(rand() % 3 != 1)//simulate packet loss if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
{ /* if packet is not recognized */
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
{
//if packet is not recognized
printf("Received unhandled packet with length: %u\n", length); printf("Received unhandled packet with length: %u\n", length);
} } else {
else
{
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
} }
} }
friend_ip = DHT_getfriendip((uint8_t *)argv[3]); friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
if(friend_ip.ip.i != 0) if(friend_ip.ip.i != 0) {
{ if(connection == -1) {
if(connection == -1)
{
printf("Started connecting to friend:"); printf("Started connecting to friend:");
printip(friend_ip); printip(friend_ip);
connection = new_connection(friend_ip); connection = new_connection(friend_ip);
} }
} }
if(inconnection == -1) if(inconnection == -1) {
{
inconnection = incoming_connection(); inconnection = incoming_connection();
if(inconnection != -1) if(inconnection != -1) {
{
printf("Someone connected to us:"); printf("Someone connected to us:");
printip(connection_ip(inconnection)); printip(connection_ip(inconnection));
} }
} }
//if someone connected to us write what he sends to a file /* if someone connected to us write what he sends to a file */
//also send him our file. /* also send him our file. */
if(inconnection != -1) if(inconnection != -1) {
{ if(write_packet(inconnection, buffer1, read1)) {
if(write_packet(inconnection, buffer1, read1))
{
printf("Wrote data.\n"); printf("Wrote data.\n");
read1 = fread(buffer1, 1, 128, file1); read1 = fread(buffer1, 1, 128, file1);
} }
read2 = read_packet(inconnection, buffer2); read2 = read_packet(inconnection, buffer2);
if(read2 != 0) if(read2 != 0) {
{
printf("Received data.\n"); printf("Received data.\n");
if(!fwrite(buffer2, read2, 1, file2)) if(!fwrite(buffer2, read2, 1, file2)) {
{
printf("file write error\n"); printf("file write error\n");
} }
if(read2 < 128) if(read2 < 128) {
{
fclose(file2); fclose(file2);
} }
} }
} }
//if we are connected to a friend send him data from the file. /* if we are connected to a friend send him data from the file.
//also put what he sends us in a file. * also put what he sends us in a file. */
if(is_connected(connection) == 3) if(is_connected(connection) == 3)
{ {
if(write_packet(0, buffer1, read1)) if(write_packet(0, buffer1, read1))
@ -185,9 +171,9 @@ int main(int argc, char *argv[])
} }
doDHT(); doDHT();
doLossless_UDP(); doLossless_UDP();
//print_clientlist(); /* print_clientlist();
//print_friendlist(); * print_friendlist();
//c_sleep(300); * c_sleep(300); */
c_sleep(1); c_sleep(1);
} }

View File

@ -47,17 +47,14 @@
#define PORT 33445 #define PORT 33445
void print_clientlist() void print_clientlist()
{ {
uint32_t i, j; uint32_t i, j;
IP_Port p_ip; IP_Port p_ip;
printf("___________________CLOSE________________________________\n"); printf("___________________CLOSE________________________________\n");
for(i = 0; i < 4; i++) for(i = 0; i < 4; i++) {
{
printf("ClientID: "); printf("ClientID: ");
for(j = 0; j < 32; j++) for(j = 0; j < 32; j++) {
{
printf("%c", close_clientlist[i].client_id[j]); printf("%c", close_clientlist[i].client_id[j]);
} }
p_ip = close_clientlist[i].ip_port; p_ip = close_clientlist[i].ip_port;
@ -75,12 +72,10 @@ void print_friendlist()
uint32_t i, j, k; uint32_t i, j, k;
IP_Port p_ip; IP_Port p_ip;
printf("_________________FRIENDS__________________________________\n"); printf("_________________FRIENDS__________________________________\n");
for(k = 0; k < num_friends; k++) for(k = 0; k < num_friends; k++) {
{
printf("FRIEND %u\n", k); printf("FRIEND %u\n", k);
printf("ID: "); printf("ID: ");
for(j = 0; j < 32; j++) for(j = 0; j < 32; j++) {
{
printf("%c", friends_list[k].client_id[j]); printf("%c", friends_list[k].client_id[j]);
} }
p_ip = DHT_getfriendip(friends_list[k].client_id); p_ip = DHT_getfriendip(friends_list[k].client_id);
@ -88,11 +83,9 @@ void print_friendlist()
printf("\nCLIENTS IN LIST:\n\n"); printf("\nCLIENTS IN LIST:\n\n");
for(i = 0; i < 4; i++) for(i = 0; i < 4; i++) {
{
printf("ClientID: "); printf("ClientID: ");
for(j = 0; j < 32; j++) for(j = 0; j < 32; j++) {
{
if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16) if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16)
printf("0"); printf("0");
printf("%hhX", friends_list[k].client_list[i].client_id[j]); printf("%hhX", friends_list[k].client_list[i].client_id[j]);
@ -113,8 +106,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
uint32_t i; uint32_t i;
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
printf("--------------------BEGIN-----------------------------\n"); printf("--------------------BEGIN-----------------------------\n");
for(i = 0; i < length; i++) for(i = 0; i < length; i++) {
{
if(data[i] < 16) if(data[i] < 16)
printf("0"); printf("0");
printf("%hhX",data[i]); printf("%hhX",data[i]);
@ -129,8 +121,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
unsigned char * val = malloc(strlen(hex_string)); unsigned char * val = malloc(strlen(hex_string));
char * pos = hex_string; char * pos = hex_string;
int i=0; int i=0;
while(i < strlen(hex_string)) while(i < strlen(hex_string)) {
{
sscanf(pos,"%2hhx",&val[i]); sscanf(pos,"%2hhx",&val[i]);
pos+=2; pos+=2;
i++; i++;
@ -149,8 +140,7 @@ int main(int argc, char *argv[])
new_keys(); new_keys();
printf("OUR ID: "); printf("OUR ID: ");
uint32_t i; uint32_t i;
for(i = 0; i < 32; i++) for(i = 0; i < 32; i++) {
{
if(self_public_key[i] < 16) if(self_public_key[i] < 16)
printf("0"); printf("0");
printf("%hhX",self_public_key[i]); printf("%hhX",self_public_key[i]);
@ -161,8 +151,8 @@ int main(int argc, char *argv[])
scanf("%s", temp_id); scanf("%s", temp_id);
DHT_addfriend(hex_string_to_bin(temp_id)); DHT_addfriend(hex_string_to_bin(temp_id));
//initialize networking /* initialize networking */
//bind to ip 0.0.0.0:PORT /* bind to ip 0.0.0.0:PORT */
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
@ -171,10 +161,10 @@ int main(int argc, char *argv[])
perror("Initialization"); perror("Initialization");
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
//bootstrap_ip_port.ip.c[0] = 127; /* bootstrap_ip_port.ip.c[0] = 127;
//bootstrap_ip_port.ip.c[1] = 0; * bootstrap_ip_port.ip.c[1] = 0;
//bootstrap_ip_port.ip.c[2] = 0; * bootstrap_ip_port.ip.c[2] = 0;
//bootstrap_ip_port.ip.c[3] = 1; * bootstrap_ip_port.ip.c[3] = 1; */
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
@ -182,20 +172,15 @@ int main(int argc, char *argv[])
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
while(1) while(1) {
{
doDHT(); doDHT();
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1) {
{ if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port))
{
//unhandled packet //unhandled packet
printpacket(data, length, ip_port); printpacket(data, length, ip_port);
} } else {
else
{
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
} }

View File

@ -42,7 +42,6 @@
#endif #endif
#define PORT 33446 #define PORT 33446
void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
@ -50,8 +49,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
uint32_t i; uint32_t i;
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
printf("--------------------BEGIN-----------------------------\n"); printf("--------------------BEGIN-----------------------------\n");
for(i = 0; i < length; i++) for(i = 0; i < length; i++) {
{
if(data[i] < 16) if(data[i] < 16)
printf("0"); printf("0");
printf("%hhX",data[i]); printf("%hhX",data[i]);
@ -117,39 +115,34 @@ void printconnection(int connection_id)
} }
*/ */
//recieve packets and send them to the packethandler
//run doLossless_UDP(); /*( recieve packets and send them to the packethandler */
/*run doLossless_UDP(); */
void Lossless_UDP() void Lossless_UDP()
{ {
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1) {
{
printf("packet with length: %u\n", length); printf("packet with length: %u\n", length);
//if(rand() % 3 != 1)//add packet loss /* if(rand() % 3 != 1)//add packet loss
// { { */
if(LosslessUDP_handlepacket(data, length, ip_port)) if(LosslessUDP_handlepacket(data, length, ip_port)) {
{
printpacket(data, length, ip_port); printpacket(data, length, ip_port);
} } else {
else
{
//printconnection(0); //printconnection(0);
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
// } /* } */
} }
doLossless_UDP(); doLossless_UDP();
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 4) if (argc < 4) {
{
printf("usage: %s ip port filename\n", argv[0]); printf("usage: %s ip port filename\n", argv[0]);
exit(0); exit(0);
} }
@ -161,8 +154,8 @@ int main(int argc, char *argv[])
if ( file==NULL ){return 1;} if ( file==NULL ){return 1;}
//initialize networking /* initialize networking */
//bind to ip 0.0.0.0:PORT /* bind to ip 0.0.0.0:PORT */
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
@ -173,17 +166,14 @@ int main(int argc, char *argv[])
printip(serverip); printip(serverip);
int connection = new_connection(serverip); int connection = new_connection(serverip);
uint64_t timer = current_time(); uint64_t timer = current_time();
while(1) while(1) {
{ /* printconnection(connection); */
// printconnection(connection);
Lossless_UDP(); Lossless_UDP();
if(is_connected(connection) == 3) if(is_connected(connection) == 3) {
{
printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
break; break;
} }
if(is_connected(connection) == 0) if(is_connected(connection) == 0) {
{
printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
return 1; return 1;
} }
@ -192,38 +182,32 @@ int main(int argc, char *argv[])
timer = current_time(); timer = current_time();
//read first part of file /*read first part of file */
read = fread(buffer, 1, 512, file); read = fread(buffer, 1, 512, file);
while(1) while(1) {
{ /* printconnection(connection); */
//printconnection(connection);
Lossless_UDP(); Lossless_UDP();
if(is_connected(connection) == 3) if(is_connected(connection) == 3) {
{
if(write_packet(connection, buffer, read)) if(write_packet(connection, buffer, read)) {
{ /* printf("Wrote data.\n"); */
//printf("Wrote data.\n");
read = fread(buffer, 1, 512, file); read = fread(buffer, 1, 512, file);
} }
//printf("%u\n", sendqueue(connection)); /* printf("%u\n", sendqueue(connection)); */
if(sendqueue(connection) == 0) if(sendqueue(connection) == 0) {
{ if(read == 0) {
if(read == 0)
{
printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
break; break;
} }
} }
} }
else else {
{
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
return 0; return 0;
} }
//c_sleep(1); /* c_sleep(1); */
} }
return 0; return 0;

View File

@ -43,7 +43,6 @@
#endif #endif
#define PORT 33445 #define PORT 33445
void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
@ -51,14 +50,14 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
uint32_t i; uint32_t i;
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
printf("--------------------BEGIN-----------------------------\n"); printf("--------------------BEGIN-----------------------------\n");
for(i = 0; i < length; i++) for(i = 0; i < length; i++) {
{
if(data[i] < 16) if(data[i] < 16)
printf("0"); printf("0");
printf("%hhX",data[i]); printf("%hhX",data[i]);
} }
printf("\n--------------------END-----------------------------\n\n\n"); printf("\n--------------------END-----------------------------\n\n\n");
} }
/* /*
void printpackets(Data test) void printpackets(Data test)
{ {
@ -113,23 +112,20 @@ void printconnection(int connection_id)
} }
*/ */
//recieve packets and send them to the packethandler
//run doLossless_UDP(); /* recieve packets and send them to the packethandler
* run doLossless_UDP(); */
void Lossless_UDP() void Lossless_UDP()
{ {
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1) {
{
//if(rand() % 3 != 1)//add packet loss //if(rand() % 3 != 1)//add packet loss
//{ //{
if(LosslessUDP_handlepacket(data, length, ip_port)) if(LosslessUDP_handlepacket(data, length, ip_port)) {
{
printpacket(data, length, ip_port); printpacket(data, length, ip_port);
} } else {
else
{
//printconnection(0); //printconnection(0);
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
@ -137,14 +133,12 @@ void Lossless_UDP()
} }
doLossless_UDP(); doLossless_UDP();
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 2) if (argc < 2) {
{
printf("usage: %s filename\n", argv[0]); printf("usage: %s filename\n", argv[0]);
exit(0); exit(0);
} }
@ -167,14 +161,11 @@ int main(int argc, char *argv[])
uint64_t timer = current_time(); uint64_t timer = current_time();
while(1) while(1) {
{
Lossless_UDP(); Lossless_UDP();
connection = incoming_connection(); connection = incoming_connection();
if(connection != -1) if(connection != -1) {
{ if(is_connected(connection) == 2) {
if(is_connected(connection) == 2)
{
printf("Recieved the connection.\n"); printf("Recieved the connection.\n");
} }
@ -185,25 +176,20 @@ int main(int argc, char *argv[])
timer = current_time(); timer = current_time();
while(1) while(1) {
{
//printconnection(0); //printconnection(0);
Lossless_UDP(); Lossless_UDP();
if(is_connected(connection) >= 2) if(is_connected(connection) >= 2) {
{
kill_connection_in(connection, 3000000); kill_connection_in(connection, 3000000);
read = read_packet(connection, buffer); read = read_packet(connection, buffer);
if(read != 0) if(read != 0) {
{
// printf("Recieved data.\n"); // printf("Recieved data.\n");
if(!fwrite(buffer, read, 1, file)) if(!fwrite(buffer, read, 1, file)) {
{
printf("file write error\n"); printf("file write error\n");
} }
} }
} }
if(is_connected(connection) == 4) if(is_connected(connection) == 4) {
{
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
fclose(file); fclose(file);
return 1; return 1;

View File

@ -104,15 +104,12 @@ int main(int argc, char *argv[])
exit(0); exit(0);
} }
initMessenger(); initMessenger();
if(argc > 3) if(argc > 3) {
{
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
} } else {
else
{
FILE *file = fopen(argv[1], "rb"); FILE *file = fopen(argv[1], "rb");
if ( file==NULL ){return 1;} if ( file==NULL ){return 1;}
int read; int read;
@ -127,8 +124,7 @@ int main(int argc, char *argv[])
printf("OUR ID: "); printf("OUR ID: ");
uint32_t i; uint32_t i;
for(i = 0; i < 32; i++) for(i = 0; i < 32; i++) {
{
if(self_public_key[i] < 16) if(self_public_key[i] < 16)
printf("0"); printf("0");
printf("%hhX",self_public_key[i]); printf("%hhX",self_public_key[i]);
@ -138,16 +134,14 @@ int main(int argc, char *argv[])
char temp_id[128]; char temp_id[128];
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
if(scanf("%s", temp_id) != 1) if(scanf("%s", temp_id) != 1) {
{
return 1; return 1;
} }
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
perror("Initialization"); perror("Initialization");
while(1) while(1) {
{
uint8_t name[128]; uint8_t name[128];
getname(num, name); getname(num, name);
printf("%s\n", name); printf("%s\n", name);
@ -163,5 +157,4 @@ int main(int argc, char *argv[])
free(buffer); free(buffer);
fclose(file); fclose(file);
} }
} }

View File

@ -51,8 +51,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
unsigned char * val = malloc(strlen(hex_string)); unsigned char * val = malloc(strlen(hex_string));
char * pos = hex_string; char * pos = hex_string;
int i=0; int i=0;
while(i < strlen(hex_string)) while(i < strlen(hex_string)) {
{
sscanf(pos,"%2hhx",&val[i]); sscanf(pos,"%2hhx",&val[i]);
pos+=2; pos+=2;
i++; i++;
@ -190,6 +189,7 @@ void do_refresh()
clrtoeol(); clrtoeol();
refresh(); refresh();
} }
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
{ {
new_lines("[i] received friend request"); new_lines("[i] received friend request");
@ -205,6 +205,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
new_lines(numchar); new_lines(numchar);
} }
} }
void print_message(int friendnumber, uint8_t * string, uint16_t length) void print_message(int friendnumber, uint8_t * string, uint16_t length)
{ {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
@ -220,6 +221,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this
new_lines(msg); new_lines(msg);
} }
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
@ -227,6 +229,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
new_lines(msg); new_lines(msg);
} }
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
@ -234,6 +237,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
new_lines(msg); new_lines(msg);
} }
void load_key(){ void load_key(){
FILE *data_file = NULL; FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) { if ((data_file = fopen("data","r"))) {
@ -260,6 +264,7 @@ void load_key(){
} }
fclose(data_file); fclose(data_file);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 4) { if (argc < 4) {
@ -317,9 +322,7 @@ int main(int argc, char *argv[])
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
nodelay(stdscr, TRUE); nodelay(stdscr, TRUE);
while(true) { while(true) {
if (on == 0 && DHT_isconnected()) {
if (on == 0 && DHT_isconnected())
{
new_lines("[i] connected to DHT\n[i] define username with /n"); new_lines("[i] connected to DHT\n[i] define username with /n");
on = 1; on = 1;
} }

View File

@ -20,6 +20,7 @@
* along with Tox. If not, see <http://www.gnu.org/licenses/>. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
#ifndef NTOX_H #ifndef NTOX_H
#define NTOX_H #define NTOX_H