stun_simple.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 __PJSTUN_H__
  20. #define __PJSTUN_H__
  21. /**
  22. * @file stun_simple.h
  23. * @brief STUN client.
  24. */
  25. #include <pjlib-util/types.h>
  26. #include <pj/sock.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * This enumeration describes STUN message types.
  30. */
  31. typedef enum pjstun_msg_type
  32. {
  33. PJSTUN_BINDING_REQUEST = 0x0001, /**< Binding request */
  34. PJSTUN_BINDING_RESPONSE = 0x0101, /**< Binding response */
  35. PJSTUN_BINDING_ERROR_RESPONSE = 0x0111, /**< Binding error */
  36. PJSTUN_SHARED_SECRET_REQUEST = 0x0002, /**< Secret request */
  37. PJSTUN_SHARED_SECRET_RESPONSE = 0x0102, /**< Secret response */
  38. PJSTUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112 /**< Secret error */
  39. } pjstun_msg_type;
  40. /**
  41. * This enumeration describes STUN attribute types.
  42. */
  43. typedef enum pjstun_attr_type
  44. {
  45. PJSTUN_ATTR_MAPPED_ADDR = 1,
  46. PJSTUN_ATTR_RESPONSE_ADDR,
  47. PJSTUN_ATTR_CHANGE_REQUEST,
  48. PJSTUN_ATTR_SOURCE_ADDR,
  49. PJSTUN_ATTR_CHANGED_ADDR,
  50. PJSTUN_ATTR_USERNAME,
  51. PJSTUN_ATTR_PASSWORD,
  52. PJSTUN_ATTR_MESSAGE_INTEGRITY,
  53. PJSTUN_ATTR_ERROR_CODE,
  54. PJSTUN_ATTR_UNKNOWN_ATTRIBUTES,
  55. PJSTUN_ATTR_REFLECTED_FROM,
  56. PJSTUN_ATTR_XOR_MAPPED_ADDR = 0x0020
  57. } pjstun_attr_type;
  58. /*
  59. * This structre describes STUN message header.
  60. */
  61. typedef struct pjstun_msg_hdr
  62. {
  63. pj_uint16_t type;
  64. pj_uint16_t length;
  65. pj_uint32_t tsx[4];
  66. } pjstun_msg_hdr;
  67. /*
  68. * This structre describes STUN attribute header.
  69. */
  70. typedef struct pjstun_attr_hdr
  71. {
  72. pj_uint16_t type;
  73. pj_uint16_t length;
  74. } pjstun_attr_hdr;
  75. /*
  76. * This structre describes STUN MAPPED-ADDR attribute.
  77. */
  78. typedef struct pjstun_mapped_addr_attr
  79. {
  80. pjstun_attr_hdr hdr;
  81. pj_uint8_t ignored;
  82. pj_uint8_t family;
  83. pj_uint16_t port;
  84. pj_uint32_t addr;
  85. } pjstun_mapped_addr_attr;
  86. typedef pjstun_mapped_addr_attr pjstun_response_addr_attr;
  87. typedef pjstun_mapped_addr_attr pjstun_changed_addr_attr;
  88. typedef pjstun_mapped_addr_attr pjstun_src_addr_attr;
  89. typedef pjstun_mapped_addr_attr pjstun_reflected_form_attr;
  90. typedef struct pjstun_change_request_attr
  91. {
  92. pjstun_attr_hdr hdr;
  93. pj_uint32_t value;
  94. } pjstun_change_request_attr;
  95. typedef struct pjstun_username_attr
  96. {
  97. pjstun_attr_hdr hdr;
  98. pj_uint32_t value[1];
  99. } pjstun_username_attr;
  100. typedef pjstun_username_attr pjstun_password_attr;
  101. typedef struct pjstun_error_code_attr
  102. {
  103. pjstun_attr_hdr hdr;
  104. pj_uint16_t ignored;
  105. pj_uint8_t err_class;
  106. pj_uint8_t number;
  107. char reason[4];
  108. } pjstun_error_code_attr;
  109. typedef struct pjstun_msg
  110. {
  111. pjstun_msg_hdr *hdr;
  112. int attr_count;
  113. pjstun_attr_hdr *attr[PJSTUN_MAX_ATTR];
  114. } pjstun_msg;
  115. /* STUN message API (stun.c). */
  116. PJ_DECL(pj_status_t) pjstun_create_bind_req( pj_pool_t *pool,
  117. void **msg, pj_size_t *len,
  118. pj_uint32_t id_hi,
  119. pj_uint32_t id_lo);
  120. PJ_DECL(pj_status_t) pjstun_parse_msg( void *buf, pj_size_t len,
  121. pjstun_msg *msg);
  122. PJ_DECL(void*) pjstun_msg_find_attr( pjstun_msg *msg, pjstun_attr_type t);
  123. /**
  124. * @defgroup PJLIB_UTIL_STUN_CLIENT Simple STUN Helper
  125. * @ingroup PJ_PROTOCOLS
  126. * @brief A simple and small footprint STUN resolution helper
  127. * @{
  128. *
  129. * This is the older implementation of STUN client, with only one function
  130. * provided (pjstun_get_mapped_addr()) to retrieve the public IP address
  131. * of multiple sockets.
  132. */
  133. /**
  134. * This is the main function to request the mapped address of local sockets
  135. * to multiple STUN servers. This function is able to find the mapped
  136. * addresses of multiple sockets simultaneously, and for each socket, two
  137. * requests will be sent to two different STUN servers to see if both servers
  138. * get the same public address for the same socket. (Note that application can
  139. * specify the same address for the two servers, but still two requests will
  140. * be sent for each server).
  141. *
  142. * This function will perform necessary retransmissions of the requests if
  143. * response is not received within a predetermined period. When all responses
  144. * have been received, the function will compare the mapped addresses returned
  145. * by the servers, and when both are equal, the address will be returned in
  146. * \a mapped_addr argument.
  147. *
  148. * @param pf The pool factory where memory will be allocated from.
  149. * @param sock_cnt Number of sockets in the socket array.
  150. * @param sock Array of local UDP sockets which public addresses are
  151. * to be queried from the STUN servers.
  152. * @param srv1 Host name or IP address string of the first STUN
  153. * server.
  154. * @param port1 The port number of the first STUN server.
  155. * @param srv2 Host name or IP address string of the second STUN
  156. * server.
  157. * @param port2 The port number of the second STUN server.
  158. * @param mapped_addr Array to receive the mapped public address of the local
  159. * UDP sockets, when the function returns PJ_SUCCESS.
  160. *
  161. * @return This functions returns PJ_SUCCESS if responses are
  162. * received from all servers AND all servers returned the
  163. * same mapped public address. Otherwise this function may
  164. * return one of the following error codes:
  165. * - PJLIB_UTIL_ESTUNNOTRESPOND: no respons from servers.
  166. * - PJLIB_UTIL_ESTUNSYMMETRIC: different mapped addresses
  167. * are returned by servers.
  168. * - etc.
  169. *
  170. */
  171. PJ_DECL(pj_status_t) pjstun_get_mapped_addr( pj_pool_factory *pf,
  172. int sock_cnt, pj_sock_t sock[],
  173. const pj_str_t *srv1, int port1,
  174. const pj_str_t *srv2, int port2,
  175. pj_sockaddr_in mapped_addr[]);
  176. /*
  177. * This structre describes configurable setting for requesting mapped address.
  178. */
  179. typedef struct pjstun_setting
  180. {
  181. /**
  182. * Specifies whether STUN request generated by old STUN library should
  183. * insert magic cookie (specified in RFC 5389) in the transaction ID.
  184. */
  185. pj_bool_t use_stun2;
  186. /**
  187. * Address family of the STUN servers.
  188. */
  189. int af;
  190. /**
  191. * Host name or IP address string of the first STUN server.
  192. */
  193. pj_str_t srv1;
  194. /**
  195. * The port number of the first STUN server.
  196. */
  197. int port1;
  198. /**
  199. * Host name or IP address string of the second STUN server.
  200. */
  201. pj_str_t srv2;
  202. /**
  203. * The port number of the second STUN server.
  204. */
  205. int port2;
  206. } pjstun_setting;
  207. /**
  208. * Another version of mapped address resolution of local sockets to multiple
  209. * STUN servers configured in #pjstun_setting. This function is able to find
  210. * the mapped addresses of multiple sockets simultaneously, and for each
  211. * socket, two requests will be sent to two different STUN servers to see if
  212. * both servers get the same public address for the same socket. (Note that
  213. * application can specify the same address for the two servers, but still
  214. * two requests will be sent for each server).
  215. *
  216. * This function will perform necessary retransmissions of the requests if
  217. * response is not received within a predetermined period. When all responses
  218. * have been received, the function will compare the mapped addresses returned
  219. * by the servers, and when both are equal, the address will be returned in
  220. * \a mapped_addr argument.
  221. *
  222. * @param pf The pool factory where memory will be allocated from.
  223. * @param opt The STUN settings.
  224. * @param sock_cnt Number of sockets in the socket array.
  225. * @param sock Array of local UDP sockets which public addresses are
  226. * to be queried from the STUN servers.
  227. * @param mapped_addr Array to receive the mapped public address of the local
  228. * UDP sockets, when the function returns PJ_SUCCESS.
  229. *
  230. * @return This functions returns PJ_SUCCESS if responses are
  231. * received from all servers AND all servers returned the
  232. * same mapped public address. Otherwise this function may
  233. * return one of the following error codes:
  234. * - PJLIB_UTIL_ESTUNNOTRESPOND: no respons from servers.
  235. * - PJLIB_UTIL_ESTUNSYMMETRIC: different mapped addresses
  236. * are returned by servers.
  237. * - etc.
  238. *
  239. */
  240. PJ_DECL(pj_status_t) pjstun_get_mapped_addr2( pj_pool_factory *pf,
  241. const pjstun_setting *opt,
  242. int sock_cnt,
  243. pj_sock_t sock[],
  244. pj_sockaddr_in mapped_addr[]);
  245. PJ_END_DECL
  246. /**
  247. * @}
  248. */
  249. #endif /* __PJSTUN_H__ */