120 lines
2.1 KiB
C++
120 lines
2.1 KiB
C++
#include <dirent.h>
|
|
#include <net/if.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static int selector(const dirent* dirent)
|
|
{
|
|
if (strcmp(dirent->d_name, "lo") == 0)
|
|
return 1;
|
|
if (strncmp(dirent->d_name, "eth", 3) == 0)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
static int comparator(const dirent** d1, const dirent** d2)
|
|
{
|
|
if (strcmp((*d1)->d_name, "lo"))
|
|
return -1;
|
|
if (strcmp((*d2)->d_name, "lo"))
|
|
return +1;
|
|
return alphasort(d1, d2);
|
|
}
|
|
|
|
char* if_indextoname(unsigned ifindex, char* ifname)
|
|
{
|
|
if (ifindex == 0)
|
|
return nullptr;
|
|
|
|
dirent** namelist;
|
|
|
|
const int count = scandir("/dev", &namelist, selector, comparator);
|
|
if (count == -1)
|
|
return nullptr;
|
|
|
|
char* result = nullptr;
|
|
if (ifindex - 1 < static_cast<unsigned>(count))
|
|
{
|
|
strcpy(ifname, namelist[ifindex - 1]->d_name);
|
|
result = ifname;
|
|
}
|
|
|
|
for (int i = 0; i < count; i++)
|
|
free(namelist[i]);
|
|
free(namelist);
|
|
|
|
return result;
|
|
}
|
|
|
|
unsigned if_nametoindex(const char* ifname)
|
|
{
|
|
dirent** namelist;
|
|
|
|
const int count = scandir("/dev", &namelist, selector, comparator);
|
|
if (count == -1)
|
|
return 0;
|
|
|
|
unsigned result = 0;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
if (strcmp(ifname, namelist[i]->d_name) != 0)
|
|
continue;
|
|
result = i + 1;
|
|
break;
|
|
}
|
|
|
|
for (int i = 0; i < count; i++)
|
|
free(namelist[i]);
|
|
free(namelist);
|
|
|
|
return result;
|
|
}
|
|
|
|
struct if_nameindex* if_nameindex(void)
|
|
{
|
|
dirent** namelist;
|
|
|
|
const int count = scandir("/dev", &namelist, selector, comparator);
|
|
if (count == -1)
|
|
return nullptr;
|
|
|
|
auto* result = static_cast<struct if_nameindex*>(malloc(sizeof(struct if_nameindex) * (count + 1)));
|
|
if (result == nullptr)
|
|
goto cleanup;
|
|
|
|
for (unsigned i = 0; i < static_cast<unsigned>(count); i++)
|
|
{
|
|
auto* if_name = strdup(namelist[i]->d_name);
|
|
if (if_name == nullptr)
|
|
{
|
|
if_freenameindex(result);
|
|
result = nullptr;
|
|
goto cleanup;
|
|
}
|
|
|
|
result[i] = {
|
|
.if_index = i,
|
|
.if_name = if_name,
|
|
};
|
|
}
|
|
|
|
result[count] = {
|
|
.if_index = 0,
|
|
.if_name = nullptr,
|
|
};
|
|
|
|
cleanup:
|
|
for (int i = 0; i < count; i++)
|
|
free(namelist[i]);
|
|
free(namelist);
|
|
|
|
return result;
|
|
}
|
|
|
|
void if_freenameindex(struct if_nameindex* ptr)
|
|
{
|
|
for (auto* entry = ptr; entry->if_name; entry++)
|
|
free(entry->if_name);
|
|
free(ptr);
|
|
}
|