/*++ * * Routine Description: * * Takes a native pointer (expressed as an int) to a hostent structure, * and converts the information in their to an IPHostEntry class. This * involves walking through an array of native pointers, and a temporary * ArrayList object is used in doing this. * * Arguments: * * nativePointer - Native pointer to hostent structure. * * * * Return Value: * * An IPHostEntry structure. * * --*/ private static IPHostEntry NativeToHostEntry(IntPtr nativePointer) { // // marshal pointer to struct // hostent Host = (hostent)Marshal.PtrToStructure(nativePointer, typeof(hostent)); IPHostEntry HostEntry = new IPHostEntry(); if (Host.h_name != IntPtr.Zero) { HostEntry.HostName = Marshal.PtrToStringAnsi(Host.h_name); GlobalLog.Print("HostEntry.HostName: " + HostEntry.HostName); } // decode h_addr_list to ArrayList of IP addresses. // The h_addr_list field is really a pointer to an array of pointers // to IP addresses. Loop through the array, and while the pointer // isn't NULL read the IP address, convert it to an IPAddress class, // and add it to the list. ArrayList TempList = new ArrayList(); int IPAddressToAdd; string AliasToAdd; IntPtr currentArrayElement; // // get the first pointer in the array // currentArrayElement = Host.h_addr_list; nativePointer = Marshal.ReadIntPtr(currentArrayElement); while (nativePointer != IntPtr.Zero) { // // if it's not null it points to an IPAddress, // read it... // IPAddressToAdd = Marshal.ReadInt32(nativePointer); #if BIGENDIAN // IP addresses from native code are always a byte array // converted to int. We need to convert the address into // a uniform integer value. IPAddressToAdd = (int)(((uint)IPAddressToAdd << 24) | (((uint)IPAddressToAdd & 0x0000FF00) << 8) | (((uint)IPAddressToAdd >> 8) & 0x0000FF00) | ((uint)IPAddressToAdd >> 24)); #endif GlobalLog.Print("currentArrayElement: " + currentArrayElement.ToString() + " nativePointer: " + nativePointer.ToString() + " IPAddressToAdd:" + IPAddressToAdd.ToString()); // // ...and add it to the list // TempList.Add(new IPAddress(IPAddressToAdd)); // // now get the next pointer in the array and start over // currentArrayElement = IntPtrHelper.Add(currentArrayElement, IntPtr.Size); nativePointer = Marshal.ReadIntPtr(currentArrayElement); } HostEntry.AddressList = new IPAddress[TempList.Count]; TempList.CopyTo(HostEntry.AddressList, 0); // // Now do the same thing for the aliases. // TempList.Clear(); currentArrayElement = Host.h_aliases; nativePointer = Marshal.ReadIntPtr(currentArrayElement); while (nativePointer != IntPtr.Zero) { GlobalLog.Print("currentArrayElement: " + ((long)currentArrayElement).ToString() + "nativePointer: " + ((long)nativePointer).ToString()); // // if it's not null it points to an Alias, // read it... // AliasToAdd = Marshal.PtrToStringAnsi(nativePointer); // // ...and add it to the list // TempList.Add(AliasToAdd); // // now get the next pointer in the array and start over // currentArrayElement = IntPtrHelper.Add(currentArrayElement, IntPtr.Size); nativePointer = Marshal.ReadIntPtr(currentArrayElement); } HostEntry.Aliases = new string[TempList.Count]; TempList.CopyTo(HostEntry.Aliases, 0); return(HostEntry); } // NativeToHostEntry
/*++ * * Routine Description: * * Takes a native pointer (expressed as an int) to a hostent structure, * and converts the information in their to an IPHostEntry class. This * involves walking through an array of native pointers, and a temporary * ArrayList object is used in doing this. * * Arguments: * * nativePointer - Native pointer to hostent structure. * * * * Return Value: * * An IPHostEntry structure. * * --*/ private static IPHostEntry NativeToHostEntry(IntPtr nativePointer) { // // marshal pointer to struct // hostent Host = Marshal.PtrToStructure <hostent>(nativePointer); IPHostEntry HostEntry = new IPHostEntry(); if (Host.h_name != IntPtr.Zero) { HostEntry.HostName = Marshal.PtrToStringAnsi(Host.h_name); if (NetEventSource.IsEnabled) { NetEventSource.Info(null, $"HostEntry.HostName: {HostEntry.HostName}"); } } // decode h_addr_list to ArrayList of IP addresses. // The h_addr_list field is really a pointer to an array of pointers // to IP addresses. Loop through the array, and while the pointer // isn't NULL read the IP address, convert it to an IPAddress class, // and add it to the list. var TempIPAddressList = new List <IPAddress>(); int IPAddressToAdd; string AliasToAdd; IntPtr currentArrayElement; // // get the first pointer in the array // currentArrayElement = Host.h_addr_list; nativePointer = Marshal.ReadIntPtr(currentArrayElement); while (nativePointer != IntPtr.Zero) { // // if it's not null it points to an IPAddress, // read it... // IPAddressToAdd = Marshal.ReadInt32(nativePointer); #if BIGENDIAN // IP addresses from native code are always a byte array // converted to int. We need to convert the address into // a uniform integer value. IPAddressToAdd = (int)(((uint)IPAddressToAdd << 24) | (((uint)IPAddressToAdd & 0x0000FF00) << 8) | (((uint)IPAddressToAdd >> 8) & 0x0000FF00) | ((uint)IPAddressToAdd >> 24)); #endif if (NetEventSource.IsEnabled) { NetEventSource.Info(null, $"currentArrayElement:{currentArrayElement} nativePointer:{nativePointer} IPAddressToAdd:{IPAddressToAdd}"); } // // ...and add it to the list // TempIPAddressList.Add(new IPAddress((long)IPAddressToAdd & 0x0FFFFFFFF)); // // now get the next pointer in the array and start over // currentArrayElement = currentArrayElement + IntPtr.Size; nativePointer = Marshal.ReadIntPtr(currentArrayElement); } HostEntry.AddressList = TempIPAddressList.ToArray(); // // Now do the same thing for the aliases. // var TempAliasList = new List <string>(); currentArrayElement = Host.h_aliases; nativePointer = Marshal.ReadIntPtr(currentArrayElement); while (nativePointer != IntPtr.Zero) { if (NetEventSource.IsEnabled) { NetEventSource.Info(null, $"currentArrayElement:{currentArrayElement} nativePointer:{nativePointer}"); } // // if it's not null it points to an Alias, // read it... // AliasToAdd = Marshal.PtrToStringAnsi(nativePointer); // // ...and add it to the list // TempAliasList.Add(AliasToAdd); // // now get the next pointer in the array and start over // currentArrayElement = currentArrayElement + IntPtr.Size; nativePointer = Marshal.ReadIntPtr(currentArrayElement); } HostEntry.Aliases = TempAliasList.ToArray(); return(HostEntry); } // NativeToHostEntry