martinzink commented on a change in pull request #1116:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1116#discussion_r664302952



##########
File path: extensions/standard-processors/processors/AppendHostInfo.cpp
##########
@@ -23,90 +23,88 @@
 #define __USE_POSIX
 #endif /* __USE_POSIX */
 
-#include <limits.h>
-#include <string.h>
 #include <memory>
 #include <string>
-#include <set>
+#include <regex>
+#include <algorithm>
 #include "core/ProcessContext.h"
 #include "core/Property.h"
 #include "core/ProcessSession.h"
 #include "core/FlowFile.h"
 #include "io/ClientSocket.h"
+#include "utils/NetworkInterfaceInfo.h"
+
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace processors {
 
-#ifndef WIN32
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
-#include <arpa/inet.h>
-#endif
-
-#ifndef HOST_NAME_MAX
-#define HOST_NAME_MAX 255
-#endif
+core::Property AppendHostInfo::InterfaceNameFilter("Network Interface Filter", 
"A regular expression to filter ip addresses based on the name of the network 
interface", "");
+core::Property AppendHostInfo::HostAttribute("Hostname Attribute", "Flowfile 
attribute used to record the agent's hostname", "source.hostname");
+core::Property AppendHostInfo::IPAttribute("IP Attribute", "Flowfile attribute 
used to record the agent's IP addresses in a comma separated list", 
"source.ipv4");
+core::Property 
AppendHostInfo::RefreshPolicy(core::PropertyBuilder::createProperty("Refresh 
Policy")
+    ->withDescription("When to recalculate the host info")
+    ->withAllowableValues<std::string>({ REFRESH_POLICY_ON_SCHEDULE, 
REFRESH_POLICY_ON_TRIGGER })
+    ->withDefaultValue(REFRESH_POLICY_ON_SCHEDULE)->build());
 
-core::Property AppendHostInfo::InterfaceName("Network Interface Name", 
"Network interface from which to read an IP v4 address", "eth0");
-core::Property AppendHostInfo::HostAttribute("Hostname Attribute", "Flowfile 
attribute to used to record the agent's hostname", "source.hostname");
-core::Property AppendHostInfo::IPAttribute("IP Attribute", "Flowfile attribute 
to used to record the agent's IP address", "source.ipv4");
 core::Relationship AppendHostInfo::Success("success", "success operational on 
the flow record");
 
 void AppendHostInfo::initialize() {
-  // Set the supported properties
-  std::set<core::Property> properties;
-  properties.insert(InterfaceName);
-  properties.insert(HostAttribute);
-  properties.insert(IPAttribute);
-  setSupportedProperties(properties);
+  setSupportedProperties({InterfaceNameFilter, HostAttribute, IPAttribute, 
RefreshPolicy});
+  setSupportedRelationships({Success});
+}
 
-  // Set the supported relationships
-  std::set<core::Relationship> relationships;
-  relationships.insert(Success);
-  setSupportedRelationships(relationships);
+void AppendHostInfo::onSchedule(const std::shared_ptr<core::ProcessContext>& 
context, const std::shared_ptr<core::ProcessSessionFactory>&) {
+  context->getProperty(HostAttribute.getName(), hostname_attribute_name_);
+  context->getProperty(IPAttribute.getName(), ipaddress_attribute_name_);
+  context->getProperty(InterfaceNameFilter.getName(), interface_name_filter_);
+  std::string refresh_policy;
+  context->getProperty(RefreshPolicy.getName(), refresh_policy);
+  if (refresh_policy == REFRESH_POLICY_ON_TRIGGER)
+    refresh_on_trigger_ = true;
+
+  if (!refresh_on_trigger_)
+    refreshHostInfo();
 }
 
-void AppendHostInfo::onTrigger(core::ProcessContext *context, 
core::ProcessSession *session) {
+void AppendHostInfo::onTrigger(core::ProcessContext*, core::ProcessSession* 
session) {
   std::shared_ptr<core::FlowFile> flow = session->get();
   if (!flow)
     return;
 
-  // Get Hostname
-
-  std::string hostAttribute = "";
-  context->getProperty(HostAttribute.getName(), hostAttribute);
-  flow->addAttribute(hostAttribute.c_str(), 
org::apache::nifi::minifi::io::Socket::getMyHostName());
+  if (refresh_on_trigger_)
+    refreshHostInfo();
 
-  // Get IP address for the specified interface
-  std::string iface;
-  context->getProperty(InterfaceName.getName(), iface);
-  // Confirm the specified interface name exists on this device
-#ifndef WIN32
-  if (if_nametoindex(iface.c_str()) != 0) {
-    struct ifreq ifr;
-    int fd = socket(AF_INET, SOCK_DGRAM, 0);
-    // Type of address to retrieve - IPv4 IP address
-    ifr.ifr_addr.sa_family = AF_INET;
-    // Copy the interface name in the ifreq structure
-    strncpy(ifr.ifr_name, iface.c_str(), IFNAMSIZ - 1);
-    ioctl(fd, SIOCGIFADDR, &ifr);
-    close(fd);
-
-    std::string ipAttribute;
-    context->getProperty(IPAttribute.getName(), ipAttribute);
-    flow->addAttribute(ipAttribute.c_str(), inet_ntoa(((struct sockaddr_in *) 
&ifr.ifr_addr)->sin_addr));
+  flow->addAttribute(hostname_attribute_name_, hostname_);
+  if (ipaddresses_.has_value()) {
+    flow->addAttribute(ipaddress_attribute_name_, ipaddresses_.value());
   }
-#endif
 
-  // Transfer to the relationship
   session->transfer(flow, Success);
 }
 
+void AppendHostInfo::refreshHostInfo() {
+  hostname_ = org::apache::nifi::minifi::io::Socket::getMyHostName();
+  auto filter = [this](const utils::NetworkInterfaceInfo& interface_info) -> 
bool {
+    bool has_ipv4_address = interface_info.hasIpV4Address();
+    bool matches_regex_or_empty_regex = interface_name_filter_.empty() || 
std::regex_match(interface_info.getName(), std::regex(interface_name_filter_));

Review comment:
       changed the interface_name_filter_ to be an optional regex instead of 
optional string in 
https://github.com/apache/nifi-minifi-cpp/pull/1116/commits/d84894db14625f14cdef9fdf92754bed8ea4ac49
 so I we only compile it once




-- 
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]


Reply via email to