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
#define DHT_H
@ -37,8 +36,6 @@ extern "C" {
/* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
/* Add a new friend to the friends list
client_id must be CLIENT_ID_SIZE bytes long.
returns 0 if success
@ -51,7 +48,6 @@ int DHT_addfriend(uint8_t * client_id);
returns 1 if failure (client_id not in friends list) */
int DHT_delfriend(uint8_t * client_id);
/* Get ip of friend
client_id must be CLIENT_ID_SIZE bytes long.
ip must be 4 bytes long.
@ -61,7 +57,6 @@ int DHT_delfriend(uint8_t * client_id);
returns ip of 1 if friend is not in list. */
IP_Port DHT_getfriendip(uint8_t * client_id);
/* Run this function at least a couple times per second (It's the main loop) */
void doDHT();
@ -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 */
void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key);
/* ROUTING FUNCTIONS */
/* 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 */
int route_tofriend(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
@ -96,8 +87,6 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length);
returns -1 if no such friend*/
int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id);
/* SAVE/LOAD functions */
/* 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. */
#include "Lossless_UDP.h"
/* maximum data packets in sent and receive queues. */
#define MAX_QUEUE_NUM 16
@ -88,7 +86,6 @@ typedef struct
uint8_t timeout; /* connection timeout in seconds. */
}Connection;
#define MAX_CONNECTIONS 256
static Connection connections[MAX_CONNECTIONS];
@ -117,7 +114,6 @@ int getconnection_id(IP_Port ip_port)
/* table of random numbers used below. */
static uint32_t randtable[6][256];
/* generate a handshake_id which depends on the ip_port.
this function will always give one unique handshake_id per ip_port.
TODO: make this better */
@ -138,6 +134,7 @@ uint32_t handshake_id(IP_Port source)
}
return id;
}
/* change the hnshake id associated with that ip_port
TODO: make this better */
void change_handshake(IP_Port source)
@ -146,7 +143,6 @@ void change_handshake(IP_Port source)
randtable[rand][((uint8_t *)&source)[rand]] = random_int();
}
/* initialize a new connection to ip_port
returns an integer corresponding to the connection id.
return -1 if it could not initialize the connection.
@ -314,6 +310,7 @@ char id_packet(int connection_id)
}
return -1;
}
/* return 0 if there is no received data in the buffer.
return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data)
@ -353,9 +350,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length)
return 0;
}
/* put the packet numbers the we are missing in requested and return the number */
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
One per packet type.
see docs/Lossless_UDP.txt for more information. */
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
{
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)
{
@ -468,8 +459,6 @@ int send_DATA(uint32_t connection_id)
/* END of packet sending functions */
/* Packet handling functions
One to handle each type of packets we receive
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 */
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
It's the main loop. */
void doLossless_UDP()

View File

@ -33,8 +33,6 @@ extern "C" {
/* maximum length of the data in the data packets */
#define MAX_DATA_SIZE 1024
/* Functions */
/* 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. */
int incoming_connection();
/* return -1 if it could not kill the connection.
return 0 if killed successfully */
int kill_connection(int connection_id);
@ -74,21 +71,16 @@ char id_packet(int connection_id);
return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data);
/* return 0 if data could not be put in packet queue
return 1 if data was put into the queue */
int write_packet(int connection_id, uint8_t * data, uint32_t length);
/* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(int connection_id);
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(int connection_id);
/* check if connection is connected
return 0 no.
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 */
int is_connected(int connection_id);
/* Call this function a couple times per second
It's the main loop. */
void doLossless_UDP();

View File

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

View File

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

View File

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

View File

@ -21,11 +21,9 @@
*
*/
#ifndef FRIEND_REQUESTS_H
#define FRIEND_REQUESTS_H
#include "DHT.h"
#include "net_crypto.h"
@ -37,7 +35,6 @@ extern "C" {
data is the data in the request and length is the length. */
int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length);
/* set the function that will be executed when a friend request for us is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
@ -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. */
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source);
#ifdef __cplusplus
}
#endif

View File

