diff options
Diffstat (limited to 'toxdns')
-rw-r--r-- | toxdns/toxdns.c | 33 | ||||
-rw-r--r-- | toxdns/toxdns.h | 24 |
2 files changed, 36 insertions, 21 deletions
diff --git a/toxdns/toxdns.c b/toxdns/toxdns.c index 20a4486c..31269c15 100644 --- a/toxdns/toxdns.c +++ b/toxdns/toxdns.c | |||
@@ -84,21 +84,25 @@ void tox_dns3_kill(void *dns3_object) | |||
84 | free(dns3_object); | 84 | free(dns3_object); |
85 | } | 85 | } |
86 | 86 | ||
87 | /* Generate a dns3 string of string_max_len used to query the dns server reffered to by to | 87 | /* Generate a dns3 string of string_max_len used to query the dns server referred to by to |
88 | * dns3_object for a tox id registered to user with name of name_len. | 88 | * dns3_object for a tox id registered to user with name of name_len. |
89 | * | 89 | * |
90 | * the uint32_t pointed by request_id will be set to the request id which must be passed to | ||
91 | * tox_decrypt_dns3_TXT() to correctly decode the response. | ||
92 | * | ||
90 | * This is what the string returned looks like: | 93 | * This is what the string returned looks like: |
91 | * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc | 94 | * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc |
92 | * | 95 | * |
93 | * returns length of string on sucess. | 96 | * returns length of string on sucess. |
94 | * returns -1 on failure. | 97 | * returns -1 on failure. |
95 | */ | 98 | */ |
96 | int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint8_t *name, | 99 | int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id, |
97 | uint8_t name_len) | 100 | uint8_t *name, uint8_t name_len) |
98 | { | 101 | { |
99 | #define DOT_INTERVAL (6 * 5) | 102 | #define DOT_INTERVAL (6 * 5) |
100 | int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES); | 103 | int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES); |
101 | int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5); | 104 | int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5); |
105 | end_len -= !(base % DOT_INTERVAL); | ||
102 | 106 | ||
103 | if (end_len > string_max_len) | 107 | if (end_len > string_max_len) |
104 | return -1; | 108 | return -1; |
@@ -121,14 +125,16 @@ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string | |||
121 | uint8_t bits = 0; | 125 | uint8_t bits = 0; |
122 | int i; | 126 | int i; |
123 | 127 | ||
124 | for (i = 0; i < (total_len / DOT_INTERVAL); ++i) { | 128 | for (i = !(total_len % DOT_INTERVAL); i < (total_len / DOT_INTERVAL); ++i) { |
125 | _encode(string, buff, DOT_INTERVAL); | 129 | _encode(string, buff, DOT_INTERVAL); |
126 | *string = '.'; | 130 | *string = '.'; |
127 | ++string; | 131 | ++string; |
128 | } | 132 | } |
129 | 133 | ||
130 | _encode(string, buff, total_len % DOT_INTERVAL); | 134 | int left = total_len - (buff - buffer); |
135 | _encode(string, buff, left); | ||
131 | #undef DOT_INTERVAL | 136 | #undef DOT_INTERVAL |
137 | *request_id = d->nonce; | ||
132 | ++d->nonce; | 138 | ++d->nonce; |
133 | 139 | ||
134 | if (d->nonce == d->nonce_start) { | 140 | if (d->nonce == d->nonce_start) { |
@@ -136,7 +142,7 @@ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string | |||
136 | } | 142 | } |
137 | 143 | ||
138 | if (end_len != string - old_str) { | 144 | if (end_len != string - old_str) { |
139 | printf("tox_generate_dns3_string Fail\n"); | 145 | printf("tox_generate_dns3_string Fail, %u != %u\n", end_len, string - old_str); |
140 | return -1; | 146 | return -1; |
141 | } | 147 | } |
142 | 148 | ||
@@ -189,18 +195,21 @@ static int decode(uint8_t *dest, uint8_t *src) | |||
189 | /* Decode and decrypt the id_record returned of length id_record_len into | 195 | /* Decode and decrypt the id_record returned of length id_record_len into |
190 | * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE). | 196 | * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE). |
191 | * | 197 | * |
198 | * request_id is the request id given by tox_generate_dns3_string() when creating the request. | ||
199 | * | ||
192 | * the id_record passed to this function should look somewhat like this: | 200 | * the id_record passed to this function should look somewhat like this: |
193 | * 4haaaa2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp | 201 | * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp |
194 | * | 202 | * |
195 | * returns -1 on failure. | 203 | * returns -1 on failure. |
196 | * returns 0 on success. | 204 | * returns 0 on success. |
197 | * | 205 | * |
198 | */ | 206 | */ |
199 | int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len) | 207 | int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len, |
208 | uint32_t request_id) | ||
200 | { | 209 | { |
201 | DNS_Object *d = dns3_object; | 210 | DNS_Object *d = dns3_object; |
202 | 211 | ||
203 | if (id_record_len != 93) | 212 | if (id_record_len != 87) |
204 | return -1; | 213 | return -1; |
205 | 214 | ||
206 | /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES)) | 215 | /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES)) |
@@ -213,12 +222,12 @@ int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, | |||
213 | return -1; | 222 | return -1; |
214 | 223 | ||
215 | uint8_t nonce[crypto_box_NONCEBYTES] = {0}; | 224 | uint8_t nonce[crypto_box_NONCEBYTES] = {0}; |
216 | memcpy(nonce, data, sizeof(uint32_t)); | 225 | memcpy(nonce, &request_id, sizeof(uint32_t)); |
217 | nonce[sizeof(uint32_t)] = 1; | 226 | nonce[sizeof(uint32_t)] = 1; |
218 | int len = decrypt_data_symmetric(d->shared_key, nonce, data + sizeof(uint32_t), length - sizeof(uint32_t), tox_id); | 227 | int len = decrypt_data_symmetric(d->shared_key, nonce, data, length, tox_id); |
219 | 228 | ||
220 | if (len != FRIEND_ADDRESS_SIZE) | 229 | if (len != FRIEND_ADDRESS_SIZE) |
221 | return -1; | 230 | return -1; |
222 | 231 | ||
223 | return 0; | 232 | return 0; |
224 | } \ No newline at end of file | 233 | } |
diff --git a/toxdns/toxdns.h b/toxdns/toxdns.h index ac84af9c..173c8b2f 100644 --- a/toxdns/toxdns.h +++ b/toxdns/toxdns.h | |||
@@ -2,7 +2,7 @@ | |||
2 | * | 2 | * |
3 | * Tox secure username DNS toxid resolving functions. | 3 | * Tox secure username DNS toxid resolving functions. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2014 Tox project All Rights Reserved. |
6 | * | 6 | * |
7 | * This file is part of Tox. | 7 | * This file is part of Tox. |
8 | * | 8 | * |
@@ -33,10 +33,10 @@ | |||
33 | * and handle responses for that server. | 33 | * and handle responses for that server. |
34 | * 3. Use tox_generate_dns3_string() to generate a string based on the name we want to query. | 34 | * 3. Use tox_generate_dns3_string() to generate a string based on the name we want to query. |
35 | * 4. take the string and use it for your DNS request like this: | 35 | * 4. take the string and use it for your DNS request like this: |
36 | * _4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc_._tox.utox.org | 36 | * _4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc._tox.utox.org |
37 | * | 37 | * |
38 | * 5. The TXT in the DNS you recieve should look like this: | 38 | * 5. The TXT in the DNS you receive should look like this: |
39 | * v=tox3;id=4haaaa2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp | 39 | * v=tox3;id=2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp |
40 | * 6. Take the id string and use it with tox_decrypt_dns3_TXT() to get the Tox id returned by the DNS server. | 40 | * 6. Take the id string and use it with tox_decrypt_dns3_TXT() to get the Tox id returned by the DNS server. |
41 | */ | 41 | */ |
42 | 42 | ||
@@ -51,28 +51,34 @@ void *tox_dns3_new(uint8_t *server_public_key); | |||
51 | */ | 51 | */ |
52 | void tox_dns3_kill(void *dns3_object); | 52 | void tox_dns3_kill(void *dns3_object); |
53 | 53 | ||
54 | /* Generate a dns3 string of string_max_len used to query the dns server reffered to by to | 54 | /* Generate a dns3 string of string_max_len used to query the dns server referred to by to |
55 | * dns3_object for a tox id registered to user with name of name_len. | 55 | * dns3_object for a tox id registered to user with name of name_len. |
56 | * | 56 | * |
57 | * the uint32_t pointed by request_id will be set to the request id which must be passed to | ||
58 | * tox_decrypt_dns3_TXT() to correctly decode the response. | ||
59 | * | ||
57 | * This is what the string returned looks like: | 60 | * This is what the string returned looks like: |
58 | * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc | 61 | * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc |
59 | * | 62 | * |
60 | * returns length of string on sucess. | 63 | * returns length of string on sucess. |
61 | * returns -1 on failure. | 64 | * returns -1 on failure. |
62 | */ | 65 | */ |
63 | int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint8_t *name, | 66 | int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id, |
64 | uint8_t name_len); | 67 | uint8_t *name, uint8_t name_len); |
65 | 68 | ||
66 | /* Decode and decrypt the id_record returned of length id_record_len into | 69 | /* Decode and decrypt the id_record returned of length id_record_len into |
67 | * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE). | 70 | * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE). |
68 | * | 71 | * |
72 | * request_id is the request id given by tox_generate_dns3_string() when creating the request. | ||
73 | * | ||
69 | * the id_record passed to this function should look somewhat like this: | 74 | * the id_record passed to this function should look somewhat like this: |
70 | * 4haaaa2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp | 75 | * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp |
71 | * | 76 | * |
72 | * returns -1 on failure. | 77 | * returns -1 on failure. |
73 | * returns 0 on success. | 78 | * returns 0 on success. |
74 | * | 79 | * |
75 | */ | 80 | */ |
76 | int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len); | 81 | int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len, |
82 | uint32_t request_id); | ||
77 | 83 | ||
78 | #endif | 84 | #endif |