Abstract PJSIP-specific elements from the pubsub API.

This helps to pave the way for RLS work that is to come.
Since this is a self-contained change and subscription
tests still pass, this work is being committed directly
to trunk instead of a working branch.

ASTERISK-23865 #close
Review: https://reviewboard.asterisk.org/r/3628



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@417233 65c4cc65-6c06-0410-ace0-fbb531ad65f3
changes/97/197/1
Mark Michelson 11 years ago
parent 4a7a36a0a1
commit bc8c08c609

@ -34,6 +34,15 @@ struct ast_datastore_info;
*/ */
struct ast_sip_publication; struct ast_sip_publication;
enum ast_sip_publish_state {
/*! Publication has just been initialized */
AST_SIP_PUBLISH_STATE_INITIALIZED,
/*! Publication is currently active */
AST_SIP_PUBLISH_STATE_ACTIVE,
/*! Publication has been terminated */
AST_SIP_PUBLISH_STATE_TERMINATED,
};
/*! /*!
* \brief Callbacks that publication handlers will define * \brief Callbacks that publication handlers will define
*/ */
@ -47,60 +56,38 @@ struct ast_sip_publish_handler {
/*! /*!
* \brief Called when a PUBLISH to establish a new publication arrives. * \brief Called when a PUBLISH to establish a new publication arrives.
* *
* \param endpoint The endpoint from whom the PUBLISH arrived * \param endpoint The endpoint from whom the PUBLISH arrived.
* \param rdata The PUBLISH request * \param resource The resource whose state is being published.
* \retval NULL PUBLISH was not accepted * \return Response code for the incoming PUBLISH
* \retval non-NULL New publication
*
* \note The callback is expected to send a response for the PUBLISH in success cases.
*/ */
struct ast_sip_publication *(*new_publication)(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata); int (*new_publication)(struct ast_sip_endpoint *endpoint, const char *resource);
/*!
* \brief Called when a PUBLISH for an existing publication arrives.
*
* This PUBLISH may be intending to change state or it may be simply renewing
* the publication since the publication is nearing expiration. The callback
* is expected to send a response to the PUBLISH.
*
* \param pub The publication on which the PUBLISH arrived
* \param rdata The PUBLISH request
* \retval 0 Publication was accepted
* \retval non-zero Publication was denied
*
* \note The callback is expected to send a response for the PUBLISH.
*/
int (*publish_refresh)(struct ast_sip_publication *pub, pjsip_rx_data *rdata);
/*! /*!
* \brief Called when a publication has reached its expiration. * \brief Called when a publication has reached its expiration.
*/ */
void (*publish_expire)(struct ast_sip_publication *pub); void (*publish_expire)(struct ast_sip_publication *pub);
/*! /*!
* \brief Called when a PUBLISH arrives to terminate a publication. * \brief Published resource has changed states.
* *
* \param pub The publication that is terminating * The state parameter can be used to take further action. For instance,
* \param rdata The PUBLISH request terminating the publication * if the state is AST_SIP_PUBLISH_STATE_INITIALIZED, then this is the initial
* PUBLISH request. This is a good time to set up datastores on the publication
* or any other initial needs.
* *
* \note The callback is expected to send a response for the PUBLISH. * AST_SIP_PUBLISH_STATE_TERMINATED is used when the remote end is terminating
*/ * its publication. This is a good opportunity to free any resources associated with
void (*publish_termination)(struct ast_sip_publication *pub, pjsip_rx_data *rdata); * the publication.
AST_LIST_ENTRY(ast_sip_publish_handler) next;
};
/*!
* \brief Create a new publication
* *
* Publication handlers should call this when a PUBLISH arrives to establish a new publication. * AST_SIP_PUBLISH_STATE_ACTIVE is used when a publication that modifies state
* arrives.
* *
* \param endpoint The endpoint from whom the PUBLISHes arrive * \param pub The publication whose state has changed
* \param rdata The PUBLISH that established the publication * \param body The body of the inbound PUBLISH
* \retval NULL Failed to create a publication * \param state The state of the publication
* \retval non-NULL The newly-created publication
*/ */
struct ast_sip_publication *ast_sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata); int (*publication_state_change)(struct ast_sip_publication *pub, pjsip_msg_body *body,
enum ast_sip_publish_state state);
AST_LIST_ENTRY(ast_sip_publish_handler) next;
};
/*! /*!
* \brief Given a publication, get the associated endpoint * \brief Given a publication, get the associated endpoint
@ -111,29 +98,6 @@ struct ast_sip_publication *ast_sip_create_publication(struct ast_sip_endpoint *
*/ */
struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub); struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub);
/*!
* \brief Create a response to an inbound PUBLISH
*
* The created response must be sent using ast_sip_publication_send_response
*
* \param pub The publication
* \param status code The status code to place in the response
* \param rdata The request to which the response is being made
* \param[out] tdata The created response
*/
int ast_sip_publication_create_response(struct ast_sip_publication *pub, int status_code, pjsip_rx_data *rdata,
pjsip_tx_data **tdata);
/*!
* \brief Send a response for an inbound PUBLISH
*
* \param pub The publication
* \param rdata The request to which the response was made
* \param tdata The response to the request
*/
pj_status_t ast_sip_publication_send_response(struct ast_sip_publication *pub, pjsip_rx_data *rdata,
pjsip_tx_data *tdata);
/*! /*!
* \brief Register a publish handler * \brief Register a publish handler
* *
@ -222,19 +186,20 @@ struct ast_sip_subscription_response_data {
}; };
#define AST_SIP_MAX_ACCEPT 32 #define AST_SIP_MAX_ACCEPT 32
enum ast_sip_subscription_notify_reason {
/*! Initial NOTIFY for subscription */
AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED,
/*! Subscription has been renewed */
AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED,
/*! Subscription is being terminated */
AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED,
/*! Other unspecified reason */
AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER
};
struct ast_sip_subscription_handler { struct ast_sip_notifier {
/*! The name of the event this handler deals with */
const char *event_name;
/*! The types of body this handler accepts.
*
* \note This option has no bearing when the handler is used in the
* notifier role. When in a subscriber role, this header is used to
* populate the Accept: header of an outbound SUBSCRIBE request
*/
const char *accept[AST_SIP_MAX_ACCEPT];
/*! /*!
* \brief Default body type defined for the event package this handler handles. * \brief Default body type defined for the event package this notifier handles.
* *
* Typically, a SUBSCRIBE request will contain one or more Accept headers that tell * Typically, a SUBSCRIBE request will contain one or more Accept headers that tell
* what format they expect the body of NOTIFY requests to use. However, every event * what format they expect the body of NOTIFY requests to use. However, every event
@ -243,163 +208,96 @@ struct ast_sip_subscription_handler {
*/ */
const char *default_accept; const char *default_accept;
/*! /*!
* \brief Called when a subscription is to be destroyed * \brief Called when a SUBSCRIBE arrives attempting to establish a new subscription.
* *
* This is a subscriber and notifier callback. * The notifier is expected to return the response that should be sent to the
* SUBSCRIBE request.
* *
* The handler is not expected to send any sort of requests or responses * If a 200-class response is returned, then the notifier's notify_required
* during this callback. The handler MUST, however, begin the destruction * callback will immediately be called into with a reason of
* process for the subscription during this callback. * AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED.
*/
void (*subscription_shutdown)(struct ast_sip_subscription *subscription);
/*!
* \brief Called when a SUBSCRIBE arrives in order to create a new subscription
*
* This is a notifier callback.
*
* If the notifier wishes to accept the subscription, then it can create
* a new ast_sip_subscription to do so.
*
* If the notifier chooses to create a new subscription, then it must accept
* the incoming subscription using pjsip_evsub_accept() and it must also
* send an initial NOTIFY with the current subscription state.
* *
* \param endpoint The endpoint from which we received the SUBSCRIBE * \param endpoint The endpoint from which we received the SUBSCRIBE
* \param rdata The SUBSCRIBE request * \param resource The name of the resource to which the subscription is being made
* \retval NULL The SUBSCRIBE has not been accepted * \return The response code to send to the SUBSCRIBE.
* \retval non-NULL The newly-created subscription
*/
struct ast_sip_subscription *(*new_subscribe)(struct ast_sip_endpoint *endpoint,
pjsip_rx_data *rdata);
/*!
* \brief Called when an endpoint renews a subscription.
*
* This is a notifier callback.
*
* Because of the way that the PJSIP evsub framework works, it will automatically
* send a response to the SUBSCRIBE. However, the subscription handler must send
* a NOTIFY with the current subscription state when this callback is called.
*
* The response_data that is passed into this callback is used to craft what should
* be in the response to the incoming SUBSCRIBE. It is initialized with a 200 status
* code and all other parameters are empty.
*
* \param sub The subscription that is being renewed
* \param rdata The SUBSCRIBE request in question
* \param[out] response_data Data pertaining to the SIP response that should be
* sent to the SUBSCRIBE
*/ */
void (*resubscribe)(struct ast_sip_subscription *sub, int (*new_subscribe)(struct ast_sip_endpoint *endpoint, const char *resource);
pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data);
/*! /*!
* \brief Called when a subscription times out. * \brief The subscription is in need of a NOTIFY request.
* *
* This is a notifier callback * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED is given immediately
* after a SUBSCRIBE is accepted. This is a good opportunity for the notifier to
* perform setup duties such as establishing Stasis subscriptions or adding
* datastores to the subscription.
* *
* This indicates that the subscription has timed out. The subscription handler is * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED is given when the
* expected to send a NOTIFY that terminates the subscription. * subscriber has terminated the subscription. If there are any duties that the
*
* \param sub The subscription that has timed out
*/
void (*subscription_timeout)(struct ast_sip_subscription *sub);
/*!
* \brief Called when a subscription is terminated via a SUBSCRIBE or NOTIFY request
*
* This is a notifier and subscriber callback.
*
* The PJSIP subscription framework will automatically send the response to the
* request. If a notifier receives this callback, then the subscription handler
* is expected to send a final NOTIFY to terminate the subscription.
*
* \param sub The subscription being terminated
* \param rdata The request that terminated the subscription
*/
void (*subscription_terminated)(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
/*!
* \brief Called when a subscription handler's outbound NOTIFY receives a response
* *
* This is a notifier callback.
* *
* \param sub The subscription * \param sub The subscription to send the NOTIFY on.
* \param rdata The NOTIFY response * \param reason The reason why the NOTIFY is being sent.
* \retval 0 Success
* \retval -1 Failure
*/ */
void (*notify_response)(struct ast_sip_subscription *sub, pjsip_rx_data *rdata); int (*notify_required)(struct ast_sip_subscription *sub, enum ast_sip_subscription_notify_reason reason);
};
struct ast_sip_subscriber {
/*! /*!
* \brief Called when a subscription handler receives an inbound NOTIFY * \brief A NOTIFY has been received.
*
* This is a subscriber callback.
* *
* Because of the way that the PJSIP evsub framework works, it will automatically * The body of the NOTIFY is provided so that it may be parsed and appropriate
* send a response to the NOTIFY. By default this will be a 200 OK response, but * internal state change may be generated.
* this callback can change details of the response by returning response data
* to use.
* *
* The response_data that is passed into this callback is used to craft what should * The state can be used to determine if the subscription has been terminated
* be in the response to the incoming SUBSCRIBE. It is initialized with a 200 status * by the far end or if this is just a typical resource state change.
* code and all other parameters are empty.
* *
* \param sub The subscription * \param sub The subscription on which the NOTIFY arrived
* \param rdata The NOTIFY request * \param body The body of the NOTIFY
* \param[out] response_data Data pertaining to the SIP response that should be * \param state The subscription state
* sent to the SUBSCRIBE
*/ */
void (*notify_request)(struct ast_sip_subscription *sub, void (*state_change)(struct ast_sip_subscription *sub, pjsip_msg_body *body, enum pjsip_evsub_state state);
pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data); };
struct ast_sip_subscription_handler {
/*! The name of the event this subscriber deals with */
const char *event_name;
/*! The types of body this subscriber accepts. */
const char *accept[AST_SIP_MAX_ACCEPT];
/*! /*!
* \brief Called when it is time for a subscriber to resubscribe * \brief Called when a subscription is to be destroyed
*
* This is a subscriber callback.
*
* The subscriber can reresh the subscription using the pjsip_evsub_initiate()
* function.
* *
* \param sub The subscription to refresh * The handler is not expected to send any sort of requests or responses
* \retval 0 Success * during this callback. The handler MUST, however, begin the destruction
* \retval non-zero Failure * process for the subscription during this callback.
*/ */
int (*refresh_subscription)(struct ast_sip_subscription *sub); void (*subscription_shutdown)(struct ast_sip_subscription *subscription);
/*! /*!
* \brief Converts the subscriber to AMI * \brief Converts the subscriber to AMI
* *
* This is a subscriber callback.
*
* \param sub The subscription * \param sub The subscription
* \param buf The string to write AMI data * \param buf The string to write AMI data
*/ */
void (*to_ami)(struct ast_sip_subscription *sub, struct ast_str **buf); void (*to_ami)(struct ast_sip_subscription *sub, struct ast_str **buf);
/*! Subscriber callbacks for this handler */
struct ast_sip_subscriber *subscriber;
/*! Notifier callbacks for this handler */
struct ast_sip_notifier *notifier;
AST_LIST_ENTRY(ast_sip_subscription_handler) next; AST_LIST_ENTRY(ast_sip_subscription_handler) next;
}; };
/*! /*!
* \brief Create a new ast_sip_subscription structure * \brief Create a new ast_sip_subscription structure
* *
* In most cases the pubsub core will create a general purpose subscription * When a subscriber wishes to create a subscription, it may call this function
* within PJSIP. However, PJSIP provides enhanced support for the following * to allocate resources and to send the initial SUBSCRIBE out.
* event packages:
*
* presence
* message-summary
* *
* If either of these events are handled by the subscription handler, then * \param subscriber The subscriber that is making the request.
* the special-purpose event subscriptions will be created within PJSIP, * \param endpoint The endpoint to whome the SUBSCRIBE will be sent.
* and it will be expected that your subscription handler make use of the * \param resource The resource to place in the SUBSCRIBE's Request-URI.
* special PJSIP APIs.
*
* \param handler The subsription handler for this subscription
* \param role Whether we are acting as subscriber or notifier for this subscription
* \param endpoint The endpoint involved in this subscription
* \param rdata If acting as a notifier, the SUBSCRIBE request that triggered subscription creation
*/ */
struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler, struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
enum ast_sip_subscription_role role, struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata); struct ast_sip_endpoint *endpoint, const char *resource);
/*! /*!
@ -426,46 +324,61 @@ struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscr
struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub); struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub);
/*! /*!
* \brief Get the underlying PJSIP evsub structure * \brief Notify a SIP subscription of a state change.
* *
* This is useful when wishing to call PJSIP's API calls in order to * This will create a NOTIFY body to be sent out for the subscribed resource.
* create SUBSCRIBEs, NOTIFIES, etc. as well as get subscription state * On real subscriptions, a NOTIFY request will be generated and sent.
* On virtual subscriptions, the NOTIFY is saved on the virtual subscription and the
* parent subscription is alerted.
* *
* This function, as well as all methods called on the pjsip_evsub should * \param sub The subscription on which a state change is occurring.
* be done in a SIP servant thread. * \param notify_data Event package-specific data used to create the NOTIFY body.
* \param terminate True if this NOTIFY is intended to terminate the subscription.
* \retval 0 Success
* \retval non-zero Failure
*/
int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data, int terminate);
/*!
* \brief Retrieve the local URI for this subscription
*
* This is the local URI as determined by the underlying SIP dialog.
* *
* \param sub The subscription * \param sub The subscription
* \retval NULL Failure * \param[out] buf The buffer into which to store the URI.
* \retval non-NULL The underlying pjsip_evsub * \param size The size of the buffer.
*/ */
pjsip_evsub *ast_sip_subscription_get_evsub(struct ast_sip_subscription *sub); void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
/*! /*!
* \brief Get the underlying PJSIP dialog structure * \brief Retrive the remote URI for this subscription
*
* Call this function when information needs to be retrieved from the
* underlying pjsip dialog.
* *
* This function, as well as all methods called on the pjsip_evsub should * This is the remote URI as determined by the underlying SIP dialog.
* be done in a SIP servant thread.
* *
* \param sub The subscription * \param sub The subscription
* \retval NULL Failure * \param[out] buf The buffer into which to store the URI.
* \retval non-NULL The underlying pjsip_dialog * \param size The size of the buffer.
*/
void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
/*!
* \brief Get the name of the subscribed resource.
*/ */
pjsip_dialog *ast_sip_subscription_get_dlg(struct ast_sip_subscription *sub); const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub);
/*! /*!
* \brief Accept a subscription request * \brief Get a header value for a subscription.
* *
* \param sub The subscription to be accepted * For notifiers, the headers of the inbound SUBSCRIBE that started the dialog
* \param rdata The received subscription request * are stored on the subscription. This method allows access to the header. The
* \param response The response code to send * return is the same as pjsip_msg_find_hdr_by_name(), meaning that it is dependent
* on the header being searched for.
* *
* \retval 0 Success * \param sub The subscription to search in.
* \retval non-zero Failure * \param header The name of the header to search for.
* \return The discovered header, or NULL if the header cannot be found.
*/ */
int ast_sip_subscription_accept(struct ast_sip_subscription *sub, pjsip_rx_data *rdata, int response); void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header);
/*! /*!
* \brief Send a request created via a PJSIP evsub method * \brief Send a request created via a PJSIP evsub method

@ -68,26 +68,24 @@ struct exten_state_subscription {
#define DEFAULT_PRESENCE_BODY "application/pidf+xml" #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
static void subscription_shutdown(struct ast_sip_subscription *sub); static void subscription_shutdown(struct ast_sip_subscription *sub);
static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint, static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
pjsip_rx_data *rdata); static int notify_required(struct ast_sip_subscription *sub,
static void resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata, enum ast_sip_subscription_notify_reason reason);
struct ast_sip_subscription_response_data *response_data);
static void subscription_timeout(struct ast_sip_subscription *sub);
static void subscription_terminated(struct ast_sip_subscription *sub,
pjsip_rx_data *rdata);
static void to_ami(struct ast_sip_subscription *sub, static void to_ami(struct ast_sip_subscription *sub,
struct ast_str **buf); struct ast_str **buf);
struct ast_sip_notifier presence_notifier = {
.default_accept = DEFAULT_PRESENCE_BODY,
.new_subscribe = new_subscribe,
.notify_required = notify_required,
};
struct ast_sip_subscription_handler presence_handler = { struct ast_sip_subscription_handler presence_handler = {
.event_name = "presence", .event_name = "presence",
.accept = { DEFAULT_PRESENCE_BODY, }, .accept = { DEFAULT_PRESENCE_BODY, },
.default_accept = DEFAULT_PRESENCE_BODY,
.subscription_shutdown = subscription_shutdown, .subscription_shutdown = subscription_shutdown,
.new_subscribe = new_subscribe,
.resubscribe = resubscribe,
.subscription_timeout = subscription_timeout,
.subscription_terminated = subscription_terminated,
.to_ami = to_ami, .to_ami = to_ami,
.notifier = &presence_notifier,
}; };
static void exten_state_subscription_destructor(void *obj) static void exten_state_subscription_destructor(void *obj)
@ -98,14 +96,12 @@ static void exten_state_subscription_destructor(void *obj)
ao2_cleanup(sub->sip_sub); ao2_cleanup(sub->sip_sub);
} }
static char *get_user_agent(pjsip_rx_data *rdata) static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
{ {
static const pj_str_t USER_AGENT = { "User-Agent", 10 };
size_t size; size_t size;
char *user_agent = NULL; char *user_agent = NULL;
pjsip_user_agent_hdr *user_agent_hdr = pjsip_msg_find_hdr_by_name( pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
rdata->msg_info.msg, &USER_AGENT, NULL); sip_sub, "User-Agent");
if (!user_agent_hdr) { if (!user_agent_hdr) {
return NULL; return NULL;
@ -132,85 +128,30 @@ static char *get_user_agent(pjsip_rx_data *rdata)
* sure that there are registered handler and provider objects available. * sure that there are registered handler and provider objects available.
*/ */
static struct exten_state_subscription *exten_state_subscription_alloc( static struct exten_state_subscription *exten_state_subscription_alloc(
struct ast_sip_endpoint *endpoint, enum ast_sip_subscription_role role, pjsip_rx_data *rdata) struct ast_sip_subscription *sip_sub, struct ast_sip_endpoint *endpoint)
{ {
RAII_VAR(struct exten_state_subscription *, exten_state_sub, struct exten_state_subscription * exten_state_sub;
ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor), ao2_cleanup);
exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
if (!exten_state_sub) { if (!exten_state_sub) {
return NULL; return NULL;
} }
if (!(exten_state_sub->sip_sub = ast_sip_create_subscription( exten_state_sub->sip_sub = ao2_bump(sip_sub);
&presence_handler, role, endpoint, rdata))) {
ast_log(LOG_WARNING, "Unable to create SIP subscription for endpoint %s\n",
ast_sorcery_object_get_id(endpoint));
return NULL;
}
exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE; exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET; exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
exten_state_sub->user_agent = get_user_agent(rdata); exten_state_sub->user_agent = get_user_agent(sip_sub);
ao2_ref(exten_state_sub, +1);
return exten_state_sub; return exten_state_sub;
} }
/*!
* \internal
* \brief Create and send a NOTIFY request to the subscriber.
*/
static void create_send_notify(struct exten_state_subscription *exten_state_sub, const char *reason,
pjsip_evsub_state evsub_state, struct ast_sip_exten_state_data *exten_state_data)
{
RAII_VAR(struct ast_str *, body_text, ast_str_create(BODY_SIZE), ast_free_ptr);
pj_str_t reason_str;
const pj_str_t *reason_str_ptr = NULL;
pjsip_tx_data *tdata;
struct ast_sip_body body;
body.type = ast_sip_subscription_get_body_type(exten_state_sub->sip_sub);
body.subtype = ast_sip_subscription_get_body_subtype(exten_state_sub->sip_sub);
if (ast_sip_pubsub_generate_body_content(body.type, body.subtype,
exten_state_data, &body_text)) {
ast_log(LOG_ERROR, "Unable to create body on NOTIFY request\n");
return;
}
body.body_text = ast_str_buffer(body_text);
if (reason) {
pj_cstr(&reason_str, reason);
reason_str_ptr = &reason_str;
}
if (pjsip_evsub_notify(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub),
evsub_state, NULL, reason_str_ptr, &tdata) != PJ_SUCCESS) {
ast_log(LOG_WARNING, "Unable to create NOTIFY request\n");
return;
}
if (ast_sip_add_body(tdata, &body)) {
ast_log(LOG_WARNING, "Unable to add body to NOTIFY request\n");
pjsip_tx_data_dec_ref(tdata);
return;
}
if (ast_sip_subscription_send_request(exten_state_sub->sip_sub, tdata) != PJ_SUCCESS) {
ast_log(LOG_WARNING, "Unable to send NOTIFY request\n");
}
}
/*! /*!
* \internal * \internal
* \brief Get device state information and send notification to the subscriber. * \brief Get device state information and send notification to the subscriber.
*/ */
static void send_notify(struct exten_state_subscription *exten_state_sub, const char *reason, static void send_notify(struct exten_state_subscription *exten_state_sub)
pjsip_evsub_state evsub_state)
{ {
RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup); RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup);
char *subtype = NULL, *message = NULL; char *subtype = NULL, *message = NULL;
pjsip_dialog *dlg;
struct ast_sip_exten_state_data exten_state_data = { struct ast_sip_exten_state_data exten_state_data = {
.exten = exten_state_sub->exten, .exten = exten_state_sub->exten,
.presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, .presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
@ -220,11 +161,10 @@ static void send_notify(struct exten_state_subscription *exten_state_sub, const
.user_agent = exten_state_sub->user_agent .user_agent = exten_state_sub->user_agent
}; };
dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub); ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
ast_copy_pj_str(exten_state_data.local, &dlg->local.info_str, exten_state_data.local, sizeof(exten_state_data.local));
sizeof(exten_state_data.local)); ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
ast_copy_pj_str(exten_state_data.remote, &dlg->remote.info_str, exten_state_data.remote, sizeof(exten_state_data.remote));
sizeof(exten_state_data.remote));
if ((exten_state_data.exten_state = ast_extension_state_extended( if ((exten_state_data.exten_state = ast_extension_state_extended(
NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) { NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) {
@ -238,14 +178,14 @@ static void send_notify(struct exten_state_subscription *exten_state_sub, const
"exten_state", 1024, 1024); "exten_state", 1024, 1024);
exten_state_data.device_state_info = info; exten_state_data.device_state_info = info;
create_send_notify(exten_state_sub, reason, evsub_state, &exten_state_data); ast_sip_subscription_notify(exten_state_sub->sip_sub, &exten_state_data, 0);
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data.pool); pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data.pool);
} }
struct notify_task_data { struct notify_task_data {
struct ast_sip_exten_state_data exten_state_data; struct ast_sip_exten_state_data exten_state_data;
struct exten_state_subscription *exten_state_sub; struct exten_state_subscription *exten_state_sub;
pjsip_evsub_state evsub_state; int terminate;
}; };
static void notify_task_data_destructor(void *obj) static void notify_task_data_destructor(void *obj)
@ -264,14 +204,12 @@ static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten
{ {
struct notify_task_data *task_data = struct notify_task_data *task_data =
ao2_alloc(sizeof(*task_data), notify_task_data_destructor); ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
struct pjsip_dialog *dlg;
if (!task_data) { if (!task_data) {
ast_log(LOG_WARNING, "Unable to create notify task data\n"); ast_log(LOG_WARNING, "Unable to create notify task data\n");
return NULL; return NULL;
} }
task_data->evsub_state = PJSIP_EVSUB_STATE_ACTIVE;
task_data->exten_state_sub = exten_state_sub; task_data->exten_state_sub = exten_state_sub;
task_data->exten_state_sub->last_exten_state = info->exten_state; task_data->exten_state_sub->last_exten_state = info->exten_state;
task_data->exten_state_sub->last_presence_state = info->presence_state; task_data->exten_state_sub->last_presence_state = info->presence_state;
@ -289,17 +227,16 @@ static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten
ao2_ref(task_data->exten_state_data.device_state_info, +1); ao2_ref(task_data->exten_state_data.device_state_info, +1);
} }
dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub); ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
ast_copy_pj_str(task_data->exten_state_data.local, &dlg->local.info_str, task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
sizeof(task_data->exten_state_data.local)); ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
ast_copy_pj_str(task_data->exten_state_data.remote, &dlg->remote.info_str, task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
sizeof(task_data->exten_state_data.remote));
if ((info->exten_state == AST_EXTENSION_DEACTIVATED) || if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
(info->exten_state == AST_EXTENSION_REMOVED)) { (info->exten_state == AST_EXTENSION_REMOVED)) {
task_data->evsub_state = PJSIP_EVSUB_STATE_TERMINATED;
ast_log(LOG_WARNING, "Watcher for hint %s %s\n", exten, info->exten_state ast_log(LOG_WARNING, "Watcher for hint %s %s\n", exten, info->exten_state
== AST_EXTENSION_REMOVED ? "removed" : "deactivated"); == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
task_data->terminate = 1;
} }
return task_data; return task_data;
@ -313,9 +250,8 @@ static int notify_task(void *obj)
task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
"exten_state", 1024, 1024); "exten_state", 1024, 1024);
create_send_notify(task_data->exten_state_sub, task_data->evsub_state == ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &task_data->exten_state_data,
PJSIP_EVSUB_STATE_TERMINATED ? "noresource" : NULL, task_data->terminate);
task_data->evsub_state, &task_data->exten_state_data);
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
task_data->exten_state_data.pool); task_data->exten_state_data.pool);
@ -407,24 +343,30 @@ static void subscription_shutdown(struct ast_sip_subscription *sub)
ao2_cleanup(exten_state_sub); ao2_cleanup(exten_state_sub);
} }
static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint, static int new_subscribe(struct ast_sip_endpoint *endpoint,
pjsip_rx_data *rdata) const char *resource)
{ {
pjsip_uri *uri = rdata->msg_info.msg->line.req.uri; if (!ast_exists_extension(NULL, endpoint->context, resource, PRIORITY_HINT, NULL)) {
pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(uri); ast_log(LOG_WARNING, "Extension %s does not exist or has no associated hint\n", resource);
RAII_VAR(struct exten_state_subscription *, exten_state_sub, NULL, ao2_cleanup); return 404;
if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) {
ast_log(LOG_WARNING, "Attempt to SUBSCRIBE to a non-SIP URI\n");
return NULL;
} }
if (!(exten_state_sub = exten_state_subscription_alloc(endpoint, AST_SIP_NOTIFIER, rdata))) { return 200;
return NULL; }
static int initial_subscribe(struct ast_sip_subscription *sip_sub)
{
struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
struct exten_state_subscription *exten_state_sub;
if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
ao2_cleanup(endpoint);
return -1;
} }
ast_copy_string(exten_state_sub->context, endpoint->context, sizeof(exten_state_sub->context)); ast_copy_string(exten_state_sub->context, endpoint->context, sizeof(exten_state_sub->context));
ast_copy_pj_str(exten_state_sub->exten, &sip_uri->user, sizeof(exten_state_sub->exten)); ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
if ((exten_state_sub->id = ast_extension_state_add_destroy_extended( if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
exten_state_sub->context, exten_state_sub->exten, exten_state_sub->context, exten_state_sub->exten,
@ -432,64 +374,50 @@ static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpo
ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n", ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
ast_sorcery_object_get_id(endpoint), exten_state_sub->exten, ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
exten_state_sub->context); exten_state_sub->context);
pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE); ao2_cleanup(endpoint);
return NULL; ao2_cleanup(exten_state_sub);
return -1;
} }
/* Go ahead and cleanup the endpoint since we don't need it anymore */
ao2_cleanup(endpoint);
/* bump the ref since ast_extension_state_add holds a reference */ /* bump the ref since ast_extension_state_add holds a reference */
ao2_ref(exten_state_sub, +1); ao2_ref(exten_state_sub, +1);
if (add_datastore(exten_state_sub)) { if (add_datastore(exten_state_sub)) {
ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n"); ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE); ao2_cleanup(exten_state_sub);
return NULL; return -1;
}
if (ast_sip_subscription_accept(exten_state_sub->sip_sub, rdata, 200)) {
ast_log(LOG_WARNING, "Unable to accept the incoming extension state subscription.\n");
pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE);
return NULL;
} }
send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_ACTIVE); send_notify(exten_state_sub);
return exten_state_sub->sip_sub; ao2_cleanup(exten_state_sub);
return 0;
} }
static void resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata, static int notify_required(struct ast_sip_subscription *sub,
struct ast_sip_subscription_response_data *response_data) enum ast_sip_subscription_notify_reason reason)
{ {
struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub); struct exten_state_subscription *exten_state_sub;
if (!exten_state_sub) {
return;
}
send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_ACTIVE); switch (reason) {
} case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED:
return initial_subscribe(sub);
static void subscription_timeout(struct ast_sip_subscription *sub) case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED:
{ case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED:
struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub); case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER:
exten_state_sub = get_exten_state_sub(sub);
if (!exten_state_sub) { if (!exten_state_sub) {
return; return -1;
} }
ast_verbose(VERBOSE_PREFIX_3 "Subscription has timed out.\n"); send_notify(exten_state_sub);
send_notify(exten_state_sub, "timeout", PJSIP_EVSUB_STATE_TERMINATED); break;
}
static void subscription_terminated(struct ast_sip_subscription *sub,
pjsip_rx_data *rdata)
{
struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
if (!exten_state_sub) {
return;
} }
ast_verbose(VERBOSE_PREFIX_3 "Subscription has been terminated.\n"); return 0;
send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_TERMINATED);
} }
static void to_ami(struct ast_sip_subscription *sub, static void to_ami(struct ast_sip_subscription *sub,

@ -49,31 +49,24 @@ AO2_GLOBAL_OBJ_STATIC(unsolicited_mwi);
#define MWI_SUBTYPE "simple-message-summary" #define MWI_SUBTYPE "simple-message-summary"
static void mwi_subscription_shutdown(struct ast_sip_subscription *sub); static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
pjsip_rx_data *rdata);
static void mwi_resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
struct ast_sip_subscription_response_data *response_data);
static void mwi_subscription_timeout(struct ast_sip_subscription *sub);
static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
struct ast_sip_subscription_response_data *response_data);
static int mwi_refresh_subscription(struct ast_sip_subscription *sub);
static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf); static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
const char *resource);
static int mwi_notify_required(struct ast_sip_subscription *sip_sub,
enum ast_sip_subscription_notify_reason reason);
static struct ast_sip_notifier mwi_notifier = {
.default_accept = MWI_TYPE"/"MWI_SUBTYPE,
.new_subscribe = mwi_new_subscribe,
.notify_required = mwi_notify_required,
};
static struct ast_sip_subscription_handler mwi_handler = { static struct ast_sip_subscription_handler mwi_handler = {
.event_name = "message-summary", .event_name = "message-summary",
.accept = { MWI_TYPE"/"MWI_SUBTYPE, }, .accept = { MWI_TYPE"/"MWI_SUBTYPE, },
.default_accept = MWI_TYPE"/"MWI_SUBTYPE,
.subscription_shutdown = mwi_subscription_shutdown, .subscription_shutdown = mwi_subscription_shutdown,
.new_subscribe = mwi_new_subscribe,
.resubscribe = mwi_resubscribe,
.subscription_timeout = mwi_subscription_timeout,
.subscription_terminated = mwi_subscription_terminated,
.notify_response = mwi_notify_response,
.notify_request = mwi_notify_request,
.refresh_subscription = mwi_refresh_subscription,
.to_ami = mwi_to_ami, .to_ami = mwi_to_ami,
.notifier = &mwi_notifier,
}; };
/*! /*!
@ -202,7 +195,7 @@ static void mwi_subscription_destructor(void *obj)
} }
static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint, static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
enum ast_sip_subscription_role role, unsigned int is_solicited, pjsip_rx_data *rdata) unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
{ {
struct mwi_subscription *sub; struct mwi_subscription *sub;
const char *endpoint_id = ast_sorcery_object_get_id(endpoint); const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
@ -216,6 +209,7 @@ static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *
/* Safe strcpy */ /* Safe strcpy */
strcpy(sub->id, endpoint_id); strcpy(sub->id, endpoint_id);
/* Unsolicited MWI doesn't actually result in a SIP subscription being /* Unsolicited MWI doesn't actually result in a SIP subscription being
* created. This is because a SIP subscription associates with a dialog. * created. This is because a SIP subscription associates with a dialog.
* Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
@ -224,13 +218,7 @@ static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *
* state not being updated on the device * state not being updated on the device
*/ */
if (is_solicited) { if (is_solicited) {
sub->sip_sub = ast_sip_create_subscription(&mwi_handler, sub->sip_sub = ao2_bump(sip_sub);
role, endpoint, rdata);
if (!sub->sip_sub) {
ast_log(LOG_WARNING, "Unable to create MWI SIP subscription for endpoint %s\n", sub->id);
ao2_cleanup(sub);
return NULL;
}
} }
sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp); sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
@ -314,7 +302,6 @@ struct unsolicited_mwi_data {
struct mwi_subscription *sub; struct mwi_subscription *sub;
struct ast_sip_endpoint *endpoint; struct ast_sip_endpoint *endpoint;
pjsip_evsub_state state; pjsip_evsub_state state;
const char *reason;
const struct ast_sip_body *body; const struct ast_sip_body *body;
}; };
@ -324,7 +311,6 @@ static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flag
struct mwi_subscription *sub = mwi_data->sub; struct mwi_subscription *sub = mwi_data->sub;
struct ast_sip_endpoint *endpoint = mwi_data->endpoint; struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
pjsip_evsub_state state = mwi_data->state; pjsip_evsub_state state = mwi_data->state;
const char *reason = mwi_data->reason;
const struct ast_sip_body *body = mwi_data->body; const struct ast_sip_body *body = mwi_data->body;
struct ast_sip_contact *contact = obj; struct ast_sip_contact *contact = obj;
const char *state_name; const char *state_name;
@ -358,9 +344,6 @@ static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flag
sub_state = pjsip_sub_state_hdr_create(tdata->pool); sub_state = pjsip_sub_state_hdr_create(tdata->pool);
pj_cstr(&sub_state->sub_state, state_name); pj_cstr(&sub_state->sub_state, state_name);
if (reason) {
pj_cstr(&sub_state->reason_param, reason);
}
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state); pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
event = pjsip_event_hdr_create(tdata->pool); event = pjsip_event_hdr_create(tdata->pool);
@ -374,13 +357,15 @@ static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flag
return 0; return 0;
} }
static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason, static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
struct ast_sip_body *body) struct ast_sip_message_accumulator *counter)
{ {
RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
"endpoint", sub->id), ao2_cleanup); "endpoint", sub->id), ao2_cleanup);
char *endpoint_aors; char *endpoint_aors;
char *aor_name; char *aor_name;
struct ast_sip_body body;
struct ast_str *body_text;
if (!endpoint) { if (!endpoint) {
ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n", ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
@ -393,17 +378,35 @@ static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsu
return; return;
} }
body.type = MWI_TYPE;
body.subtype = MWI_SUBTYPE;
body_text = ast_str_create(64);
if (!body_text) {
return;
}
if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, counter, &body_text)) {
ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
ast_free(body_text);
return;
}
body.body_text = ast_str_buffer(body_text);
endpoint_aors = ast_strdupa(endpoint->aors); endpoint_aors = ast_strdupa(endpoint->aors);
ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
sub->id, counter->new_msgs, counter->old_msgs);
while ((aor_name = strsep(&endpoint_aors, ","))) { while ((aor_name = strsep(&endpoint_aors, ","))) {
RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
struct unsolicited_mwi_data mwi_data = { struct unsolicited_mwi_data mwi_data = {
.sub = sub, .sub = sub,
.endpoint = endpoint, .endpoint = endpoint,
.state = state, .body = &body,
.reason = reason,
.body = body,
}; };
if (!aor) { if (!aor) {
@ -419,63 +422,25 @@ static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsu
ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data); ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
} }
ast_free(body_text);
} }
static void send_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason) static void send_mwi_notify(struct mwi_subscription *sub)
{ {
const pj_str_t *reason_str_ptr = NULL;
struct ast_sip_message_accumulator counter = { struct ast_sip_message_accumulator counter = {
.old_msgs = 0, .old_msgs = 0,
.new_msgs = 0, .new_msgs = 0,
}; };
RAII_VAR(struct ast_str *, body_text, ast_str_create(64), ast_free_ptr);
pjsip_tx_data *tdata;
pj_str_t reason_str;
struct ast_sip_body body;
body.type = sub->is_solicited ?
ast_sip_subscription_get_body_type(sub->sip_sub) :
MWI_TYPE;
body.subtype = sub->is_solicited ?
ast_sip_subscription_get_body_subtype(sub->sip_sub) :
MWI_SUBTYPE;
ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter); ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
if (reason) {
pj_cstr(&reason_str, reason);
reason_str_ptr = &reason_str;
}
if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &counter, &body_text)) {
ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
return;
}
body.body_text = ast_str_buffer(body_text);
ast_debug(5, "Sending %s MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
sub->is_solicited ? "solicited" : "unsolicited", sub->id, counter.new_msgs,
counter.old_msgs);
if (sub->is_solicited) { if (sub->is_solicited) {
if (pjsip_evsub_notify(ast_sip_subscription_get_evsub(sub->sip_sub), ast_sip_subscription_notify(sub->sip_sub, &counter, 0);
state, NULL, reason_str_ptr, &tdata) != PJ_SUCCESS) {
ast_log(LOG_WARNING, "Unable to create MWI NOTIFY request to %s.\n", sub->id);
return;
}
if (ast_sip_add_body(tdata, &body)) {
ast_log(LOG_WARNING, "Unable to add body to MWI NOTIFY request\n");
return; return;
} }
if (ast_sip_subscription_send_request(sub->sip_sub, tdata) != PJ_SUCCESS) {
ast_log(LOG_WARNING, "Unable to send MWI NOTIFY request to %s\n", sub->id); send_unsolicited_mwi_notify(sub, &counter);
return;
}
} else {
send_unsolicited_mwi_notify(sub, state, reason, &body);
}
} }
static int unsubscribe_stasis(void *obj, void *arg, int flags) static int unsubscribe_stasis(void *obj, void *arg, int flags)
@ -620,10 +585,9 @@ static int mwi_on_aor(void *obj, void *arg, int flags)
} }
static struct mwi_subscription *mwi_create_subscription( static struct mwi_subscription *mwi_create_subscription(
struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
{ {
struct mwi_subscription *sub = mwi_subscription_alloc( struct mwi_subscription *sub = mwi_subscription_alloc(endpoint, 1, sip_sub);
endpoint, AST_SIP_NOTIFIER, 1, rdata);
if (!sub) { if (!sub) {
return NULL; return NULL;
@ -640,29 +604,23 @@ static struct mwi_subscription *mwi_create_subscription(
} }
static struct mwi_subscription *mwi_subscribe_single( static struct mwi_subscription *mwi_subscribe_single(
struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name) struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
{ {
RAII_VAR(struct ast_sip_aor *, aor, RAII_VAR(struct ast_sip_aor *, aor,
ast_sip_location_retrieve_aor(name), ao2_cleanup); ast_sip_location_retrieve_aor(name), ao2_cleanup);
struct mwi_subscription *sub; struct mwi_subscription *sub;
if (!aor) { if (!aor) {
/*! I suppose it's possible for the AOR to disappear on us
* between accepting the subscription and sending the first
* NOTIFY...
*/
ast_log(LOG_WARNING, "Unable to locate aor %s. MWI " ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
"subscription failed.\n", name); "subscription failed.\n", name);
return NULL; return NULL;
} }
if (ast_strlen_zero(aor->mailboxes)) { if (!(sub = mwi_create_subscription(endpoint, sip_sub))) {
ast_log(LOG_WARNING, "AOR %s has no configured mailboxes. "
"MWI subscription failed\n", name);
return NULL;
}
if (mwi_validate_for_aor(aor, endpoint, 0)) {
return NULL;
}
if (!(sub = mwi_create_subscription(endpoint, rdata))) {
return NULL; return NULL;
} }
@ -671,15 +629,11 @@ static struct mwi_subscription *mwi_subscribe_single(
} }
static struct mwi_subscription *mwi_subscribe_all( static struct mwi_subscription *mwi_subscribe_all(
struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
{ {
struct mwi_subscription *sub; struct mwi_subscription *sub;
if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) { sub = mwi_create_subscription(endpoint, sip_sub);
return NULL;
}
sub = mwi_create_subscription(endpoint, rdata);
if (!sub) { if (!sub) {
return NULL; return NULL;
@ -689,106 +643,89 @@ static struct mwi_subscription *mwi_subscribe_all(
return sub; return sub;
} }
static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint, static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
pjsip_rx_data *rdata) const char *resource)
{ {
/* It's not obvious here, but the reference(s) to this subscription, struct ast_sip_aor *aor;
* once this function exits, is held by the stasis subscription(s)
* created in mwi_stasis_subscription_alloc()
*/
RAII_VAR(struct mwi_subscription *, sub, NULL, ao2_cleanup);
pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
pjsip_sip_uri *sip_ruri;
char aor_name[80];
if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) { if (ast_strlen_zero(resource)) {
ast_log(LOG_WARNING, "Attempt to SUBSCRIBE to a non-SIP URI\n"); if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
return NULL; return 500;
} }
sip_ruri = pjsip_uri_get_uri(ruri); return 200;
ast_copy_pj_str(aor_name, &sip_ruri->user, sizeof(aor_name));
/* no aor in uri? subscribe to all on endpoint */
if (!(sub = ast_strlen_zero(aor_name) ? mwi_subscribe_all(endpoint, rdata) :
mwi_subscribe_single(endpoint, rdata, aor_name))) {
return NULL;
} }
ast_sip_subscription_accept(sub->sip_sub, rdata, 200); aor = ast_sip_location_retrieve_aor(resource);
send_mwi_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, NULL);
return sub->sip_sub;
}
static void mwi_resubscribe(struct ast_sip_subscription *sub, if (!aor) {
pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data) ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
{ "subscription failed.\n", resource);
struct mwi_subscription *mwi_sub; return 404;
pjsip_evsub_state state; }
pjsip_evsub *evsub;
RAII_VAR(struct ast_datastore *, mwi_datastore,
ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
if (!mwi_datastore) { if (ast_strlen_zero(aor->mailboxes)) {
return; ast_log(LOG_WARNING, "AOR %s has no configured mailboxes. "
"MWI subscription failed\n", resource);
return 404;
} }
mwi_sub = mwi_datastore->data; if (mwi_validate_for_aor(aor, endpoint, 0)) {
evsub = ast_sip_subscription_get_evsub(sub); return 500;
state = pjsip_evsub_get_state(evsub); }
send_mwi_notify(mwi_sub, state, NULL); return 200;
} }
static void mwi_subscription_timeout(struct ast_sip_subscription *sub) static int mwi_initial_subscription(struct ast_sip_subscription *sip_sub)
{ {
struct mwi_subscription *mwi_sub; const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
RAII_VAR(struct ast_datastore *, mwi_datastore, struct mwi_subscription *sub;
ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup); struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
if (!mwi_datastore) { /* no aor in uri? subscribe to all on endpoint */
return; if (ast_strlen_zero(resource)) {
sub = mwi_subscribe_all(endpoint, sip_sub);
} else {
sub = mwi_subscribe_single(endpoint, sip_sub, resource);
} }
if (!sub) {
ao2_cleanup(endpoint);
return -1;
}
mwi_sub = mwi_datastore->data; send_mwi_notify(sub);
ast_log(LOG_NOTICE, "MWI subscription for %s has timed out.\n", mwi_sub->id);
send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, "timeout"); ao2_cleanup(sub);
ao2_cleanup(endpoint);
return 0;
} }
static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata) static int mwi_notify_required(struct ast_sip_subscription *sip_sub,
enum ast_sip_subscription_notify_reason reason)
{ {
struct mwi_subscription *mwi_sub; struct mwi_subscription *mwi_sub;
RAII_VAR(struct ast_datastore *, mwi_datastore, struct ast_datastore *mwi_datastore;
ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
switch (reason) {
case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED:
return mwi_initial_subscription(sip_sub);
case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED:
case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED:
case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER:
mwi_datastore = ast_sip_subscription_get_datastore(sip_sub, "MWI datastore");
if (!mwi_datastore) { if (!mwi_datastore) {
return; return -1;
} }
mwi_sub = mwi_datastore->data; mwi_sub = mwi_datastore->data;
ast_log(LOG_NOTICE, "MWI subscription for %s has been terminated\n", mwi_sub->id); send_mwi_notify(mwi_sub);
ao2_cleanup(mwi_datastore);
send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, NULL); break;
} }
static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata)
{
/* We don't really care about NOTIFY responses for the moment */
}
static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
struct ast_sip_subscription_response_data *response_data)
{
ast_log(LOG_WARNING, "Received an MWI NOTIFY request? This should not happen\n");
}
static int mwi_refresh_subscription(struct ast_sip_subscription *sub)
{
ast_log(LOG_WARNING, "Being told to refresh an MWI subscription? This should not happen\n");
return 0; return 0;
} }
@ -834,7 +771,7 @@ static int serialized_notify(void *userdata)
{ {
struct mwi_subscription *mwi_sub = userdata; struct mwi_subscription *mwi_sub = userdata;
send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_ACTIVE, NULL); send_mwi_notify(mwi_sub);
ao2_ref(mwi_sub, -1); ao2_ref(mwi_sub, -1);
return 0; return 0;
} }
@ -885,7 +822,7 @@ static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags
} }
if (endpoint->subscription.mwi.aggregate) { if (endpoint->subscription.mwi.aggregate) {
aggregate_sub = mwi_subscription_alloc(endpoint, AST_SIP_NOTIFIER, 0, NULL); aggregate_sub = mwi_subscription_alloc(endpoint, 0, NULL);
if (!aggregate_sub) { if (!aggregate_sub) {
return 0; return 0;
} }
@ -894,7 +831,7 @@ static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags
mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes); mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
while ((mailbox = strsep(&mailboxes, ","))) { while ((mailbox = strsep(&mailboxes, ","))) {
struct mwi_subscription *sub = aggregate_sub ?: struct mwi_subscription *sub = aggregate_sub ?:
mwi_subscription_alloc(endpoint, AST_SIP_SUBSCRIBER, 0, NULL); mwi_subscription_alloc(endpoint, 0, NULL);
RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub, RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup); mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
if (mwi_stasis_sub) { if (mwi_stasis_sub) {

@ -79,7 +79,7 @@ static int pidf_generate_body_content(void *body, void *data)
return 0; return 0;
} }
#define MAX_STRING_GROWTHS 3 #define MAX_STRING_GROWTHS 5
#define XML_PROLOG 39 #define XML_PROLOG 39
static void pidf_to_string(void *body, struct ast_str **str) static void pidf_to_string(void *body, struct ast_str **str)

File diff suppressed because it is too large Load Diff

@ -1,7 +1,7 @@
{ {
global: global:
LINKER_SYMBOL_PREFIXast_sip_create_subscription; LINKER_SYMBOL_PREFIXast_sip_create_subscription;
LINKER_SYMBOL_PREFIXast_sip_subsription_get_endpoint; LINKER_SYMBOL_PREFIXast_sip_subscription_get_endpoint;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_serializer; LINKER_SYMBOL_PREFIXast_sip_subscription_get_serializer;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_evsub; LINKER_SYMBOL_PREFIXast_sip_subscription_get_evsub;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_dlg; LINKER_SYMBOL_PREFIXast_sip_subscription_get_dlg;
@ -30,6 +30,11 @@
LINKER_SYMBOL_PREFIXast_sip_pubsub_generate_body_content; LINKER_SYMBOL_PREFIXast_sip_pubsub_generate_body_content;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_body_type; LINKER_SYMBOL_PREFIXast_sip_subscription_get_body_type;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_body_subtype; LINKER_SYMBOL_PREFIXast_sip_subscription_get_body_subtype;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_resource_name;
LINKER_SYMBOL_PREFIXast_sip_subscription_notify;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_local_uri;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_remote_uri;
LINKER_SYMBOL_PREFIXast_sip_subscription_get_header;
local: local:
*; *;
}; };

@ -96,7 +96,7 @@ static int xpidf_generate_body_content(void *body, void *data)
return 0; return 0;
} }
#define MAX_STRING_GROWTHS 3 #define MAX_STRING_GROWTHS 5
#define XML_PROLOG 39 #define XML_PROLOG 39
static void xpidf_to_string(void *body, struct ast_str **str) static void xpidf_to_string(void *body, struct ast_str **str)

Loading…
Cancel
Save