From 8cd68a1eb3fe7136651a7408d4e5a1492e98c60d Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 19 Jun 2015 11:59:41 +0800 Subject: [PATCH] ignore any intranet bandwidth. --- trunk/src/app/srs_app_utility.cpp | 58 ++++++++++++++++++++++++++++--- trunk/src/app/srs_app_utility.hpp | 3 ++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index daddc7334..1737b9536 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #endif #include #include +#include using namespace std; #include @@ -1018,6 +1019,46 @@ void srs_update_network_devices() #endif } +// we detect all network device as internet or intranet device, by its ip address. +// key is device name, for instance, eth0 +// value is whether internet, for instance, true. +static std::map _srs_device_ifs; + +bool srs_net_device_is_internet(string ifname) +{ + if (_srs_device_ifs.find(ifname) == _srs_device_ifs.end()) { + return false; + } + return _srs_device_ifs[ifname]; +} + +bool srs_net_device_is_internet(in_addr_t addr) +{ + u_int32_t addr_h = ntohl(addr); + + // lo, 127.0.0.0-127.0.0.1 + if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) { + return false; + } + + // Class A 10.0.0.0-10.255.255.255 + if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) { + return false; + } + + // Class B 172.16.0.0-172.31.255.255 + if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) { + return false; + } + + // Class C 192.168.0.0-192.168.255.255 + if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) { + return false; + } + + return true; +} + SrsNetworkRtmpServer::SrsNetworkRtmpServer() { ok = false; @@ -1186,7 +1227,9 @@ void retrieve_local_ipv4_ips() ifaddrs* p = ifap; while (p != NULL) { - sockaddr* addr = p->ifa_addr; + ifaddrs* cur = p; + sockaddr* addr = cur->ifa_addr; + p = p->ifa_next; // retrieve ipv4 addr // ignore the tun0 network device, @@ -1208,9 +1251,16 @@ void retrieve_local_ipv4_ips() srs_trace("retrieve local ipv4 ip=%s, index=%d", ip.c_str(), (int)ips.size()); ips.push_back(ip); } + + // set the device internet status. + if (!srs_net_device_is_internet(inaddr->s_addr)) { + srs_trace("detect intranet address: %s", ip.c_str()); + _srs_device_ifs[cur->ifa_name] = false; + } else { + srs_trace("detect internet address: %s", ip.c_str()); + _srs_device_ifs[cur->ifa_name] = true; + } } - - p = p->ifa_next; } freeifaddrs(ifap); @@ -1326,7 +1376,7 @@ void srs_api_dump_summaries(std::stringstream& ss) // ignore the lo interface. std::string inter = o.name; - if (!o.ok || inter == "lo") { + if (!o.ok || inter == "lo" || !srs_net_device_is_internet(inter)) { continue; } diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index cd95b9781..dedacb23c 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -609,6 +609,9 @@ extern SrsNetworkDevices* srs_get_network_devices(); extern int srs_get_network_devices_count(); // the deamon st-thread will update it. extern void srs_update_network_devices(); +// detect whether specified device is internet public address. +extern bool srs_net_device_is_internet(std::string ifname); +extern bool srs_net_device_is_internet(in_addr_t addr); // system connections, and srs rtmp network summary class SrsNetworkRtmpServer