From 211ad859de157c676bde6529a829e66db9fe4ff6 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Mon, 4 Apr 2016 19:52:41 +0200 Subject: [PATCH] sip/call/mncc: Move source/dest into the call structure In preparation of a better show calls VTY command it is of interest to know which number has been dialed by whom. For that store the source/dest in there. MNCC: Change the talloc root context to the call and don't try to free the strings after calling the routing code SIP: Use talloc_strdup to duplicate them. Call: Add null check because the talloc_strdup of the SIP layer could have failed. --- src/app.c | 24 +++++++++++++++++------- src/call.h | 3 +++ src/mncc.c | 19 ++++++++----------- src/mncc.h | 3 +-- src/sip.c | 9 +++++---- src/sip.h | 2 +- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/app.c b/src/app.c index 47ee150..4a88b36 100644 --- a/src/app.c +++ b/src/app.c @@ -59,25 +59,35 @@ void app_setup(struct app_config *cfg) cfg->mncc.conn.on_disconnect = app_mncc_disconnected; } -static void route_to_sip(struct call *call, const char *source, const char *dest) +static void route_to_sip(struct call *call) { - if (sip_create_remote_leg(&g_app.sip.agent, call, source, dest) != 0) + if (sip_create_remote_leg(&g_app.sip.agent, call) != 0) call->initial->release_call(call->initial); } -static void route_to_mncc(struct call *call, const char *source, - const char *dest) +static void route_to_mncc(struct call *call) { - if (mncc_create_remote_leg(&g_app.mncc.conn, call, source, dest) != 0) + if (mncc_create_remote_leg(&g_app.mncc.conn, call) != 0) call->initial->release_call(call->initial); } void app_route_call(struct call *call, const char *source, const char *dest) { + + if (!source || !dest) { + LOGP(DAPP, LOGL_ERROR, "call(%u) missing source(%p)/dest(%p)\n", + call->id, source, dest); + call->initial->release_call(call->initial); + return; + } + + call->source = source; + call->dest = dest; + if (call->initial->type == CALL_TYPE_MNCC) - route_to_sip(call, source, dest); + route_to_sip(call); else - route_to_mncc(call, source, dest); + route_to_mncc(call); } const char *app_media_name(int ptmsg) diff --git a/src/call.h b/src/call.h index e844485..7cb4932 100644 --- a/src/call.h +++ b/src/call.h @@ -27,6 +27,9 @@ struct call { unsigned int id; struct call_leg *initial; struct call_leg *remote; + + const char *source; + const char *dest; }; enum { diff --git a/src/mncc.c b/src/mncc.c index 4778424..a1926e4 100644 --- a/src/mncc.c +++ b/src/mncc.c @@ -254,18 +254,16 @@ static void continue_mo_call(struct mncc_call_leg *leg) leg->state = MNCC_CC_PROCEEDING; if (leg->called.type == GSM340_TYPE_INTERNATIONAL) - dest = talloc_asprintf(tall_mncc_ctx, "+%.32s", leg->called.number); + dest = talloc_asprintf(leg, "+%.32s", leg->called.number); else - dest = talloc_asprintf(tall_mncc_ctx, "%.32s", leg->called.number); + dest = talloc_asprintf(leg, "%.32s", leg->called.number); if (leg->conn->app->use_imsi_as_id) - source = talloc_asprintf(tall_mncc_ctx, "%.16s", leg->imsi); + source = talloc_asprintf(leg, "%.16s", leg->imsi); else - source = talloc_asprintf(tall_mncc_ctx, "%.32s", leg->calling.number); + source = talloc_asprintf(leg, "%.32s", leg->calling.number); app_route_call(leg->base.call, source, dest); - talloc_free(source); - talloc_free(dest); } static void continue_mt_call(struct mncc_call_leg *leg) @@ -641,8 +639,7 @@ static void check_hello(struct mncc_connection *conn, char *buf, int rc) conn->state = MNCC_READY; } -int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call, - const char *calling, const char *called) +int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call) { struct mncc_call_leg *leg; struct gsm_mncc mncc = { 0, }; @@ -673,15 +670,15 @@ int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call, mncc.fields |= MNCC_F_CALLING; mncc.calling.plan = 1; mncc.calling.type = 0x0; - memcpy(&mncc.calling.number, calling, sizeof(mncc.calling.number)); + memcpy(&mncc.calling.number, call->source, sizeof(mncc.calling.number)); if (conn->app->use_imsi_as_id) { - snprintf(mncc.imsi, 15, "%s", called); + snprintf(mncc.imsi, 15, "%s", call->dest); } else { mncc.fields |= MNCC_F_CALLED; mncc.called.plan = 1; mncc.called.type = 0x0; - memcpy(&mncc.called.number, called, sizeof(mncc.called.number)); + memcpy(&mncc.called.number, call->dest, sizeof(mncc.called.number)); } /* diff --git a/src/mncc.h b/src/mncc.h index 7078283..b1ad5ca 100644 --- a/src/mncc.h +++ b/src/mncc.h @@ -30,5 +30,4 @@ struct mncc_connection { void mncc_connection_init(struct mncc_connection *conn, struct app_config *cfg); void mncc_connection_start(struct mncc_connection *conn); -int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call, - const char *calling, const char *called); +int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call); diff --git a/src/sip.c b/src/sip.c index 36e4967..139c620 100644 --- a/src/sip.c +++ b/src/sip.c @@ -136,7 +136,9 @@ static void new_call(struct sip_agent *agent, nua_handle_t *nh, nua_handle_bind(nh, leg); leg->sdp_payload = talloc_strdup(leg, sip->sip_payload->pl_data); - app_route_call(call, from, to); + app_route_call(call, + talloc_strdup(leg, from), + talloc_strdup(leg, to)); } void nua_callback(nua_event_t event, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[]) @@ -316,8 +318,7 @@ static int send_invite(struct sip_agent *agent, struct sip_call_leg *leg, return 0; } -int sip_create_remote_leg(struct sip_agent *agent, struct call *call, - const char *source, const char *dest) +int sip_create_remote_leg(struct sip_agent *agent, struct call *call) { struct sip_call_leg *leg; @@ -341,7 +342,7 @@ int sip_create_remote_leg(struct sip_agent *agent, struct call *call, return -2; } - return send_invite(agent, leg, source, dest); + return send_invite(agent, leg, call->source, call->dest); } char *make_sip_uri(struct sip_agent *agent) diff --git a/src/sip.h b/src/sip.h index 4ba0a0e..d25c0b1 100644 --- a/src/sip.h +++ b/src/sip.h @@ -21,4 +21,4 @@ struct sip_agent { void sip_agent_init(struct sip_agent *agent, struct app_config *app); int sip_agent_start(struct sip_agent *agent); -int sip_create_remote_leg(struct sip_agent *agent, struct call *call, const char *local, const char *remote); +int sip_create_remote_leg(struct sip_agent *agent, struct call *call);