Debian Codegraph @

main/dns_srv.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief DNS SRV Record Support
22 *
23 * \author Joshua Colp <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
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 /* PRIORITY */
60 ptr += dns_parse_short((unsigned char *) ptr, &priority);
61 if (ptr >= end_of_record) {
62 return NULL;
63 }
64
65 /* WEIGHT */
66 ptr += dns_parse_short((unsigned char *) ptr, &weight);
67 if (ptr >= end_of_record) {
68 return NULL;
69 }
70
71 /* PORT */
72 ptr += dns_parse_short((unsigned char *) ptr, &port);
73 if (ptr >= end_of_record) {
74 return NULL;
75 }
76
77 /*
78 * The return value from dn_expand represents the size of the replacement
79 * in the buffer which MAY be compressed. Since the expanded replacement
80 * is NULL terminated, you can use strlen() to get the expanded size.
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); /* SAFE */
105 srv->generic.data_ptr = srv->data;
106
107 return (struct ast_dns_record *)srv;
108 }
109
110 /* This implementation was taken from the existing srv.c which, after reading the RFC, implements it
111 * as it should.
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 /* Find the lowest current priority to work on, but if the priority is already zero there is no lower priority */
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 /* Find all records which match this priority */
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 /* Records with a weight of zero must always be at the head */
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 /* Apply weighting - as each record is passed the sum of all previous weights (plus its own) is stored away, and then a random weight
149 * is calculated. The first record with a weight sum greater than the random weight is put in the new list and the whole thing starts
150 * once again.
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 /* if all the remaining entries have weight == 0,
161 then just append them to the result list and quit */
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 /* now that the new list has been ordered,
183 put it in place */
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 }