fgerlits commented on code in PR #1412:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1412#discussion_r980225624


##########
extensions/standard-processors/tests/unit/ListenSyslogTests.cpp:
##########
@@ -197,21 +197,10 @@ constexpr std::string_view rfc5424_logger_example_1 = 
R"(<13>1 2022-03-17T10:10:
 
 constexpr std::string_view invalid_syslog = "not syslog";
 
-void sendUDPPacket(const std::string_view content, uint64_t port) {
-  asio::io_context io_context;
-  asio::ip::udp::socket socket(io_context);
-  asio::ip::udp::endpoint 
remote_endpoint(asio::ip::address::from_string("127.0.0.1"), port);
-  socket.open(asio::ip::udp::v4());
-  std::error_code err;
-  socket.send_to(asio::buffer(content, content.size()), remote_endpoint, 0, 
err);
-  REQUIRE(!err);
-  socket.close();
-}
-
 void check_for_only_basic_attributes(core::FlowFile& flow_file, uint16_t port, 
std::string_view protocol) {
   CHECK(std::to_string(port) == flow_file.getAttribute("syslog.port"));
   CHECK(protocol == flow_file.getAttribute("syslog.protocol"));
-  CHECK("127.0.0.1" == flow_file.getAttribute("syslog.sender"));
+  CHECK(("::ffff:127.0.0.1" == flow_file.getAttribute("syslog.sender") || 
"::1" == flow_file.getAttribute("syslog.sender")));

Review Comment:
   this seems to be only IPv6 now; how does this work in the IPv4 case?



##########
extensions/standard-processors/processors/PutUDP.cpp:
##########
@@ -107,51 +98,53 @@ void PutUDP::onTrigger(core::ProcessContext* context, 
core::ProcessSession* cons
     return;
   }
 
-  const auto nonthrowing_sockaddr_ntop = [](const sockaddr* const sa) -> 
std::string {
-    return utils::try_expression([sa] { return utils::net::sockaddr_ntop(sa); 
}).value_or("(n/a)");
+  asio::io_context io_context;
+
+  const auto resolve_hostname = [&io_context, &hostname, &port]() -> 
nonstd::expected<udp::resolver::results_type, std::error_code> {
+    udp::resolver resolver(io_context);
+    std::error_code error_code;
+    auto resolved_query = resolver.resolve(hostname, port, error_code);
+    if (error_code)
+      return nonstd::make_unexpected(error_code);
+    return resolved_query;
   };
 
-  const auto debug_log_resolved_names = [&, this](const addrinfo& names) -> 
decltype(auto) {
-    if (logger_->should_log(core::logging::LOG_LEVEL::debug)) {
-      std::vector<std::string> names_vector;
-      for (const addrinfo* it = &names; it; it = it->ai_next) {
-        names_vector.push_back(nonthrowing_sockaddr_ntop(it->ai_addr));
+  const auto send_data_to_endpoint = [&io_context, &data, &logger = 
this->logger_](const udp::resolver::results_type& resolved_query) -> 
nonstd::expected<void, std::error_code> {
+    std::error_code error;
+    for (const auto& resolver_entry : resolved_query) {
+      error.clear();
+      udp::socket socket(io_context);
+      socket.open(resolver_entry.endpoint().protocol(), error);
+      if (error) {
+        logger->log_debug("opening %s socket failed due to %s ", 
resolver_entry.endpoint().protocol() == udp::v4() ? "IPv4" : "IPv6", 
error.message());
+        continue;
       }
-      logger_->log_debug("resolved \'%s\' to: %s",
-          hostname,
-          names_vector | ranges::views::join(',') | ranges::to<std::string>());
+      socket.send_to(asio::buffer(data.buffer), resolver_entry.endpoint(), 
udp::socket::message_flags{}, error);
+      if (error) {
+        core::logging::LOG_DEBUG(logger) << "sending to endpoint " << 
resolver_entry.endpoint() << " failed due to " << error.message();

Review Comment:
   Are these errors expected and harmless?  If not, we should log it on error 
(or warning) level; also at line 119.
   
   Also, I think it would be useful to add a debug-level log in the successful 
case, so we can see that "sending to X failed", "sending to Y failed", "sending 
to Z succeeded".



##########
libminifi/test/Utils.h:
##########
@@ -111,24 +111,42 @@ bool countLogOccurrencesUntil(const std::string& pattern,
   return false;
 }
 
-bool sendMessagesViaTCP(const std::vector<std::string_view>& contents, 
uint64_t port) {
+bool sendMessagesViaTCP(const std::vector<std::string_view>& contents, const 
asio::ip::tcp::endpoint& remote_endpoint) {
   asio::io_context io_context;
   asio::ip::tcp::socket socket(io_context);
-  asio::ip::tcp::endpoint 
remote_endpoint(asio::ip::address::from_string("127.0.0.1"), port);
   socket.connect(remote_endpoint);
   std::error_code err;
   for (auto& content : contents) {
     std::string tcp_message(content);
     tcp_message += '\n';
     asio::write(socket, asio::buffer(tcp_message, tcp_message.size()), err);
   }
-  if (err) {
+  if (err)
+    return false;
+  socket.close();
+  return true;

Review Comment:
   I can see this was like this before, but why don't we (try to) close the 
socket in the error case?  Maybe the `connect()` and the first `write()` were 
successful, only (say) the second `write()` failed.



##########
extensions/standard-processors/tests/unit/ListenSyslogTests.cpp:
##########
@@ -269,19 +258,37 @@ TEST_CASE("ListenSyslog without parsing test", 
"[ListenSyslog]") {
   std::string protocol;
 
   SECTION("UDP") {
+    asio::ip::udp::endpoint endpoint;
+    SECTION("sending through IPv4", "[IPv4]") {
+      endpoint = asio::ip::udp::endpoint(asio::ip::address_v4::loopback(), 
SYSLOG_PORT);
+    }
+    SECTION("sending through IPv6", "[IPv6]") {
+      if (utils::isIPv6Disabled())
+        return;

Review Comment:
   this will exit the test case, so the "TCP" section won't be run



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