Skip to content

Commit

Permalink
sipreg: supports PNS custom contact URI (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
codyit authored Jun 8, 2023
1 parent 3dfac91 commit e83debd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/re_sipreg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ void sipreg_incfailc(struct sipreg *reg);

int sipreg_set_fbregint(struct sipreg *reg, uint32_t fbregint);
void sipreg_set_srcport(struct sipreg *reg, uint16_t srcport);
int sipreg_set_contact_params(struct sipreg *reg, const char *cparams);
21 changes: 20 additions & 1 deletion src/sipreg/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct sipreg {
struct sip_auth *auth;
struct mbuf *hdrs;
char *cuser;
char *cparams;
sip_resp_h *resph;
void *arg;
uint32_t expires;
Expand Down Expand Up @@ -93,6 +94,7 @@ static void destructor(void *arg)
mem_deref(reg->sip);
mem_deref(reg->hdrs);
mem_deref(reg->params);
mem_deref(reg->cparams);
}


Expand Down Expand Up @@ -299,8 +301,10 @@ static int send_handler(enum sip_transp tp, struct sa *src,
sa_set_port(src, reg->srcport);

reg->laddr = *src;
err = mbuf_printf(mb, "Contact: <sip:%s@%J%s>;expires=%u%s%s",
err = mbuf_printf(mb, "Contact: <sip:%s@%J%s%s%s>;expires=%u%s%s",
reg->cuser, &reg->laddr, sip_transp_param(reg->tp),
reg->cparams ? ";" : "",
reg->cparams ? reg->cparams : "",
reg->expires,
reg->params ? ";" : "",
reg->params ? reg->params : "");
Expand Down Expand Up @@ -619,3 +623,18 @@ void sipreg_set_srcport(struct sipreg *reg, uint16_t srcport)

reg->srcport = srcport;
}


/**
* Set contact URI optional parameters
*
* @param reg SIP registration client
* @param cparams Contact URI optional parameters
*/
int sipreg_set_contact_params(struct sipreg *reg, const char *cparams)
{
if (!reg)
return EINVAL;

return str_dup(&reg->cparams, cparams);
}
9 changes: 9 additions & 0 deletions test/mock/sipsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ static bool sip_msg_handler(const struct sip_msg *msg, void *arg)
int err;

if (0 == pl_strcmp(&msg->met, "REGISTER")) {
mbuf_set_pos(msg->mb, 0);
if (sip_msg_decode(&srv->sip_msgs[srv->n_register_req],
msg->mb)) {
DEBUG_NOTICE("sip message cannot be parsed (%r)\n",
&msg->met);
return false;
}
++srv->n_register_req;
}
else {
Expand Down Expand Up @@ -55,6 +62,8 @@ static void destructor(void *arg)
srv->terminate = true;

sip_close(srv->sip, false);
for (unsigned i=0; i<RE_ARRAY_SIZE(srv->sip_msgs); i++)
mem_deref(srv->sip_msgs[i]);
mem_deref(srv->sip);
}

Expand Down
15 changes: 15 additions & 0 deletions test/sipreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ static void sip_resp_handler(int err, const struct sip_msg *msg, void *arg)
re_cancel();
}

#define CPARAMS "some-param=test;other-param=123;"


static int reg_test(enum sip_transp tp, bool deprecated, uint16_t srcport)
{
struct test test;
struct sip_server *srv = NULL;
struct sipreg *reg = NULL;
struct sip *sip = NULL;
const struct sip_hdr *contact_hdr = NULL;
char reg_uri[256];
int err;

Expand Down Expand Up @@ -148,6 +151,8 @@ static int reg_test(enum sip_transp tp, bool deprecated, uint16_t srcport)
"sip:x@test",
3600, "x", NULL, 0, 0, NULL, NULL, false,
sip_resp_handler, &test, NULL, NULL);
err |= sipreg_set_contact_params(reg, CPARAMS);

if (srcport)
sipreg_set_srcport(reg, srcport);

Expand All @@ -168,6 +173,16 @@ static int reg_test(enum sip_transp tp, bool deprecated, uint16_t srcport)
TEST_ASSERT(srv->n_register_req > 0);
TEST_ASSERT(test.n_resp > 0);

if (!deprecated) {
contact_hdr = sip_msg_hdr(
srv->sip_msgs[srv->n_register_req - 1],
SIP_HDR_CONTACT);
err = re_regex(contact_hdr->val.p, contact_hdr->val.l,
";" CPARAMS ">;expires=", NULL);
TEST_ERR(err);
}


out:
mem_deref(reg);

Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ struct sip_server {
bool terminate;

unsigned n_register_req;
struct sip_msg *sip_msgs[16];
};

int sip_server_alloc(struct sip_server **srvp);
Expand Down

0 comments on commit e83debd

Please sign in to comment.