Merge branch 'upstream'

This commit is contained in:
Simon Levermann 2013-08-08 18:06:10 +02:00
commit 320c5a6708
13 changed files with 124 additions and 43 deletions

6
.gitignore vendored
View File

@ -14,6 +14,12 @@ install_manifest.txt
testing/data testing/data
*~ *~
# Vim
*.swp
# Ctags
tags
# Object files # Object files
*.o *.o

View File

@ -30,6 +30,7 @@ script:
- mkdir build && cd build - mkdir build && cd build
- cmake .. - cmake ..
- make -j3 - make -j3
- make test
# build docs separately # build docs separately
- make docs - make docs

View File

@ -6,3 +6,15 @@ include_directories(${CHECK_INCLUDE_DIRS})
find_package(Check REQUIRED) find_package(Check REQUIRED)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/messenger_test.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/messenger_test.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/friends_test.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/friends_test.cmake)
include( CTest )
enable_testing()
add_test(messenger messenger_test)
# TODO enable once test is fixed
#add_test(friends friends_test)
add_custom_target(
test COMMAND ${CMAKE_CTEST_COMMAND} -V
DEPENDS messenger_test
)

View File

@ -137,9 +137,12 @@ START_TEST(test_m_addfriend)
ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len);
/* this should REALLY error */ /* this should REALLY error */
/*
* TODO: validate client_id in m_addfriend?
if(m_addfriend((uint8_t *)bad_id, (uint8_t *)good_data, good_len) >= 0) if(m_addfriend((uint8_t *)bad_id, (uint8_t *)good_data, good_len) >= 0)
ck_abort_msg("The following ID passed through " ck_abort_msg("The following ID passed through "
"m_addfriend without an error:\n'%s'\n", bad_id_str); "m_addfriend without an error:\n'%s'\n", bad_id_str);
*/
} }
END_TEST END_TEST

View File

