sip_transport.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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_SIP_TRANSPORT_H__
  20. #define __PJSIP_SIP_TRANSPORT_H__
  21. /**
  22. * @file sip_transport.h
  23. * @brief SIP Transport
  24. */
  25. #include <pjsip/sip_msg.h>
  26. #include <pjsip/sip_parser.h>
  27. #include <pjsip/sip_resolve.h>
  28. #include <pj/sock.h>
  29. #include <pj/list.h>
  30. #include <pj/ioqueue.h>
  31. #include <pj/timer.h>
  32. PJ_BEGIN_DECL
  33. /**
  34. * @defgroup PJSIP_TRANSPORT Transport
  35. * @ingroup PJSIP_CORE
  36. * @brief This is the transport framework.
  37. *
  38. * The transport framework is fully extensible. Please see
  39. * <A HREF="/docs.htm">PJSIP Developer's Guide</A> PDF
  40. * document for more information.
  41. *
  42. * Application MUST register at least one transport to PJSIP before any
  43. * messages can be sent or received. Please see @ref PJSIP_TRANSPORT_UDP
  44. * on how to create/register UDP transport to the transport framework.
  45. *
  46. * @{
  47. */
  48. /*****************************************************************************
  49. *
  50. * GENERAL TRANSPORT (NAMES, TYPES, ETC.)
  51. *
  52. *****************************************************************************/
  53. /*
  54. * Forward declaration for transport factory (since it is referenced by
  55. * the transport factory itself).
  56. */
  57. typedef struct pjsip_tpfactory pjsip_tpfactory;
  58. /**
  59. * Flags for SIP transports.
  60. */
  61. enum pjsip_transport_flags_e
  62. {
  63. PJSIP_TRANSPORT_RELIABLE = 1, /**< Transport is reliable. */
  64. PJSIP_TRANSPORT_SECURE = 2, /**< Transport is secure. */
  65. PJSIP_TRANSPORT_DATAGRAM = 4 /**< Datagram based transport.
  66. (it's also assumed to be
  67. connectionless) */
  68. };
  69. /**
  70. * Check if transport tp is reliable.
  71. */
  72. #define PJSIP_TRANSPORT_IS_RELIABLE(tp) \
  73. ((tp)->flag & PJSIP_TRANSPORT_RELIABLE)
  74. /**
  75. * Check if transport tp is secure.
  76. */
  77. #define PJSIP_TRANSPORT_IS_SECURE(tp) \
  78. ((tp)->flag & PJSIP_TRANSPORT_SECURE)
  79. /**
  80. * Register new transport type to PJSIP. The PJSIP transport framework
  81. * contains the info for some standard transports, as declared by
  82. * #pjsip_transport_type_e. Application may use non-standard transport
  83. * with PJSIP, but before it does so, it must register the information
  84. * about the new transport type to PJSIP by calling this function.
  85. *
  86. * @param tp_flag The flags describing characteristics of this
  87. * transport type.
  88. * @param tp_name Transport type name.
  89. * @param def_port Default port to be used for the transport.
  90. * @param p_tp_type On successful registration, it will be filled with
  91. * the registered type. This argument is optional.
  92. *
  93. * @return PJ_SUCCESS if registration is successful, or
  94. * PJSIP_ETYPEEXISTS if the same transport type has
  95. * already been registered.
  96. */
  97. PJ_DECL(pj_status_t) pjsip_transport_register_type(unsigned tp_flag,
  98. const char *tp_name,
  99. int def_port,
  100. int *p_tp_type);
  101. /**
  102. * Get the transport type from the transport name.
  103. *
  104. * @param name Transport name, such as "TCP", or "UDP".
  105. *
  106. * @return The transport type, or PJSIP_TRANSPORT_UNSPECIFIED if
  107. * the name is not recognized as the name of supported
  108. * transport.
  109. */
  110. PJ_DECL(pjsip_transport_type_e)
  111. pjsip_transport_get_type_from_name(const pj_str_t *name);
  112. /**
  113. * Get the first transport type that has the specified flags.
  114. *
  115. * @param flag The transport flag.
  116. *
  117. * @return Transport type.
  118. */
  119. PJ_DECL(pjsip_transport_type_e)
  120. pjsip_transport_get_type_from_flag(unsigned flag);
  121. /**
  122. * Get the socket address family of a given transport type.
  123. *
  124. * @param type Transport type.
  125. *
  126. * @return Transport type.
  127. */
  128. PJ_DECL(int) pjsip_transport_type_get_af(pjsip_transport_type_e type);
  129. /**
  130. * Get transport flag from type.
  131. *
  132. * @param type Transport type.
  133. *
  134. * @return Transport flags.
  135. */
  136. PJ_DECL(unsigned)
  137. pjsip_transport_get_flag_from_type( pjsip_transport_type_e type );
  138. /**
  139. * Get the default SIP port number for the specified type.
  140. *
  141. * @param type Transport type.
  142. *
  143. * @return The port number, which is the default SIP port number for
  144. * the specified type.
  145. */
  146. PJ_DECL(int)
  147. pjsip_transport_get_default_port_for_type(pjsip_transport_type_e type);
  148. /**
  149. * Get transport type name.
  150. *
  151. * @param t Transport type.
  152. *
  153. * @return Transport name.
  154. */
  155. PJ_DECL(const char*) pjsip_transport_get_type_name(pjsip_transport_type_e t);
  156. /**
  157. * Get longer description for the specified transport type.
  158. *
  159. * @param t Transport type.
  160. *
  161. * @return Transport description.
  162. */
  163. PJ_DECL(const char*) pjsip_transport_get_type_desc(pjsip_transport_type_e t);
  164. /*****************************************************************************
  165. *
  166. * TRANSPORT SELECTOR.
  167. *
  168. *****************************************************************************/
  169. /**
  170. * This structure describes the type of data in pjsip_tpselector.
  171. */
  172. typedef enum pjsip_tpselector_type
  173. {
  174. /** Transport is not specified. */
  175. PJSIP_TPSELECTOR_NONE,
  176. /** Use the specific transport to send request. */
  177. PJSIP_TPSELECTOR_TRANSPORT,
  178. /** Use the specific listener to send request. */
  179. PJSIP_TPSELECTOR_LISTENER,
  180. /** Use the IP version criteria to send request. */
  181. PJSIP_TPSELECTOR_IP_VER,
  182. } pjsip_tpselector_type;
  183. /**
  184. * This enumerator describes the IP version criteria for pjsip_tpselector.
  185. */
  186. typedef enum pjsip_tpselector_ip_ver
  187. {
  188. /** IPv4 only. */
  189. PJSIP_TPSELECTOR_USE_IPV4_ONLY,
  190. /**
  191. * No preference. IP version used will depend on the order of addresses
  192. * returned by pjsip_resolver.
  193. */
  194. PJSIP_TPSELECTOR_NO_PREFERENCE,
  195. /** IPv4 is preferred. */
  196. PJSIP_TPSELECTOR_PREFER_IPV4,
  197. /** IPv6 is preferred. */
  198. PJSIP_TPSELECTOR_PREFER_IPV6,
  199. /** IPv6 only. */
  200. PJSIP_TPSELECTOR_USE_IPV6_ONLY
  201. } pjsip_tpselector_ip_ver;
  202. /**
  203. * This structure describes the transport/listener preference to be used
  204. * when sending outgoing requests.
  205. *
  206. * Normally transport will be selected automatically according to rules about
  207. * sending requests. But some applications (such as proxies or B2BUAs) may
  208. * want to explicitly use specific transport to send requests, for example
  209. * when they want to make sure that outgoing request should go from a specific
  210. * network interface.
  211. *
  212. * The pjsip_tpselector structure is used for that purpose, i.e. to allow
  213. * application specificly request that a particular transport/listener
  214. * should be used to send request. This structure is used when calling
  215. * pjsip_tsx_set_transport() and pjsip_dlg_set_transport().
  216. *
  217. * If application disables connection reuse and wants to force creating
  218. * a new transport, it needs to consider the following couple of things:
  219. * - If it still wants to reuse an existing transport (if any), it
  220. * needs to keep a reference to that transport and specifically set
  221. * the transport to be used for sending requests.
  222. * - Delete those existing transports manually when no longer needed.
  223. */
  224. typedef struct pjsip_tpselector
  225. {
  226. /** The type of data in the union */
  227. pjsip_tpselector_type type;
  228. /**
  229. * Whether to disable reuse of an existing connection.
  230. * This setting will be ignored if (type == PJSIP_TPSELECTOR_TRANSPORT)
  231. * and transport in the union below is set.
  232. */
  233. pj_bool_t disable_connection_reuse;
  234. /**
  235. * Union representing the transport/listener/IP version criteria
  236. * to be used.
  237. */
  238. union {
  239. pjsip_transport *transport;
  240. pjsip_tpfactory *listener;
  241. pjsip_tpselector_ip_ver ip_ver;
  242. void *ptr;
  243. } u;
  244. } pjsip_tpselector;
  245. /**
  246. * Add transport/listener reference in the selector to prevent the specified
  247. * transport/listener from being destroyed while application still has
  248. * reference to it.
  249. *
  250. * @param sel The transport selector.
  251. */
  252. PJ_DECL(void) pjsip_tpselector_add_ref(pjsip_tpselector *sel);
  253. /**
  254. * Decrement transport/listener reference in the selector.
  255. * @param sel The transport selector
  256. */
  257. PJ_DECL(void) pjsip_tpselector_dec_ref(pjsip_tpselector *sel);
  258. /*****************************************************************************
  259. *
  260. * RECEIVE DATA BUFFER.
  261. *
  262. *****************************************************************************/
  263. /**
  264. * A customized ioqueue async operation key which is used by transport
  265. * to locate rdata when a pending read operation completes.
  266. */
  267. typedef struct pjsip_rx_data_op_key
  268. {
  269. pj_ioqueue_op_key_t op_key; /**< ioqueue op_key. */
  270. pjsip_rx_data *rdata; /**< rdata associated with this */
  271. } pjsip_rx_data_op_key;
  272. /**
  273. * Incoming message buffer.
  274. * This structure keep all the information regarding the received message. This
  275. * buffer lifetime is only very short, normally after the transaction has been
  276. * called, this buffer will be deleted/recycled. So care must be taken when
  277. * allocating storage from the pool of this buffer.
  278. */
  279. struct pjsip_rx_data
  280. {
  281. /**
  282. * tp_info is part of rdata that remains static for the duration of the
  283. * buffer. It is initialized when the buffer was created by transport.
  284. */
  285. struct
  286. {
  287. /** Memory pool for this buffer. */
  288. pj_pool_t *pool;
  289. /** The transport object which received this packet. */
  290. pjsip_transport *transport;
  291. /** Other transport specific data to be attached to this buffer. */
  292. void *tp_data;
  293. /** Ioqueue key. */
  294. pjsip_rx_data_op_key op_key;
  295. } tp_info;
  296. /**
  297. * pkt_info is initialized by transport when it receives an incoming
  298. * packet.
  299. */
  300. struct
  301. {
  302. /** Time when the message was received. */
  303. pj_time_val timestamp;
  304. /** Pointer to the original packet. */
  305. char packet[PJSIP_MAX_PKT_LEN];
  306. /** Zero termination for the packet. */
  307. pj_uint32_t zero;
  308. /** The length of the packet received. */
  309. pj_ssize_t len;
  310. /** The source address from which the packet was received. */
  311. pj_sockaddr src_addr;
  312. /** The length of the source address. */
  313. int src_addr_len;
  314. /** The IP source address string (NULL terminated). */
  315. char src_name[PJ_INET6_ADDRSTRLEN];
  316. /** The IP source port number. */
  317. int src_port;
  318. } pkt_info;
  319. /**
  320. * msg_info is initialized by transport mgr (tpmgr) before this buffer
  321. * is passed to endpoint.
  322. */
  323. struct
  324. {
  325. /** Start of msg buffer. */
  326. char *msg_buf;
  327. /** Length fo message. */
  328. int len;
  329. /** The parsed message, if any. */
  330. pjsip_msg *msg;
  331. /** Short description about the message.
  332. * Application should use #pjsip_rx_data_get_info() instead.
  333. */
  334. char *info;
  335. /** The Call-ID header as found in the message. */
  336. pjsip_cid_hdr *cid;
  337. /** The From header as found in the message. */
  338. pjsip_from_hdr *from;
  339. /** The To header as found in the message. */
  340. pjsip_to_hdr *to;
  341. /** The topmost Via header as found in the message. */
  342. pjsip_via_hdr *via;
  343. /** The CSeq header as found in the message. */
  344. pjsip_cseq_hdr *cseq;
  345. /** Max forwards header. */
  346. pjsip_max_fwd_hdr *max_fwd;
  347. /** The first route header. */
  348. pjsip_route_hdr *route;
  349. /** The first record-route header. */
  350. pjsip_rr_hdr *record_route;
  351. /** Content-type header. */
  352. pjsip_ctype_hdr *ctype;
  353. /** Content-length header. */
  354. pjsip_clen_hdr *clen;
  355. /** "Require" header containing aggregates of all Require
  356. * headers found in the message, or NULL.
  357. */
  358. pjsip_require_hdr *require;
  359. /** "Supported" header containing aggregates of all Supported
  360. * headers found in the message, or NULL.
  361. */
  362. pjsip_supported_hdr *supported;
  363. /** The list of error generated by the parser when parsing
  364. this message.
  365. */
  366. pjsip_parser_err_report parse_err;
  367. } msg_info;
  368. /**
  369. * endpt_info is initialized by endpoint after this buffer reaches
  370. * endpoint.
  371. */
  372. struct
  373. {
  374. /**
  375. * Data attached by modules to this message.
  376. */
  377. void *mod_data[PJSIP_MAX_MODULE];
  378. } endpt_info;
  379. };
  380. /**
  381. * Get printable information about the message in the rdata.
  382. *
  383. * @param rdata The receive data buffer.
  384. *
  385. * @return Printable information.
  386. */
  387. PJ_DECL(char*) pjsip_rx_data_get_info(pjsip_rx_data *rdata);
  388. /**
  389. * Clone pjsip_rx_data. This will duplicate the contents of
  390. * pjsip_rx_data and add reference count to the transport.
  391. * Once application has finished using the cloned pjsip_rx_data,
  392. * it must release it by calling #pjsip_rx_data_free_cloned().
  393. *
  394. * By default (if flags is set to zero), this function copies the
  395. * transport pointer in \a tp_info, duplicates the \a pkt_info,
  396. * perform deep clone of the \a msg_info parts of the rdata, and
  397. * fills the \a endpt_info (i.e. the \a mod_data) with zeros.
  398. *
  399. * @param src The source to be cloned.
  400. * @param flags Optional flags. Must be zero for now.
  401. * @param p_rdata Pointer to receive the cloned rdata.
  402. *
  403. * @return PJ_SUCCESS on success or the appropriate error.
  404. */
  405. PJ_DECL(pj_status_t) pjsip_rx_data_clone(const pjsip_rx_data *src,
  406. unsigned flags,
  407. pjsip_rx_data **p_rdata);
  408. /**
  409. * Free cloned pjsip_rx_data. This function must be and must only
  410. * be called for a cloned pjsip_rx_data. Specifically, it must NOT
  411. * be called for the original pjsip_rx_data that is returned by
  412. * transports.
  413. *
  414. * This function will free the memory used by the pjsip_rx_data and
  415. * decrement the transport reference counter.
  416. *
  417. * @param rdata The receive data buffer.
  418. *
  419. * @return PJ_SUCCESS on success or the appropriate error.
  420. */
  421. PJ_DECL(pj_status_t) pjsip_rx_data_free_cloned(pjsip_rx_data *rdata);
  422. /*****************************************************************************
  423. *
  424. * TRANSMIT DATA BUFFER MANIPULATION.
  425. *
  426. *****************************************************************************/
  427. /** Customized ioqueue async operation key, used by transport to keep
  428. * callback parameters.
  429. */
  430. typedef struct pjsip_tx_data_op_key
  431. {
  432. /** ioqueue pending operation key. */
  433. pj_ioqueue_op_key_t key;
  434. /** Transmit data associated with this key. */
  435. pjsip_tx_data *tdata;
  436. /** Arbitrary token (attached by transport) */
  437. void *token;
  438. /** Callback to be called when pending transmit operation has
  439. completed.
  440. */
  441. void (*callback)(pjsip_transport*,void*,pj_ssize_t);
  442. } pjsip_tx_data_op_key;
  443. /**
  444. * Data structure for sending outgoing message. Application normally creates
  445. * this buffer by calling #pjsip_endpt_create_tdata.
  446. *
  447. * The lifetime of this buffer is controlled by the reference counter in this
  448. * structure, which is manipulated by calling #pjsip_tx_data_add_ref and
  449. * #pjsip_tx_data_dec_ref. When the reference counter has reached zero, then
  450. * this buffer will be destroyed.
  451. *
  452. * A transaction object normally will add reference counter to this buffer
  453. * when application calls #pjsip_tsx_send_msg, because it needs to keep the
  454. * message for retransmission. The transaction will release the reference
  455. * counter once its state has reached final state.
  456. */
  457. struct pjsip_tx_data
  458. {
  459. /** This is for transmission queue; it's managed by transports. */
  460. PJ_DECL_LIST_MEMBER(struct pjsip_tx_data);
  461. /** Memory pool for this buffer. */
  462. pj_pool_t *pool;
  463. /** A name to identify this buffer. */
  464. char obj_name[PJ_MAX_OBJ_NAME];
  465. /** Short information describing this buffer and the message in it.
  466. * Application should use #pjsip_tx_data_get_info() instead of
  467. * directly accessing this member.
  468. */
  469. char *info;
  470. /** For response message, this contains the reference to timestamp when
  471. * the original request message was received. The value of this field
  472. * is set when application creates response message to a request by
  473. * calling #pjsip_endpt_create_response.
  474. */
  475. pj_time_val rx_timestamp;
  476. /** The transport manager for this buffer. */
  477. pjsip_tpmgr *mgr;
  478. /** Ioqueue asynchronous operation key. */
  479. pjsip_tx_data_op_key op_key;
  480. /** Lock object. */
  481. pj_lock_t *lock;
  482. /** The message in this buffer. */
  483. pjsip_msg *msg;
  484. /** Strict route header saved by #pjsip_process_route_set(), to be
  485. * restored by #pjsip_restore_strict_route_set().
  486. */
  487. pjsip_route_hdr *saved_strict_route;
  488. /** Buffer to the printed text representation of the message. When the
  489. * content of this buffer is set, then the transport will send the content
  490. * of this buffer instead of re-printing the message structure. If the
  491. * message structure has changed, then application must invalidate this
  492. * buffer by calling #pjsip_tx_data_invalidate_msg.
  493. */
  494. pjsip_buffer buf;
  495. /** Reference counter. */
  496. pj_atomic_t *ref_cnt;
  497. /** Being processed by transport? */
  498. int is_pending;
  499. /** Transport manager internal. */
  500. void *token;
  501. /** Callback to be called when this tx_data has been transmitted. */
  502. void (*cb)(void*, pjsip_tx_data*, pj_ssize_t);
  503. /** Destination information, to be used to determine the network address
  504. * of the message. For a request, this information is initialized when
  505. * the request is sent with #pjsip_endpt_send_request_stateless() and
  506. * network address is resolved. For CANCEL request, this information
  507. * will be copied from the original INVITE to make sure that the CANCEL
  508. * request goes to the same physical network address as the INVITE
  509. * request.
  510. */
  511. struct
  512. {
  513. /** Server name.
  514. */
  515. pj_str_t name;
  516. /** Server addresses resolved.
  517. */
  518. pjsip_server_addresses addr;
  519. /** Current server address being tried.
  520. */
  521. unsigned cur_addr;
  522. } dest_info;
  523. /** Transport information, only valid during on_tx_request() and
  524. * on_tx_response() callback.
  525. */
  526. struct
  527. {
  528. pjsip_transport *transport; /**< Transport being used. */
  529. pj_sockaddr dst_addr; /**< Destination address. */
  530. int dst_addr_len; /**< Length of address. */
  531. char dst_name[PJ_INET6_ADDRSTRLEN]; /**< Destination address. */
  532. int dst_port; /**< Destination port. */
  533. } tp_info;
  534. /**
  535. * Transport selector, to specify which transport to be used.
  536. * The value here must be set with pjsip_tx_data_set_transport(),
  537. * to allow reference counter to be set properly.
  538. */
  539. pjsip_tpselector tp_sel;
  540. /**
  541. * Special flag to indicate that this transmit data is a request that has
  542. * been updated with proper authentication response and is ready to be
  543. * sent for retry.
  544. */
  545. pj_bool_t auth_retry;
  546. /**
  547. * Arbitrary data attached by PJSIP modules.
  548. */
  549. void *mod_data[PJSIP_MAX_MODULE];
  550. /**
  551. * If via_addr is set, it will be used as the "sent-by" field of the
  552. * Via header for outgoing requests as long as the request uses via_tp
  553. * transport. Normally application should not use or access these fields.
  554. */
  555. pjsip_host_port via_addr; /**< Via address. */
  556. const void *via_tp; /**< Via transport. */
  557. };
  558. /**
  559. * Create a new, blank transmit buffer. The reference count is initialized
  560. * to zero.
  561. *
  562. * @param mgr The transport manager.
  563. * @param tdata Pointer to receive transmit data.
  564. *
  565. * @return PJ_SUCCESS, or the appropriate error code.
  566. *
  567. * @see pjsip_endpt_create_tdata
  568. */
  569. PJ_DECL(pj_status_t) pjsip_tx_data_create( pjsip_tpmgr *mgr,
  570. pjsip_tx_data **tdata );
  571. /**
  572. * Add reference counter to the transmit buffer. The reference counter controls
  573. * the life time of the buffer, ie. when the counter reaches zero, then it
  574. * will be destroyed.
  575. *
  576. * @param tdata The transmit buffer.
  577. */
  578. PJ_DECL(void) pjsip_tx_data_add_ref( pjsip_tx_data *tdata );
  579. /**
  580. * Decrement reference counter of the transmit buffer.
  581. * When the transmit buffer is no longer used, it will be destroyed and
  582. * caller is informed with PJSIP_EBUFDESTROYED return status.
  583. *
  584. * @param tdata The transmit buffer data.
  585. * @return This function will always succeeded eventhough the return
  586. * status is non-zero. A status PJSIP_EBUFDESTROYED will be
  587. * returned to inform that buffer is destroyed.
  588. */
  589. PJ_DECL(pj_status_t) pjsip_tx_data_dec_ref( pjsip_tx_data *tdata );
  590. /**
  591. * Print the SIP message to transmit data buffer's internal buffer. This
  592. * may allocate memory for the buffer, if the buffer has not been allocated
  593. * yet, and encode the SIP message to that buffer.
  594. *
  595. * @param tdata The transmit buffer.
  596. *
  597. * @return PJ_SUCCESS on success of the appropriate error code.
  598. */
  599. PJ_DECL(pj_status_t) pjsip_tx_data_encode(pjsip_tx_data *tdata);
  600. /**
  601. * Check if transmit data buffer contains a valid message.
  602. *
  603. * @param tdata The transmit buffer.
  604. * @return Non-zero (PJ_TRUE) if buffer contains a valid message.
  605. */
  606. PJ_DECL(pj_bool_t) pjsip_tx_data_is_valid( pjsip_tx_data *tdata );
  607. /**
  608. * Invalidate the print buffer to force message to be re-printed. Call
  609. * when the message has changed after it has been printed to buffer. The
  610. * message is printed to buffer normally by transport when it is about to be
  611. * sent to the wire. Subsequent sending of the message will not cause
  612. * the message to be re-printed, unless application invalidates the buffer
  613. * by calling this function.
  614. *
  615. * @param tdata The transmit buffer.
  616. */
  617. PJ_DECL(void) pjsip_tx_data_invalidate_msg( pjsip_tx_data *tdata );
  618. /**
  619. * Get short printable info about the transmit data. This will normally return
  620. * short information about the message.
  621. *
  622. * @param tdata The transmit buffer.
  623. *
  624. * @return Null terminated info string.
  625. */
  626. PJ_DECL(char*) pjsip_tx_data_get_info( pjsip_tx_data *tdata );
  627. /**
  628. * Set the explicit transport to be used when sending this transmit data.
  629. * Application should not need to call this function, but rather use
  630. * pjsip_tsx_set_transport() and pjsip_dlg_set_transport() instead (which
  631. * will call this function).
  632. *
  633. * @param tdata The transmit buffer.
  634. * @param sel Transport selector.
  635. *
  636. * @return PJ_SUCCESS on success.
  637. */
  638. PJ_DECL(pj_status_t) pjsip_tx_data_set_transport(pjsip_tx_data *tdata,
  639. const pjsip_tpselector *sel);
  640. /**
  641. * Clone pjsip_tx_data. This will duplicate the message contents of
  642. * pjsip_tx_data (pjsip_tx_data.msg) and add reference count to the tdata.
  643. * Once application has finished using the cloned pjsip_tx_data,
  644. * it must release it by calling #pjsip_tx_data_dec_ref().
  645. * Currently, this will only clone response message.
  646. *
  647. * @param src The source to be cloned.
  648. * @param flags Optional flags. Must be zero for now.
  649. * @param p_rdata Pointer to receive the cloned tdata.
  650. *
  651. * @return PJ_SUCCESS on success or the appropriate error.
  652. */
  653. PJ_DECL(pj_status_t) pjsip_tx_data_clone(const pjsip_tx_data *src,
  654. unsigned flags,
  655. pjsip_tx_data **p_rdata);
  656. /*****************************************************************************
  657. *
  658. * TRANSPORT
  659. *
  660. *****************************************************************************/
  661. /**
  662. * Type of callback to receive transport operation status.
  663. */
  664. typedef void (*pjsip_transport_callback)(pjsip_transport *tp, void *token,
  665. pj_ssize_t sent_bytes);
  666. /**
  667. * This structure describes transport key to be registered to hash table.
  668. */
  669. typedef struct pjsip_transport_key
  670. {
  671. /**
  672. * Transport type.
  673. */
  674. long type;
  675. /**
  676. * Destination address.
  677. */
  678. pj_sockaddr rem_addr;
  679. } pjsip_transport_key;
  680. /**
  681. * Enumeration of transport direction types.
  682. */
  683. typedef enum pjsip_transport_dir
  684. {
  685. PJSIP_TP_DIR_NONE, /**< Direction not set, normally used by
  686. connectionless transports such as
  687. UDP transport. */
  688. PJSIP_TP_DIR_OUTGOING, /**< Outgoing connection or client mode,
  689. this is only for connection-oriented
  690. transports. */
  691. PJSIP_TP_DIR_INCOMING, /**< Incoming connection or server mode,
  692. this is only for connection-oriented
  693. transports. */
  694. } pjsip_transport_dir;
  695. /**
  696. * This structure represent the "public" interface of a SIP transport.
  697. * Applications normally extend this structure to include transport
  698. * specific members.
  699. */
  700. struct pjsip_transport
  701. {
  702. char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
  703. pj_pool_t *pool; /**< Pool used by transport. */
  704. pj_atomic_t *ref_cnt; /**< Reference counter. */
  705. pj_lock_t *lock; /**< Lock object. */
  706. pj_grp_lock_t *grp_lock; /**< Group lock for sync with
  707. ioqueue and timer. */
  708. pj_bool_t tracing; /**< Tracing enabled? */
  709. pj_bool_t is_shutdown; /**< Being shutdown? */
  710. pj_bool_t is_destroying; /**< Destroy in progress? */
  711. /** Key for indexing this transport in hash table. */
  712. pjsip_transport_key key;
  713. char *type_name; /**< Type name. */
  714. unsigned flag; /**< #pjsip_transport_flags_e */
  715. char *info; /**< Transport info/description.*/
  716. int addr_len; /**< Length of addresses. */
  717. pj_sockaddr local_addr; /**< Bound address. */
  718. pjsip_host_port local_name; /**< Published name (eg. STUN). */
  719. pjsip_host_port remote_name; /**< Remote address name. */
  720. pjsip_transport_dir dir; /**< Connection direction. */
  721. pjsip_endpoint *endpt; /**< Endpoint instance. */
  722. pjsip_tpmgr *tpmgr; /**< Transport manager. */
  723. pjsip_tpfactory *factory; /**< Factory instance. Note: it
  724. may be invalid/shutdown. */
  725. pj_timer_entry idle_timer; /**< Timer when ref cnt is zero.*/
  726. pj_timestamp last_recv_ts; /**< Last time receiving data. */
  727. pj_size_t last_recv_len; /**< Last received data length. */
  728. void *data; /**< Internal transport data. */
  729. unsigned initial_timeout;/**< Initial timeout interval
  730. to be applied to incoming
  731. TCP/TLS transports when no
  732. valid data received after
  733. a successful connection. */
  734. /**
  735. * Function to be called by transport manager to send SIP message.
  736. *
  737. * @param transport The transport to send the message.
  738. * @param packet The buffer to send.
  739. * @param length The length of the buffer to send.
  740. * @param op_key Completion token, which will be supplied to
  741. * caller when pending send operation completes.
  742. * @param rem_addr The remote destination address.
  743. * @param addr_len Size of remote address.
  744. * @param callback If supplied, the callback will be called
  745. * once a pending transmission has completed. If
  746. * the function completes immediately (i.e. return
  747. * code is not PJ_EPENDING), the callback will not
  748. * be called.
  749. *
  750. * @return Should return PJ_SUCCESS only if data has been
  751. * succesfully queued to operating system for
  752. * transmission. Otherwise it may return PJ_EPENDING
  753. * if the underlying transport can not send the
  754. * data immediately and will send it later, which in
  755. * this case caller doesn't have to do anything
  756. * except wait the calback to be called, if it
  757. * supplies one.
  758. * Other return values indicate the error code.
  759. */
  760. pj_status_t (*send_msg)(pjsip_transport *transport,
  761. pjsip_tx_data *tdata,
  762. const pj_sockaddr_t *rem_addr,
  763. int addr_len,
  764. void *token,
  765. pjsip_transport_callback callback);
  766. /**
  767. * Instruct the transport to initiate graceful shutdown procedure.
  768. * After all objects release their reference to this transport,
  769. * the transport will be deleted.
  770. *
  771. * Note that application MUST use #pjsip_transport_shutdown() instead.
  772. *
  773. * @param transport The transport.
  774. *
  775. * @return PJ_SUCCESS on success.
  776. */
  777. pj_status_t (*do_shutdown)(pjsip_transport *transport);
  778. /**
  779. * Forcefully destroy this transport regardless whether there are
  780. * objects that currently use this transport. This function should only
  781. * be called by transport manager or other internal objects (such as the
  782. * transport itself) who know what they're doing. Application should use
  783. * #pjsip_transport_shutdown() instead.
  784. *
  785. * @param transport The transport.
  786. *
  787. * @return PJ_SUCCESS on success.
  788. */
  789. pj_status_t (*destroy)(pjsip_transport *transport);
  790. /*
  791. * Application may extend this structure..
  792. */
  793. };
  794. /**
  795. * Register a transport instance to the transport manager. This function
  796. * is normally called by the transport instance when it is created
  797. * by application.
  798. *
  799. * @param mgr The transport manager.
  800. * @param tp The new transport to be registered.
  801. *
  802. * @return PJ_SUCCESS on success.
  803. */
  804. PJ_DECL(pj_status_t) pjsip_transport_register( pjsip_tpmgr *mgr,
  805. pjsip_transport *tp );
  806. /**
  807. * Start graceful shutdown procedure for this transport. After graceful
  808. * shutdown has been initiated, no new reference can be obtained for
  809. * the transport. However, existing objects that currently uses the
  810. * transport may still use this transport to send and receive packets.
  811. *
  812. * After all objects release their reference to this transport,
  813. * the transport will be destroyed immediately.
  814. *
  815. * @param tp The transport.
  816. *
  817. * @return PJ_SUCCESS on success.
  818. */
  819. PJ_DECL(pj_status_t) pjsip_transport_shutdown(pjsip_transport *tp);
  820. /**
  821. * Start shutdown procedure for this transport. If \a force is false,
  822. * the API is the same as #pjsip_transport_shutdown(), while
  823. * if \a force is true, existing transport users will immediately
  824. * receive PJSIP_TP_STATE_DISCONNECTED notification and should not
  825. * use the transport anymore. In either case, transport will
  826. * only be destroyed after all objects release their references.
  827. *
  828. * @param tp The transport.
  829. * @param force Force transport to immediately send
  830. * disconnection state notification.
  831. *
  832. * @return PJ_SUCCESS on success.
  833. */
  834. PJ_DECL(pj_status_t) pjsip_transport_shutdown2(pjsip_transport *tp,
  835. pj_bool_t force);
  836. /**
  837. * Destroy a transport when there is no object currently uses the transport.
  838. * This function is normally called internally by transport manager or the
  839. * transport itself. Application should use #pjsip_transport_shutdown()
  840. * instead.
  841. *
  842. * @param tp The transport instance.
  843. *
  844. * @return PJ_SUCCESS on success or the appropriate error code.
  845. * Some of possible errors are PJSIP_EBUSY if the
  846. * transport's reference counter is not zero.
  847. */
  848. PJ_DECL(pj_status_t) pjsip_transport_destroy( pjsip_transport *tp);
  849. /**
  850. * Add reference counter to the specified transport. Any objects that wishes
  851. * to keep the reference of the transport MUST increment the transport's
  852. * reference counter to prevent it from being destroyed.
  853. *
  854. * @param tp The transport instance.
  855. *
  856. * @return PJ_SUCCESS on success or the appropriate error code.
  857. */
  858. PJ_DECL(pj_status_t) pjsip_transport_add_ref( pjsip_transport *tp );
  859. /**
  860. * Decrement reference counter of the specified transport. When an object no
  861. * longer want to keep the reference to the transport, it must decrement the
  862. * reference counter. When the reference counter of the transport reaches
  863. * zero, the transport manager will start the idle timer to destroy the
  864. * transport if no objects acquire the reference counter during the idle
  865. * interval.
  866. *
  867. * @param tp The transport instance.
  868. *
  869. * @return PJ_SUCCESS on success.
  870. */
  871. PJ_DECL(pj_status_t) pjsip_transport_dec_ref( pjsip_transport *tp );
  872. /**
  873. * This function is called by transport instances to report an incoming
  874. * packet to the transport manager. The transport manager then would try to
  875. * parse all SIP messages in the packet, and for each parsed SIP message, it
  876. * would report the message to the SIP endpoint (#pjsip_endpoint).
  877. *
  878. * @param mgr The transport manager instance.
  879. * @param rdata The receive data buffer containing the packet. The
  880. * transport MUST fully initialize tp_info and pkt_info
  881. * member of the rdata.
  882. *
  883. * @return The number of bytes successfully processed from the
  884. * packet. If the transport is datagram oriented, the
  885. * value will be equal to the size of the packet. For
  886. * stream oriented transport (e.g. TCP, TLS), the value
  887. * returned may be less than the packet size, if
  888. * partial message is received. The transport then MUST
  889. * keep the remainder part and report it again to
  890. * this function once more data/packet is received.
  891. */
  892. PJ_DECL(pj_ssize_t) pjsip_tpmgr_receive_packet(pjsip_tpmgr *mgr,
  893. pjsip_rx_data *rdata);
  894. /*****************************************************************************
  895. *
  896. * TRANSPORT FACTORY
  897. *
  898. *****************************************************************************/
  899. /**
  900. * A transport factory is normally used for connection oriented transports
  901. * (such as TCP or TLS) to create instances of transports. It registers
  902. * a new transport type to the transport manager, and the transport manager
  903. * would ask the factory to create a transport instance when it received
  904. * command from application to send a SIP message using the specified
  905. * transport type.
  906. */
  907. struct pjsip_tpfactory
  908. {
  909. /** This list is managed by transport manager. */
  910. PJ_DECL_LIST_MEMBER(struct pjsip_tpfactory);
  911. char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
  912. pj_pool_t *pool; /**< Owned memory pool. */
  913. pj_lock_t *lock; /**< Lock object. */
  914. pjsip_transport_type_e type; /**< Transport type. */
  915. char *type_name; /**< Type string name. */
  916. unsigned flag; /**< Transport flag. */
  917. char *info; /**< Transport info/description.*/
  918. pj_sockaddr local_addr; /**< Bound address. */
  919. pjsip_host_port addr_name; /**< Published name. */
  920. /**
  921. * Create new outbound connection suitable for sending SIP message
  922. * to specified remote address.
  923. * Note that the factory is responsible for both creating the
  924. * transport and registering it to the transport manager.
  925. */
  926. pj_status_t (*create_transport)(pjsip_tpfactory *factory,
  927. pjsip_tpmgr *mgr,
  928. pjsip_endpoint *endpt,
  929. const pj_sockaddr *rem_addr,
  930. int addr_len,
  931. pjsip_transport **transport);
  932. /**
  933. * Create new outbound connection suitable for sending SIP message
  934. * to specified remote address by also considering outgoing SIP
  935. * message data.
  936. * Note that the factory is responsible for both creating the
  937. * transport and registering it to the transport manager.
  938. */
  939. pj_status_t (*create_transport2)(pjsip_tpfactory *factory,
  940. pjsip_tpmgr *mgr,
  941. pjsip_endpoint *endpt,
  942. const pj_sockaddr *rem_addr,
  943. int addr_len,
  944. pjsip_tx_data *tdata,
  945. pjsip_transport **transport);
  946. /**
  947. * Destroy the listener.
  948. */
  949. pj_status_t (*destroy)(pjsip_tpfactory *factory);
  950. /*
  951. * Application may extend this structure..
  952. */
  953. };
  954. /**
  955. * Register a transport factory.
  956. *
  957. * @param mgr The transport manager.
  958. * @param tpf Transport factory.
  959. *
  960. * @return PJ_SUCCESS if listener was successfully created.
  961. */
  962. PJ_DECL(pj_status_t) pjsip_tpmgr_register_tpfactory(pjsip_tpmgr *mgr,
  963. pjsip_tpfactory *tpf);
  964. /**
  965. * Unregister factory.
  966. *
  967. * @param mgr The transport manager.
  968. * @param tpf Transport factory.
  969. *
  970. * @return PJ_SUCCESS is sucessfully unregistered.
  971. */
  972. PJ_DECL(pj_status_t) pjsip_tpmgr_unregister_tpfactory(pjsip_tpmgr *mgr,
  973. pjsip_tpfactory *tpf);
  974. /*****************************************************************************
  975. *
  976. * TRANSPORT MANAGER
  977. *
  978. *****************************************************************************/
  979. /**
  980. * Type of callback to be called when transport manager receives incoming
  981. * SIP message.
  982. *
  983. * @param ep Endpoint.
  984. * @param status Receiption status.
  985. * @param rd Received packet.
  986. */
  987. typedef void (*pjsip_rx_callback)(pjsip_endpoint *ep, pj_status_t status,
  988. pjsip_rx_data *rd);
  989. /**
  990. * Type of callback to be called before transport manager is about
  991. * to transmit SIP message.
  992. *
  993. * @param ep Endpoint.
  994. * @param td Transmit data.
  995. */
  996. typedef pj_status_t (*pjsip_tx_callback)(pjsip_endpoint *ep, pjsip_tx_data*td);
  997. /**
  998. * Create a transport manager. Normally application doesn't need to call
  999. * this function directly, since a transport manager will be created and
  1000. * destroyed automatically by the SIP endpoint.
  1001. *
  1002. * @param pool Pool.
  1003. * @param endpt Endpoint instance.
  1004. * @param rx_cb Callback to receive incoming message.
  1005. * @param tx_cb Callback to be called before transport manager is sending
  1006. * outgoing message.
  1007. * @param p_mgr Pointer to receive the new transport manager.
  1008. *
  1009. * @return PJ_SUCCESS or the appropriate error code on error.
  1010. */
  1011. PJ_DECL(pj_status_t) pjsip_tpmgr_create( pj_pool_t *pool,
  1012. pjsip_endpoint * endpt,
  1013. pjsip_rx_callback rx_cb,
  1014. pjsip_tx_callback tx_cb,
  1015. pjsip_tpmgr **p_mgr);
  1016. /**
  1017. * Find out the appropriate local address info (IP address and port) to
  1018. * advertise in Contact header based on the remote address to be
  1019. * contacted. The local address info would be the address name of the
  1020. * transport or listener which will be used to send the request.
  1021. *
  1022. * In this implementation, it will only select the transport based on
  1023. * the transport type in the request.
  1024. *
  1025. * @see pjsip_tpmgr_find_local_addr2()
  1026. *
  1027. * @param tpmgr The transport manager.
  1028. * @param pool Pool to allocate memory for the IP address.
  1029. * @param type Destination address to contact.
  1030. * @param sel Optional pointer to prefered transport, if any.
  1031. * @param ip_addr Pointer to receive the IP address.
  1032. * @param port Pointer to receive the port number.
  1033. *
  1034. * @return PJ_SUCCESS, or the appropriate error code.
  1035. */
  1036. PJ_DECL(pj_status_t) pjsip_tpmgr_find_local_addr( pjsip_tpmgr *tpmgr,
  1037. pj_pool_t *pool,
  1038. pjsip_transport_type_e type,
  1039. const pjsip_tpselector *sel,
  1040. pj_str_t *ip_addr,
  1041. int *port);
  1042. /**
  1043. * Parameter for pjsip_tpmgr_find_local_addr2() function.
  1044. */
  1045. typedef struct pjsip_tpmgr_fla2_param
  1046. {
  1047. /**
  1048. * Specify transport type to use. This must be set.
  1049. */
  1050. pjsip_transport_type_e tp_type;
  1051. /**
  1052. * Optional pointer to preferred transport, if any.
  1053. */
  1054. const pjsip_tpselector *tp_sel;
  1055. /**
  1056. * Destination host, if known. The destination host is needed
  1057. * if \a local_if field below is set.
  1058. */
  1059. pj_str_t dst_host;
  1060. /**
  1061. * Specify if the function should return which local interface
  1062. * to use for the specified destination in \a dst_host. By definition,
  1063. * the returned address will always be local interface address.
  1064. */
  1065. pj_bool_t local_if;
  1066. /**
  1067. * The returned address.
  1068. */
  1069. pj_str_t ret_addr;
  1070. /**
  1071. * The returned port.
  1072. */
  1073. pj_uint16_t ret_port;
  1074. /**
  1075. * Returned pointer to the transport. Only set if local_if is set.
  1076. */
  1077. const void *ret_tp;
  1078. } pjsip_tpmgr_fla2_param;
  1079. /**
  1080. * Initialize with default values.
  1081. *
  1082. * @param prm The parameter to be initialized.
  1083. */
  1084. PJ_DECL(void) pjsip_tpmgr_fla2_param_default(pjsip_tpmgr_fla2_param *prm);
  1085. /**
  1086. * Find out the appropriate local address info (IP address and port) to
  1087. * advertise in Contact or Via header header based on the remote address
  1088. * to be contacted. The local address info would be the address name of the
  1089. * transport or listener which will be used to send the request.
  1090. *
  1091. * @see pjsip_tpmgr_find_local_addr()
  1092. *
  1093. * @param tpmgr The transport manager.
  1094. * @param pool Pool to allocate memory for the IP address.
  1095. * @param prm Function input and output parameters.
  1096. *
  1097. * @return PJ_SUCCESS, or the appropriate error code.
  1098. */
  1099. PJ_DECL(pj_status_t) pjsip_tpmgr_find_local_addr2(pjsip_tpmgr *tpmgr,
  1100. pj_pool_t *pool,
  1101. pjsip_tpmgr_fla2_param *prm);
  1102. /**
  1103. * Return number of transports currently registered to the transport
  1104. * manager.
  1105. *
  1106. * @param mgr The transport manager.
  1107. *
  1108. * @return Number of transports.
  1109. */
  1110. PJ_DECL(unsigned) pjsip_tpmgr_get_transport_count(pjsip_tpmgr *mgr);
  1111. /**
  1112. * Destroy a transport manager. Normally application doesn't need to call
  1113. * this function directly, since a transport manager will be created and
  1114. * destroyed automatically by the SIP endpoint.
  1115. *
  1116. * @param mgr The transport manager.
  1117. *
  1118. * @return PJ_SUCCESS on success.
  1119. */
  1120. PJ_DECL(pj_status_t) pjsip_tpmgr_destroy(pjsip_tpmgr *mgr);
  1121. /**
  1122. * Dump transport info and status to log.
  1123. *
  1124. * @param mgr The transport manager.
  1125. */
  1126. PJ_DECL(void) pjsip_tpmgr_dump_transports(pjsip_tpmgr *mgr);
  1127. /**
  1128. * Parameter for pjsip_tpmgr_shutdown_all() function.
  1129. */
  1130. typedef struct pjsip_tpmgr_shutdown_param
  1131. {
  1132. /**
  1133. * Specify whether disconnection state notification should be sent
  1134. * immediately, see pjsip_transport_shutdown2() for more info.
  1135. *
  1136. * Default: PJ_TRUE.
  1137. */
  1138. pj_bool_t force;
  1139. /**
  1140. * Specify whether UDP transports should also be shutdown.
  1141. *
  1142. * Default: PJ_TRUE.
  1143. */
  1144. pj_bool_t include_udp;
  1145. } pjsip_tpmgr_shutdown_param;
  1146. /**
  1147. * Initialize transports shutdown parameter with default values.
  1148. *
  1149. * @param prm The parameter to be initialized.
  1150. */
  1151. PJ_DECL(void) pjsip_tpmgr_shutdown_param_default(
  1152. pjsip_tpmgr_shutdown_param *prm);
  1153. /**
  1154. * Shutdown all transports. This basically invokes pjsip_transport_shutdown2()
  1155. * on all transports.
  1156. *
  1157. * @param mgr The transport manager.
  1158. * @param param The function parameters.
  1159. *
  1160. * @return PJ_SUCCESS on success.
  1161. */
  1162. PJ_DECL(pj_status_t) pjsip_tpmgr_shutdown_all(
  1163. pjsip_tpmgr *mgr,
  1164. const pjsip_tpmgr_shutdown_param *param);
  1165. /*****************************************************************************
  1166. *
  1167. * PUBLIC API
  1168. *
  1169. *****************************************************************************/
  1170. /**
  1171. * Find transport to be used to send message to remote destination. If no
  1172. * suitable transport is found, a new one will be created.
  1173. *
  1174. * This is an internal function since normally application doesn't have access
  1175. * to transport manager. Application should use pjsip_endpt_acquire_transport()
  1176. * instead.
  1177. *
  1178. * @param mgr The transport manager instance.
  1179. * @param type The type of transport to be acquired.
  1180. * @param remote The remote address to send message to.
  1181. * @param addr_len Length of the remote address.
  1182. * @param sel Optional pointer to transport selector instance which is
  1183. * used to find explicit transport, if required.
  1184. * @param tp Pointer to receive the transport instance, if one is found.
  1185. *
  1186. * @return PJ_SUCCESS on success, or the appropriate error code.
  1187. */
  1188. PJ_DECL(pj_status_t) pjsip_tpmgr_acquire_transport(pjsip_tpmgr *mgr,
  1189. pjsip_transport_type_e type,
  1190. const pj_sockaddr_t *remote,
  1191. int addr_len,
  1192. const pjsip_tpselector *sel,
  1193. pjsip_transport **tp);
  1194. /**
  1195. * Find suitable transport for sending SIP message to specified remote
  1196. * destination by also considering the outgoing SIP message. If no suitable
  1197. * transport is found, a new one will be created.
  1198. *
  1199. * This is an internal function since normally application doesn't have access
  1200. * to transport manager. Application should use pjsip_endpt_acquire_transport2()
  1201. * instead.
  1202. *
  1203. * @param mgr The transport manager instance.
  1204. * @param type The type of transport to be acquired.
  1205. * @param remote The remote address to send message to.
  1206. * @param addr_len Length of the remote address.
  1207. * @param sel Optional pointer to transport selector instance which is
  1208. * used to find explicit transport, if required.
  1209. * @param tdata Optional pointer to data to be sent.
  1210. * @param tp Pointer to receive the transport instance, if one is found.
  1211. *
  1212. * @return PJ_SUCCESS on success, or the appropriate error code.
  1213. */
  1214. PJ_DECL(pj_status_t) pjsip_tpmgr_acquire_transport2(pjsip_tpmgr *mgr,
  1215. pjsip_transport_type_e type,
  1216. const pj_sockaddr_t *remote,
  1217. int addr_len,
  1218. const pjsip_tpselector *sel,
  1219. pjsip_tx_data *tdata,
  1220. pjsip_transport **tp);
  1221. /**
  1222. * Type of callback to receive notification when message or raw data
  1223. * has been sent.
  1224. *
  1225. * @param token The token that was given when calling the function
  1226. * to send message or raw data.
  1227. * @param tdata The transmit buffer used to send the message.
  1228. * @param bytes_sent Number of bytes sent. On success, the value will be
  1229. * positive number indicating the number of bytes sent.
  1230. * On failure, the value will be a negative number of
  1231. * the error code (i.e. bytes_sent = -status).
  1232. */
  1233. typedef void (*pjsip_tp_send_callback)(void *token, pjsip_tx_data *tdata,
  1234. pj_ssize_t bytes_sent);
  1235. /**
  1236. * This is a low-level function to send a SIP message using the specified
  1237. * transport to the specified destination.
  1238. *
  1239. * @param tr The SIP transport to be used.
  1240. * @param tdata Transmit data buffer containing SIP message.
  1241. * @param addr Destination address.
  1242. * @param addr_len Length of destination address.
  1243. * @param token Arbitrary token to be returned back to callback.
  1244. * @param cb Optional callback to be called to notify caller about
  1245. * the completion status of the pending send operation.
  1246. *
  1247. * @return If the message has been sent successfully, this function
  1248. * will return PJ_SUCCESS and the callback will not be
  1249. * called. If message cannot be sent immediately, this
  1250. * function will return PJ_EPENDING, and application will
  1251. * be notified later about the completion via the callback.
  1252. * Any statuses other than PJ_SUCCESS or PJ_EPENDING
  1253. * indicates immediate failure, and in this case the
  1254. * callback will not be called.
  1255. */
  1256. PJ_DECL(pj_status_t) pjsip_transport_send( pjsip_transport *tr,
  1257. pjsip_tx_data *tdata,
  1258. const pj_sockaddr_t *addr,
  1259. int addr_len,
  1260. void *token,
  1261. pjsip_tp_send_callback cb);
  1262. /**
  1263. * This is a low-level function to send raw data to a destination.
  1264. *
  1265. * See also #pjsip_endpt_send_raw() and #pjsip_endpt_send_raw_to_uri().
  1266. *
  1267. * @param mgr Transport manager.
  1268. * @param tp_type Transport type.
  1269. * @param sel Optional pointer to transport selector instance if
  1270. * application wants to use a specific transport instance
  1271. * rather then letting transport manager finds the suitable
  1272. * transport.
  1273. * @param tdata Optional transmit data buffer to be used. If this value
  1274. * is NULL, this function will create one internally. If
  1275. * tdata is specified, this function will decrement the
  1276. * reference counter upon completion.
  1277. * @param raw_data The data to be sent.
  1278. * @param data_len The length of the data.
  1279. * @param addr Destination address.
  1280. * @param addr_len Length of destination address.
  1281. * @param token Arbitrary token to be returned back to callback.
  1282. * @param cb Optional callback to be called to notify caller about
  1283. * the completion status of the pending send operation.
  1284. *
  1285. * @return If the message has been sent successfully, this function
  1286. * will return PJ_SUCCESS and the callback will not be
  1287. * called. If message cannot be sent immediately, this
  1288. * function will return PJ_EPENDING, and application will
  1289. * be notified later about the completion via the callback.
  1290. * Any statuses other than PJ_SUCCESS or PJ_EPENDING
  1291. * indicates immediate failure, and in this case the
  1292. * callback will not be called.
  1293. */
  1294. PJ_DECL(pj_status_t) pjsip_tpmgr_send_raw(pjsip_tpmgr *mgr,
  1295. pjsip_transport_type_e tp_type,
  1296. const pjsip_tpselector *sel,
  1297. pjsip_tx_data *tdata,
  1298. const void *raw_data,
  1299. pj_size_t data_len,
  1300. const pj_sockaddr_t *addr,
  1301. int addr_len,
  1302. void *token,
  1303. pjsip_tp_send_callback cb);
  1304. /**
  1305. * Enumeration of transport state types.
  1306. */
  1307. typedef enum pjsip_transport_state
  1308. {
  1309. PJSIP_TP_STATE_CONNECTED, /**< Transport connected, applicable only
  1310. to connection-oriented transports
  1311. such as TCP and TLS. */
  1312. PJSIP_TP_STATE_DISCONNECTED, /**< Transport disconnected, applicable
  1313. only to connection-oriented
  1314. transports such as TCP and TLS. */
  1315. PJSIP_TP_STATE_SHUTDOWN, /**< Transport shutdown, either
  1316. due to TCP/TLS disconnect error
  1317. from the network, or when shutdown
  1318. is initiated by PJSIP itself. */
  1319. PJSIP_TP_STATE_DESTROY, /**< Transport destroy, when transport
  1320. is about to be destroyed. */
  1321. } pjsip_transport_state;
  1322. /**
  1323. * Definition of transport state listener key.
  1324. */
  1325. typedef void pjsip_tp_state_listener_key;
  1326. /**
  1327. * Structure of transport state info passed by #pjsip_tp_state_callback.
  1328. */
  1329. typedef struct pjsip_transport_state_info {
  1330. /**
  1331. * The last error code related to the transport state.
  1332. */
  1333. pj_status_t status;
  1334. /**
  1335. * Optional extended info, the content is specific for each transport type.
  1336. */
  1337. void *ext_info;
  1338. /**
  1339. * Optional user data. In global transport state notification, this will
  1340. * always be NULL.
  1341. */
  1342. void *user_data;
  1343. } pjsip_transport_state_info;
  1344. /**
  1345. * Type of callback to receive transport state notifications, such as
  1346. * transport connected/disconnected. Application may shutdown the transport
  1347. * in this callback.
  1348. *
  1349. * @param tp The transport instance.
  1350. * @param state The transport state.
  1351. * @param info The transport state info.
  1352. */
  1353. typedef void (*pjsip_tp_state_callback)(
  1354. pjsip_transport *tp,
  1355. pjsip_transport_state state,
  1356. const pjsip_transport_state_info *info);
  1357. /**
  1358. * Set callback of global transport state notification. The caller will be
  1359. * notified whenever the state of any transport is changed. The type of events
  1360. * are defined in #pjsip_transport_state.
  1361. *
  1362. * Note that this function will override the existing callback, if any, so
  1363. * application is recommended to keep the old callback and manually forward
  1364. * the notification to the old callback, otherwise other component that
  1365. * concerns about the transport state will no longer receive transport state
  1366. * events.
  1367. *
  1368. * @param mgr Transport manager.
  1369. * @param cb Callback to be called to notify caller about transport
  1370. * state changing.
  1371. *
  1372. * @return PJ_SUCCESS on success, or the appropriate error code.
  1373. */
  1374. PJ_DECL(pj_status_t) pjsip_tpmgr_set_state_cb(pjsip_tpmgr *mgr,
  1375. pjsip_tp_state_callback cb);
  1376. /**
  1377. * Get the callback of global transport state notification.
  1378. *
  1379. * @param mgr Transport manager.
  1380. *
  1381. * @return The transport state callback or NULL if it is not set.
  1382. */
  1383. PJ_DECL(pjsip_tp_state_callback) pjsip_tpmgr_get_state_cb(
  1384. const pjsip_tpmgr *mgr);
  1385. /**
  1386. * Add a listener to the specified transport for transport state notification.
  1387. *
  1388. * @param tp The transport.
  1389. * @param cb Callback to be called to notify listener about transport
  1390. * state changing.
  1391. * @param user_data The user data.
  1392. * @param key Output key, used to remove this listener.
  1393. *
  1394. * @return PJ_SUCCESS on success, or the appropriate error code.
  1395. */
  1396. PJ_DECL(pj_status_t) pjsip_transport_add_state_listener (
  1397. pjsip_transport *tp,
  1398. pjsip_tp_state_callback cb,
  1399. void *user_data,
  1400. pjsip_tp_state_listener_key **key);
  1401. /**
  1402. * Remove a listener from the specified transport for transport state
  1403. * notification.
  1404. *
  1405. * @param tp The transport.
  1406. * @param key The listener key.
  1407. * @param user_data The user data, for validation purpose.
  1408. *
  1409. * @return PJ_SUCCESS on success, or the appropriate error code.
  1410. */
  1411. PJ_DECL(pj_status_t) pjsip_transport_remove_state_listener (
  1412. pjsip_transport *tp,
  1413. pjsip_tp_state_listener_key *key,
  1414. const void *user_data);
  1415. /**
  1416. * Structure of dropped received data.
  1417. */
  1418. typedef struct pjsip_tp_dropped_data
  1419. {
  1420. /**
  1421. * The transport receiving the data.
  1422. */
  1423. pjsip_transport *tp;
  1424. /**
  1425. * The data.
  1426. */
  1427. void *data;
  1428. /**
  1429. * The data length.
  1430. * If the status field below indicates an invalid SIP message
  1431. * (PJSIP_EINVALIDMSG) and application detects a SIP message
  1432. * at position p, it can pass the data back to PJSIP to be processed
  1433. * by setting the len to p. This can be useful for apps which
  1434. * wishes to use the same transport for SIP signalling and non-SIP
  1435. * purposes (such as SIP outbound using STUN message).
  1436. */
  1437. pj_size_t len;
  1438. /**
  1439. * The status or reason of drop. For example, a leading newlines (common
  1440. * keep-alive packet) will be dropped with status PJ_EIGNORED, an invalid
  1441. * SIP message will have status PJSIP_EINVALIDMSG, a SIP message overflow
  1442. * will have status PJSIP_ERXOVERFLOW.
  1443. */
  1444. pj_status_t status;
  1445. } pjsip_tp_dropped_data;
  1446. /**
  1447. * Type of callback to data dropping notifications.
  1448. *
  1449. * @param data The dropped data.
  1450. */
  1451. typedef void (*pjsip_tp_on_rx_dropped_cb)(pjsip_tp_dropped_data *data);
  1452. /**
  1453. * Set callback of data dropping. The caller will be notified whenever any
  1454. * received data is dropped (due to leading newlines or keep-alive packet or
  1455. * invalid SIP message). This callback can be useful for application,
  1456. * for example, to implement custom keep-alive mechanism or connection
  1457. * availability detection.
  1458. *
  1459. * @param mgr Transport manager.
  1460. * @param cb The callback function, set to NULL to reset the callback.
  1461. *
  1462. * @return PJ_SUCCESS on success, or the appropriate error code.
  1463. */
  1464. PJ_DECL(pj_status_t) pjsip_tpmgr_set_drop_data_cb(pjsip_tpmgr *mgr,
  1465. pjsip_tp_on_rx_dropped_cb cb);
  1466. /**
  1467. * Structure of received data that will be passed to data received
  1468. * notification callback.
  1469. */
  1470. typedef struct pjsip_tp_rx_data
  1471. {
  1472. /**
  1473. * The transport receiving the data.
  1474. */
  1475. pjsip_transport *tp;
  1476. /**
  1477. * The data.
  1478. */
  1479. void *data;
  1480. /**
  1481. * The data length.
  1482. * If application wishes to discard some data of len p, it can pass
  1483. * the remaining data back to PJSIP to be processed by setting the len
  1484. * to (len - p).
  1485. * If application wants to shutdown the transport from within the
  1486. * callback (for example after if finds out that the data is
  1487. * suspicious/garbage), app must set the len to zero to prevent
  1488. * further processing of the data.
  1489. */
  1490. pj_size_t len;
  1491. } pjsip_tp_rx_data;
  1492. /**
  1493. * Type of callback to data received notifications.
  1494. *
  1495. * @param data The received data.
  1496. */
  1497. typedef pj_status_t (*pjsip_tp_on_rx_data_cb)(pjsip_tp_rx_data *data);
  1498. /**
  1499. * Set callback to be called whenever any data is received by a stream
  1500. * oriented transport. This can be useful for application to do its own
  1501. * verification, filtering, or logging of potential malicious activities.
  1502. *
  1503. * @param mgr Transport manager.
  1504. * @param cb The callback function, set to NULL to reset the callback.
  1505. *
  1506. * @return PJ_SUCCESS on success, or the appropriate error code.
  1507. */
  1508. PJ_DECL(pj_status_t) pjsip_tpmgr_set_recv_data_cb(pjsip_tpmgr *mgr,
  1509. pjsip_tp_on_rx_data_cb cb);
  1510. /**
  1511. * @}
  1512. */
  1513. PJ_END_DECL
  1514. #endif /* __PJSIP_SIP_TRANSPORT_H__ */