8 #ifndef INCL_net_interface_h 9 #define INCL_net_interface_h 18 #pragma comment(lib, "IPHLPAPI.lib") 62 typedef IP_ADAPTER_ADDRESSES
SysItem;
65 typedef struct ifaddrs SysItem;
75 IP_ADAPTER_UNICAST_ADDRESS* detail_unicast;
86 detail_unicast = NULL;
120 typedef Item IterKey;
121 typedef Item IterItem;
129 void iterInitMutable()
132 const IterItem* iterFirst(IterKey& key)
const {
133 const IterItem* result;
138 result = item_get(key, ptr_);
140 if (family_ != AF_UNSPEC && (key.addr == NULL || key.addr->sa_family != family_))
141 result = iterNext(key);
148 const IterItem* iterNext(IterKey& key)
const {
150 if (key.detail != NULL) {
151 if (item_next_address(key)) {
152 if (family_ != AF_UNSPEC && (key.addr == NULL || key.addr->sa_family != family_))
156 if (item_next(key)) {
157 item_get(key, key.detail);
158 if (family_ != AF_UNSPEC && (key.addr == NULL || key.addr->sa_family != family_))
160 if (active_ && !item_active(key.detail))
179 const ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
180 ULONG family = AF_UNSPEC;
181 if (family_ == AF_INET || family_ == AF_INET6)
184 const uint MAX_TRIES = 6;
186 ptr_ = (SysItem*)::malloc(size);
188 ULONG result = GetAdaptersAddresses(family, flags, NULL, ptr_, &size);
189 for (uint i=0; result == ERROR_BUFFER_OVERFLOW && i < MAX_TRIES; ++i) {
190 ptr_ = (SysItem*)::realloc(ptr_, size);
191 result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, ptr_, &size);
194 if (result != ERROR_SUCCESS)
205 static bool item_next_address(
Item& item) {
206 if (item.detail_unicast != NULL && (item.detail_unicast = item.detail_unicast->Next) != NULL) {
207 item.
addr = item.detail_unicast->Address.lpSockaddr;
213 static bool item_next(
Item& item) {
219 static bool item_active(
const SysItem* ptr)
220 {
return ptr->OperStatus & IfOperStatusUp; }
222 static const Item* item_get(
Item& item,
const SysItem* ptr) {
224 item.
id = ptr->AdapterName;
225 item.detail_name.set_win32(ptr->FriendlyName);
226 item.
name = item.detail_name;
228 item.
active = item_active(ptr);
229 if ((item.detail_unicast = ptr->FirstUnicastAddress) != NULL)
230 item.
addr = item.detail_unicast->Address.lpSockaddr;
238 if (::getifaddrs(&ptr_) != 0)
249 static bool item_next_address(
Item&)
252 static bool item_next(
Item& item) {
258 static bool item_active(
const SysItem* ptr)
259 {
return ptr->ifa_flags & IFF_UP; }
261 static const Item* item_get(
Item& item,
const SysItem* ptr) {
263 item.
id = ptr->ifa_name;
264 item.
name = ptr->ifa_name;
265 item.
scope_id = if_nametoindex(ptr->ifa_name);
266 item.
addr = ptr->ifa_addr;
267 item.
active = item_active(ptr);
~NetworkInterfaces()
Destructor frees interface data.
Definition: net_interface.h:115
struct sockaddr * addr
Interface address.
Definition: net_interface.h:79
Network iterface item.
Definition: net_interface.h:69
Query network adapter interfaces from OS.
Definition: net_interface.h:58
NetworkInterfaces(int family=AF_UNSPEC, bool active=true)
Constructor queries OS for interfaces.
Definition: net_interface.h:105
String container.
Definition: string.h:674
Evo implementation detail: Container iterators.
Forward iterator.
Definition: iter.h:340
uint32 scope_id
Interface scope ID for IPv6.
Definition: net_interface.h:78
SubString id
Interface ID string (terminated)
Definition: net_interface.h:70
Evo C++ Library namespace.
Definition: alg.h:11
Item()
Constructor.
Definition: net_interface.h:83
IteratorFw< NetworkInterfaces >::Const Iter
Iterator (const) - IteratorFw.
Definition: net_interface.h:125
Reference and access existing string data.
Definition: substring.h:229
const SysItem * detail
Interface system details (system specific)
Definition: net_interface.h:72
bool active
Whether interface is active.
Definition: net_interface.h:80
struct ifaddrs SysItem
System interface item.
Definition: net_interface.h:65
SubString name
Interface name (terminated)
Definition: net_interface.h:71