res_pjsip_session: Fix cherry pick to master compile error.
[asterisk/asterisk.git] / res / res_pjsip / pjsip_resolver.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 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib-util/errno.h>
23
24 #include <arpa/nameser.h>
25
26 #include "asterisk/astobj2.h"
27 #include "asterisk/dns_core.h"
28 #include "asterisk/dns_query_set.h"
29 #include "asterisk/dns_srv.h"
30 #include "asterisk/dns_naptr.h"
31 #include "asterisk/res_pjsip.h"
32 #include "include/res_pjsip_private.h"
33
34 #ifdef HAVE_PJSIP_EXTERNAL_RESOLVER
35
36 /*! \brief Structure which contains transport+port information for an active query */
37 struct sip_target {
38         /*! \brief The transport to be used */
39         pjsip_transport_type_e transport;
40         /*! \brief The port */
41         int port;
42 };
43
44 /*! \brief The vector used for current targets */
45 AST_VECTOR(targets, struct sip_target);
46
47 /*! \brief Structure which keeps track of resolution */
48 struct sip_resolve {
49         /*! \brief Addresses currently being resolved, indexed based on index of queries in query set */
50         struct targets resolving;
51         /*! \brief Active queries */
52         struct ast_dns_query_set *queries;
53         /*! \brief Current viable server addresses */
54         pjsip_server_addresses addresses;
55         /*! \brief Callback to invoke upon completion */
56         pjsip_resolver_callback *callback;
57         /*! \brief User provided data */
58         void *token;
59 };
60
61 /*! \brief Our own defined transports, reduces the size of sip_available_transports */
62 enum sip_resolver_transport {
63         SIP_RESOLVER_TRANSPORT_UDP,
64         SIP_RESOLVER_TRANSPORT_TCP,
65         SIP_RESOLVER_TRANSPORT_TLS,
66         SIP_RESOLVER_TRANSPORT_UDP6,
67         SIP_RESOLVER_TRANSPORT_TCP6,
68         SIP_RESOLVER_TRANSPORT_TLS6,
69 };
70
71 /*! \brief Available transports on the system */
72 static int sip_available_transports[] = {
73         /* This is a list of transports with whether they are available as a valid transport
74          * stored. We use our own identifier as to reduce the size of sip_available_transports.
75          * As this array is only manipulated at startup it does not require a lock to protect
76          * it.
77          */
78         [SIP_RESOLVER_TRANSPORT_UDP] = 0,
79         [SIP_RESOLVER_TRANSPORT_TCP] = 0,
80         [SIP_RESOLVER_TRANSPORT_TLS] = 0,
81         [SIP_RESOLVER_TRANSPORT_UDP6] = 0,
82         [SIP_RESOLVER_TRANSPORT_TCP6] = 0,
83         [SIP_RESOLVER_TRANSPORT_TLS6] = 0,
84 };
85
86 /*!
87  * \internal
88  * \brief Destroy resolution data
89  *
90  * \param data The resolution data to destroy
91  *
92  * \return Nothing
93  */
94 static void sip_resolve_destroy(void *data)
95 {
96         struct sip_resolve *resolve = data;
97
98         AST_VECTOR_FREE(&resolve->resolving);
99         ao2_cleanup(resolve->queries);
100 }
101
102 /*!
103  * \internal
104  * \brief Check whether a transport is available or not
105  *
106  * \param transport The PJSIP transport type
107  *
108  * \return 1 success (transport is available)
109  * \return 0 failure (transport is not available)
110  */
111 static int sip_transport_is_available(enum pjsip_transport_type_e transport)
112 {
113         enum sip_resolver_transport resolver_transport;
114
115         if (transport == PJSIP_TRANSPORT_UDP) {
116                 resolver_transport = SIP_RESOLVER_TRANSPORT_UDP;
117         } else if (transport == PJSIP_TRANSPORT_TCP) {
118                 resolver_transport = SIP_RESOLVER_TRANSPORT_TCP;
119         } else if (transport == PJSIP_TRANSPORT_TLS) {
120                 resolver_transport = SIP_RESOLVER_TRANSPORT_TLS;
121         } else if (transport == PJSIP_TRANSPORT_UDP6) {
122                 resolver_transport = SIP_RESOLVER_TRANSPORT_UDP6;
123         } else if (transport == PJSIP_TRANSPORT_TCP6) {
124                 resolver_transport = SIP_RESOLVER_TRANSPORT_TCP6;
125         } else if (transport == PJSIP_TRANSPORT_TLS6) {
126                 resolver_transport = SIP_RESOLVER_TRANSPORT_TLS6;
127         } else {
128                 return 0;
129         }
130
131         return sip_available_transports[resolver_transport];
132 }
133
134 /*!
135  * \internal
136  * \brief Add a query to be resolved
137  *
138  * \param resolve The ongoing resolution
139  * \param name What to resolve
140  * \param rr_type The type of record to look up
141  * \param rr_class The type of class to look up
142  * \param transport The transport to use for any resulting records
143  * \param port The port to use for any resulting records - if not specified the
144  *             default for the transport is used
145  *
146  * \retval 0 success
147  * \retval -1 failure
148  */
149 static int sip_resolve_add(struct sip_resolve *resolve, const char *name, int rr_type, int rr_class, pjsip_transport_type_e transport, int port)
150 {
151         struct sip_target target = {
152                 .transport = transport,
153                 .port = port,
154         };
155
156         if (!resolve->queries) {
157                 resolve->queries = ast_dns_query_set_create();
158         }
159
160         if (!resolve->queries) {
161                 return -1;
162         }
163
164         if (!port) {
165                 target.port = pjsip_transport_get_default_port_for_type(transport);
166         }
167
168         if (AST_VECTOR_APPEND(&resolve->resolving, target)) {
169                 return -1;
170         }
171
172         ast_debug(2, "[%p] Added target '%s' with record type '%d', transport '%s', and port '%d'\n",
173                 resolve, name, rr_type, pjsip_transport_get_type_name(transport), target.port);
174
175         return ast_dns_query_set_add(resolve->queries, name, rr_type, rr_class);
176 }
177
178 /*!
179  * \internal
180  * \brief Task used to invoke the user specific callback
181  *
182  * \param data The complete resolution
183  *
184  * \return Nothing
185  */
186 static int sip_resolve_invoke_user_callback(void *data)
187 {
188         struct sip_resolve *resolve = data;
189         int idx;
190
191         for (idx = 0; idx < resolve->addresses.count; ++idx) {
192                 /* This includes space for the IP address, [, ], :, and the port */
193                 char addr[PJ_INET6_ADDRSTRLEN + 10];
194
195                 ast_debug(2, "[%p] Address '%d' is %s with transport '%s'\n",
196                         resolve, idx, pj_sockaddr_print(&resolve->addresses.entry[idx].addr, addr, sizeof(addr), 3),
197                         pjsip_transport_get_type_name(resolve->addresses.entry[idx].type));
198         }
199
200         ast_debug(2, "[%p] Invoking user callback with '%d' addresses\n", resolve, resolve->addresses.count);
201         resolve->callback(resolve->addresses.count ? PJ_SUCCESS : PJLIB_UTIL_EDNSNOANSWERREC, resolve->token, &resolve->addresses);
202
203         ao2_ref(resolve, -1);
204
205         return 0;
206 }
207
208 /*!
209  * \internal
210  * \brief Handle a NAPTR record according to RFC3263
211  *
212  * \param resolve The ongoing resolution
213  * \param record The NAPTR record itself
214  * \param service The service to look for
215  * \param transport The transport to use for resulting queries
216  *
217  * \retval 0 success
218  * \retval -1 failure (record not handled / supported)
219  */
220 static int sip_resolve_handle_naptr(struct sip_resolve *resolve, const struct ast_dns_record *record,
221         const char *service, pjsip_transport_type_e transport)
222 {
223         if (strcasecmp(ast_dns_naptr_get_service(record), service)) {
224                 return -1;
225         }
226
227         if (!sip_transport_is_available(transport) &&
228                 !sip_transport_is_available(transport + PJSIP_TRANSPORT_IPV6)) {
229                 ast_debug(2, "[%p] NAPTR service %s skipped as transport is unavailable\n",
230                         resolve, service);
231                 return -1;
232         }
233
234         if (strcasecmp(ast_dns_naptr_get_flags(record), "s")) {
235                 ast_debug(2, "[%p] NAPTR service %s received with unsupported flags '%s'\n",
236                         resolve, service, ast_dns_naptr_get_flags(record));
237                 return -1;
238         }
239
240         if (ast_strlen_zero(ast_dns_naptr_get_replacement(record))) {
241                 return -1;
242         }
243
244         return sip_resolve_add(resolve, ast_dns_naptr_get_replacement(record), ns_t_srv, ns_c_in,
245                 transport, 0);
246 }
247
248 /*!
249  * \internal
250  * \brief Query set callback function, invoked when all queries have completed
251  *
252  * \param query_set The completed query set
253  *
254  * \return Nothing
255  */
256 static void sip_resolve_callback(const struct ast_dns_query_set *query_set)
257 {
258         struct sip_resolve *resolve = ast_dns_query_set_get_data(query_set);
259         struct ast_dns_query_set *queries = resolve->queries;
260         struct targets resolving;
261         int idx, address_count = 0, have_naptr = 0, have_srv = 0;
262         unsigned short order = 0;
263         int strict_order = 0;
264
265         ast_debug(2, "[%p] All parallel queries completed\n", resolve);
266
267         resolve->queries = NULL;
268
269         /* This purposely steals the resolving list so we can add entries to the new one in
270          * the same loop and also have access to the old.
271          */
272         resolving = resolve->resolving;
273         AST_VECTOR_INIT(&resolve->resolving, 0);
274
275         /* The order of queries is what defines the preference order for the records within
276          * this specific query set. The preference order overall is defined as a result of
277          * drilling down from other records. Each completed query set replaces the results
278          * of the last.
279          */
280         for (idx = 0; idx < ast_dns_query_set_num_queries(queries); ++idx) {
281                 struct ast_dns_query *query = ast_dns_query_set_get(queries, idx);
282                 struct ast_dns_result *result = ast_dns_query_get_result(query);
283                 struct sip_target *target;
284                 const struct ast_dns_record *record;
285
286                 if (!result) {
287                         ast_debug(2, "[%p] No result information for target '%s' of type '%d'\n", resolve,
288                                 ast_dns_query_get_name(query), ast_dns_query_get_rr_type(query));
289                         continue;
290                 }
291
292                 target = AST_VECTOR_GET_ADDR(&resolving, idx);
293                 for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {
294
295                         if (ast_dns_record_get_rr_type(record) == ns_t_a ||
296                                 ast_dns_record_get_rr_type(record) == ns_t_aaaa) {
297                                 /* If NAPTR or SRV records exist the subsequent results from them take preference */
298                                 if (have_naptr || have_srv) {
299                                         ast_debug(2, "[%p] %s record being skipped on target '%s' because NAPTR or SRV record exists\n",
300                                                 resolve, ast_dns_record_get_rr_type(record) == ns_t_a ? "A" : "AAAA",
301                                                 ast_dns_query_get_name(query));
302                                         continue;
303                                 }
304
305                                 /* PJSIP has a fixed maximum number of addresses that can exist, so limit ourselves to that */
306                                 if (address_count == PJSIP_MAX_RESOLVED_ADDRESSES) {
307                                         break;
308                                 }
309
310                                 resolve->addresses.entry[address_count].type = target->transport;
311
312                                 /* Populate address information for the new address entry */
313                                 if (ast_dns_record_get_rr_type(record) == ns_t_a) {
314                                         ast_debug(2, "[%p] A record received on target '%s'\n", resolve, ast_dns_query_get_name(query));
315                                         resolve->addresses.entry[address_count].addr_len = sizeof(pj_sockaddr_in);
316                                         pj_sockaddr_init(pj_AF_INET(), &resolve->addresses.entry[address_count].addr, NULL,
317                                                 target->port);
318                                         resolve->addresses.entry[address_count].addr.ipv4.sin_addr = *(struct pj_in_addr*)ast_dns_record_get_data(record);
319                                 } else {
320                                         ast_debug(2, "[%p] AAAA record received on target '%s'\n", resolve, ast_dns_query_get_name(query));
321                                         resolve->addresses.entry[address_count].addr_len = sizeof(pj_sockaddr_in6);
322                                         pj_sockaddr_init(pj_AF_INET6(), &resolve->addresses.entry[address_count].addr, NULL,
323                                                 target->port);
324                                         pj_memcpy(&resolve->addresses.entry[address_count].addr.ipv6.sin6_addr, ast_dns_record_get_data(record),
325                                                 ast_dns_record_get_data_size(record));
326                                 }
327
328                                 address_count++;
329                         } else if (ast_dns_record_get_rr_type(record) == ns_t_srv) {
330                                 if (have_naptr) {
331                                         ast_debug(2, "[%p] SRV record being skipped on target '%s' because NAPTR record exists\n",
332                                                 resolve, ast_dns_query_get_name(query));
333                                         continue;
334                                 }
335
336                                 /* SRV records just create new queries for AAAA+A, nothing fancy */
337                                 ast_debug(2, "[%p] SRV record received on target '%s'\n", resolve, ast_dns_query_get_name(query));
338
339                                 if (sip_transport_is_available(target->transport + PJSIP_TRANSPORT_IPV6)) {
340                                         sip_resolve_add(resolve, ast_dns_srv_get_host(record), ns_t_aaaa, ns_c_in, target->transport + PJSIP_TRANSPORT_IPV6,
341                                                 ast_dns_srv_get_port(record));
342                                         have_srv = 1;
343                                 }
344
345                                 if (sip_transport_is_available(target->transport)) {
346                                         sip_resolve_add(resolve, ast_dns_srv_get_host(record), ns_t_a, ns_c_in, target->transport,
347                                                 ast_dns_srv_get_port(record));
348                                         have_srv = 1;
349                                 }
350                         } else if (ast_dns_record_get_rr_type(record) == ns_t_naptr) {
351                                 int added = -1;
352
353                                 ast_debug(2, "[%p] NAPTR record received on target '%s'\n", resolve, ast_dns_query_get_name(query));
354
355                                 if (strict_order && (ast_dns_naptr_get_order(record) != order)) {
356                                         ast_debug(2, "[%p] NAPTR record skipped because order '%hu' does not match strict order '%hu'\n",
357                                                 resolve, ast_dns_naptr_get_order(record), order);
358                                         continue;
359                                 }
360
361                                 if (target->transport == PJSIP_TRANSPORT_UNSPECIFIED || target->transport == PJSIP_TRANSPORT_UDP) {
362                                         added = sip_resolve_handle_naptr(resolve, record, "sip+d2u", PJSIP_TRANSPORT_UDP);
363                                 }
364                                 if (target->transport == PJSIP_TRANSPORT_UNSPECIFIED || target->transport == PJSIP_TRANSPORT_TCP) {
365                                         added = sip_resolve_handle_naptr(resolve, record, "sip+d2t", PJSIP_TRANSPORT_TCP);
366                                 }
367                                 if (target->transport == PJSIP_TRANSPORT_UNSPECIFIED || target->transport == PJSIP_TRANSPORT_TLS) {
368                                         added = sip_resolve_handle_naptr(resolve, record, "sips+d2t", PJSIP_TRANSPORT_TLS);
369                                 }
370
371                                 /* If this record was successfully handled then we need to limit ourselves to this order */
372                                 if (!added) {
373                                         have_naptr = 1;
374                                         strict_order = 1;
375                                         order = ast_dns_naptr_get_order(record);
376                                 }
377                         }
378                 }
379         }
380
381         /* Update the server addresses count, this is not limited as it can never exceed the max allowed */
382         resolve->addresses.count = address_count;
383
384         /* Free the vector we stole as we are responsible for it */
385         AST_VECTOR_FREE(&resolving);
386
387         /* If additional queries were added start the resolution process again */
388         if (resolve->queries) {
389                 ast_debug(2, "[%p] New queries added, performing parallel resolution again\n", resolve);
390                 ast_dns_query_set_resolve_async(resolve->queries, sip_resolve_callback, resolve);
391                 ao2_ref(queries, -1);
392                 return;
393         }
394
395         ast_debug(2, "[%p] Resolution completed - %d viable targets\n", resolve, resolve->addresses.count);
396
397         /* Push a task to invoke the callback, we do this so it is guaranteed to run in a PJSIP thread */
398         ao2_ref(resolve, +1);
399         if (ast_sip_push_task(NULL, sip_resolve_invoke_user_callback, resolve)) {
400                 ao2_ref(resolve, -1);
401         }
402
403         ao2_ref(queries, -1);
404 }
405
406 /*!
407  * \internal
408  * \brief Determine what address family a host may be if it is already an IP address
409  *
410  * \param host The host (which may be an IP address)
411  *
412  * \retval 6 The host is an IPv6 address
413  * \retval 4 The host is an IPv4 address
414  * \retval 0 The host is not an IP address
415  */
416 static int sip_resolve_get_ip_addr_ver(const pj_str_t *host)
417 {
418         pj_in_addr dummy;
419         pj_in6_addr dummy6;
420
421         if (pj_inet_aton(host, &dummy) > 0) {
422                 return 4;
423         }
424
425         if (pj_inet_pton(pj_AF_INET6(), host, &dummy6) == PJ_SUCCESS) {
426                 return 6;
427         }
428
429         return 0;
430 }
431
432 /*!
433  * \internal
434  * \brief Perform SIP resolution of a host
435  *
436  * \param resolver Configured resolver instance
437  * \param pool Memory pool to allocate things from
438  * \param target The target we are resolving
439  * \param token User data to pass to the resolver callback
440  * \param cb User resolver callback to invoke upon resolution completion
441  */
442 static void sip_resolve(pjsip_resolver_t *resolver, pj_pool_t *pool, const pjsip_host_info *target,
443         void *token, pjsip_resolver_callback *cb)
444 {
445         int ip_addr_ver;
446         pjsip_transport_type_e type = target->type;
447         struct sip_resolve *resolve;
448         char host[NI_MAXHOST];
449         int res = 0;
450
451         ast_copy_pj_str(host, &target->addr.host, sizeof(host));
452
453         ast_debug(2, "Performing SIP DNS resolution of target '%s'\n", host);
454
455         /* If the provided target is already an address don't bother resolving */
456         ip_addr_ver = sip_resolve_get_ip_addr_ver(&target->addr.host);
457
458         /* Determine the transport to use if none has been explicitly specified */
459         if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
460                 /* If we've been told to use a secure or reliable transport restrict ourselves to that */
461 #if PJ_HAS_TCP
462                 if (target->flag & PJSIP_TRANSPORT_SECURE) {
463                         type = PJSIP_TRANSPORT_TLS;
464                 } else if (target->flag & PJSIP_TRANSPORT_RELIABLE) {
465                         type = PJSIP_TRANSPORT_TCP;
466                 } else
467 #endif
468                 /* According to the RFC otherwise if an explicit IP address OR an explicit port is specified
469                  * we use UDP
470                  */
471                 if (ip_addr_ver || target->addr.port) {
472                         type = PJSIP_TRANSPORT_UDP;
473                 }
474
475                 if (ip_addr_ver == 6) {
476                         type = (pjsip_transport_type_e)((int) type + PJSIP_TRANSPORT_IPV6);
477                 }
478         }
479
480         ast_debug(2, "Transport type for target '%s' is '%s'\n", host, pjsip_transport_get_type_name(type));
481
482         /* If it's already an address call the callback immediately */
483         if (ip_addr_ver) {
484                 pjsip_server_addresses addresses = {
485                         .entry[0].type = type,
486                         .count = 1,
487                 };
488
489                 if (ip_addr_ver == 4) {
490                         addresses.entry[0].addr_len = sizeof(pj_sockaddr_in);
491                         pj_sockaddr_init(pj_AF_INET(), &addresses.entry[0].addr, NULL, 0);
492                         pj_inet_aton(&target->addr.host, &addresses.entry[0].addr.ipv4.sin_addr);
493                 } else {
494                         addresses.entry[0].addr_len = sizeof(pj_sockaddr_in6);
495                         pj_sockaddr_init(pj_AF_INET6(), &addresses.entry[0].addr, NULL, 0);
496                         pj_inet_pton(pj_AF_INET6(), &target->addr.host, &addresses.entry[0].addr.ipv6.sin6_addr);
497                 }
498
499                 pj_sockaddr_set_port(&addresses.entry[0].addr, !target->addr.port ? pjsip_transport_get_default_port_for_type(type) : target->addr.port);
500
501                 ast_debug(2, "Target '%s' is an IP address, skipping resolution\n", host);
502
503                 cb(PJ_SUCCESS, token, &addresses);
504
505                 return;
506         }
507
508         resolve = ao2_alloc_options(sizeof(*resolve), sip_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
509         if (!resolve) {
510                 cb(PJ_ENOMEM, token, NULL);
511                 return;
512         }
513
514         resolve->callback = cb;
515         resolve->token = token;
516
517         if (AST_VECTOR_INIT(&resolve->resolving, 2)) {
518                 ao2_ref(resolve, -1);
519                 cb(PJ_ENOMEM, token, NULL);
520                 return;
521         }
522
523         ast_debug(2, "[%p] Created resolution tracking for target '%s'\n", resolve, host);
524
525         /* If no port has been specified we can do NAPTR + SRV */
526         if (!target->addr.port) {
527                 char srv[NI_MAXHOST];
528
529                 res |= sip_resolve_add(resolve, host, ns_t_naptr, ns_c_in, type, 0);
530
531                 if ((type == PJSIP_TRANSPORT_TLS || type == PJSIP_TRANSPORT_UNSPECIFIED) &&
532                         (sip_transport_is_available(PJSIP_TRANSPORT_TLS) ||
533                                 sip_transport_is_available(PJSIP_TRANSPORT_TLS6))) {
534                         snprintf(srv, sizeof(srv), "_sips._tcp.%s", host);
535                         res |= sip_resolve_add(resolve, srv, ns_t_srv, ns_c_in, PJSIP_TRANSPORT_TLS, 0);
536                 }
537                 if ((type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_UNSPECIFIED) &&
538                         (sip_transport_is_available(PJSIP_TRANSPORT_TCP) ||
539                                 sip_transport_is_available(PJSIP_TRANSPORT_TCP6))) {
540                         snprintf(srv, sizeof(srv), "_sip._tcp.%s", host);
541                         res |= sip_resolve_add(resolve, srv, ns_t_srv, ns_c_in, PJSIP_TRANSPORT_TCP, 0);
542                 }
543                 if ((type == PJSIP_TRANSPORT_UDP || type == PJSIP_TRANSPORT_UNSPECIFIED) &&
544                         (sip_transport_is_available(PJSIP_TRANSPORT_UDP) ||
545                                 sip_transport_is_available(PJSIP_TRANSPORT_UDP6))) {
546                         snprintf(srv, sizeof(srv), "_sip._udp.%s", host);
547                         res |= sip_resolve_add(resolve, srv, ns_t_srv, ns_c_in, PJSIP_TRANSPORT_UDP, 0);
548                 }
549         }
550
551         if ((type == PJSIP_TRANSPORT_UNSPECIFIED && sip_transport_is_available(PJSIP_TRANSPORT_UDP6)) ||
552                 sip_transport_is_available(type + PJSIP_TRANSPORT_IPV6)) {
553                 res |= sip_resolve_add(resolve, host, ns_t_aaaa, ns_c_in, (type == PJSIP_TRANSPORT_UNSPECIFIED ? PJSIP_TRANSPORT_UDP6 : type + PJSIP_TRANSPORT_IPV6), target->addr.port);
554         }
555
556         if ((type == PJSIP_TRANSPORT_UNSPECIFIED && sip_transport_is_available(PJSIP_TRANSPORT_UDP)) ||
557                 sip_transport_is_available(type)) {
558                 res |= sip_resolve_add(resolve, host, ns_t_a, ns_c_in, (type == PJSIP_TRANSPORT_UNSPECIFIED ? PJSIP_TRANSPORT_UDP : type), target->addr.port);
559         }
560
561         if (res) {
562                 ao2_ref(resolve, -1);
563                 cb(PJ_ENOMEM, token, NULL);
564                 return;
565         }
566
567         ast_debug(2, "[%p] Starting initial resolution using parallel queries for target '%s'\n", resolve, host);
568         ast_dns_query_set_resolve_async(resolve->queries, sip_resolve_callback, resolve);
569
570         ao2_ref(resolve, -1);
571 }
572
573 /*!
574  * \internal
575  * \brief Determine if a specific transport is configured on the system
576  *
577  * \param pool A memory pool to allocate things from
578  * \param transport The type of transport to check
579  * \param name A friendly name to print in the verbose message
580  *
581  * \return Nothing
582  */
583 static void sip_check_transport(pj_pool_t *pool, pjsip_transport_type_e transport, const char *name)
584 {
585         pjsip_tpmgr_fla2_param prm;
586         enum sip_resolver_transport resolver_transport;
587
588         pjsip_tpmgr_fla2_param_default(&prm);
589         prm.tp_type = transport;
590
591         if (transport == PJSIP_TRANSPORT_UDP) {
592                 resolver_transport = SIP_RESOLVER_TRANSPORT_UDP;
593         } else if (transport == PJSIP_TRANSPORT_TCP) {
594                 resolver_transport = SIP_RESOLVER_TRANSPORT_TCP;
595         } else if (transport == PJSIP_TRANSPORT_TLS) {
596                 resolver_transport = SIP_RESOLVER_TRANSPORT_TLS;
597         } else if (transport == PJSIP_TRANSPORT_UDP6) {
598                 resolver_transport = SIP_RESOLVER_TRANSPORT_UDP6;
599         } else if (transport == PJSIP_TRANSPORT_TCP6) {
600                 resolver_transport = SIP_RESOLVER_TRANSPORT_TCP6;
601         } else if (transport == PJSIP_TRANSPORT_TLS6) {
602                 resolver_transport = SIP_RESOLVER_TRANSPORT_TLS6;
603         } else {
604                 ast_verb(2, "'%s' is an unsupported SIP transport\n", name);
605                 return;
606         }
607
608         if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),
609                 pool, &prm) == PJ_SUCCESS) {
610                 ast_verb(2, "'%s' is an available SIP transport\n", name);
611                 sip_available_transports[resolver_transport] = 1;
612         } else {
613                 ast_verb(2, "'%s' is not an available SIP transport, disabling resolver support for it\n",
614                         name);
615         }
616 }
617
618 /*! \brief External resolver implementation for PJSIP */
619 static pjsip_ext_resolver resolver = {
620         .resolve = sip_resolve,
621 };
622
623 /*!
624  * \internal
625  * \brief Task to determine available transports and set ourselves an external resolver
626  *
627  * \retval 0 success
628  * \retval -1 failure
629  */
630 static int sip_replace_resolver(void *data)
631 {
632         pj_pool_t *pool;
633
634
635         pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Transport Availability", 256, 256);
636         if (!pool) {
637                 return -1;
638         }
639
640         /* Determine what transports are available on the system */
641         sip_check_transport(pool, PJSIP_TRANSPORT_UDP, "UDP+IPv4");
642         sip_check_transport(pool, PJSIP_TRANSPORT_TCP, "TCP+IPv4");
643         sip_check_transport(pool, PJSIP_TRANSPORT_TLS, "TLS+IPv4");
644         sip_check_transport(pool, PJSIP_TRANSPORT_UDP6, "UDP+IPv6");
645         sip_check_transport(pool, PJSIP_TRANSPORT_TCP6, "TCP+IPv6");
646         sip_check_transport(pool, PJSIP_TRANSPORT_TLS6, "TLS+IPv6");
647
648         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
649
650         /* Replace the PJSIP resolver with our own implementation */
651         pjsip_endpt_set_ext_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
652         return 0;
653 }
654
655 void ast_sip_initialize_resolver(void)
656 {
657         /* Replace the existing PJSIP resolver with our own implementation */
658         ast_sip_push_task_synchronous(NULL, sip_replace_resolver, NULL);
659 }
660
661 #else
662
663 void ast_sip_initialize_resolver(void)
664 {
665         /* External resolver support does not exist in the version of PJSIP in use */
666         ast_log(LOG_NOTICE, "The version of PJSIP in use does not support external resolvers, using PJSIP provided resolver\n");
667 }
668
669 #endif