On my (cursed?) MacBook Pro, calling
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
results in an exception:
System.ArgumentException: length
at System.Array.Copy (System.Array sourceArray, Int32 sourceIndex, System.Array destinationArray, Int32 destinationIndex, Int32 length) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.MacOsNetworkInterface.ImplGetAllNetworkInterfaces () [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces () [0x00000] in <filename unknown>:0
Some printfs later, and we see why:
Array.Copy(array.Length=12, 3, array.Length=0, 0, 0)
Array.Copy(array.Length=12, 4, array.Length=0, 0, 0)
Array.Copy(array.Length=12, 4, array.Length=0, 0, 0)
Array.Copy(array.Length=12, 3, array.Length=6, 0, 6)
Array.Copy(array.Length=12, 3, array.Length=6, 0, 6)
Array.Copy(array.Length=12, 3, array.Length=8, 0, 4)
Array.Copy(array.Length=12, 4, array.Length=6, 0, 6)
Array.Copy(array.Length=12, 8, array.Length=6, 0, 6)
# BOOM!
(Apparently I have 8 interfaces: lo0, gif0, stf0, en0, en1, fw0, p2p0,
and vboxnet0.)
The problem is readily obvious: we're trying to read 6 bytes of data
when only 4 are available, and Array.Copy() complains appropriately.
The fix -- which looks to almost contradict
a02076ba -- is to use
`sockaddrdl.sdl_data.Length - sockaddrdl.sdl_nlen` instead of
`sockaddrdl.sdl_data.Length - macAddress.Length` (or sdl_alen).
sdl_nlen is the offset into the source sockaddrdl.sdl_data array, and
in this case sockaddrdl.sdl_alen (the length of the destination
macAddress) isn't the same as sockaddrdl.sdl_nlen, hence the breakage.