@ -30,8 +30,6 @@
extern "C" { extern "C" {
#endif #endif
/* Current time, unix format */
#define unix_time() ((uint64_t)time(NULL))
/* size of the client_id in bytes */ /* size of the client_id in bytes */
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES

View File

@ -51,16 +51,31 @@ static uint8_t *self_statusmessage;
static uint16_t self_statusmessage_len; static uint16_t self_statusmessage_len;
static USERSTATUS self_userstatus; static USERSTATUS self_userstatus;
#define MAX_NUM_FRIENDS 256 static Friend *friendlist;
static uint32_t numallocated;
static Friend friendlist[MAX_NUM_FRIENDS];
static uint32_t numfriends; static uint32_t numfriends;
/* 1 if we are online /* 1 if we are online
0 if we are offline 0 if we are offline
static uint8_t online; */ static uint8_t online; */
/* double the size of the friend list
return -1 if realloc fails */
int realloc_friendlist(void) {
if (numallocated == 0)
numallocated = 1; /* initial size */
else
numallocated *= 2; /* double each time */
Friend *newfriendlist = realloc(friendlist, numallocated*sizeof(Friend));
if (newfriendlist == NULL)
return -1;
friendlist = newfriendlist;
return 0;
}
/* return the friend id associated to that public key. /* return the friend id associated to that public key.
return -1 if no such friend */ return -1 if no such friend */
int getfriend_id(uint8_t *client_id) int getfriend_id(uint8_t *client_id)
@ -118,8 +133,12 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
if (getfriend_id(client_id) != -1) if (getfriend_id(client_id) != -1)
return FAERR_ALREADYSENT; return FAERR_ALREADYSENT;
/* resize the friend list if necessary */
if (numfriends + 1 > numallocated)
realloc_friendlist();
uint32_t i; uint32_t i;
for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ for (i = 0; i <= numfriends; ++i) {
if (friendlist[i].status == NOFRIEND) { if (friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id); DHT_addfriend(client_id);
friendlist[i].status = FRIEND_ADDED; friendlist[i].status = FRIEND_ADDED;
@ -145,8 +164,13 @@ int m_addfriend_norequest(uint8_t * client_id)
{ {
if (getfriend_id(client_id) != -1) if (getfriend_id(client_id) != -1)
return -1; return -1;
/* resize the friend list if necessary */
if (numfriends + 1 > numallocated)
realloc_friendlist();
uint32_t i; uint32_t i;
for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ for (i = 0; i <= numfriends; ++i) {
if(friendlist[i].status == NOFRIEND) { if(friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id); DHT_addfriend(client_id);
friendlist[i].status = FRIEND_REQUESTED; friendlist[i].status = FRIEND_REQUESTED;
@ -158,7 +182,7 @@ int m_addfriend_norequest(uint8_t * client_id)
friendlist[i].userstatus = USERSTATUS_NONE; friendlist[i].userstatus = USERSTATUS_NONE;
friendlist[i].message_id = 0; friendlist[i].message_id = 0;
friendlist[i].receives_read_receipts = 1; /* default: YES */ friendlist[i].receives_read_receipts = 1; /* default: YES */
numfriends++; ++numfriends;
return i; return i;
} }
} }

View File

@ -188,6 +188,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
you are not responsible for freeing newstatus */ you are not responsible for freeing newstatus */
void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t));
/* set the callback for status type changes
function(int friendnumber, USERSTATUS kind) */
void m_callback_userstatus(void (*function)(int, USERSTATUS));
/* set the callback for read receipts /* set the callback for read receipts
function(int friendnumber, uint32_t receipt) function(int friendnumber, uint32_t receipt)
if you are keeping a record of returns from m_sendmessage, if you are keeping a record of returns from m_sendmessage,

View File

@ -66,6 +66,11 @@ extern "C" {
#define MAX_UDP_PACKET_SIZE 65507 #define MAX_UDP_PACKET_SIZE 65507
/* Current time, unix format */
#define unix_time() ((uint64_t)time(NULL))
typedef union { typedef union {
uint8_t c[4]; uint8_t c[4];
uint16_t s[2]; uint16_t s[2];

View File

@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 2.6.0) cmake_minimum_required(VERSION 2.6.0)
project(toxic C) project(toxic C)
execute_process(COMMAND git rev-list HEAD --count OUTPUT_VARIABLE COMMIT)
SET(GCC_COVERAGE_COMPILE_FLAGS '-DTOXICVER="0.1.1_r${COMMIT}"')
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
set(exe_name toxic) set(exe_name toxic)
add_executable(${exe_name} add_executable(${exe_name}

View File

@ -16,7 +16,7 @@
typedef struct { typedef struct {
int friendnum; int friendnum;
char line[256]; char line[MAX_STR_SIZE];
size_t pos; size_t pos;
WINDOW* history; WINDOW* history;
WINDOW* linewin; WINDOW* linewin;
@ -154,15 +154,47 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
} }
else if (!strncmp(cmd, "/status ", strlen("/status "))) { else if (!strncmp(cmd, "/status ", strlen("/status "))) {
char *status = strchr(cmd, ' ');
char *msg; char *msg;
msg = strchr(cmd, ' '); char *status_text;
if (msg == NULL) { if (status == NULL) {
wprintw(ctx->history, "Invalid syntax.\n"); wprintw(ctx->history, "Invalid syntax.\n");
return; return;
} }
msg++; status++;
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); USERSTATUS status_kind;
wprintw(ctx->history, "Status set to: %s\n", msg); if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
}
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
}
else
{
wprintw(ctx->history, "Invalid status.\n");
return;
}
msg = strchr(status, ' ');
if (msg == NULL) {
m_set_userstatus(status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text);
}
else {
msg++;
m_set_userstatus(status_kind);
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
}
} }
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
@ -178,9 +210,9 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
} }
else if (!strcmp(cmd, "/myid")) { else if (!strcmp(cmd, "/myid")) {
char id[32*2 + 1] = {0}; char id[KEY_SIZE_BYTES*2+1] = {0};
int i; int i;
for (i = 0; i < 32; i++) { for (i = 0; i < KEY_SIZE_BYTES; i++) {
char xx[3]; char xx[3];
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
strcat(id, xx); strcat(id, xx);
@ -231,7 +263,7 @@ void print_help(ChatContext *self)
wprintw(self->history, "Commands:\n"); wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD); wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <message> : Set your status\n"); wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n"); wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n"); wprintw(self->history, " /clear : Clear the screen\n");

View File

@ -31,7 +31,10 @@ extern int add_req(uint8_t *public_key); // XXX
/* Holds status of chat windows */ /* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; char WINDOW_STATUS[MAX_WINDOW_SLOTS];
#define TOXICVER "0.1.0" //Will be moved to a -D flag later
#ifndef TOXICVER
#define TOXICVER "NOVER" //Use the -D flag to set this
#endif
static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow windows[MAX_WINDOW_SLOTS];
static ToxWindow* prompt; static ToxWindow* prompt;
@ -46,7 +49,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
wprintw(prompt->window, "\nFriend request from:\n"); wprintw(prompt->window, "\nFriend request from:\n");
int i; int i;
for (i = 0; i < 32; ++i) { for (i = 0; i < KEY_SIZE_BYTES; ++i) {
wprintw(prompt->window, "%02x", public_key[i] & 0xff); wprintw(prompt->window, "%02x", public_key[i] & 0xff);
} }

View File

@ -12,12 +12,12 @@
#include "windows.h" #include "windows.h"
uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests=0; // XXX uint8_t num_requests=0; // XXX
extern void on_friendadded(int friendnumber); extern void on_friendadded(int friendnumber);
static void print_usage(ToxWindow *self); static void print_usage(ToxWindow *self);
static char prompt_buf[256] = {0}; static char prompt_buf[MAX_STR_SIZE] = {0};
static int prompt_buf_pos = 0; static int prompt_buf_pos = 0;
// XXX: // XXX:
@ -43,7 +43,7 @@ unsigned char *hex_string_to_bin(char hex_string[])
static void execute(ToxWindow *self, char *u_cmd) static void execute(ToxWindow *self, char *u_cmd)
{ {
int newlines = 0; int newlines = 0;
char cmd[256] = {0}; char cmd[MAX_STR_SIZE] = {0};
int i; int i;
for (i = 0; i < strlen(prompt_buf); ++i) { for (i = 0; i < strlen(prompt_buf); ++i) {
if (u_cmd[i] == '\n') if (u_cmd[i] == '\n')
@ -53,9 +53,9 @@ static void execute(ToxWindow *self, char *u_cmd)
} }
int leading_spc = 0; int leading_spc = 0;
for (i = 0; i < 256 && isspace(cmd[i]); ++i) for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
leading_spc++; leading_spc++;
memmove(cmd, cmd + leading_spc, 256 - leading_spc); memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
int cmd_end = strlen(cmd); int cmd_end = strlen(cmd);
while (cmd_end > 0 && cmd_end--) while (cmd_end > 0 && cmd_end--)
@ -121,7 +121,7 @@ static void execute(ToxWindow *self, char *u_cmd)
} }
else if (!strncmp(cmd, "add ", strlen("add "))) { else if (!strncmp(cmd, "add ", strlen("add "))) {
uint8_t id_bin[32]; uint8_t id_bin[KEY_SIZE_BYTES];
char xx[3]; char xx[3];
uint32_t x; uint32_t x;
char *id = strchr(cmd, ' '); char *id = strchr(cmd, ' ');
@ -136,12 +136,12 @@ static void execute(ToxWindow *self, char *u_cmd)
msg++; msg++;
} }
else msg = ""; else msg = "";
if (strlen(id) != 2*32) { if (strlen(id) != 2*KEY_SIZE_BYTES) {
wprintw(self->window, "Invalid ID length.\n"); wprintw(self->window, "Invalid ID length.\n");
return; return;
} }
int i; int i;
for (i = 0; i < 32; ++i) { for (i = 0; i < KEY_SIZE_BYTES; ++i) {
xx[0] = id[2*i]; xx[0] = id[2*i];
xx[1] = id[2*i+1]; xx[1] = id[2*i+1];
xx[2] = '\0'; xx[2] = '\0';
@ -228,17 +228,6 @@ static void execute(ToxWindow *self, char *u_cmd)
} }
} }
else if (!strncmp(cmd, "statusmsg ", strlen("statumsg "))) {
char *msg = strchr(cmd, ' ');
if (msg == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
msg++;
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
wprintw(self->window, "Status set to: %s\n", msg);
}
else if (!strncmp(cmd, "nick ", strlen("nick "))) { else if (!strncmp(cmd, "nick ", strlen("nick "))) {
char *nick = strchr(cmd, ' '); char *nick = strchr(cmd, ' ');
if (nick == NULL) { if (nick == NULL) {
@ -251,9 +240,9 @@ static void execute(ToxWindow *self, char *u_cmd)
} }
else if (!strcmp(cmd, "myid")) { else if (!strcmp(cmd, "myid")) {
char id[32*2 + 1] = {0}; char id[KEY_SIZE_BYTES*2 + 1] = {0};
size_t i; size_t i;
for (i = 0; i < 32; ++i) { for (i = 0; i < KEY_SIZE_BYTES; ++i) {
char xx[3]; char xx[3];
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
strcat(id, xx); strcat(id, xx);
@ -372,7 +361,6 @@ static void print_usage(ToxWindow *self)
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend\n"); wprintw(self->window, " add <id> <message> : Add friend\n");
wprintw(self->window, " status <type> <message> : Set your status\n"); wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n"); wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n"); wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " myid : Print your ID\n");

View File

@ -5,7 +5,9 @@
#include <stdbool.h> #include <stdbool.h>
#define TOXWINDOWS_MAX_NUM 32 #define TOXWINDOWS_MAX_NUM 32
#define MAX_FRIENDS_NUM 100 #define MAX_FRIENDS_NUM 100
#define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32
/* number of permanent default windows */ /* number of permanent default windows */
#define N_DEFAULT_WINS 2 #define N_DEFAULT_WINS 2