Messaging now works in group chats.

This commit is contained in:
irungentoo 2013-09-08 20:48:52 -04:00
parent 2e33ffeb8c
commit 0d41d7f9c0
3 changed files with 139 additions and 6 deletions

View File

@ -110,6 +110,9 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id)
uint32_t i, j = 0;
uint64_t temp_time = unix_time();
if (memcmp(chat->self_public_key, client_id, crypto_box_PUBLICKEYBYTES) == 0)
return -1;
for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) {
++j;
@ -201,7 +204,7 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length,
uint64_t temp_time = unix_time();
for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv > temp_time + BAD_NODE_TIMEOUT) {
if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) {
if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0)
++sent;
}
@ -392,7 +395,9 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
message_num = ntohl(message_num);
if (message_num - chat->group[peernum].last_message_number > 64 ||
if (chat->group[peernum].last_message_number == 0) {
chat->group[peernum].last_message_number = message_num;
} else if (message_num - chat->group[peernum].last_message_number > 64 ||
message_num == chat->group[peernum].last_message_number)
return 1;
@ -400,9 +405,11 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
int handled = 0;
if (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] == 64
&& chat->group_message != NULL) { /* If message is chat message */
(*chat->group_message)(chat, peernum, data + GROUP_DATA_MIN_SIZE, len - 1, chat->group_message_userdata);
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);
handled = 1;
}
@ -420,10 +427,16 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t
return 1;
uint8_t packet[MAX_DATA_SIZE];
++chat->message_number;
if (chat->message_number == 0)
chat->message_number = 1;
uint32_t message_num = htonl(chat->message_number);
//TODO
memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES);
memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num));
memcpy(packet + GROUP_DATA_MIN_SIZE, data, len);
packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id;
return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50);
}

View File

@ -54,6 +54,11 @@ void print_group(Group_Chat *chat)
}
}
void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
{
printf("%u: %s | %u\n", peer_number, message, length);
}
int main()
{
IP ip;
@ -68,6 +73,7 @@ int main()
exit(1);
networking_registerhandler(chats[i]->net, 48, &handle_groupchatpacket, chats[i]);
callback_groupmessage(chats[i], &print_message, 0);
}
printf("ok\n");

View File

@ -0,0 +1,114 @@
#include "group_chats.h"
#define NUM_CHATS 8
#ifdef WIN32
#define c_sleep(x) Sleep(1*x)
#else
#define c_sleep(x) usleep(1000*x)
#endif
Group_Chat *chat;
void print_close(Group_Close *close)
{
uint32_t i, j;
IP_Port p_ip;
printf("___________________CLOSE________________________________\n");
for (i = 0; i < GROUP_CLOSE_CONNECTIONS; i++) {
printf("ClientID: ");
for (j = 0; j < CLIENT_ID_SIZE; j++) {
printf("%02hhX", close[i].client_id[j]);
}
p_ip = close[i].ip_port;
printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
ntohs(p_ip.port));
printf("\nTimestamp: %llu", (long long unsigned int) close[i].last_recv);
printf("\n");
}
}
void print_group(Group_Chat *chat)
{
uint32_t i, j;
printf("-----------------\nClientID: ");
for (j = 0; j < CLIENT_ID_SIZE; j++) {
printf("%02hhX", chat->self_public_key[j]);
}
printf("\n___________________GROUP________________________________\n");
for (i = 0; i < chat->numpeers; i++) {
printf("ClientID: ");
for (j = 0; j < CLIENT_ID_SIZE; j++) {
printf("%02hhX", chat->group[i].client_id[j]);
}
printf("\nTimestamp: %llu", (long long unsigned int) chat->group[i].last_recv);
printf("\nlast_pinged: %llu", (long long unsigned int) chat->group[i].last_pinged);
printf("\npingid: %llu", (long long unsigned int) chat->group[i].pingid);
printf("\n");
}
}
unsigned char *hex_string_to_bin(char hex_string[])
{
size_t len = strlen(hex_string);
unsigned char *val = malloc(len);
char *pos = hex_string;
int i;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
{
printf("%u: %s | %u\n", peer_number, message, length);
}
int main(int argc, char *argv[])
{
IP ip;
ip.uint32 = 0;
uint32_t i;
chat = new_groupchat(new_networking(ip, 12745));
if (chat == 0)
exit(1);
networking_registerhandler(chat->net, 48, &handle_groupchatpacket, chat);
callback_groupmessage(chat, &print_message, 0);
printf("ok\n");
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
/* bootstrap_ip_port.ip.c[0] = 127;
* bootstrap_ip_port.ip.c[1] = 0;
* bootstrap_ip_port.ip.c[2] = 0;
* bootstrap_ip_port.ip.c[3] = 1; */
bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]);
chat_bootstrap(chat, bootstrap_ip_port, hex_string_to_bin(argv[3]));
while (1) {
networking_poll(chat->net);
do_groupchat(chat);
printf("%u ", chat->numpeers);
printf("%u\n", group_sendmessage(chat, "Install Gentoo", sizeof("Install Gentoo")));
//print_close(chat->close);
// print_group(chat);
c_sleep(100);
}
return 0;
}