mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
IPv6 support (for 3.0 release) (#988)
* IPv6 support, part 1. * IPv6 support, part 2. * Some more IPv6 work. * Made functions for address:port paŕsing IPv6-capable. * Fixed type (compile warning). * Fixed formatting. * Reverted option change. * Replaced abort() by proper error handling. * Also retrieving local IPv6 addresses now.
This commit is contained in:
parent
db08f1586c
commit
feaae341b9
20 changed files with 296 additions and 201 deletions
|
@ -154,50 +154,79 @@ int64_t srs_update_system_time_ms()
|
|||
return _srs_system_time_us_cache / 1000;
|
||||
}
|
||||
|
||||
string srs_dns_resolve(string host)
|
||||
string srs_dns_resolve(string host, int& family)
|
||||
{
|
||||
if (inet_addr(host.c_str()) != INADDR_NONE) {
|
||||
return host;
|
||||
}
|
||||
|
||||
hostent* answer = gethostbyname(host.c_str());
|
||||
if (answer == NULL) {
|
||||
addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = family;
|
||||
addrinfo* result = NULL;
|
||||
if(getaddrinfo(host.c_str(), NULL, NULL, &result) != 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char ipv4[16];
|
||||
memset(ipv4, 0, sizeof(ipv4));
|
||||
|
||||
// covert the first entry to ip.
|
||||
if (answer->h_length > 0) {
|
||||
inet_ntop(AF_INET, answer->h_addr_list[0], ipv4, sizeof(ipv4));
|
||||
char address_string[64];
|
||||
const int success = getnameinfo(result->ai_addr, result->ai_addrlen,
|
||||
(char*)&address_string, sizeof(address_string),
|
||||
NULL, 0,
|
||||
NI_NUMERICHOST);
|
||||
freeaddrinfo(result);
|
||||
|
||||
if(success) {
|
||||
family = result->ai_family;
|
||||
return string(address_string);
|
||||
}
|
||||
|
||||
return ipv4;
|
||||
return "";
|
||||
}
|
||||
|
||||
void srs_parse_hostport(const string& hostport, string& host, int& port)
|
||||
{
|
||||
size_t pos = hostport.find(":");
|
||||
const size_t pos = hostport.rfind(":"); // Look for ":" from the end, to work with IPv6.
|
||||
if (pos != std::string::npos) {
|
||||
string p = hostport.substr(pos + 1);
|
||||
host = hostport.substr(0, pos);
|
||||
const string p = hostport.substr(pos + 1);
|
||||
if ((pos >= 1) &&
|
||||
(hostport[0] == '[') &&
|
||||
(hostport[pos - 1] == ']')) {
|
||||
// Handle IPv6 in RFC 2732 format, e.g. [3ffe:dead:beef::1]:1935
|
||||
host = hostport.substr(1, pos - 2);
|
||||
}
|
||||
else {
|
||||
// Handle IP address
|
||||
host = hostport.substr(0, pos);
|
||||
}
|
||||
port = ::atoi(p.c_str());
|
||||
} else {
|
||||
host = hostport;
|
||||
}
|
||||
}
|
||||
|
||||
static int check_ipv6()
|
||||
{
|
||||
int sd = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if(sd >= 0) {
|
||||
close(sd);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void srs_parse_endpoint(string hostport, string& ip, int& port)
|
||||
{
|
||||
ip = "0.0.0.0";
|
||||
|
||||
size_t pos = string::npos;
|
||||
if ((pos = hostport.find(":")) != string::npos) {
|
||||
ip = hostport.substr(0, pos);
|
||||
string sport = hostport.substr(pos + 1);
|
||||
const size_t pos = hostport.rfind(":"); // Look for ":" from the end, to work with IPv6.
|
||||
if (pos != std::string::npos) {
|
||||
if ((pos >= 1) &&
|
||||
(hostport[0] == '[') &&
|
||||
(hostport[pos - 1] == ']')) {
|
||||
// Handle IPv6 in RFC 2732 format, e.g. [3ffe:dead:beef::1]:1935
|
||||
ip = hostport.substr(1, pos - 2);
|
||||
}
|
||||
else {
|
||||
// Handle IP address
|
||||
ip = hostport.substr(0, pos);
|
||||
}
|
||||
const string sport = hostport.substr(pos + 1);
|
||||
port = ::atoi(sport.c_str());
|
||||
} else {
|
||||
ip = check_ipv6() ? "::" : "0.0.0.0";
|
||||
port = ::atoi(hostport.c_str());
|
||||
}
|
||||
}
|
||||
|
@ -317,7 +346,7 @@ string srs_string_remove(string str, string remove_chars)
|
|||
|
||||
bool srs_string_ends_with(string str, string flag)
|
||||
{
|
||||
ssize_t pos = str.rfind(flag);
|
||||
const size_t pos = str.rfind(flag);
|
||||
return (pos != string::npos) && (pos == str.length() - flag.length());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue