Is this a bug in IPAddress?

I have recently run across something that appears to be a bug in the IPAddress class.  I am including a source example below.  In short, the IPAddress class will return a byte array using the GetAddressBytes() method, however, that byte array can not be used in the constructor of another IPAddress object.  It looks like IPAddress requires the byte array used in a constructor to be 16 bytes, but the one returned by the GetAddressBytes() method is only 4 bytes.

In addition, another minor bug is that when the class throws an ArgumentException, the Message property is set to the parameter name, and the Parameter property is set to null.


using System;

using System.Net;

namespace IPAddressBug

{

   /// <summary>

   /// Summary description for Class1.

   /// </summary>

   class Class1

   {

      /// <summary>

      /// The main entry point for the application.

      /// </summary>

      [STAThread]

      static void Main(string[] args)

      {

      // This works correctly

         try

         {

            byte[] address = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7F };

            IPAddress ipAddress = new IPAddress( address );

         }

         catch ( ArgumentException ex )

         {

            Console.WriteLine( "Unable to create IPAddress from 16-byte byte array" );

            Console.WriteLine( "Exception message is {0}, Parameter is {1}", ex.Message, ex.ParamName );

         }

         // This code breaks because the constructor expects a 16-byte array

         try

         {

            byte[] address = new byte[] { 0x01, 0x00, 0x00, 0x7F };

            IPAddress ipAddress = new IPAddress( address );

         }

         catch ( ArgumentException ex )

         {

            Console.WriteLine( "Unable to create IPAddress from 4-byte byte array" );

            Console.WriteLine( "Exception message is {0}, Parameter is {1}", ex.Message, ex.ParamName );

         }

         // This works correctly

         try

         {

            IPAddress ipAddress = new IPAddress( 16777343 );

         }

         catch ( ArgumentException ex )

         {

            Console.WriteLine( "Unable to create IPAddress from long" );

            Console.WriteLine( "Exception message is {0}, Parameter is {1}", ex.Message, ex.ParamName );

         }

         // The following code breaks because the byte[] returned by IPAddress.GetAddressBytes is

         // 4 bytes long, but the constructor requires a 16-byte array.

         try

         {

            IPAddress ipAddress = new IPAddress( 16777343 );

            byte[] byte_address = ipAddress.GetAddressBytes( );

            IPAddress ipAddress2 = new IPAddress( byte_address );

         }

         catch ( ArgumentException ex )

         {

            Console.WriteLine( "Unable to create IPAddress from byte array returned from IPAddress" );

            Console.WriteLine( "Exception message is {0}, Parameter is {1}", ex.Message, ex.ParamName );

         }

      }

   }

}


 



Answer this question

Is this a bug in IPAddress?

  • CleverCoder

    Thank you for the quick response.  I'll look for it in the next service pack for v1.1.
  • Eylem Ugurel

    Which version of the framework are you using. The reason the IPAddress class expects 4 bytes is because it expects to create a IPv4 address. If you want to create an IPv6 address then its supported only on v1.1 and above.



  • Stephen Todd MSFT

    Hi,
    Yes this indeed is a bug and I have filed a bug internally. THanks for bringing this to our attention. This should be fixed in the next service pack release for v1.1. Just as an FYI, this has been fixed in the v2.0 of the .NET framework.

    Thanks

  • cehlers

    I am using v1.1 of the framework.  The problem is that the class is not consistent.  It does not expect 4 bytes on the constructor.  In fact, if you disassemble it, you can see that it is expecting 16 bytes.  However, if you construct it with a long and get bytes, it only returns 4 bytes.  If you pass those same 4 bytes into the constructor, it will fail.  The documentation does not explain what it is is looking for, or why.  It just says it accepts a byte array.



    public IPAddress(byte[] address, long scopeid)
    {
    --code snipped --


    if (address.Length != 0x10)
     {
     throw new ArgumentException("address");
     }
    --code snipped --

    }
     

  • Is this a bug in IPAddress?