Skip to content

Introduce netdev_check_if_exists to check if interface exists #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions linux/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <netinet/in.h>
#include <unistd.h>
#include <err.h>
#include <ifaddrs.h>

#include "netdev.h"
#include "platform.h"
Expand Down Expand Up @@ -146,3 +147,22 @@ bool netdev_set_up_promisc(const char *const ifname, bool up, bool promisc)
close(fd);
return true;
}

bool netdev_check_if_exists(const char *const ifname)
{
struct ifaddrs *ifaddr, *ifa;
bool found = false;

if (getifaddrs(&ifaddr) == -1) {
LOG_WARN("can not check interface names, continuing");
return true;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifa->ifa_name, ifname) == 0) {
found = true;
break;
}
}
freeifaddrs(ifaddr);
return found;
}
2 changes: 2 additions & 0 deletions linux/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ bool netdev_set_ip_address(const char* ifname, uint32_t ip);

bool netdev_set_up_promisc(const char *const ifname, bool up, bool promisc);

bool netdev_check_if_exists(const char *const ifname);

#ifdef __cplusplus
}
#endif
Expand Down