res_pjsip_transport_websocket: Fix security events and simplify implementation.
[asterisk/asterisk.git] / include / asterisk / res_pjsip.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #ifndef _RES_PJSIP_H
20 #define _RES_PJSIP_H
21
22 #include "asterisk/stringfields.h"
23 /* Needed for struct ast_sockaddr */
24 #include "asterisk/netsock2.h"
25 /* Needed for linked list macros */
26 #include "asterisk/linkedlists.h"
27 /* Needed for ast_party_id */
28 #include "asterisk/channel.h"
29 /* Needed for ast_sorcery */
30 #include "asterisk/sorcery.h"
31 /* Needed for ast_dnsmgr */
32 #include "asterisk/dnsmgr.h"
33 /* Needed for ast_endpoint */
34 #include "asterisk/endpoints.h"
35 /* Needed for ast_t38_ec_modes */
36 #include "asterisk/udptl.h"
37 /* Needed for pj_sockaddr */
38 #include <pjlib.h>
39 /* Needed for ast_rtp_dtls_cfg struct */
40 #include "asterisk/rtp_engine.h"
41
42 /* Forward declarations of PJSIP stuff */
43 struct pjsip_rx_data;
44 struct pjsip_module;
45 struct pjsip_tx_data;
46 struct pjsip_dialog;
47 struct pjsip_transport;
48 struct pjsip_tpfactory;
49 struct pjsip_tls_setting;
50 struct pjsip_tpselector;
51
52 /*!
53  * \brief Structure for SIP transport information
54  */
55 struct ast_sip_transport_state {
56         /*! \brief Transport itself */
57         struct pjsip_transport *transport;
58
59         /*! \brief Transport factory */
60         struct pjsip_tpfactory *factory;
61 };
62
63 #define SIP_SORCERY_DOMAIN_ALIAS_TYPE "domain_alias"
64
65 /*!
66  * Details about a SIP domain alias
67  */
68 struct ast_sip_domain_alias {
69         /*! Sorcery object details */
70         SORCERY_OBJECT(details);
71         AST_DECLARE_STRING_FIELDS(
72                 /*! Domain to be aliased to */
73                 AST_STRING_FIELD(domain);
74         );
75 };
76
77 /*! \brief Maximum number of ciphers supported for a TLS transport */
78 #define SIP_TLS_MAX_CIPHERS 64
79
80 /*
81  * \brief Transport to bind to
82  */
83 struct ast_sip_transport {
84         /*! Sorcery object details */
85         SORCERY_OBJECT(details);
86         AST_DECLARE_STRING_FIELDS(
87                 /*! Certificate of authority list file */
88                 AST_STRING_FIELD(ca_list_file);
89                 /*! Public certificate file */
90                 AST_STRING_FIELD(cert_file);
91                 /*! Optional private key of the certificate file */
92                 AST_STRING_FIELD(privkey_file);
93                 /*! Password to open the private key */
94                 AST_STRING_FIELD(password);
95                 /*! External signaling address */
96                 AST_STRING_FIELD(external_signaling_address);
97                 /*! External media address */
98                 AST_STRING_FIELD(external_media_address);
99                 /*! Optional domain to use for messages if provided could not be found */
100                 AST_STRING_FIELD(domain);
101                 );
102         /*! Type of transport */
103         enum ast_transport type;
104         /*! Address and port to bind to */
105         pj_sockaddr host;
106         /*! Number of simultaneous asynchronous operations */
107         unsigned int async_operations;
108         /*! Optional external port for signaling */
109         unsigned int external_signaling_port;
110         /*! TLS settings */
111         pjsip_tls_setting tls;
112         /*! Configured TLS ciphers */
113         pj_ssl_cipher ciphers[SIP_TLS_MAX_CIPHERS];
114         /*! Optional local network information, used for NAT purposes */
115         struct ast_ha *localnet;
116         /*! DNS manager for refreshing the external address */
117         struct ast_dnsmgr_entry *external_address_refresher;
118         /*! Optional external address information */
119         struct ast_sockaddr external_address;
120         /*! Transport state information */
121         struct ast_sip_transport_state *state;
122         /*! QOS DSCP TOS bits */
123         unsigned int tos;
124         /*! QOS COS value */
125         unsigned int cos;
126 };
127
128 /*!
129  * \brief Structure for SIP nat hook information
130  */
131 struct ast_sip_nat_hook {
132         /*! Sorcery object details */
133         SORCERY_OBJECT(details);
134         /*! Callback for when a message is going outside of our local network */
135         void (*outgoing_external_message)(struct pjsip_tx_data *tdata, struct ast_sip_transport *transport);
136 };
137
138 /*!
139  * \brief Contact associated with an address of record
140  */
141 struct ast_sip_contact {
142         /*! Sorcery object details, the id is the aor name plus a random string */
143         SORCERY_OBJECT(details);
144         AST_DECLARE_STRING_FIELDS(
145                 /*! Full URI of the contact */
146                 AST_STRING_FIELD(uri);
147         );
148         /*! Absolute time that this contact is no longer valid after */
149         struct timeval expiration_time;
150         /*! Frequency to send OPTIONS requests to contact. 0 is disabled. */
151         unsigned int qualify_frequency;
152         /*! If true authenticate the qualify if needed */
153         int authenticate_qualify;
154 };
155
156 #define CONTACT_STATUS "contact_status"
157
158 /*!
159  * \brief Status type for a contact.
160  */
161 enum ast_sip_contact_status_type {
162         UNAVAILABLE,
163         AVAILABLE
164 };
165
166 /*!
167  * \brief A contact's status.
168  *
169  * \detail Maintains a contact's current status and round trip time
170  *         if available.
171  */
172 struct ast_sip_contact_status {
173         SORCERY_OBJECT(details);
174         /*! Current status for a contact (default - unavailable) */
175         enum ast_sip_contact_status_type status;
176         /*! The round trip start time set before sending a qualify request */
177         struct timeval rtt_start;
178         /*! The round trip time in microseconds */
179         int64_t rtt;
180 };
181
182 /*!
183  * \brief A SIP address of record
184  */
185 struct ast_sip_aor {
186         /*! Sorcery object details, the id is the AOR name */
187         SORCERY_OBJECT(details);
188         AST_DECLARE_STRING_FIELDS(
189                 /*! Voicemail boxes for this AOR */
190                 AST_STRING_FIELD(mailboxes);
191         );
192         /*! Minimum expiration time */
193         unsigned int minimum_expiration;
194         /*! Maximum expiration time */
195         unsigned int maximum_expiration;
196         /*! Default contact expiration if one is not provided in the contact */
197         unsigned int default_expiration;
198         /*! Frequency to send OPTIONS requests to AOR contacts. 0 is disabled. */
199         unsigned int qualify_frequency;
200         /*! If true authenticate the qualify if needed */
201         int authenticate_qualify;
202         /*! Maximum number of external contacts, 0 to disable */
203         unsigned int max_contacts;
204         /*! Whether to remove any existing contacts not related to an incoming REGISTER when it comes in */
205         unsigned int remove_existing;
206         /*! Any permanent configured contacts */
207         struct ao2_container *permanent_contacts;
208 };
209
210 /*!
211  * \brief DTMF modes for SIP endpoints
212  */
213 enum ast_sip_dtmf_mode {
214         /*! No DTMF to be used */
215         AST_SIP_DTMF_NONE,
216         /* XXX Should this be 2833 instead? */
217         /*! Use RFC 4733 events for DTMF */
218         AST_SIP_DTMF_RFC_4733,
219         /*! Use DTMF in the audio stream */
220         AST_SIP_DTMF_INBAND,
221         /*! Use SIP INFO DTMF (blech) */
222         AST_SIP_DTMF_INFO,
223 };
224
225 /*!
226  * \brief Methods of storing SIP digest authentication credentials.
227  *
228  * Note that both methods result in MD5 digest authentication being
229  * used. The two methods simply alter how Asterisk determines the
230  * credentials for a SIP authentication
231  */
232 enum ast_sip_auth_type {
233         /*! Credentials stored as a username and password combination */
234         AST_SIP_AUTH_TYPE_USER_PASS,
235         /*! Credentials stored as an MD5 sum */
236         AST_SIP_AUTH_TYPE_MD5,
237         /*! Credentials not stored this is a fake auth */
238         AST_SIP_AUTH_TYPE_ARTIFICIAL
239 };
240
241 #define SIP_SORCERY_AUTH_TYPE "auth"
242
243 struct ast_sip_auth {
244         /* Sorcery ID of the auth is its name */
245         SORCERY_OBJECT(details);
246         AST_DECLARE_STRING_FIELDS(
247                 /* Identification for these credentials */
248                 AST_STRING_FIELD(realm);
249                 /* Authentication username */
250                 AST_STRING_FIELD(auth_user);
251                 /* Authentication password */
252                 AST_STRING_FIELD(auth_pass);
253                 /* Authentication credentials in MD5 format (hash of user:realm:pass) */
254                 AST_STRING_FIELD(md5_creds);
255         );
256         /* The time period (in seconds) that a nonce may be reused */
257         unsigned int nonce_lifetime;
258         /* Used to determine what to use when authenticating */
259         enum ast_sip_auth_type type;
260 };
261
262 struct ast_sip_auth_array {
263         /*! Array of Sorcery IDs of auth sections */
264         const char **names;
265         /*! Number of credentials in the array */
266         unsigned int num;
267 };
268
269 /*!
270  * \brief Different methods by which incoming requests can be matched to endpoints
271  */
272 enum ast_sip_endpoint_identifier_type {
273         /*! Identify based on user name in From header */
274         AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME = (1 << 0),
275 };
276
277 enum ast_sip_session_refresh_method {
278         /*! Use reinvite to negotiate direct media */
279         AST_SIP_SESSION_REFRESH_METHOD_INVITE,
280         /*! Use UPDATE to negotiate direct media */
281         AST_SIP_SESSION_REFRESH_METHOD_UPDATE,
282 };
283
284 enum ast_sip_direct_media_glare_mitigation {
285         /*! Take no special action to mitigate reinvite glare */
286         AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE,
287         /*! Do not send an initial direct media session refresh on outgoing call legs
288          * Subsequent session refreshes will be sent no matter the session direction
289          */
290         AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING,
291         /*! Do not send an initial direct media session refresh on incoming call legs
292          * Subsequent session refreshes will be sent no matter the session direction
293          */
294         AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING,
295 };
296
297 enum ast_sip_session_media_encryption {
298         /*! Invalid media encryption configuration */
299         AST_SIP_MEDIA_TRANSPORT_INVALID = 0,
300         /*! Do not allow any encryption of session media */
301         AST_SIP_MEDIA_ENCRYPT_NONE,
302         /*! Offer SDES-encrypted session media */
303         AST_SIP_MEDIA_ENCRYPT_SDES,
304         /*! Offer encrypted session media with datagram TLS key exchange */
305         AST_SIP_MEDIA_ENCRYPT_DTLS,
306 };
307
308 enum ast_sip_session_redirect {
309         /*! User portion of the target URI should be used as the target in the dialplan */
310         AST_SIP_REDIRECT_USER = 0,
311         /*! Target URI should be used as the target in the dialplan */
312         AST_SIP_REDIRECT_URI_CORE,
313         /*! Target URI should be used as the target within chan_pjsip itself */
314         AST_SIP_REDIRECT_URI_PJSIP,
315 };
316
317 /*!
318  * \brief Session timers options
319  */
320 struct ast_sip_timer_options {
321         /*! Minimum session expiration period, in seconds */
322         unsigned int min_se;
323         /*! Session expiration period, in seconds */
324         unsigned int sess_expires;
325 };
326
327 /*!
328  * \brief Endpoint configuration for SIP extensions.
329  *
330  * SIP extensions, in this case refers to features
331  * indicated in Supported or Required headers.
332  */
333 struct ast_sip_endpoint_extensions {
334         /*! Enabled SIP extensions */
335         unsigned int flags;
336         /*! Timer options */
337         struct ast_sip_timer_options timer;
338 };
339
340 /*!
341  * \brief Endpoint configuration for unsolicited MWI
342  */
343 struct ast_sip_mwi_configuration {
344         AST_DECLARE_STRING_FIELDS(
345                 /*! Configured voicemail boxes for this endpoint. Used for MWI */
346                 AST_STRING_FIELD(mailboxes);
347                 /*! Username to use when sending MWI NOTIFYs to this endpoint */
348                 AST_STRING_FIELD(fromuser);
349         );
350         /* Should mailbox states be combined into a single notification? */
351         unsigned int aggregate;
352 };
353
354 /*!
355  * \brief Endpoint subscription configuration
356  */
357 struct ast_sip_endpoint_subscription_configuration {
358         /*! Indicates if endpoint is allowed to initiate subscriptions */
359         unsigned int allow;
360         /*! The minimum allowed expiration for subscriptions from endpoint */
361         unsigned int minexpiry;
362         /*! Message waiting configuration */
363         struct ast_sip_mwi_configuration mwi;
364 };
365
366 /*!
367  * \brief NAT configuration options for endpoints
368  */
369 struct ast_sip_endpoint_nat_configuration {
370         /*! Whether to force using the source IP address/port for sending responses */
371         unsigned int force_rport;
372         /*! Whether to rewrite the Contact header with the source IP address/port or not */
373         unsigned int rewrite_contact;
374 };
375
376 /*!
377  * \brief Party identification options for endpoints
378  *
379  * This includes caller ID, connected line, and redirecting-related options
380  */
381 struct ast_sip_endpoint_id_configuration {
382         struct ast_party_id self;
383         /*! Do we accept identification information from this endpoint */
384         unsigned int trust_inbound;
385         /*! Do we send private identification information to this endpoint? */
386         unsigned int trust_outbound;
387         /*! Do we send P-Asserted-Identity headers to this endpoint? */
388         unsigned int send_pai;
389         /*! Do we send Remote-Party-ID headers to this endpoint? */
390         unsigned int send_rpid;
391         /*! Do we add Diversion headers to applicable outgoing requests/responses? */
392         unsigned int send_diversion;
393         /*! When performing connected line update, which method should be used */
394         enum ast_sip_session_refresh_method refresh_method;
395 };
396
397 /*!
398  * \brief Call pickup configuration options for endpoints
399  */
400 struct ast_sip_endpoint_pickup_configuration {
401         /*! Call group */
402         ast_group_t callgroup;
403         /*! Pickup group */
404         ast_group_t pickupgroup;
405         /*! Named call group */
406         struct ast_namedgroups *named_callgroups;
407         /*! Named pickup group */
408         struct ast_namedgroups *named_pickupgroups;
409 };
410
411 /*!
412  * \brief Configuration for one-touch INFO recording
413  */
414 struct ast_sip_info_recording_configuration {
415         AST_DECLARE_STRING_FIELDS(
416                 /*! Feature to enact when one-touch recording INFO with Record: On is received */
417                 AST_STRING_FIELD(onfeature);
418                 /*! Feature to enact when one-touch recording INFO with Record: Off is received */
419                 AST_STRING_FIELD(offfeature);
420         );
421         /*! Is one-touch recording permitted? */
422         unsigned int enabled;
423 };
424
425 /*!
426  * \brief Endpoint configuration options for INFO packages
427  */
428 struct ast_sip_endpoint_info_configuration {
429         /*! Configuration for one-touch recording */
430         struct ast_sip_info_recording_configuration recording;
431 };
432
433 /*!
434  * \brief RTP configuration for SIP endpoints
435  */
436 struct ast_sip_media_rtp_configuration {
437         AST_DECLARE_STRING_FIELDS(
438                 /*! Configured RTP engine for this endpoint. */
439                 AST_STRING_FIELD(engine);
440         );
441         /*! Whether IPv6 RTP is enabled or not */
442         unsigned int ipv6;
443         /*! Whether symmetric RTP is enabled or not */
444         unsigned int symmetric;
445         /*! Whether ICE support is enabled or not */
446         unsigned int ice_support;
447         /*! Whether to use the "ptime" attribute received from the endpoint or not */
448         unsigned int use_ptime;
449         /*! Do we use AVPF exclusively for this endpoint? */
450         unsigned int use_avpf;
451         /*! \brief DTLS-SRTP configuration information */
452         struct ast_rtp_dtls_cfg dtls_cfg;
453         /*! Should SRTP use a 32 byte tag instead of an 80 byte tag? */
454         unsigned int srtp_tag_32;
455         /*! Do we use media encryption? what type? */
456         enum ast_sip_session_media_encryption encryption;
457 };
458
459 /*!
460  * \brief Direct media options for SIP endpoints
461  */
462 struct ast_sip_direct_media_configuration {
463         /*! Boolean indicating if direct_media is permissible */
464         unsigned int enabled;
465         /*! When using direct media, which method should be used */
466         enum ast_sip_session_refresh_method method;
467         /*! Take steps to mitigate glare for direct media */
468         enum ast_sip_direct_media_glare_mitigation glare_mitigation;
469         /*! Do not attempt direct media session refreshes if a media NAT is detected */
470         unsigned int disable_on_nat;
471 };
472
473 struct ast_sip_t38_configuration {
474         /*! Whether T.38 UDPTL support is enabled or not */
475         unsigned int enabled;
476         /*! Error correction setting for T.38 UDPTL */
477         enum ast_t38_ec_modes error_correction;
478         /*! Explicit T.38 max datagram value, may be 0 to indicate the remote side can be trusted */
479         unsigned int maxdatagram;
480         /*! Whether NAT Support is enabled for T.38 UDPTL sessions or not */
481         unsigned int nat;
482         /*! Whether to use IPv6 for UDPTL or not */
483         unsigned int ipv6;
484 };
485
486 /*!
487  * \brief Media configuration for SIP endpoints
488  */
489 struct ast_sip_endpoint_media_configuration {
490         AST_DECLARE_STRING_FIELDS(
491                 /*! Optional media address to use in SDP */
492                 AST_STRING_FIELD(address);
493                 /*! SDP origin username */
494                 AST_STRING_FIELD(sdpowner);
495                 /*! SDP session name */
496                 AST_STRING_FIELD(sdpsession);
497         );
498         /*! RTP media configuration */
499         struct ast_sip_media_rtp_configuration rtp;
500         /*! Direct media options */
501         struct ast_sip_direct_media_configuration direct_media;
502         /*! T.38 (FoIP) options */
503         struct ast_sip_t38_configuration t38;
504         /*! Codec preferences */
505         struct ast_codec_pref prefs;
506         /*! Configured codecs */
507         struct ast_format_cap *codecs;
508         /*! DSCP TOS bits for audio streams */
509         unsigned int tos_audio;
510         /*! Priority for audio streams */
511         unsigned int cos_audio;
512         /*! DSCP TOS bits for video streams */
513         unsigned int tos_video;
514         /*! Priority for video streams */
515         unsigned int cos_video;
516 };
517
518 /*!
519  * \brief An entity with which Asterisk communicates
520  */
521 struct ast_sip_endpoint {
522         SORCERY_OBJECT(details);
523         AST_DECLARE_STRING_FIELDS(
524                 /*! Context to send incoming calls to */
525                 AST_STRING_FIELD(context);
526                 /*! Name of an explicit transport to use */
527                 AST_STRING_FIELD(transport);
528                 /*! Outbound proxy to use */
529                 AST_STRING_FIELD(outbound_proxy);
530                 /*! Explicit AORs to dial if none are specified */
531                 AST_STRING_FIELD(aors);
532                 /*! Musiconhold class to suggest that the other side use when placing on hold */
533                 AST_STRING_FIELD(mohsuggest);
534                 /*! Configured tone zone for this endpoint. */
535                 AST_STRING_FIELD(zone);
536                 /*! Configured language for this endpoint. */
537                 AST_STRING_FIELD(language);
538                 /*! Default username to place in From header */
539                 AST_STRING_FIELD(fromuser);
540                 /*! Domain to place in From header */
541                 AST_STRING_FIELD(fromdomain);
542         );
543         /*! Configuration for extensions */
544         struct ast_sip_endpoint_extensions extensions;
545         /*! Configuration relating to media */
546         struct ast_sip_endpoint_media_configuration media;
547         /*! SUBSCRIBE/NOTIFY configuration options */
548         struct ast_sip_endpoint_subscription_configuration subscription;
549         /*! NAT configuration */
550         struct ast_sip_endpoint_nat_configuration nat;
551         /*! Party identification options */
552         struct ast_sip_endpoint_id_configuration id;
553         /*! Configuration options for INFO packages */
554         struct ast_sip_endpoint_info_configuration info;
555         /*! Call pickup configuration */
556         struct ast_sip_endpoint_pickup_configuration pickup;
557         /*! Inbound authentication credentials */
558         struct ast_sip_auth_array inbound_auths;
559         /*! Outbound authentication credentials */
560         struct ast_sip_auth_array outbound_auths;
561         /*! DTMF mode to use with this endpoint */
562         enum ast_sip_dtmf_mode dtmf;
563         /*! Method(s) by which the endpoint should be identified. */
564         enum ast_sip_endpoint_identifier_type ident_method;
565         /*! Boolean indicating if ringing should be sent as inband progress */
566         unsigned int inband_progress;
567         /*! Pointer to the persistent Asterisk endpoint */
568         struct ast_endpoint *persistent;
569         /*! The number of channels at which busy device state is returned */
570         unsigned int devicestate_busy_at;
571         /*! Whether fax detection is enabled or not (CNG tone detection) */
572         unsigned int faxdetect;
573         /*! Determines if transfers (using REFER) are allowed by this endpoint */
574         unsigned int allowtransfer;
575         /*! Method used when handling redirects */
576         enum ast_sip_session_redirect redirect_method;
577 };
578
579 /*!
580  * \brief Initialize an auth array with the configured values.
581  *
582  * \param array Array to initialize
583  * \param auth_names Comma-separated list of names to set in the array
584  * \retval 0 Success
585  * \retval non-zero Failure
586  */
587 int ast_sip_auth_array_init(struct ast_sip_auth_array *array, const char *auth_names);
588
589 /*!
590  * \brief Free contents of an auth array.
591  *
592  * \param array Array whose contents are to be freed
593  */
594 void ast_sip_auth_array_destroy(struct ast_sip_auth_array *array);
595
596 /*!
597  * \brief Possible returns from ast_sip_check_authentication
598  */
599 enum ast_sip_check_auth_result {
600     /*! Authentication needs to be challenged */
601     AST_SIP_AUTHENTICATION_CHALLENGE,
602     /*! Authentication succeeded */
603     AST_SIP_AUTHENTICATION_SUCCESS,
604     /*! Authentication failed */
605     AST_SIP_AUTHENTICATION_FAILED,
606     /*! Authentication encountered some internal error */
607     AST_SIP_AUTHENTICATION_ERROR,
608 };
609
610 /*!
611  * \brief An interchangeable way of handling digest authentication for SIP.
612  *
613  * An authenticator is responsible for filling in the callbacks provided below. Each is called from a publicly available
614  * function in res_sip. The authenticator can use configuration or other local policy to determine whether authentication
615  * should take place and what credentials should be used when challenging and authenticating a request.
616  */
617 struct ast_sip_authenticator {
618     /*!
619      * \brief Check if a request requires authentication
620      * See ast_sip_requires_authentication for more details
621      */
622     int (*requires_authentication)(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
623         /*!
624          * \brief Check that an incoming request passes authentication.
625          *
626          * The tdata parameter is useful for adding information such as digest challenges.
627          *
628          * \param endpoint The endpoint sending the incoming request
629          * \param rdata The incoming request
630          * \param tdata Tentative outgoing request.
631          */
632         enum ast_sip_check_auth_result (*check_authentication)(struct ast_sip_endpoint *endpoint,
633                         pjsip_rx_data *rdata, pjsip_tx_data *tdata);
634 };
635
636 /*!
637  * \brief an interchangeable way of responding to authentication challenges
638  *
639  * An outbound authenticator takes incoming challenges and formulates a new SIP request with
640  * credentials.
641  */
642 struct ast_sip_outbound_authenticator {
643         /*!
644          * \brief Create a new request with authentication credentials
645          *
646          * \param auths An array of IDs of auth sorcery objects
647          * \param challenge The SIP response with authentication challenge(s)
648          * \param tsx The transaction in which the challenge was received
649          * \param new_request The new SIP request with challenge response(s)
650          * \retval 0 Successfully created new request
651          * \retval -1 Failed to create a new request
652          */
653         int (*create_request_with_auth)(const struct ast_sip_auth_array *auths, struct pjsip_rx_data *challenge,
654                         struct pjsip_transaction *tsx, struct pjsip_tx_data **new_request);
655 };
656
657 /*!
658  * \brief An entity responsible for identifying the source of a SIP message
659  */
660 struct ast_sip_endpoint_identifier {
661     /*!
662      * \brief Callback used to identify the source of a message.
663      * See ast_sip_identify_endpoint for more details
664      */
665     struct ast_sip_endpoint *(*identify_endpoint)(pjsip_rx_data *rdata);
666 };
667
668 /*!
669  * \brief Register a SIP service in Asterisk.
670  *
671  * This is more-or-less a wrapper around pjsip_endpt_register_module().
672  * Registering a service makes it so that PJSIP will call into the
673  * service at appropriate times. For more information about PJSIP module
674  * callbacks, see the PJSIP documentation. Asterisk modules that call
675  * this function will likely do so at module load time.
676  *
677  * \param module The module that is to be registered with PJSIP
678  * \retval 0 Success
679  * \retval -1 Failure
680  */
681 int ast_sip_register_service(pjsip_module *module);
682
683 /*!
684  * This is the opposite of ast_sip_register_service().  Unregistering a
685  * service means that PJSIP will no longer call into the module any more.
686  * This will likely occur when an Asterisk module is unloaded.
687  *
688  * \param module The PJSIP module to unregister
689  */
690 void ast_sip_unregister_service(pjsip_module *module);
691
692 /*!
693  * \brief Register a SIP authenticator
694  *
695  * An authenticator has three main purposes:
696  * 1) Determining if authentication should be performed on an incoming request
697  * 2) Gathering credentials necessary for issuing an authentication challenge
698  * 3) Authenticating a request that has credentials
699  *
700  * Asterisk provides a default authenticator, but it may be replaced by a
701  * custom one if desired.
702  *
703  * \param auth The authenticator to register
704  * \retval 0 Success
705  * \retval -1 Failure
706  */
707 int ast_sip_register_authenticator(struct ast_sip_authenticator *auth);
708
709 /*!
710  * \brief Unregister a SIP authenticator
711  *
712  * When there is no authenticator registered, requests cannot be challenged
713  * or authenticated.
714  *
715  * \param auth The authenticator to unregister
716  */
717 void ast_sip_unregister_authenticator(struct ast_sip_authenticator *auth);
718
719  /*!
720  * \brief Register an outbound SIP authenticator
721  *
722  * An outbound authenticator is responsible for creating responses to
723  * authentication challenges by remote endpoints.
724  *
725  * \param auth The authenticator to register
726  * \retval 0 Success
727  * \retval -1 Failure
728  */
729 int ast_sip_register_outbound_authenticator(struct ast_sip_outbound_authenticator *outbound_auth);
730
731 /*!
732  * \brief Unregister an outbound SIP authenticator
733  *
734  * When there is no outbound authenticator registered, authentication challenges
735  * will be handled as any other final response would be.
736  *
737  * \param auth The authenticator to unregister
738  */
739 void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authenticator *auth);
740
741 /*!
742  * \brief Register a SIP endpoint identifier
743  *
744  * An endpoint identifier's purpose is to determine which endpoint a given SIP
745  * message has come from.
746  *
747  * Multiple endpoint identifiers may be registered so that if an endpoint
748  * cannot be identified by one identifier, it may be identified by another.
749  *
750  * Asterisk provides two endpoint identifiers. One identifies endpoints based
751  * on the user part of the From header URI. The other identifies endpoints based
752  * on the source IP address.
753  *
754  * If the order in which endpoint identifiers is run is important to you, then
755  * be sure to load individual endpoint identifier modules in the order you wish
756  * for them to be run in modules.conf
757  *
758  * \param identifier The SIP endpoint identifier to register
759  * \retval 0 Success
760  * \retval -1 Failure
761  */
762 int ast_sip_register_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier);
763
764 /*!
765  * \brief Unregister a SIP endpoint identifier
766  *
767  * This stops an endpoint identifier from being used.
768  *
769  * \param identifier The SIP endoint identifier to unregister
770  */
771 void ast_sip_unregister_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier);
772
773 /*!
774  * \brief Allocate a new SIP endpoint
775  *
776  * This will return an endpoint with its refcount increased by one. This reference
777  * can be released using ao2_ref().
778  *
779  * \param name The name of the endpoint.
780  * \retval NULL Endpoint allocation failed
781  * \retval non-NULL The newly allocated endpoint
782  */
783 void *ast_sip_endpoint_alloc(const char *name);
784
785 /*!
786  * \brief Get a pointer to the PJSIP endpoint.
787  *
788  * This is useful when modules have specific information they need
789  * to register with the PJSIP core.
790  * \retval NULL endpoint has not been created yet.
791  * \retval non-NULL PJSIP endpoint.
792  */
793 pjsip_endpoint *ast_sip_get_pjsip_endpoint(void);
794
795 /*!
796  * \brief Get a pointer to the SIP sorcery structure.
797  *
798  * \retval NULL sorcery has not been initialized
799  * \retval non-NULL sorcery structure
800  */
801 struct ast_sorcery *ast_sip_get_sorcery(void);
802
803 /*!
804  * \brief Initialize transport support on a sorcery instance
805  *
806  * \param sorcery The sorcery instance
807  *
808  * \retval -1 failure
809  * \retval 0 success
810  */
811 int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery);
812
813 /*!
814  * \brief Initialize qualify support on a sorcery instance
815  *
816  * \param sorcery The sorcery instance
817  *
818  * \retval -1 failure
819  * \retval 0 success
820  */
821 int ast_sip_initialize_sorcery_qualify(struct ast_sorcery *sorcery);
822
823 /*!
824  * \brief Initialize location support on a sorcery instance
825  *
826  * \param sorcery The sorcery instance
827  *
828  * \retval -1 failure
829  * \retval 0 success
830  */
831 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery);
832
833 /*!
834  * \brief Retrieve a named AOR
835  *
836  * \param aor_name Name of the AOR
837  *
838  * \retval NULL if not found
839  * \retval non-NULL if found
840  */
841 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name);
842
843 /*!
844  * \brief Retrieve the first bound contact for an AOR
845  *
846  * \param aor Pointer to the AOR
847  * \retval NULL if no contacts available
848  * \retval non-NULL if contacts available
849  */
850 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor);
851
852 /*!
853  * \brief Retrieve all contacts currently available for an AOR
854  *
855  * \param aor Pointer to the AOR
856  *
857  * \retval NULL if no contacts available
858  * \retval non-NULL if contacts available
859  */
860 struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor);
861
862 /*!
863  * \brief Retrieve the first bound contact from a list of AORs
864  *
865  * \param aor_list A comma-separated list of AOR names
866  * \retval NULL if no contacts available
867  * \retval non-NULL if contacts available
868  */
869 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list);
870
871 /*!
872  * \brief Retrieve a named contact
873  *
874  * \param contact_name Name of the contact
875  *
876  * \retval NULL if not found
877  * \retval non-NULL if found
878  */
879 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name);
880
881 /*!
882  * \brief Add a new contact to an AOR
883  *
884  * \param aor Pointer to the AOR
885  * \param uri Full contact URI
886  * \param expiration_time Optional expiration time of the contact
887  *
888  * \retval -1 failure
889  * \retval 0 success
890  */
891 int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time);
892
893 /*!
894  * \brief Update a contact
895  *
896  * \param contact New contact object with details
897  *
898  * \retval -1 failure
899  * \retval 0 success
900  */
901 int ast_sip_location_update_contact(struct ast_sip_contact *contact);
902
903 /*!
904 * \brief Delete a contact
905 *
906 * \param contact Contact object to delete
907 *
908 * \retval -1 failure
909 * \retval 0 success
910 */
911 int ast_sip_location_delete_contact(struct ast_sip_contact *contact);
912
913 /*!
914  * \brief Initialize domain aliases support on a sorcery instance
915  *
916  * \param sorcery The sorcery instance
917  *
918  * \retval -1 failure
919  * \retval 0 success
920  */
921 int ast_sip_initialize_sorcery_domain_alias(struct ast_sorcery *sorcery);
922
923 /*!
924  * \brief Initialize authentication support on a sorcery instance
925  *
926  * \param sorcery The sorcery instance
927  *
928  * \retval -1 failure
929  * \retval 0 success
930  */
931 int ast_sip_initialize_sorcery_auth(struct ast_sorcery *sorcery);
932
933 /*!
934  * \brief Callback called when an outbound request with authentication credentials is to be sent in dialog
935  *
936  * This callback will have the created request on it. The callback's purpose is to do any extra
937  * housekeeping that needs to be done as well as to send the request out.
938  *
939  * This callback is only necessary if working with a PJSIP API that sits between the application
940  * and the dialog layer.
941  *
942  * \param dlg The dialog to which the request belongs
943  * \param tdata The created request to be sent out
944  * \param user_data Data supplied with the callback
945  *
946  * \retval 0 Success
947  * \retval -1 Failure
948  */
949 typedef int (*ast_sip_dialog_outbound_auth_cb)(pjsip_dialog *dlg, pjsip_tx_data *tdata, void *user_data);
950
951 /*!
952  * \brief Set up outbound authentication on a SIP dialog
953  *
954  * This sets up the infrastructure so that all requests associated with a created dialog
955  * can be re-sent with authentication credentials if the original request is challenged.
956  *
957  * \param dlg The dialog on which requests will be authenticated
958  * \param endpoint The endpoint whom this dialog pertains to
959  * \param cb Callback to call to send requests with authentication
960  * \param user_data Data to be provided to the callback when it is called
961  *
962  * \retval 0 Success
963  * \retval -1 Failure
964  */
965 int ast_sip_dialog_setup_outbound_authentication(pjsip_dialog *dlg, const struct ast_sip_endpoint *endpoint,
966                 ast_sip_dialog_outbound_auth_cb cb, void *user_data);
967
968 /*!
969  * \brief Initialize the distributor module
970  *
971  * The distributor module is responsible for taking an incoming
972  * SIP message and placing it into the threadpool. Once in the threadpool,
973  * the distributor will perform endpoint lookups and authentication, and
974  * then distribute the message up the stack to any further modules.
975  *
976  * \retval -1 Failure
977  * \retval 0 Success
978  */
979 int ast_sip_initialize_distributor(void);
980
981 /*!
982  * \brief Destruct the distributor module.
983  *
984  * Unregisters pjsip modules and cleans up any allocated resources.
985  */
986 void ast_sip_destroy_distributor(void);
987
988 /*!
989  * \brief Retrieves a reference to the artificial auth.
990  *
991  * \retval The artificial auth
992  */
993 struct ast_sip_auth *ast_sip_get_artificial_auth(void);
994
995 /*!
996  * \brief Retrieves a reference to the artificial endpoint.
997  *
998  * \retval The artificial endpoint
999  */
1000 struct ast_sip_endpoint *ast_sip_get_artificial_endpoint(void);
1001
1002 /*!
1003  * \page Threading model for SIP
1004  *
1005  * There are three major types of threads that SIP will have to deal with:
1006  * \li Asterisk threads
1007  * \li PJSIP threads
1008  * \li SIP threadpool threads (a.k.a. "servants")
1009  *
1010  * \par Asterisk Threads
1011  *
1012  * Asterisk threads are those that originate from outside of SIP but within
1013  * Asterisk. The most common of these threads are PBX (channel) threads and
1014  * the autoservice thread. Most interaction with these threads will be through
1015  * channel technology callbacks. Within these threads, it is fine to handle
1016  * Asterisk data from outside of SIP, but any handling of SIP data should be
1017  * left to servants, \b especially if you wish to call into PJSIP for anything.
1018  * Asterisk threads are not registered with PJLIB, so attempting to call into
1019  * PJSIP will cause an assertion to be triggered, thus causing the program to
1020  * crash.
1021  *
1022  * \par PJSIP Threads
1023  *
1024  * PJSIP threads are those that originate from handling of PJSIP events, such
1025  * as an incoming SIP request or response, or a transaction timeout. The role
1026  * of these threads is to process information as quickly as possible so that
1027  * the next item on the SIP socket(s) can be serviced. On incoming messages,
1028  * Asterisk automatically will push the request to a servant thread. When your
1029  * module callback is called, processing will already be in a servant. However,
1030  * for other PSJIP events, such as transaction state changes due to timer
1031  * expirations, your module will be called into from a PJSIP thread. If you
1032  * are called into from a PJSIP thread, then you should push whatever processing
1033  * is needed to a servant as soon as possible. You can discern if you are currently
1034  * in a SIP servant thread using the \ref ast_sip_thread_is_servant function.
1035  *
1036  * \par Servants
1037  *
1038  * Servants are where the bulk of SIP work should be performed. These threads
1039  * exist in order to do the work that Asterisk threads and PJSIP threads hand
1040  * off to them. Servant threads register themselves with PJLIB, meaning that
1041  * they are capable of calling PJSIP and PJLIB functions if they wish.
1042  *
1043  * \par Serializer
1044  *
1045  * Tasks are handed off to servant threads using the API call \ref ast_sip_push_task.
1046  * The first parameter of this call is a serializer. If this pointer
1047  * is NULL, then the work will be handed off to whatever servant can currently handle
1048  * the task. If this pointer is non-NULL, then the task will not be executed until
1049  * previous tasks pushed with the same serializer have completed. For more information
1050  * on serializers and the benefits they provide, see \ref ast_threadpool_serializer
1051  *
1052  * \note
1053  *
1054  * Do not make assumptions about individual threads based on a corresponding serializer.
1055  * In other words, just because several tasks use the same serializer when being pushed
1056  * to servants, it does not mean that the same thread is necessarily going to execute those
1057  * tasks, even though they are all guaranteed to be executed in sequence.
1058  */
1059
1060 /*!
1061  * \brief Create a new serializer for SIP tasks
1062  *
1063  * See \ref ast_threadpool_serializer for more information on serializers.
1064  * SIP creates serializers so that tasks operating on similar data will run
1065  * in sequence.
1066  *
1067  * \retval NULL Failure
1068  * \retval non-NULL Newly-created serializer
1069  */
1070 struct ast_taskprocessor *ast_sip_create_serializer(void);
1071
1072 /*!
1073  * \brief Set a serializer on a SIP dialog so requests and responses are automatically serialized
1074  *
1075  * Passing a NULL serializer is a way to remove a serializer from a dialog.
1076  *
1077  * \param dlg The SIP dialog itself
1078  * \param serializer The serializer to use
1079  */
1080 void ast_sip_dialog_set_serializer(pjsip_dialog *dlg, struct ast_taskprocessor *serializer);
1081
1082 /*!
1083  * \brief Set an endpoint on a SIP dialog so in-dialog requests do not undergo endpoint lookup.
1084  *
1085  * \param dlg The SIP dialog itself
1086  * \param endpoint The endpoint that this dialog is communicating with
1087  */
1088 void ast_sip_dialog_set_endpoint(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint);
1089
1090 /*!
1091  * \brief Get the endpoint associated with this dialog
1092  *
1093  * This function increases the refcount of the endpoint by one. Release
1094  * the reference once you are finished with the endpoint.
1095  *
1096  * \param dlg The SIP dialog from which to retrieve the endpoint
1097  * \retval NULL No endpoint associated with this dialog
1098  * \retval non-NULL The endpoint.
1099  */
1100 struct ast_sip_endpoint *ast_sip_dialog_get_endpoint(pjsip_dialog *dlg);
1101
1102 /*!
1103  * \brief Pushes a task to SIP servants
1104  *
1105  * This uses the serializer provided to determine how to push the task.
1106  * If the serializer is NULL, then the task will be pushed to the
1107  * servants directly. If the serializer is non-NULL, then the task will be
1108  * queued behind other tasks associated with the same serializer.
1109  *
1110  * \param serializer The serializer to which the task belongs. Can be NULL
1111  * \param sip_task The task to execute
1112  * \param task_data The parameter to pass to the task when it executes
1113  * \retval 0 Success
1114  * \retval -1 Failure
1115  */
1116 int ast_sip_push_task(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data);
1117
1118 /*!
1119  * \brief Push a task to SIP servants and wait for it to complete
1120  *
1121  * Like \ref ast_sip_push_task except that it blocks until the task completes.
1122  *
1123  * \warning \b Never use this function in a SIP servant thread. This can potentially
1124  * cause a deadlock. If you are in a SIP servant thread, just call your function
1125  * in-line.
1126  *
1127  * \param serializer The SIP serializer to which the task belongs. May be NULL.
1128  * \param sip_task The task to execute
1129  * \param task_data The parameter to pass to the task when it executes
1130  * \retval 0 Success
1131  * \retval -1 Failure
1132  */
1133 int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data);
1134
1135 /*!
1136  * \brief Determine if the current thread is a SIP servant thread
1137  *
1138  * \retval 0 This is not a SIP servant thread
1139  * \retval 1 This is a SIP servant thread
1140  */
1141 int ast_sip_thread_is_servant(void);
1142
1143 /*!
1144  * \brief SIP body description
1145  *
1146  * This contains a type and subtype that will be added as
1147  * the "Content-Type" for the message as well as the body
1148  * text.
1149  */
1150 struct ast_sip_body {
1151         /*! Type of the body, such as "application" */
1152         const char *type;
1153         /*! Subtype of the body, such as "sdp" */
1154         const char *subtype;
1155         /*! The text to go in the body */
1156         const char *body_text;
1157 };
1158
1159 /*!
1160  * \brief General purpose method for creating a UAC dialog with an endpoint
1161  *
1162  * \param endpoint A pointer to the endpoint
1163  * \param aor_name Optional name of the AOR to target, may even be an explicit SIP URI
1164  * \param request_user Optional user to place into the target URI
1165  *
1166  * \retval non-NULL success
1167  * \retval NULL failure
1168  */
1169 pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, const char *aor_name, const char *request_user);
1170
1171 /*!
1172  * \brief General purpose method for creating a UAS dialog with an endpoint
1173  *
1174  * \param endpoint A pointer to the endpoint
1175  * \param rdata The request that is starting the dialog
1176  */
1177 pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
1178
1179 /*!
1180  * \brief General purpose method for creating a SIP request
1181  *
1182  * Its typical use would be to create one-off requests such as an out of dialog
1183  * SIP MESSAGE.
1184  *
1185  * The request can either be in- or out-of-dialog. If in-dialog, the
1186  * dlg parameter MUST be present. If out-of-dialog the endpoint parameter
1187  * MUST be present. If both are present, then we will assume that the message
1188  * is to be sent in-dialog.
1189  *
1190  * The uri parameter can be specified if the request should be sent to an explicit
1191  * URI rather than one configured on the endpoint.
1192  *
1193  * \param method The method of the SIP request to send
1194  * \param dlg Optional. If specified, the dialog on which to request the message.
1195  * \param endpoint Optional. If specified, the request will be created out-of-dialog
1196  * to the endpoint.
1197  * \param uri Optional. If specified, the request will be sent to this URI rather
1198  * this value.
1199  * than one configured for the endpoint.
1200  * \param[out] tdata The newly-created request
1201  * \retval 0 Success
1202  * \retval -1 Failure
1203  */
1204 int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg,
1205                 struct ast_sip_endpoint *endpoint, const char *uri,
1206                 pjsip_tx_data **tdata);
1207
1208 /*!
1209  * \brief General purpose method for sending a SIP request
1210  *
1211  * This is a companion function for \ref ast_sip_create_request. The request
1212  * created there can be passed to this function, though any request may be
1213  * passed in.
1214  *
1215  * This will automatically set up handling outbound authentication challenges if
1216  * they arrive.
1217  *
1218  * \param tdata The request to send
1219  * \param dlg Optional. If specified, the dialog on which the request should be sent
1220  * \param endpoint Optional. If specified, the request is sent out-of-dialog to the endpoint.
1221  * \retval 0 Success
1222  * \retval -1 Failure
1223  */
1224 int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint);
1225
1226 /*!
1227  * \brief Determine if an incoming request requires authentication
1228  *
1229  * This calls into the registered authenticator's requires_authentication callback
1230  * in order to determine if the request requires authentication.
1231  *
1232  * If there is no registered authenticator, then authentication will be assumed
1233  * not to be required.
1234  *
1235  * \param endpoint The endpoint from which the request originates
1236  * \param rdata The incoming SIP request
1237  * \retval non-zero The request requires authentication
1238  * \retval 0 The request does not require authentication
1239  */
1240 int ast_sip_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
1241
1242 /*!
1243  * \brief Method to determine authentication status of an incoming request
1244  *
1245  * This will call into a registered authenticator. The registered authenticator will
1246  * do what is necessary to determine whether the incoming request passes authentication.
1247  * A tentative response is passed into this function so that if, say, a digest authentication
1248  * challenge should be sent in the ensuing response, it can be added to the response.
1249  *
1250  * \param endpoint The endpoint from the request was sent
1251  * \param rdata The request to potentially authenticate
1252  * \param tdata Tentative response to the request
1253  * \return The result of checking authentication.
1254  */
1255 enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpoint *endpoint,
1256                 pjsip_rx_data *rdata, pjsip_tx_data *tdata);
1257
1258 /*!
1259  * \brief Create a response to an authentication challenge
1260  *
1261  * This will call into an outbound authenticator's create_request_with_auth callback
1262  * to create a new request with authentication credentials. See the create_request_with_auth
1263  * callback in the \ref ast_sip_outbound_authenticator structure for details about
1264  * the parameters and return values.
1265  */
1266 int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
1267                 pjsip_transaction *tsx, pjsip_tx_data **new_request);
1268
1269 /*!
1270  * \brief Determine the endpoint that has sent a SIP message
1271  *
1272  * This will call into each of the registered endpoint identifiers'
1273  * identify_endpoint() callbacks until one returns a non-NULL endpoint.
1274  * This will return an ao2 object. Its reference count will need to be
1275  * decremented when completed using the endpoint.
1276  *
1277  * \param rdata The inbound SIP message to use when identifying the endpoint.
1278  * \retval NULL No matching endpoint
1279  * \retval non-NULL The matching endpoint
1280  */
1281 struct ast_sip_endpoint *ast_sip_identify_endpoint(pjsip_rx_data *rdata);
1282
1283 /*!
1284  * \brief Add a header to an outbound SIP message
1285  *
1286  * \param tdata The message to add the header to
1287  * \param name The header name
1288  * \param value The header value
1289  * \retval 0 Success
1290  * \retval -1 Failure
1291  */
1292 int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value);
1293
1294 /*!
1295  * \brief Add a body to an outbound SIP message
1296  *
1297  * If this is called multiple times, the latest body will replace the current
1298  * body.
1299  *
1300  * \param tdata The message to add the body to
1301  * \param body The message body to add
1302  * \retval 0 Success
1303  * \retval -1 Failure
1304  */
1305 int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body);
1306
1307 /*!
1308  * \brief Add a multipart body to an outbound SIP message
1309  *
1310  * This will treat each part of the input array as part of a multipart body and
1311  * add each part to the SIP message.
1312  *
1313  * \param tdata The message to add the body to
1314  * \param bodies The parts of the body to add
1315  * \retval 0 Success
1316  * \retval -1 Failure
1317  */
1318 int ast_sip_add_body_multipart(pjsip_tx_data *tdata, const struct ast_sip_body *bodies[], int num_bodies);
1319
1320 /*!
1321  * \brief Append body data to a SIP message
1322  *
1323  * This acts mostly the same as ast_sip_add_body, except that rather than replacing
1324  * a body if it currently exists, it appends data to an existing body.
1325  *
1326  * \param tdata The message to append the body to
1327  * \param body The string to append to the end of the current body
1328  * \retval 0 Success
1329  * \retval -1 Failure
1330  */
1331 int ast_sip_append_body(pjsip_tx_data *tdata, const char *body_text);
1332
1333 /*!
1334  * \brief Copy a pj_str_t into a standard character buffer.
1335  *
1336  * pj_str_t is not NULL-terminated. Any place that expects a NULL-
1337  * terminated string needs to have the pj_str_t copied into a separate
1338  * buffer.
1339  *
1340  * This method copies the pj_str_t contents into the destination buffer
1341  * and NULL-terminates the buffer.
1342  *
1343  * \param dest The destination buffer
1344  * \param src The pj_str_t to copy
1345  * \param size The size of the destination buffer.
1346  */
1347 void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size);
1348
1349 /*!
1350  * \brief Get the looked-up endpoint on an out-of dialog request or response
1351  *
1352  * The function may ONLY be called on out-of-dialog requests or responses. For
1353  * in-dialog requests and responses, it is required that the user of the dialog
1354  * has the looked-up endpoint stored locally.
1355  *
1356  * This function should never return NULL if the message is out-of-dialog. It will
1357  * always return NULL if the message is in-dialog.
1358  *
1359  * This function will increase the reference count of the returned endpoint by one.
1360  * Release your reference using the ao2_ref function when finished.
1361  *
1362  * \param rdata Out-of-dialog request or response
1363  * \return The looked up endpoint
1364  */
1365 struct ast_sip_endpoint *ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata);
1366
1367 /*!
1368  * \brief Retrieve any endpoints available to sorcery.
1369  *
1370  * \retval Endpoints available to sorcery, NULL if no endpoints found.
1371  */
1372 struct ao2_container *ast_sip_get_endpoints(void);
1373
1374 /*!
1375  * \brief Retrieve relevant SIP auth structures from sorcery
1376  *
1377  * \param auths Array of sorcery IDs of auth credentials to retrieve
1378  * \param[out] out The retrieved auths are stored here
1379  */
1380 int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out);
1381
1382 /*!
1383  * \brief Clean up retrieved auth structures from memory
1384  *
1385  * Call this function once you have completed operating on auths
1386  * retrieved from \ref ast_sip_retrieve_auths
1387  *
1388  * \param auths An array of auth structures to clean up
1389  * \param num_auths The number of auths in the array
1390  */
1391 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths);
1392
1393 /*!
1394  * \brief Checks if the given content type matches type/subtype.
1395  *
1396  * Compares the pjsip_media_type with the passed type and subtype and
1397  * returns the result of that comparison.  The media type parameters are
1398  * ignored.
1399  *
1400  * \param content_type The pjsip_media_type structure to compare
1401  * \param type The media type to compare
1402  * \param subtype The media subtype to compare
1403  * \retval 0 No match
1404  * \retval -1 Match
1405  */
1406 int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype);
1407
1408 /*!
1409  * \brief Send a security event notification for when an invalid endpoint is requested
1410  *
1411  * \param name Name of the endpoint requested
1412  * \param rdata Received message
1413  */
1414 void ast_sip_report_invalid_endpoint(const char *name, pjsip_rx_data *rdata);
1415
1416 /*!
1417  * \brief Send a security event notification for when an ACL check fails
1418  *
1419  * \param endpoint Pointer to the endpoint in use
1420  * \param rdata Received message
1421  * \param name Name of the ACL
1422  */
1423 void ast_sip_report_failed_acl(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name);
1424
1425 /*!
1426  * \brief Send a security event notification for when a challenge response has failed
1427  *
1428  * \param endpoint Pointer to the endpoint in use
1429  * \param rdata Received message
1430  */
1431 void ast_sip_report_auth_failed_challenge_response(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
1432
1433 /*!
1434  * \brief Send a security event notification for when authentication succeeds
1435  *
1436  * \param endpoint Pointer to the endpoint in use
1437  * \param rdata Received message
1438  */
1439 void ast_sip_report_auth_success(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
1440
1441 /*!
1442  * \brief Send a security event notification for when an authentication challenge is sent
1443  *
1444  * \param endpoint Pointer to the endpoint in use
1445  * \param rdata Received message
1446  * \param tdata Sent message
1447  */
1448 void ast_sip_report_auth_challenge_sent(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata);
1449
1450 /*!
1451  * \brief Send a security event notification for when a request is not supported
1452  *
1453  * \param endpoint Pointer to the endpoint in use
1454  * \param rdata Received message
1455  * \param req_type the type of request
1456  */
1457 void ast_sip_report_req_no_support(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
1458                                    const char* req_type);
1459
1460 /*!
1461  * \brief Send a security event notification for when a memory limit is hit.
1462  *
1463  * \param endpoint Pointer to the endpoint in use
1464  * \param rdata Received message
1465  */
1466 void ast_sip_report_mem_limit(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
1467
1468 void ast_sip_initialize_global_headers(void);
1469 void ast_sip_destroy_global_headers(void);
1470
1471 int ast_sip_add_global_request_header(const char *name, const char *value, int replace);
1472 int ast_sip_add_global_response_header(const char *name, const char *value, int replace);
1473
1474 int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery);
1475
1476 /*!
1477  * \brief Retrieves the value associated with the given key.
1478  *
1479  * \param ht the hash table/dictionary to search
1480  * \param key the key to find
1481  *
1482  * \retval the value associated with the key, NULL otherwise.
1483  */
1484 void *ast_sip_dict_get(void *ht, const char *key);
1485
1486 /*!
1487  * \brief Using the dictionary stored in mod_data array at a given id,
1488  *        retrieve the value associated with the given key.
1489  *
1490  * \param mod_data a module data array
1491  * \param id the mod_data array index
1492  * \param key the key to find
1493  *
1494  * \retval the value associated with the key, NULL otherwise.
1495  */
1496 #define ast_sip_mod_data_get(mod_data, id, key)         \
1497         ast_sip_dict_get(mod_data[id], key)
1498
1499 /*!
1500  * \brief Set the value for the given key.
1501  *
1502  * Note - if the hash table does not exist one is created first, the key/value
1503  * pair is set, and the hash table returned.
1504  *
1505  * \param pool the pool to allocate memory in
1506  * \param ht the hash table/dictionary in which to store the key/value pair
1507  * \param key the key to associate a value with
1508  * \param val the value to associate with a key
1509  *
1510  * \retval the given, or newly created, hash table.
1511  */
1512 void *ast_sip_dict_set(pj_pool_t* pool, void *ht,
1513                        const char *key, void *val);
1514
1515 /*!
1516  * \brief Utilizing a mod_data array for a given id, set the value
1517  *        associated with the given key.
1518  *
1519  * For a given structure's mod_data array set the element indexed by id to
1520  * be a dictionary containing the key/val pair.
1521  *
1522  * \param pool a memory allocation pool
1523  * \param mod_data a module data array
1524  * \param id the mod_data array index
1525  * \param key the key to find
1526  * \param val the value to associate with a key
1527  */
1528 #define ast_sip_mod_data_set(pool, mod_data, id, key, val)              \
1529         mod_data[id] = ast_sip_dict_set(pool, mod_data[id], key, val)
1530
1531 /*!
1532  * \brief Function pointer for contact callbacks.
1533  */
1534 typedef int (*on_contact_t)(const struct ast_sip_aor *aor,
1535                             const struct ast_sip_contact *contact,
1536                             int last, void *arg);
1537
1538 /*!
1539  * \brief For every contact on an AOR call the given 'on_contact' handler.
1540  *
1541  * \param aor the aor containing a list of contacts to iterate
1542  * \param on_contact callback on each contact on an AOR
1543  * \param arg user data passed to handler
1544  * \retval 0 Success, non-zero on failure
1545  */
1546 int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
1547                              on_contact_t on_contact, void *arg);
1548
1549 /*!
1550  * \brief Handler used to convert a contact to a string.
1551  *
1552  * \param aor the aor containing a list of contacts to iterate
1553  * \param contact the contact to convert
1554  * \param last is this the last contact
1555  * \param arg user data passed to handler
1556  * \retval 0 Success, non-zero on failure
1557  */
1558 int ast_sip_contact_to_str(const struct ast_sip_aor *aor,
1559                            const struct ast_sip_contact *contact,
1560                            int last, void *arg);
1561
1562 /*!
1563  * \brief For every aor in the comma separated aors string call the
1564  *        given 'on_aor' handler.
1565  *
1566  * \param aors a comma separated list of aors
1567  * \param on_aor callback for each aor
1568  * \param arg user data passed to handler
1569  * \retval 0 Success, non-zero on failure
1570  */
1571 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg);
1572
1573 /*!
1574  * \brief For every auth in the array call the given 'on_auth' handler.
1575  *
1576  * \param array an array of auths
1577  * \param on_auth callback for each auth
1578  * \param arg user data passed to handler
1579  * \retval 0 Success, non-zero on failure
1580  */
1581 int ast_sip_for_each_auth(const struct ast_sip_auth_array *array,
1582                           ao2_callback_fn on_auth, void *arg);
1583
1584 /*!
1585  * \brief Converts the given auth type to a string
1586  *
1587  * \param type the auth type to convert
1588  * \retval a string representative of the auth type
1589  */
1590 const char *ast_sip_auth_type_to_str(enum ast_sip_auth_type type);
1591
1592 /*!
1593  * \brief Converts an auths array to a string of comma separated values
1594  *
1595  * \param auths an auth array
1596  * \param buf the string buffer to write the object data
1597  * \retval 0 Success, non-zero on failure
1598  */
1599 int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf);
1600
1601 /*
1602  * \brief AMI variable container
1603  */
1604 struct ast_sip_ami {
1605         /*! Manager session */
1606         struct mansession *s;
1607         /*! Manager message */
1608         const struct message *m;
1609         /*! user specified argument data */
1610         void *arg;
1611 };
1612
1613 /*!
1614  * \brief Creates a string to store AMI event data in.
1615  *
1616  * \param event the event to set
1617  * \param ami AMI session and message container
1618  * \retval an initialized ast_str or NULL on error.
1619  */
1620 struct ast_str *ast_sip_create_ami_event(const char *event,
1621                                          struct ast_sip_ami *ami);
1622
1623 /*!
1624  * \brief An entity responsible formatting endpoint information.
1625  */
1626 struct ast_sip_endpoint_formatter {
1627         /*!
1628          * \brief Callback used to format endpoint information over AMI.
1629          */
1630         int (*format_ami)(const struct ast_sip_endpoint *endpoint,
1631                           struct ast_sip_ami *ami);
1632         AST_RWLIST_ENTRY(ast_sip_endpoint_formatter) next;
1633 };
1634
1635 /*!
1636  * \brief Register an endpoint formatter.
1637  *
1638  * \param obj the formatter to register
1639  * \retval 0 Success
1640  * \retval -1 Failure
1641  */
1642 int ast_sip_register_endpoint_formatter(struct ast_sip_endpoint_formatter *obj);
1643
1644 /*!
1645  * \brief Unregister an endpoint formatter.
1646  *
1647  * \param obj the formatter to unregister
1648  */
1649 void ast_sip_unregister_endpoint_formatter(struct ast_sip_endpoint_formatter *obj);
1650
1651 /*!
1652  * \brief Converts a sorcery object to a string of object properties.
1653  *
1654  * \param obj the sorcery object to convert
1655  * \param str the string buffer to write the object data
1656  * \retval 0 Success, non-zero on failure
1657  */
1658 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf);
1659
1660 /*!
1661  * \brief Formats the endpoint and sends over AMI.
1662  *
1663  * \param endpoint the endpoint to format and send
1664  * \param endpoint ami AMI variable container
1665  * \param count the number of formatters operated on
1666  * \retval 0 Success, otherwise non-zero on error
1667  */
1668 int ast_sip_format_endpoint_ami(struct ast_sip_endpoint *endpoint,
1669                                 struct ast_sip_ami *ami, int *count);
1670
1671 /*!
1672  * \brief Format auth details for AMI.
1673  *
1674  * \param auths an auth array
1675  * \param ami ami variable container
1676  * \retval 0 Success, non-zero on failure
1677  */
1678 int ast_sip_format_auths_ami(const struct ast_sip_auth_array *auths,
1679                              struct ast_sip_ami *ami);
1680
1681 #endif /* _RES_PJSIP_H */