lordgamez commented on code in PR #1599:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1599#discussion_r1301503268
##########
libminifi/include/utils/net/AsioSocketUtils.h:
##########
@@ -89,28 +94,71 @@ class AsioSocketConnection : public io::BaseStream {
}
private:
+#ifndef WIN32
template<typename SocketType>
- bool bindToLocalInterface(SocketType& socket) {
+ void bindToLocalInterfaceIfSpecified(SocketType& socket) {
if (local_network_interface_.empty()) {
- return true;
+ return;
+ }
+
+ using ifaddrs_uniq_ptr = std::unique_ptr<ifaddrs, utils::ifaddrs_deleter>;
+ const auto if_list_ptr = []() -> ifaddrs_uniq_ptr {
+ ifaddrs *list = nullptr;
+ [[maybe_unused]] const auto get_ifa_success = getifaddrs(&list) == 0;
+ assert(get_ifa_success || !list);
+ return ifaddrs_uniq_ptr{ list };
+ }();
+ if (!if_list_ptr) {
+ return;
+ }
+
+ const auto advance_func = [](const ifaddrs *const p) { return p->ifa_next;
};
+ const auto predicate = [this](const ifaddrs *const item) {
+ return item->ifa_addr && item->ifa_name && (item->ifa_addr->sa_family ==
AF_INET || item->ifa_addr->sa_family == AF_INET6)
+ && item->ifa_name == local_network_interface_;
+ };
+ auto item_found = [&]() -> ifaddrs* {
+ for (auto it = if_list_ptr.get(); it; it = advance_func(it)) {
+ if (predicate(it)) { return it; }
+ }
+ return nullptr;
+ }();
+
+ if (item_found == nullptr) {
+ logger_->log_error("Could not find specified network interface: '%s'",
local_network_interface_);
+ return;
+ }
+
+ std::string address;
+ if (item_found->ifa_addr->sa_family == AF_INET) {
+ struct sockaddr_in *ipv4_addr = (struct sockaddr_in
*)item_found->ifa_addr;
+ char ip_str[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &(ipv4_addr->sin_addr), ip_str, INET_ADDRSTRLEN);
+ address = ip_str;
+ } else if (item_found->ifa_addr->sa_family == AF_INET6) {
+ struct sockaddr_in6 *ipv6_addr = (struct sockaddr_in6
*)item_found->ifa_addr;
+ char ip_str[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &(ipv6_addr->sin6_addr), ip_str, INET6_ADDRSTRLEN);
+ address = ip_str;
+ } else {
+ logger_->log_error("Invalid IP address family found");
+ return;
Review Comment:
Good point, updated in fca226d812d928c168c05ede1a113fb87e77da25
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]