mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
Refine the local ip and interface retrieve
This commit is contained in:
parent
26bcc09017
commit
215b1c234b
7 changed files with 85 additions and 81 deletions
|
@ -3682,13 +3682,15 @@ srs_error_t SrsConfig::check_normal_config()
|
||||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d", get_stats_network());
|
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d", get_stats_network());
|
||||||
}
|
}
|
||||||
if (true) {
|
if (true) {
|
||||||
vector<std::string> ips = srs_get_local_ips();
|
vector<SrsIPAddress*> ips = srs_get_local_ips();
|
||||||
int index = get_stats_network();
|
int index = get_stats_network();
|
||||||
if (index >= (int)ips.size()) {
|
if (index >= (int)ips.size()) {
|
||||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d of %d",
|
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d of %d",
|
||||||
index, (int)ips.size());
|
index, (int)ips.size());
|
||||||
}
|
}
|
||||||
srs_warn("stats network use index=%d, ip=%s", index, ips.at(index).c_str());
|
|
||||||
|
SrsIPAddress* addr = ips.at(index);
|
||||||
|
srs_warn("stats network use index=%d, ip=%s, ifname=%s", index, addr->ip.c_str(), addr->ifname.c_str());
|
||||||
}
|
}
|
||||||
if (true) {
|
if (true) {
|
||||||
SrsConfDirective* conf = get_stats_disk_device();
|
SrsConfDirective* conf = get_stats_disk_device();
|
||||||
|
|
|
@ -621,9 +621,10 @@ static std::string get_host_candidate_ips(SrsConfDirective* c)
|
||||||
{
|
{
|
||||||
string candidate = _srs_config->get_stream_caster_gb28181_host(c);
|
string candidate = _srs_config->get_stream_caster_gb28181_host(c);
|
||||||
if (candidate == "*" || candidate == "0.0.0.0") {
|
if (candidate == "*" || candidate == "0.0.0.0") {
|
||||||
std::vector<std::string> ips = srs_get_local_ips();
|
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
||||||
int index = _srs_config->get_stats_network();
|
int index = _srs_config->get_stats_network();
|
||||||
return ips.at(index);
|
SrsIPAddress* ip = ips.at(index);
|
||||||
|
return ip->ip;
|
||||||
} else {
|
} else {
|
||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,10 +65,10 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat()
|
||||||
return srs_error_wrap(err, "http uri parse hartbeart url failed. url=%s", url.c_str());
|
return srs_error_wrap(err, "http uri parse hartbeart url failed. url=%s", url.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ip = "";
|
SrsIPAddress* ip = NULL;
|
||||||
std::string device_id = _srs_config->get_heartbeat_device_id();
|
std::string device_id = _srs_config->get_heartbeat_device_id();
|
||||||
|
|
||||||
vector<string>& ips = srs_get_local_ips();
|
vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
||||||
if (!ips.empty()) {
|
if (!ips.empty()) {
|
||||||
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];
|
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat()
|
||||||
SrsAutoFree(SrsJsonObject, obj);
|
SrsAutoFree(SrsJsonObject, obj);
|
||||||
|
|
||||||
obj->set("device_id", SrsJsonAny::str(device_id.c_str()));
|
obj->set("device_id", SrsJsonAny::str(device_id.c_str()));
|
||||||
obj->set("ip", SrsJsonAny::str(ip.c_str()));
|
obj->set("ip", SrsJsonAny::str(ip->ip.c_str()));
|
||||||
|
|
||||||
if (_srs_config->get_heartbeat_summaries()) {
|
if (_srs_config->get_heartbeat_summaries()) {
|
||||||
SrsJsonObject* summaries = SrsJsonAny::object();
|
SrsJsonObject* summaries = SrsJsonAny::object();
|
||||||
|
|
|
@ -111,15 +111,20 @@ static std::vector<std::string> get_candidate_ips()
|
||||||
std::vector<std::string> candidate_ips;
|
std::vector<std::string> candidate_ips;
|
||||||
|
|
||||||
string candidate = _srs_config->get_rtc_server_candidates();
|
string candidate = _srs_config->get_rtc_server_candidates();
|
||||||
if (candidate == "*" || candidate == "0.0.0.0") {
|
if (candidate != "*" && candidate != "0.0.0.0") {
|
||||||
std::vector<std::string> tmp = srs_get_local_ips();
|
|
||||||
for (int i = 0; i < (int)tmp.size(); ++i) {
|
|
||||||
if (tmp[i] != "127.0.0.1") {
|
|
||||||
candidate_ips.push_back(tmp[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
candidate_ips.push_back(candidate);
|
candidate_ips.push_back(candidate);
|
||||||
|
return candidate_ips;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For * or 0.0.0.0, auto discovery expose ip addresses.
|
||||||
|
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
||||||
|
for (int i = 0; i < (int)ips.size(); ++i) {
|
||||||
|
SrsIPAddress* ip = ips[i];
|
||||||
|
if (!ip->is_loopback) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
candidate_ips.push_back(ip->ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
return candidate_ips;
|
return candidate_ips;
|
||||||
|
|
|
@ -81,13 +81,13 @@ SrsSimpleRtmpClient::~SrsSimpleRtmpClient()
|
||||||
|
|
||||||
srs_error_t SrsSimpleRtmpClient::connect_app()
|
srs_error_t SrsSimpleRtmpClient::connect_app()
|
||||||
{
|
{
|
||||||
std::vector<std::string> ips = srs_get_local_ips();
|
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
||||||
assert(_srs_config->get_stats_network() < (int)ips.size());
|
assert(_srs_config->get_stats_network() < (int)ips.size());
|
||||||
std::string local_ip = ips[_srs_config->get_stats_network()];
|
SrsIPAddress* local_ip = ips[_srs_config->get_stats_network()];
|
||||||
|
|
||||||
bool debug_srs_upnode = _srs_config->get_debug_srs_upnode(req->vhost);
|
bool debug_srs_upnode = _srs_config->get_debug_srs_upnode(req->vhost);
|
||||||
|
|
||||||
return do_connect_app(local_ip, debug_srs_upnode);
|
return do_connect_app(local_ip->ip, debug_srs_upnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsClientInfo::SrsClientInfo()
|
SrsClientInfo::SrsClientInfo()
|
||||||
|
|
|
@ -144,9 +144,9 @@ bool srs_net_device_is_internet(const sockaddr* addr)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<string> _srs_system_ips;
|
vector<SrsIPAddress*> _srs_system_ips;
|
||||||
|
|
||||||
void discover_network_iface(ifaddrs* cur, vector<string>& ips, stringstream& ss0, stringstream& ss1, bool ipv6)
|
void discover_network_iface(ifaddrs* cur, vector<SrsIPAddress*>& ips, stringstream& ss0, stringstream& ss1, bool ipv6, bool loopback)
|
||||||
{
|
{
|
||||||
char saddr[64];
|
char saddr[64];
|
||||||
char* h = (char*)saddr;
|
char* h = (char*)saddr;
|
||||||
|
@ -160,10 +160,17 @@ void discover_network_iface(ifaddrs* cur, vector<string>& ips, stringstream& ss0
|
||||||
std::string ip(saddr, strlen(saddr));
|
std::string ip(saddr, strlen(saddr));
|
||||||
ss0 << ", iface[" << (int)ips.size() << "] " << cur->ifa_name << " " << (ipv6? "ipv6":"ipv4")
|
ss0 << ", iface[" << (int)ips.size() << "] " << cur->ifa_name << " " << (ipv6? "ipv6":"ipv4")
|
||||||
<< " 0x" << std::hex << cur->ifa_flags << std::dec << " " << ip;
|
<< " 0x" << std::hex << cur->ifa_flags << std::dec << " " << ip;
|
||||||
ips.push_back(ip);
|
|
||||||
|
SrsIPAddress* ip_address = new SrsIPAddress();
|
||||||
|
ip_address->ip = ip;
|
||||||
|
ip_address->is_ipv4 = !ipv6;
|
||||||
|
ip_address->is_loopback = loopback;
|
||||||
|
ip_address->ifname = cur->ifa_name;
|
||||||
|
ip_address->is_internet = srs_net_device_is_internet(cur->ifa_addr);
|
||||||
|
ips.push_back(ip_address);
|
||||||
|
|
||||||
// set the device internet status.
|
// set the device internet status.
|
||||||
if (!srs_net_device_is_internet(cur->ifa_addr)) {
|
if (!ip_address->is_internet) {
|
||||||
ss1 << ", intranet ";
|
ss1 << ", intranet ";
|
||||||
_srs_device_ifs[cur->ifa_name] = false;
|
_srs_device_ifs[cur->ifa_name] = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -175,10 +182,16 @@ void discover_network_iface(ifaddrs* cur, vector<string>& ips, stringstream& ss0
|
||||||
|
|
||||||
void retrieve_local_ips()
|
void retrieve_local_ips()
|
||||||
{
|
{
|
||||||
vector<string>& ips = _srs_system_ips;
|
vector<SrsIPAddress*>& ips = _srs_system_ips;
|
||||||
|
|
||||||
|
// Release previous IPs.
|
||||||
|
for (int i = 0; i < (int)ips.size(); i++) {
|
||||||
|
SrsIPAddress* ip = ips[i];
|
||||||
|
srs_freep(ip);
|
||||||
|
}
|
||||||
ips.clear();
|
ips.clear();
|
||||||
|
|
||||||
|
// Get the addresses.
|
||||||
ifaddrs* ifap;
|
ifaddrs* ifap;
|
||||||
if (getifaddrs(&ifap) == -1) {
|
if (getifaddrs(&ifap) == -1) {
|
||||||
srs_warn("retrieve local ips, getifaddrs failed.");
|
srs_warn("retrieve local ips, getifaddrs failed.");
|
||||||
|
@ -207,8 +220,9 @@ void retrieve_local_ips()
|
||||||
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
||||||
// Ignore IFF_PROMISC(Interface is in promiscuous mode), which may be set by Wireshark.
|
// Ignore IFF_PROMISC(Interface is in promiscuous mode), which may be set by Wireshark.
|
||||||
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_LOOPBACK) || (cur->ifa_flags & IFF_POINTOPOINT);
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_LOOPBACK) || (cur->ifa_flags & IFF_POINTOPOINT);
|
||||||
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||||
if (ipv4 && ready && !ignored) {
|
if (ipv4 && ready && !ignored) {
|
||||||
discover_network_iface(cur, ips, ss0, ss1, false);
|
discover_network_iface(cur, ips, ss0, ss1, false, loopback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,8 +241,9 @@ void retrieve_local_ips()
|
||||||
bool ipv6 = (cur->ifa_addr->sa_family == AF_INET6);
|
bool ipv6 = (cur->ifa_addr->sa_family == AF_INET6);
|
||||||
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
||||||
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC) || (cur->ifa_flags & IFF_LOOPBACK);
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC) || (cur->ifa_flags & IFF_LOOPBACK);
|
||||||
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||||
if (ipv6 && ready && !ignored) {
|
if (ipv6 && ready && !ignored) {
|
||||||
discover_network_iface(cur, ips, ss0, ss1, true);
|
discover_network_iface(cur, ips, ss0, ss1, true, loopback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,8 +263,9 @@ void retrieve_local_ips()
|
||||||
bool ipv4 = (cur->ifa_addr->sa_family == AF_INET);
|
bool ipv4 = (cur->ifa_addr->sa_family == AF_INET);
|
||||||
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
||||||
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC);
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC);
|
||||||
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||||
if (ipv4 && ready && !ignored) {
|
if (ipv4 && ready && !ignored) {
|
||||||
discover_network_iface(cur, ips, ss0, ss1, false);
|
discover_network_iface(cur, ips, ss0, ss1, false, loopback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,7 +276,7 @@ void retrieve_local_ips()
|
||||||
freeifaddrs(ifap);
|
freeifaddrs(ifap);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<string>& srs_get_local_ips()
|
vector<SrsIPAddress*>& srs_get_local_ips()
|
||||||
{
|
{
|
||||||
if (_srs_system_ips.empty()) {
|
if (_srs_system_ips.empty()) {
|
||||||
retrieve_local_ips();
|
retrieve_local_ips();
|
||||||
|
@ -277,72 +293,39 @@ string srs_get_public_internet_address()
|
||||||
return _public_internet_address;
|
return _public_internet_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string>& ips = srs_get_local_ips();
|
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
||||||
|
|
||||||
// find the best match public address.
|
// find the best match public address.
|
||||||
for (int i = 0; i < (int)ips.size(); i++) {
|
for (int i = 0; i < (int)ips.size(); i++) {
|
||||||
std::string ip = ips[i];
|
SrsIPAddress* ip = ips[i];
|
||||||
// TODO: FIXME: Support ipv6.
|
if (!ip->is_internet) {
|
||||||
if (ip.find(".") == string::npos) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr_t addr = inet_addr(ip.c_str());
|
srs_warn("use public address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||||
uint32_t addr_h = ntohl(addr);
|
_public_internet_address = ip->ip;
|
||||||
// lo, 127.0.0.0-127.0.0.1
|
return ip->ip;
|
||||||
if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
|
|
||||||
srs_trace("ignore private address: %s", ip.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Class A 10.0.0.0-10.255.255.255
|
|
||||||
if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) {
|
|
||||||
srs_trace("ignore private address: %s", ip.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Class B 172.16.0.0-172.31.255.255
|
|
||||||
if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) {
|
|
||||||
srs_trace("ignore private address: %s", ip.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Class C 192.168.0.0-192.168.255.255
|
|
||||||
if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) {
|
|
||||||
srs_trace("ignore private address: %s", ip.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
srs_warn("use public address as ip: %s", ip.c_str());
|
|
||||||
|
|
||||||
_public_internet_address = ip;
|
|
||||||
return ip;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// no public address, use private address.
|
// no public address, use private address.
|
||||||
for (int i = 0; i < (int)ips.size(); i++) {
|
for (int i = 0; i < (int)ips.size(); i++) {
|
||||||
std::string ip = ips[i];
|
SrsIPAddress* ip = ips[i];
|
||||||
// TODO: FIXME: Support ipv6.
|
if (ip->is_loopback) {
|
||||||
if (ip.find(".") == string::npos) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr_t addr = inet_addr(ip.c_str());
|
srs_warn("use private address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||||
uint32_t addr_h = ntohl(addr);
|
_public_internet_address = ip->ip;
|
||||||
// lo, 127.0.0.0-127.0.0.1
|
return ip->ip;
|
||||||
if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
|
|
||||||
srs_trace("ignore private address: %s", ip.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
srs_warn("use private address as ip: %s", ip.c_str());
|
|
||||||
|
|
||||||
_public_internet_address = ip;
|
|
||||||
return ip;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, use first whatever kind of address.
|
// Finally, use first whatever kind of address.
|
||||||
if (!ips.empty() && _public_internet_address.empty()) {
|
if (!ips.empty() && _public_internet_address.empty()) {
|
||||||
string ip = ips.at(0);
|
SrsIPAddress* ip = ips[0];
|
||||||
srs_warn("use first address as ip: %s", ip.c_str());
|
|
||||||
|
srs_warn("use first address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||||
_public_internet_address = ip;
|
_public_internet_address = ip->ip;
|
||||||
return ip;
|
return ip->ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
|
|
|
@ -51,7 +51,20 @@ extern bool srs_string_is_rtmp(std::string url);
|
||||||
extern bool srs_is_digit_number(std::string str);
|
extern bool srs_is_digit_number(std::string str);
|
||||||
|
|
||||||
// Get local ip, fill to @param ips
|
// Get local ip, fill to @param ips
|
||||||
extern std::vector<std::string>& srs_get_local_ips();
|
struct SrsIPAddress
|
||||||
|
{
|
||||||
|
// The network interface name, such as eth0, en0, eth1.
|
||||||
|
std::string ifname;
|
||||||
|
// The IP v4 or v6 address.
|
||||||
|
std::string ip;
|
||||||
|
// Whether the ip is IPv4 address.
|
||||||
|
bool is_ipv4;
|
||||||
|
// Whether the ip is internet public IP address.
|
||||||
|
bool is_internet;
|
||||||
|
// Whether the ip is loopback, such as 127.0.0.1
|
||||||
|
bool is_loopback;
|
||||||
|
};
|
||||||
|
extern std::vector<SrsIPAddress*>& srs_get_local_ips();
|
||||||
|
|
||||||
// Get local public ip, empty string if no public internet address found.
|
// Get local public ip, empty string if no public internet address found.
|
||||||
extern std::string srs_get_public_internet_address();
|
extern std::string srs_get_public_internet_address();
|
||||||
|
|
Loading…
Reference in a new issue