Merge "res_calendar: Specialized calendars depend on symbols of general calendar."
[asterisk/asterisk.git] / main / dns_srv.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
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 <jcolp@digium.com>
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
53         ptr = dns_find_record(data, size, query->result->answer, query->result->answer_size);
54         ast_assert(ptr != NULL);
55
56         end_of_record = ptr + size;
57
58         /* PRIORITY */
59         ptr += dns_parse_short((unsigned char *) ptr, &priority);
60         if (ptr >= end_of_record) {
61                 return NULL;
62         }
63
64         /* WEIGHT */
65         ptr += dns_parse_short((unsigned char *) ptr, &weight);
66         if (ptr >= end_of_record) {
67                 return NULL;
68         }
69
70         /* PORT */
71         ptr += dns_parse_short((unsigned char *) ptr, &port);
72         if (ptr >= end_of_record) {
73                 return NULL;
74         }
75
76         host_size = dn_expand((unsigned char *)query->result->answer, (unsigned char *) end_of_record, (unsigned char *) ptr, host, sizeof(host) - 1);
77         if (host_size < 0) {
78                 ast_log(LOG_ERROR, "Failed to expand domain name: %s\n", strerror(errno));
79                 return NULL;
80         }
81
82         if (!strcmp(host, ".")) {
83                 return NULL;
84         }
85
86         srv = ast_calloc(1, sizeof(*srv) + size + host_size + 1);
87         if (!srv) {
88                 return NULL;
89         }
90
91         srv->priority = priority;
92         srv->weight = weight;
93         srv->port = port;
94
95         srv->host = srv->data + size;
96         strcpy((char *)srv->host, host); /* SAFE */
97         ((char *)srv->host)[host_size] = '\0';
98
99         srv->generic.data_ptr = srv->data;
100
101         return (struct ast_dns_record *)srv;
102 }
103
104 /* This implementation was taken from the existing srv.c which, after reading the RFC, implements it
105  * as it should.
106  */
107 void dns_srv_sort(struct ast_dns_result *result)
108 {
109         struct ast_dns_record *current;
110         struct dns_records newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
111
112         while (AST_LIST_FIRST(&result->records)) {
113                 unsigned short cur_priority = ((struct ast_dns_srv_record *)(AST_LIST_FIRST(&result->records)))->priority;
114                 struct dns_records temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
115
116                 /* Find the lowest current priority to work on, but if the priority is already zero there is no lower priority */
117                 if (cur_priority) {
118                         AST_LIST_TRAVERSE(&result->records, current, list) {
119                                 if (((struct ast_dns_srv_record *)current)->priority < cur_priority) {
120                                         cur_priority = ((struct ast_dns_srv_record *)current)->priority;
121                                 }
122                         }
123                 }
124
125                 /* Find all records which match this priority */
126                 AST_LIST_TRAVERSE_SAFE_BEGIN(&result->records, current, list) {
127                         if (((struct ast_dns_srv_record *)current)->priority != cur_priority) {
128                                 continue;
129                         }
130
131                         AST_LIST_REMOVE_CURRENT(list);
132
133                         /* Records with a weight of zero must always be at the head */
134                         if (((struct ast_dns_srv_record *)current)->weight == 0) {
135                                 AST_LIST_INSERT_HEAD(&temp_list, current, list);
136                         } else {
137                                 AST_LIST_INSERT_TAIL(&temp_list, current, list);
138                         }
139                 }
140                 AST_LIST_TRAVERSE_SAFE_END;
141
142                 /* Apply weighting - as each record is passed the sum of all previous weights (plus its own) is stored away, and then a random weight
143                  * 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
144                  * once again.
145                  */
146                 while (AST_LIST_FIRST(&temp_list)) {
147                         unsigned int weight_sum = 0;
148                         unsigned int random_weight;
149
150                         AST_LIST_TRAVERSE(&temp_list, current, list) {
151                                 ((struct ast_dns_srv_record *)current)->weight_sum = weight_sum += ((struct ast_dns_srv_record *)current)->weight;
152                         }
153
154                         /* if all the remaining entries have weight == 0,
155                            then just append them to the result list and quit */
156                         if (weight_sum == 0) {
157                                 AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
158                                 break;
159                         }
160
161                         random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
162
163                         AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
164                                 if (((struct ast_dns_srv_record *)current)->weight_sum < random_weight) {
165                                         continue;
166                                 }
167
168                                 AST_LIST_MOVE_CURRENT(&newlist, list);
169                                 break;
170                         }
171                         AST_LIST_TRAVERSE_SAFE_END;
172                 }
173
174         }
175
176         /* now that the new list has been ordered,
177            put it in place */
178
179         AST_LIST_APPEND_LIST(&result->records, &newlist, list);
180 }
181
182 const char *ast_dns_srv_get_host(const struct ast_dns_record *record)
183 {
184         struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
185
186         ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
187         return srv->host;
188 }
189
190 unsigned short ast_dns_srv_get_priority(const struct ast_dns_record *record)
191 {
192         struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
193
194         ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
195         return srv->priority;
196 }
197
198 unsigned short ast_dns_srv_get_weight(const struct ast_dns_record *record)
199 {
200         struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
201
202         ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
203         return srv->weight;
204 }
205
206 unsigned short ast_dns_srv_get_port(const struct ast_dns_record *record)
207 {
208         struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
209
210         ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
211         return srv->port;
212 }