2013-07-10 01:27:47 +08:00
|
|
|
/* Messenger test
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-10 01:27:47 +08:00
|
|
|
* This program adds a friend and accepts all friend requests with the proper message.
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-10 01:27:47 +08:00
|
|
|
* It tries sending a message to the added friend.
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-08-09 19:57:05 +08:00
|
|
|
* If it receives a message from a friend it replies back.
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
|
|
|
*
|
2013-07-13 04:27:19 +08:00
|
|
|
* This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c
|
2013-07-10 01:27:47 +08:00
|
|
|
*
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-18 00:07:19 +08:00
|
|
|
* Command line arguments are the ip, port and public_key of a node (for bootstrapping).
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-18 00:07:19 +08:00
|
|
|
* EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-18 00:07:19 +08:00
|
|
|
* Or the argument can be the path to the save file.
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-18 00:07:19 +08:00
|
|
|
* EX: ./test Save.bak
|
2013-07-26 09:45:56 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-11 11:24:11 +08:00
|
|
|
*
|
2013-07-10 01:27:47 +08:00
|
|
|
*/
|
2013-07-09 08:50:25 +08:00
|
|
|
|
|
|
|
#include "../core/Messenger.h"
|
2013-07-28 00:10:41 +08:00
|
|
|
#include "misc_tools.h"
|
2013-07-09 08:50:25 +08:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
#define c_sleep(x) Sleep(1*x)
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#define c_sleep(x) usleep(1000*x)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
/* FIXME needed as print_request has to match the interface expected by
|
|
|
|
* networking_requesthandler and so cannot take a Messenger * */
|
|
|
|
static Messenger *m;
|
|
|
|
|
2013-08-12 20:23:46 +08:00
|
|
|
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void* userdata)
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
2013-08-09 19:57:05 +08:00
|
|
|
printf("Friend request received from: \n");
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("ClientID: ");
|
|
|
|
uint32_t j;
|
|
|
|
for(j = 0; j < 32; j++)
|
|
|
|
{
|
|
|
|
if(public_key[j] < 16)
|
|
|
|
printf("0");
|
|
|
|
printf("%hhX", public_key[j]);
|
|
|
|
}
|
|
|
|
printf("\nOf length: %u with data: %s \n", length, data);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-07-11 05:31:58 +08:00
|
|
|
if(length != sizeof("Install Gentoo"))
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-07-10 11:16:59 +08:00
|
|
|
if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
|
2013-07-10 01:20:48 +08:00
|
|
|
//if the request contained the message of peace the person is obviously a friend so we add him.
|
|
|
|
{
|
|
|
|
printf("Friend request accepted.\n");
|
2013-08-11 11:24:11 +08:00
|
|
|
m_addfriend_norequest(m, public_key);
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2013-08-12 20:23:46 +08:00
|
|
|
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
2013-08-09 19:57:05 +08:00
|
|
|
printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
|
2013-08-11 11:24:11 +08:00
|
|
|
m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-07-18 00:07:19 +08:00
|
|
|
if (argc < 4 && argc != 2) {
|
|
|
|
printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
|
2013-07-09 08:50:25 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
|
|
|
m = initMessenger();
|
|
|
|
if( !m ){
|
|
|
|
fputs("Failed to allocate messenger datastructure\n", stderr);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
if(argc > 3) {
|
2013-07-18 00:07:19 +08:00
|
|
|
IP_Port bootstrap_ip_port;
|
|
|
|
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
|
|
|
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
|
|
|
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
2013-07-26 16:02:17 +08:00
|
|
|
} else {
|
2013-07-18 00:07:19 +08:00
|
|
|
FILE *file = fopen(argv[1], "rb");
|
|
|
|
if ( file==NULL ){return 1;}
|
|
|
|
int read;
|
|
|
|
uint8_t buffer[128000];
|
|
|
|
read = fread(buffer, 1, 128000, file);
|
2013-08-11 11:24:11 +08:00
|
|
|
printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read));
|
2013-07-18 00:07:19 +08:00
|
|
|
fclose(file);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
}
|
2013-08-12 20:23:46 +08:00
|
|
|
m_callback_friendrequest(m, print_request, NULL);
|
|
|
|
m_callback_friendmessage(m, print_message, NULL);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("OUR ID: ");
|
|
|
|
uint32_t i;
|
2013-07-26 16:02:17 +08:00
|
|
|
for(i = 0; i < 32; i++) {
|
2013-07-09 08:50:25 +08:00
|
|
|
if(self_public_key[i] < 16)
|
|
|
|
printf("0");
|
|
|
|
printf("%hhX",self_public_key[i]);
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
|
|
|
setname(m, (uint8_t *)"Anon", 5);
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
char temp_id[128];
|
|
|
|
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
|
2013-07-26 16:02:17 +08:00
|
|
|
if(scanf("%s", temp_id) != 1) {
|
2013-07-13 04:27:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
perror("Initialization");
|
2013-07-18 00:07:19 +08:00
|
|
|
|
2013-07-26 16:02:17 +08:00
|
|
|
while(1) {
|
2013-07-18 23:47:27 +08:00
|
|
|
uint8_t name[128];
|
2013-08-11 11:24:11 +08:00
|
|
|
getname(m, num, name);
|
2013-07-18 23:47:27 +08:00
|
|
|
printf("%s\n", name);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
|
|
|
m_sendmessage(m, num, (uint8_t*)"Test", 5);
|
|
|
|
doMessenger(m);
|
2013-07-10 01:20:48 +08:00
|
|
|
c_sleep(30);
|
2013-07-18 00:07:19 +08:00
|
|
|
FILE *file = fopen("Save.bak", "wb");
|
|
|
|
if ( file==NULL ){return 1;}
|
2013-08-11 11:24:11 +08:00
|
|
|
uint8_t * buffer = malloc(Messenger_size(m));
|
|
|
|
Messenger_save(m, buffer);
|
|
|
|
size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
|
|
|
|
if (write_result < Messenger_size(m)) {return 1;}
|
2013-07-18 00:07:19 +08:00
|
|
|
free(buffer);
|
|
|
|
fclose(file);
|
2013-08-11 11:24:11 +08:00
|
|
|
}
|
|
|
|
cleanupMessenger(m);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|