nat_detect.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 __PJNATH_NAT_DETECT_H__
  20. #define __PJNATH_NAT_DETECT_H__
  21. /**
  22. * @file ice_session.h
  23. * @brief ICE session management
  24. */
  25. #include <pjnath/stun_session.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJNATH_NAT_DETECT NAT Classification/Detection Tool
  29. * @brief NAT Classification/Detection Tool
  30. * @ingroup PJNATH
  31. * @{
  32. *
  33. * This module provides one function to perform NAT classification and
  34. * detection. NAT type detection is performed by calling
  35. * #pj_stun_detect_nat_type() function.
  36. */
  37. /**
  38. * This enumeration describes the NAT types, as specified by RFC 3489
  39. * Section 5, NAT Variations.
  40. */
  41. typedef enum pj_stun_nat_type
  42. {
  43. /**
  44. * NAT type is unknown because the detection has not been performed.
  45. */
  46. PJ_STUN_NAT_TYPE_UNKNOWN,
  47. /**
  48. * NAT type is unknown because there is failure in the detection
  49. * process, possibly because server does not support RFC 3489.
  50. */
  51. PJ_STUN_NAT_TYPE_ERR_UNKNOWN,
  52. /**
  53. * This specifies that the client has open access to Internet (or
  54. * at least, its behind a firewall that behaves like a full-cone NAT,
  55. * but without the translation)
  56. */
  57. PJ_STUN_NAT_TYPE_OPEN,
  58. /**
  59. * This specifies that communication with server has failed, probably
  60. * because UDP packets are blocked.
  61. */
  62. PJ_STUN_NAT_TYPE_BLOCKED,
  63. /**
  64. * Firewall that allows UDP out, and responses have to come back to
  65. * the source of the request (like a symmetric NAT, but no
  66. * translation.
  67. */
  68. PJ_STUN_NAT_TYPE_SYMMETRIC_UDP,
  69. /**
  70. * A full cone NAT is one where all requests from the same internal
  71. * IP address and port are mapped to the same external IP address and
  72. * port. Furthermore, any external host can send a packet to the
  73. * internal host, by sending a packet to the mapped external address.
  74. */
  75. PJ_STUN_NAT_TYPE_FULL_CONE,
  76. /**
  77. * A symmetric NAT is one where all requests from the same internal
  78. * IP address and port, to a specific destination IP address and port,
  79. * are mapped to the same external IP address and port. If the same
  80. * host sends a packet with the same source address and port, but to
  81. * a different destination, a different mapping is used. Furthermore,
  82. * only the external host that receives a packet can send a UDP packet
  83. * back to the internal host.
  84. */
  85. PJ_STUN_NAT_TYPE_SYMMETRIC,
  86. /**
  87. * A restricted cone NAT is one where all requests from the same
  88. * internal IP address and port are mapped to the same external IP
  89. * address and port. Unlike a full cone NAT, an external host (with
  90. * IP address X) can send a packet to the internal host only if the
  91. * internal host had previously sent a packet to IP address X.
  92. */
  93. PJ_STUN_NAT_TYPE_RESTRICTED,
  94. /**
  95. * A port restricted cone NAT is like a restricted cone NAT, but the
  96. * restriction includes port numbers. Specifically, an external host
  97. * can send a packet, with source IP address X and source port P,
  98. * to the internal host only if the internal host had previously sent
  99. * a packet to IP address X and port P.
  100. */
  101. PJ_STUN_NAT_TYPE_PORT_RESTRICTED
  102. } pj_stun_nat_type;
  103. /**
  104. * This structure contains the result of NAT classification function.
  105. */
  106. typedef struct pj_stun_nat_detect_result
  107. {
  108. /**
  109. * Status of the detection process. If this value is not PJ_SUCCESS,
  110. * the detection has failed and \a nat_type field will contain
  111. * PJ_STUN_NAT_TYPE_UNKNOWN.
  112. */
  113. pj_status_t status;
  114. /**
  115. * The text describing the status, if the status is not PJ_SUCCESS.
  116. */
  117. const char *status_text;
  118. /**
  119. * This contains the NAT type as detected by the detection procedure.
  120. * This value is only valid when the \a status is PJ_SUCCESS.
  121. */
  122. pj_stun_nat_type nat_type;
  123. /**
  124. * Text describing that NAT type.
  125. */
  126. const char *nat_type_name;
  127. } pj_stun_nat_detect_result;
  128. /**
  129. * Type of callback to be called when the NAT detection function has
  130. * completed.
  131. */
  132. typedef void pj_stun_nat_detect_cb(void *user_data,
  133. const pj_stun_nat_detect_result *res);
  134. /**
  135. * Get the NAT name from the specified NAT type.
  136. *
  137. * @param type NAT type.
  138. *
  139. * @return NAT name.
  140. */
  141. PJ_DECL(const char*) pj_stun_get_nat_name(pj_stun_nat_type type);
  142. /**
  143. * Perform NAT classification function according to the procedures
  144. * specified in RFC 3489. Once this function returns successfully,
  145. * the procedure will run in the "background" and will complete
  146. * asynchronously. Application can register a callback to be notified
  147. * when such detection has completed.
  148. *
  149. * See also #pj_stun_detect_nat_type2() which supports IPv6.
  150. *
  151. * @param server STUN server address.
  152. * @param stun_cfg A structure containing various STUN configurations,
  153. * such as the ioqueue and timer heap instance used
  154. * to receive network I/O and timer events.
  155. * @param user_data Application data, which will be returned back
  156. * in the callback.
  157. * @param cb Callback to be registered to receive notification
  158. * about detection result.
  159. *
  160. * @return If this function returns PJ_SUCCESS, the procedure
  161. * will complete asynchronously and callback will be
  162. * called when it completes. For other return
  163. * values, it means that an error has occured and
  164. * the procedure did not start.
  165. */
  166. PJ_DECL(pj_status_t) pj_stun_detect_nat_type(const pj_sockaddr_in *server,
  167. pj_stun_config *stun_cfg,
  168. void *user_data,
  169. pj_stun_nat_detect_cb *cb);
  170. /**
  171. * Variant of #pj_stun_detect_nat_type() that supports IPv6.
  172. *
  173. * @param server STUN server address.
  174. * @param stun_cfg A structure containing various STUN configurations,
  175. * such as the ioqueue and timer heap instance used
  176. * to receive network I/O and timer events.
  177. * @param user_data Application data, which will be returned back
  178. * in the callback.
  179. * @param cb Callback to be registered to receive notification
  180. * about detection result.
  181. *
  182. * @return If this function returns PJ_SUCCESS, the procedure
  183. * will complete asynchronously and callback will be
  184. * called when it completes. For other return
  185. * values, it means that an error has occured and
  186. * the procedure did not start.
  187. */
  188. PJ_DECL(pj_status_t) pj_stun_detect_nat_type2(const pj_sockaddr *server,
  189. pj_stun_config *stun_cfg,
  190. void *user_data,
  191. pj_stun_nat_detect_cb *cb);
  192. /**
  193. * @}
  194. */
  195. PJ_END_DECL
  196. #endif /* __PJNATH_NAT_DETECT_H__ */