Merge official

This commit is contained in:
Michael Rose 2013-08-15 11:24:40 +02:00
commit 52d3194e87
24 changed files with 682 additions and 561 deletions

View File

@ -1,4 +1,4 @@
![Project Tox](https://rbt.asia/boards/g/img/0352/79/1373823047559.png "Project Tox")
![Project Tox](https://raw.github.com/irungentoo/ProjectTox-Core/master/other/tox.png "Project Tox")
Project Tox, _also known as Tox_, is a FOSS (Free and Open Source Software) instant messaging application aimed to replace Skype.<br />
With the rise of governmental monitoring programs, Tox aims to be an easy to use, all-in-one communication platform (including audio, and videochats in the future) that ensures their users full privacy and secure message delivery.<br /> <br />

View File

@ -197,6 +197,43 @@ START_TEST(test_endtoend)
}
END_TEST
START_TEST(test_large_data)
{
unsigned char k[crypto_box_BEFORENMBYTES];
unsigned char n[crypto_box_NONCEBYTES];
unsigned char m1[MAX_DATA_SIZE - ENCRYPTION_PADDING];
unsigned char c1[sizeof(m1) + ENCRYPTION_PADDING];
unsigned char m1prime[sizeof(m1)];
unsigned char m2[MAX_DATA_SIZE];
unsigned char c2[sizeof(m2) + ENCRYPTION_PADDING];
int c1len, c2len;
int m1plen;
//Generate random messages
rand_bytes(m1, sizeof(m1));
rand_bytes(m2, sizeof(m2));
rand_bytes(n, crypto_box_NONCEBYTES);
//Generate key
rand_bytes(k, crypto_box_BEFORENMBYTES);
c1len = encrypt_data_fast(k, n, m1, sizeof(m1), c1);
c2len = encrypt_data_fast(k, n, m2, sizeof(m2), c2);
ck_assert_msg(c1len == sizeof(m1) + ENCRYPTION_PADDING, "Could not encrypt max size");
ck_assert_msg(c2len == -1, "incorrectly succeeded encrypting massive size");
m1plen = decrypt_data_fast(k, n, c1, c1len, m1prime);
ck_assert_msg(m1plen == sizeof(m1), "decrypted text lengths differ");
ck_assert_msg(memcmp(m1prime, m1, sizeof(m1)) == 0, "decrypted texts differ");
}
END_TEST
#define DEFTESTCASE(NAME) \
TCase *NAME = tcase_create(#NAME); \
tcase_add_test(NAME, test_##NAME); \
@ -209,6 +246,7 @@ Suite* crypto_suite(void)
DEFTESTCASE(known);
DEFTESTCASE(fast_known);
DEFTESTCASE(endtoend);
DEFTESTCASE(large_data);
return s;
}

View File

@ -63,17 +63,6 @@
/*----------------------------------------------------------------------------------*/
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
uint64_t timestamp;
uint64_t last_pinged;
/* Returned by this node. Either our friend or us */
IP_Port ret_ip_port;
uint64_t ret_timestamp;
} Client_data;
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS];
@ -115,6 +104,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
/*----------------------------------------------------------------------------------*/
Client_data * DHT_get_close_list(void)
{
return close_clientlist;
}
/* Compares client_id1 and client_id2 with client_id
* return 0 if both are same distance
* return 1 if client_id1 is closer
@ -633,6 +628,8 @@ static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
int DHT_addfriend(uint8_t * client_id)
{
if(friend_number(client_id) != -1) /*Is friend already in DHT?*/
return 1;
Friend * temp;
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
if (temp == NULL)
@ -930,49 +927,30 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
}
/* Handle a received ping request for */
static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length)
static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
{
if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
|| length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
uint64_t ping_id;
memcpy(&ping_id, packet + 1, sizeof(uint64_t));
int friendnumber = friend_number(source_pubkey);
if (friendnumber == -1)
return 1;
/* check if request is for us. */
if (id_equal(packet + 1, self_public_key)) {
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
Friend * friend = &friends_list[friendnumber];
int len = handle_request(public_key, data, packet, length);
if (len != sizeof(uint64_t) + 1)
return 1;
uint64_t ping_id;
memcpy(&ping_id, data + 1, sizeof(uint64_t));
int friendnumber = friend_number(public_key);
if (friendnumber == -1)
return 1;
Friend * friend = &friends_list[friendnumber];
if (data[0] == 0) {
/* 1 is reply */
send_NATping(public_key, ping_id, 1);
friend->recvNATping_timestamp = unix_time();
if (packet[0] == 0) {
/* 1 is reply */
send_NATping(source_pubkey, ping_id, 1);
friend->recvNATping_timestamp = unix_time();
return 0;
} else if (packet[0] == 1) {
if (friend->NATping_id == ping_id) {
friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
friend->hole_punching = 1;
return 0;
} else if (data[0] == 1) {
if (friend->NATping_id == ping_id) {
friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
friend->hole_punching = 1;
return 0;
}
}
return 1;
}
/* if request is not for us, try routing it. */
route_packet(packet + 1, packet, length);
return 0;
return 1;
}
/* Get the most common ip in the ip_portlist
@ -1080,7 +1058,7 @@ void DHT_init(void)
networking_registerhandler(1, &handle_ping_response);
networking_registerhandler(2, &handle_getnodes);
networking_registerhandler(3, &handle_sendnodes);
networking_registerhandler(254, &handle_NATping);
cryptopacket_registerhandler(254, &handle_NATping);
}
void doDHT(void)

View File

@ -34,6 +34,19 @@ extern "C" {
/* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
uint64_t timestamp;
uint64_t last_pinged;
/* Returned by this node. Either our friend or us */
IP_Port ret_ip_port;
uint64_t ret_timestamp;
} Client_data;
Client_data * DHT_get_close_list(void);
/* Add a new friend to the friends list
client_id must be CLIENT_ID_SIZE bytes long.
returns 0 if success

View File

@ -75,9 +75,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
return -1;
}
/*
/*
* returns a uint16_t that represents the checksum of address of length len
*
*
* TODO: Another checksum algorithm might be better.
*/
static uint16_t address_checksum(uint8_t *address, uint32_t len)
@ -94,7 +94,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
/*
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
*
*/
void getaddress(Messenger *m, uint8_t *address)
{
@ -118,7 +118,7 @@ void getaddress(Messenger *m, uint8_t *address)
* return FAERR_ALREADYSENT if friend request already sent or already a friend
* return FAERR_UNKNOWN for unknown error
* return FAERR_BADCHECKSUM if bad checksum in address
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different
* (the nospam for that friend was set to the new one)
*/
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
@ -449,8 +449,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
m->friendlist[friendnumber].receives_read_receipts = yesno;
}
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; */
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */
/* set the function that will be executed when a friend request is received. */
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata)
{
@ -461,55 +460,48 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_message = function;
m->friend_message_isset = 1;
m->friend_message_userdata = userdata;
}
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_action = function;
m->friend_action_isset = 1;
m->friend_action_userdata = userdata;
}
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_namechange = function;
m->friend_namechange_isset = 1;
m->friend_namechange_userdata = userdata;
}
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
{
m->friend_statusmessagechange = function;
m->friend_statusmessagechange_isset = 1;
m->friend_statuschange_userdata = userdata;
}
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata)
{
m->friend_userstatuschange = function;
m->friend_userstatuschange_isset = 1;
m->friend_userstatuschange_userdata = userdata;
}
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata)
{
m->read_receipt = function;
m->read_receipt_isset = 1;
m->read_receipt_userdata = userdata;
}
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata)
{
m->friend_connectionstatuschange = function;
m->friend_connectionstatuschange_isset = 1;
m->friend_connectionstatuschange_userdata = userdata;
}
static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status)
{
if (!m->friend_connectionstatuschange_isset)
if (!m->friend_connectionstatuschange)
return;
if (status == NOFRIEND)
return;
@ -580,7 +572,10 @@ Messenger * initMessenger(void)
/* run this before closing shop */
void cleanupMessenger(Messenger *m){
/* FIXME TODO it seems no one frees friendlist or all the elements status */
/* FIXME TODO ideally cleanupMessenger will mirror initMessenger
* this requires the other modules to expose cleanup functions
*/
free(m->friendlist);
free(m);
}
@ -648,7 +643,7 @@ void doFriends(Messenger *m)
case PACKET_ID_NICKNAME: {
if (data_length >= MAX_NAME_LENGTH || data_length == 0)
break;
if(m->friend_namechange_isset)
if(m->friend_namechange)
m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
memcpy(m->friendlist[i].name, data, data_length);
m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
@ -659,7 +654,7 @@ void doFriends(Messenger *m)
break;
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
if (m->friend_statusmessagechange_isset)
if (m->friend_statusmessagechange)
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH),
m->friend_statuschange_userdata);
set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
@ -670,7 +665,7 @@ void doFriends(Messenger *m)
if (data_length != 1)
break;
USERSTATUS status = data[0];
if (m->friend_userstatuschange_isset)
if (m->friend_userstatuschange)
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
set_friend_userstatus(m, i, status);
break;
@ -683,12 +678,12 @@ void doFriends(Messenger *m)
if (m->friendlist[i].receives_read_receipts) {
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
}
if (m->friend_message_isset)
if (m->friend_message)
(*m->friend_message)(m, i, message, message_length, m->friend_message_userdata);
break;
}
case PACKET_ID_ACTION: {
if (m->friend_action_isset)
if (m->friend_action)
(*m->friend_action)(m, i, data, data_length, m->friend_action_userdata);
break;
}
@ -698,7 +693,7 @@ void doFriends(Messenger *m)
break;
memcpy(&msgid, data, sizeof(msgid));
msgid = ntohl(msgid);
if (m->read_receipt_isset)
if (m->read_receipt)
(*m->read_receipt)(m, i, msgid, m->read_receipt_userdata);
break;
}
@ -744,7 +739,7 @@ void doMessenger(Messenger *m)
doNetCrypto();
doInbound(m);
doFriends(m);
timer_poll();
}

