Skip to content

Commit

Permalink
sip: fix TCP source port (#921)
Browse files Browse the repository at this point in the history
* sip: fix TCP source port

The TCP source has at the `struct sip` has to be set before the TCP connection
is opened. This commit fixes an issue that was introduced with
5c709fc which postponed the call to the
`send_handler()` after the TCP connection is established.

* sipreg: remove unneeded check
  • Loading branch information
cspiel1 authored Aug 30, 2023
1 parent 2905f4f commit 9ac5d49
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/re_sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ int sip_dialog_update(struct sip_dialog *dlg, const struct sip_msg *msg);
bool sip_dialog_rseq_valid(struct sip_dialog *dlg, const struct sip_msg *msg);
const char *sip_dialog_callid(const struct sip_dialog *dlg);
int sip_dialog_set_callid(struct sip_dialog *dlg, const char *callid);
void sip_dialog_set_srcport(struct sip_dialog *dlg, uint16_t srcport);
uint16_t sip_dialog_srcport(struct sip_dialog *dlg);
const char *sip_dialog_uri(const struct sip_dialog *dlg);
uint32_t sip_dialog_lseq(const struct sip_dialog *dlg);
enum sip_transp sip_dialog_tp(const struct sip_dialog *dlg);
Expand Down
32 changes: 32 additions & 0 deletions src/sip/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct sip_dialog {
uint32_t rseq;
size_t cpos;
enum sip_transp tp;
uint32_t srcport;
};


Expand Down Expand Up @@ -578,6 +579,37 @@ int sip_dialog_set_callid(struct sip_dialog *dlg, const char *callid)
}


/**
* Set TCP source port for a SIP Dialog
*
* @param dlg SIP Dialog
* @param srcport The TCP source port to be used
*/
void sip_dialog_set_srcport(struct sip_dialog *dlg, uint16_t srcport)
{
if (!dlg)
return;

dlg->srcport = srcport;
}


/**
* Get TCP source port for a SIP Dialog
*
* @param dlg SIP Dialog
*
* @return TCP source port
*/
uint16_t sip_dialog_srcport(struct sip_dialog *dlg)
{
if (!dlg)
return 0;

return dlg->srcport;
}


/**
* Get the local sequence number from a SIP Dialog
*
Expand Down
7 changes: 7 additions & 0 deletions src/sip/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct sip_request {
bool canceled;
bool provrecv;
uint16_t port;
uint16_t srcport;
};


Expand Down Expand Up @@ -224,6 +225,11 @@ static int request(struct sip_request *req, enum sip_transp tp,
if (!req->branch || !mb)
goto out;

if (req->srcport) {
struct sip_conncfg cfg = {.srcport = req->srcport};
err = sip_conncfg_set(req->sip, dst, &cfg);
}

if (!req->stateful) {
err = sip_send_conn(req->sip, NULL, tp, dst, mb,
connect_handler, req);
Expand Down Expand Up @@ -940,6 +946,7 @@ int sip_drequestf(struct sip_request **reqp, struct sip *sip, bool stateful,
goto out;

req->reqp = reqp;
req->srcport = sip_dialog_srcport(dlg);
err = sip_request_send(req, sip, sip_dialog_route(dlg));

out:
Expand Down
13 changes: 4 additions & 9 deletions src/sipreg/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ static int send_handler(enum sip_transp tp, struct sa *src,
int err;

(void)contp;
(void)dst;

reg->tp = tp;
if (reg->srcport && tp != SIP_TRANSP_UDP)
Expand All @@ -313,14 +314,6 @@ static int send_handler(enum sip_transp tp, struct sa *src,
err |= mbuf_printf(mb, ";reg-id=%d", reg->regid);

err |= mbuf_printf(mb, "\r\n");

if (reg->srcport) {
struct sip_conncfg cfg;
memset(&cfg, 0, sizeof(cfg));
cfg.srcport = reg->srcport;
err = sip_conncfg_set(reg->sip, dst, &cfg);
}

return err;
}

Expand Down Expand Up @@ -618,10 +611,12 @@ int sipreg_set_fbregint(struct sipreg *reg, uint32_t fbregint)
*/
void sipreg_set_srcport(struct sipreg *reg, uint16_t srcport)
{
if (!reg)
if (!reg || !reg->dlg)
return;

reg->srcport = srcport;

sip_dialog_set_srcport(reg->dlg, srcport);
}


Expand Down

0 comments on commit 9ac5d49

Please sign in to comment.