2014-06-19 22:50:46 +08:00
|
|
|
#include "../toxcore/network.h"
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "../toxdns/toxdns.h"
|
2014-06-14 23:31:56 +08:00
|
|
|
#include "misc_tools.c"
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static uint32_t create_packet(uint8_t *packet, uint8_t *string, uint8_t str_len, uint8_t id)
|
2014-06-19 22:50:46 +08:00
|
|
|
{
|
|
|
|
memset(packet, 0, str_len + 13 + 16);
|
|
|
|
packet[0] = id;
|
|
|
|
packet[1] = rand();
|
|
|
|
packet[5] = 1;
|
|
|
|
packet[11] = 1;
|
|
|
|
packet[12] = '.';
|
|
|
|
memcpy(packet + 13, string, str_len);
|
|
|
|
uint32_t i, c = 0;
|
|
|
|
|
|
|
|
for (i = str_len + 12; i != 11; --i) {
|
|
|
|
if (packet[i] == '.') {
|
|
|
|
packet[i] = c;
|
|
|
|
c = 0;
|
|
|
|
} else {
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
packet[str_len + 13 + 2] = 16;
|
|
|
|
packet[str_len + 13 + 4] = 1;
|
|
|
|
packet[str_len + 13 + 7] = 0x29;
|
|
|
|
packet[str_len + 13 + 8] = 16;
|
|
|
|
packet[str_len + 13 + 12] = 0x80;
|
|
|
|
return str_len + 13 + 16;
|
|
|
|
}
|
2014-06-14 23:31:56 +08:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 4) {
|
|
|
|
printf("Usage: %s domain domain_public_key queried_username\nEX: %s utox.org D3154F65D28A5B41A05D4AC7E4B39C6B1C233CC857FB365C56E8392737462A12 username\n",
|
|
|
|
argv[0], argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-06-19 22:50:46 +08:00
|
|
|
IP ip = {0};
|
|
|
|
ip.family = AF_INET;
|
2017-01-07 16:28:53 +08:00
|
|
|
Socket sock = net_socket(ip.family, TOX_SOCK_DGRAM, TOX_PROTO_UDP);
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!sock_valid(sock)) {
|
2014-06-19 22:50:46 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!addr_resolve_or_parse_ip(argv[1], &ip, 0)) {
|
2014-06-19 22:50:46 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2017-01-07 16:28:53 +08:00
|
|
|
uint32_t request_id;
|
2014-06-14 23:31:56 +08:00
|
|
|
uint8_t string[1024] = {0};
|
|
|
|
void *d = tox_dns3_new(hex_string_to_bin(argv[2]));
|
|
|
|
int len = tox_generate_dns3_string(d, string + 1, sizeof(string) - 1, &request_id, (uint8_t *)argv[3], strlen(argv[3]));
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (len == -1) {
|
2014-06-14 23:31:56 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-14 23:31:56 +08:00
|
|
|
|
|
|
|
string[0] = '_';
|
|
|
|
memcpy(string + len + 1, "._tox.", sizeof("._tox."));
|
|
|
|
memcpy((char *)(string + len + 1 + sizeof("._tox.") - 1), argv[1], strlen(argv[1]));
|
2014-06-19 22:50:46 +08:00
|
|
|
uint8_t packet[512];
|
|
|
|
uint8_t id = rand();
|
|
|
|
uint32_t p_len = create_packet(packet, string, strlen((char *)string), id);
|
|
|
|
|
2017-01-07 16:28:53 +08:00
|
|
|
IP_Port ip_port;
|
|
|
|
ip_port.port = 53;
|
|
|
|
ip_port.ip = ip;
|
|
|
|
|
|
|
|
if (net_sendto_ip4(sock, (char *)packet, p_len, ip_port) != p_len) {
|
2014-06-19 22:50:46 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2016-09-05 21:43:58 +08:00
|
|
|
uint8_t buffer[512] = {0};
|
2016-11-02 22:34:33 +08:00
|
|
|
int r_len = recv(sock, (char *)buffer, sizeof(buffer), 0);
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (r_len < (int)p_len) {
|
2014-06-19 22:50:46 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-19 22:50:46 +08:00
|
|
|
|
2017-01-07 16:28:53 +08:00
|
|
|
unsigned int i;
|
2017-03-04 14:02:26 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) {
|
|
|
|
;
|
|
|
|
}
|
2014-06-14 23:31:56 +08:00
|
|
|
|
2015-02-25 09:29:01 +08:00
|
|
|
uint8_t tox_id[TOX_ADDRESS_SIZE];
|
2014-06-14 23:31:56 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (tox_decrypt_dns3_TXT(d, tox_id, buffer + i + 1, r_len - (i + 1), request_id) != 0) {
|
2014-06-14 23:31:56 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-06-14 23:31:56 +08:00
|
|
|
|
|
|
|
printf("The Tox id for username %s is:\n", argv[3]);
|
|
|
|
|
|
|
|
//unsigned int i;
|
2015-02-25 09:29:01 +08:00
|
|
|
for (i = 0; i < TOX_ADDRESS_SIZE; ++i) {
|
2014-06-14 23:31:56 +08:00
|
|
|
printf("%02hhX", tox_id[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|