Example #1
0
        public void WSAAddressToStringTest()
        {
            var addr = new SOCKADDR(localIP4);
            var len  = 256U;
            var sb   = new StringBuilder((int)len);

            Assert.That(WSAAddressToString(addr, addr.Size, default, sb, ref len), ResultIs.Not.Value(-1));
Example #2
0
        public void ThatFamilyChangesImpactsAllViews(ushort family)
        {
            var sa = new SOCKADDR();

            VerifySockAddr(ref sa, 0);
            sa.Family = family;
            VerifySockAddr(ref sa, family);
        }
Example #3
0
        public void GetBestInterfaceExTest()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var gw = primaryAdapter.GatewayAddresses.Select(a => a.Address.Convert()).FirstOrDefault();
#pragma warning restore CS0618 // Type or member is obsolete
            var sa = new SOCKADDR(gw.GetAddressBytes(), 0, gw.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? 0 : (uint)gw.ScopeId);
            Assert.That(GetBestInterfaceEx(sa, out var idx), Is.Zero);
            Assert.That(idx, Is.EqualTo(primaryAdapter.IfIndex));
        }
Example #4
0
 private static void VerifySockAddr(ref SOCKADDR sa, ushort family)
 {
     Assert.Equal(family, sa.Family);
     Assert.Equal(family, sa.Unspec.Family);
     Assert.Equal(family, sa.InProc.Family);
     Assert.Equal(family, sa.InterProc.Family);
     Assert.Equal(family, sa.IPv4.Family);
     Assert.Equal(family, sa.IPv6.Family);
     Assert.Equal(family, sa.ZeroTier.Family);
 }
Example #5
0
        public void getsocknameTest()
        {
            using var sckt = GetTcpSocket();
            var cname = new SOCKADDR(inet_addr("127.0.0.1"), htons(80));

            Assert.That(connect(sckt, cname, cname.Size), ResultIs.Not.Value(-1));
            var addr = SOCKADDR.Empty;
            var sz   = (int)addr.Size;

            Assert.That(getsockname(sckt, addr, ref sz), ResultIs.Not.Value(-1));
            Assert.That(sz, Is.LessThanOrEqualTo((int)addr.Size));
        }
Example #6
0
        public static IList <AdapterInfo> getIPAddresses()
        {
            var   adapters  = new List <AdapterInfo>();
            uint  outBufLen = 0;
            ERROR err       = (ERROR)Sockets.SocketsApi.GetAdapterAddresses(AddressFamily.InterNetwork, (uint)0, IntPtr.Zero, ref outBufLen);

            if (ERROR.ERROR_BUFFER_OVERFLOW == err)
            {
                byte[] buffer = new byte[outBufLen];

                var bufferHandle = Microsoft.Phone.InteropServices.GCHandle.Alloc(buffer, GCHandleType.Pinned);

                IntPtr pAdapterAddresses = bufferHandle.AddrOfPinnedObject();
                try
                {
                    err = (ERROR)Sockets.SocketsApi.GetAdapterAddresses(AddressFamily.InterNetwork, (uint)0, pAdapterAddresses, ref outBufLen);
                    if (ERROR.ERROR_SUCCESS == err)
                    {
                        IntPtr currPtr = pAdapterAddresses;
                        while (IntPtr.Zero != currPtr)
                        {
                            IP_ADAPTER_ADDRESSES addr = (IP_ADAPTER_ADDRESSES)Microsoft.Phone.InteropServices.Marshal.PtrToStructure(currPtr, typeof(IP_ADAPTER_ADDRESSES));

                            AdapterInfo ainfo = new AdapterInfo();

                            ainfo.FriendlyName = addr.FriendlyName;
                            ainfo.Description  = addr.Description;

                            if (IntPtr.Zero != addr.FirstUnicastAddress)
                            {
                                IP_ADAPTER_UNICAST_ADDRESS unicastAddr = (IP_ADAPTER_UNICAST_ADDRESS)Microsoft.Phone.InteropServices.Marshal.PtrToStructure(addr.FirstUnicastAddress, typeof(IP_ADAPTER_UNICAST_ADDRESS));
                                if (IntPtr.Zero != unicastAddr.Address.lpSockAddr)
                                {
                                    // TODO check ipv4 and ipv6

                                    SOCKADDR  socketAddr = (SOCKADDR)Microsoft.Phone.InteropServices.Marshal.PtrToStructure(unicastAddr.Address.lpSockAddr, typeof(SOCKADDR));
                                    byte[]    saData     = socketAddr.sa_data; // skip take
                                    IPAddress ipAddr     = new IPAddress(GetBytes(saData));
                                    ainfo.Address = ipAddr;
                                }
                            }
                            adapters.Add(ainfo);
                            currPtr = addr.Next;
                        }
                    }
                }
                finally
                {
                    bufferHandle.Free();
                }
            }
            return(adapters);
        }
Example #7
0
        public static IList <IPAddress> GetIPAddresses(FAMILY family)
        {
            List <IPAddress> addresses = new List <IPAddress>();

            foreach (IP_ADAPTER_ADDRESSES addr in GetIPAdapters(family))
            {
                if (IntPtr.Zero != addr.FirstUnicastAddress)
                {
                    IP_ADAPTER_UNICAST_ADDRESS unicastAddr = (IP_ADAPTER_UNICAST_ADDRESS)Marshal.PtrToStructure(addr.FirstUnicastAddress, typeof(IP_ADAPTER_UNICAST_ADDRESS));
                    if (IntPtr.Zero != unicastAddr.Address.lpSockAddr)
                    {
                        SOCKADDR  socketAddr = (SOCKADDR)Marshal.PtrToStructure(unicastAddr.Address.lpSockAddr, typeof(SOCKADDR));
                        byte[]    saData     = socketAddr.sa_data.Skip(2).Take(4).ToArray();
                        IPAddress ipAddr     = new IPAddress(saData);
                        addresses.Add(ipAddr);
                    }
                }
            }

            return(addresses);
        }