@ -26,12 +26,10 @@
#include "net_crypto.h"
/* Our public and secret keys. */
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
typedef struct
{
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
@ -177,7 +175,6 @@ int read_cryptpacket(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 */
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
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 */
@ -327,9 +323,6 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
return 1;
}
/* get crypto connection id from public key of peer
return -1 if there are no connections like we are looking for
return id if it found it */
@ -349,7 +342,6 @@ int getcryptconnection_id(uint8_t * public_key)
return -1;
}
/* 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. */
@ -447,7 +439,6 @@ int crypto_kill(int crypt_connection_id)
return 1;
}
/* accept an incoming connection using the parameters provided by crypto_inbound
return -1 if not successful
returns the crypt_connection_id if successful */
@ -505,7 +496,6 @@ int is_cryptoconnected(int crypt_connection_id)
return 0;
}
/* Generate our public and private keys
Only call this function the first time the program starts. */
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. */
void random_nonce(uint8_t * nonce);
/* return 0 if there is no received data in the buffer
return -1 if the packet was discarded.
return length of received data if successful */
int read_cryptpacket(int crypt_connection_id, uint8_t * data);
/* return 0 if data could not be put in packet queue
return 1 if data was put into the queue */
int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
@ -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 */
int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id);
/* puts the senders public key in the request in public_key, the data from the request
in data if a friend or ping request was sent to us and returns the length of the data.
packet is the request packet and length is its length
return -1 if not valid request. */
int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length);
/* Start a secure connection with other peer who has public_key and ip_port
returns -1 if failure
returns crypt_connection_id of the initialized connection if everything went well. */
int crypto_connect(uint8_t * public_key, IP_Port ip_port);
/* kill a crypto connection
return 0 if killed successfully
return 1 if there was a problem. */
@ -102,7 +97,6 @@ int crypto_kill(int crypt_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);
/* accept an incoming connection using the parameters provided by crypto_inbound
return -1 if not successful
returns the crypt_connection_id if successful */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -43,7 +43,6 @@
#endif
#define PORT 33445
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;
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
printf("--------------------BEGIN-----------------------------\n");
for(i = 0; i < length; i++)
{
for(i = 0; i < length; i++) {
if(data[i] < 16)
printf("0");
printf("%hhX",data[i]);
}
printf("\n--------------------END-----------------------------\n\n\n");
}
/*
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()
{
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
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(LosslessUDP_handlepacket(data, length, ip_port))
{
if(LosslessUDP_handlepacket(data, length, ip_port)) {
printpacket(data, length, ip_port);
}
else
{
} else {
//printconnection(0);
printf("Received handled packet with length: %u\n", length);
}
@ -137,14 +133,12 @@ void Lossless_UDP()
}
doLossless_UDP();
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
if (argc < 2) {
printf("usage: %s filename\n", argv[0]);
exit(0);
}
@ -167,14 +161,11 @@ int main(int argc, char *argv[])
uint64_t timer = current_time();
while(1)
{
while(1) {
Lossless_UDP();
connection = incoming_connection();
if(connection != -1)
{
if(is_connected(connection) == 2)
{
if(connection != -1) {
if(is_connected(connection) == 2) {
printf("Recieved the connection.\n");
}
@ -185,25 +176,20 @@ int main(int argc, char *argv[])
timer = current_time();
while(1)
{
while(1) {
//printconnection(0);
Lossless_UDP();
if(is_connected(connection) >= 2)
{
if(is_connected(connection) >= 2) {
kill_connection_in(connection, 3000000);
read = read_packet(connection, buffer);
if(read != 0)
{
if(read != 0) {
// printf("Recieved data.\n");
if(!fwrite(buffer, read, 1, file))
{
if(!fwrite(buffer, read, 1, file)) {
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));
fclose(file);
return 1;

View File

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

View File

@ -51,8 +51,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
unsigned char * val = malloc(strlen(hex_string));
char * pos = hex_string;
int i=0;
while(i < strlen(hex_string))
{
while(i < strlen(hex_string)) {
sscanf(pos,"%2hhx",&val[i]);
pos+=2;
i++;
@ -190,6 +189,7 @@ void do_refresh()
clrtoeol();
refresh();
}
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
{
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);
}
}
void print_message(int friendnumber, uint8_t * string, uint16_t 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
new_lines(msg);
}
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
char name[MAX_NAME_LENGTH];
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);
new_lines(msg);
}
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
char name[MAX_NAME_LENGTH];
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);
new_lines(msg);
}
void load_key(){
FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) {
@ -260,6 +264,7 @@ void load_key(){
}
fclose(data_file);
}
int main(int argc, char *argv[])
{
if (argc < 4) {
@ -317,9 +322,7 @@ int main(int argc, char *argv[])
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
nodelay(stdscr, 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");
on = 1;
}

View File

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