diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-09-10 20:36:05 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-09-10 20:36:12 +0200 |
commit | dc088ee4158029c41e69bd7fab295498accecdfc (patch) | |
tree | 2be56280b284689eb2be764d31c3538500224cea | |
parent | 3f60536f66ee50f07162d6ec186c70d76566f4f6 (diff) |
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/interface_reader.cpp | 18 | ||||
-rw-r--r-- | src/interface_reader.hpp | 4 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/interface_reader.cpp b/src/interface_reader.cpp index 9b0bf14..cb80053 100644 --- a/src/interface_reader.cpp +++ b/src/interface_reader.cpp @@ -45,6 +45,12 @@ interface_reader::read_interfaces(void) std::string interface_name = ifa->ifa_name; + // If we do not have any interface names to filter for, we use them all. + // If we have any, we filter + if (this->do_filter() && !this->interface_whitelisted(interface_name)) { + continue; + } + std::optional<std::string> address; int family = ifa->ifa_addr->sa_family; if (family == AF_INET || family == AF_INET6) { @@ -76,3 +82,15 @@ interface_reader::get_interfaces(void) const return this->interfaces; } +bool +interface_reader::do_filter(void) const +{ + return !this->interface_names.empty(); +} + +bool +interface_reader::interface_whitelisted(std::string const& if_name) const +{ + return std::find(this->interface_names.begin(), this->interface_names.end(), if_name) != this->interface_names.end(); +} + diff --git a/src/interface_reader.hpp b/src/interface_reader.hpp index 7d85416..f38a12f 100644 --- a/src/interface_reader.hpp +++ b/src/interface_reader.hpp @@ -18,6 +18,10 @@ class interface_reader { std::vector<std::string> interface_names; std::vector<interface> interfaces; + + bool do_filter(void) const; + bool interface_whitelisted(std::string const& if_name) const; + }; #endif // GETIF_INTERFACE_READER_H |