From 867a9c2e5bf6eec5e7661d984e9e8eb89f522fed Mon Sep 17 00:00:00 2001 From: chengh Date: Sun, 5 Sep 2021 08:56:05 +0800 Subject: [PATCH] For #1920, refine rtmp listen ip and port check --- trunk/src/app/srs_app_config.cpp | 14 +++++++++++--- trunk/src/kernel/srs_kernel_utility.cpp | 18 ++++++++++++++++++ trunk/src/kernel/srs_kernel_utility.hpp | 3 +++ trunk/src/utest/srs_utest_kernel.cpp | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index c90c622fec..d664536b18 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -3617,9 +3617,17 @@ srs_error_t SrsConfig::check_normal_config() return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen requires params"); } for (int i = 0; i < (int)listens.size(); i++) { - string port = listens[i]; - if (port.empty() || ::atoi(port.c_str()) <= 0) { - return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen.port=%s is invalid", port.c_str()); + int port; string ip; + srs_parse_endpoint(listens[i], ip, port); + + // check ip + if (!srs_check_ip_addr_valid(ip)) { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen.ip=%s is invalid", ip.c_str()); + } + + // check port + if (port <= 0) { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen.port=%d is invalid", port); } } } diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 36e8608ab4..645c333456 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -258,6 +258,24 @@ void srs_parse_endpoint(string hostport, string& ip, int& port) } } +bool srs_check_ip_addr_valid(string ip) +{ + unsigned char buf[sizeof(struct in6_addr)]; + + // check ipv4 + int ret = inet_pton(AF_INET, ip.data(), buf); + if (ret > 0) { + return true; + } + + ret = inet_pton(AF_INET6, ip.data(), buf); + if (ret > 0) { + return true; + } + + return false; +} + string srs_int2str(int64_t value) { // len(max int64_t) is 20, plus one "+-." diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index 7f82048b3f..b019b8f620 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -45,6 +45,9 @@ extern void srs_parse_hostport(std::string hostport, std::string& host, int& por // @remark The hostport format in <[ip:]port>, where ip is default to "0.0.0.0". extern void srs_parse_endpoint(std::string hostport, std::string& ip, int& port); +// Check whether the ip is valid. +extern bool srs_check_ip_addr_valid(std::string ip); + // Parse the int64 value to string. extern std::string srs_int2str(int64_t value); // Parse the float value to string, precise is 2. diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index e59a26763d..872c986410 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -5528,3 +5528,18 @@ VOID TEST(KernelUtilityTest, CoverStringAssign) ASSERT_STREQ("", sps.c_str()); } +VOID TEST(KernelUtilityTest, CoverCheckIPAddrValid) +{ + ASSERT_TRUE(srs_check_ip_addr_valid("172.16.254.1")); + ASSERT_TRUE(srs_check_ip_addr_valid("2001:0db8:85a3:0:0:8A2E:0370:7334")); + ASSERT_FALSE(srs_check_ip_addr_valid("")); + + //IPv4 any addr + ASSERT_TRUE(srs_check_ip_addr_valid("0.0.0.0")); + //IPV6 any addr + ASSERT_TRUE(srs_check_ip_addr_valid("::")); + + ASSERT_FALSE(srs_check_ip_addr_valid("256.256.256.256")); + ASSERT_FALSE(srs_check_ip_addr_valid("2001:0db8:85a3:0:0:8A2E:0370:7334:")); + ASSERT_FALSE(srs_check_ip_addr_valid("1e1.4.5.6")); +} \ No newline at end of file