[svn-commits] mmichelson: branch mmichelson/outbound_auth r383395 - in /team/mmichelson/out...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Tue Mar 19 15:59:12 CDT 2013
Author: mmichelson
Date: Tue Mar 19 15:59:09 2013
New Revision: 383395
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383395
Log:
Separate request creation from sending.
This allows for custom headers to be added easily to SIP requests. It
also allows flexibility in creation and sending of requests. A request
could be created via a PJSIP API call and then sent using the Asterisk
one, or vice versa.
Tested and it works. All that's necessary now is some cleanup and it'll
be review time!
Modified:
team/mmichelson/outbound_auth/include/asterisk/res_sip.h
team/mmichelson/outbound_auth/res/res_sip.c
team/mmichelson/outbound_auth/res/res_sip.exports.in
team/mmichelson/outbound_auth/res/res_sip/sip_options.c
Modified: team/mmichelson/outbound_auth/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/include/asterisk/res_sip.h?view=diff&rev=383395&r1=383394&r2=383395
==============================================================================
--- team/mmichelson/outbound_auth/include/asterisk/res_sip.h (original)
+++ team/mmichelson/outbound_auth/include/asterisk/res_sip.h Tue Mar 19 15:59:09 2013
@@ -810,26 +810,43 @@
pjsip_dialog *ast_sip_create_dialog(const struct ast_sip_endpoint *endpoint, const char *aor_name, const char *request_user);
/*!
- * \brief General purpose method for sending a SIP request
- *
- * Its typical use would be to send one-off messages such as an out of dialog
+ * \brief General purpose method for creating a SIP request
+ *
+ * Its typical use would be to create one-off requests such as an out of dialog
* SIP MESSAGE.
*
- * The request can either be sent in- or out-of-dialog. If sent in-dialog, the
- * dlg parameter MUST be present. If sent out-of-dialog the endpoint parameter
+ * The request can either be in- or out-of-dialog. If in-dialog, the
+ * dlg parameter MUST be present. If out-of-dialog the endpoint parameter
* MUST be present. If both are present, then we will assume that the message
* is to be sent in-dialog.
*
* \param method The method of the SIP request to send
- * \param body The message body for the SIP request
- * \dlg Optional. If specified, the dialog on which to send the message.
- * \endpoint Optional. If specified, the request will be sent out-of-dialog to the endpoint.
- * \retval 0 Success
- * \retval -1 Failure
- */
-int ast_sip_send_request(const char *method, const struct ast_sip_body *body,
- struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint);
-
+ * \param dlg Optional. If specified, the dialog on which to request the message.
+ * \param endpoint Optional. If specified, the request will be created out-of-dialog to the endpoint.
+ * \param[out] tdata The newly-created request
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, pjsip_tx_data **tdata);
+
+/*!
+ * \brief General purpose method for sending a SIP request
+ *
+ * This is a companion function for \ref ast_sip_create_request. The request
+ * created there can be passed to this function, though any request may be
+ * passed in.
+ *
+ * This will automatically set up handling outbound authentication challenges if
+ * they arrive.
+ *
+ * \param tdata The request to send
+ * \param dlg Optional. If specified, the dialog on which the request should be sent
+ * \param endpoint Optional. If specified, the request is sent out-of-dialog to the endpoint.
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint);
+
/*!
* \brief Determine if an incoming request requires authentication
*
Modified: team/mmichelson/outbound_auth/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/res/res_sip.c?view=diff&rev=383395&r1=383394&r2=383395
==============================================================================
--- team/mmichelson/outbound_auth/res/res_sip.c (original)
+++ team/mmichelson/outbound_auth/res/res_sip.c Tue Mar 19 15:59:09 2013
@@ -455,54 +455,23 @@
return NULL;
}
-static int send_in_dialog_request(const pjsip_method *method, const struct ast_sip_body *body, struct pjsip_dialog *dlg)
-{
- pj_status_t status;
- pjsip_tx_data *tdata;
-
- status = pjsip_dlg_create_request(dlg, method, -1, &tdata);
- if (status != PJ_SUCCESS) {
+static int create_in_dialog_request(const pjsip_method *method, struct pjsip_dialog *dlg, pjsip_tx_data **tdata)
+{
+ if (pjsip_dlg_create_request(dlg, method, -1, tdata) != PJ_SUCCESS) {
ast_log(LOG_WARNING, "Unable to create in-dialog request.\n");
return -1;
}
- if (body) {
- ast_sip_add_body(tdata, body);
- }
-
- status = pjsip_dlg_send_request(dlg, tdata, -1, NULL);
- if (status != PJ_SUCCESS) {
- ast_log(LOG_WARNING, "Unable to send in-dialog request.\n");
- return -1;
- }
-
- return 0;
-}
-
-static void send_request_cb(void *token, pjsip_event *e)
-{
- RAII_VAR(struct ast_sip_endpoint *, endpoint, token, ao2_cleanup);
- pjsip_transaction *tsx = e->body.tsx_state.tsx;
- pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
- pjsip_tx_data *tdata;
-
- if (tsx->status_code != 401 && tsx->status_code != 407) {
- return;
- }
-
- ast_sip_create_auth_challenge_response(endpoint, challenge, tsx, &tdata);
-
- pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
-}
-
-static int send_out_of_dialog_request(const pjsip_method *method, const struct ast_sip_body *body, struct ast_sip_endpoint *endpoint)
+ return 0;
+}
+
+static int create_out_of_dialog_request(const pjsip_method *method, struct ast_sip_endpoint *endpoint, pjsip_tx_data **tdata)
{
RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
pj_str_t remote_uri;
pj_str_t from;
pj_pool_t *pool;
pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
- pjsip_tx_data *tdata;
contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
if (!contact || ast_strlen_zero(contact->uri)) {
@@ -534,7 +503,7 @@
}
if (pjsip_endpt_create_request(ast_sip_get_pjsip_endpoint(), method, &remote_uri,
- &from, &remote_uri, &from, NULL, -1, NULL, &tdata) != PJ_SUCCESS) {
+ &from, &remote_uri, &from, NULL, -1, NULL, tdata) != PJ_SUCCESS) {
ast_log(LOG_ERROR, "Unable to create outbound %.*s request to endpoint %s\n",
(int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
@@ -545,11 +514,58 @@
* data into the outbound request's pool
*/
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-
+ return 0;
+}
+
+int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, pjsip_tx_data **tdata)
+{
+ const pjsip_method *pmethod = get_pjsip_method(method);
+
+ if (!pmethod) {
+ ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
+ return -1;
+ }
+
+ if (dlg) {
+ return create_in_dialog_request(pmethod, dlg, tdata);
+ } else {
+ return create_out_of_dialog_request(pmethod, endpoint, tdata);
+ }
+}
+
+static int send_in_dialog_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg)
+{
+ if (pjsip_dlg_send_request(dlg, tdata, -1, NULL) != PJ_SUCCESS) {
+ ast_log(LOG_WARNING, "Unable to send in-dialog request.\n");
+ return -1;
+ }
+ return 0;
+}
+
+static void send_request_cb(void *token, pjsip_event *e)
+{
+ RAII_VAR(struct ast_sip_endpoint *, endpoint, token, ao2_cleanup);
+ pjsip_transaction *tsx = e->body.tsx_state.tsx;
+ pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
+ pjsip_tx_data *tdata;
+
+ if (tsx->status_code != 401 && tsx->status_code != 407) {
+ return;
+ }
+
+ ast_sip_create_auth_challenge_response(endpoint, challenge, tsx, &tdata);
+
+ pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, NULL, NULL);
+}
+
+static int send_out_of_dialog_request(pjsip_tx_data *tdata, struct ast_sip_endpoint *endpoint)
+{
ao2_ref(endpoint, +1);
if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, -1, endpoint, send_request_cb) != PJ_SUCCESS) {
ast_log(LOG_ERROR, "Error attempting to send outbound %.*s request to endpoint %s\n",
- (int) pj_strlen(&method->name), pj_strbuf(&method->name), ast_sorcery_object_get_id(endpoint));
+ (int) pj_strlen(&tdata->msg->line.req.method.name),
+ pj_strbuf(&tdata->msg->line.req.method.name),
+ ast_sorcery_object_get_id(endpoint));
ao2_ref(endpoint, -1);
return -1;
}
@@ -557,19 +573,14 @@
return 0;
}
-int ast_sip_send_request(const char *method, const struct ast_sip_body *body, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
-{
- const pjsip_method *pmethod = get_pjsip_method(method);
-
- if (!pmethod) {
- ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method);
- return -1;
- }
+int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
+{
+ ast_assert(tdata->msg->type == PJSIP_REQUEST_MSG);
if (dlg) {
- return send_in_dialog_request(pmethod, body, dlg);
+ return send_in_dialog_request(tdata, dlg);
} else {
- return send_out_of_dialog_request(pmethod, body, endpoint);
+ return send_out_of_dialog_request(tdata, endpoint);
}
}
Modified: team/mmichelson/outbound_auth/res/res_sip.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/res/res_sip.exports.in?view=diff&rev=383395&r1=383394&r2=383395
==============================================================================
--- team/mmichelson/outbound_auth/res/res_sip.exports.in (original)
+++ team/mmichelson/outbound_auth/res/res_sip.exports.in Tue Mar 19 15:59:09 2013
@@ -11,6 +11,7 @@
LINKER_SYMBOL_PREFIXast_sip_create_serializer;
LINKER_SYMBOL_PREFIXast_sip_push_task;
LINKER_SYMBOL_PREFIXast_sip_push_task_synchronous;
+ LINKER_SYMBOL_PREFIXast_sip_create_request;
LINKER_SYMBOL_PREFIXast_sip_send_request;
LINKER_SYMBOL_PREFIXast_sip_requires_authentication;
LINKER_SYMBOL_PREFIXast_sip_authenticate_request;
Modified: team/mmichelson/outbound_auth/res/res_sip/sip_options.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/outbound_auth/res/res_sip/sip_options.c?view=diff&rev=383395&r1=383394&r2=383395
==============================================================================
--- team/mmichelson/outbound_auth/res/res_sip/sip_options.c (original)
+++ team/mmichelson/outbound_auth/res/res_sip/sip_options.c Tue Mar 19 15:59:09 2013
@@ -217,9 +217,11 @@
static int send_qualify_request(void *data)
{
struct ast_sip_endpoint *endpoint = data;
+ pjsip_tx_data *tdata;
/* YAY! Send an OPTIONS request. */
- ast_sip_send_request("OPTIONS", NULL, NULL, endpoint);
+ ast_sip_create_request("OPTIONS", NULL, endpoint, &tdata);
+ ast_sip_send_request(tdata, NULL, endpoint);
ao2_cleanup(endpoint);
return 0;
@@ -300,6 +302,7 @@
{
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
const char *endpoint_name;
+ pjsip_tx_data *tdata;
switch (cmd) {
case CLI_INIT:
@@ -324,7 +327,12 @@
return CLI_FAILURE;
}
- if (ast_sip_send_request("OPTIONS", NULL, NULL, endpoint)) {
+ if (ast_sip_create_request("OPTIONS", NULL, endpoint, &tdata)) {
+ ast_log(LOG_ERROR, "Unable to create OPTIONS request to endpoint %s\n", endpoint_name);
+ return CLI_FAILURE;
+ }
+
+ if (ast_sip_send_request(tdata, NULL, endpoint)) {
ast_log(LOG_ERROR, "Unable to send OPTIONS request to endpoint %s\n", endpoint_name);
return CLI_FAILURE;
}
More information about the svn-commits
mailing list