| 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 |
#include "asterisk.h" |
| 26 |
#include "asterisk/dns_core.h" |
| 27 |
#include "asterisk/dns_test.h" |
| 28 |
#include "asterisk/utils.h" |
| 29 |
|
| 30 |
#ifdef TEST_FRAMEWORK |
| 31 |
|
| 32 |
const char DNS_HEADER[] = { |
| 33 |
|
| 34 |
0x00, 0x00, |
| 35 |
|
| 36 |
0x85, |
| 37 |
|
| 38 |
0x80, |
| 39 |
|
| 40 |
0x00, 0x01, |
| 41 |
|
| 42 |
0x00, 0x00, |
| 43 |
|
| 44 |
0x00, 0x00, |
| 45 |
|
| 46 |
0x00, 0x00, |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static int generate_dns_header(unsigned short num_records, char *buf) |
| 65 |
{ |
| 66 |
unsigned short net_num_records = htons(num_records); |
| 67 |
|
| 68 |
memcpy(buf, DNS_HEADER, ARRAY_LEN(DNS_HEADER)); |
| 69 |
|
| 70 |
memcpy(&buf[6], &net_num_records, sizeof(num_records)); |
| 71 |
|
| 72 |
return ARRAY_LEN(DNS_HEADER); |
| 73 |
} |
| 74 |
|
| 75 |
const char DNS_QUESTION [] = { |
| 76 |
|
| 77 |
0x05, 0x67, 0x6f, 0x6f, 0x73, 0x65, |
| 78 |
|
| 79 |
0x08, 0x66, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x73, |
| 80 |
|
| 81 |
0x00, |
| 82 |
|
| 83 |
0x00, 0x23, |
| 84 |
|
| 85 |
0x00, 0x01, |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static int generate_dns_question(char *buf) |
| 103 |
{ |
| 104 |
memcpy(buf, DNS_QUESTION, ARRAY_LEN(DNS_QUESTION)); |
| 105 |
return ARRAY_LEN(DNS_QUESTION); |
| 106 |
} |
| 107 |
|
| 108 |
const char NAPTR_ANSWER [] = { |
| 109 |
|
| 110 |
0xc0, 0x0c, |
| 111 |
|
| 112 |
0x00, 0x23, |
| 113 |
|
| 114 |
0x00, 0x01, |
| 115 |
|
| 116 |
0x00, 0x00, 0x30, 0x39, |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static int generate_dns_answer(int ttl, char *buf) |
| 137 |
{ |
| 138 |
int net_ttl = htonl(ttl); |
| 139 |
|
| 140 |
memcpy(buf, NAPTR_ANSWER, ARRAY_LEN(NAPTR_ANSWER)); |
| 141 |
|
| 142 |
if (ttl) { |
| 143 |
memcpy(&buf[6], &net_ttl, sizeof(int)); |
| 144 |
} |
| 145 |
|
| 146 |
return ARRAY_LEN(NAPTR_ANSWER); |
| 147 |
} |
| 148 |
|
| 149 |
int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf) |
| 150 |
{ |
| 151 |
uint8_t len = string->len; |
| 152 |
size_t actual_len = strlen(string->val); |
| 153 |
buf[0] = len; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if (actual_len) { |
| 160 |
memcpy(&buf[1], string->val, strlen(string->val)); |
| 161 |
} |
| 162 |
|
| 163 |
return actual_len + 1; |
| 164 |
} |
| 165 |
|
| 166 |
int ast_dns_test_write_domain(const char *string, char *buf) |
| 167 |
{ |
| 168 |
char *copy = ast_strdupa(string); |
| 169 |
char *part; |
| 170 |
char *ptr = buf; |
| 171 |
static const struct ast_dns_test_string null_label = { |
| 172 |
.len = 0, |
| 173 |
.val = "", |
| 174 |
}; |
| 175 |
|
| 176 |
while (1) { |
| 177 |
struct ast_dns_test_string dns_str; |
| 178 |
part = strsep(©, "."); |
| 179 |
if (ast_strlen_zero(part)) { |
| 180 |
break; |
| 181 |
} |
| 182 |
dns_str.len = strlen(part); |
| 183 |
dns_str.val = part; |
| 184 |
|
| 185 |
ptr += ast_dns_test_write_string(&dns_str, ptr); |
| 186 |
} |
| 187 |
ptr += ast_dns_test_write_string(&null_label, ptr); |
| 188 |
|
| 189 |
return ptr - buf; |
| 190 |
} |
| 191 |
|
| 192 |
int ast_dns_test_generate_result(struct ast_dns_query *query, void *records, size_t num_records, |
| 193 |
size_t record_size, record_fn generate, char *buffer) |
| 194 |
{ |
| 195 |
char *ptr = buffer; |
| 196 |
char *record_iter; |
| 197 |
|
| 198 |
ptr += generate_dns_header(num_records, ptr); |
| 199 |
ptr += generate_dns_question(ptr); |
| 200 |
|
| 201 |
for (record_iter = records; record_iter < (char *) records + num_records * record_size; record_iter += record_size) { |
| 202 |
unsigned short rdlength; |
| 203 |
unsigned short net_rdlength; |
| 204 |
|
| 205 |
|
| 206 |
ptr += generate_dns_answer(0, ptr); |
| 207 |
rdlength = generate(record_iter, ptr + 2); |
| 208 |
net_rdlength = htons(rdlength); |
| 209 |
memcpy(ptr, &net_rdlength, 2); |
| 210 |
ptr += 2; |
| 211 |
ptr += rdlength; |
| 212 |
} |
| 213 |
|
| 214 |
return ptr - buffer; |
| 215 |
} |
| 216 |
|
| 217 |
#else |
| 218 |
|
| 219 |
int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf) |
| 220 |
{ |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
int ast_dns_test_write_domain(const char *string, char *buf) |
| 225 |
{ |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
int ast_dns_test_generate_result(struct ast_dns_query *query, void *records, size_t num_records, |
| 230 |
size_t record_size, record_fn generate, char *buffer) |
| 231 |
{ |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
#endif |