stun_sock.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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_STUN_SOCK_H__
  20. #define __PJNATH_STUN_SOCK_H__
  21. /**
  22. * @file stun_sock.h
  23. * @brief STUN aware socket transport
  24. */
  25. #include <pjnath/stun_config.h>
  26. #include <pjlib-util/resolver.h>
  27. #include <pj/ioqueue.h>
  28. #include <pj/lock.h>
  29. #include <pj/sock.h>
  30. #include <pj/sock_qos.h>
  31. PJ_BEGIN_DECL
  32. /**
  33. * @addtogroup PJNATH_STUN_SOCK
  34. * @{
  35. *
  36. * The STUN transport provides asynchronous UDP like socket transport
  37. * with the additional STUN capability. It has the following features:
  38. *
  39. * - API to send and receive UDP packets
  40. *
  41. * - multiplex STUN and non-STUN incoming packets and distinguish between
  42. * STUN responses that belong to internal requests with application data
  43. * (the application data may be STUN packets as well)
  44. *
  45. * - DNS SRV resolution to the STUN server (if wanted), along with fallback
  46. * to DNS A resolution if SRV record is not found.
  47. *
  48. * - STUN keep-alive maintenance, and handle changes to the mapped address
  49. * (when the NAT binding changes)
  50. *
  51. */
  52. /**
  53. * Opaque type to represent a STUN transport.
  54. */
  55. typedef struct pj_stun_sock pj_stun_sock;
  56. /**
  57. * Types of operation being reported in \a on_status() callback of
  58. * pj_stun_sock_cb. Application may retrieve the string representation
  59. * of these constants with pj_stun_sock_op_name().
  60. */
  61. typedef enum pj_stun_sock_op
  62. {
  63. /**
  64. * Asynchronous DNS resolution.
  65. */
  66. PJ_STUN_SOCK_DNS_OP = 1,
  67. /**
  68. * Initial STUN Binding request.
  69. */
  70. PJ_STUN_SOCK_BINDING_OP,
  71. /**
  72. * Subsequent STUN Binding request for keeping the binding
  73. * alive.
  74. */
  75. PJ_STUN_SOCK_KEEP_ALIVE_OP,
  76. /**
  77. * IP address change notification from the keep-alive operation.
  78. */
  79. PJ_STUN_SOCK_MAPPED_ADDR_CHANGE
  80. } pj_stun_sock_op;
  81. /**
  82. * This structure contains callbacks that will be called by the STUN
  83. * transport to notify application about various events.
  84. */
  85. typedef struct pj_stun_sock_cb
  86. {
  87. /**
  88. * Notification when incoming packet has been received.
  89. *
  90. * @param stun_sock The STUN transport.
  91. * @param data The packet.
  92. * @param data_len Length of the packet.
  93. * @param src_addr The source address of the packet.
  94. * @param addr_len The length of the source address.
  95. *
  96. * @return Application should normally return PJ_TRUE to let
  97. * the STUN transport continue its operation. However
  98. * it must return PJ_FALSE if it has destroyed the
  99. * STUN transport in this callback.
  100. */
  101. pj_bool_t (*on_rx_data)(pj_stun_sock *stun_sock,
  102. void *pkt,
  103. unsigned pkt_len,
  104. const pj_sockaddr_t *src_addr,
  105. unsigned addr_len);
  106. /**
  107. * Notifification when asynchronous send operation has completed.
  108. *
  109. * @param stun_sock The STUN transport.
  110. * @param send_key The send operation key that was given in
  111. * #pj_stun_sock_sendto().
  112. * @param sent If value is positive non-zero it indicates the
  113. * number of data sent. When the value is negative,
  114. * it contains the error code which can be retrieved
  115. * by negating the value (i.e. status=-sent).
  116. *
  117. * @return Application should normally return PJ_TRUE to let
  118. * the STUN transport continue its operation. However
  119. * it must return PJ_FALSE if it has destroyed the
  120. * STUN transport in this callback.
  121. */
  122. pj_bool_t (*on_data_sent)(pj_stun_sock *stun_sock,
  123. pj_ioqueue_op_key_t *send_key,
  124. pj_ssize_t sent);
  125. /**
  126. * Notification when the status of the STUN transport has changed. This
  127. * callback may be called for the following conditions:
  128. * - the first time the publicly mapped address has been resolved from
  129. * the STUN server, this callback will be called with \a op argument
  130. * set to PJ_STUN_SOCK_BINDING_OP \a status argument set to
  131. * PJ_SUCCESS.
  132. * - anytime when the transport has detected that the publicly mapped
  133. * address has changed, this callback will be called with \a op
  134. * argument set to PJ_STUN_SOCK_KEEP_ALIVE_OP and \a status
  135. * argument set to PJ_SUCCESS. On this case and the case above,
  136. * application will get the resolved public address in the
  137. * #pj_stun_sock_info structure.
  138. * - for any terminal error (such as STUN time-out, DNS resolution
  139. * failure, or keep-alive failure), this callback will be called
  140. * with the \a status argument set to non-PJ_SUCCESS.
  141. *
  142. * @param stun_sock The STUN transport.
  143. * @param op The operation that triggers the callback.
  144. * @param status The status.
  145. *
  146. * @return Must return PJ_FALSE if it has destroyed the
  147. * STUN transport in this callback. Application should
  148. * normally destroy the socket and return PJ_FALSE
  149. * upon encountering terminal error, otherwise it
  150. * should return PJ_TRUE to let the STUN socket operation
  151. * continues.
  152. */
  153. pj_bool_t (*on_status)(pj_stun_sock *stun_sock,
  154. pj_stun_sock_op op,
  155. pj_status_t status);
  156. } pj_stun_sock_cb;
  157. /**
  158. * This structure contains information about the STUN transport. Application
  159. * may query this information by calling #pj_stun_sock_get_info().
  160. */
  161. typedef struct pj_stun_sock_info
  162. {
  163. /**
  164. * The bound address of the socket.
  165. */
  166. pj_sockaddr bound_addr;
  167. /**
  168. * IP address of the STUN server.
  169. */
  170. pj_sockaddr srv_addr;
  171. /**
  172. * The publicly mapped address. It may contain zero address when the
  173. * mapped address has not been resolved. Application may query whether
  174. * this field contains valid address with pj_sockaddr_has_addr().
  175. */
  176. pj_sockaddr mapped_addr;
  177. /**
  178. * Number of interface address aliases. The interface address aliases
  179. * are list of all interface addresses in this host.
  180. */
  181. unsigned alias_cnt;
  182. /**
  183. * Array of interface address aliases.
  184. */
  185. pj_sockaddr aliases[PJ_ICE_ST_MAX_CAND];
  186. } pj_stun_sock_info;
  187. /**
  188. * This describe the settings to be given to the STUN transport during its
  189. * creation. Application should initialize this structure by calling
  190. * #pj_stun_sock_cfg_default().
  191. */
  192. typedef struct pj_stun_sock_cfg
  193. {
  194. /**
  195. * The group lock to be used by the STUN socket. If NULL, the STUN socket
  196. * will create one internally.
  197. *
  198. * Default: NULL
  199. */
  200. pj_grp_lock_t *grp_lock;
  201. /**
  202. * Packet buffer size.
  203. *
  204. * Default value is PJ_STUN_SOCK_PKT_LEN.
  205. */
  206. unsigned max_pkt_size;
  207. /**
  208. * Specify the number of simultaneous asynchronous read operations to
  209. * be invoked to the ioqueue. Having more than one read operations will
  210. * increase performance on multiprocessor systems since the application
  211. * will be able to process more than one incoming packets simultaneously.
  212. * Default value is 1.
  213. */
  214. unsigned async_cnt;
  215. /**
  216. * Specify the interface where the socket should be bound to. If the
  217. * address is zero, socket will be bound to INADDR_ANY. If the address
  218. * is non-zero, socket will be bound to this address only, and the
  219. * transport will have only one address alias (the \a alias_cnt field
  220. * in #pj_stun_sock_info structure. If the port is set to zero, the
  221. * socket will bind at any port (chosen by the OS).
  222. */
  223. pj_sockaddr bound_addr;
  224. /**
  225. * Specify the port range for STUN socket binding, relative to the start
  226. * port number specified in \a bound_addr. Note that this setting is only
  227. * applicable when the start port number is non zero.
  228. *
  229. * Default value is zero.
  230. */
  231. pj_uint16_t port_range;
  232. /**
  233. * Specify the STUN keep-alive duration, in seconds. The STUN transport
  234. * does keep-alive by sending STUN Binding request to the STUN server.
  235. * If this value is zero, the PJ_STUN_KEEP_ALIVE_SEC value will be used.
  236. * If the value is negative, it will disable STUN keep-alive.
  237. */
  238. int ka_interval;
  239. /**
  240. * QoS traffic type to be set on this transport. When application wants
  241. * to apply QoS tagging to the transport, it's preferable to set this
  242. * field rather than \a qos_param fields since this is more portable.
  243. *
  244. * Default value is PJ_QOS_TYPE_BEST_EFFORT.
  245. */
  246. pj_qos_type qos_type;
  247. /**
  248. * Set the low level QoS parameters to the transport. This is a lower
  249. * level operation than setting the \a qos_type field and may not be
  250. * supported on all platforms.
  251. *
  252. * By default all settings in this structure are disabled.
  253. */
  254. pj_qos_params qos_params;
  255. /**
  256. * Specify if STUN socket should ignore any errors when setting the QoS
  257. * traffic type/parameters.
  258. *
  259. * Default: PJ_TRUE
  260. */
  261. pj_bool_t qos_ignore_error;
  262. /**
  263. * Specify target value for socket receive buffer size. It will be
  264. * applied using setsockopt(). When it fails to set the specified size,
  265. * it will try with lower value until the highest possible is
  266. * successfully set.
  267. *
  268. * Default: 0 (OS default)
  269. */
  270. unsigned so_rcvbuf_size;
  271. /**
  272. * Specify target value for socket send buffer size. It will be
  273. * applied using setsockopt(). When it fails to set the specified size,
  274. * it will try with lower value until the highest possible is
  275. * successfully set.
  276. *
  277. * Default: 0 (OS default)
  278. */
  279. unsigned so_sndbuf_size;
  280. } pj_stun_sock_cfg;
  281. /**
  282. * Retrieve the name representing the specified operation.
  283. */
  284. PJ_DECL(const char*) pj_stun_sock_op_name(pj_stun_sock_op op);
  285. /**
  286. * Initialize the STUN transport setting with its default values.
  287. *
  288. * @param cfg The STUN transport config.
  289. */
  290. PJ_DECL(void) pj_stun_sock_cfg_default(pj_stun_sock_cfg *cfg);
  291. /**
  292. * Create the STUN transport using the specified configuration. Once
  293. * the STUN transport has been create, application should call
  294. * #pj_stun_sock_start() to start the transport.
  295. *
  296. * @param stun_cfg The STUN configuration which contains among other
  297. * things the ioqueue and timer heap instance for
  298. * the operation of this transport.
  299. * @param af Address family of socket. Currently pj_AF_INET()
  300. * and pj_AF_INET6() are supported.
  301. * @param name Optional name to be given to this transport to
  302. * assist debugging.
  303. * @param cb Callback to receive events/data from the transport.
  304. * @param cfg Optional transport settings.
  305. * @param user_data Arbitrary application data to be associated with
  306. * this transport.
  307. * @param p_sock Pointer to receive the created transport instance.
  308. *
  309. * @return PJ_SUCCESS if the operation has been successful,
  310. * or the appropriate error code on failure.
  311. */
  312. PJ_DECL(pj_status_t) pj_stun_sock_create(pj_stun_config *stun_cfg,
  313. const char *name,
  314. int af,
  315. const pj_stun_sock_cb *cb,
  316. const pj_stun_sock_cfg *cfg,
  317. void *user_data,
  318. pj_stun_sock **p_sock);
  319. /**
  320. * Start the STUN transport. This will start the DNS SRV resolution for
  321. * the STUN server (if desired), and once the server is resolved, STUN
  322. * Binding request will be sent to resolve the publicly mapped address.
  323. * Once the initial STUN Binding response is received, the keep-alive
  324. * timer will be started.
  325. *
  326. * @param stun_sock The STUN transport instance.
  327. * @param domain The domain, hostname, or IP address of the STUN
  328. * server. When this parameter contains domain name,
  329. * the \a resolver parameter must be set to activate
  330. * DNS SRV resolution.
  331. * @param default_port The default STUN port number to use when DNS SRV
  332. * resolution is not used. If DNS SRV resolution is
  333. * used, the server port number will be set from the
  334. * DNS SRV records. The recommended value for this
  335. * parameter is PJ_STUN_PORT.
  336. * @param resolver If this parameter is not NULL, then the \a domain
  337. * parameter will be first resolved with DNS SRV and
  338. * then fallback to using DNS A/AAAA resolution when
  339. * DNS SRV resolution fails. If this parameter is
  340. * NULL, the \a domain parameter will be resolved as
  341. * hostname.
  342. *
  343. * @return PJ_SUCCESS if the operation has been successfully
  344. * queued, or the appropriate error code on failure.
  345. * When this function returns PJ_SUCCESS, the final
  346. * result of the allocation process will be notified
  347. * to application in \a on_status() callback.
  348. */
  349. PJ_DECL(pj_status_t) pj_stun_sock_start(pj_stun_sock *stun_sock,
  350. const pj_str_t *domain,
  351. pj_uint16_t default_port,
  352. pj_dns_resolver *resolver);
  353. /**
  354. * Destroy the STUN transport.
  355. *
  356. * @param sock The STUN transport socket.
  357. *
  358. * @return PJ_SUCCESS if the operation has been successful,
  359. * or the appropriate error code on failure.
  360. */
  361. PJ_DECL(pj_status_t) pj_stun_sock_destroy(pj_stun_sock *sock);
  362. /**
  363. * Associate a user data with this STUN transport. The user data may then
  364. * be retrieved later with #pj_stun_sock_get_user_data().
  365. *
  366. * @param stun_sock The STUN transport instance.
  367. * @param user_data Arbitrary data.
  368. *
  369. * @return PJ_SUCCESS if the operation has been successful,
  370. * or the appropriate error code on failure.
  371. */
  372. PJ_DECL(pj_status_t) pj_stun_sock_set_user_data(pj_stun_sock *stun_sock,
  373. void *user_data);
  374. /**
  375. * Retrieve the previously assigned user data associated with this STUN
  376. * transport.
  377. *
  378. * @param stun_sock The STUN transport instance.
  379. *
  380. * @return The user/application data.
  381. */
  382. PJ_DECL(void*) pj_stun_sock_get_user_data(pj_stun_sock *stun_sock);
  383. /**
  384. * Get the group lock for this STUN transport.
  385. *
  386. * @param stun_sock The STUN transport instance.
  387. *
  388. * @return The group lock.
  389. */
  390. PJ_DECL(pj_grp_lock_t *) pj_stun_sock_get_grp_lock(pj_stun_sock *stun_sock);
  391. /**
  392. * Get the STUN transport info. The transport info contains, among other
  393. * things, the allocated relay address.
  394. *
  395. * @param stun_sock The STUN transport instance.
  396. * @param info Pointer to be filled with STUN transport info.
  397. *
  398. * @return PJ_SUCCESS if the operation has been successful,
  399. * or the appropriate error code on failure.
  400. */
  401. PJ_DECL(pj_status_t) pj_stun_sock_get_info(pj_stun_sock *stun_sock,
  402. pj_stun_sock_info *info);
  403. /**
  404. * Send a data to the specified address. This function may complete
  405. * asynchronously and in this case \a on_data_sent() will be called.
  406. *
  407. * @param stun_sock The STUN transport instance.
  408. * @param send_key Optional send key for sending the packet down to
  409. * the ioqueue. This value will be given back to
  410. * \a on_data_sent() callback
  411. * @param pkt The data/packet to be sent to peer.
  412. * @param pkt_len Length of the data.
  413. * @param flag pj_ioqueue_sendto() flag.
  414. * @param dst_addr The remote address.
  415. * @param addr_len Length of the address.
  416. *
  417. * @return PJ_SUCCESS if data has been sent immediately, or
  418. * PJ_EPENDING if data cannot be sent immediately. In
  419. * this case the \a on_data_sent() callback will be
  420. * called when data is actually sent. Any other return
  421. * value indicates error condition.
  422. */
  423. PJ_DECL(pj_status_t) pj_stun_sock_sendto(pj_stun_sock *stun_sock,
  424. pj_ioqueue_op_key_t *send_key,
  425. const void *pkt,
  426. unsigned pkt_len,
  427. unsigned flag,
  428. const pj_sockaddr_t *dst_addr,
  429. unsigned addr_len);
  430. /**
  431. * @}
  432. */
  433. PJ_END_DECL
  434. #endif /* __PJNATH_STUN_SOCK_H__ */