| 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 |
#include "asterisk.h" |
| 29 |
|
| 30 |
#include <netinet/in.h> |
| 31 |
#include <resolv.h> |
| 32 |
|
| 33 |
#include "asterisk/dns_core.h" |
| 34 |
#include "asterisk/dns_txt.h" |
| 35 |
#include "asterisk/dns_internal.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
|
| 38 |
struct ast_dns_record *dns_txt_alloc(struct ast_dns_query *query, const char *data, const size_t size) |
| 39 |
{ |
| 40 |
struct ast_dns_txt_record *txt; |
| 41 |
const char *end_of_record = data + size; |
| 42 |
size_t count = 0; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
while (data < end_of_record) { |
| 47 |
uint8_t byte_count = (uint8_t) *data; |
| 48 |
count++; |
| 49 |
data += byte_count + 1; |
| 50 |
} |
| 51 |
|
| 52 |
if (data != end_of_record) { |
| 53 |
|
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
txt = ast_calloc(1, sizeof(*txt) + size); |
| 58 |
if (!txt) { |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
txt->count = count; |
| 63 |
txt->generic.data_ptr = txt->data; |
| 64 |
|
| 65 |
return (struct ast_dns_record *) txt; |
| 66 |
} |
| 67 |
|
| 68 |
size_t ast_dns_txt_get_count(const struct ast_dns_record *record) |
| 69 |
{ |
| 70 |
struct ast_dns_txt_record *txt = (struct ast_dns_txt_record *) record; |
| 71 |
ast_assert(ast_dns_record_get_rr_type(record) == T_TXT); |
| 72 |
return txt->count; |
| 73 |
} |
| 74 |
|
| 75 |
struct ast_vector_string *ast_dns_txt_get_strings(const struct ast_dns_record *record) |
| 76 |
{ |
| 77 |
struct ast_vector_string *strings; |
| 78 |
|
| 79 |
const size_t size = ast_dns_record_get_data_size(record); |
| 80 |
const char *data = ast_dns_record_get_data(record); |
| 81 |
const char *end_of_record = data + size; |
| 82 |
|
| 83 |
ast_assert(ast_dns_record_get_rr_type(record) == T_TXT); |
| 84 |
|
| 85 |
strings = ast_malloc(sizeof(struct ast_vector_const_string)); |
| 86 |
if (!strings) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
if (AST_VECTOR_INIT(strings, ast_dns_txt_get_count(record))) { |
| 91 |
ast_free(strings); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
while (data < end_of_record) { |
| 96 |
char *s; |
| 97 |
uint8_t bytes = (uint8_t) *data; |
| 98 |
|
| 99 |
s = ast_malloc(bytes + 1); |
| 100 |
if (!s) { |
| 101 |
ast_dns_txt_free_strings(strings); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
memcpy(s, &data[1], bytes); |
| 106 |
s[bytes] = 0; |
| 107 |
|
| 108 |
|
| 109 |
AST_VECTOR_APPEND(strings, s); |
| 110 |
|
| 111 |
data += bytes + 1; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if (data != end_of_record) { |
| 116 |
ast_dns_txt_free_strings(strings); |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
return strings; |
| 121 |
} |
| 122 |
|
| 123 |
void ast_dns_txt_free_strings(struct ast_vector_string *strings) |
| 124 |
{ |
| 125 |
AST_VECTOR_CALLBACK_VOID(strings, ast_free); |
| 126 |
AST_VECTOR_PTR_FREE(strings); |
| 127 |
} |