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