Merge branch 'master' into Integration

This commit is contained in:
Coren[m] 2013-09-09 21:31:11 +02:00
commit b91b87f285
8 changed files with 71 additions and 22 deletions

View File

@ -1,4 +1,4 @@
#include "group_chats.h"
#include "../../toxcore/group_chats.h"
#define NUM_CHATS 8
#ifdef WIN32
@ -84,6 +84,7 @@ int main()
ip_port.port = htons(12745);
for (i = 0; i < NUM_CHATS; ++i) {
group_newpeer(chats[0], chats[i]->self_public_key);
chat_bootstrap(chats[i], ip_port, chats[0]->self_public_key);
printf("%u\n", i);
}

View File

@ -1,4 +1,4 @@
#include "group_chats.h"
#include "../../toxcore/group_chats.h"
#define NUM_CHATS 8
#ifdef WIN32

View File

@ -25,6 +25,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/tox.c \
../toxcore/util.h \
../toxcore/util.c \
../toxcore/group_chats.h \
../toxcore/group_chats.c \
../toxcore/misc_tools.h
libtoxcore_la_CFLAGS = -I$(top_srcdir) \

View File

@ -346,17 +346,18 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
}
/* Set the name of a friend.
/* Set the name and name_length of a friend.
*
* return 0 if success.
* return -1 if failure.
*/
static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
static int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t len)
{
if (friend_not_valid(m, friendnumber))
return -1;
memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
m->friendlist[friendnumber].name_length = len;
memcpy(m->friendlist[friendnumber].name, name, len);
return 0;
}
@ -1033,7 +1034,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
for (i = 0; i < num; ++i) {
if (temp[i].status >= 3) {
int fnum = m_addfriend_norequest(m, temp[i].client_id);
setfriendname(m, fnum, temp[i].name);
setfriendname(m, fnum, temp[i].name, temp[i].name_length);
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
} else if (temp[i].status != 0) {
/* TODO: This is not a good way to do this. */

View File

@ -30,6 +30,7 @@
#include "DHT.h"
#include "friend_requests.h"
#include "LAN_discovery.h"
#include "group_chats.h"
#define MAX_NAME_LENGTH 128
#define MAX_STATUSMESSAGE_LENGTH 1007
@ -133,6 +134,9 @@ typedef struct Messenger {
Friend *friendlist;
uint32_t numfriends;
Group_Chat *chats;
uint32_t numchats;
uint64_t last_LANdiscovery;
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);

View File

@ -278,7 +278,7 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id)
#define PING_TIMEOUT 5
static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
{
if (peernum < 0 || peernum >= chat->numpeers)
if ((uint32_t)peernum >= chat->numpeers)
return -1;
uint64_t temp_time = unix_time();
@ -296,7 +296,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
{
if (peernum < 0 || peernum >= chat->numpeers)
if ((uint32_t)peernum >= chat->numpeers)
return -1;
sendnodes_data contents;
@ -321,7 +321,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
if (len != sizeof(getnodes_data))
return 1;
if (peernum < 0 || peernum >= chat->numpeers)
if ((uint32_t)peernum >= chat->numpeers)
return 1;
getnodes_data contents;
@ -336,7 +336,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
{
if (peernum < 0 || peernum >= chat->numpeers)
if ((uint32_t)peernum >= chat->numpeers)
return 1;
if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t))
@ -391,6 +391,14 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
if (peernum == -1)
return 1;
uint64_t temp_time = unix_time();
/* Spam prevention (1 message per peer per second limit.)
if (chat->group[peernum].last_recv == temp_time)
return 1;
chat->group[peernum].last_recv = temp_time;
*/
uint32_t message_num;
memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
message_num = ntohl(message_num);
@ -403,14 +411,34 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
chat->group[peernum].last_message_number = message_num;
int handled = 0;
int handled = 1;
uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;
if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64) {
if (chat->group_message != NULL) /* If message is chat message */
(*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - GROUP_DATA_MIN_SIZE,
chat->group_message_userdata);
switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) {
case 0: /* If message is ping */
if (contents_len != 0)
return 1;
chat->group[peernum].last_recv_msgping = temp_time;
case 16: /* If message is new peer */
if (contents_len != crypto_box_PUBLICKEYBYTES)
return 1;
addpeer(chat, contents);
break;
case 64: /* If message is chat message */
if (chat->group_message != NULL)
(*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);
break;
default:
handled = 0;
break;
handled = 1;
}
if (handled == 1) {
@ -465,9 +493,6 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
int peernum = peer_in_chat(chat, public_key);
if (peernum == -1)/*NOTE: This is just for testing and will be removed later.*/
peernum = addpeer(chat, public_key);
if (peernum == -1)
return 1;
@ -493,6 +518,12 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
return send_data(chat, message, length, 64); //TODO: better return values?
}
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
{
addpeer(chat, client_id);
return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values?
}
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
void *userdata)
{

View File

@ -25,7 +25,7 @@
#ifndef GROUP_CHATS_H
#define GROUP_CHATS_H
#include "../../toxcore/net_crypto.h"
#include "net_crypto.h"
#ifdef __cplusplus
extern "C" {
@ -33,9 +33,11 @@ extern "C" {
typedef struct {
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
uint64_t last_recv;
uint64_t pingid;
uint64_t last_pinged;
uint64_t last_recv;
uint64_t last_recv_msgping;
uint32_t last_message_number;
} Group_Peer;
@ -78,6 +80,14 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
*/
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
/*
* Tell everyone about a new peer (a person we are inviting for example.)
*
*/
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id);
/* Create a new group chat.
*
* Returns a new group chat instance if success.

View File

@ -184,7 +184,7 @@ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
/* Get name of friendnumber and put it in name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
*
* return 0 if success.
* return length of name (with the NULL terminator) if success.
* return -1 if failure.
*/
int tox_getname(void *tox, int friendnumber, uint8_t *name)