sip_transport_udp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 __PJSIP_TRANSPORT_UDP_H__
  20. #define __PJSIP_TRANSPORT_UDP_H__
  21. /**
  22. * @file sip_transport_udp.h
  23. * @brief SIP UDP Transport.
  24. */
  25. #include <pjsip/sip_transport.h>
  26. #include <pj/sock_qos.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJSIP_TRANSPORT_UDP UDP Transport
  30. * @ingroup PJSIP_TRANSPORT
  31. * @brief API to create and register UDP transport.
  32. * @{
  33. * The functions below are used to create UDP transport and register
  34. * the transport to the framework.
  35. */
  36. /**
  37. * Flag that can be specified when calling #pjsip_udp_transport_pause() or
  38. * #pjsip_udp_transport_restart().
  39. */
  40. enum
  41. {
  42. /**
  43. * This flag tells the transport to keep the existing/internal socket
  44. * handle.
  45. */
  46. PJSIP_UDP_TRANSPORT_KEEP_SOCKET = 1,
  47. /**
  48. * This flag tells the transport to destroy the existing/internal socket
  49. * handle. Naturally this flag and PJSIP_UDP_TRANSPORT_KEEP_SOCKET are
  50. * mutually exclusive.
  51. */
  52. PJSIP_UDP_TRANSPORT_DESTROY_SOCKET = 2
  53. };
  54. /**
  55. * Settings to be specified when creating the UDP transport. Application
  56. * should initialize this structure with its default values by calling
  57. * pjsip_udp_transport_cfg_default().
  58. */
  59. typedef struct pjsip_udp_transport_cfg
  60. {
  61. /**
  62. * Address family to use. Valid values are pj_AF_INET() and
  63. * pj_AF_INET6(). Default is pj_AF_INET().
  64. */
  65. int af;
  66. /**
  67. * Address to bind the socket to.
  68. */
  69. pj_sockaddr bind_addr;
  70. /**
  71. * Optional published address, which is the address to be
  72. * advertised as the address of this SIP transport.
  73. * By default the bound address will be used as the published address.
  74. */
  75. pjsip_host_port addr_name;
  76. /**
  77. * Number of simultaneous asynchronous accept() operations to be
  78. * supported. It is recommended that the number here corresponds to
  79. * the number of processors in the system (or the number of SIP
  80. * worker threads).
  81. *
  82. * Default: 1
  83. */
  84. unsigned async_cnt;
  85. /**
  86. * QoS traffic type to be set on this transport. When application wants
  87. * to apply QoS tagging to the transport, it's preferable to set this
  88. * field rather than \a qos_param fields since this is more portable.
  89. *
  90. * Default is QoS not set.
  91. */
  92. pj_qos_type qos_type;
  93. /**
  94. * Set the low level QoS parameters to the transport. This is a lower
  95. * level operation than setting the \a qos_type field and may not be
  96. * supported on all platforms.
  97. *
  98. * Default is QoS not set.
  99. */
  100. pj_qos_params qos_params;
  101. /**
  102. * Specify options to be set on the transport.
  103. *
  104. * By default there is no options.
  105. *
  106. */
  107. pj_sockopt_params sockopt_params;
  108. } pjsip_udp_transport_cfg;
  109. /**
  110. * Initialize pjsip_udp_transport_cfg structure with default values for
  111. * the specifed address family.
  112. *
  113. * @param cfg The structure to initialize.
  114. * @param af Address family to be used.
  115. */
  116. PJ_DECL(void) pjsip_udp_transport_cfg_default(pjsip_udp_transport_cfg *cfg,
  117. int af);
  118. /**
  119. * Start UDP IPv4/IPv6 transport.
  120. *
  121. * @param endpt The SIP endpoint.
  122. * @param cfg UDP transport settings. Application should initialize
  123. * this setting with #pjsip_udp_transport_cfg_default().
  124. * @param p_transport Pointer to receive the transport.
  125. *
  126. * @return PJ_SUCCESS when the transport has been successfully
  127. * started and registered to transport manager, or
  128. * the appropriate error code.
  129. */
  130. PJ_DECL(pj_status_t) pjsip_udp_transport_start2(
  131. pjsip_endpoint *endpt,
  132. const pjsip_udp_transport_cfg *cfg,
  133. pjsip_transport **p_transport);
  134. /**
  135. * Start UDP transport.
  136. *
  137. * @param endpt The SIP endpoint.
  138. * @param local Optional local address to bind. If this argument
  139. * is NULL, the UDP transport will be bound to arbitrary
  140. * UDP port.
  141. * @param a_name Published address (only the host and port portion is
  142. * used). If this argument is NULL, then the bound address
  143. * will be used as the published address.
  144. * @param async_cnt Number of simultaneous async operations.
  145. * @param p_transport Pointer to receive the transport.
  146. *
  147. * @return PJ_SUCCESS when the transport has been successfully
  148. * started and registered to transport manager, or
  149. * the appropriate error code.
  150. */
  151. PJ_DECL(pj_status_t) pjsip_udp_transport_start(pjsip_endpoint *endpt,
  152. const pj_sockaddr_in *local,
  153. const pjsip_host_port *a_name,
  154. unsigned async_cnt,
  155. pjsip_transport **p_transport);
  156. /**
  157. * Start IPv6 UDP transport.
  158. */
  159. PJ_DECL(pj_status_t) pjsip_udp_transport_start6(pjsip_endpoint *endpt,
  160. const pj_sockaddr_in6 *local,
  161. const pjsip_host_port *a_name,
  162. unsigned async_cnt,
  163. pjsip_transport **p_transport);
  164. /**
  165. * Attach IPv4 UDP socket as a new transport and start the transport.
  166. *
  167. * @param endpt The SIP endpoint.
  168. * @param sock UDP socket to use.
  169. * @param a_name Published address (only the host and port portion is
  170. * used).
  171. * @param async_cnt Number of simultaneous async operations.
  172. * @param p_transport Pointer to receive the transport.
  173. *
  174. * @return PJ_SUCCESS when the transport has been successfully
  175. * started and registered to transport manager, or
  176. * the appropriate error code.
  177. */
  178. PJ_DECL(pj_status_t) pjsip_udp_transport_attach(pjsip_endpoint *endpt,
  179. pj_sock_t sock,
  180. const pjsip_host_port *a_name,
  181. unsigned async_cnt,
  182. pjsip_transport **p_transport);
  183. /**
  184. * Attach IPv4 or IPv6 UDP socket as a new transport and start the transport.
  185. *
  186. * @param endpt The SIP endpoint.
  187. * @param type Transport type, which is PJSIP_TRANSPORT_UDP for IPv4
  188. * or PJSIP_TRANSPORT_UDP6 for IPv6 socket.
  189. * @param sock UDP socket to use.
  190. * @param a_name Published address (only the host and port portion is
  191. * used).
  192. * @param async_cnt Number of simultaneous async operations.
  193. * @param p_transport Pointer to receive the transport.
  194. *
  195. * @return PJ_SUCCESS when the transport has been successfully
  196. * started and registered to transport manager, or
  197. * the appropriate error code.
  198. */
  199. PJ_DECL(pj_status_t) pjsip_udp_transport_attach2(pjsip_endpoint *endpt,
  200. pjsip_transport_type_e type,
  201. pj_sock_t sock,
  202. const pjsip_host_port *a_name,
  203. unsigned async_cnt,
  204. pjsip_transport **p_transport);
  205. /**
  206. * Retrieve the internal socket handle used by the UDP transport. Note
  207. * that this socket normally is registered to ioqueue, so if application
  208. * wants to make use of this socket, it should temporarily pause the
  209. * transport.
  210. *
  211. * @param transport The UDP transport.
  212. *
  213. * @return The socket handle, or PJ_INVALID_SOCKET if no socket
  214. * is currently being used (for example, when transport
  215. * is being paused).
  216. */
  217. PJ_DECL(pj_sock_t) pjsip_udp_transport_get_socket(pjsip_transport *transport);
  218. /**
  219. * Temporarily pause or shutdown the transport. When transport is being
  220. * paused, it cannot be used by the SIP stack to send or receive SIP
  221. * messages.
  222. *
  223. * Two types of operations are supported by this function:
  224. * - to temporarily make this transport unavailable for SIP uses, but
  225. * otherwise keep the socket handle intact. Application then can
  226. * retrieve the socket handle with #pjsip_udp_transport_get_socket()
  227. * and use it to send/receive application data (for example, STUN
  228. * messages). In this case, application should specify
  229. * PJSIP_UDP_TRANSPORT_KEEP_SOCKET when calling this function, and
  230. * also to specify this flag when calling #pjsip_udp_transport_restart()
  231. * later.
  232. * - to temporarily shutdown the transport, including closing down
  233. * the internal socket handle. This is useful for example to
  234. * temporarily suspend the application for an indefinite period. In
  235. * this case, application should specify PJSIP_UDP_TRANSPORT_DESTROY_SOCKET
  236. * flag when calling this function, and specify a new socket when
  237. * calling #pjsip_udp_transport_restart().
  238. *
  239. * @param transport The UDP transport.
  240. * @param option Pause option.
  241. *
  242. * @return PJ_SUCCESS if transport is paused successfully,
  243. * or the appropriate error code.
  244. */
  245. PJ_DECL(pj_status_t) pjsip_udp_transport_pause(pjsip_transport *transport,
  246. unsigned option);
  247. /**
  248. * Restart the transport. Several operations are supported by this function:
  249. * - if transport was made temporarily unavailable to SIP stack with
  250. * pjsip_udp_transport_pause() and PJSIP_UDP_TRANSPORT_KEEP_SOCKET,
  251. * application can make the transport available to the SIP stack
  252. * again, by specifying PJSIP_UDP_TRANSPORT_KEEP_SOCKET flag here.
  253. * - if application wants to replace the internal socket with a new
  254. * socket, it must specify PJSIP_UDP_TRANSPORT_DESTROY_SOCKET when
  255. * calling this function, so that the internal socket will be destroyed
  256. * if it hasn't been closed. In this case, application has two choices
  257. * on how to create the new socket: 1) to let the transport create
  258. * the new socket, in this case the \a sock option should be set
  259. * to \a PJ_INVALID_SOCKET and optionally the \a local parameter can be
  260. * filled with the desired address and port where the new socket
  261. * should be bound to, or 2) to specify its own socket to be used
  262. * by this transport, by specifying a valid socket in \a sock argument
  263. * and set the \a local argument to NULL. In both cases, application
  264. * may specify the published address of the socket in \a a_name
  265. * argument.
  266. *
  267. * Note that prior to calling this method, any locks acquired need to
  268. * be released temporarily to avoid any deadlock scenario.
  269. * This method will loop to wait for read operation to finish before actually
  270. * restart the transport. This might lead to deadlock if any other thread with
  271. * the read operation tries to acquire the same lock held by the thread calling
  272. * this method.
  273. * Please see https://github.com/pjsip/pjproject/pull/2731 for more details.
  274. *
  275. * @param transport The UDP transport.
  276. * @param option Restart option.
  277. * @param sock Optional socket to be used by the transport.
  278. * @param local The address where the socket should be bound to.
  279. * If this argument is NULL, socket will be bound
  280. * to any available port.
  281. * @param a_name Optionally specify the published address for
  282. * this transport. If the socket is not replaced
  283. * (PJSIP_UDP_TRANSPORT_KEEP_SOCKET flag is
  284. * specified), then if this argument is NULL, the
  285. * previous value will be used. If the socket is
  286. * replaced and this argument is NULL, the bound
  287. * address will be used as the published address
  288. * of the transport.
  289. *
  290. * @return PJ_SUCCESS if transport can be restarted, or
  291. * the appropriate error code.
  292. */
  293. PJ_DECL(pj_status_t) pjsip_udp_transport_restart(pjsip_transport *transport,
  294. unsigned option,
  295. pj_sock_t sock,
  296. const pj_sockaddr_in *local,
  297. const pjsip_host_port *a_name);
  298. /**
  299. * Restart the transport. Several operations are supported by this function:
  300. * - if transport was made temporarily unavailable to SIP stack with
  301. * pjsip_udp_transport_pause() and PJSIP_UDP_TRANSPORT_KEEP_SOCKET,
  302. * application can make the transport available to the SIP stack
  303. * again, by specifying PJSIP_UDP_TRANSPORT_KEEP_SOCKET flag here.
  304. * - if application wants to replace the internal socket with a new
  305. * socket, it must specify PJSIP_UDP_TRANSPORT_DESTROY_SOCKET when
  306. * calling this function, so that the internal socket will be destroyed
  307. * if it hasn't been closed. In this case, application has two choices
  308. * on how to create the new socket: 1) to let the transport create
  309. * the new socket, in this case the \a sock option should be set
  310. * to \a PJ_INVALID_SOCKET and optionally the \a local parameter can be
  311. * filled with the desired address and port where the new socket
  312. * should be bound to, or 2) to specify its own socket to be used
  313. * by this transport, by specifying a valid socket in \a sock argument
  314. * and set the \a local argument to NULL. In both cases, application
  315. * may specify the published address of the socket in \a a_name
  316. * argument. This is another version of pjsip_udp_transport_restart()
  317. * able to restart IPv6 transport.
  318. *
  319. * Note that prior to calling this method, any locks acquired need to
  320. * be released temporarily to avoid any deadlock scenario.
  321. * This method will loop to wait for read operation to finish before actually
  322. * restart the transport. This might lead to deadlock if any other thread with
  323. * the read operation tries to acquire the same lock held by the thread calling
  324. * this method.
  325. * Please see https://github.com/pjsip/pjproject/pull/2731 for more details.
  326. *
  327. * @param transport The UDP transport.
  328. * @param option Restart option.
  329. * @param sock Optional socket to be used by the transport.
  330. * @param local The address where the socket should be bound to.
  331. * If this argument is NULL, socket will be bound
  332. * to any available port.
  333. * @param a_name Optionally specify the published address for
  334. * this transport. If the socket is not replaced
  335. * (PJSIP_UDP_TRANSPORT_KEEP_SOCKET flag is
  336. * specified), then if this argument is NULL, the
  337. * previous value will be used. If the socket is
  338. * replaced and this argument is NULL, the bound
  339. * address will be used as the published address
  340. * of the transport.
  341. *
  342. * @return PJ_SUCCESS if transport can be restarted, or
  343. * the appropriate error code.
  344. */
  345. PJ_DECL(pj_status_t) pjsip_udp_transport_restart2(pjsip_transport *transport,
  346. unsigned option,
  347. pj_sock_t sock,
  348. const pj_sockaddr *local,
  349. const pjsip_host_port *a_name);
  350. PJ_END_DECL
  351. /**
  352. * @}
  353. */
  354. #endif /* __PJSIP_TRANSPORT_UDP_H__ */