View File

@ -111,28 +111,20 @@ typedef struct Messenger {
uint32_t numfriends;
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_message_isset;
void* friend_message_userdata;
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_action_isset;
void* friend_action_userdata;
void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_namechange_isset;
void* friend_namechange_userdata;
void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
uint8_t friend_statusmessagechange_isset;
void* friend_statusmessagechange_userdata;
void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*);
uint8_t friend_userstatuschange_isset;
void* friend_userstatuschange_userdata;
void (*read_receipt)(struct Messenger *m, int, uint32_t, void*);
uint8_t read_receipt_isset;
void* read_receipt_userdata;
void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*);
uint8_t friend_statuschange_isset;
void* friend_statuschange_userdata;
void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*);
uint8_t friend_connectionstatuschange_isset;
void* friend_connectionstatuschange_userdata;
@ -141,7 +133,7 @@ typedef struct Messenger {
/*
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
*
*/
void getaddress(Messenger *m, uint8_t *address);
@ -157,7 +149,7 @@ void getaddress(Messenger *m, uint8_t *address);
* return -4 if friend request already sent or already a friend
* return -5 for unknown error
* return -6 if bad checksum in address
* return -7 if the friend was already there but the nospam was different
* return -7 if the friend was already there but the nospam was different
* (the nospam for that friend was set to the new one)
*/
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);

