Merge pull request #2006 from steffen-kiess/posix-sockets-2
authorJonathan Pryor <jonpryor@vt.edu>
Mon, 18 Jan 2016 22:38:38 +0000 (17:38 -0500)
committerJonathan Pryor <jonpryor@vt.edu>
Mon, 18 Jan 2016 22:38:38 +0000 (17:38 -0500)
[Mono.Posix] Add wrappers for struct sockaddr_*

1  2 
configure.ac
mcs/class/Mono.Posix/Mono.Unix.Native/Syscall.cs
mcs/class/Mono.Posix/Test/Mono.Unix.Native/SocketTest.cs
support/map.h

diff --cc configure.ac
Simple merge
index 4125978df0c8371b3c21d2a86e7e00364e2f7e54,74af55b2fd6c2eccc24007858195507f240b0fb4..4ab7ed53ad6618270bc52ce8e03ee756e7935781
@@@ -1414,13 -1431,146 +1431,151 @@@ namespace Mono.Unix.Native 
        public struct Linger {
                public int l_onoff;
                public int l_linger;
 +
 +              public override string ToString ()
 +              {
 +                      return string.Format ("{0}, {1}", l_onoff, l_linger);
 +              }
        }
  
+       [Map]
+       [StructLayout (LayoutKind.Sequential)]
+       [CLSCompliant (false)]
+       public struct InAddr : IEquatable<InAddr> {
+               public uint s_addr;
+               public unsafe InAddr (byte b0, byte b1, byte b2, byte b3)
+               {
+                       s_addr = 0;
+                       fixed (uint* ptr = &s_addr) {
+                               byte* bytePtr = (byte*) ptr;
+                               bytePtr[0] = b0;
+                               bytePtr[1] = b1;
+                               bytePtr[2] = b2;
+                               bytePtr[3] = b3;
+                       }
+               }
+               public unsafe InAddr (byte[] buffer)
+               {
+                       if (buffer.Length != 4)
+                               throw new ArgumentException ("buffer.Length != 4", "buffer");
+                       s_addr = 0;
+                       fixed (uint* ptr = &s_addr)
+                               Marshal.Copy (buffer, 0, (IntPtr) ptr, 4);
+               }
+               public unsafe void CopyFrom (byte[] source, int startIndex)
+               {
+                       fixed (uint* ptr = &s_addr)
+                               Marshal.Copy (source, startIndex, (IntPtr) ptr, 4);
+               }
+               public unsafe void CopyTo (byte[] destination, int startIndex)
+               {
+                       fixed (uint* ptr = &s_addr)
+                               Marshal.Copy ((IntPtr) ptr, destination, startIndex, 4);
+               }
+               public unsafe byte this[int index] {
+                       get {
+                               if (index < 0 || index >= 4)
+                                       throw new ArgumentOutOfRangeException ("index", "index < 0 || index >= 4");
+                               fixed (uint* ptr = &s_addr)
+                                       return ((byte*) ptr)[index];
+                       }
+                       set {
+                               if (index < 0 || index >= 4)
+                                       throw new ArgumentOutOfRangeException ("index", "index < 0 || index >= 4");
+                               fixed (uint* ptr = &s_addr)
+                                       ((byte*) ptr)[index] = value;
+                       }
+               }
+               public override string ToString ()
+               {
+                       return NativeConvert.ToIPAddress (this).ToString ();
+               }
+               public override int GetHashCode ()
+               {
+                       return s_addr.GetHashCode ();
+               }
+               public override bool Equals (object obj)
+               {
+                       if (!(obj is InAddr))
+                               return false;
+                       return Equals ((InAddr) obj);
+               }
+               public bool Equals (InAddr value)
+               {
+                       return s_addr == value.s_addr;
+               }
+       }
+       [Map]
+       [StructLayout (LayoutKind.Sequential)]
+       public struct In6Addr : IEquatable<In6Addr> {
+               ulong addr0;
+               ulong addr1;
+               public unsafe In6Addr (byte[] buffer)
+               {
+                       if (buffer.Length != 16)
+                               throw new ArgumentException ("buffer.Length != 16", "buffer");
+                       addr0 = addr1 = 0;
+                       fixed (ulong* ptr = &addr0)
+                               Marshal.Copy (buffer, 0, (IntPtr) ptr, 16);
+               }
+               public unsafe void CopyFrom (byte[] source, int startIndex)
+               {
+                       fixed (ulong* ptr = &addr0)
+                               Marshal.Copy (source, startIndex, (IntPtr) ptr, 16);
+               }
+               public unsafe void CopyTo (byte[] destination, int startIndex)
+               {
+                       fixed (ulong* ptr = &addr0)
+                               Marshal.Copy ((IntPtr) ptr, destination, startIndex, 16);
+               }
+               public unsafe byte this[int index] {
+                       get {
+                               if (index < 0 || index >= 16)
+                                       throw new ArgumentOutOfRangeException ("index", "index < 0 || index >= 16");
+                               fixed (ulong* ptr = &addr0)
+                                       return ((byte*) ptr)[index];
+                       }
+                       set {
+                               if (index < 0 || index >= 16)
+                                       throw new ArgumentOutOfRangeException ("index", "index < 0 || index >= 16");
+                               fixed (ulong* ptr = &addr0)
+                                       ((byte*) ptr)[index] = value;
+                       }
+               }
+               public override string ToString ()
+               {
+                       return NativeConvert.ToIPAddress (this).ToString ();
+               }
+               public override int GetHashCode ()
+               {
+                       return addr0.GetHashCode () ^ addr1.GetHashCode ();
+               }
+               public override bool Equals (object obj)
+               {
+                       if (!(obj is In6Addr))
+                               return false;
+                       return Equals ((In6Addr) obj);
+               }
+               public bool Equals (In6Addr value)
+               {
+                       return addr0 == value.addr0 && addr1 == value.addr1;
+               }
+       }
        #endregion
  
        #region Classes
diff --cc support/map.h
Simple merge