Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapToIPv4 fails for some IP Addresses #10

Closed
robsomerville opened this issue Jul 11, 2014 · 1 comment
Closed

MapToIPv4 fails for some IP Addresses #10

robsomerville opened this issue Jul 11, 2014 · 1 comment
Labels

Comments

@robsomerville
Copy link

My IP of my host is: 10.0.2.181

This function is throwing an exception: (UDPChannel.cs)

    static IPAddress MapToIPv4(IPAddress address)
    {
        if (address.AddressFamily == AddressFamily.InterNetwork)
            return address;
        Byte[] bytes = address.GetAddressBytes();
        Int64 newAddress = (bytes[12] & 0xff) | (bytes[13] & 0xff) << 8 | (bytes[14] & 0xff) << 16 | (bytes[15] & 0xff) << 24;
        return new IPAddress(newAddress);
    }

The value for newAddress is being set to a negative number (I think because the last part of my IP is > 127. My fix was to change to:

    static IPAddress MapToIPv4(IPAddress address)
    {
        if (address.AddressFamily == AddressFamily.InterNetwork)
            return address;
        Byte[] bytes = address.GetAddressBytes();
        Byte[] array = new Byte[4];
        array[0] = bytes[12]; array[1] = bytes[13]; array[2] = bytes[14]; array[3] = bytes[15];
        return new IPAddress(array);
    }
@longshine
Copy link
Member

Thank you! It seems the last shifting (bytes[15] & 0xff) << 24 will cause a negative value if the byte is gt 127. You are welcome to fork this repo and make a pull request of fix, if you wish to.

This method is borrowed from IPAddress class in .NET 4.5, which has the same issue :(
See also:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants