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());
|
||||
}
|
||||
if (true) {
|
||||
vector<std::string> ips = srs_get_local_ips();
|
||||
vector<SrsIPAddress*> ips = srs_get_local_ips();
|
||||
int index = get_stats_network();
|
||||
if (index >= (int)ips.size()) {
|
||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d of %d",
|
||||
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) {
|
||||
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);
|
||||
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();
|
||||
return ips.at(index);
|
||||
SrsIPAddress* ip = ips.at(index);
|
||||
return ip->ip;
|
||||
} else {
|
||||
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());
|
||||
}
|
||||
|
||||
std::string ip = "";
|
||||
SrsIPAddress* ip = NULL;
|
||||
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()) {
|
||||
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat()
|
|||
SrsAutoFree(SrsJsonObject, obj);
|
||||
|
||||
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()) {
|
||||
SrsJsonObject* summaries = SrsJsonAny::object();
|
||||
|
|
|
@ -111,15 +111,20 @@ static std::vector<std::string> get_candidate_ips()
|
|||
std::vector<std::string> candidate_ips;
|
||||
|
||||
string candidate = _srs_config->get_rtc_server_candidates();
|
||||
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 {
|
||||
if (candidate != "*" && candidate != "0.0.0.0") {
|
||||
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;
|
||||
|
|
|
@ -81,13 +81,13 @@ SrsSimpleRtmpClient::~SrsSimpleRtmpClient()
|
|||
|
||||
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());
|
||||
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);
|
||||
|
||||
return do_connect_app(local_ip, debug_srs_upnode);
|
||||
return do_connect_app(local_ip->ip, debug_srs_upnode);
|
||||
}
|
||||
|
||||
SrsClientInfo::SrsClientInfo()
|
||||
|
|
|
@ -144,9 +144,9 @@ bool srs_net_device_is_internet(const sockaddr* addr)
|
|||
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* h = (char*)saddr;
|
||||
|
@ -160,10 +160,17 @@ void discover_network_iface(ifaddrs* cur, vector<string>& ips, stringstream& ss0
|
|||
std::string ip(saddr, strlen(saddr));
|
||||
ss0 << ", iface[" << (int)ips.size() << "] " << cur->ifa_name << " " << (ipv6? "ipv6":"ipv4")
|
||||
<< " 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.
|
||||
if (!srs_net_device_is_internet(cur->ifa_addr)) {
|
||||
if (!ip_address->is_internet) {
|
||||
ss1 << ", intranet ";
|
||||
_srs_device_ifs[cur->ifa_name] = false;
|
||||
} else {
|
||||
|
@ -175,10 +182,16 @@ void discover_network_iface(ifaddrs* cur, vector<string>& ips, stringstream& ss0
|
|||
|
||||
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();
|
||||
|
||||
|
||||
// Get the addresses.
|
||||
ifaddrs* ifap;
|
||||
if (getifaddrs(&ifap) == -1) {
|
||||
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);
|
||||
// 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 loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||
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 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 loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||
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 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 loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
||||
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);
|
||||
}
|
||||
|
||||
vector<string>& srs_get_local_ips()
|
||||
vector<SrsIPAddress*>& srs_get_local_ips()
|
||||
{
|
||||
if (_srs_system_ips.empty()) {
|
||||
retrieve_local_ips();
|
||||
|
@ -277,72 +293,39 @@ string srs_get_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.
|
||||
for (int i = 0; i < (int)ips.size(); i++) {
|
||||
std::string ip = ips[i];
|
||||
// TODO: FIXME: Support ipv6.
|
||||
if (ip.find(".") == string::npos) {
|
||||
SrsIPAddress* ip = ips[i];
|
||||
if (!ip->is_internet) {
|
||||
continue;
|
||||
}
|
||||
|
||||
in_addr_t addr = inet_addr(ip.c_str());
|
||||
uint32_t addr_h = ntohl(addr);
|
||||
// lo, 127.0.0.0-127.0.0.1
|
||||
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;
|
||||
|
||||
srs_warn("use public address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||
_public_internet_address = ip->ip;
|
||||
return ip->ip;
|
||||
}
|
||||
|
||||
// no public address, use private address.
|
||||
for (int i = 0; i < (int)ips.size(); i++) {
|
||||
std::string ip = ips[i];
|
||||
// TODO: FIXME: Support ipv6.
|
||||
if (ip.find(".") == string::npos) {
|
||||
SrsIPAddress* ip = ips[i];
|
||||
if (ip->is_loopback) {
|
||||
continue;
|
||||
}
|
||||
|
||||
in_addr_t addr = inet_addr(ip.c_str());
|
||||
uint32_t addr_h = ntohl(addr);
|
||||
// lo, 127.0.0.0-127.0.0.1
|
||||
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;
|
||||
|
||||
srs_warn("use private address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||
_public_internet_address = ip->ip;
|
||||
return ip->ip;
|
||||
}
|
||||
|
||||
// Finally, use first whatever kind of address.
|
||||
if (!ips.empty() && _public_internet_address.empty()) {
|
||||
string ip = ips.at(0);
|
||||
srs_warn("use first address as ip: %s", ip.c_str());
|
||||
|
||||
_public_internet_address = ip;
|
||||
return ip;
|
||||
SrsIPAddress* ip = ips[0];
|
||||
|
||||
srs_warn("use first address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
||||
_public_internet_address = ip->ip;
|
||||
return ip->ip;
|
||||
}
|
||||
|
||||
return "";
|
||||
|
|
|
@ -51,7 +51,20 @@ extern bool srs_string_is_rtmp(std::string url);
|
|||
extern bool srs_is_digit_number(std::string str);
|
||||
|
||||
// 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.
|
||||
extern std::string srs_get_public_internet_address();
|
||||
|
|
Loading…
Reference in a new issue