View File

@ -123,39 +123,23 @@ static int request_received(uint8_t * client_id)
}
static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t length)
static int friendreq_handlepacket(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
{
if (packet[0] == 32) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
return 1;
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
if (handle_friendrequest_isset == 0)
return 1;
if (handle_friendrequest_isset == 0)
return 1;
if (length <= sizeof(nospam))
return 1;
if (request_received(source_pubkey))
return 1;
if (memcmp(packet, &nospam, sizeof(nospam)) != 0)
return 1;
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
int len = handle_request(public_key, data, packet, length);
if (len == -1)
return 1;
if (len <= sizeof(nospam))
return 1;
if (request_received(public_key))
return 1;
if (memcmp(data, &nospam, sizeof(nospam)) != 0)
return 1;
addto_receivedlist(public_key);
(*handle_friendrequest)(public_key, data + 4, len - 4, handle_friendrequest_userdata);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;
}
}
return 1;
addto_receivedlist(source_pubkey);
(*handle_friendrequest)(source_pubkey, packet + 4, length - 4, handle_friendrequest_userdata);
return 0;
}
void friendreq_init(void)
{
networking_registerhandler(32, &friendreq_handlepacket);
cryptopacket_registerhandler(32, &friendreq_handlepacket);
}

View File

@ -71,33 +71,6 @@ uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
return check; // We return zero if mem is made out of zeroes.
}
/* encrypts plain of length length to encrypted of length + 16 using the
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
return -1 if there was a problem.
return length of encrypted data if everything was fine. */
int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint8_t *plain, uint32_t length, uint8_t *encrypted)
{
if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0)
return -1;
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
/* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero
apparently memcmp should not be used so we do this instead:*/
if(crypto_iszero(temp_encrypted, crypto_box_BOXZEROBYTES) != 0)
return -1;
/* unpad the encrypted message */
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
}
/* Precomputes the shared key from their public_key and our secret_key.
This way we can avoid an expensive elliptic curve scalar multiply for each
encrypt/decrypt operation.
@ -114,7 +87,7 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0)
return -1;
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
@ -129,35 +102,6 @@ int encrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
}
/* decrypts encrypted of length length to plain of length length - 16 using the
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
return -1 if there was a problem(decryption failed)
return length of plain data if everything was fine. */
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint8_t *encrypted, uint32_t length, uint8_t *plain)
{
if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
return -1;
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
nonce, public_key, secret_key) == -1)
return -1;
/* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero
apparently memcmp should not be used so we do this instead:*/
if(crypto_iszero(temp_plain, crypto_box_ZEROBYTES) != 0)
return -1;
/* unpad the plain message */
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES);
return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
}
/* Fast decrypt. Depends on enc_ley from encrypt_precompute. */
int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
uint8_t *encrypted, uint32_t length, uint8_t *plain)
@ -165,7 +109,7 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
return -1;
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
@ -184,6 +128,22 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
}
int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint8_t *plain, uint32_t length, uint8_t *encrypted)
{
uint8_t k[crypto_box_BEFORENMBYTES];
encrypt_precompute(public_key, secret_key, k);
return encrypt_data_fast(k, nonce, plain, length, encrypted);
}
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint8_t *encrypted, uint32_t length, uint8_t *plain)
{
uint8_t k[crypto_box_BEFORENMBYTES];
encrypt_precompute(public_key, secret_key, k);
return decrypt_data_fast(k, nonce, encrypted, length, plain);
}
/* increment the given nonce by 1 */
static void increment_nonce(uint8_t *nonce)
{
@ -261,15 +221,18 @@ 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)
{
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING)
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING)
return -1;
uint8_t nonce[crypto_box_NONCEBYTES];
uint8_t temp[MAX_DATA_SIZE];
memcpy(temp + 1, data, length);
temp[0] = request_id;
random_nonce(nonce);
int len = encrypt_data(public_key, self_secret_key, nonce, data, length,
int len = encrypt_data(public_key, self_secret_key, nonce, temp, length,
1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
if (len == -1)
return -1;
packet[0] = request_id;
packet[0] = 32;
memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES);
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
@ -281,7 +244,7 @@ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t
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)
static int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length)
{
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
@ -289,16 +252,51 @@ int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t
memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
uint8_t nonce[crypto_box_NONCEBYTES];
uint8_t temp[MAX_DATA_SIZE];
memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data);
if(len1 == -1)
length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
if(len1 == -1 || len1 == 0)
return -1;
request_id[0] = temp[0];
--len1;
memcpy(data, temp + 1, len1);
return len1;
} else
return -1;
}
static cryptopacket_handler_callback cryptopackethandlers[256] = {0};
void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb)
{
cryptopackethandlers[byte] = cb;
}
static int cryptopacket_handle(IP_Port source, uint8_t * packet, uint32_t length)
{
if (packet[0] == 32) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
return 1;
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
uint8_t number;
int len = handle_request(public_key, data, &number, packet, length);
if (len == -1 || len == 0)
return 1;
if (!cryptopackethandlers[number]) return 1;
cryptopackethandlers[number](source, public_key, data, len - 1);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;
}
}
return 1;
}
/* 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 */
@ -619,6 +617,7 @@ void initNetCrypto(void)
{
memset(crypto_connections, 0 ,sizeof(crypto_connections));
memset(incoming_connections, -1 ,sizeof(incoming_connections));
networking_registerhandler(32, &cryptopacket_handle);
uint32_t i;
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
crypto_connections[i].number = ~0;

View File

@ -25,6 +25,7 @@
#define NET_CRYPTO_H
#include "Lossless_UDP.h"
#include "DHT.h"
#ifdef __cplusplus
extern "C" {
@ -88,11 +89,10 @@ 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);
typedef int (*cryptopacket_handler_callback)(IP_Port ip_port, uint8_t * source_pubkey, uint8_t *data, uint32_t len);
/* Function to call when request beginning with byte is received */
void cryptopacket_registerhandler(uint8_t byte, cryptopacket_handler_callback cb);
/* Start a secure connection with other peer who has public_key and ip_port
returns -1 if failure

BIN
other/tox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -53,10 +53,10 @@ void print_clientlist()
uint32_t i, j;
IP_Port p_ip;
printf("___________________CLOSE________________________________\n");
for(i = 0; i < 4; i++) {
for(i = 0; i < 32; i++) {
printf("ClientID: ");
for(j = 0; j < 32; j++) {
printf("%hhX", close_clientlist[i].client_id[j]);
printf("%02hhX", close_clientlist[i].client_id[j]);
}
p_ip = close_clientlist[i].ip_port;
printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));

View File

@ -52,28 +52,15 @@ uint8_t num_requests = 0;
void get_id(Messenger *m, char *data)
{
char idstring0[200];
char idstring1[FRIEND_ADDRESS_SIZE][5];
char idstring2[FRIEND_ADDRESS_SIZE][5];
sprintf(data, "[i] ID: ");
int offset = strlen(data);
int i = 0;
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for(i = 0; i < FRIEND_ADDRESS_SIZE; i++)
for(; i < FRIEND_ADDRESS_SIZE; i++)
{
if (address[i] < (FRIEND_ADDRESS_SIZE / 2))
strcpy(idstring1[i],"0");
else
strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",address[i]);
sprintf(data + 2*i + offset, "%02X ", address[i]);
}
strcpy(idstring0,"[i] ID: ");
int j = 0;
for (j = 0; j < FRIEND_ADDRESS_SIZE; j++) {
strcat(idstring0,idstring1[j]);
strcat(idstring0,idstring2[j]);
}
memcpy(data, idstring0, strlen(idstring0));
}
void new_lines(char *line)

View File

@ -8,8 +8,10 @@ set(exe_name toxic)
add_executable(${exe_name}
main.c
windows.c
prompt.c
friendlist.c
dhtstatus.c
chat.c
configdir.c)

View File

@ -13,6 +13,8 @@
#include "../../core/network.h"
#include "windows.h"
#include "friendlist.h"
#include "chat.h"
#define CURS_Y_OFFSET 3
@ -24,10 +26,6 @@ typedef struct {
WINDOW* linewin;
} ChatContext;
extern int active_window;
extern void del_window(ToxWindow *w, int f_num);
extern void fix_name(uint8_t *name);
void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
@ -316,11 +314,10 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "Your ID: %s\n", id);
wprintw(ctx->history, "%s\n", id);
}
else if (strcmp(ctx->line, "/close") == 0) {
active_window = 0; // Go to prompt screen
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self, f_num);

6
testing/toxic/chat.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef CHAT_H_6489PZ13
#define CHAT_H_6489PZ13
ToxWindow new_chat(Messenger *m, int friendnum);
#endif /* end of include guard: CHAT_H_6489PZ13 */

89
testing/toxic/dhtstatus.c Normal file
View File

@ -0,0 +1,89 @@
#include "dhtstatus.h"
#include "string.h"
#include "../../core/network.h"
#include "../../core/DHT.h"
typedef uint8_t ipbuf[3*4+3+1];
static int num_selected = 0;
static void printip(ipbuf buf, IP ip)
{
sprintf((char*)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]);
}
static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key)
{
switch(key) {
case KEY_UP:
case 'k':
if (--num_selected < 0)
num_selected = CLIENT_ID_SIZE-1;
break;
case KEY_DOWN:
case 'j':
num_selected = (num_selected+1) % CLIENT_ID_SIZE;
break;
case '\n':
break;
default:
break;
}
}
static void dhtstatus_onDraw(ToxWindow *self)
{
Client_data * close_clientlist = DHT_get_close_list();
curs_set(0);
werase(self->window);
uint64_t now = unix_time();
uint32_t i, j;
ipbuf ipbuf;
wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now);
for(i = 0; i < CLIENT_ID_SIZE; i++) {
Client_data * client = close_clientlist + i;
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window,"[%02i] ", i);
uint16_t port = ntohs(client->ip_port.port);
if(port) {
for(j = 0; j < 32; j++)
wprintw(self->window, "%02hhx", client->client_id[j]);
printip(ipbuf, client->ip_port.ip);
wprintw(self->window, " %15s %5u ", ipbuf, port);
wprintw(self->window, " %3llu ", now - client->timestamp);
wprintw(self->window, " %3llu ", now - client->last_pinged);
port = ntohs(client->ret_ip_port.port);
if(port) {
printip(ipbuf, client->ret_ip_port.ip);
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp);
}
}
wprintw(self->window, "\n");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
}
wrefresh(self->window);
}
static void dhtstatus_onInit(ToxWindow *self, Messenger *m)
{
}
ToxWindow new_dhtstatus()
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &dhtstatus_onKey;
ret.onDraw = &dhtstatus_onDraw;
ret.onInit = &dhtstatus_onInit;
strcpy(ret.title, "[dht status]");
return ret;
}

