Log4net
  1. Log4net
  2. LOG4NET-112

Add support to the UdpAppender for IP v6 remote addresses

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.10
    • Fix Version/s: 1.2.11
    • Component/s: Appenders
    • Labels:
      None

      Description

      The UdpClient object must be configured for either IP v4 or IP v6 when it is created. Once configured it cannot be used to send to remote addresses that are from a different network family.

      Specifying the network family is not supported on NET 1.0, NETCF 1.0, SSCLI 1.0

        Issue Links

          Activity

          Hide
          Nicko Cadell added a comment -

          Details from an email on the log4net-dev list.
          From: Olivier DALET
          Subject: BUG REPORT : IP Address parsing (.NET 2.0 and log4net 1.2.10) causes UdpAppender to crash

          Hi,

          I've detected a bug in IPAddressConverter.ConvertFrom
          (log4net-1.2.10\src\Util\TypeConverters\IPAddressConverter.cs):

          If compiled with .NET framework 2.0, the IP is resolved via dns using this code:

          IPHostEntry host = Dns.GetHostEntry(str);

          then the first address in the host.AddressList array is returned.
          When executed on my computer, this array contains the following items:

          + [0] {::1} System.Net.IPAddress
          + [1]

          {192.168.0.2 }

          System.Net.IPAddress
          + [2]

          {192.168.206.1}

          System.Net.IPAddress
          + [3]

          {192.168.114.1}

          System.Net.IPAddress

          So, with the existing code, the address ::1 is selected... and the UdpAppender throws an Exception

          log4net:ERROR [UdpAppender] Unable to send logging event to remote host ::1 on port 1234.
          System.Net.Sockets.SocketException : Une adresse incompatible avec le protocole demandé a été utilisée
          à System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset,
          Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
          à System.Net.Sockets.UdpClient.Send (Byte[] dgram, Int32 bytes, IPEndPoint endPoint)
          à log4net.Appender.UdpAppender.Append(LoggingEvent loggingEvent)

          To avoid this:

          • first I added this property to IPAddressConverter:

          public bool IsIPv6Supported { get

          { return false; }

          }

          note that I'm not networking aware, and thus this may be replaced by some code able to detect wether or not IP v6 is supported...

          • then I replaced "return host.AddressList[0];" with:

          foreach (IPAddress address in host.AddressList)

          { if ((address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) && !IsIPv6Supported) continue; return address; }

          This way, I return the first IPv4 address.

          Hope this helps

          aproximatively translated from French into English: The used address is incompatible with the requested protocol

          PS: log4net is really fantastic a library. I knew it existed for a long time but am just using it for a few months. I love it.

          Olivier DALET
          ---------------------------------
          http://odalet.wordpress.com
          http://aspadvice.com/blogs/oliviers_net_blog

          Show
          Nicko Cadell added a comment - Details from an email on the log4net-dev list. From: Olivier DALET Subject: BUG REPORT : IP Address parsing (.NET 2.0 and log4net 1.2.10) causes UdpAppender to crash Hi, I've detected a bug in IPAddressConverter.ConvertFrom (log4net-1.2.10\src\Util\TypeConverters\IPAddressConverter.cs): If compiled with .NET framework 2.0, the IP is resolved via dns using this code: IPHostEntry host = Dns.GetHostEntry(str); then the first address in the host.AddressList array is returned. When executed on my computer, this array contains the following items: + [0] {::1} System.Net.IPAddress + [1] {192.168.0.2 } System.Net.IPAddress + [2] {192.168.206.1} System.Net.IPAddress + [3] {192.168.114.1} System.Net.IPAddress So, with the existing code, the address ::1 is selected... and the UdpAppender throws an Exception log4net:ERROR [UdpAppender] Unable to send logging event to remote host ::1 on port 1234. System.Net.Sockets.SocketException : Une adresse incompatible avec le protocole demandé a été utilisée à System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP) à System.Net.Sockets.UdpClient.Send (Byte[] dgram, Int32 bytes, IPEndPoint endPoint) à log4net.Appender.UdpAppender.Append(LoggingEvent loggingEvent) To avoid this: first I added this property to IPAddressConverter: public bool IsIPv6Supported { get { return false; } } note that I'm not networking aware, and thus this may be replaced by some code able to detect wether or not IP v6 is supported... then I replaced "return host.AddressList [0] ;" with: foreach (IPAddress address in host.AddressList) { if ((address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) && !IsIPv6Supported) continue; return address; } This way, I return the first IPv4 address. Hope this helps aproximatively translated from French into English: The used address is incompatible with the requested protocol PS: log4net is really fantastic a library. I knew it existed for a long time but am just using it for a few months. I love it. – Olivier DALET --------------------------------- http://odalet.wordpress.com http://aspadvice.com/blogs/oliviers_net_blog
          Hide
          Nicko Cadell added a comment -

          Fixed by adding support for IPv6 to the UdpAppender

          Show
          Nicko Cadell added a comment - Fixed by adding support for IPv6 to the UdpAppender
          Hide
          Dag Christensen added a comment -

          The patch doesn't seem to solve this problem? When I specify "127.0.0.1" for RemoteAddress on my Vista box, it is resolved as a IPv6 address by log4net:
          log4net: XmlHierarchyConfigurator: Setting Property [RemoteAddress] to IPAddress value [x::x:x:x:x%10]

          I checked the code in trunk that Olivier refers to, and it still returns the first address in IPAddressConverter.ConvertFrom.

          Should this be reopened, or are there some steps I am missing?

          Dag

          Show
          Dag Christensen added a comment - The patch doesn't seem to solve this problem? When I specify "127.0.0.1" for RemoteAddress on my Vista box, it is resolved as a IPv6 address by log4net: log4net: XmlHierarchyConfigurator: Setting Property [RemoteAddress] to IPAddress value [x::x:x:x:x%10] I checked the code in trunk that Olivier refers to, and it still returns the first address in IPAddressConverter.ConvertFrom. Should this be reopened, or are there some steps I am missing? — Dag
          Hide
          Marc Borgers added a comment -

          The problem is located in a the routine that does the conversion from hostname to the ipaddress in IPAddressConverter TryParse does not work.

          Here is what I did and what seems to work. I replaced the callback with my own to work around the proble:

          public object ConvertFrom(object source)
          {
          string str = source as string;
          if (str != null && str.Length > 0)
          {
          try
          {
          // It seems that TryParse, used in the Log4Net code does not work
          if (str.Trim(validIpAddressChars).Length == 0)
          {
          try

          { // try to parse the string as an IP address return IPAddress.Parse(str); }

          catch(FormatException)

          { // Ignore a FormatException, try to resolve via DNS }

          }

          // Try to resolve via DNS. This is a blocking call.
          IPHostEntry host = Dns.GetHostByName(str);
          if (host != null &&
          host.AddressList != null &&
          host.AddressList.Length > 0 &&
          host.AddressList[0] != null)

          { return host.AddressList[0]; }

          }
          catch(Exception ex)

          { throw ConversionNotSupportedException.Create(typeof(IPAddress), source, ex); }

          }
          throw ConversionNotSupportedException.Create(typeof(IPAddress), source);
          }

          Show
          Marc Borgers added a comment - The problem is located in a the routine that does the conversion from hostname to the ipaddress in IPAddressConverter TryParse does not work. Here is what I did and what seems to work. I replaced the callback with my own to work around the proble: public object ConvertFrom(object source) { string str = source as string; if (str != null && str.Length > 0) { try { // It seems that TryParse, used in the Log4Net code does not work if (str.Trim(validIpAddressChars).Length == 0) { try { // try to parse the string as an IP address return IPAddress.Parse(str); } catch(FormatException) { // Ignore a FormatException, try to resolve via DNS } } // Try to resolve via DNS. This is a blocking call. IPHostEntry host = Dns.GetHostByName(str); if (host != null && host.AddressList != null && host.AddressList.Length > 0 && host.AddressList [0] != null) { return host.AddressList[0]; } } catch(Exception ex) { throw ConversionNotSupportedException.Create(typeof(IPAddress), source, ex); } } throw ConversionNotSupportedException.Create(typeof(IPAddress), source); }

            People

            • Assignee:
              Nicko Cadell
              Reporter:
              Nicko Cadell
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development