Skip to content

Commit 28636e1

Browse files
authored
Do ref for nua handles when intercepting query results. Allow override. (#291)
* Do ref for nua handles when intercepting query results. * nua_handle can override nua handle defaults
1 parent 1199d22 commit 28636e1

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

libsofia-sip-ua/nta/nta.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8196,6 +8196,7 @@ nta_outgoing_t *outgoing_create(nta_agent_t *agent,
81968196
SU_DEBUG_3(("nta outgoing create: %s\n",
81978197
invalid < 0 ? "invalid URI" :
81988198
!orq->orq_branch ? "no branch" : "invalid message"));
8199+
orq->orq_intercept_query_results = 0; /* Let the caller do nua_handle_unref for it */
81998200
outgoing_free(orq);
82008201
return NULL;
82018202
}
@@ -8221,8 +8222,10 @@ nta_outgoing_t *outgoing_create(nta_agent_t *agent,
82218222

82228223
if (orq->orq_status < 300)
82238224
retval = (void *)-1; /* NONE */
8224-
else
8225-
retval = NULL, orq->orq_request = NULL;
8225+
else {
8226+
retval = NULL, orq->orq_request = NULL;
8227+
orq->orq_intercept_query_results = 0; /* Let the caller do nua_handle_unref (su_home_unref) for it */
8228+
}
82268229

82278230
outgoing_free(orq);
82288231

@@ -8920,6 +8923,11 @@ void outgoing_reclaim(nta_outgoing_t *orq)
89208923
if (orq->orq_resolver)
89218924
outgoing_destroy_resolver(orq);
89228925
#endif
8926+
if (orq->orq_intercept_query_results) {
8927+
su_home_unref((su_home_t *)orq->orq_intercept_query_results);
8928+
orq->orq_intercept_query_results = 0;
8929+
}
8930+
89238931
su_free(orq->orq_agent->sa_home, orq);
89248932
}
89258933

@@ -11048,7 +11056,7 @@ void outgoing_answer_a(sres_context_t *orq, sres_query_t *q,
1104811056
outgoing_query_results(orq, sq, results, found);
1104911057
}
1105011058

11051-
int nua_client_intercept_query_results(const void *nua_handle, void *cr, nta_outgoing_t *orq, sip_t const *sip);
11059+
int nua_client_intercept_query_results(const void *handle, void *cr, nta_outgoing_t *orq, sip_t const *sip);
1105211060

1105311061
/** Store A/AAAA query results */
1105411062
static void

libsofia-sip-ua/nua/nua_client.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,12 @@ int nua_base_client_request(nua_client_request_t *cr, msg_t *msg, sip_t *sip,
942942
tagi_t const *tags)
943943
{
944944
nua_handle_t *nh = cr->cr_owner;
945+
nua_handle_t *intercept_nh = NULL;
945946
int proxy_is_set = NH_PISSET(nh, proxy);
946947
url_string_t * proxy = NH_PGET(nh, proxy);
947948
int call_tls_orq_connect_timeout_is_set = NH_PISSET(nh, call_tls_orq_connect_timeout);
948949
uint32_t call_tls_orq_connect_timeout = NH_PGET(nh, call_tls_orq_connect_timeout);
949-
int intercept_query_results_is_set = NUA_PISSET(nh->nh_nua, nh, intercept_query_results);
950+
int intercept_query_results_is_set = NH_PGET(nh, intercept_query_results);
950951

951952
if (nh->nh_auth) {
952953
if (cr->cr_challenged ||
@@ -960,13 +961,17 @@ int nua_base_client_request(nua_client_request_t *cr, msg_t *msg, sip_t *sip,
960961

961962
assert(cr->cr_orq == NULL);
962963

964+
if (intercept_query_results_is_set) {
965+
intercept_nh = nua_handle_ref(nh);
966+
}
967+
963968
cr->cr_orq = nta_outgoing_mcreate(nh->nh_nua->nua_nta,
964969
nua_client_orq_response,
965970
nua_client_request_ref(cr),
966971
NULL,
967972
msg,
968973
TAG_IF(intercept_query_results_is_set,
969-
NTATAG_INTERCEPT_QUERY_RESULTS(nh)),
974+
NTATAG_INTERCEPT_QUERY_RESULTS(intercept_nh)),
970975
TAG_IF(proxy_is_set,
971976
NTATAG_DEFAULT_PROXY(proxy)),
972977
TAG_IF(call_tls_orq_connect_timeout_is_set,
@@ -975,6 +980,9 @@ int nua_base_client_request(nua_client_request_t *cr, msg_t *msg, sip_t *sip,
975980

976981
if (cr->cr_orq == NULL) {
977982
nua_client_request_unref(cr);
983+
if (intercept_nh) {
984+
nua_handle_unref(intercept_nh);
985+
}
978986
return -1;
979987
}
980988

@@ -989,7 +997,7 @@ static inline void nua_client_intercept_response(nua_client_request_t *cr,
989997
nua_handle_t *nh = cr->cr_owner;
990998
nua_t *nua = nh ? nh->nh_nua : NULL;
991999
nua_dialog_usage_t *du = cr->cr_usage;
992-
unsigned intercept_query_results = nh ? NUA_PISSET(nh->nh_nua, nh, intercept_query_results) : 0;
1000+
unsigned intercept_query_results = nh ? NH_PGET(nh, intercept_query_results) : 0;
9931001

9941002
if (intercept_query_results && sip && status != 408 && nua) {
9951003
/* At this point we have a successful response */
@@ -1013,7 +1021,7 @@ static inline void nua_client_intercept_response(nua_client_request_t *cr,
10131021
}
10141022
}
10151023

1016-
int nua_client_intercept_query_results(const void *nua_handle, void *_cr,
1024+
int nua_client_intercept_query_results(const void *handle, void *_cr,
10171025
nta_outgoing_t *orq,
10181026
sip_t const *sip)
10191027
{
@@ -1023,28 +1031,43 @@ int nua_client_intercept_query_results(const void *nua_handle, void *_cr,
10231031

10241032
if (nta_outgoing_query_results(orq, &results, &found) != NULL) {
10251033
/* See NTATAG_INTERCEPT_QUERY_RESULTS and NUTAG_INTERCEPT_QUERY_RESULTS */
1026-
nua_handle_t *nh = nua_handle ? (nua_handle_t *)nua_handle : cr->cr_owner;
1034+
nua_handle_t *nh = handle ? (nua_handle_t *)handle : (cr ? cr->cr_owner : NULL);
10271035
nua_dialog_usage_t *du = cr ? cr->cr_usage : NULL;
10281036
nua_dialog_state_t *ds = du ? du->du_dialog : NULL;
10291037

10301038
SU_DEBUG_0(("Query results have been intercepted. Trying to override...\n" VA_NONE));
10311039

10321040
if (!ds && nh) {
1033-
ds = nh->nh_ds;
1041+
if (NH_IS_VALID(nh)) {
1042+
ds = nh->nh_ds;
1043+
} else {
1044+
SU_DEBUG_0(("Warning! nh is not valid! Can not intercept! No ds!\n" VA_NONE));
1045+
}
10341046
}
10351047

10361048
if (ds && ds->ds_intercepted_ip) {
10371049
if (found) {
1038-
msg_t *response = nta_outgoing_getresponse(orq);
1039-
su_home_t *home = response ? msg_home(response) : msg_home(nh);
1050+
msg_t *request = nta_outgoing_getrequest(orq);
1051+
su_home_t *home = request ? msg_home(request) : NULL;
1052+
1053+
if (!home) {
1054+
if (NH_IS_VALID(nh)) {
1055+
home = msg_home(nh);
1056+
} else {
1057+
SU_DEBUG_0(("Warning! nh is not valid! Can not intercept! No home!\n" VA_NONE));
1058+
}
1059+
}
10401060

1041-
SU_DEBUG_0(("Intercepted IP address: [%s]->[%s]\n", results[0], ds->ds_intercepted_ip));
1042-
results[0] = su_strdup(home, ds->ds_intercepted_ip);
1043-
msg_destroy(response);
1061+
if (home) {
1062+
SU_DEBUG_0(("Intercepted IP address: [%s]->[%s]\n", results[0], ds->ds_intercepted_ip));
1063+
results[0] = su_strdup(home, ds->ds_intercepted_ip);
10441064

1045-
if (found > 1) {
1046-
results[1] = NULL;
1065+
if (found > 1) {
1066+
results[1] = NULL;
1067+
}
10471068
}
1069+
1070+
msg_destroy(request);
10481071
}
10491072
}
10501073

libsofia-sip-ua/nua/nua_session.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ static
12291229
int nua_invite_client_ack(nua_client_request_t *cr, tagi_t const *tags)
12301230
{
12311231
nua_handle_t *nh = cr->cr_owner;
1232+
nua_handle_t *intercept_nh = NULL;
12321233
nua_dialog_state_t *ds = nh->nh_ds;
12331234
nua_session_usage_t *ss = nua_dialog_usage_private(cr->cr_usage);
12341235

@@ -1360,13 +1361,16 @@ int nua_invite_client_ack(nua_client_request_t *cr, tagi_t const *tags)
13601361

13611362
proxy_is_set = NH_PISSET(nh, proxy);
13621363
proxy = NH_PGET(nh, proxy);
1363-
intercept_query_results_is_set = NUA_PISSET(nh->nh_nua, nh, intercept_query_results);
1364+
intercept_query_results_is_set = NH_PGET(nh, intercept_query_results);
1365+
if (intercept_query_results_is_set) {
1366+
intercept_nh = nua_handle_ref(nh);
1367+
}
13641368

13651369
if ((ack = nta_outgoing_mcreate(nh->nh_nua->nua_nta, NULL, NULL, NULL,
13661370
msg,
13671371
NTATAG_ACK_BRANCH(invite_branch),
13681372
TAG_IF(intercept_query_results_is_set,
1369-
NTATAG_INTERCEPT_QUERY_RESULTS(nh)),
1373+
NTATAG_INTERCEPT_QUERY_RESULTS(intercept_nh)),
13701374
TAG_IF(proxy_is_set,
13711375
NTATAG_DEFAULT_PROXY(proxy)),
13721376
SIPTAG_END(),
@@ -1384,6 +1388,11 @@ int nua_invite_client_ack(nua_client_request_t *cr, tagi_t const *tags)
13841388
reason = "SIP;cause=500;text=\"Internal Error\"";
13851389
}
13861390

1391+
if (!ack && intercept_nh) {
1392+
/* Unref in case of outgoing create error */
1393+
nua_handle_unref(intercept_nh);
1394+
}
1395+
13871396
if (ss && reason)
13881397
ss->ss_reason = reason;
13891398

0 commit comments

Comments
 (0)