winlin

ignore any intranet bandwidth.

@@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #endif 35 #endif
36 #include <stdlib.h> 36 #include <stdlib.h>
37 #include <sys/time.h> 37 #include <sys/time.h>
  38 +#include <map>
38 using namespace std; 39 using namespace std;
39 40
40 #include <srs_kernel_log.hpp> 41 #include <srs_kernel_log.hpp>
@@ -1018,6 +1019,46 @@ void srs_update_network_devices() @@ -1018,6 +1019,46 @@ void srs_update_network_devices()
1018 #endif 1019 #endif
1019 } 1020 }
1020 1021
  1022 +// we detect all network device as internet or intranet device, by its ip address.
  1023 +// key is device name, for instance, eth0
  1024 +// value is whether internet, for instance, true.
  1025 +static std::map<std::string, bool> _srs_device_ifs;
  1026 +
  1027 +bool srs_net_device_is_internet(string ifname)
  1028 +{
  1029 + if (_srs_device_ifs.find(ifname) == _srs_device_ifs.end()) {
  1030 + return false;
  1031 + }
  1032 + return _srs_device_ifs[ifname];
  1033 +}
  1034 +
  1035 +bool srs_net_device_is_internet(in_addr_t addr)
  1036 +{
  1037 + u_int32_t addr_h = ntohl(addr);
  1038 +
  1039 + // lo, 127.0.0.0-127.0.0.1
  1040 + if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
  1041 + return false;
  1042 + }
  1043 +
  1044 + // Class A 10.0.0.0-10.255.255.255
  1045 + if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) {
  1046 + return false;
  1047 + }
  1048 +
  1049 + // Class B 172.16.0.0-172.31.255.255
  1050 + if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) {
  1051 + return false;
  1052 + }
  1053 +
  1054 + // Class C 192.168.0.0-192.168.255.255
  1055 + if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) {
  1056 + return false;
  1057 + }
  1058 +
  1059 + return true;
  1060 +}
  1061 +
1021 SrsNetworkRtmpServer::SrsNetworkRtmpServer() 1062 SrsNetworkRtmpServer::SrsNetworkRtmpServer()
1022 { 1063 {
1023 ok = false; 1064 ok = false;
@@ -1186,7 +1227,9 @@ void retrieve_local_ipv4_ips() @@ -1186,7 +1227,9 @@ void retrieve_local_ipv4_ips()
1186 1227
1187 ifaddrs* p = ifap; 1228 ifaddrs* p = ifap;
1188 while (p != NULL) { 1229 while (p != NULL) {
1189 - sockaddr* addr = p->ifa_addr; 1230 + ifaddrs* cur = p;
  1231 + sockaddr* addr = cur->ifa_addr;
  1232 + p = p->ifa_next;
1190 1233
1191 // retrieve ipv4 addr 1234 // retrieve ipv4 addr
1192 // ignore the tun0 network device, 1235 // ignore the tun0 network device,
@@ -1208,9 +1251,16 @@ void retrieve_local_ipv4_ips() @@ -1208,9 +1251,16 @@ void retrieve_local_ipv4_ips()
1208 srs_trace("retrieve local ipv4 ip=%s, index=%d", ip.c_str(), (int)ips.size()); 1251 srs_trace("retrieve local ipv4 ip=%s, index=%d", ip.c_str(), (int)ips.size());
1209 ips.push_back(ip); 1252 ips.push_back(ip);
1210 } 1253 }
1211 - }  
1212 1254
1213 - p = p->ifa_next; 1255 + // set the device internet status.
  1256 + if (!srs_net_device_is_internet(inaddr->s_addr)) {
  1257 + srs_trace("detect intranet address: %s", ip.c_str());
  1258 + _srs_device_ifs[cur->ifa_name] = false;
  1259 + } else {
  1260 + srs_trace("detect internet address: %s", ip.c_str());
  1261 + _srs_device_ifs[cur->ifa_name] = true;
  1262 + }
  1263 + }
1214 } 1264 }
1215 1265
1216 freeifaddrs(ifap); 1266 freeifaddrs(ifap);
@@ -1326,7 +1376,7 @@ void srs_api_dump_summaries(std::stringstream& ss) @@ -1326,7 +1376,7 @@ void srs_api_dump_summaries(std::stringstream& ss)
1326 1376
1327 // ignore the lo interface. 1377 // ignore the lo interface.
1328 std::string inter = o.name; 1378 std::string inter = o.name;
1329 - if (!o.ok || inter == "lo") { 1379 + if (!o.ok || inter == "lo" || !srs_net_device_is_internet(inter)) {
1330 continue; 1380 continue;
1331 } 1381 }
1332 1382
@@ -609,6 +609,9 @@ extern SrsNetworkDevices* srs_get_network_devices(); @@ -609,6 +609,9 @@ extern SrsNetworkDevices* srs_get_network_devices();
609 extern int srs_get_network_devices_count(); 609 extern int srs_get_network_devices_count();
610 // the deamon st-thread will update it. 610 // the deamon st-thread will update it.
611 extern void srs_update_network_devices(); 611 extern void srs_update_network_devices();
  612 +// detect whether specified device is internet public address.
  613 +extern bool srs_net_device_is_internet(std::string ifname);
  614 +extern bool srs_net_device_is_internet(in_addr_t addr);
612 615
613 // system connections, and srs rtmp network summary 616 // system connections, and srs rtmp network summary
614 class SrsNetworkRtmpServer 617 class SrsNetworkRtmpServer