From 28bd075d456cff2f260595ea2767088082909d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 5 Dec 2021 20:54:22 +0100 Subject: [PATCH] Avoid dependency on `ext-filter` --- src/DnsConnector.php | 2 +- src/HappyEyeBallsConnector.php | 2 +- src/TcpConnector.php | 2 +- src/TcpServer.php | 2 +- tests/FunctionalConnectorTest.php | 10 +++++----- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/DnsConnector.php b/src/DnsConnector.php index 0b51a52c..d326d6d9 100644 --- a/src/DnsConnector.php +++ b/src/DnsConnector.php @@ -39,7 +39,7 @@ public function connect($uri) $connector = $this->connector; // skip DNS lookup / URI manipulation if this URI already contains an IP - if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) { + if (@\inet_pton($host) !== false) { return $connector->connect($original); } diff --git a/src/HappyEyeBallsConnector.php b/src/HappyEyeBallsConnector.php index 0a7c6ecb..9caa58ad 100644 --- a/src/HappyEyeBallsConnector.php +++ b/src/HappyEyeBallsConnector.php @@ -50,7 +50,7 @@ public function connect($uri) $host = \trim($parts['host'], '[]'); // skip DNS lookup / URI manipulation if this URI already contains an IP - if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) { + if (@\inet_pton($host) !== false) { return $this->connector->connect($original); } diff --git a/src/TcpConnector.php b/src/TcpConnector.php index 6195c6a7..a4d3b5ba 100644 --- a/src/TcpConnector.php +++ b/src/TcpConnector.php @@ -34,7 +34,7 @@ public function connect($uri) } $ip = \trim($parts['host'], '[]'); - if (false === \filter_var($ip, \FILTER_VALIDATE_IP)) { + if (@\inet_pton($ip) === false) { return Promise\reject(new \InvalidArgumentException( 'Given URI "' . $uri . '" does not contain a valid host IP (EINVAL)', \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22 diff --git a/src/TcpServer.php b/src/TcpServer.php index 53d5317b..442af702 100644 --- a/src/TcpServer.php +++ b/src/TcpServer.php @@ -160,7 +160,7 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a ); } - if (false === \filter_var(\trim($parts['host'], '[]'), \FILTER_VALIDATE_IP)) { + if (@\inet_pton(\trim($parts['host'], '[]')) === false) { throw new \InvalidArgumentException( 'Given URI "' . $uri . '" does not contain a valid host IP (EINVAL)', \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22 diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index 1536a2ea..0ece63ae 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -90,7 +90,7 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() $ip = Block\await($this->request('dual.tlund.se', $connector), $loop, self::TIMEOUT); - $this->assertSame($ip, filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6), $ip); + $this->assertNotFalse(inet_pton($ip)); } /** @@ -110,8 +110,8 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP() throw $e; } - $this->assertSame($ip, filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4), $ip); - $this->assertFalse(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6), $ip); + $this->assertNotFalse(inet_pton($ip)); + $this->assertEquals(4, strlen(inet_pton($ip))); } /** @@ -131,8 +131,8 @@ public function connectionToRemoteTCP6ServerShouldResultInOurIP() throw $e; } - $this->assertFalse(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4), $ip); - $this->assertSame($ip, filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6), $ip); + $this->assertNotFalse(inet_pton($ip)); + $this->assertEquals(16, strlen(inet_pton($ip))); } public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpConnectionToServer()