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