1
/* $Id: sip_transport.h 4173 2012-06-20 10:39:05Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJSIP_SIP_TRANSPORT_H__
21
#define __PJSIP_SIP_TRANSPORT_H__
24
* @file sip_transport.h
25
* @brief SIP Transport
28
#include <pjsip/sip_msg.h>
29
#include <pjsip/sip_parser.h>
30
#include <pjsip/sip_resolve.h>
33
#include <pj/ioqueue.h>
39
* @defgroup PJSIP_TRANSPORT Transport
41
* @brief This is the transport framework.
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.
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.
54
/*****************************************************************************
56
* GENERAL TRANSPORT (NAMES, TYPES, ETC.)
58
*****************************************************************************/
61
* Forward declaration for transport factory (since it is referenced by
62
* the transport factory itself).
64
typedef struct pjsip_tpfactory pjsip_tpfactory;
68
* Flags for SIP transports.
70
enum pjsip_transport_flags_e
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
80
* Check if transport tp is reliable.
82
#define PJSIP_TRANSPORT_IS_RELIABLE(tp) \
83
((tp)->flag & PJSIP_TRANSPORT_RELIABLE)
86
* Check if transport tp is secure.
88
#define PJSIP_TRANSPORT_IS_SECURE(tp) \
89
((tp)->flag & PJSIP_TRANSPORT_SECURE)
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.
98
* @param tp_flag The flags describing characteristics of this
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.
105
* @return PJ_SUCCESS if registration is successful, or
106
* PJSIP_ETYPEEXISTS if the same transport type has
107
* already been registered.
109
PJ_DECL(pj_status_t) pjsip_transport_register_type(unsigned tp_flag,
116
* Get the transport type from the transport name.
118
* @param name Transport name, such as "TCP", or "UDP".
120
* @return The transport type, or PJSIP_TRANSPORT_UNSPECIFIED if
121
* the name is not recognized as the name of supported
124
PJ_DECL(pjsip_transport_type_e)
125
pjsip_transport_get_type_from_name(const pj_str_t *name);
128
* Get the transport type for the specified flags.
130
* @param flag The transport flag.
132
* @return Transport type.
134
PJ_DECL(pjsip_transport_type_e)
135
pjsip_transport_get_type_from_flag(unsigned flag);
138
* Get the socket address family of a given transport type.
140
* @param type Transport type.
142
* @return Transport type.
144
PJ_DECL(int) pjsip_transport_type_get_af(pjsip_transport_type_e type);
147
* Get transport flag from type.
149
* @param type Transport type.
151
* @return Transport flags.
154
pjsip_transport_get_flag_from_type( pjsip_transport_type_e type );
157
* Get the default SIP port number for the specified type.
159
* @param type Transport type.
161
* @return The port number, which is the default SIP port number for
162
* the specified type.
165
pjsip_transport_get_default_port_for_type(pjsip_transport_type_e type);
168
* Get transport type name.
170
* @param t Transport type.
172
* @return Transport name.
174
PJ_DECL(const char*) pjsip_transport_get_type_name(pjsip_transport_type_e t);
177
* Get longer description for the specified transport type.
179
* @param t Transport type.
181
* @return Transport description.
183
PJ_DECL(const char*) pjsip_transport_get_type_desc(pjsip_transport_type_e t);
187
/*****************************************************************************
189
* TRANSPORT SELECTOR.
191
*****************************************************************************/
194
* This structure describes the type of data in pjsip_tpselector.
196
typedef enum pjsip_tpselector_type
198
/** Transport is not specified. */
199
PJSIP_TPSELECTOR_NONE,
201
/** Use the specific transport to send request. */
202
PJSIP_TPSELECTOR_TRANSPORT,
204
/** Use the specific listener to send request. */
205
PJSIP_TPSELECTOR_LISTENER,
207
} pjsip_tpselector_type;
211
* This structure describes the transport/listener preference to be used
212
* when sending outgoing requests.
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
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().
225
typedef struct pjsip_tpselector
227
/** The type of data in the union */
228
pjsip_tpselector_type type;
230
/** Union representing the transport/listener criteria to be used. */
232
pjsip_transport *transport;
233
pjsip_tpfactory *listener;
241
* Add transport/listener reference in the selector to prevent the specified
242
* transport/listener from being destroyed while application still has
245
* @param sel The transport selector.
247
PJ_DECL(void) pjsip_tpselector_add_ref(pjsip_tpselector *sel);
251
* Decrement transport/listener reference in the selector.
252
* @param sel The transport selector
254
PJ_DECL(void) pjsip_tpselector_dec_ref(pjsip_tpselector *sel);
257
/*****************************************************************************
259
* RECEIVE DATA BUFFER.
261
*****************************************************************************/
264
* A customized ioqueue async operation key which is used by transport
265
* to locate rdata when a pending read operation completes.
267
typedef struct pjsip_rx_data_op_key
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;
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.
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.
290
/** Memory pool for this buffer. */
293
/** The transport object which received this packet. */
294
pjsip_transport *transport;
296
/** Other transport specific data to be attached to this buffer. */
300
pjsip_rx_data_op_key op_key;
306
* pkt_info is initialized by transport when it receives an incoming
311
/** Time when the message was received. */
312
pj_time_val timestamp;
314
/** Pointer to the original packet. */
315
char packet[PJSIP_MAX_PKT_LEN];
317
/** Zero termination for the packet. */
320
/** The length of the packet received. */
323
/** The source address from which the packet was received. */
324
pj_sockaddr src_addr;
326
/** The length of the source address. */
329
/** The IP source address string (NULL terminated). */
330
char src_name[PJ_INET6_ADDRSTRLEN];
332
/** The IP source port number. */
339
* msg_info is initialized by transport mgr (tpmgr) before this buffer
340
* is passed to endpoint.
344
/** Start of msg buffer. */
347
/** Length fo message. */
350
/** The parsed message, if any. */
353
/** Short description about the message.
354
* Application should use #pjsip_rx_data_get_info() instead.
358
/** The Call-ID header as found in the message. */
361
/** The From header as found in the message. */
362
pjsip_from_hdr *from;
364
/** The To header as found in the message. */
367
/** The topmost Via header as found in the message. */
370
/** The CSeq header as found in the message. */
371
pjsip_cseq_hdr *cseq;
373
/** Max forwards header. */
374
pjsip_max_fwd_hdr *max_fwd;
376
/** The first route header. */
377
pjsip_route_hdr *route;
379
/** The first record-route header. */
380
pjsip_rr_hdr *record_route;
382
/** Content-type header. */
383
pjsip_ctype_hdr *ctype;
385
/** Content-length header. */
386
pjsip_clen_hdr *clen;
388
/** "Require" header containing aggregates of all Require
389
* headers found in the message, or NULL.
391
pjsip_require_hdr *require;
393
/** "Supported" header containing aggregates of all Supported
394
* headers found in the message, or NULL.
396
pjsip_supported_hdr *supported;
398
/** The list of error generated by the parser when parsing
401
pjsip_parser_err_report parse_err;
407
* endpt_info is initialized by endpoint after this buffer reaches
413
* Data attached by modules to this message.
415
void *mod_data[PJSIP_MAX_MODULE];
422
* Get printable information about the message in the rdata.
424
* @param rdata The receive data buffer.
426
* @return Printable information.
428
PJ_DECL(char*) pjsip_rx_data_get_info(pjsip_rx_data *rdata);
431
/*****************************************************************************
433
* TRANSMIT DATA BUFFER MANIPULATION.
435
*****************************************************************************/
437
/** Customized ioqueue async operation key, used by transport to keep
438
* callback parameters.
440
typedef struct pjsip_tx_data_op_key
442
/** ioqueue pending operation key. */
443
pj_ioqueue_op_key_t key;
445
/** Transmit data associated with this key. */
446
pjsip_tx_data *tdata;
448
/** Arbitrary token (attached by transport) */
451
/** Callback to be called when pending transmit operation has
454
void (*callback)(pjsip_transport*,void*,pj_ssize_t);
455
} pjsip_tx_data_op_key;
459
* Data structure for sending outgoing message. Application normally creates
460
* this buffer by calling #pjsip_endpt_create_tdata.
462
* The lifetime of this buffer is controlled by the reference counter in this
463
* structure, which is manipulated by calling #pjsip_tx_data_add_ref and
464
* #pjsip_tx_data_dec_ref. When the reference counter has reached zero, then
465
* this buffer will be destroyed.
467
* A transaction object normally will add reference counter to this buffer
468
* when application calls #pjsip_tsx_send_msg, because it needs to keep the
469
* message for retransmission. The transaction will release the reference
470
* counter once its state has reached final state.
474
/** This is for transmission queue; it's managed by transports. */
475
PJ_DECL_LIST_MEMBER(struct pjsip_tx_data);
477
/** Memory pool for this buffer. */
480
/** A name to identify this buffer. */
481
char obj_name[PJ_MAX_OBJ_NAME];
483
/** Short information describing this buffer and the message in it.
484
* Application should use #pjsip_tx_data_get_info() instead of
485
* directly accessing this member.
489
/** For response message, this contains the reference to timestamp when
490
* the original request message was received. The value of this field
491
* is set when application creates response message to a request by
492
* calling #pjsip_endpt_create_response.
494
pj_time_val rx_timestamp;
496
/** The transport manager for this buffer. */
499
/** Ioqueue asynchronous operation key. */
500
pjsip_tx_data_op_key op_key;
505
/** The message in this buffer. */
508
/** Strict route header saved by #pjsip_process_route_set(), to be
509
* restored by #pjsip_restore_strict_route_set().
511
pjsip_route_hdr *saved_strict_route;
513
/** Buffer to the printed text representation of the message. When the
514
* content of this buffer is set, then the transport will send the content
515
* of this buffer instead of re-printing the message structure. If the
516
* message structure has changed, then application must invalidate this
517
* buffer by calling #pjsip_tx_data_invalidate_msg.
521
/** Reference counter. */
522
pj_atomic_t *ref_cnt;
524
/** Being processed by transport? */
527
/** Transport manager internal. */
530
/** Callback to be called when this tx_data has been transmitted. */
531
void (*cb)(void*, pjsip_tx_data*, pj_ssize_t);
533
/** Destination information, to be used to determine the network address
534
* of the message. For a request, this information is initialized when
535
* the request is sent with #pjsip_endpt_send_request_stateless() and
536
* network address is resolved. For CANCEL request, this information
537
* will be copied from the original INVITE to make sure that the CANCEL
538
* request goes to the same physical network address as the INVITE
547
/** Server addresses resolved.
549
pjsip_server_addresses addr;
551
/** Current server address being tried.
557
/** Transport information, only valid during on_tx_request() and
558
* on_tx_response() callback.
562
pjsip_transport *transport; /**< Transport being used. */
563
pj_sockaddr dst_addr; /**< Destination address. */
564
int dst_addr_len; /**< Length of address. */
565
char dst_name[PJ_INET6_ADDRSTRLEN]; /**< Destination address. */
566
int dst_port; /**< Destination port. */
570
* Transport selector, to specify which transport to be used.
571
* The value here must be set with pjsip_tx_data_set_transport(),
572
* to allow reference counter to be set properly.
574
pjsip_tpselector tp_sel;
577
* Special flag to indicate that this transmit data is a request that has
578
* been updated with proper authentication response and is ready to be
581
pj_bool_t auth_retry;
584
* Arbitrary data attached by PJSIP modules.
586
void *mod_data[PJSIP_MAX_MODULE];
589
* If via_addr is set, it will be used as the "sent-by" field of the
590
* Via header for outgoing requests as long as the request uses via_tp
591
* transport. Normally application should not use or access these fields.
593
pjsip_host_port via_addr; /**< Via address. */
594
const void *via_tp; /**< Via transport. */
599
* Create a new, blank transmit buffer. The reference count is initialized
602
* @param mgr The transport manager.
603
* @param tdata Pointer to receive transmit data.
605
* @return PJ_SUCCESS, or the appropriate error code.
607
* @see pjsip_endpt_create_tdata
609
PJ_DECL(pj_status_t) pjsip_tx_data_create( pjsip_tpmgr *mgr,
610
pjsip_tx_data **tdata );
613
* Add reference counter to the transmit buffer. The reference counter controls
614
* the life time of the buffer, ie. when the counter reaches zero, then it
617
* @param tdata The transmit buffer.
619
PJ_DECL(void) pjsip_tx_data_add_ref( pjsip_tx_data *tdata );
622
* Decrement reference counter of the transmit buffer.
623
* When the transmit buffer is no longer used, it will be destroyed and
624
* caller is informed with PJSIP_EBUFDESTROYED return status.
626
* @param tdata The transmit buffer data.
627
* @return This function will always succeeded eventhough the return
628
* status is non-zero. A status PJSIP_EBUFDESTROYED will be
629
* returned to inform that buffer is destroyed.
631
PJ_DECL(pj_status_t) pjsip_tx_data_dec_ref( pjsip_tx_data *tdata );
634
* Print the SIP message to transmit data buffer's internal buffer. This
635
* may allocate memory for the buffer, if the buffer has not been allocated
636
* yet, and encode the SIP message to that buffer.
638
* @param tdata The transmit buffer.
640
* @return PJ_SUCCESS on success of the appropriate error code.
642
PJ_DECL(pj_status_t) pjsip_tx_data_encode(pjsip_tx_data *tdata);
645
* Check if transmit data buffer contains a valid message.
647
* @param tdata The transmit buffer.
648
* @return Non-zero (PJ_TRUE) if buffer contains a valid message.
650
PJ_DECL(pj_bool_t) pjsip_tx_data_is_valid( pjsip_tx_data *tdata );
653
* Invalidate the print buffer to force message to be re-printed. Call
654
* when the message has changed after it has been printed to buffer. The
655
* message is printed to buffer normally by transport when it is about to be
656
* sent to the wire. Subsequent sending of the message will not cause
657
* the message to be re-printed, unless application invalidates the buffer
658
* by calling this function.
660
* @param tdata The transmit buffer.
662
PJ_DECL(void) pjsip_tx_data_invalidate_msg( pjsip_tx_data *tdata );
665
* Get short printable info about the transmit data. This will normally return
666
* short information about the message.
668
* @param tdata The transmit buffer.
670
* @return Null terminated info string.
672
PJ_DECL(char*) pjsip_tx_data_get_info( pjsip_tx_data *tdata );
675
* Set the explicit transport to be used when sending this transmit data.
676
* Application should not need to call this function, but rather use
677
* pjsip_tsx_set_transport() and pjsip_dlg_set_transport() instead (which
678
* will call this function).
680
* @param tdata The transmit buffer.
681
* @param sel Transport selector.
683
* @return PJ_SUCCESS on success.
685
PJ_DECL(pj_status_t) pjsip_tx_data_set_transport(pjsip_tx_data *tdata,
686
const pjsip_tpselector *sel);
689
/*****************************************************************************
693
*****************************************************************************/
695
* Type of callback to receive transport operation status.
697
typedef void (*pjsip_transport_callback)(pjsip_transport *tp, void *token,
698
pj_ssize_t sent_bytes);
701
* This structure describes transport key to be registered to hash table.
703
typedef struct pjsip_transport_key
711
* Destination address.
713
pj_sockaddr rem_addr;
715
} pjsip_transport_key;
719
* Enumeration of transport direction types.
721
typedef enum pjsip_transport_dir
723
PJSIP_TP_DIR_NONE, /**< Direction not set, normally used by
724
connectionless transports such as
726
PJSIP_TP_DIR_OUTGOING, /**< Outgoing connection or client mode,
727
this is only for connection-oriented
729
PJSIP_TP_DIR_INCOMING, /**< Incoming connection or server mode,
730
this is only for connection-oriented
732
} pjsip_transport_dir;
736
* This structure represent the "public" interface of a SIP transport.
737
* Applications normally extend this structure to include transport
740
struct pjsip_transport
742
char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
744
pj_pool_t *pool; /**< Pool used by transport. */
745
pj_atomic_t *ref_cnt; /**< Reference counter. */
746
pj_lock_t *lock; /**< Lock object. */
747
pj_bool_t tracing; /**< Tracing enabled? */
748
pj_bool_t is_shutdown; /**< Being shutdown? */
749
pj_bool_t is_destroying; /**< Destroy in progress? */
751
/** Key for indexing this transport in hash table. */
752
pjsip_transport_key key;
754
char *type_name; /**< Type name. */
755
unsigned flag; /**< #pjsip_transport_flags_e */
756
char *info; /**< Transport info/description.*/
758
int addr_len; /**< Length of addresses. */
759
pj_sockaddr local_addr; /**< Bound address. */
760
pjsip_host_port local_name; /**< Published name (eg. STUN). */
761
pjsip_host_port remote_name; /**< Remote address name. */
762
pjsip_transport_dir dir; /**< Connection direction. */
764
pjsip_endpoint *endpt; /**< Endpoint instance. */
765
pjsip_tpmgr *tpmgr; /**< Transport manager. */
766
pj_timer_entry idle_timer; /**< Timer when ref cnt is zero.*/
768
void *data; /**< Internal transport data. */
771
* Function to be called by transport manager to send SIP message.
773
* @param transport The transport to send the message.
774
* @param packet The buffer to send.
775
* @param length The length of the buffer to send.
776
* @param op_key Completion token, which will be supplied to
777
* caller when pending send operation completes.
778
* @param rem_addr The remote destination address.
779
* @param addr_len Size of remote address.
780
* @param callback If supplied, the callback will be called
781
* once a pending transmission has completed. If
782
* the function completes immediately (i.e. return
783
* code is not PJ_EPENDING), the callback will not
786
* @return Should return PJ_SUCCESS only if data has been
787
* succesfully queued to operating system for
788
* transmission. Otherwise it may return PJ_EPENDING
789
* if the underlying transport can not send the
790
* data immediately and will send it later, which in
791
* this case caller doesn't have to do anything
792
* except wait the calback to be called, if it
794
* Other return values indicate the error code.
796
pj_status_t (*send_msg)(pjsip_transport *transport,
797
pjsip_tx_data *tdata,
798
const pj_sockaddr_t *rem_addr,
801
pjsip_transport_callback callback);
804
* Instruct the transport to initiate graceful shutdown procedure.
805
* After all objects release their reference to this transport,
806
* the transport will be deleted.
808
* Note that application MUST use #pjsip_transport_shutdown() instead.
810
* @param transport The transport.
812
* @return PJ_SUCCESS on success.
814
pj_status_t (*do_shutdown)(pjsip_transport *transport);
817
* Forcefully destroy this transport regardless whether there are
818
* objects that currently use this transport. This function should only
819
* be called by transport manager or other internal objects (such as the
820
* transport itself) who know what they're doing. Application should use
821
* #pjsip_transport_shutdown() instead.
823
* @param transport The transport.
825
* @return PJ_SUCCESS on success.
827
pj_status_t (*destroy)(pjsip_transport *transport);
830
* Application may extend this structure..
836
* Register a transport instance to the transport manager. This function
837
* is normally called by the transport instance when it is created
840
* @param mgr The transport manager.
841
* @param tp The new transport to be registered.
843
* @return PJ_SUCCESS on success.
845
PJ_DECL(pj_status_t) pjsip_transport_register( pjsip_tpmgr *mgr,
846
pjsip_transport *tp );
850
* Start graceful shutdown procedure for this transport. After graceful
851
* shutdown has been initiated, no new reference can be obtained for
852
* the transport. However, existing objects that currently uses the
853
* transport may still use this transport to send and receive packets.
855
* After all objects release their reference to this transport,
856
* the transport will be destroyed immediately.
858
* @param tp The transport.
860
* @return PJ_SUCCESS on success.
862
PJ_DECL(pj_status_t) pjsip_transport_shutdown(pjsip_transport *tp);
865
* Destroy a transport when there is no object currently uses the transport.
866
* This function is normally called internally by transport manager or the
867
* transport itself. Application should use #pjsip_transport_shutdown()
870
* @param tp The transport instance.
872
* @return PJ_SUCCESS on success or the appropriate error code.
873
* Some of possible errors are PJSIP_EBUSY if the
874
* transport's reference counter is not zero.
876
PJ_DECL(pj_status_t) pjsip_transport_destroy( pjsip_transport *tp);
879
* Add reference counter to the specified transport. Any objects that wishes
880
* to keep the reference of the transport MUST increment the transport's
881
* reference counter to prevent it from being destroyed.
883
* @param tp The transport instance.
885
* @return PJ_SUCCESS on success or the appropriate error code.
887
PJ_DECL(pj_status_t) pjsip_transport_add_ref( pjsip_transport *tp );
890
* Decrement reference counter of the specified transport. When an object no
891
* longer want to keep the reference to the transport, it must decrement the
892
* reference counter. When the reference counter of the transport reaches
893
* zero, the transport manager will start the idle timer to destroy the
894
* transport if no objects acquire the reference counter during the idle
897
* @param tp The transport instance.
899
* @return PJ_SUCCESS on success.
901
PJ_DECL(pj_status_t) pjsip_transport_dec_ref( pjsip_transport *tp );
905
* This function is called by transport instances to report an incoming
906
* packet to the transport manager. The transport manager then would try to
907
* parse all SIP messages in the packet, and for each parsed SIP message, it
908
* would report the message to the SIP endpoint (#pjsip_endpoint).
910
* @param mgr The transport manager instance.
911
* @param rdata The receive data buffer containing the packet. The
912
* transport MUST fully initialize tp_info and pkt_info
913
* member of the rdata.
915
* @return The number of bytes successfully processed from the
916
* packet. If the transport is datagram oriented, the
917
* value will be equal to the size of the packet. For
918
* stream oriented transport (e.g. TCP, TLS), the value
919
* returned may be less than the packet size, if
920
* partial message is received. The transport then MUST
921
* keep the remainder part and report it again to
922
* this function once more data/packet is received.
924
PJ_DECL(pj_ssize_t) pjsip_tpmgr_receive_packet(pjsip_tpmgr *mgr,
925
pjsip_rx_data *rdata);
928
/*****************************************************************************
932
*****************************************************************************/
936
* A transport factory is normally used for connection oriented transports
937
* (such as TCP or TLS) to create instances of transports. It registers
938
* a new transport type to the transport manager, and the transport manager
939
* would ask the factory to create a transport instance when it received
940
* command from application to send a SIP message using the specified
943
struct pjsip_tpfactory
945
/** This list is managed by transport manager. */
946
PJ_DECL_LIST_MEMBER(struct pjsip_tpfactory);
948
char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
950
pj_pool_t *pool; /**< Owned memory pool. */
951
pj_lock_t *lock; /**< Lock object. */
953
pjsip_transport_type_e type; /**< Transport type. */
954
char *type_name; /**< Type string name. */
955
unsigned flag; /**< Transport flag. */
957
pj_sockaddr local_addr; /**< Bound address. */
958
pjsip_host_port addr_name; /**< Published name. */
961
* Create new outbound connection suitable for sending SIP message
962
* to specified remote address.
963
* Note that the factory is responsible for both creating the
964
* transport and registering it to the transport manager.
966
pj_status_t (*create_transport)(pjsip_tpfactory *factory,
968
pjsip_endpoint *endpt,
969
const pj_sockaddr *rem_addr,
971
pjsip_transport **transport);
974
* Create new outbound connection suitable for sending SIP message
975
* to specified remote address by also considering outgoing SIP
977
* Note that the factory is responsible for both creating the
978
* transport and registering it to the transport manager.
980
pj_status_t (*create_transport2)(pjsip_tpfactory *factory,
982
pjsip_endpoint *endpt,
983
const pj_sockaddr *rem_addr,
985
pjsip_tx_data *tdata,
986
pjsip_transport **transport);
989
* Destroy the listener.
991
pj_status_t (*destroy)(pjsip_tpfactory *factory);
994
* Application may extend this structure..
1001
* Register a transport factory.
1003
* @param mgr The transport manager.
1004
* @param tpf Transport factory.
1006
* @return PJ_SUCCESS if listener was successfully created.
1008
PJ_DECL(pj_status_t) pjsip_tpmgr_register_tpfactory(pjsip_tpmgr *mgr,
1009
pjsip_tpfactory *tpf);
1012
* Unregister factory.
1014
* @param mgr The transport manager.
1015
* @param tpf Transport factory.
1017
* @return PJ_SUCCESS is sucessfully unregistered.
1019
PJ_DECL(pj_status_t) pjsip_tpmgr_unregister_tpfactory(pjsip_tpmgr *mgr,
1020
pjsip_tpfactory *tpf);
1023
/*****************************************************************************
1027
*****************************************************************************/
1030
* Type of callback to be called when transport manager receives incoming
1033
* @param ep Endpoint.
1034
* @param status Receiption status.
1035
* @param rd Received packet.
1037
typedef void (*pjsip_rx_callback)(pjsip_endpoint *ep, pj_status_t status,
1041
* Type of callback to be called before transport manager is about
1042
* to transmit SIP message.
1044
* @param ep Endpoint.
1045
* @param td Transmit data.
1047
typedef pj_status_t (*pjsip_tx_callback)(pjsip_endpoint *ep, pjsip_tx_data*td);
1050
* Create a transport manager. Normally application doesn't need to call
1051
* this function directly, since a transport manager will be created and
1052
* destroyed automatically by the SIP endpoint.
1055
* @param endpt Endpoint instance.
1056
* @param rx_cb Callback to receive incoming message.
1057
* @param tx_cb Callback to be called before transport manager is sending
1059
* @param p_mgr Pointer to receive the new transport manager.
1061
* @return PJ_SUCCESS or the appropriate error code on error.
1063
PJ_DECL(pj_status_t) pjsip_tpmgr_create( pj_pool_t *pool,
1064
pjsip_endpoint * endpt,
1065
pjsip_rx_callback rx_cb,
1066
pjsip_tx_callback tx_cb,
1067
pjsip_tpmgr **p_mgr);
1071
* Find out the appropriate local address info (IP address and port) to
1072
* advertise in Contact header based on the remote address to be
1073
* contacted. The local address info would be the address name of the
1074
* transport or listener which will be used to send the request.
1076
* In this implementation, it will only select the transport based on
1077
* the transport type in the request.
1079
* @param tpmgr The transport manager.
1080
* @param pool Pool to allocate memory for the IP address.
1081
* @param type Destination address to contact.
1082
* @param sel Optional pointer to prefered transport, if any.
1083
* @param ip_addr Pointer to receive the IP address.
1084
* @param port Pointer to receive the port number.
1086
* @return PJ_SUCCESS, or the appropriate error code.
1088
PJ_DECL(pj_status_t) pjsip_tpmgr_find_local_addr( pjsip_tpmgr *tpmgr,
1090
pjsip_transport_type_e type,
1091
const pjsip_tpselector *sel,
1096
* Return number of transports currently registered to the transport
1099
* @param mgr The transport manager.
1101
* @return Number of transports.
1103
PJ_DECL(unsigned) pjsip_tpmgr_get_transport_count(pjsip_tpmgr *mgr);
1107
* Destroy a transport manager. Normally application doesn't need to call
1108
* this function directly, since a transport manager will be created and
1109
* destroyed automatically by the SIP endpoint.
1111
* @param mgr The transport manager.
1113
* @return PJ_SUCCESS on success.
1115
PJ_DECL(pj_status_t) pjsip_tpmgr_destroy(pjsip_tpmgr *mgr);
1119
* Dump transport info and status to log.
1121
* @param mgr The transport manager.
1123
PJ_DECL(void) pjsip_tpmgr_dump_transports(pjsip_tpmgr *mgr);
1126
/*****************************************************************************
1130
*****************************************************************************/
1134
* Find transport to be used to send message to remote destination. If no
1135
* suitable transport is found, a new one will be created.
1137
* This is an internal function since normally application doesn't have access
1138
* to transport manager. Application should use pjsip_endpt_acquire_transport()
1141
* @param mgr The transport manager instance.
1142
* @param type The type of transport to be acquired.
1143
* @param remote The remote address to send message to.
1144
* @param addr_len Length of the remote address.
1145
* @param sel Optional pointer to transport selector instance which is
1146
* used to find explicit transport, if required.
1147
* @param tp Pointer to receive the transport instance, if one is found.
1149
* @return PJ_SUCCESS on success, or the appropriate error code.
1151
PJ_DECL(pj_status_t) pjsip_tpmgr_acquire_transport(pjsip_tpmgr *mgr,
1152
pjsip_transport_type_e type,
1153
const pj_sockaddr_t *remote,
1155
const pjsip_tpselector *sel,
1156
pjsip_transport **tp);
1159
* Find suitable transport for sending SIP message to specified remote
1160
* destination by also considering the outgoing SIP message. If no suitable
1161
* transport is found, a new one will be created.
1163
* This is an internal function since normally application doesn't have access
1164
* to transport manager. Application should use pjsip_endpt_acquire_transport2()
1167
* @param mgr The transport manager instance.
1168
* @param type The type of transport to be acquired.
1169
* @param remote The remote address to send message to.
1170
* @param addr_len Length of the remote address.
1171
* @param sel Optional pointer to transport selector instance which is
1172
* used to find explicit transport, if required.
1173
* @param tdata Optional pointer to data to be sent.
1174
* @param tp Pointer to receive the transport instance, if one is found.
1176
* @return PJ_SUCCESS on success, or the appropriate error code.
1178
PJ_DECL(pj_status_t) pjsip_tpmgr_acquire_transport2(pjsip_tpmgr *mgr,
1179
pjsip_transport_type_e type,
1180
const pj_sockaddr_t *remote,
1182
const pjsip_tpselector *sel,
1183
pjsip_tx_data *tdata,
1184
pjsip_transport **tp);
1187
* Type of callback to receive notification when message or raw data
1190
* @param token The token that was given when calling the function
1191
* to send message or raw data.
1192
* @param tdata The transmit buffer used to send the message.
1193
* @param bytes_sent Number of bytes sent. On success, the value will be
1194
* positive number indicating the number of bytes sent.
1195
* On failure, the value will be a negative number of
1196
* the error code (i.e. bytes_sent = -status).
1198
typedef void (*pjsip_tp_send_callback)(void *token, pjsip_tx_data *tdata,
1199
pj_ssize_t bytes_sent);
1203
* This is a low-level function to send a SIP message using the specified
1204
* transport to the specified destination.
1206
* @param tr The SIP transport to be used.
1207
* @param tdata Transmit data buffer containing SIP message.
1208
* @param addr Destination address.
1209
* @param addr_len Length of destination address.
1210
* @param token Arbitrary token to be returned back to callback.
1211
* @param cb Optional callback to be called to notify caller about
1212
* the completion status of the pending send operation.
1214
* @return If the message has been sent successfully, this function
1215
* will return PJ_SUCCESS and the callback will not be
1216
* called. If message cannot be sent immediately, this
1217
* function will return PJ_EPENDING, and application will
1218
* be notified later about the completion via the callback.
1219
* Any statuses other than PJ_SUCCESS or PJ_EPENDING
1220
* indicates immediate failure, and in this case the
1221
* callback will not be called.
1223
PJ_DECL(pj_status_t) pjsip_transport_send( pjsip_transport *tr,
1224
pjsip_tx_data *tdata,
1225
const pj_sockaddr_t *addr,
1228
pjsip_tp_send_callback cb);
1232
* This is a low-level function to send raw data to a destination.
1234
* See also #pjsip_endpt_send_raw() and #pjsip_endpt_send_raw_to_uri().
1236
* @param mgr Transport manager.
1237
* @param tp_type Transport type.
1238
* @param sel Optional pointer to transport selector instance if
1239
* application wants to use a specific transport instance
1240
* rather then letting transport manager finds the suitable
1242
* @param tdata Optional transmit data buffer to be used. If this value
1243
* is NULL, this function will create one internally. If
1244
* tdata is specified, this function will decrement the
1245
* reference counter upon completion.
1246
* @param raw_data The data to be sent.
1247
* @param data_len The length of the data.
1248
* @param addr Destination address.
1249
* @param addr_len Length of destination address.
1250
* @param token Arbitrary token to be returned back to callback.
1251
* @param cb Optional callback to be called to notify caller about
1252
* the completion status of the pending send operation.
1254
* @return If the message has been sent successfully, this function
1255
* will return PJ_SUCCESS and the callback will not be
1256
* called. If message cannot be sent immediately, this
1257
* function will return PJ_EPENDING, and application will
1258
* be notified later about the completion via the callback.
1259
* Any statuses other than PJ_SUCCESS or PJ_EPENDING
1260
* indicates immediate failure, and in this case the
1261
* callback will not be called.
1263
PJ_DECL(pj_status_t) pjsip_tpmgr_send_raw(pjsip_tpmgr *mgr,
1264
pjsip_transport_type_e tp_type,
1265
const pjsip_tpselector *sel,
1266
pjsip_tx_data *tdata,
1267
const void *raw_data,
1269
const pj_sockaddr_t *addr,
1272
pjsip_tp_send_callback cb);
1276
* Enumeration of transport state types.
1278
typedef enum pjsip_transport_state
1280
PJSIP_TP_STATE_CONNECTED, /**< Transport connected, applicable only
1281
to connection-oriented transports
1282
such as TCP and TLS. */
1283
PJSIP_TP_STATE_DISCONNECTED /**< Transport disconnected, applicable
1284
only to connection-oriented
1285
transports such as TCP and TLS. */
1286
} pjsip_transport_state;
1290
* Definition of transport state listener key.
1292
typedef void pjsip_tp_state_listener_key;
1295
* Structure of transport state info passed by #pjsip_tp_state_callback.
1297
typedef struct pjsip_transport_state_info {
1299
* The last error code related to the transport state.
1304
* Optional extended info, the content is specific for each transport type.
1309
* Optional user data. In global transport state notification, this will
1314
} pjsip_transport_state_info;
1318
* Type of callback to receive transport state notifications, such as
1319
* transport connected/disconnected. Application may shutdown the transport
1322
* @param tp The transport instance.
1323
* @param state The transport state.
1324
* @param info The transport state info.
1326
typedef void (*pjsip_tp_state_callback)(
1327
pjsip_transport *tp,
1328
pjsip_transport_state state,
1329
const pjsip_transport_state_info *info);
1333
* Set callback of global transport state notification. The caller will be
1334
* notified whenever the state of any transport is changed. The type of events
1335
* are defined in #pjsip_transport_state.
1337
* Note that this function will override the existing callback, if any, so
1338
* application is recommended to keep the old callback and manually forward
1339
* the notification to the old callback, otherwise other component that
1340
* concerns about the transport state will no longer receive transport state
1343
* @param mgr Transport manager.
1344
* @param cb Callback to be called to notify caller about transport
1347
* @return PJ_SUCCESS on success, or the appropriate error code.
1349
PJ_DECL(pj_status_t) pjsip_tpmgr_set_state_cb(pjsip_tpmgr *mgr,
1350
pjsip_tp_state_callback cb);
1354
* Get the callback of global transport state notification.
1356
* @param mgr Transport manager.
1358
* @return The transport state callback or NULL if it is not set.
1360
PJ_DECL(pjsip_tp_state_callback) pjsip_tpmgr_get_state_cb(
1361
const pjsip_tpmgr *mgr);
1365
* Add a listener to the specified transport for transport state notification.
1367
* @param tp The transport.
1368
* @param cb Callback to be called to notify listener about transport
1370
* @param user_data The user data.
1371
* @param key Output key, used to remove this listener.
1373
* @return PJ_SUCCESS on success, or the appropriate error code.
1375
PJ_DECL(pj_status_t) pjsip_transport_add_state_listener (
1376
pjsip_transport *tp,
1377
pjsip_tp_state_callback cb,
1379
pjsip_tp_state_listener_key **key);
1383
* Remove a listener from the specified transport for transport state
1386
* @param tp The transport.
1387
* @param key The listener key.
1388
* @param user_data The user data, for validation purpose.
1390
* @return PJ_SUCCESS on success, or the appropriate error code.
1392
PJ_DECL(pj_status_t) pjsip_transport_remove_state_listener (
1393
pjsip_transport *tp,
1394
pjsip_tp_state_listener_key *key,
1395
const void *user_data);
1405
#endif /* __PJSIP_SIP_TRANSPORT_H__ */