dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.81k stars 196 forks source link

IP_ADDRESS_PREFIX size seems wrong #405

Closed iquiw closed 1 year ago

iquiw commented 1 year ago

Describe the bug and how to reproduce

Run the following program. (Please change InterfaceIndex to proper value below)

        static void Main(string[] args)
        {
            InitializeIpForwardEntry(out var route);

            route.InterfaceIndex = 6;

            route.DestinationPrefix.Prefix.Ipv6.sin6_family = Ws2_32.ADDRESS_FAMILY.AF_INET6;
            route.DestinationPrefix.Prefix.Ipv6.sin6_addr = new Ws2_32.IN6_ADDR(IPAddress.Parse("2404:6800:4004:801::2003").GetAddressBytes());
            route.DestinationPrefix.PrefixLength = 128;

            route.NextHop.Ipv6.sin6_family = Ws2_32.ADDRESS_FAMILY.AF_INET6;
            route.NextHop.Ipv6.sin6_addr = new Ws2_32.IN6_ADDR(IPAddress.Parse("fe80::1").GetAddressBytes());
            route.SitePrefixLength = 128;
            route.Metric = 0;
            route.Protocol = IpHlpApi.MIB_IPFORWARD_PROTO.MIB_IPPROTO_NETMGMT;
            route.Loopback = false;
            route.AutoconfigureAddress = false;
            route.Publish = false;
            route.Immortal = false;

            CreateIpForwardEntry2(ref route);
        }

Then NextHop and RouteMetric of the route are wrong values.

> Get-NetRoute -DestinationPrefix 2404:6800:4004:801::2003/128

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
6       2404:6800:4004:801::2003/128                   ::                                                 3 45       ActiveStore

What code is involved

I suppose structure size (= 30) of IP_ADDRESS_PREFIX is wrong.

https://github.com/dahall/Vanara/blob/91490dcd1be6bb1687b7188ba0cf9870febeb2d8/PInvoke/IpHlpApi/NetIOApi.cs#L5850-L5851

The value 32 seems correct according to C++ sizeof() output.

        [StructLayout(LayoutKind.Sequential, Pack = 4, Size = 32)]
        public struct IP_ADDRESS_PREFIX : IEquatable<IP_ADDRESS_PREFIX>

Expected behavior

With Size = 32, NextHop and Mertic will be the correct values.

> Get-NetRoute -DestinationPrefix 2404:6800:4004:801::2003/128

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
6       2404:6800:4004:801::2003/128                   fe80::1                                            0 45       ActiveStore