[asterisk-dev] PJSIP in dialog OPTIONS method handling

Matthew Jordan mjordan at digium.com
Tue Apr 15 09:36:52 CDT 2014

On Thu, Apr 10, 2014 at 12:56 AM, Yaron Nachum <nachum.yaron at gmail.com> wrote:
> Hi Mathew,
> I wasn't aware that some of you are listening to both lists. Sorry for that
> :-(

No worries. Everyone gets one cross-post allowance :-)

> BTW - I got spam messages from you dev list.

Yeah, sorry about that. I think we finally got the offending spammer,
as I've stopped receiving ... things... from them.

(This is why we can't have nice things on the internet)

> Regrading the issue - I will open a ticket shortly.
> While waiting for an answer I was looking at your PJSIP module supplement
> documentation, and I thought I might try to do it there. After playing with
> it, and looking at other modules to see how to send a response,  I managed
> to do it and it seems to work fine. Could anyone please look at it and
> verify that I am not doing anything wrong? Below you can see the handler
> module (I registered it against incoming OPTIONS requests).
> static int options_incoming_request(struct ast_sip_session *session,
> pjsip_rx_data *rdata)
> {
>         pjsip_tx_data *tdata;
>         pj_status_t status;
>         pjsip_response_addr res_addr;
>         status = ast_sip_create_response(rdata, 200, NULL, &tdata);
>         if (status != PJ_SUCCESS) {
>                 ast_log(LOG_ERROR, "Unable to create response (%d)\n",
> status);
>                 return status;
>         }
>         /* Get where to send request. */
>         status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
>         if (status != PJ_SUCCESS) {
>                  ast_log(LOG_ERROR, "Unable to get response address (%d)\n",
> status);
>                  return status;
>         }
>         status = ast_sip_send_response(&res_addr, tdata,
> ast_pjsip_rdata_get_endpoint(rdata));
>         return PJ_TRUE;
> }

That's a good start, but it's not quite right.

ast_sip_create_response is for responding to out of dialog requests:

 * \brief General purpose method for creating a SIP response
 * Its typical use would be to create responses for out of dialog
 * requests.
 * \param rdata The rdata from the incoming request.
 * \param st_code The response code to transmit.
 * \param contact The contact with which this request is associated.
 * \param[out] tdata The newly-created response
 * The provided contact is attached to tdata with its reference bumped, but will
 * not survive for the entire lifetime of tdata since the contact is cleaned up
 * when all supplements have completed execution.
 * \retval 0 Success
 * \retval -1 Failure
int ast_sip_create_response(const pjsip_rx_data *rdata, int st_code,
    struct ast_sip_contact *contact, pjsip_tx_data **p_tdata);

If I understand the problem correctly, the OPTIONS request is arriving
in the context of an active dialog, which is why the normal OPTIONS
request handling in res_pjsip/pjsip_options apparently isn't handling

If you're implementing this as a session supplement, you should use
the supplement's mechanism of sending the response, which is defined
in res_sip_session.h:

 * \brief Send a SIP response
 * This will send the SIP response specified in tdata and
 * call into any registered supplements' outgoing_response callback.
 * \param session The session on which to send the response.
 * \param tdata The response to send
void ast_sip_session_send_response(struct ast_sip_session *session,
pjsip_tx_data *tdata);

Feel free to open an issue and attach the patch after signing a
license contributor agreement. Once you feel like you have something
working, please post it up to review board. Some guidelines on
contributing patches are available on the Asterisk wiki here:


It'd be great to get this fixed in the next point release of Asterisk 12.



Matthew Jordan
Digium, Inc. | Engineering Manager
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at: http://digium.com & http://asterisk.org

More information about the asterisk-dev mailing list