Skip to content

Commit

Permalink
For #1920, refine rtmp listen ip and port check
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-guanghua committed Sep 5, 2021
1 parent 714e182 commit 867a9c2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
14 changes: 11 additions & 3 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions trunk/src/kernel/srs_kernel_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+-."
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/kernel/srs_kernel_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions trunk/src/utest/srs_utest_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

0 comments on commit 867a9c2

Please sign in to comment.