forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns.c
205 lines (179 loc) · 4.95 KB
/
dns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "main.h"
static uint32_t parseargument(uint8_t *dest, char_t *src, uint16_t length)
{
_Bool reset = 0, at = 0;
char_t *a = src, *b = src + length;
uint8_t *d = dest;
uint32_t pin = 0;
while(a != b) {
if(*a == ':')
{
a++;
int bits = 32;
while(a != b)
{
uint8_t ch;
if(*a >= 'A' && *a <= 'Z')
{
ch = (*a - 'A');
} else if(*a >= 'a' && *a <= 'z')
{
ch = (*a - 'a' + 26);
} else if(*a >= '0' && *a <= '9')
{
ch = (*a - '0' + 52);
} else if(*a == '+') {
ch = 62;
} else if(*a == '/') {
ch = 63;
} else
{
break;
}
bits -= 6;
if(bits >= 0){pin |= (uint32_t)ch << bits;
} else
{
pin |= (uint32_t)ch >> -bits;
}
a++;
}
break;
}
switch(*a) {
case '0' ... '9':
case 'A' ... 'Z':
case 'a' ... 'z':
case '.': {
if(reset) {
d = dest;
reset = 0;
at = 0;
}
*d++ = *a;
break;
}
case '@': {
memcpy(d, "._tox.", 6);
d += 6;
at = 1;
break;
}
default: {
reset = 1;
break;
}
}
a++;
}
if(!at) {
memcpy(d, "._tox.toxme.se", 14);
d += 14;
}
*d = 0;
debug("Parsed: (%.*s)->(%s) pin=%X\n", length, src, dest, pin);
return pin;
}
static _Bool parserecord(uint8_t *dest, uint8_t *src, uint32_t pin)
{
_Bool _id = 0, _pub = 0;
uint32_t version = 0;
uint8_t *id = dest;
uint8_t *a = src;
while(*a) {
if(_id) {
if(*a >= '0' && *a <= '9') {
if(id == dest + 38) {
debug("id too long\n");
return 0;
}
*id++ = *a - '0';
} else if(*a >= 'A' && *a <= 'F') {
if(id == dest + 38) {
debug("id too long\n");
return 0;
}
*id++ = *a - 'A' + 10;
} else if(*a != ' ') {
if(id != dest + 38) {
debug("id too short\n");
return 0;
}
_id = 0;
}
}
if(_pub) {
if(*a >= '0' && *a <= '9') {
if(id == dest + 32) {
debug("id too long\n");
return 0;
}
*id++ = *a - '0';
} else if(*a >= 'A' && *a <= 'F') {
if(id == dest + 32) {
debug("id too long\n");
return 0;
}
*id++ = *a - 'A' + 10;
} else if(*a != ' ') {
if(id != dest + 32) {
debug("id too short\n");
return 0;
}
_pub = 0;
//writechecksum(dest);
}
}
if(version == 1 && dest == id && memcmp("id=", a, 3) == 0) {
_id = 1;
a += 2;
}
if(version == 2 && dest == id && memcmp("pub=", a, 3) == 0) {
_pub = 1;
a += 3;
}
if(!version && memcmp("v=tox1", a, 6) == 0) {
version = 1;
}
if(!version && memcmp("v=tox2", a, 6) == 0) {
version = 2;
}
a++;
}
if(!version) {
debug("invalid version\n");
return 0;
}
return 1;
}
static void dns_thread(void *data)
{
uint16_t length = *(uint16_t*)data;
uint8_t result[256];
uint32_t pin = parseargument(result, data + 2, length);
_Bool success = 0;
#ifdef __WIN32__
DNS_RECORD *record = NULL;
DnsQuery((char*)result, DNS_TYPE_TEXT, 0, NULL, &record, NULL);
while(record) {
/* just take the first successfully parsed record (for now), and only parse the first string (seems to work) */
DNS_TXT_DATA *txt = &record->Data.Txt;
if(txt->pStringArray[0]) {
debug("Attempting:\n%s\n", txt->pStringArray[0]);
if((success = parserecord(data, (uint8_t*)txt->pStringArray[0], pin))) {
break;
}
}
record = record->pNext;
}
#else
#endif
postmessage(DNS_RESULT, success, 0, data);
}
void dns_request(char_t *name, uint16_t length)
{
void *data = malloc((2 + length < TOX_FRIEND_ADDRESS_SIZE) ? TOX_FRIEND_ADDRESS_SIZE : 2 + length * sizeof(char_t));
memcpy(data, &length, 2);
memcpy(data + 2, name, length * sizeof(char_t));
thread(dns_thread, data);
}