This commit is contained in:
Sean Qureshi 2013-08-03 11:41:47 -07:00
commit e4d63c63c7
10 changed files with 180 additions and 123 deletions

View File

@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 2.6.0)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
if(UNIX)
find_package(Curses REQUIRED)
endif()
if(NOT WIN32)
option(USE_NACL "Use NaCl library instead of libsodium")
endif()
@ -29,15 +33,15 @@ if(NOT USE_NACL)
endif()
macro(linkCoreLibraries exe_name)
add_dependencies(${exe_name} core)
add_dependencies(${exe_name} toxcore)
if(WIN32)
include_directories(${CMAKE_HOME_DIRECTORY}/sodium/include/)
target_link_libraries(${exe_name} core
target_link_libraries(${exe_name} toxcore
${CMAKE_SOURCE_DIR}/sodium/lib/libsodium.a
ws2_32)
else()
include_directories(${SODIUM_INCLUDE_DIR})
target_link_libraries(${exe_name} core
target_link_libraries(${exe_name} toxcore
${LINK_CRYPTO_LIBRARY})
endif()

View File

@ -18,6 +18,14 @@ Build dependencies:
```bash
apt-get install build-essential libtool autotools-dev automake libconfig-dev ncurses-dev cmake checkinstall
```
On Fedora:
```bash
yum groupinstall "Development Tools"
yum install libtool autoconf automake libconfig-devel ncurses-devel cmake
```
Note that `libconfig-dev` should be >= 1.4.
You should get and install [libsodium](https://github.com/jedisct1/libsodium):
@ -31,6 +39,20 @@ sudo checkinstall --install --pkgname libsodium --pkgversion 0.4.2 --nodoc
sudo ldconfig
```
Or if checkinstall is not easily available for your distribution (e.g. Fedora),
this will install the libs to /usr/local/lib and the headers to /usr/local/include:
```bash
git clone git://github.com/jedisct1/libsodium.git
cd libsodium
git checkout tags/0.4.2
./autogen.sh
./configure
make check
sudo make install
```
Then clone this repo and generate makefile:
```bash
git clone git://github.com/irungentoo/ProjectTox-Core.git

View File

@ -7,6 +7,7 @@ With the rise of governmental monitoring programs, Tox aims to be an easy to use
**IRC**: #tox on freenode, alternatively, you can use the [webchat](http://webchat.freenode.net/?channels=#tox).<br />
**Website**: [http://tox.im](http://tox.im)
**Developer Blog**: [http://dev.tox.im](http://dev.tox.im)
**Website translations**: [see stal888's repository](https://github.com/stal888/ProjectTox-Website)<br/>
**Qt GUI**: [see nurupo's repository](https://github.com/nurupo/ProjectTox-Qt-GUI)

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 2.6.0)
project(core C)
project(toxcore C)
if(WIN32)
include_directories(${CMAKE_HOME_DIRECTORY}/sodium/include/)
@ -16,4 +16,4 @@ set(core_sources
LAN_discovery.c
Messenger.c)
add_library(core ${core_sources})
add_library(toxcore SHARED ${core_sources})

View File

@ -119,23 +119,39 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
* return 1 if client_id1 is closer
* return 2 if client_id2 is closer
*/
int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)
int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2)
{
uint32_t i;
uint8_t tmp1, tmp2;
size_t i;
uint8_t distance1, distance2;
for(i = 0; i < CLIENT_ID_SIZE; ++i) {
tmp1 = abs(client_id[i] ^ client_id1[i]);
tmp2 = abs(client_id[i] ^ client_id2[i]);
distance1 = abs(id[i] ^ id1[i]);
distance2 = abs(id[i] ^ id2[i]);
if(tmp1 < tmp2)
if(distance1 < distance2)
return 1;
else if(tmp1 > tmp2)
if(distance1 > distance2)
return 2;
}
return 0;
}
int ipport_equal(IP_Port a, IP_Port b)
{
return (a.ip.i == b.ip.i) && (a.port == b.port);
}
int id_equal(uint8_t* a, uint8_t* b)
{
return memcmp(a, b, CLIENT_ID_SIZE) == 0;
}
int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
{
return timestamp + timeout <= time_now;
}
/* check if client with client_id is already in list of length length.
* if it is then set its corresponding timestamp to current time.
* if the id is already in the list with a different ip_port, update it.
@ -150,12 +166,11 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
for(i = 0; i < length; ++i) {
/*If ip_port is assigned to a different client_id replace it*/
if(list[i].ip_port.ip.i == ip_port.ip.i &&
list[i].ip_port.port == ip_port.port) {
if(ipport_equal(list[i].ip_port, ip_port)) {
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
}
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
if(id_equal(list[i].client_id, client_id)) {
/* Refresh the client timestamp. */
list[i].timestamp = temp_time;
list[i].ip_port.ip.i = ip_port.ip.i;
@ -172,10 +187,12 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
{
uint32_t i;
for(i = 0; i < length; ++i) {
if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
if(id_equal(list[i].client_id, client_id))
return 1;
}
return 0;
}
@ -184,10 +201,12 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
static int friend_number(uint8_t * client_id)
{
uint32_t i;
for(i = 0; i < num_friends; ++i) {
if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
if(id_equal(friends_list[i].client_id, client_id))
return i;
}
return -1;
}
@ -199,11 +218,11 @@ static int friend_number(uint8_t * client_id)
int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
{
uint32_t i, j, k;
uint64_t temp_time = unix_time();
uint64_t temp_time = unix_time();
int num_nodes = 0, closest, tout, inlist;
for (i = 0; i < LCLIENT_LIST; ++i) {
tout = close_clientlist[i].timestamp <= temp_time - BAD_NODE_TIMEOUT;
tout = is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT);
inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, close_clientlist[i].client_id);
/* if node isn't good or is already in list. */
@ -218,7 +237,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
num_nodes++;
} else {
for(j = 0; j < MAX_SENT_NODES; ++j) {
closest = id_closest( client_id,
nodes_list[j].client_id,
@ -237,7 +258,8 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
for(i = 0; i < num_friends; ++i) {
for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
tout = friends_list[i].client_list[j].timestamp <= temp_time - BAD_NODE_TIMEOUT;
tout = is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT);
inlist = client_in_nodelist( nodes_list,
MAX_SENT_NODES,
friends_list[i].client_list[j].client_id);
@ -288,7 +310,7 @@ int replace_bad( Client_data * list,
uint64_t temp_time = unix_time();
for(i = 0; i < length; ++i) {
/* if node is bad */
if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) {
if(is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) {
memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
list[i].ip_port = ip_port;
list[i].timestamp = temp_time;
@ -376,27 +398,23 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
uint32_t i, j;
uint64_t temp_time = unix_time();
if (memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) {
for (i = 0; i < LCLIENT_LIST; ++i) {
if (id_equal(client_id, self_public_key)) {
if (memcmp( nodeclient_id,
close_clientlist[i].client_id,
CLIENT_ID_SIZE ) == 0) {
for (i = 0; i < LCLIENT_LIST; ++i) {
if (id_equal(nodeclient_id, close_clientlist[i].client_id)) {
close_clientlist[i].ret_ip_port = ip_port;
close_clientlist[i].ret_timestamp = temp_time;
return;
}
}
} else {
for (i = 0; i < num_friends; ++i) {
if (memcmp( client_id,
friends_list[i].client_id,
CLIENT_ID_SIZE ) == 0) {
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
if (memcmp( nodeclient_id,
friends_list[i].client_list[j].client_id,
CLIENT_ID_SIZE ) == 0) {
} else {
for (i = 0; i < num_friends; ++i) {
if (id_equal(client_id, friends_list[i].client_id)) {
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
if (id_equal(nodeclient_id, friends_list[i].client_list[j].client_id)) {
friends_list[i].client_list[j].ret_ip_port = ip_port;
friends_list[i].client_list[j].ret_timestamp = temp_time;
return;
@ -404,6 +422,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
}
}
}
}
}
@ -419,14 +438,15 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
uint64_t temp_time = unix_time();
for (i = 0; i < LPING_ARRAY; ++i ) {
if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
if (!is_timeout(temp_time, pings[i].timestamp, PING_TIMEOUT)) {
pinging = 0;
if (ip_port.ip.i != 0 &&
pings[i].ip_port.ip.i == ip_port.ip.i &&
pings[i].ip_port.port == ip_port.port)
if (ip_port.ip.i != 0 && ipport_equal(pings[i].ip_port, ip_port))
++pinging;
if (ping_id != 0 && pings[i].ping_id == ping_id)
++pinging;
if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
return 1;
}
@ -443,14 +463,15 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
uint64_t temp_time = unix_time();
for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
if(!is_timeout(temp_time, send_nodes[i].timestamp, PING_TIMEOUT)) {
pinging = 0;
if(ip_port.ip.i != 0 &&
send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
send_nodes[i].ip_port.port == ip_port.port)
if(ip_port.ip.i != 0 && ipport_equal(send_nodes[i].ip_port, ip_port))
++pinging;
if(ping_id != 0 && send_nodes[i].ping_id == ping_id)
++pinging;
if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
return 1;
}
@ -471,10 +492,9 @@ uint64_t add_pinging(IP_Port ip_port)
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
uint64_t temp_time = unix_time();
for(i = 0; i < PING_TIMEOUT; ++i ) {
for(j = 0; j < LPING_ARRAY; ++j ) {
if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
if(is_timeout(temp_time, pings[j].timestamp, PING_TIMEOUT - i)) {
pings[j].timestamp = temp_time;
pings[j].ip_port = ip_port;
pings[j].ping_id = ping_id;
@ -495,7 +515,7 @@ uint64_t add_gettingnodes(IP_Port ip_port)
for(i = 0; i < PING_TIMEOUT; ++i ) {
for(j = 0; j < LSEND_NODES_ARRAY; ++j ) {
if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) {
if(is_timeout(temp_time, send_nodes[j].timestamp, PING_TIMEOUT - i)) {
send_nodes[j].timestamp = temp_time;
send_nodes[j].ip_port = ip_port;
send_nodes[j].ping_id = ping_id;
@ -513,8 +533,7 @@ uint64_t add_gettingnodes(IP_Port ip_port)
static int pingreq(IP_Port ip_port, uint8_t * public_key)
{
/* check if packet is gonna be sent to ourself */
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0
|| is_pinging(ip_port, 0))
if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
return 1;
uint64_t ping_id = add_pinging(ip_port);
@ -548,7 +567,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
{
/* check if packet is gonna be sent to ourself */
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
if(id_equal(public_key, self_public_key))
return 1;
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
@ -577,8 +596,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
{
/* check if packet is gonna be sent to ourself */
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0
|| is_gettingnodes(ip_port, 0))
if(id_equal(public_key, self_public_key) || is_gettingnodes(ip_port, 0))
return 1;
uint64_t ping_id = add_gettingnodes(ip_port);
@ -617,7 +635,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
{
/* check if packet is gonna be sent to ourself */
if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)
if(id_equal(public_key, self_public_key))
return 1;
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
@ -665,7 +683,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
return 1;
/* check if packet is from ourself. */
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
if(id_equal(packet + 1, self_public_key))
return 1;
int len = decrypt_data( packet + 1,
@ -691,7 +709,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
return 1;
/* check if packet is from ourself. */
if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
if(id_equal(packet + 1, self_public_key))
return 1;
int len = decrypt_data( packet + 1,
@ -720,7 +738,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
return 1;
/* check if packet is from ourself. */
if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)
if (id_equal(packet + 1, self_public_key))
return 1;
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
@ -810,16 +828,20 @@ int DHT_delfriend(uint8_t * client_id)
Friend * temp;
for (i = 0; i < num_friends; ++i) {
/* Equal */
if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
if (id_equal(friends_list[i].client_id, client_id)) {
--num_friends;
if (num_friends != i) {
memcpy( friends_list[i].client_id,
friends_list[num_friends].client_id,
CLIENT_ID_SIZE );
}
temp = realloc(friends_list, sizeof(Friend) * (num_friends));
if (temp != NULL)
friends_list = temp;
if (temp == NULL)
return 1;
friends_list = temp;
return 0;
}
}
@ -836,12 +858,9 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
for (i = 0; i < num_friends; ++i) {
/* Equal */
if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) {
if (id_equal(friends_list[i].client_id, client_id)) {
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
if (memcmp( friends_list[i].client_list[j].client_id,
client_id,
CLIENT_ID_SIZE ) == 0 &&
friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)
if (id_equal(friends_list[i].client_list[j].client_id, client_id) && !is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT))
return friends_list[i].client_list[j].ip_port;
}
return empty;
@ -865,14 +884,14 @@ void doDHTFriends()
uint32_t num_nodes = 0;
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
/* if node is not dead. */
if (friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) {
if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
pingreq( friends_list[i].client_list[j].ip_port,
friends_list[i].client_list[j].client_id );
friends_list[i].client_list[j].last_pinged = temp_time;
}
/* if node is good. */
if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) {
if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) {
index[num_nodes] = j;
++num_nodes;
}
@ -903,14 +922,14 @@ void doClose()
for (i = 0; i < LCLIENT_LIST; ++i) {
/* if node is not dead. */
if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
pingreq( close_clientlist[i].ip_port,
close_clientlist[i].client_id );
close_clientlist[i].last_pinged = temp_time;
}
/* if node is good. */
if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
if (!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) {
index[num_nodes] = i;
++num_nodes;
}
@ -937,10 +956,12 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
{
uint32_t i;
for (i = 0; i < LCLIENT_LIST; ++i) {
if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
if (id_equal(client_id, close_clientlist[i].client_id))
return sendpacket(close_clientlist[i].ip_port, packet, length);
}
return -1;
}
@ -966,10 +987,9 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
client = &friend->client_list[i];
/*If ip is not zero and node is good */
if (client->ret_ip_port.ip.i != 0 &&
client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
if (memcmp(client->client_id, friend->client_id, CLIENT_ID_SIZE) == 0)
if (id_equal(client->client_id, friend->client_id))
return 0;
ip_portlist[num_ips] = client->ret_ip_port;
@ -997,8 +1017,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
client = &friend->client_list[i];
/*If ip is not zero and node is good */
if (client->ret_ip_port.ip.i != 0 &&
client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
if (sendpacket(client->ip_port, packet, length) == length)
++sent;
@ -1028,8 +1047,7 @@ int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
client = &friend->client_list[i];
/*If ip is not zero and node is good */
if(client->ret_ip_port.ip.i != 0 &&
client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
if(client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
ip_list[n] = client->ip_port;
++n;
}
@ -1052,7 +1070,7 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
uint32_t i;
for (i = 0; i < num_friends; ++i) {
/* Equal */
if (memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0)
if (id_equal(friends_list[i].client_id, friend_id))
return friend_iplist(ip_portlist, i);
}
return -1;
@ -1094,7 +1112,7 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
return 1;
/* check if request is for us. */
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
if (id_equal(packet + 1, self_public_key)) {
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
@ -1329,7 +1347,7 @@ int DHT_isconnected()
uint64_t temp_time = unix_time();
for(i = 0; i < LCLIENT_LIST; ++i) {
if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)
if(!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT))
return 1;
}
return 0;

View File

@ -57,6 +57,7 @@
#include <sodium.h>
#else
#include <crypto_box.h>
#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
#endif
#ifdef __cplusplus

View File

@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "nTox.h"
#include "misc_tools.h"
@ -54,7 +54,7 @@ void get_id(char *data)
{
if (self_public_key[i] < (PUB_KEY_BYTES / 2))
strcpy(idstring1[i],"0");
else
else
strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",self_public_key[i]);
}
@ -71,9 +71,9 @@ void get_id(char *data)
void new_lines(char *line)
{
int i = 0;
for (i = HISTORY-1; i > 0; i--)
for (i = HISTORY-1; i > 0; i--)
strncpy(lines[i], lines[i-1], STRING_LENGTH - 1);
strncpy(lines[0], line, STRING_LENGTH - 1);
do_refresh();
}
@ -133,7 +133,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
if (inpt_command == 'f') { // add friend command: /f ID
int i;
char temp_id[128];
for (i = 0; i < 128; i++)
for (i = 0; i < 128; i++)
temp_id[i] = line[i+prompt_offset];
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
@ -166,8 +166,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
}
else if (inpt_command == 'm') { //message command: /m friendnumber messsage
size_t len = strlen(line);
if(len < 3)
return;
if(len < 3)
return;
char numstring[len-3];
char message[len-3];
@ -248,7 +248,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
else if (inpt_command == 'q') { //exit
endwin();
exit(EXIT_SUCCESS);
} else {
} else {
new_lines("[i] invalid command");
}
} else {
@ -335,7 +335,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
new_lines(format_message((char*)string, friendnumber));
}
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];
getname(friendnumber, (uint8_t*)name);
@ -344,7 +344,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
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];
getname(friendnumber, (uint8_t*)name);
@ -353,7 +353,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
new_lines(msg);
}
void load_key()
void load_key()
{
FILE *data_file = NULL;
data_file = fopen("data","r");
@ -368,7 +368,7 @@ void load_key()
exit(1);
}
Messenger_load(data, size);
} else {
} else {
//else save new keys
int size = Messenger_size();
uint8_t data[size];
@ -418,9 +418,9 @@ int main(int argc, char *argv[])
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
else
exit(1);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
nodelay(stdscr, TRUE);
while(true) {
@ -441,7 +441,7 @@ int main(int argc, char *argv[])
if (c == '\n') {
line_eval(lines, line);
strcpy(line, "");
} else if (c == 127) {
} else if (c == 8 || c == 127) {
line[strlen(line)-1] = '\0';
} else if (isalnum(c) || ispunct(c) || c == ' ') {
strcpy(line, appender(line, (char) c));

View File

@ -32,6 +32,7 @@ uint32_t maxnumfriends;
char line[STRING_LENGTH];
char users_id[200];
int friend_request_received;
void do_header()
{
@ -44,10 +45,11 @@ void do_header()
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
{
friend_request_received = 1;
printf("\n\n[i] received friend request with message\n");
printf((char *)data);
printf("'%s'",(char *)data);
char numchar[100];
sprintf(numchar, "\n\n[i] accept request with /a %u\n\n", num_requests);
sprintf(numchar, "\n[i] accept request with /a %u\n\n", num_requests);
printf(numchar);
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
++num_requests;
@ -151,23 +153,32 @@ void add_friend()
void list_friends()
{
int activefriends = 0;
int i;
printf("\n[i] Friend List");
printf("----- PENDING -----\n\n");
for (i = 0; i <= maxnumfriends; i++) {
if (m_friendstatus(i) == 4)
activefriends++;
char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name);
if (m_friendstatus(i) > 0 && m_friendstatus(i) < 4)
printf("[%d] %s\n", i, (uint8_t*)name);
}
printf("\n");
printf("\n[i] Friend List | Total: %d\n\n", activefriends);
printf("----- ACTIVE -----\n\n");
for (i = 0; i <= 256; i++) {/* TODO: fix this properly*/
for (i = 0; i <= maxnumfriends; i++) {
char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name);
if (m_friendstatus(i) == 4)
printf("[%d] %s\n\n", i, (uint8_t*)name);
printf("[%d] %s\n", i, (uint8_t*)name);
}
printf("\n");
}
void delete_friend()
@ -244,7 +255,7 @@ void change_nickname()
fclose(name_file);
}
void change_status()
void change_status(int savetofile)
{
uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0;
@ -263,20 +274,21 @@ void change_status()
sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
printf(numstring);
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "w");
fprintf(status_file, "%s", (char*)status);
fclose(status_file);
if (savetofile == 1) {
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "w");
fprintf(status_file, "%s", (char*)status);
fclose(status_file);
}
}
void accept_friend_request()
{
friend_request_received = 0;
uint8_t numf = atoi(line + 3);
char numchar[100];
sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
printf(numchar);
int num = m_addfriend_norequest(pending_requests[numf]);
sprintf(numchar, "\n[i] added friendnumber %d\n\n", num);
sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num);
printf(numchar);
++maxnumfriends;
}
@ -288,7 +300,7 @@ void line_eval(char* line)
char inpt_command = line[1];
if(inpt_command == 'f') {
add_friend(line);
add_friend();
}
else if (inpt_command == 'r') {
@ -297,32 +309,33 @@ void line_eval(char* line)
}
else if (inpt_command == 'l') {
list_friends(line);
list_friends();
}
else if (inpt_command == 'd') {
delete_friend(line);
delete_friend();
}
/* Send message to friend */
else if (inpt_command == 'm') {
message_friend(line);
message_friend();
}
else if (inpt_command == 'n') {
change_nickname(line);
change_nickname();
}
else if (inpt_command == 's') {
change_status(line);
change_status(1);
}
else if (inpt_command == 'a') {
accept_friend_request(line);
if (friend_request_received == 1)
accept_friend_request(line);
}
/* EXIT */
else if (inpt_command == 'q') {
uint8_t status[MAX_USERSTATUS_LENGTH] = "Offline";
m_set_userstatus(status, strlen((char*)status));
strcpy(line, "---Offline");
change_status(0);
exit(EXIT_SUCCESS);
}
}
@ -368,8 +381,7 @@ int main(int argc, char *argv[])
nameloaded = 1;
printf("%s\n", name);
fclose(name_file);
}
}
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "r");
@ -383,7 +395,6 @@ int main(int argc, char *argv[])
printf("%s\n", status);
fclose(status_file);
}
m_callback_friendrequest(print_request);
m_callback_friendmessage(print_message);

View File

@ -39,7 +39,7 @@ void list_friends();
void delete_friend();
void message_friend();
void change_nickname();
void change_status();
void change_status(int savetofile);
void accept_friend_request();
void line_eval(char* line);
void get_input();

View File

@ -50,7 +50,7 @@ void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) {
void on_message(int friendnumber, uint8_t* string, uint16_t length) {
size_t i;
wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string);
wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
for(i=0; i<w_num; i++) {
if(windows[i].onMessage != NULL)