View File

@ -0,0 +1,8 @@
#ifndef _dhtstatus_h
#define _dhtstatus_h
#include "windows.h"
ToxWindow new_dhtstatus();
#endif

View File

@ -11,12 +11,9 @@
#include "../../core/network.h"
#include "windows.h"
#include "friendlist.h"
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
extern int add_window(ToxWindow w, int n);
extern ToxWindow new_chat(Messenger *m, int friendnum);
extern int active_window;
static char * WINDOW_STATUS;
typedef struct {
uint8_t name[MAX_NAME_LENGTH];
@ -54,8 +51,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num;
add_window(new_chat(m, num), i);
active_window = i;
add_window(m, new_chat(m, num), i);
break;
}
}
@ -111,7 +107,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == num_selected) {
active_window = i;
set_active_window(i);
break;
}
}
@ -121,8 +117,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = num_selected;
add_window(new_chat(m, num_selected), i);
active_window = i;
add_window(m, new_chat(m, num_selected), i);
break;
}
}
@ -169,7 +164,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
}
ToxWindow new_friendlist() {
ToxWindow new_friendlist(char * ws) {
WINDOW_STATUS = ws;
ToxWindow ret;
memset(&ret, 0, sizeof(ret));

View File

@ -0,0 +1,12 @@
#ifndef FRIENDLIST_H_53I41IM
#define FRIENDLIST_H_53I41IM
#include "windows.h"
#include "chat.h"
ToxWindow new_friendlist(char * ws);
int friendlist_onFriendAdded(Messenger *m, int num);
void disable_chatwin(int f_num);
void fix_name(uint8_t *name);
#endif /* end of include guard: FRIENDLIST_H_53I41IM */

View File

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <signal.h>
#ifdef _win32
#include <direct.h>
@ -21,104 +22,21 @@
#include "configdir.h"
#include "windows.h"
#include "prompt.h"
#include "friendlist.h"
extern ToxWindow new_prompt();
extern ToxWindow new_friendlist();
extern int friendlist_onFriendAdded(Messenger *m, int num);
extern void disable_chatwin(int f_num);
extern int add_req(uint8_t *public_key); // XXX
extern unsigned char *hex_string_to_bin(char hex_string[]);
static int store_data(char*);
/* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS];
#ifndef TOXICVER
#define TOXICVER "NOVER" //Use the -D flag to set this
#endif
static ToxWindow windows[MAX_WINDOW_SLOTS];
static ToxWindow* prompt;
static Messenger *m;
static char *DATA_FILE;
int w_num;
int active_window;
/* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
void on_window_resize(int sig)
{
int n = add_req(public_key);
wprintw(prompt->window, "\nFriend request from:\n");
int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
}
wprintw(prompt->window, "\nWith the message: %s\n", data);
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length);
}
endwin();
refresh();
clear();
}
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
}
}
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], m, friendnumber, string, length);
}
}
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], friendnumber, string, length);
}
}
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i;
for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
}
}
void on_friendadded(int friendnumber)
{
friendlist_onFriendAdded(m, friendnumber);
int st;
if ((st = store_data(DATA_FILE)) != 0) {
wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st);
}
}
/* CALLBACKS END */
static void init_term()
{
/* Setup terminal */
signal(SIGWINCH, on_window_resize);
initscr();
cbreak();
keypad(stdscr, 1);
@ -136,10 +54,10 @@ static void init_term()
refresh();
}
static void init_tox()
static Messenger *init_tox()
{
/* Init core */
m = initMessenger();
Messenger *m = initMessenger();
/* Callbacks */
m_callback_friendrequest(m, on_request, NULL);
@ -154,6 +72,7 @@ static void init_tox()
#else
setname(m, (uint8_t*) "Hipster", sizeof("Hipster"));
#endif
return m;
}
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@ -202,67 +121,7 @@ int init_connection(void)
return 0;
}
void init_window_status()
{
/* Default window values decrement from -2 */
int i;
for (i = 0; i < N_DEFAULT_WINS; ++i)
WINDOW_STATUS[i] = -(i+2);
int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1;
}
int add_window(ToxWindow w, int n)
{
if (w_num >= TOXWINDOWS_MAX_NUM)
return -1;
if (LINES < 2)
return -1;
w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL)
return -1;
windows[n] = w;
w.onInit(&w, m);
w_num++;
return n;
}
/* Deletes window w and cleans up */
void del_window(ToxWindow *w, int f_num)
{
delwin(w->window);
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == f_num) {
WINDOW_STATUS[i] = -1;
disable_chatwin(f_num);
break;
}
}
clear();
refresh();
}
static void init_windows()
{
w_num = 0;
int n_prompt = 0;
int n_friendslist = 1;
if (add_window(new_prompt(), n_prompt) == -1
|| add_window(new_friendlist(), n_friendslist) == -1) {
fprintf(stderr, "add_window() failed.\n");
endwin();
exit(1);
}
prompt = &windows[n_prompt];
}
static void do_tox()
static void do_tox(Messenger *m, ToxWindow * prompt)
{
static int conn_try = 0;
static int conn_err = 0;
@ -286,190 +145,69 @@ static void do_tox()
doMessenger(m);
}
static void populate_friends()
static void load_data(Messenger *m, char *path)
{
wprintw(prompt->window, "Populating friends...\n");
uint32_t i;
for (i = 0; i < m->numfriends; i++) {
wprintw(prompt->window, "Added friend %d\n", i);
friendlist_onFriendAdded(m, i);
FILE *fd;
size_t len;
uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
fclose(fd);
endwin();
exit(1);
}
}
/*
* Store Messenger data to path
* Return 0 Messenger stored successfully
* Return 1 malloc failed
* Return 2 fopen failed
* Return 3 fwrite failed
*/
static int store_data(char *path)
{
FILE *fd;
size_t len;
uint8_t *buf;
if (fread(buf, len, 1, fd) != 1){
fprintf(stderr, "fread() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
Messenger_load(m, buf, len);
}
else {
len = Messenger_size(m);
buf = malloc(len);
if (buf == NULL) {
return 1;
fprintf(stderr, "malloc() failed.\n");
endwin();
exit(1);
}
Messenger_save(m, buf);
fd = fopen(path, "w");
if (fd == NULL) {
free(buf);
return 2;
fprintf(stderr, "fopen() failed.\n");
free(buf);
endwin();
exit(1);
}
if (fwrite(buf, len, 1, fd) != 1) {
free(buf);
fclose(fd);
return 3;
}
free(buf);
fclose(fd);
wprintw(prompt->window, "Messenger stored\n");
return 0;
}
static void load_data(char *path) {
FILE *fd;
size_t len;
uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
fclose(fd);
endwin();
exit(1);
}
if (fread(buf, len, 1, fd) != 1) {
fprintf(stderr, "fread() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
if (Messenger_load(m, buf, len) != 0) {
fprintf(stderr, "Problem while loading messenger");
}
free(buf);
fclose(fd);
} else {
int st;
if ((st = store_data(path)) != 0) {
fprintf(stderr, "storing messenger failed with error code: %d", st);
endwin();
exit(1);
}
}
}
static void draw_bar()
{
static int odd = 0;
int blinkrate = 30;
attron(COLOR_PAIR(4));
mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4));
move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD);
printw(" TOXIC " TOXICVER "|");
attroff(COLOR_PAIR(4) | A_BOLD);
int i;
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (WINDOW_STATUS[i] != -1) {
if (i == active_window)
attron(A_BOLD);
odd = (odd+1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate/2)))
attron(COLOR_PAIR(3));
printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate/2)))
attroff(COLOR_PAIR(3));
if (i == active_window) {
attroff(A_BOLD);
}
}
}
refresh();
}
void prepare_window(WINDOW *w)
{
mvwin(w, 0, 0);
wresize(w, LINES-2, COLS);
}
/* Shows next window when tab or back-tab is pressed */
void set_active_window(int ch)
{
int f_inf = 0;
int max = MAX_WINDOW_SLOTS-1;
if (ch == '\t') {
int i = (active_window + 1) % max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
i = (i + 1) % max;
if (f_inf++ > max) { // infinite loop check
endwin();
exit(2);
}
}
}else {
int i = active_window - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
exit(2);
}
if (fwrite(buf, len, 1, fd) != 1){
fprintf(stderr, "fwrite() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
}
free(buf);
fclose(fd);
}
int main(int argc, char *argv[])
{
int ch;
ToxWindow* a;
char *user_config_dir = get_user_config_dir();
int config_err = create_user_config_dir(user_config_dir);
if(config_err) {
DATA_FILE = "data";
} else {
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data");
}
free(user_config_dir);
char *DATA_FILE = NULL;
int config_err = 0;
/* This is broken */
int f_loadfromfile = 1;
int f_flag = 0;
int i = 0;
@ -479,7 +217,7 @@ int main(int argc, char *argv[])
else if (argv[i][0] == '-') {
if (argv[i][1] == 'f') {
if (argv[i + 1] != NULL)
DATA_FILE = argv[i + 1];
DATA_FILE = strdup(argv[i + 1]);
else
f_flag = -1;
} else if (argv[i][1] == 'n') {
@ -488,15 +226,27 @@ int main(int argc, char *argv[])
}
}
if (DATA_FILE == NULL ) {
config_err = create_user_config_dir(user_config_dir);
if (config_err) {
DATA_FILE = strdup("data");
} else {
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data");
}
}
free(user_config_dir);
init_term();
init_tox();
init_windows();
Messenger *m = init_tox();
ToxWindow *prompt = init_windows(m);
init_window_status();
if(f_loadfromfile) {
load_data(DATA_FILE);
populate_friends();
}
if(f_loadfromfile)
load_data(m, DATA_FILE);
free(DATA_FILE);
if (f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD);
@ -513,25 +263,11 @@ int main(int argc, char *argv[])
}
while(true) {
/* Update tox */
do_tox();
do_tox(m, prompt);
/* Draw */
a = &windows[active_window];
prepare_window(a->window);
a->blink = false;
draw_bar();
a->onDraw(a);
/* Handle input */
ch = getch();
if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch);
else if (ch != ERR)
a->onKey(a, m, ch);
draw_active_window(m);
}
cleanupMessenger(m);
free(DATA_FILE);
return 0;
}

