Skip to content

Commit

Permalink
Merge pull request #12 from rtckit/v0.3.3
Browse files Browse the repository at this point in the history
v0.3.3
  • Loading branch information
cdosoftei authored Sep 27, 2021
2 parents d0f9a6f + 109834c commit d953fe5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rtckit/sip",
"description": "Parser/Renderer for SIP protocol written in PHP",
"version": "0.3.2",
"version": "0.3.3",
"type": "library",
"keywords": [
"sip",
Expand Down
48 changes: 44 additions & 4 deletions src/Header/ViaHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,38 @@ public static function parse(array $hbody): ViaHeader

$pv = isset($p[1]) ? trim($p[1]) : '';

if ($p[0] === 'branch') {
$val->branch = $pv;
} else {
$val->params[$p[0]] = $pv;
switch ($p[0]) {
case 'branch':
$val->branch = $pv;
break;

case 'received':
if (!filter_var($pv, FILTER_VALIDATE_IP)) {
throw new InvalidHeaderParameter('Invalid Via header received parameter', Response::BAD_REQUEST);
}

$val->received = $pv;
break;

case 'rport':
if (!strlen($pv)) {
$val->rport = 0;
} else if (!ctype_digit($pv)) {
throw new InvalidHeaderParameter('Invalid Via header rport parameter', Response::BAD_REQUEST);
}

$rport = (int)$pv;

if ($rport > 65535) {
throw new InvalidHeaderParameter('Invalid Via header rport parameter', Response::BAD_REQUEST);
}

$val->rport = $rport;
break;

default:
$val->params[$p[0]] = $pv;
break;
}
}

Expand Down Expand Up @@ -117,6 +145,18 @@ public function render(string $hname): string
$ret .= ";branch={$value->branch}";
}

if (isset($value->received)) {
$ret .= ";received={$value->received}";
}

if (isset($value->rport)) {
if (!$value->rport) {
$ret .= ";rport";
} else {
$ret .= ";rport={$value->rport}";
}
}

foreach ($value->params as $pk => $pv) {
$ret .= ';' . $pk . (!isset($pv[0]) ? '' : "={$pv}");
}
Expand Down
6 changes: 6 additions & 0 deletions src/Header/ViaValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class ViaValue
/** @var string Via branch parameters */
public string $branch;

/** @var string Source IP address, different than the Via host */
public string $received;

/** @var int Response port */
public int $rport;

/** @var array<string, string> Additional parameters */
public array $params = [];
}
50 changes: 49 additions & 1 deletion tests/Header/ViaHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ public function testShouldNotParseMissingHost()
ViaHeader::parse(['SIP/2.0/TCP']);
}

public function testShouldNotParseEmptyReceivedParameter()
{
$this->expectException(InvalidHeaderParameter::class);
ViaHeader::parse(['SIP/2.0/TCP 178.73.76.230:5060;branch=z9hG4bKiokioukju908;received']);
}

public function testShouldNotParseNonIPReceivedParameter()
{
$this->expectException(InvalidHeaderParameter::class);
ViaHeader::parse(['SIP/2.0/TCP 178.73.76.230:5060;branch=z9hG4bKiokioukju908;received=some.fqdn.com']);
}

public function testShouldNotParseNonNumericRPortParameter()
{
$this->expectException(InvalidHeaderParameter::class);
ViaHeader::parse(['SIP/2.0/TCP 178.73.76.230:5060;branch=z9hG4bKiokioukju908;rport=onethousandtwentyfour']);
}

public function testShouldNotParseLargeRPortParameter()
{
$this->expectException(InvalidHeaderParameter::class);
ViaHeader::parse(['SIP/2.0/TCP 178.73.76.230:5060;branch=z9hG4bKiokioukju908;rport=318272']);
}

public function testShouldNotParseEmptyParameterNames()
{
$this->expectException(InvalidHeaderParameter::class);
Expand All @@ -83,14 +107,38 @@ public function testShouldRenderWellFormedValues()
$via->values[0]->transport = 'UDP';
$via->values[0]->host = '192.0.2.4:5060';
$via->values[0]->branch = 'z9hG4bKnashds7';
$via->values[0]->received = '64.52.36.12';
$via->values[0]->rport = 1025;
$via->values[0]->params['custom'] = 'something';

$rendered = $via->render('Via');

$this->assertNotNull($rendered);
$this->assertIsString($rendered);
$this->assertEquals(
'Via: SIP/2.0/UDP 192.0.2.4:5060;branch=z9hG4bKnashds7;received=64.52.36.12;rport=1025;custom=something' . "\r\n",
$rendered
);
}

public function testShouldRenderEmptyRPort()
{
$via = new ViaHeader;
$via->values[0] = new ViaValue;
$via->values[0]->protocol = 'SIP';
$via->values[0]->version = '2.0';
$via->values[0]->transport = 'UDP';
$via->values[0]->host = '192.0.2.4:5060';
$via->values[0]->branch = 'z9hG4bKnashds7';
$via->values[0]->rport = 0;
$via->values[0]->params['custom'] = 'something';

$rendered = $via->render('Via');

$this->assertNotNull($rendered);
$this->assertIsString($rendered);
$this->assertEquals(
'Via: SIP/2.0/UDP 192.0.2.4:5060;branch=z9hG4bKnashds7;custom=something' . "\r\n",
'Via: SIP/2.0/UDP 192.0.2.4:5060;branch=z9hG4bKnashds7;rport;custom=something' . "\r\n",
$rendered
);
}
Expand Down

0 comments on commit d953fe5

Please sign in to comment.