mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
parent
714e182096
commit
3f94d26ca4
4 changed files with 47 additions and 3 deletions
|
@ -3617,9 +3617,17 @@ srs_error_t SrsConfig::check_normal_config()
|
||||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen requires params");
|
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen requires params");
|
||||||
}
|
}
|
||||||
for (int i = 0; i < (int)listens.size(); i++) {
|
for (int i = 0; i < (int)listens.size(); i++) {
|
||||||
string port = listens[i];
|
int port; string ip;
|
||||||
if (port.empty() || ::atoi(port.c_str()) <= 0) {
|
srs_parse_endpoint(listens[i], ip, port);
|
||||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen.port=%s is invalid", port.c_str());
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
string srs_int2str(int64_t value)
|
||||||
{
|
{
|
||||||
// len(max int64_t) is 20, plus one "+-."
|
// len(max int64_t) is 20, plus one "+-."
|
||||||
|
|
|
@ -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".
|
// @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);
|
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.
|
// Parse the int64 value to string.
|
||||||
extern std::string srs_int2str(int64_t value);
|
extern std::string srs_int2str(int64_t value);
|
||||||
// Parse the float value to string, precise is 2.
|
// Parse the float value to string, precise is 2.
|
||||||
|
|
|
@ -5528,3 +5528,18 @@ VOID TEST(KernelUtilityTest, CoverStringAssign)
|
||||||
ASSERT_STREQ("", sps.c_str());
|
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"));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue