From dad67b7449b897918c9356754effb8060422ba1d Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Wed, 4 Dec 2024 14:48:17 +0100 Subject: [PATCH] ua,call: prevent double ua_accept API function ua_accept() can be called by multiple modules which handle the shortly added UA_EVENT_SIPSESS_CONN. This commit prevents that multiple call/sipsess/sip_dialog objects are created for a single SIP INVITE message. --- include/baresip.h | 2 ++ src/call.c | 6 ++++++ src/ua.c | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/include/baresip.h b/include/baresip.h index 4d88c804d..003c2fa0b 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -236,6 +236,7 @@ bool call_has_video(const struct call *call); bool call_early_video_available(const struct call *call); bool call_refresh_allowed(const struct call *call); bool call_ack_pending(const struct call *call); +bool call_sess_cmp(const struct call *call, const struct sip_msg *msg); int call_transfer(struct call *call, const char *uri); int call_replace_transfer(struct call *target_call, struct call *source_call); int call_status(struct re_printf *pf, const struct call *call); @@ -963,6 +964,7 @@ int ua_call_alloc(struct call **callp, struct ua *ua, struct call *xcall, const char *local_uri, bool use_rtp); struct call *ua_find_call_state(const struct ua *ua, enum call_state st); +struct call *ua_find_call_msg(struct ua *ua, const struct sip_msg *msg); int ua_raise(struct ua *ua); int ua_set_autoanswer_value(struct ua *ua, const char *value); void ua_add_extension(struct ua *ua, const char *extension); diff --git a/src/call.c b/src/call.c index 0ddf2574b..f35e9d2a3 100644 --- a/src/call.c +++ b/src/call.c @@ -2294,6 +2294,12 @@ int call_accept(struct call *call, struct sipsess_sock *sess_sock, } +bool call_sess_cmp(const struct call *call, const struct sip_msg *msg) +{ + return sipsess_has_msg(call->sess, msg); +} + + static void delayed_answer_handler(void *arg) { struct call *call = arg; diff --git a/src/ua.c b/src/ua.c index f94f311b7..216457bf4 100644 --- a/src/ua.c +++ b/src/ua.c @@ -504,6 +504,22 @@ struct call *ua_find_active_call(struct ua *ua) } +struct call *ua_find_call_msg(struct ua *ua, const struct sip_msg *msg) +{ + if (!ua || !msg) + return NULL; + + struct le *le = NULL; + for (le = list_tail(&ua->calls); le; le = le->prev) { + struct call *call = le->data; + if (call_sess_cmp(call, msg)) + break; + } + + return le ? le->data : NULL; +} + + static void call_event_handler(struct call *call, enum call_event ev, const char *str, void *arg) { @@ -786,6 +802,11 @@ int ua_accept(struct ua *ua, const struct sip_msg *msg) if (!ua || !msg) return EINVAL; + if (ua_find_call_msg(ua, msg)) { + warning("ua: call was already accepted\n"); + return EINVAL; + } + err = pl_strdup(&to_uri, &msg->to.auri); if (err) goto error;