Skip to content

Commit aa71a40

Browse files
Introduce netdev_check_if_exists to check if interface exists
Signed-off-by: Sudip Mukherjee <[email protected]>
1 parent e398a90 commit aa71a40

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

linux/netdev.c

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <netinet/in.h>
1616
#include <unistd.h>
1717
#include <err.h>
18+
#include <ifaddrs.h>
1819

1920
#include "netdev.h"
2021
#include "platform.h"
@@ -146,3 +147,22 @@ bool netdev_set_up_promisc(const char *const ifname, bool up, bool promisc)
146147
close(fd);
147148
return true;
148149
}
150+
151+
bool netdev_check_if_exists(const char *const ifname)
152+
{
153+
struct ifaddrs *ifaddr, *ifa;
154+
bool found = false;
155+
156+
if (getifaddrs(&ifaddr) == -1) {
157+
LOG_WARN("can not check interface names, continuing");
158+
return true;
159+
}
160+
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
161+
if (strcmp(ifa->ifa_name, ifname) == 0) {
162+
found = true;
163+
break;
164+
}
165+
}
166+
freeifaddrs(ifaddr);
167+
return found;
168+
}

linux/netdev.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ bool netdev_set_ip_address(const char* ifname, uint32_t ip);
3636

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

39+
bool netdev_check_if_exists(const char *const ifname);
40+
3941
#ifdef __cplusplus
4042
}
4143
#endif

0 commit comments

Comments
 (0)