Skip to content

Conversation

KillingSpark
Copy link
Contributor

@KillingSpark KillingSpark commented May 16, 2025

I want to display the provided reason in Cancel messages to the user of baresip, but as far as I could tell the Cancel messages are not communicated outside of libre.
Related to this feature request: baresip/baresip#3363

As per the feedback in #1309 I implemented passing the cancel message in a way that shouldn't break the API. I didn't want to force push over the other PR, so I made a new one.

The one thing I'm not happy with is that I needed to duplicate the forward definition of the function to get the message from the sip_trans. If you have any suggestions to solve this more elegantly, I'd be happy to implement them.

SIP trace for reference what I want to achieve. the important line here is this: Reason: SIP;cause=200;text="Call completed elsewhere"

INVITE sip:REDACT@REDACT;transport=tls SIP/2.0
Call-ID: a01984cd356f30a3c20b3c29b5cacdd9@REDACT
CSeq: 17429 INVITE
From: "+REDACT" <sip:+REDACT@REDACT>;tag=XVPLSfvv5yY
To: "REDACT" <sip:+REDACT@REDACT>
Via: SIP/2.0/TLS REDACT;branch=z9hG4bKtAnn4iOZQS2OYCtGatPSGw
Max-Forwards: 68
Contact: <sip:REDACT@REDACT;transport=tls;ob>
Allow: INVITE,ACK,CANCEL,BYE,REFER,REGISTER,SUBSCRIBE,PUBLISH
Supported: replaces
Content-Type: application/sdp
User-Agent: REDACT
Content-Length: 786

v=0
o=- 1745825554478 1745825554480 IN IP4 REDACT
s=CROWN
c=IN IP4 REDACT
t=0 0
m=audio REDACT RTP/AVP 109 9 8 0 101
a=rtpmap:109 opus/48000/2
a=fmtp:109 maxplaybackrate=48000;stereo=1;useinbandfec=1
a=rtpmap:9 G722/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
a=rtcp-xr:voip-metrics stat-summary=loss,dup,jitt
a=crypto:1 AES_256_CM_HMAC_SHA1_80 inline:REDACT
a=crypto:2 AES_256_CM_HMAC_SHA1_32 inline:REDACT
a=crypto:3 AES_CM_128_HMAC_SHA1_80 inline:REDACT
a=crypto:4 AES_CM_128_HMAC_SHA1_32 inline:REDACT

REDACT@REDACT: selected for REDACT
REDACT@REDACT: selected for REDACT
ua: using connection-address REDACT of SDP offer
stream: audio: starting mediaenc 'srtp' (wait_secure=0)
12:51:03.336#
TLS REDACT -> REDACT
SIP/2.0 180 Ringing
Call-ID: a01984cd356f30a3c20b3c29b5cacdd9@REDACT
CSeq: 17429 INVITE
From: "+REDACT" <sip:+REDACT@REDACT>;tag=XVPLSfvv5yY
To: "REDACT" <sip:+REDACT@REDACT>;tag=af4d4c450de16ddf
Via: SIP/2.0/TLS REDACT;branch=z9hG4bKtAnn4iOZQS2OYCtGatPSGw
Server: baresip v3.21.0 (x86_64/Linux)
Contact: <sip:REDACT@REDACT;transport=tls>
Allow: INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO,MESSAGE,UPDATE,REFER
Content-Length: 0


sip:REDACT@REDACT: Incoming call from: +REDACT sip:+REDACT@REDACT - audio-video: sendrecv-inactive - (press 'a' to accept)
12:51:07.636#
TLS REDACT -> REDACT
CANCEL sip:REDACT@REDACT;transport=tls SIP/2.0
Call-ID: a01984cd356f30a3c20b3c29b5cacdd9@REDACT
CSeq: 17429 CANCEL
From: "+REDACT" <sip:+REDACT@REDACT>;tag=XVPLSfvv5yY
To: "REDACT" <sip:+REDACT@REDACT>
Via: SIP/2.0/TLS REDACT;branch=z9hG4bKtAnn4iOZQS2OYCtGatPSGw
Max-Forwards: 70
Contact: <sip:REDACT@REDACT;transport=tls;ob>
Reason: SIP;cause=200;text="Call completed elsewhere"
User-Agent: REDACT
Content-Length: 0

src/sip/strans.c Outdated
mem_deref(st->mb);
}

const struct sip_msg *sipsess_cancel_msg(struct sip_strans *st) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if st is NULL please return NULL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

@@ -219,6 +223,7 @@ static bool cancel_handler(struct sip *sip, const struct sip_msg *msg)

case TRYING:
case PROCEEDING:
st->cancel_msg = mem_ref((void *)msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do mem_deref first to avoid memory leaks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite get what this should achieve? I store a ref to this message, calling mem_deref first would effectively keep the ref counter the same right?

I tried to copy the code for strans.msg which does a mem_ref before storing it in the strans.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if cancel_msg points to another object before calling, there will be a leak.

please look at this example:

src/http/client.c-1000- mem_deref(cli->tls);
src/http/client.c:1001: cli->tls = mem_ref(tls);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Sorry for the confusion. I didn't assume that this codepath could be taken more than once, but better safe than sorry 👍

@KillingSpark KillingSpark marked this pull request as draft May 16, 2025 12:38
@KillingSpark
Copy link
Contributor Author

This doesn't work yet, the sipsess->st seems to be cleared before the cancel_handler is called. I'll see if I can track down how this happens...

@KillingSpark KillingSpark marked this pull request as ready for review May 16, 2025 13:12
src/sip/sip.h Outdated
@@ -104,3 +104,5 @@ int sip_keepalive_udp(struct sip_keepalive *ka, struct sip *sip,
/* sip_conncfg */
struct sip_conncfg *sip_conncfg_find(struct sip *sip,
const struct sa *paddr);

const struct sip_msg *sipsess_cancel_msg(struct sip_strans *st);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line can be moved to re_sip.h

src/sip/strans.c Outdated
@@ -44,7 +45,6 @@ struct sip_strans {
bool invite;
};


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, please revert ...

@alfredh
Copy link
Contributor

alfredh commented May 20, 2025

Thanks for the updates ... This looks good now ...

@sreimers sreimers merged commit 74e7952 into baresip:main May 20, 2025
38 checks passed
jmontorosf pushed a commit to sipfront/re that referenced this pull request Jun 12, 2025
* store cancel message in sip_trans and pass to termination/close handler

* protect against null pointer access, and get cancel message before sipsess->st gets cleared in sip_treply

* deref cancel message before setting it to avoid leaks and move function to re_sip.h

* fix typo

* Revert unintended deleted empty line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants