Skip to content

Commit b257123

Browse files
committed
b2b_entities: new function: b2b_send_indlg_auth_req()
1 parent b5a3d1d commit b257123

File tree

1 file changed

+99
-70
lines changed
  • modules/b2b_entities

1 file changed

+99
-70
lines changed

modules/b2b_entities/dlg.c

Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,75 @@ int b2b_send_indlg_req(b2b_dlg_t* dlg, enum b2b_entity_type et, str* b2b_key,
23232323
return -1;
23242324
}
23252325

2326+
static int build_extra_headers_from_msg(str buf, str *extra_hdr, str *new_hdrs, str *body);
2327+
2328+
#define RETURN_GOTO_DONE -2
2329+
#define RETURN_GOTO_ERROR -1
2330+
#define RETURN_CONTINUE 0
2331+
#define RETURN_GOTO_B2B_ROUTE 1
2332+
2333+
int b2b_send_indlg_auth_req(int statuscode, struct authenticate_body *auth,
2334+
struct sip_msg * msg, struct cell *t,
2335+
b2b_dlg_t *dlg, enum b2b_entity_type etype, str* b2b_key)
2336+
{
2337+
struct uac_credential* crd;
2338+
static struct authenticate_nc_cnonce auth_nc_cnonce;
2339+
struct digest_auth_response response;
2340+
str extra_headers = {NULL, 0};
2341+
str body = {NULL, 0};
2342+
str *new_hdr;
2343+
str msg_body;
2344+
2345+
crd = uac_auth_api._lookup_realm( &auth->realm );
2346+
if(crd)
2347+
{
2348+
if ((auth->flags & QOP_AUTH_INT) && get_body(msg, &msg_body) < 0) {
2349+
LM_ERR("Failed to get message body\n");
2350+
return RETURN_GOTO_DONE;
2351+
}
2352+
memset(&auth_nc_cnonce, 0,
2353+
sizeof(struct authenticate_nc_cnonce));
2354+
if (uac_auth_api._do_uac_auth(&msg_body, &t->method,
2355+
&t->uac[0].uri, crd, auth, &auth_nc_cnonce,
2356+
&response) != 0)
2357+
{
2358+
LM_ERR("failed in do_uac_auth()\n");
2359+
dlg->state = B2B_TERMINATED;
2360+
return RETURN_GOTO_ERROR;
2361+
}
2362+
new_hdr = uac_auth_api._build_authorization_hdr(statuscode,
2363+
&t->uac[0].uri, crd, auth,
2364+
&auth_nc_cnonce, &response);
2365+
if (!new_hdr)
2366+
{
2367+
LM_ERR("failed to build auth hdr\n");
2368+
dlg->state = B2B_TERMINATED;
2369+
return RETURN_GOTO_ERROR;
2370+
}
2371+
LM_DBG("auth is [%.*s]\n", new_hdr->len, new_hdr->s);
2372+
if (build_extra_headers_from_msg(t->uac[0].request.buffer,
2373+
new_hdr, &extra_headers, &body) < 0 ) {
2374+
LM_ERR("failed to build extra msgs after auth\n");
2375+
dlg->state = B2B_TERMINATED;
2376+
return RETURN_GOTO_ERROR;
2377+
}
2378+
LM_DBG("extra is [%.*s]\n",
2379+
extra_headers.len, extra_headers.s);
2380+
pkg_free(new_hdr->s);
2381+
new_hdr->s = NULL; new_hdr->len = 0;
2382+
2383+
b2b_send_indlg_req(dlg, etype, b2b_key, &t->method,
2384+
&extra_headers, 0, &body, 0);
2385+
pkg_free(extra_headers.s);
2386+
2387+
return RETURN_GOTO_B2B_ROUTE;
2388+
}
2389+
else
2390+
dlg->state = B2B_TERMINATED;
2391+
2392+
return RETURN_CONTINUE;
2393+
}
2394+
23262395
int _b2b_send_request(b2b_dlg_t* dlg, b2b_req_data_t* req_data)
23272396
{
23282397
enum b2b_entity_type et = req_data->et;
@@ -2873,7 +2942,6 @@ static int build_extra_headers_from_msg(str buf, str *extra_hdr,
28732942
void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
28742943
{
28752944
struct sip_msg * msg;
2876-
str msg_body;
28772945
str* b2b_key;
28782946
unsigned int hash_index, local_index;
28792947
b2b_notify_t b2b_cback;
@@ -2885,8 +2953,6 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
28852953
dlg_leg_t* leg;
28862954
struct to_body* pto;
28872955
str to_tag, callid, from_tag;
2888-
str extra_headers = {NULL, 0};
2889-
str body = {NULL, 0};
28902956
struct hdr_field* hdr;
28912957
unsigned int method_id = 0;
28922958
struct cseq_body cb;
@@ -2895,11 +2961,7 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
28952961
int dlg_based_search = 0;
28962962
struct to_body to_hdr_parsed, from_hdr_parsed;
28972963
int dlg_state = 0, prev_state = B2B_UNDEFINED;
2898-
struct uac_credential* crd;
28992964
struct authenticate_body *auth = NULL;
2900-
static struct authenticate_nc_cnonce auth_nc_cnonce;
2901-
struct digest_auth_response response;
2902-
str *new_hdr;
29032965
char dummy_fl_buf[7/*SIP/2.0*/ + 1 + 3/*statuscode*/ + 1 + 7/*Timeout*/];
29042966
static str sdp_ct = str_init("Content-Type: application/sdp\r\n");
29052967
int old_route_type;
@@ -2912,6 +2974,7 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
29122974
unsigned int ua_flags = 0;
29132975
int ua_ev_type = -1;
29142976
int ua_auto_ack = 0;
2977+
int ret;
29152978

29162979
to_hdr_parsed.param_lst = from_hdr_parsed.param_lst = NULL;
29172980

@@ -3226,72 +3289,38 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
32263289
DAUTH_AHFM_ANYSUP, &auth);
32273290
break;
32283291
}
3292+
ret = RETURN_CONTINUE;
32293293
if(auth)
3294+
ret = b2b_send_indlg_auth_req(statuscode, auth, msg, t, dlg, etype, b2b_key);
3295+
switch(ret)
32303296
{
3231-
crd = uac_auth_api._lookup_realm( &auth->realm );
3232-
if(crd)
3233-
{
3234-
if ((auth->flags & QOP_AUTH_INT) && get_body(msg, &msg_body) < 0) {
3235-
LM_ERR("Failed to get message body\n");
3236-
goto done;
3237-
}
3238-
memset(&auth_nc_cnonce, 0,
3239-
sizeof(struct authenticate_nc_cnonce));
3240-
if (uac_auth_api._do_uac_auth(&msg_body, &t->method,
3241-
&t->uac[0].uri, crd, auth, &auth_nc_cnonce,
3242-
&response) != 0)
3243-
{
3244-
LM_ERR("failed in do_uac_auth()\n");
3245-
dlg->state = B2B_TERMINATED;
3246-
B2BE_LOCK_RELEASE(htable, hash_index);
3247-
goto error;
3248-
}
3249-
new_hdr = uac_auth_api._build_authorization_hdr(statuscode,
3250-
&t->uac[0].uri, crd, auth,
3251-
&auth_nc_cnonce, &response);
3252-
if (!new_hdr)
3253-
{
3254-
LM_ERR("failed to build auth hdr\n");
3255-
dlg->state = B2B_TERMINATED;
3256-
B2BE_LOCK_RELEASE(htable, hash_index);
3257-
goto error;
3258-
}
3259-
LM_DBG("auth is [%.*s]\n", new_hdr->len, new_hdr->s);
3260-
if (build_extra_headers_from_msg(t->uac[0].request.buffer,
3261-
new_hdr, &extra_headers, &body) < 0 ) {
3262-
LM_ERR("failed to build extra msgs after auth\n");
3263-
dlg->state = B2B_TERMINATED;
3264-
B2BE_LOCK_RELEASE(htable, hash_index);
3265-
goto error;
3266-
}
3267-
LM_DBG("extra is [%.*s]\n",
3268-
extra_headers.len, extra_headers.s);
3269-
pkg_free(new_hdr->s);
3270-
new_hdr->s = NULL; new_hdr->len = 0;
3271-
3272-
b2b_send_indlg_req(dlg, etype, b2b_key, &t->method,
3273-
&extra_headers, 0, &body, 0);
3274-
pkg_free(extra_headers.s);
3275-
3276-
dlg->state = B2B_NEW_AUTH;
3277-
B2BE_LOCK_RELEASE(htable, hash_index);
3278-
3279-
/* run the b2b route */
3280-
if(ref_script_route_is_valid(reply_route_ref)) {
3281-
msg->flags = t->uac[0].br_flags;
3282-
swap_route_type(old_route_type, ONREPLY_ROUTE);
3283-
run_top_route(sroutes->request[reply_route_ref->idx], msg);
3284-
set_route_type(old_route_type);
3285-
b2b_apply_lumps(msg);
3286-
}
3287-
goto b2b_route;
3288-
}
3289-
else
3290-
dlg->state = B2B_TERMINATED;
3291-
}
3292-
else
3293-
{
3297+
case RETURN_GOTO_DONE:
3298+
goto done;
3299+
break;
3300+
case RETURN_GOTO_ERROR:
3301+
B2BE_LOCK_RELEASE(htable, hash_index);
3302+
goto error;
3303+
break;
3304+
case RETURN_CONTINUE:
32943305
dlg->state = B2B_TERMINATED;
3306+
break;
3307+
case RETURN_GOTO_B2B_ROUTE:
3308+
dlg->state = B2B_NEW_AUTH;
3309+
B2BE_LOCK_RELEASE(htable, hash_index);
3310+
3311+
/* run the b2b route */
3312+
if(ref_script_route_is_valid(reply_route_ref)) {
3313+
msg->flags = t->uac[0].br_flags;
3314+
swap_route_type(old_route_type, ONREPLY_ROUTE);
3315+
run_top_route(sroutes->request[reply_route_ref->idx], msg);
3316+
set_route_type(old_route_type);
3317+
b2b_apply_lumps(msg);
3318+
}
3319+
goto b2b_route;
3320+
break;
3321+
default:
3322+
LM_ERR("Unexpected return code [%d] from b2b_send_indlg_auth_req()\n", ret);
3323+
goto error;
32953324
}
32963325
}
32973326
}

0 commit comments

Comments
 (0)