networking - Desktop Computer - Detecting type of internet connection -
is there way detect if computer connected via cable or mobile network internet?
i tried analyze trace-route output realized need large , up-to-date database detect type of network data packages take.
have 2 different types of network maybe different attributes detect?
you can use networklistmanager apis find if network connected internet. example below both , checks see if network free. can remove cost related code:
hresult getfreeinternetinterface(_out_ guid* pinterfaceguid) { bool fcoinitialized = false; hresult hr = coinitializeex(null, coinit_multithreaded); if (succeeded(hr)) { fcoinitialized = true; ccomptr<inetworklistmanager> pnetworklistmanager; hr = pnetworklistmanager.cocreateinstance(__uuidof(networklistmanager)); if (succeeded(hr)) { ccomptr<ienumnetworkconnections> pnetworkconnections; hr = pnetworklistmanager->getnetworkconnections(&pnetworkconnections); bool ffound = false; while (succeeded(hr)) { ccomptr<inetworkconnection> pnetworkconnection; hr = pnetworkconnections->next(1, &pnetworkconnection, nullptr); if (hr == s_ok) { ccomptr<inetworkconnectioncost> pnetworkconnectioncost; hr = pnetworkconnection.queryinterface(&pnetworkconnectioncost); if (succeeded(hr)) { nlm_connectivity nlmconnectivity = nlm_connectivity_disconnected; dword dwcost = nlm_connection_cost_unknown; if (succeeded(pnetworkconnection->getconnectivity(&nlmconnectivity)) && (nlmconnectivity & (nlm_connectivity_ipv4_internet | nlm_connectivity_ipv6_internet)) != 0 && succeeded(pnetworkconnectioncost->getcost(&dwcost)) && dwcost == nlm_connection_cost_unrestricted && succeeded(pnetworkconnection->getadapterid(pinterfaceguid))) { ffound = true; break; } } } else hr = e_nointerface; } if (succeeded(hr) && !ffound) hr = e_nointerface; } } if (fcoinitialized) couninitialize(); return hr; }
once have interface guid , ip helper apis interface type. here code had looks non-cellular networks. can take , modify need:
bool getadapteraddresses( _out_ pip_adapter_addresses * ppiaa ) { *ppiaa = null; dword len = 0; dword flags = gaa_flag_skip_anycast|gaa_flag_skip_multicast|gaa_flag_skip_dns_server; if (getadaptersaddresses(af_unspec, flags, null, null, &len) != error_buffer_overflow) return false; pip_adapter_addresses piaa = (pip_adapter_addresses)localalloc(lptr, len); if (piaa) { getadaptersaddresses(af_unspec, flags, null, piaa, &len); *ppiaa = piaa; return true; } return false; } bool isnoncellulariftype( _in_ iftype iftype ) { return // ignore loopback , wwan iftype != if_type_software_loopback && iftype != if_type_wwanpp && iftype != if_type_wwanpp2; } dword getnoncellularifindex( ) { dword ifindex = (dword)(-1); pip_adapter_addresses piaa; if (getadapteraddresses(&piaa)) { pip_adapter_addresses piaalist = piaa; while (piaa) { // non-cellular interface if (piaa->operstatus == ifoperstatusup && isnoncellulariftype(piaa->iftype)) { pip_adapter_unicast_address_lh punicastaddr = piaa->firstunicastaddress; // through unicast addresses valid ipv4 or ipv6 addresses while (punicastaddr) { lpsockaddr paddr = punicastaddr->address.lpsockaddr; if (paddr->sa_family == af_inet) { if (!in4_is_addr_linklocal(&((sockaddr_in *)paddr)->sin_addr)) { ifindex = piaa->ifindex; break; } } else if (paddr->sa_family == af_inet6) { if (!in6_is_addr_linklocal(&((sockaddr_in6 *)paddr)->sin6_addr)) { ifindex = piaa->ifindex; break; } } punicastaddr = punicastaddr->next; } // no need keep looking more? if (ifindex != (dword)(-1)) break; // while (piaa) } piaa = piaa->next; } localfree(piaalist); } return ifindex; }
Comments
Post a Comment