remove a bunch of useless #include "options.h"
[asterisk/asterisk.git] / main / srv.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Funding provided by nic.at
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief DNS SRV Record Lookup Support for Asterisk
24  *
25  * \author Mark Spencer <markster@digium.com>
26  * 
27  * \arg See also \ref AstENUM
28  *
29  * \note Funding provided by nic.at
30  */
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #ifdef __APPLE__
39 #if __APPLE_CC__ >= 1495
40 #include <arpa/nameser_compat.h>
41 #endif
42 #endif
43 #include <resolv.h>
44
45 #include "asterisk/channel.h"
46 #include "asterisk/srv.h"
47 #include "asterisk/dns.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/linkedlists.h"
50
51 #ifdef __APPLE__
52 #undef T_SRV
53 #define T_SRV 33
54 #endif
55
56 struct srv_entry {
57         unsigned short priority;
58         unsigned short weight;
59         unsigned short port;
60         unsigned int weight_sum;
61         AST_LIST_ENTRY(srv_entry) list;
62         char host[1];
63 };
64
65 struct srv_context {
66         unsigned int have_weights:1;
67         AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry) entries;
68 };
69
70 static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result)
71 {
72         struct srv {
73                 unsigned short priority;
74                 unsigned short weight;
75                 unsigned short port;
76         } __attribute__ ((__packed__)) *srv = (struct srv *) answer;
77
78         int res = 0;
79         char repl[256] = "";
80         struct srv_entry *entry;
81
82         if (len < sizeof(*srv))
83                 return -1;
84
85         answer += sizeof(*srv);
86         len -= sizeof(*srv);
87
88         if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
89                 ast_log(LOG_WARNING, "Failed to expand hostname\n");
90                 return -1;
91         }
92
93         /* the magic value "." for the target domain means that this service
94            is *NOT* available at the domain we searched */
95         if (!strcmp(repl, "."))
96                 return -1;
97
98         if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
99                 return -1;
100         
101         entry->priority = ntohs(srv->priority);
102         entry->weight = ntohs(srv->weight);
103         entry->port = ntohs(srv->port);
104         strcpy(entry->host, repl);
105
106         *result = entry;
107         
108         return 0;
109 }
110
111 static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
112 {
113         struct srv_context *c = (struct srv_context *) context;
114         struct srv_entry *entry = NULL;
115         struct srv_entry *current;
116
117         if (parse_srv(answer, len, fullanswer, &entry))
118                 return -1;
119
120         if (entry->weight)
121                 c->have_weights = 1;
122
123         AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) {
124                 /* insert this entry just before the first existing
125                    entry with a higher priority */
126                 if (current->priority <= entry->priority)
127                         continue;
128
129                 AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
130                 entry = NULL;
131                 break;
132         }
133         AST_LIST_TRAVERSE_SAFE_END;
134
135         /* if we didn't find a place to insert the entry before an existing
136            entry, then just add it to the end */
137         if (entry)
138                 AST_LIST_INSERT_TAIL(&c->entries, entry, list);
139
140         return 1;
141 }
142
143 /* Do the bizarre SRV record weight-handling algorithm
144    involving sorting and random number generation...
145
146    See RFC 2782 if you want know why this code does this
147 */
148 static void process_weights(struct srv_context *context)
149 {
150         struct srv_entry *current;
151         struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
152
153         while (AST_LIST_FIRST(&context->entries)) {
154                 unsigned int random_weight;
155                 unsigned int weight_sum;
156                 unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
157                 struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
158                 weight_sum = 0;
159
160                 AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
161                         if (current->priority != cur_priority)
162                                 break;
163
164                         AST_LIST_MOVE_CURRENT(&temp_list, list);
165                 }
166                 AST_LIST_TRAVERSE_SAFE_END;
167
168                 while (AST_LIST_FIRST(&temp_list)) {
169                         weight_sum = 0;
170                         AST_LIST_TRAVERSE(&temp_list, current, list)
171                                 current->weight_sum = weight_sum += current->weight;
172
173                         /* if all the remaining entries have weight == 0,
174                            then just append them to the result list and quit */
175                         if (weight_sum == 0) {
176                                 AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
177                                 break;
178                         }
179
180                         random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
181
182                         AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
183                                 if (current->weight < random_weight)
184                                         continue;
185
186                                 AST_LIST_MOVE_CURRENT(&newlist, list);
187                                 break;
188                         }
189                         AST_LIST_TRAVERSE_SAFE_END;
190                 }
191
192         }
193
194         /* now that the new list has been ordered,
195            put it in place */
196
197         AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
198 }
199
200 int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
201 {
202         struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
203         struct srv_entry *current;
204         int ret;
205
206         if (chan && ast_autoservice_start(chan) < 0)
207                 return -1;
208
209         ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
210
211         if (context.have_weights)
212                 process_weights(&context);
213
214         if (chan)
215                 ret |= ast_autoservice_stop(chan);
216
217         /* TODO: there could be a "." entry in the returned list of
218            answers... if so, this requires special handling */
219
220         /* the list of entries will be sorted in the proper selection order
221            already, so we just need the first one (if any) */
222
223         if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
224                 ast_copy_string(host, current->host, hostlen);
225                 *port = current->port;
226                 ast_free(current);
227                 if (option_verbose > 3) {
228                         ast_verbose(VERBOSE_PREFIX_3 "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
229                                     service, host, *port);
230                 }
231         } else {
232                 host[0] = '\0';
233                 *port = -1;
234         }
235
236         while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list)))
237                 ast_free(current);
238
239         return ret;
240 }