[Mono-list] Mono on Linux and Missing methods

Zacharia Karami heatedphoenix at gmail.com
Fri May 29 11:29:45 UTC 2015


To finalize this voyage into missing methods, here's my fix for any future
(would-be) users of MapToIPv4/6.


namespace ExtensionMethods
>
> {
>
> public static class IPextension
>
> {
>
> internal static object GetInstanceField (Type type, object instance,
>> string fieldName)//Steals private values from classes
>
> {
>
> BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public |
>> BindingFlags.NonPublic
>
>                          | BindingFlags.Static;
>
> FieldInfo field = type.GetField (fieldName, bindFlags);
>
> return field.GetValue (instance);
>
> }
>
>
>> public static IPAddress MapToIPv4 (this IPAddress ipa)
>
> {
>
> ushort[] m_Numbers = GetInstanceField (typeof(IPAddress), ipa,
>> "m_Numbers") as ushort[];
>
>
>> foreach (ushort u in m_Numbers) {
>
> Console.WriteLine (u);
>
> }
>
>
>> if (ipa.AddressFamily == AddressFamily.InterNetwork)
>
> return ipa;
>
> if (ipa.AddressFamily != AddressFamily.InterNetworkV6)
>
> throw new Exception ("Only AddressFamily.InterNetworkV6 can be converted
>> to IPv4");
>
>  //Test for 0000 0000 0000 0000 0000 FFFF xxxx xxxx
>
> for (int i = 0; i < 5; i++) {
>
> if (m_Numbers[i] != 0x0000)
>
> throw new Exception ("Address does not have the ::FFFF prefix");
>
> }
>
> if (m_Numbers [5] != 0xFFFF)
>
> throw new Exception ("Address does not have the ::FFFF prefix");
>
>  //We've got an IPv4 address
>
> byte[] ipv4Bytes = new byte [4];
>
> Buffer.BlockCopy (m_Numbers, 12, ipv4Bytes, 0, 4);
>
> return new IPAddress (ipv4Bytes);
>
> }
>
>
>> public static IPAddress MapToIPv6 (this IPAddress ipa)
>
> {
>
> if (ipa.AddressFamily == AddressFamily.InterNetworkV6)
>
> return ipa;
>
> if (ipa.AddressFamily != AddressFamily.InterNetwork)
>
> throw new Exception ("Only AddressFamily.InterNetworkV4 can be converted
>> to IPv6");
>
>  byte[] ipv4Bytes = ipa.GetAddressBytes ();
>
> byte[] ipv6Bytes = new byte [16] {
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
>
> ipv4Bytes [0], ipv4Bytes [1], ipv4Bytes [2], ipv4Bytes [3]
>
> };
>
> return new IPAddress (ipv6Bytes);
>
> }
>
> }
>
> }
>
>
Simply use the methods as you would in NET 4.5. Add "using
ExtensionMethods".
Note, this steals the private values from IPAddress because that's how the
solution was implemented (it needs those values as the original solution
was made to be INSIDE the IPAddress class).

Credit obviously goes to the person who implemented the original solution.

Good luck & regards,
Zack

On Fri, May 22, 2015 at 1:51 PM, Zacharia Karami <heatedphoenix at gmail.com>
wrote:

> Thank you, this is indeed the conclusion I stumbled upon right before your
> previous email.
>
> My solution (for anyone else reading with this issue at some point) is to
> just implement the patch as produced by someone for the Mono project myself
> (as linked to here https://github.com/mono/mono/pull/641).
>
> Just turn it into an extension method for IPaddress and there you go. I'll
> use your tip in the future, though.
>
> Thanks and regards,
> Zacharia
>
> On Fri, May 22, 2015 at 1:42 PM, Edward Ned Harvey (mono) <
> edward.harvey.mono at clevertrove.com> wrote:
>
>> > From: Zacharia Karami [mailto:heatedphoenix at gmail.com]
>> >
>> > "It works in 4.5" on Windows. But again, there is a problem (in so far
>> that
>>
>> Oh! Ok, I get it. You should check the mono class compatibility page.
>> Although *most* stuff in .Net is implemented in mono, there are areas where
>> things are known absent or broken. So whenever you encounter something like
>> this, which works in .Net and doesn't work in mono, always start by
>> checking the class status page.
>>
>> From http://mono-project.com, Documentation, About Mono, Class Status,
>> Status.
>>
>> Here, you can browse the 4.5 column, System.Net.IPAddress, and the two
>> methods MapToIPv4 and MapToIPv6 are both marked as broken. There may be
>> more info somewhere about *why* it's broken and what needs to happen to fix
>> it, but that would be a discussion best for the mono-dev list, to assist in
>> fixing mono sources.
>>
>> In this case, you have a definite answer. That method is definitely just
>> broken in mono, end of story. Your only solution is not to use that method
>> on mono... Hopefully there is a workaround you can use without suffering
>> too much pain.
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20150529/a058ba89/attachment.html>


More information about the Mono-list mailing list