[System] Fix MacOsNetworkInterface.ImplGetAllNetworkInterfaces()
authorJonathan Pryor <jonpryor@vt.edu>
Thu, 5 Apr 2012 19:39:45 +0000 (15:39 -0400)
committerJonathan Pryor <jonpryor@vt.edu>
Thu, 5 Apr 2012 19:39:45 +0000 (15:39 -0400)
commitf12dacdc78685b283f6e95d9609abf13092b7ce9
tree617e60f13f33e9ea69993df76ef21ca6432f4a2b
parent9f954a0a1efde4fe44d9592ee29917ad3a6c81be
[System] Fix MacOsNetworkInterface.ImplGetAllNetworkInterfaces()

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.
mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs