| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include <netinet/in.h> |
| 33 |
#include <arpa/nameser.h> |
| 34 |
#include <resolv.h> |
| 35 |
|
| 36 |
#include "asterisk/dns_core.h" |
| 37 |
#include "asterisk/dns_srv.h" |
| 38 |
#include "asterisk/linkedlists.h" |
| 39 |
#include "asterisk/dns_internal.h" |
| 40 |
#include "asterisk/utils.h" |
| 41 |
|
| 42 |
struct ast_dns_record *dns_srv_alloc(struct ast_dns_query *query, const char *data, const size_t size) |
| 43 |
{ |
| 44 |
uint16_t priority; |
| 45 |
uint16_t weight; |
| 46 |
uint16_t port; |
| 47 |
const char *ptr; |
| 48 |
const char *end_of_record; |
| 49 |
struct ast_dns_srv_record *srv; |
| 50 |
int host_size; |
| 51 |
char host[NI_MAXHOST] = ""; |
| 52 |
size_t host_len; |
| 53 |
|
| 54 |
ptr = dns_find_record(data, size, query->result->answer, query->result->answer_size); |
| 55 |
ast_assert(ptr != NULL); |
| 56 |
|
| 57 |
end_of_record = ptr + size; |
| 58 |
|
| 59 |
|
| 60 |
ptr += dns_parse_short((unsigned char *) ptr, &priority); |
| 61 |
if (ptr >= end_of_record) { |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
ptr += dns_parse_short((unsigned char *) ptr, &weight); |
| 67 |
if (ptr >= end_of_record) { |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
ptr += dns_parse_short((unsigned char *) ptr, &port); |
| 73 |
if (ptr >= end_of_record) { |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
host_size = dn_expand((unsigned char *)query->result->answer, |
| 83 |
(unsigned char *) end_of_record, (unsigned char *) ptr, host, sizeof(host) - 1); |
| 84 |
if (host_size < 0) { |
| 85 |
ast_log(LOG_ERROR, "Failed to expand domain name: %s\n", strerror(errno)); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
if (!strcmp(host, ".")) { |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
host_len = strlen(host) + 1; |
| 94 |
srv = ast_calloc(1, sizeof(*srv) + size + host_len); |
| 95 |
if (!srv) { |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
srv->priority = priority; |
| 100 |
srv->weight = weight; |
| 101 |
srv->port = port; |
| 102 |
|
| 103 |
srv->host = srv->data + size; |
| 104 |
ast_copy_string((char *)srv->host, host, host_len); |
| 105 |
srv->generic.data_ptr = srv->data; |
| 106 |
|
| 107 |
return (struct ast_dns_record *)srv; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void dns_srv_sort(struct ast_dns_result *result) |
| 114 |
{ |
| 115 |
struct ast_dns_record *current; |
| 116 |
struct dns_records newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 117 |
|
| 118 |
while (AST_LIST_FIRST(&result->records)) { |
| 119 |
unsigned short cur_priority = ((struct ast_dns_srv_record *)(AST_LIST_FIRST(&result->records)))->priority; |
| 120 |
struct dns_records temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 121 |
|
| 122 |
|
| 123 |
if (cur_priority) { |
| 124 |
AST_LIST_TRAVERSE(&result->records, current, list) { |
| 125 |
if (((struct ast_dns_srv_record *)current)->priority < cur_priority) { |
| 126 |
cur_priority = ((struct ast_dns_srv_record *)current)->priority; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&result->records, current, list) { |
| 133 |
if (((struct ast_dns_srv_record *)current)->priority != cur_priority) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
AST_LIST_REMOVE_CURRENT(list); |
| 138 |
|
| 139 |
|
| 140 |
if (((struct ast_dns_srv_record *)current)->weight == 0) { |
| 141 |
AST_LIST_INSERT_HEAD(&temp_list, current, list); |
| 142 |
} else { |
| 143 |
AST_LIST_INSERT_TAIL(&temp_list, current, list); |
| 144 |
} |
| 145 |
} |
| 146 |
AST_LIST_TRAVERSE_SAFE_END; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
while (AST_LIST_FIRST(&temp_list)) { |
| 153 |
unsigned int weight_sum = 0; |
| 154 |
unsigned int random_weight; |
| 155 |
|
| 156 |
AST_LIST_TRAVERSE(&temp_list, current, list) { |
| 157 |
((struct ast_dns_srv_record *)current)->weight_sum = weight_sum += ((struct ast_dns_srv_record *)current)->weight; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if (weight_sum == 0) { |
| 163 |
AST_LIST_APPEND_LIST(&newlist, &temp_list, list); |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0))); |
| 168 |
|
| 169 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) { |
| 170 |
if (((struct ast_dns_srv_record *)current)->weight_sum < random_weight) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
AST_LIST_MOVE_CURRENT(&newlist, list); |
| 175 |
break; |
| 176 |
} |
| 177 |
AST_LIST_TRAVERSE_SAFE_END; |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
AST_LIST_APPEND_LIST(&result->records, &newlist, list); |
| 186 |
} |
| 187 |
|
| 188 |
const char *ast_dns_srv_get_host(const struct ast_dns_record *record) |
| 189 |
{ |
| 190 |
struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record; |
| 191 |
|
| 192 |
ast_assert(ast_dns_record_get_rr_type(record) == T_SRV); |
| 193 |
return srv->host; |
| 194 |
} |
| 195 |
|
| 196 |
unsigned short ast_dns_srv_get_priority(const struct ast_dns_record *record) |
| 197 |
{ |
| 198 |
struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record; |
| 199 |
|
| 200 |
ast_assert(ast_dns_record_get_rr_type(record) == T_SRV); |
| 201 |
return srv->priority; |
| 202 |
} |
| 203 |
|
| 204 |
unsigned short ast_dns_srv_get_weight(const struct ast_dns_record *record) |
| 205 |
{ |
| 206 |
struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record; |
| 207 |
|
| 208 |
ast_assert(ast_dns_record_get_rr_type(record) == T_SRV); |
| 209 |
return srv->weight; |
| 210 |
} |
| 211 |
|
| 212 |
unsigned short ast_dns_srv_get_port(const struct ast_dns_record *record) |
| 213 |
{ |
| 214 |
struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record; |
| 215 |
|
| 216 |
ast_assert(ast_dns_record_get_rr_type(record) == T_SRV); |
| 217 |
return srv->port; |
| 218 |
} |