View File

@ -11,11 +11,12 @@
#include "../../core/network.h"
#include "windows.h"
#include "prompt.h"
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests=0; // XXX
extern void on_friendadded(int friendnumber);
static friendAddedFn *on_friendadded;
static char prompt_buf[MAX_STR_SIZE] = {0};
static int prompt_buf_pos = 0;
@ -87,7 +88,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args)
wprintw(self->window, "Failed to add friend.\n");
else {
wprintw(self->window, "Friend accepted as: %d.\n", num);
on_friendadded(num);
on_friendadded(m, num);
}
}
@ -121,6 +122,11 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
}
id_bin[i] = x;
}
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
id[i] = toupper(id[i]);
}
int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1);
switch (num) {
case FAERR_TOOLONG:
@ -138,9 +144,15 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
case FAERR_UNKNOWN:
wprintw(self->window, "Undefined error when adding friend.\n");
break;
case FAERR_BADCHECKSUM:
wprintw(self->window, "Bad checksum in address.\n");
break;
case FAERR_SETNEWNOSPAM:
wprintw(self->window, "Nospam was different.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded(num);
on_friendadded(m, num);
break;
}
}
@ -226,7 +238,7 @@ void cmd_myid(ToxWindow *self, Messenger *m, char **args)
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(self->window, "Your ID: %s\n", id);
wprintw(self->window, "%s\n", id);
}
void cmd_nick(ToxWindow *self, Messenger *m, char **args)
@ -422,8 +434,9 @@ static void prompt_onInit(ToxWindow *self, Messenger *m)
wclrtoeol(self->window);
}
ToxWindow new_prompt()
ToxWindow new_prompt(friendAddedFn *f)
{
on_friendadded = f;
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &prompt_onKey;

14
testing/toxic/prompt.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef PROMPT_H_UZYGWFFL
#define PROMPT_H_UZYGWFFL
#include "windows.h"
typedef void (friendAddedFn)(Messenger *m, int friendnumber);
ToxWindow new_prompt(friendAddedFn *f);
int add_req(uint8_t *public_key);
unsigned char *hex_string_to_bin(char hex_string[]);
#endif /* end of include guard: PROMPT_H_UZYGWFFL */

239
testing/toxic/windows.c Normal file
View File

@ -0,0 +1,239 @@
#include "friendlist.h"
#include "prompt.h"
#include "dhtstatus.h"
#include "windows.h"
/* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS];
static int w_num;
static ToxWindow windows[MAX_WINDOW_SLOTS];
static Messenger *m;
int active_window;
static ToxWindow* prompt;
/* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
{
int n = add_req(public_key);
wprintw(prompt->window, "\nFriend request from:\n");
int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
}
wprintw(prompt->window, "\nWith the message: %s\n", data);
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length);
}
}
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
}
}
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], m, friendnumber, string, length);
}
}
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], friendnumber, string, length);
}
}
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
{
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i;
for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
}
}
void on_friendadded(Messenger *m, int friendnumber)
{
friendlist_onFriendAdded(m, friendnumber);
}
/* CALLBACKS END */
int add_window(Messenger *m, ToxWindow w, int n)
{
if (w_num >= TOXWINDOWS_MAX_NUM)
return -1;
if (LINES < 2)
return -1;
w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL)
return -1;
windows[n] = w;
w.onInit(&w, m);
w_num++;
active_window = n;
return n;
}
/* Deletes window w and cleans up */
void del_window(ToxWindow *w, int f_num)
{
active_window = 0; // Go to prompt screen
delwin(w->window);
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == f_num) {
WINDOW_STATUS[i] = -1;
disable_chatwin(f_num);
break;
}
}
clear();
refresh();
}
/* Shows next window when tab or back-tab is pressed */
void set_active_window(int ch)
{
int f_inf = 0;
int max = MAX_WINDOW_SLOTS-1;
if (ch == '\t') {
int i = (active_window + 1) % max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
i = (i + 1) % max;
if (f_inf++ > max) { // infinite loop check
endwin();
exit(2);
}
}
}else {
int i = active_window - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
exit(2);
}
}
}
}
void init_window_status()
{
/* Default window values decrement from -2 */
int i;
for (i = 0; i < N_DEFAULT_WINS; ++i)
WINDOW_STATUS[i] = -(i+2);
int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1;
}
ToxWindow *init_windows()
{
w_num = 0;
int n_prompt = 0;
int n_friendslist = 1;
int n_dhtstatus = 2;
if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
|| add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1
|| add_window(m, new_dhtstatus(), n_dhtstatus) == -1) {
fprintf(stderr, "add_window() failed.\n");
endwin();
exit(1);
}
active_window = n_prompt;
prompt = &windows[n_prompt];
return prompt;
}
static void draw_bar()
{
static int odd = 0;
int blinkrate = 30;
attron(COLOR_PAIR(4));
mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4));
move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD);
printw(" TOXIC " TOXICVER "|");
attroff(COLOR_PAIR(4) | A_BOLD);
int i;
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (WINDOW_STATUS[i] != -1) {
if (i == active_window)
attron(A_BOLD);
odd = (odd+1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate/2)))
attron(COLOR_PAIR(3));
printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate/2)))
attroff(COLOR_PAIR(3));
if (i == active_window) {
attroff(A_BOLD);
}
}
}
refresh();
}
void prepare_window(WINDOW *w)
{
mvwin(w, 0, 0);
wresize(w, LINES-2, COLS);
}
void draw_active_window(Messenger *m)
{
ToxWindow *a = &windows[active_window];
prepare_window(a->window);
a->blink = false;
draw_bar();
a->onDraw(a);
/* Handle input */
int ch = getch();
if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch);
else if (ch != ERR)
a->onKey(a, m, ch);
}

View File

@ -1,19 +1,28 @@
/*
* Toxic -- Tox Curses Client
*/
#ifndef _windows_h
#define _windows_h
#include <curses.h>
#include <stdint.h>
#include <stdbool.h>
#include "../../core/Messenger.h"
#define TOXWINDOWS_MAX_NUM 32
#define MAX_FRIENDS_NUM 100
#define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32
/* number of permanent default windows */
#define N_DEFAULT_WINS 2
#define N_DEFAULT_WINS 3
/* maximum window slots for WINDOW_STATUS array */
#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM
#ifndef TOXICVER
#define TOXICVER "NOVER" //Use the -D flag to set this
#endif
typedef struct ToxWindow_ ToxWindow;
struct ToxWindow_ {
@ -32,3 +41,17 @@ struct ToxWindow_ {
WINDOW* window;
};
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata);
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
void init_window_status();
ToxWindow * init_windows();
void draw_active_window(Messenger * m);
int add_window(Messenger *m, ToxWindow w, int n);
void del_window(ToxWindow *w, int f_num);
void set_active_window(int ch);
#endif