STDMETHODIMP HostWrap::COMGETTER(NetworkInterfaces)(ComSafeArrayOut(IHostNetworkInterface *, aNetworkInterfaces)) { LogRelFlow(("{%p} %s: enter aNetworkInterfaces=%p\n", this, "Host::getNetworkInterfaces", aNetworkInterfaces)); VirtualBoxBase::clearError(); HRESULT hrc; try { CheckComArgOutPointerValidThrow(aNetworkInterfaces); AutoCaller autoCaller(this); if (FAILED(autoCaller.rc())) throw autoCaller.rc(); hrc = getNetworkInterfaces(ArrayComTypeOutConverter<IHostNetworkInterface>(ComSafeArrayOutArg(aNetworkInterfaces)).array()); } catch (HRESULT hrc2) { hrc = hrc2; } catch (...) { hrc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS); } LogRelFlow(("{%p} %s: leave *aNetworkInterfaces=%zu hrc=%Rhrc\n", this, "Host::getNetworkInterfaces", ComSafeArraySize(*aNetworkInterfaces), hrc)); return hrc; }
int main(void) { for (const auto& dev: getNetworkInterfaces()) { printf("%s: %s broadcast:%s flags:%s\n", dev.name.c_str(), dev.ipaddr_str().c_str(), dev.brdaddr_str().c_str(), dev.flags_str().c_str() ); } return 0; }
shared::CDataContainer CSystem::getBinding(const std::vector<std::string> & parameters, const std::string & requestContent) { if (parameters.size() > 2) { std::string query = parameters[2]; if (boost::iequals(query, "SerialPorts")) return getSerialPorts(); if (boost::iequals(query, "NetworkInterfaces")) return getNetworkInterfaces(true); if (boost::iequals(query, "NetworkInterfacesWithoutLoopback")) return getNetworkInterfaces(false); if (boost::iequals(query, "platformIsWindows")) return platformIs("windows"); if (boost::iequals(query, "platformIsLinux")) return platformIs("linux"); if (boost::iequals(query, "platformIsMac")) return platformIs("mac"); return CResult::GenerateError("unsupported binding query : " + query); } return CResult::GenerateError("Cannot retreive url parameters"); }
int32_t getNetworkInterfacesCount() { struct ifaddrs *interfaces,*ints; int32_t interface_count=0; interfaces = getNetworkInterfaces(); if(interfaces == NULL) return _ERR; ints = interfaces; for(; ints != NULL;) { interface_count++; ints = ints->ifa_next; } freeNetworkInterfaces(interfaces); return interface_count; }
uint8_t *getIPAddress(uint32_t interface) { struct ifaddrs *interfaces, *ints; uint8_t *address = NULL; uint32_t i=0; interfaces = getNetworkInterfaces(); ints = interfaces; if(interfaces == NULL) return NULL; for(; i < interface; i++) { if(ints == NULL) break; ints = ints->ifa_next; } if(ints != NULL) { switch(ints->ifa_addr->sa_family) { case AF_INET: address = (uint8_t *) malloc(INET_ADDRSTRLEN); inet_ntop(AF_INET, &(((struct sockaddr_in *)ints->ifa_addr)->sin_addr), (char *)address, INET_ADDRSTRLEN); break; case AF_INET6: address = (uint8_t *) malloc(INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)ints->ifa_addr)->sin6_addr), (char *)address, INET6_ADDRSTRLEN); break; default: address = NULL; break; } } freeNetworkInterfaces(interfaces); return address; }
uint8_t *getNetworkInterfaceName(uint32_t interface) { struct ifaddrs *interfaces, *ints; uint8_t *name = NULL; uint32_t i=0; interfaces = getNetworkInterfaces(); ints = interfaces; if(interfaces == NULL) return NULL; for(; i < interface; i++) { if(ints == NULL) break; ints = ints->ifa_next; } if(ints != NULL) { name = sys_strdup((uint8_t *)ints->ifa_name); } freeNetworkInterfaces(interfaces); return name; }