sip_transport_tcp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_TCP_H__
  20. #define __PJSIP_TRANSPORT_TCP_H__
  21. /**
  22. * @file sip_transport_tcp.h
  23. * @brief SIP TCP Transport.
  24. */
  25. #include <pjsip/sip_transport.h>
  26. #include <pj/sock_qos.h>
  27. /* Only declare the API if PJ_HAS_TCP is true */
  28. #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
  29. PJ_BEGIN_DECL
  30. /**
  31. * @defgroup PJSIP_TRANSPORT_TCP TCP Transport
  32. * @ingroup PJSIP_TRANSPORT
  33. * @brief API to create and register TCP transport.
  34. * @{
  35. * The functions below are used to create TCP transport and register
  36. * the transport to the framework.
  37. */
  38. /**
  39. * Settings to be specified when creating the TCP transport. Application
  40. * should initialize this structure with its default values by calling
  41. * pjsip_tcp_transport_cfg_default().
  42. */
  43. typedef struct pjsip_tcp_transport_cfg
  44. {
  45. /**
  46. * Address family to use. Valid values are pj_AF_INET() and
  47. * pj_AF_INET6(). Default is pj_AF_INET().
  48. */
  49. int af;
  50. /**
  51. * Optional address to bind the socket to. Default is to bind to
  52. * PJ_INADDR_ANY and to any available port.
  53. */
  54. pj_sockaddr bind_addr;
  55. /**
  56. * Should SO_REUSEADDR be used for the listener socket.
  57. * Default value is PJSIP_TCP_TRANSPORT_REUSEADDR.
  58. */
  59. pj_bool_t reuse_addr;
  60. /**
  61. * Optional published address, which is the address to be
  62. * advertised as the address of this SIP transport.
  63. * It can be set using IP address or hostname.
  64. * By default the bound address will be used as the published address.
  65. */
  66. pjsip_host_port addr_name;
  67. /**
  68. * Number of simultaneous asynchronous accept() operations to be
  69. * supported. It is recommended that the number here corresponds to
  70. * the number of processors in the system (or the number of SIP
  71. * worker threads).
  72. *
  73. * Default: 1
  74. */
  75. unsigned async_cnt;
  76. /**
  77. * QoS traffic type to be set on this transport. When application wants
  78. * to apply QoS tagging to the transport, it's preferable to set this
  79. * field rather than \a qos_param fields since this is more portable.
  80. *
  81. * Default is QoS not set.
  82. */
  83. pj_qos_type qos_type;
  84. /**
  85. * Set the low level QoS parameters to the transport. This is a lower
  86. * level operation than setting the \a qos_type field and may not be
  87. * supported on all platforms.
  88. *
  89. * Default is QoS not set.
  90. */
  91. pj_qos_params qos_params;
  92. /**
  93. * Specify options to be set on the transport.
  94. *
  95. * By default there is no options.
  96. *
  97. */
  98. pj_sockopt_params sockopt_params;
  99. /**
  100. * Intial timeout interval to be applied to incoming transports
  101. * (i.e. server side) when no valid data received after a successful
  102. * connection.
  103. *
  104. * Default: PJSIP_TCP_INITIAL_TIMEOUT
  105. */
  106. unsigned initial_timeout;
  107. } pjsip_tcp_transport_cfg;
  108. /**
  109. * Initialize pjsip_tcp_transport_cfg structure with default values for
  110. * the specifed address family.
  111. *
  112. * @param cfg The structure to initialize.
  113. * @param af Address family to be used.
  114. */
  115. PJ_DECL(void) pjsip_tcp_transport_cfg_default(pjsip_tcp_transport_cfg *cfg,
  116. int af);
  117. /**
  118. * Register support for SIP TCP transport by creating TCP listener on
  119. * the specified address and port. This function will create an
  120. * instance of SIP TCP transport factory and register it to the
  121. * transport manager.
  122. *
  123. * @param endpt The SIP endpoint.
  124. * @param local Optional local address to bind, or specify the
  125. * address to bind the server socket to. Both IP
  126. * interface address and port fields are optional.
  127. * If IP interface address is not specified, socket
  128. * will be bound to PJ_INADDR_ANY. If port is not
  129. * specified, socket will be bound to any port
  130. * selected by the operating system.
  131. * @param async_cnt Number of simultaneous asynchronous accept()
  132. * operations to be supported. It is recommended that
  133. * the number here corresponds to the number of
  134. * processors in the system (or the number of SIP
  135. * worker threads).
  136. * @param p_factory Optional pointer to receive the instance of the
  137. * SIP TCP transport factory just created.
  138. *
  139. * @return PJ_SUCCESS when the transport has been successfully
  140. * started and registered to transport manager, or
  141. * the appropriate error code.
  142. */
  143. PJ_DECL(pj_status_t) pjsip_tcp_transport_start(pjsip_endpoint *endpt,
  144. const pj_sockaddr_in *local,
  145. unsigned async_cnt,
  146. pjsip_tpfactory **p_factory);
  147. /**
  148. * A newer variant of #pjsip_tcp_transport_start(), which allows specifying
  149. * the published/public address of the TCP transport.
  150. *
  151. * @param endpt The SIP endpoint.
  152. * @param local Optional local address to bind, or specify the
  153. * address to bind the server socket to. Both IP
  154. * interface address and port fields are optional.
  155. * If IP interface address is not specified, socket
  156. * will be bound to PJ_INADDR_ANY. If port is not
  157. * specified, socket will be bound to any port
  158. * selected by the operating system.
  159. * @param a_name Optional published address, which is the address to be
  160. * advertised as the address of this SIP transport.
  161. * It can be set using IP address or hostname.
  162. * If this argument is NULL, then the bound address
  163. * will be used as the published address.
  164. * @param async_cnt Number of simultaneous asynchronous accept()
  165. * operations to be supported. It is recommended that
  166. * the number here corresponds to the number of
  167. * processors in the system (or the number of SIP
  168. * worker threads).
  169. * @param p_factory Optional pointer to receive the instance of the
  170. * SIP TCP transport factory just created.
  171. *
  172. * @return PJ_SUCCESS when the transport has been successfully
  173. * started and registered to transport manager, or
  174. * the appropriate error code.
  175. */
  176. PJ_DECL(pj_status_t) pjsip_tcp_transport_start2(pjsip_endpoint *endpt,
  177. const pj_sockaddr_in *local,
  178. const pjsip_host_port *a_name,
  179. unsigned async_cnt,
  180. pjsip_tpfactory **p_factory);
  181. /**
  182. * Another variant of #pjsip_tcp_transport_start().
  183. *
  184. * @param endpt The SIP endpoint.
  185. * @param cfg TCP transport settings. Application should initialize
  186. * this setting with #pjsip_tcp_transport_cfg_default().
  187. * @param p_factory Optional pointer to receive the instance of the
  188. * SIP TCP transport factory just created.
  189. *
  190. * @return PJ_SUCCESS when the transport has been successfully
  191. * started and registered to transport manager, or
  192. * the appropriate error code.
  193. */
  194. PJ_DECL(pj_status_t) pjsip_tcp_transport_start3(
  195. pjsip_endpoint *endpt,
  196. const pjsip_tcp_transport_cfg *cfg,
  197. pjsip_tpfactory **p_factory
  198. );
  199. /**
  200. * Retrieve the internal socket handle used by the TCP transport. Note
  201. * that this socket normally is registered to ioqueue, so application
  202. * needs to take care not to perform operation that disrupts ioqueue
  203. * operation.
  204. *
  205. * @param transport The TCP transport.
  206. *
  207. * @return The socket handle, or PJ_INVALID_SOCKET if no socket
  208. * is currently being used.
  209. */
  210. PJ_DECL(pj_sock_t) pjsip_tcp_transport_get_socket(pjsip_transport *transport);
  211. /**
  212. * Start the TCP listener, if the listener is not started yet. This is useful
  213. * to start the listener manually, if listener was not started when
  214. * PJSIP_TCP_TRANSPORT_DONT_CREATE_LISTENER is set to 0.
  215. *
  216. * @param factory The SIP TCP transport factory.
  217. *
  218. * @param local The address where the listener should be bound to.
  219. * Both IP interface address and port fields are optional.
  220. * If IP interface address is not specified, socket
  221. * will be bound to PJ_INADDR_ANY. If port is not
  222. * specified, socket will be bound to any port
  223. * selected by the operating system.
  224. *
  225. * @param a_name The published address for the listener.
  226. * It can be set using IP address or hostname.
  227. * If this argument is NULL, then the bound address will
  228. * be used as the published address.
  229. *
  230. * @return PJ_SUCCESS when the listener has been successfully
  231. * started.
  232. */
  233. PJ_DECL(pj_status_t) pjsip_tcp_transport_lis_start(pjsip_tpfactory *factory,
  234. const pj_sockaddr *local,
  235. const pjsip_host_port *a_name);
  236. /**
  237. * Restart the TCP listener. This will close the listener socket and recreate
  238. * the socket based on the config used when starting the transport.
  239. *
  240. * @param factory The SIP TCP transport factory.
  241. *
  242. * @param local The address where the listener should be bound to.
  243. * Both IP interface address and port fields are optional.
  244. * If IP interface address is not specified, socket
  245. * will be bound to PJ_INADDR_ANY. If port is not
  246. * specified, socket will be bound to any port
  247. * selected by the operating system.
  248. *
  249. * @param a_name The published address for the listener.
  250. * It can be set using IP address or hostname.
  251. * If this argument is NULL, then the bound address will
  252. * be used as the published address.
  253. *
  254. * @return PJ_SUCCESS when the listener has been successfully
  255. * restarted.
  256. *
  257. */
  258. PJ_DECL(pj_status_t) pjsip_tcp_transport_restart(pjsip_tpfactory *factory,
  259. const pj_sockaddr *local,
  260. const pjsip_host_port *a_name);
  261. PJ_END_DECL
  262. /**
  263. * @}
  264. */
  265. #endif /* PJ_HAS_TCP */
  266. #endif /* __PJSIP_TRANSPORT_TCP_H__ */