Add support for RFC 4662 resource list subscriptions.
[asterisk/asterisk.git] / include / asterisk / res_pjsip_pubsub.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_PUBSUB_H
20 #define _RES_PJSIP_PUBSUB_H
21
22 #include "asterisk/linkedlists.h"
23
24 /* Forward declarations */
25 struct pjsip_rx_data;
26 struct pjsip_tx_data;
27 struct pjsip_evsub;
28 struct ast_sip_endpoint;
29 struct ast_datastore;
30 struct ast_datastore_info;
31
32 /*!
33  * \brief Opaque structure representing a publication
34  */
35 struct ast_sip_publication;
36
37 enum ast_sip_publish_state {
38     /*! Publication has just been initialized */
39     AST_SIP_PUBLISH_STATE_INITIALIZED,
40     /*! Publication is currently active */
41     AST_SIP_PUBLISH_STATE_ACTIVE,
42     /*! Publication has been terminated */
43     AST_SIP_PUBLISH_STATE_TERMINATED,
44 };
45
46 /*!
47  * \brief Callbacks that publication handlers will define
48  */
49 struct ast_sip_publish_handler {
50         /*! \brief The name of the event this handler deals with */
51         const char *event_name;
52
53         /*! \brief Publications */
54         struct ao2_container *publications;
55
56         /*!
57          * \brief Called when a PUBLISH to establish a new publication arrives.
58          *
59          * \param endpoint The endpoint from whom the PUBLISH arrived.
60          * \param resource The resource whose state is being published.
61          * \param event_configuration The name of the event type configuration to use for this resource.
62          * \return Response code for the incoming PUBLISH
63          */
64         int (*new_publication)(struct ast_sip_endpoint *endpoint, const char *resource, const char *event_configuration);
65         /*!
66          * \brief Called when a publication has reached its expiration.
67          */
68         void (*publish_expire)(struct ast_sip_publication *pub);
69         /*!
70          * \brief Published resource has changed states.
71          *
72          * The state parameter can be used to take further action. For instance,
73          * if the state is AST_SIP_PUBLISH_STATE_INITIALIZED, then this is the initial
74          * PUBLISH request. This is a good time to set up datastores on the publication
75          * or any other initial needs.
76          *
77          * AST_SIP_PUBLISH_STATE_TERMINATED is used when the remote end is terminating
78          * its publication. This is a good opportunity to free any resources associated with
79          * the publication.
80          *
81          * AST_SIP_PUBLISH_STATE_ACTIVE is used when a publication that modifies state
82          * arrives.
83          *
84          * \param pub The publication whose state has changed
85          * \param body The body of the inbound PUBLISH
86          * \param state The state of the publication
87          */
88         int (*publication_state_change)(struct ast_sip_publication *pub, pjsip_msg_body *body,
89                         enum ast_sip_publish_state state);
90         AST_LIST_ENTRY(ast_sip_publish_handler) next;
91 };
92
93 /*!
94  * \brief Given a publication, get the associated endpoint
95  *
96  * \param pub The publication
97  * \retval NULL Failure
98  * \retval non-NULL The associated endpoint
99  */
100 struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub);
101
102 /*!
103  * \brief Given a publication, get the resource the publication is to
104  *
105  * \param pub The publication
106  * \return The resource
107  */
108 const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub);
109
110 /*!
111  * \brief Given a publication, get the configuration name for the event type in use
112  *
113  * \param pub The publication
114  * \return The configuration name
115  */
116 const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub);
117
118 /*!
119  * \brief Register a publish handler
120  *
121  * \retval 0 Handler was registered successfully
122  * \retval non-zero Handler was not registered successfully
123  */
124 int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler);
125
126 /*!
127  * \brief Unregister a publish handler
128  */
129 void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler);
130
131 /*!
132  * \brief Add a datastore to a SIP publication
133  *
134  * Note that SIP uses reference counted datastores. The datastore passed into this function
135  * must have been allocated using ao2_alloc() or there will be serious problems.
136  *
137  * \param publication The publication to add the datastore to
138  * \param datastore The datastore to be added to the subscription
139  * \retval 0 Success
140  * \retval -1 Failure
141  */
142 int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore);
143
144 /*!
145  * \brief Retrieve a publication datastore
146  *
147  * The datastore retrieved will have its reference count incremented. When the caller is done
148  * with the datastore, the reference counted needs to be decremented using ao2_ref().
149  *
150  * \param publication The publication from which to retrieve the datastore
151  * \param name The name of the datastore to retrieve
152  * \retval NULL Failed to find the specified datastore
153  * \retval non-NULL The specified datastore
154  */
155 struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name);
156
157 /*!
158  * \brief Remove a publication datastore from the publication
159  *
160  * This operation may cause the datastore's free() callback to be called if the reference
161  * count reaches zero.
162  *
163  * \param publication The publication to remove the datastore from
164  * \param name The name of the datastore to remove
165  */
166 void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name);
167
168 /*!
169  * \brief Opaque structure representing an RFC 3265 SIP subscription
170  */
171 struct ast_sip_subscription;
172
173 /*!
174  * \brief Role for the subscription that is being created
175  */
176 enum ast_sip_subscription_role {
177         /* Sending SUBSCRIBEs, receiving NOTIFYs */
178         AST_SIP_SUBSCRIBER,
179         /* Sending NOTIFYs, receiving SUBSCRIBEs */
180         AST_SIP_NOTIFIER,
181 };
182
183 /*!
184  * \brief Data for responses to SUBSCRIBEs and NOTIFIEs
185  *
186  * Some of PJSIP's evsub callbacks expect us to provide them
187  * with data so that they can craft a response rather than have
188  * us create our own response.
189  *
190  * Filling in the structure is optional, since the framework
191  * will automatically respond with a 200 OK response if we do
192  * not provide it with any additional data.
193  */
194 struct ast_sip_subscription_response_data {
195         /*! Status code of the response */
196         int status_code;
197         /*! Optional status text */
198         const char *status_text;
199         /*! Optional additional headers to add to the response */
200         struct ast_variable *headers;
201         /*! Optional body to add to the response */
202         struct ast_sip_body *body;
203 };
204
205 #define AST_SIP_MAX_ACCEPT 32
206 enum ast_sip_subscription_notify_reason {
207         /*! Initial NOTIFY for subscription */
208         AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED,
209         /*! Subscription has been renewed */
210         AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED,
211         /*! Subscription is being terminated */
212         AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED,
213         /*! Other unspecified reason */
214         AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER
215 };
216
217 struct ast_sip_notifier {
218         /*!
219          * \brief Default body type defined for the event package this notifier handles.
220          *
221          * Typically, a SUBSCRIBE request will contain one or more Accept headers that tell
222          * what format they expect the body of NOTIFY requests to use. However, every event
223          * package is required to define a default body format type to be used if a SUBSCRIBE
224          * request for the event contains no Accept header.
225          */
226         const char *default_accept;
227         /*!
228          * \brief Called when a SUBSCRIBE arrives attempting to establish a new subscription.
229          *
230          * The notifier is expected to return the response that should be sent to the
231          * SUBSCRIBE request.
232          *
233          * If a 200-class response is returned, then the notifier's notify_required
234          * callback will immediately be called into with a reason of
235          * AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED.
236          *
237          * \param endpoint The endpoint from which we received the SUBSCRIBE
238          * \param resource The name of the resource to which the subscription is being made
239          * \return The response code to send to the SUBSCRIBE.
240          */
241         int (*new_subscribe)(struct ast_sip_endpoint *endpoint, const char *resource);
242         /*!
243          * \brief Called when an inbound subscription has been accepted.
244          *
245          * This is a prime opportunity for notifiers to add any notifier-specific
246          * data to the subscription (such as datastores) that it needs to.
247          *
248          * \note There is no need to send a NOTIFY request when this callback
249          * is called
250          *
251          * \param sub The new subscription
252          * \retval 0 Success
253          * \retval -1 Failure
254          */
255         int (*subscription_established)(struct ast_sip_subscription *sub);
256         /*!
257          * \brief Supply data needed to create a NOTIFY body.
258          *
259          * The returned data must be an ao2 object. The caller of this function
260          * will be responsible for decrementing the refcount of the returned object
261          *
262          * \param sub The subscription
263          * \return An ao2 object that can be used to create a NOTIFY body.
264          */
265         void *(*get_notify_data)(struct ast_sip_subscription *sub);
266 };
267
268 struct ast_sip_subscriber {
269         /*!
270          * \brief A NOTIFY has been received.
271          *
272          * The body of the NOTIFY is provided so that it may be parsed and appropriate
273          * internal state change may be generated.
274          *
275          * The state can be used to determine if the subscription has been terminated
276          * by the far end or if this is just a typical resource state change.
277          *
278          * \param sub The subscription on which the NOTIFY arrived
279          * \param body The body of the NOTIFY
280          * \param state The subscription state
281          */
282         void (*state_change)(struct ast_sip_subscription *sub, pjsip_msg_body *body, enum pjsip_evsub_state state);
283 };
284
285 struct ast_sip_subscription_handler {
286         /*! The name of the event this subscriber deals with */
287         const char *event_name;
288         /*! The types of body this subscriber accepts. */
289         const char *accept[AST_SIP_MAX_ACCEPT];
290         /*!
291          * \brief Called when a subscription is to be destroyed
292          *
293          * The handler is not expected to send any sort of requests or responses
294          * during this callback. The handler MUST, however, begin the destruction
295          * process for the subscription during this callback.
296          */
297         void (*subscription_shutdown)(struct ast_sip_subscription *subscription);
298         /*!
299          * \brief Converts the subscriber to AMI
300          *
301          * \param sub The subscription
302          * \param buf The string to write AMI data
303          */
304         void (*to_ami)(struct ast_sip_subscription *sub, struct ast_str **buf);
305         /*! Subscriber callbacks for this handler */
306         struct ast_sip_subscriber *subscriber;
307         /*! Notifier callbacks for this handler */
308         struct ast_sip_notifier *notifier;
309         AST_LIST_ENTRY(ast_sip_subscription_handler) next;
310 };
311
312 /*!
313  * \brief Create a new ast_sip_subscription structure
314  *
315  * When a subscriber wishes to create a subscription, it may call this function
316  * to allocate resources and to send the initial SUBSCRIBE out.
317  *
318  * \param subscriber The subscriber that is making the request.
319  * \param endpoint The endpoint to whome the SUBSCRIBE will be sent.
320  * \param resource The resource to place in the SUBSCRIBE's Request-URI.
321  */
322 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
323                 struct ast_sip_endpoint *endpoint, const char *resource);
324
325
326 /*!
327  * \brief Get the endpoint that is associated with this subscription
328  *
329  * This function will increase the reference count of the endpoint. Be sure to
330  * release the reference to it when you are finished with the endpoint.
331  *
332  * \retval NULL Could not get endpoint
333  * \retval non-NULL The endpoint
334  */
335 struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub);
336
337 /*!
338  * \brief Get the serializer for the subscription
339  *
340  * Tasks that originate outside of a SIP servant thread should get the serializer
341  * and push the task to the serializer.
342  *
343  * \param sub The subscription
344  * \retval NULL Failure
345  * \retval non-NULL The subscription's serializer
346  */
347 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub);
348
349 /*!
350  * \brief Notify a SIP subscription of a state change.
351  *
352  * This tells the pubsub core that the state of a subscribed resource has changed.
353  * The pubsub core will generate an appropriate NOTIFY request to send to the
354  * subscriber.
355  *
356  * \param sub The subscription on which a state change is occurring.
357  * \param notify_data Event package-specific data used to create the NOTIFY body.
358  * \param terminate True if this NOTIFY is intended to terminate the subscription.
359  * \retval 0 Success
360  * \retval non-zero Failure
361  */
362 int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data, int terminate);
363
364 /*!
365  * \brief Retrieve the local URI for this subscription
366  *
367  * This is the local URI of the subscribed resource.
368  *
369  * \param sub The subscription
370  * \param[out] buf The buffer into which to store the URI.
371  * \param size The size of the buffer.
372  */
373 void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
374
375 /*!
376  * \brief Retrive the remote URI for this subscription
377  *
378  * This is the remote URI as determined by the underlying SIP dialog.
379  *
380  * \param sub The subscription
381  * \param[out] buf The buffer into which to store the URI.
382  * \param size The size of the buffer.
383  */
384 void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
385
386 /*!
387  * \brief Get the name of the subscribed resource.
388  */
389 const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub);
390
391 /*!
392  * \brief Get a header value for a subscription.
393  *
394  * For notifiers, the headers of the inbound SUBSCRIBE that started the dialog
395  * are stored on the subscription. This method allows access to the header. The
396  * return is the same as pjsip_msg_find_hdr_by_name(), meaning that it is dependent
397  * on the header being searched for.
398  *
399  * \param sub The subscription to search in.
400  * \param header The name of the header to search for.
401  * \return The discovered header, or NULL if the header cannot be found.
402  */
403 void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header);
404
405 /*!
406  * \brief Send a request created via a PJSIP evsub method
407  *
408  * Callers of this function should take care to do so within a SIP servant
409  * thread.
410  *
411  * \param sub The subscription on which to send the request
412  * \param tdata The request to send
413  * \retval 0 Success
414  * \retval non-zero Failure
415  */
416 int ast_sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata);
417
418 /*!
419  * \brief Alternative for ast_datastore_alloc()
420  *
421  * There are two major differences between this and ast_datastore_alloc()
422  * 1) This allocates a refcounted object
423  * 2) This will fill in a uid if one is not provided
424  *
425  * DO NOT call ast_datastore_free() on a datastore allocated in this
426  * way since that function will attempt to free the datastore rather
427  * than play nicely with its refcount.
428  *
429  * \param info Callbacks for datastore
430  * \param uid Identifier for datastore
431  * \retval NULL Failed to allocate datastore
432  * \retval non-NULL Newly allocated datastore
433  */
434 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid);
435
436 /*!
437  * \brief Add a datastore to a SIP subscription
438  *
439  * Note that SIP uses reference counted datastores. The datastore passed into this function
440  * must have been allocated using ao2_alloc() or there will be serious problems.
441  *
442  * \param subscription The ssubscription to add the datastore to
443  * \param datastore The datastore to be added to the subscription
444  * \retval 0 Success
445  * \retval -1 Failure
446  */
447 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore);
448
449 /*!
450  * \brief Retrieve a subscription datastore
451  *
452  * The datastore retrieved will have its reference count incremented. When the caller is done
453  * with the datastore, the reference counted needs to be decremented using ao2_ref().
454  *
455  * \param subscription The subscription from which to retrieve the datastore
456  * \param name The name of the datastore to retrieve
457  * \retval NULL Failed to find the specified datastore
458  * \retval non-NULL The specified datastore
459  */
460 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name);
461
462 /*!
463  * \brief Remove a subscription datastore from the subscription
464  *
465  * This operation may cause the datastore's free() callback to be called if the reference
466  * count reaches zero.
467  *
468  * \param subscription The subscription to remove the datastore from
469  * \param name The name of the datastore to remove
470  */
471 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name);
472
473 /*!
474  * \brief Register a subscription handler
475  *
476  * \retval 0 Handler was registered successfully
477  * \retval non-zero Handler was not registered successfully
478  */
479 int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler);
480
481 /*!
482  * \brief Unregister a subscription handler
483  */
484 void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler);
485
486 /*!
487  * \brief Pubsub body generator
488  *
489  * A body generator is responsible for taking Asterisk content
490  * and converting it into a body format to be placed in an outbound
491  * SIP NOTIFY or PUBLISH request.
492  */
493 struct ast_sip_pubsub_body_generator {
494         /*!
495          * \brief Content type
496          * In "plain/text", "plain" is the type
497          */
498         const char *type;
499         /*!
500          * \brief Content subtype
501          * In "plain/text", "text" is the subtype
502          */
503         const char *subtype;
504         /*!
505          * \brief allocate body structure.
506          *
507          * Body generators will have this method called when a NOTIFY
508          * or PUBLISH body needs to be created. The type returned depends on
509          * the type of content being produced for the body. The data parameter
510          * is provided by the subscription handler and will vary between different
511          * event types.
512          *
513          * \param data The subscription data provided by the event handler
514          * \retval non-NULL The allocated body
515          * \retval NULL Failure
516          */
517         void *(*allocate_body)(void *data);
518         /*!
519          * \brief Add content to the body of a SIP request
520          *
521          * The body of the request has already been allocated by the body generator's
522          * allocate_body callback.
523          *
524          * \param body The body of the SIP request. The type is determined by the
525          * content type.
526          * \param data The subscription data used to populate the body. The type is
527          * determined by the content type.
528          */
529         int (*generate_body_content)(void *body, void *data);
530         /*!
531          * \brief Convert the body to a string.
532          *
533          * \param body The request body.
534          * \param str The converted string form of the request body
535          */
536         void (*to_string)(void *body, struct ast_str **str);
537         /*!
538          * \brief Deallocate resources created for the body
539          *
540          * Optional callback to destroy resources allocated for the
541          * message body.
542          *
543          * \param body Body to be destroyed
544          */
545         void (*destroy_body)(void *body);
546         AST_LIST_ENTRY(ast_sip_pubsub_body_generator) list;
547 };
548
549 /*!
550  * \brief Body supplement
551  *
552  * Body supplements provide additions to bodies not already
553  * provided by body generators. This may include proprietary
554  * extensions, optional content, or other nonstandard fare.
555  */
556 struct ast_sip_pubsub_body_supplement {
557         /*!
558          * \brief Content type
559          * In "plain/text", "plain" is the type
560          */
561         const char *type;
562         /*!
563          * \brief Content subtype
564          * In "plain/text", "text" is the subtype
565          */
566         const char *subtype;
567         /*!
568          * \brief Add additional content to a SIP request body.
569          *
570          * A body generator will have already allocated a body and populated
571          * it with base data for the event. The supplement's duty is, if desired,
572          * to extend the body to have optional data beyond what a base RFC specifies.
573          *
574          * \param body The body of the SIP request. The type is determined by the
575          * body generator that allocated the body.
576          * \param data The subscription data used to populate the body. The type is
577          * determined by the content type.
578          */
579         int (*supplement_body)(void *body, void *data);
580         AST_LIST_ENTRY(ast_sip_pubsub_body_supplement) list;
581 };
582
583 /*!
584  * \since 13.0.0
585  * \brief Generate body content for a PUBLISH or NOTIFY
586  *
587  * This function takes a pre-allocated body and calls into registered body
588  * generators in order to fill in the body with appropriate details.
589  * The primary body generator will be called first, followed by the
590  * supplementary body generators
591  *
592  * \param content_type The content type of the body
593  * \param content_subtype The content subtype of the body
594  * \param data The data associated with body generation.
595  * \param[out] str The string representation of the generated body
596  * \retval 0 Success
597  * \retval non-zero Failure
598  */
599 int ast_sip_pubsub_generate_body_content(const char *content_type,
600                 const char *content_subtype, void *data, struct ast_str **str);
601
602 /*!
603  * \since 13.0.0
604  * \brief Register a body generator with the pubsub core.
605  *
606  * This may fail if an attempt is made to register a primary body supplement
607  * for a given content type if a primary body supplement for that content type
608  * has already been registered.
609  *
610  * \param generator Body generator to register
611  * \retval 0 Success
612  * \retval -1 Failure
613  */
614 int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator);
615
616 /*!
617  * \since 13.0.0
618  * \brief Unregister a body generator with the pubsub core.
619  *
620  * \param generator Body generator to unregister
621  */
622 void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator);
623
624 /*!
625  * \since 13.0.0
626  * \brief Register a body generator with the pubsub core.
627  *
628  * This may fail if an attempt is made to register a primary body supplement
629  * for a given content type if a primary body supplement for that content type
630  * has already been registered.
631  *
632  * \param generator Body generator to register
633  * \retval 0 Success
634  * \retval -1 Failure
635  */
636 int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
637
638 /*!
639  * \since 13.0.0
640  * \brief Unregister a body generator with the pubsub core.
641  *
642  * \param generator Body generator to unregister
643  */
644 void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
645
646 /*!
647  * \since 13.0.0
648  * \brief Get the body type used for this subscription
649  */
650 const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub);
651
652 /*!
653  * \since 13.0.0
654  * \brief Get the body subtype used for this subscription
655  */
656 const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub);
657
658 #endif /* RES_PJSIP_PUBSUB_H */