2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
|
|
|
* Copyright © 2013 Tox project.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
*
|
|
|
|
*
|
2023-12-25 08:28:36 +08:00
|
|
|
* This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c Messenger_test.c -lsodium
|
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
|
2017-01-14 23:46:31 +08:00
|
|
|
*/
|
2022-01-13 03:07:34 +08:00
|
|
|
#include <stdint.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-07-09 08:50:25 +08:00
|
|
|
|
2017-03-04 02:30:11 +08:00
|
|
|
#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
|
2013-07-09 08:50:25 +08:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../toxcore/Messenger.h"
|
2022-03-04 04:15:09 +08:00
|
|
|
#include "../toxcore/ccompat.h"
|
2018-08-10 07:53:39 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "misc_tools.h"
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length,
|
|
|
|
void *userdata)
|
2013-09-27 17:24:33 +08:00
|
|
|
{
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("Message with length %u received from %u: %s \n", (unsigned)length, friendnumber, string);
|
2018-08-12 19:35:01 +08:00
|
|
|
m_send_message_generic(m, friendnumber, type, (const uint8_t *)"Test1", 6, nullptr);
|
2013-09-27 17:24:33 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 23:48:35 +08:00
|
|
|
/* TODO(irungentoo): needed as print_request has to match the interface expected by
|
2013-08-11 11:24:11 +08:00
|
|
|
* networking_requesthandler and so cannot take a Messenger * */
|
|
|
|
static Messenger *m;
|
|
|
|
|
2016-09-30 22:15:50 +08:00
|
|
|
static void print_request(Messenger *m2, const uint8_t *public_key, const uint8_t *data, size_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;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (j = 0; j < 32; j++) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (public_key[j] < 16) {
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("0");
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("%hhX", public_key[j]);
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("\nOf length: %u with data: %s \n", (unsigned)length, data);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if (length != sizeof("Install Gentoo")) {
|
2013-07-10 01:20:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-01-10 21:39:07 +08:00
|
|
|
if (memcmp(data, "Install Gentoo", sizeof("Install Gentoo")) == 0)
|
2013-08-17 01:11:09 +08:00
|
|
|
//if the request contained the message of peace the person is obviously a friend so we add him.
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
|
|
|
printf("Friend request accepted.\n");
|
2016-09-30 22:15:50 +08:00
|
|
|
m_addfriend_norequest(m2, public_key);
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-09-11 04:59:33 +08:00
|
|
|
/* let user override default by cmdline */
|
2018-10-09 09:00:19 +08:00
|
|
|
bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
2013-09-11 06:14:20 +08:00
|
|
|
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (argvoffset < 0) {
|
2013-09-11 06:14:20 +08:00
|
|
|
exit(1);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-11 04:59:33 +08:00
|
|
|
|
|
|
|
/* with optional --ipvx, now it can be 1-4 arguments... */
|
2018-09-10 05:21:20 +08:00
|
|
|
if (argc != argvoffset + 4) {
|
2013-09-11 04:59:33 +08:00
|
|
|
printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
|
2013-07-09 08:50:25 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2024-01-25 20:54:59 +08:00
|
|
|
const Memory *mem = os_memory();
|
2023-08-23 18:26:11 +08:00
|
|
|
Mono_Time *const mono_time = mono_time_new(mem, nullptr, nullptr);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
|
|
|
if (mono_time == nullptr) {
|
|
|
|
fputs("Failed to allocate monotonic timer datastructure\n", stderr);
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-08-10 07:53:39 +08:00
|
|
|
|
2014-08-15 03:27:34 +08:00
|
|
|
Messenger_Options options = {0};
|
|
|
|
options.ipv6enabled = ipv6enabled;
|
2022-02-18 06:03:32 +08:00
|
|
|
Messenger_Error err;
|
2024-01-25 20:54:59 +08:00
|
|
|
m = new_messenger(mono_time, mem, os_random(), os_network(), &options, &err);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
if (!m) {
|
2022-02-09 03:15:37 +08:00
|
|
|
fprintf(stderr, "Failed to allocate messenger datastructure: %d\n", err);
|
2013-08-11 11:24:11 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-09-11 04:59:33 +08:00
|
|
|
if (argc == argvoffset + 4) {
|
2022-01-13 03:07:34 +08:00
|
|
|
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
|
|
|
|
|
|
|
|
if (port_conv <= 0 || port_conv > UINT16_MAX) {
|
|
|
|
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint16_t port = net_htons((uint16_t)port_conv);
|
2013-09-11 04:59:33 +08:00
|
|
|
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
|
2018-07-05 18:31:29 +08:00
|
|
|
int res = dht_bootstrap_from_address(m->dht, argv[argvoffset + 1],
|
2013-09-15 00:42:17 +08:00
|
|
|
ipv6enabled, port, bootstrap_key);
|
2013-09-11 04:59:33 +08:00
|
|
|
free(bootstrap_key);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-11 04:59:33 +08:00
|
|
|
if (!res) {
|
|
|
|
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-18 00:07:19 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2016-08-23 05:44:58 +08:00
|
|
|
m_callback_friendrequest(m, print_request);
|
2016-08-20 02:01:58 +08:00
|
|
|
m_callback_friendmessage(m, print_message);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("OUR ID: ");
|
|
|
|
uint32_t i;
|
2013-08-13 23:50:33 +08:00
|
|
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
|
|
|
getaddress(m, address);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (address[i] < 16) {
|
2013-07-09 08:50:25 +08:00
|
|
|
printf("0");
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
printf("%hhX", address[i]);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
setname(m, (const uint8_t *)"Anon", 5);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2014-02-09 13:59:03 +08:00
|
|
|
char temp_hex_id[128];
|
2013-08-13 23:50:33 +08:00
|
|
|
printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!fgets(temp_hex_id, sizeof(temp_hex_id), stdin)) {
|
2014-02-26 00:48:27 +08:00
|
|
|
exit(0);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-02-26 00:48:27 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if ((strlen(temp_hex_id) > 0) && (temp_hex_id[strlen(temp_hex_id) - 1] == '\n')) {
|
2014-02-26 00:48:27 +08:00
|
|
|
temp_hex_id[strlen(temp_hex_id) - 1] = '\0';
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-02-09 22:43:16 +08:00
|
|
|
|
2014-02-09 13:59:03 +08:00
|
|
|
uint8_t *bin_id = hex_string_to_bin(temp_hex_id);
|
2018-08-16 06:09:46 +08:00
|
|
|
const int num = m_addfriend(m, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
2014-02-09 13:59:03 +08:00
|
|
|
free(bin_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
perror("Initialization");
|
2013-07-18 00:07:19 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
while (1) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2018-08-10 07:53:39 +08:00
|
|
|
|
2013-07-18 23:47:27 +08:00
|
|
|
uint8_t name[128];
|
2018-08-16 06:09:46 +08:00
|
|
|
const char *const filename = "Save.bak";
|
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
|
|
|
|
2018-08-12 19:35:01 +08:00
|
|
|
m_send_message_generic(m, num, MESSAGE_NORMAL, (const uint8_t *)"Test", 5, nullptr);
|
2018-01-29 05:30:39 +08:00
|
|
|
do_messenger(m, nullptr);
|
2013-07-10 01:20:48 +08:00
|
|
|
c_sleep(30);
|
2018-08-16 06:09:46 +08:00
|
|
|
FILE *file = fopen(filename, "wb");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (file == nullptr) {
|
2018-08-16 06:09:46 +08:00
|
|
|
printf("Failed to open file %s\n", filename);
|
2018-02-23 10:22:38 +08:00
|
|
|
kill_messenger(m);
|
2013-08-17 01:11:09 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
uint8_t *buffer = (uint8_t *)malloc(messenger_size(m));
|
2018-08-16 06:09:46 +08:00
|
|
|
|
|
|
|
if (buffer == nullptr) {
|
|
|
|
fputs("Failed to allocate memory\n", stderr);
|
|
|
|
fclose(file);
|
|
|
|
kill_messenger(m);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-24 02:49:59 +08:00
|
|
|
messenger_save(m, buffer);
|
2018-08-16 06:09:46 +08:00
|
|
|
const size_t write_result = fwrite(buffer, 1, messenger_size(m), file);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-10-24 02:49:59 +08:00
|
|
|
if (write_result < messenger_size(m)) {
|
2018-08-16 06:09:46 +08:00
|
|
|
free(buffer);
|
|
|
|
fclose(file);
|
2018-02-23 10:22:38 +08:00
|
|
|
kill_messenger(m);
|
2013-08-17 01:11:09 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
free(buffer);
|
|
|
|
fclose(file);
|
2013-08-11 11:24:11 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|