Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/grammar/pegjs/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,27 @@ method_param = "method="i method: Method {

ttl_param = "ttl="i ttl: ttl {
options = options || { data: {}};
if(!options.data.params) options.data.params={};
options.data.params['ttl'] = ttl; }
if(!options.data.uri_params) options.data.uri_params={};
options.data.uri_params['ttl'] = ttl; }

maddr_param = "maddr="i maddr: host {
maddr_param = "maddr="i maddr: maddr_host {
options = options || { data: {}};
if(!options.data.uri_params) options.data.uri_params={};
options.data.uri_params['maddr'] = maddr; }

// maddr carries a host value but must not go through the "host" rule, whose action
// stores the URI (or Via sent-by) host and would overwrite it with the maddr value.
// The host_type set by the sub-rules is restored so it keeps describing the host.
maddr_host = host_type: maddr_host_type ( hostname / IPv4address / IPv6reference ) {
options = options || { data: {}};
if (host_type === undefined) { delete options.data.host_type; }
else { options.data.host_type = host_type; }
return text(); }

maddr_host_type = "" {
options = options || { data: {}};
return options.data.host_type; }

lr_param = "lr"i ('=' token)? {
options = options || { data: {}};
if(!options.data.uri_params) options.data.uri_params={};
Expand Down Expand Up @@ -870,7 +883,7 @@ via_ttl = "ttl"i EQUAL via_ttl_value: ttl {
options = options || { data: {}};
options.data.ttl = via_ttl_value; }

via_maddr = "maddr"i EQUAL via_maddr: host {
via_maddr = "maddr"i EQUAL via_maddr: maddr_host {
options = options || { data: {}};
options.data.maddr = via_maddr; }

Expand Down
33 changes: 33 additions & 0 deletions test/spec/core/grammar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,39 @@ describe("Core Grammar", () => {
});
});

describe("URI maddr and ttl parameters", () => {
// RFC 3261 Section 19.1.3
const uriString = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15";

it("keeps the URI host when maddr is present", () => {
const uri = Grammar.URIParse(uriString) as URI;
expect(uri.host).toEqual("atlanta.com");
expect(uri.getParam("maddr")).toEqual("239.255.255.1");
});

it("stores ttl as a URI parameter", () => {
const uri = Grammar.URIParse(uriString) as URI;
expect(uri.hasParam("ttl")).toBeTruthy();
expect(uri.getParam("ttl")).toEqual("15");
expect(uri.toString()).toEqual(uriString);
});

it("does not leak a URI ttl into the header parameters", () => {
const contacts = Grammar.parse("<sip:alice@atlanta.com;ttl=15>;expires=60", "Contact");
const contact = contacts[0].parsed as NameAddrHeader;
expect(contact.hasParam("ttl")).toBeFalsy();
expect(contact.getParam("expires")).toEqual("60");
expect(contact.uri.getParam("ttl")).toEqual("15");
});

it("keeps the Via sent-by host when maddr is present", () => {
const via = Grammar.parse("SIP/2.0/UDP pc33.atlanta.com;maddr=224.2.0.1;branch=z9hG4bK776asdhds", "Via");
expect(via.host).toEqual("pc33.atlanta.com");
expect(via.host_type).toEqual("domain");
expect(via.maddr).toEqual("224.2.0.1");
});
});

describe("Replaces", () => {
const goods = [
"98732@sip.example.com\r\n" + " ;from-tag=r33th4x0r\r\n" + " ;to-tag=ff87ff",
Expand Down