diff options
author | irungentoo <irungentoo@gmail.com> | 2014-06-19 10:50:46 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-06-19 10:50:46 -0400 |
commit | 3c64c87ea7e3b458714533d1df8e742c5d274966 (patch) | |
tree | e47574b04cae6975a2062281c98fb5bc85efc894 /testing/dns3_test.c | |
parent | db724df189525de3738cdfdd2566c82553e8d88c (diff) |
dns3_test now automatically does the DNS request.
tox_decrypt_dns3_TXT no longer needs id_record to be null terminated.
Diffstat (limited to 'testing/dns3_test.c')
-rw-r--r-- | testing/dns3_test.c | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/testing/dns3_test.c b/testing/dns3_test.c index 69649f50..7052aae7 100644 --- a/testing/dns3_test.c +++ b/testing/dns3_test.c | |||
@@ -2,8 +2,45 @@ | |||
2 | 2 | ||
3 | #include "../toxdns/toxdns.h" | 3 | #include "../toxdns/toxdns.h" |
4 | #include "../toxcore/tox.h" | 4 | #include "../toxcore/tox.h" |
5 | #include "../toxcore/network.h" | ||
5 | #include "misc_tools.c" | 6 | #include "misc_tools.c" |
6 | 7 | ||
8 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
9 | |||
10 | #define c_sleep(x) Sleep(1*x) | ||
11 | |||
12 | #else | ||
13 | #define c_sleep(x) usleep(1000*x) | ||
14 | |||
15 | #endif | ||
16 | |||
17 | uint32_t create_packet(uint8_t *packet, uint8_t *string, uint8_t str_len, uint8_t id) | ||
18 | { | ||
19 | memset(packet, 0, str_len + 13 + 16); | ||
20 | packet[0] = id; | ||
21 | packet[1] = rand(); | ||
22 | packet[5] = 1; | ||
23 | packet[11] = 1; | ||
24 | packet[12] = '.'; | ||
25 | memcpy(packet + 13, string, str_len); | ||
26 | uint32_t i, c = 0; | ||
27 | |||
28 | for (i = str_len + 12; i != 11; --i) { | ||
29 | if (packet[i] == '.') { | ||
30 | packet[i] = c; | ||
31 | c = 0; | ||
32 | } else { | ||
33 | ++c; | ||
34 | } | ||
35 | } | ||
36 | |||
37 | packet[str_len + 13 + 2] = 16; | ||
38 | packet[str_len + 13 + 4] = 1; | ||
39 | packet[str_len + 13 + 7] = 0x29; | ||
40 | packet[str_len + 13 + 8] = 16; | ||
41 | packet[str_len + 13 + 12] = 0x80; | ||
42 | return str_len + 13 + 16; | ||
43 | } | ||
7 | 44 | ||
8 | int main(int argc, char *argv[]) | 45 | int main(int argc, char *argv[]) |
9 | { | 46 | { |
@@ -13,6 +50,22 @@ int main(int argc, char *argv[]) | |||
13 | exit(0); | 50 | exit(0); |
14 | } | 51 | } |
15 | 52 | ||
53 | IP ip = {0}; | ||
54 | ip.family = AF_INET; | ||
55 | sock_t sock = socket(ip.family, SOCK_DGRAM, IPPROTO_UDP); | ||
56 | |||
57 | if (!sock_valid(sock)) | ||
58 | return -1; | ||
59 | |||
60 | if (!addr_resolve_or_parse_ip(argv[1], &ip, 0)) | ||
61 | return -1; | ||
62 | |||
63 | struct sockaddr_in target; | ||
64 | size_t addrsize = sizeof(struct sockaddr_in); | ||
65 | target.sin_family = AF_INET; | ||
66 | target.sin_addr = ip.ip4.in_addr; | ||
67 | target.sin_port = htons(53); | ||
68 | |||
16 | uint8_t string[1024] = {0}; | 69 | uint8_t string[1024] = {0}; |
17 | void *d = tox_dns3_new(hex_string_to_bin(argv[2])); | 70 | void *d = tox_dns3_new(hex_string_to_bin(argv[2])); |
18 | unsigned int i; | 71 | unsigned int i; |
@@ -30,13 +83,24 @@ int main(int argc, char *argv[]) | |||
30 | string[0] = '_'; | 83 | string[0] = '_'; |
31 | memcpy(string + len + 1, "._tox.", sizeof("._tox.")); | 84 | memcpy(string + len + 1, "._tox.", sizeof("._tox.")); |
32 | memcpy((char *)(string + len + 1 + sizeof("._tox.") - 1), argv[1], strlen(argv[1])); | 85 | memcpy((char *)(string + len + 1 + sizeof("._tox.") - 1), argv[1], strlen(argv[1])); |
33 | printf("Do a DNS request and find the TXT record for:\n%s\nThen paste the contents of the data contained in the id field here:\n", | 86 | uint8_t packet[512]; |
34 | string); | 87 | uint8_t id = rand(); |
88 | uint32_t p_len = create_packet(packet, string, strlen((char *)string), id); | ||
89 | |||
90 | if (sendto(sock, (char *) packet, p_len, 0, (struct sockaddr *)&target, addrsize) != p_len) | ||
91 | return -1; | ||
92 | |||
93 | uint8_t buffer[512] = {}; | ||
94 | int r_len = recv(sock, buffer, sizeof(buffer), 0); | ||
95 | |||
96 | if (r_len < (int)p_len) | ||
97 | return -1; | ||
98 | |||
99 | for (i = r_len - 1; i != 0 && buffer[i] != '='; --i); | ||
35 | 100 | ||
36 | scanf("%s", string); | ||
37 | uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE]; | 101 | uint8_t tox_id[TOX_FRIEND_ADDRESS_SIZE]; |
38 | 102 | ||
39 | if (tox_decrypt_dns3_TXT(d, tox_id, string, strlen((char *)string), request_id) != 0) | 103 | if (tox_decrypt_dns3_TXT(d, tox_id, buffer + i + 1, r_len - (i + 1), request_id) != 0) |
40 | return -1; | 104 | return -1; |
41 | 105 | ||
42 | printf("The Tox id for username %s is:\n", argv[3]); | 106 | printf("The Tox id for username %s is:\n", argv[3]); |