srv_resolver.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJLIB_UTIL_SRV_RESOLVER_H__
  20. #define __PJLIB_UTIL_SRV_RESOLVER_H__
  21. /**
  22. * @file srv_resolver.h
  23. * @brief DNS SRV resolver
  24. */
  25. #include <pjlib-util/resolver.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_DNS_SRV_RESOLVER DNS SRV Resolution Helper
  29. * @ingroup PJ_DNS
  30. * @{
  31. *
  32. * \section PJ_DNS_SRV_RESOLVER_INTRO DNS SRV Resolution Helper
  33. *
  34. * This module provides an even higher layer of abstraction for the DNS
  35. * resolution framework, to resolve DNS SRV names.
  36. *
  37. * The #pj_dns_srv_resolve() function will asynchronously resolve the server
  38. * name into IP address(es) with a single function call. If the SRV name
  39. * contains multiple names, then each will be resolved with individual
  40. * DNS A resolution to get the IP addresses. Upon successful completion,
  41. * application callback will be called with each IP address of the
  42. * target selected based on the load-balancing and fail-over criteria
  43. * below.
  44. *
  45. * When the resolver fails to resolve the name using DNS SRV resolution
  46. * (for example when the DNS SRV record is not present in the DNS server),
  47. * the resolver will fallback to using DNS A record resolution to resolve
  48. * the name.
  49. *
  50. * \subsection PJ_DNS_SRV_RESOLVER_FAILOVER_LOADBALANCE Load-Balancing and Fail-Over
  51. *
  52. * When multiple targets are returned in the DNS SRV response, server entries
  53. * are selected based on the following rule (which is described in RFC 2782):
  54. * - targets will be sorted based on the priority first.
  55. * - for targets with the same priority, #pj_dns_srv_resolve() will select
  56. * only one target according to its weight. To select this one target,
  57. * the function associates running-sum for all targets, and generates
  58. * a random number between zero and the total running-sum (inclusive).
  59. * The target selected is the first target with running-sum greater than
  60. * or equal to this random number.
  61. *
  62. * The above procedure will select one target for each priority, allowing
  63. * application to fail-over to the next target when the previous target fails.
  64. * These targets are returned in the #pj_dns_srv_record structure
  65. * argument of the callback.
  66. *
  67. * \section PJ_DNS_SRV_RESOLVER_REFERENCE Reference
  68. *
  69. * Reference:
  70. * - <A HREF="http://www.ietf.org/rfc/rfc2782.txt">RFC 2782</A>:
  71. * A DNS RR for specifying the location of services (DNS SRV)
  72. */
  73. /**
  74. * Flags to be specified when starting the DNS SRV query.
  75. */
  76. typedef enum pj_dns_srv_option
  77. {
  78. /**
  79. * Specify if the resolver should fallback with DNS A
  80. * resolution when the SRV resolution fails. This option may
  81. * be specified together with PJ_DNS_SRV_FALLBACK_AAAA to
  82. * make the resolver fallback to both DNS A and DNS AAAA
  83. * resolutions if SRV resolution fails.
  84. */
  85. PJ_DNS_SRV_FALLBACK_A = 1,
  86. /**
  87. * Specify if the resolver should fallback with DNS AAAA
  88. * resolution when the SRV resolution fails. This option may
  89. * be specified together with PJ_DNS_SRV_FALLBACK_AAAA to
  90. * make the resolver fallback to both DNS A and DNS AAAA
  91. * resolutions if SRV resolution fails.
  92. */
  93. PJ_DNS_SRV_FALLBACK_AAAA = 2,
  94. /**
  95. * Specify if the resolver should try to resolve with DNS AAAA
  96. * resolution of each targets in the DNS SRV record. If this
  97. * option is not specified, the SRV resolver will query the
  98. * DNS A record for the target instead.
  99. */
  100. PJ_DNS_SRV_RESOLVE_AAAA = 4,
  101. /**
  102. * Specify if the resolver should try to resolve with DNS AAAA
  103. * resolution only (i.e: without DNS A resolution) for each targets
  104. * in the DNS SRV record.
  105. */
  106. PJ_DNS_SRV_RESOLVE_AAAA_ONLY = 8
  107. } pj_dns_srv_option;
  108. /**
  109. * This structure represents DNS SRV records as the result of DNS SRV
  110. * resolution using #pj_dns_srv_resolve().
  111. */
  112. typedef struct pj_dns_srv_record
  113. {
  114. /** Number of address records. */
  115. unsigned count;
  116. /** Address records. */
  117. struct
  118. {
  119. /** Server priority (the lower the higher the priority). */
  120. unsigned priority;
  121. /** Server weight (the higher the more load it can handle). */
  122. unsigned weight;
  123. /** Port number. */
  124. pj_uint16_t port;
  125. /** The host address. */
  126. pj_dns_addr_record server;
  127. } entry[PJ_DNS_SRV_MAX_ADDR];
  128. } pj_dns_srv_record;
  129. /** Opaque declaration for DNS SRV query */
  130. typedef struct pj_dns_srv_async_query pj_dns_srv_async_query;
  131. /**
  132. * Type of callback function to receive notification from the resolver
  133. * when the resolution process completes.
  134. */
  135. typedef void pj_dns_srv_resolver_cb(void *user_data,
  136. pj_status_t status,
  137. const pj_dns_srv_record *rec);
  138. /**
  139. * Start DNS SRV resolution for the specified name. The full name of the
  140. * entry will be concatenated from \a res_name and \a domain_name fragments.
  141. *
  142. * @param domain_name The domain name part of the name.
  143. * @param res_name The full service name, including the transport name
  144. * and with all the leading underscore characters and
  145. * ending dot (e.g. "_sip._udp.", "_stun._udp.").
  146. * @param def_port The port number to be assigned to the resolved address
  147. * when the DNS SRV resolution fails and the name is
  148. * resolved with DNS A resolution.
  149. * @param pool Memory pool used to allocate memory for the query.
  150. * @param resolver The resolver instance.
  151. * @param option Option flags, which can be constructed from
  152. * #pj_dns_srv_option bitmask. Note that this argument
  153. * was called "fallback_a" in pjsip version 0.8.0 and
  154. * older, but the new option should be backward
  155. * compatible with existing applications. If application
  156. * specifies PJ_TRUE as "fallback_a" value, it will
  157. * correspond to PJ_DNS_SRV_FALLBACK_A option.
  158. * @param token Arbitrary data to be associated with this query when
  159. * the calback is called.
  160. * @param cb Pointer to callback function to receive the
  161. * notification when the resolution process completes.
  162. * @param p_query Optional pointer to receive the query object, if one
  163. * was started. If this pointer is specified, a NULL will
  164. * be returned if response cache is available immediately.
  165. *
  166. * @return PJ_SUCCESS on success, or the appropriate error code.
  167. */
  168. PJ_DECL(pj_status_t) pj_dns_srv_resolve(const pj_str_t *domain_name,
  169. const pj_str_t *res_name,
  170. unsigned def_port,
  171. pj_pool_t *pool,
  172. pj_dns_resolver *resolver,
  173. unsigned option,
  174. void *token,
  175. pj_dns_srv_resolver_cb *cb,
  176. pj_dns_srv_async_query **p_query);
  177. /**
  178. * Cancel an outstanding DNS SRV query.
  179. *
  180. * @param query The pending asynchronous query to be cancelled.
  181. * @param notify If non-zero, the callback will be called with failure
  182. * status to notify that the query has been cancelled.
  183. *
  184. * @return PJ_SUCCESS on success, or the appropriate error code,
  185. */
  186. PJ_DECL(pj_status_t) pj_dns_srv_cancel_query(pj_dns_srv_async_query *query,
  187. pj_bool_t notify);
  188. /**
  189. * @}
  190. */
  191. PJ_END_DECL
  192. #endif /* __PJLIB_UTIL_SRV_RESOLVER_H__ */