You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by fr...@apache.org on 2026/09/25 19:32:18 UTC

(logging-log4net) 01/01: report who accepted the connection in ListenAddressBindsOnlyThatAddress #328

This is an automated email from the ASF dual-hosted git repository.

FreeAndNil pushed a commit to branch Feature/328-telnet-listen-address-diagnostics
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git

commit a849af7f3a7c04a72f9a5c57e11b5f493f788063
Author: Jan Friedrich <fr...@apache.org>
AuthorDate: Fri Sep 25 21:32:02 2026 +0200

    report who accepted the connection in ListenAddressBindsOnlyThatAddress #328
---
 src/log4net.Tests/Appender/TelnetAppenderTest.cs | 42 +++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/src/log4net.Tests/Appender/TelnetAppenderTest.cs b/src/log4net.Tests/Appender/TelnetAppenderTest.cs
index bd5f2572..597287d5 100644
--- a/src/log4net.Tests/Appender/TelnetAppenderTest.cs
+++ b/src/log4net.Tests/Appender/TelnetAppenderTest.cs
@@ -21,6 +21,7 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Net;
+using System.Net.NetworkInformation;
 using System.Net.Sockets;
 using System.Text;
 using System.Threading;
@@ -429,7 +430,16 @@ public void ListenAddressBindsOnlyThatAddress()
       }
 
       using Socket external = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-      Assert.That(() => external.Connect(new IPEndPoint(routable!, port)), Throws.TypeOf<SocketException>());
+      SocketException? refused = null;
+      try
+      {
+        external.Connect(new IPEndPoint(routable!, port));
+      }
+      catch (SocketException e)
+      {
+        refused = e;
+      }
+      Assert.That(refused, Is.Not.Null, () => DescribeUnexpectedConnection(external, port));
     }
     finally
     {
@@ -437,6 +447,36 @@ public void ListenAddressBindsOnlyThatAddress()
     }
   }
 
+  /// <summary>
+  /// Collects what a reviewer needs to tell who accepted a connection that should have been refused.
+  /// </summary>
+  private static string DescribeUnexpectedConnection(Socket connected, int port)
+  {
+    StringBuilder text = new();
+    text.AppendLine($"connected from {connected.LocalEndPoint} to {connected.RemoteEndPoint}");
+    text.AppendLine($"host name {Dns.GetHostName()} resolves to [{string.Join(", ", (object[])Dns.GetHostAddresses(Dns.GetHostName()))}]");
+    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
+    {
+      string[] addresses = Array.ConvertAll([.. nic.GetIPProperties().UnicastAddresses], a => a.Address.ToString());
+      text.AppendLine($"interface {nic.Name} ({nic.NetworkInterfaceType}, {nic.OperationalStatus}): {string.Join(", ", addresses)}");
+    }
+    IPEndPoint[] listeners = Array.FindAll(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(), l => l.Port == port);
+    text.AppendLine($"listeners on port {port}: [{string.Join(", ", (object[])listeners)}]");
+    // The appender greets every client, so its welcome line identifies it as the peer.
+    try
+    {
+      connected.ReceiveTimeout = 2_000;
+      byte[] buffer = new byte[256];
+      int read = connected.Receive(buffer);
+      text.AppendLine($"peer sent: {Encoding.ASCII.GetString(buffer, 0, read).Trim()}");
+    }
+    catch (SocketException e)
+    {
+      text.AppendLine($"peer sent nothing: {e.SocketErrorCode}");
+    }
+    return text.ToString();
+  }
+
   /// <summary>
   /// Asks the OS for a currently unused TCP port - a fixed port would collide with
   /// other tests or processes on the build machine.