staticvoid Free(if_list* list, bool names_too) { while (list) {
if_list* it = list;
list = it->next; if (names_too) free(it->data.if_name);
free(it);
}
}
};
staticvoid __if_nameindex_callback(void* context, nlmsghdr* hdr) {
if_list** list = reinterpret_cast<if_list**>(context); if (hdr->nlmsg_type == RTM_NEWLINK) {
ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
// Create a new entry and set the interface index.
if_list* new_link = new if_list(list);
new_link->data.if_index = ifi->ifi_index;
// Go through the various bits of information and find the name.
rtattr* rta = IFLA_RTA(ifi);
size_t rta_len = IFLA_PAYLOAD(hdr); while (RTA_OK(rta, rta_len)) { if (rta->rta_type == IFLA_IFNAME) {
new_link->data.if_name = strndup(reinterpret_cast<char*>(RTA_DATA(rta)), RTA_PAYLOAD(rta));
}
rta = RTA_NEXT(rta, rta_len);
}
}
}
struct if_nameindex* if_nameindex() {
if_list* list = nullptr;
// Open the netlink socket and ask for all the links;
NetlinkConnection nc; bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__if_nameindex_callback, &list); if (!okay) {
if_list::Free(list, true); return nullptr;
}
// Count the interfaces.
size_t interface_count = 0; for (if_list* it = list; it != nullptr; it = it->next) {
++interface_count;
}
// Build the array POSIX requires us to return. struct if_nameindex* result = newstruct if_nameindex[interface_count + 1]; if (result) { struct if_nameindex* out = result; for (if_list* it = list; it != nullptr; it = it->next) {
out->if_index = it->data.if_index;
out->if_name = it->data.if_name;
++out;
}
out->if_index = 0;
out->if_name = nullptr;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.