Skip to content

Commit

Permalink
set TCP source port in Via and Contact header (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 authored Apr 27, 2022
1 parent 37a1d6a commit b7b2ca9
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/re_sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ struct sip_keepalive;
struct dnsc;

typedef bool(sip_msg_h)(const struct sip_msg *msg, void *arg);
typedef int(sip_send_h)(enum sip_transp tp, const struct sa *src,
typedef int(sip_send_h)(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg);
typedef void(sip_resp_h)(int err, const struct sip_msg *msg, void *arg);
Expand Down
16 changes: 12 additions & 4 deletions src/sip/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
static int request(struct sip_request *req, enum sip_transp tp,
const struct sa *dst)
{
struct mbuf *mb = NULL;
struct mbuf *mbs = NULL;
struct mbuf *mb = NULL;
struct mbuf *cont = NULL;
char *branch = NULL;
int err = ENOMEM;
Expand All @@ -170,7 +171,8 @@ static int request(struct sip_request *req, enum sip_transp tp,
req->provrecv = false;

branch = mem_alloc(24, NULL);
mb = mbuf_alloc(1024);
mbs = mbuf_alloc(256);
mb = mbuf_alloc(1024);

if (!branch || !mb)
goto out;
Expand All @@ -181,11 +183,16 @@ static int request(struct sip_request *req, enum sip_transp tp,
if (err)
goto out;

err = req->sendh ? req->sendh(tp, &laddr, dst, mbs, &cont, req->arg) :
0;
if (err)
goto out;

mbuf_set_pos(mbs, 0);
err = mbuf_printf(mb, "%s %s SIP/2.0\r\n", req->met, req->uri);
err |= mbuf_printf(mb, "Via: SIP/2.0/%s %J;branch=%s;rport\r\n",
sip_transp_name(tp), &laddr, branch);
err |= req->sendh ? req->sendh(tp, &laddr, dst, mb, &cont, req->arg) :
0;
err |= mbuf_write_mem(mb, mbuf_buf(mbs), mbuf_get_left(mbs));
err |= mbuf_write_mem(mb, mbuf_buf(req->mb), mbuf_get_left(req->mb));
err |= cont ? mbuf_write_mem(mb, mbuf_buf(cont), mbuf_get_left(cont)) :
0;
Expand All @@ -206,6 +213,7 @@ static int request(struct sip_request *req, enum sip_transp tp,

out:
mem_deref(branch);
mem_deref(mbs);
mem_deref(mb);

return err;
Expand Down
20 changes: 20 additions & 0 deletions src/sip/sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,23 @@ void sip_set_trace_handler(struct sip *sip, sip_trace_h *traceh)

sip->traceh = traceh;
}


struct sip_conncfg *sip_conncfg_find(struct sip *sip,
const struct sa *paddr)
{
struct le *le;

le = list_head(hash_list(sip->ht_conncfg, sa_hash(paddr, SA_ALL)));
for (; le; le = le->next) {

struct sip_conncfg *cfg = le->data;

if (!sa_cmp(&cfg->paddr, paddr, SA_ALL))
continue;

return cfg;
}

return NULL;
}
4 changes: 4 additions & 0 deletions src/sip/sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ int sip_keepalive_tcp(struct sip_keepalive *ka, struct sip_conn *conn,
int sip_keepalive_udp(struct sip_keepalive *ka, struct sip *sip,
struct udp_sock *us, const struct sa *paddr,
uint32_t interval);

/* sip_conncfg */
struct sip_conncfg *sip_conncfg_find(struct sip *sip,
const struct sa *paddr);
30 changes: 8 additions & 22 deletions src/sip/transp.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,6 @@ static const struct sip_transport *transp_find(struct sip *sip,
}


static struct sip_conncfg *conncfg_find(struct sip *sip,
const struct sa *paddr)
{
struct le *le;

le = list_head(hash_list(sip->ht_conncfg, sa_hash(paddr, SA_ALL)));
for (; le; le = le->next) {

struct sip_conncfg *cfg = le->data;

if (!sa_cmp(&cfg->paddr, paddr, SA_ALL))
continue;

return cfg;
}

return NULL;
}


static struct sip_conn *conn_find(struct sip *sip, const struct sa *paddr,
bool secure)
{
Expand Down Expand Up @@ -787,7 +767,7 @@ static int conn_send(struct sip_connqent **qentp, struct sip *sip, bool secure,
conn->sip = sip;
conn->tp = secure ? SIP_TRANSP_TLS : SIP_TRANSP_TCP;

conncfg = conncfg_find(sip, dst);
conncfg = sip_conncfg_find(sip, dst);
if (conncfg && conncfg->srcport) {
struct sa src;
sa_init(&src, sa_af(dst));
Expand Down Expand Up @@ -1526,6 +1506,7 @@ int sip_transp_laddr(struct sip *sip, struct sa *laddr, enum sip_transp tp,
const struct sa *dst)
{
const struct sip_transport *transp;
struct sip_conncfg *conncfg;

if (!sip || !laddr)
return EINVAL;
Expand All @@ -1535,6 +1516,11 @@ int sip_transp_laddr(struct sip *sip, struct sa *laddr, enum sip_transp tp,
return EPROTONOSUPPORT;

*laddr = transp->laddr;
if (tp != SIP_TRANSP_UDP) {
conncfg = sip_conncfg_find(sip, dst);
if (conncfg && conncfg->srcport)
sa_set_port(laddr, conncfg->srcport);
}

return 0;
}
Expand Down Expand Up @@ -1894,7 +1880,7 @@ int sip_conncfg_set(struct sip *sip, const struct sa *paddr,
if (!sip || !sa_isset(paddr, SA_ALL))
return EINVAL;

cfg = conncfg_find(sip, paddr);
cfg = sip_conncfg_find(sip, paddr);
if (cfg) {
cfg->srcport = conncfg.srcport;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/sipevent/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
}


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/sipevent/subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
}


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand Down
6 changes: 4 additions & 2 deletions src/sipreg/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
}


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand All @@ -276,9 +276,11 @@ static int send_handler(enum sip_transp tp, const struct sa *src,
(void)dst;
(void)contp;

reg->laddr = *src;
reg->tp = tp;
if (reg->srcport && tp != SIP_TRANSP_UDP)
sa_set_port(src, reg->srcport);

reg->laddr = *src;
err = mbuf_printf(mb, "Contact: <sip:%s@%J%s>;expires=%u%s%s",
reg->cuser, &reg->laddr, sip_transp_param(reg->tp),
reg->expires,
Expand Down
2 changes: 1 addition & 1 deletion src/sipsess/ack.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void tmr_handler(void *arg)
}


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/sipsess/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
static int invite(struct sipsess *sess);


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/sipsess/modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void reinvite_resp_handler(int err, const struct sip_msg *msg,
}


static int send_handler(enum sip_transp tp, const struct sa *src,
static int send_handler(enum sip_transp tp, struct sa *src,
const struct sa *dst, struct mbuf *mb,
struct mbuf **contp, void *arg)
{
Expand Down

0 comments on commit b7b2ca9

Please sign in to comment.