コード例 #1
0
        /// <summary>Gets the active udp listeners. Uses the native GetUdpTable api.</summary>
        public override IPEndPoint[] GetActiveUdpListeners()
        {
            uint              size         = 0;
            uint              result       = 0;
            SafeLocalFree     buffer       = null;
            List <IPEndPoint> udpListeners = new List <IPEndPoint>();

            // Check if it support IPv4 for IPv6 only modes.
            if (Socket.OSSupportsIPv4)
            {
                // Get the size of buffer needed
                result = UnsafeNetInfoNativeMethods.GetUdpTable(SafeLocalFree.Zero, ref size, true);
                while (result == IpHelperErrors.ErrorInsufficientBuffer)
                {
                    try {
                        //allocate the buffer and get the udptable
                        buffer = SafeLocalFree.LocalAlloc((int)size);
                        result = UnsafeNetInfoNativeMethods.GetUdpTable(buffer, ref size, true);

                        if (result == IpHelperErrors.Success)
                        {
                            //the table info just gives us the number of rows.
                            IntPtr      newPtr       = buffer.DangerousGetHandle();
                            MibUdpTable udpTableInfo = (MibUdpTable)Marshal.PtrToStructure(newPtr, typeof(MibUdpTable));

                            if (udpTableInfo.numberOfEntries > 0)
                            {
                                //we need to skip over the tableinfo to get the inline rows
                                newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udpTableInfo.numberOfEntries));
                                for (int i = 0; i < udpTableInfo.numberOfEntries; i++)
                                {
                                    MibUdpRow udpRow    = (MibUdpRow)Marshal.PtrToStructure(newPtr, typeof(MibUdpRow));
                                    int       localPort = udpRow.localPort1 << 8 | udpRow.localPort2;

                                    udpListeners.Add(new IPEndPoint(udpRow.localAddr, (int)localPort));

                                    //we increment the pointer to the next row
                                    newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udpRow));
                                }
                            }
                        }
                    }
                    finally {
                        if (buffer != null)
                        {
                            buffer.Close();
                        }
                    }
                }
                // if we don't have any ipv4 interfaces detected, just continue
                if (result != IpHelperErrors.Success && result != IpHelperErrors.ErrorNoData)
                {
                    throw new NetworkInformationException((int)result);
                }
            }

            if (Socket.OSSupportsIPv6)
            {
                // Get the size of buffer needed
                size   = 0;
                result = UnsafeNetInfoNativeMethods.GetExtendedUdpTable(SafeLocalFree.Zero, ref size, true,
                                                                        (uint)AddressFamily.InterNetworkV6,
                                                                        UdpTableClass.UdpTableOwnerPid, 0);
                while (result == IpHelperErrors.ErrorInsufficientBuffer)
                {
                    try {
                        // Allocate the buffer and get the udptable
                        buffer = SafeLocalFree.LocalAlloc((int)size);
                        result = UnsafeNetInfoNativeMethods.GetExtendedUdpTable(buffer, ref size, true,
                                                                                (uint)AddressFamily.InterNetworkV6,
                                                                                UdpTableClass.UdpTableOwnerPid, 0);

                        if (result == IpHelperErrors.Success)
                        {
                            // The table info just gives us the number of rows.
                            IntPtr newPtr = buffer.DangerousGetHandle();
                            MibUdp6TableOwnerPid udp6TableOwnerPid
                                = (MibUdp6TableOwnerPid)Marshal.PtrToStructure(newPtr, typeof(MibUdp6TableOwnerPid));

                            if (udp6TableOwnerPid.numberOfEntries > 0)
                            {
                                // We need to skip over the tableinfo to get the inline rows
                                newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udp6TableOwnerPid.numberOfEntries));
                                for (int i = 0; i < udp6TableOwnerPid.numberOfEntries; i++)
                                {
                                    MibUdp6RowOwnerPid udp6RowOwnerPid
                                        = (MibUdp6RowOwnerPid)Marshal.PtrToStructure(newPtr,
                                                                                     typeof(MibUdp6RowOwnerPid));
                                    int localPort = udp6RowOwnerPid.localPort1 << 8 | udp6RowOwnerPid.localPort2;

                                    udpListeners.Add(new IPEndPoint(new IPAddress(udp6RowOwnerPid.localAddr,
                                                                                  udp6RowOwnerPid.localScopeId), localPort));

                                    // We increment the pointer to the next row
                                    newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udp6RowOwnerPid));
                                }
                            }
                        }
                    }
                    finally {
                        if (buffer != null)
                        {
                            buffer.Close();
                        }
                    }
                }
                // If we don't have any ipv6 interfaces detected, just continue
                if (result != IpHelperErrors.Success && result != IpHelperErrors.ErrorNoData)
                {
                    throw new NetworkInformationException((int)result);
                }
            }

            return(udpListeners.ToArray());
        }
コード例 #2
0
        public override IPEndPoint[] GetActiveUdpListeners()
        {
            uint              dwOutBufLen = 0;
            uint              num2        = 0;
            SafeLocalFree     pUdpTable   = null;
            List <IPEndPoint> list        = new List <IPEndPoint>();

            if (Socket.OSSupportsIPv4)
            {
                num2 = UnsafeNetInfoNativeMethods.GetUdpTable(SafeLocalFree.Zero, ref dwOutBufLen, true);
                while (num2 == 0x7a)
                {
                    try
                    {
                        pUdpTable = SafeLocalFree.LocalAlloc((int)dwOutBufLen);
                        num2      = UnsafeNetInfoNativeMethods.GetUdpTable(pUdpTable, ref dwOutBufLen, true);
                        if (num2 == 0)
                        {
                            IntPtr      handle = pUdpTable.DangerousGetHandle();
                            MibUdpTable table  = (MibUdpTable)Marshal.PtrToStructure(handle, typeof(MibUdpTable));
                            if (table.numberOfEntries > 0)
                            {
                                handle = (IntPtr)(((long)handle) + Marshal.SizeOf(table.numberOfEntries));
                                for (int i = 0; i < table.numberOfEntries; i++)
                                {
                                    MibUdpRow structure = (MibUdpRow)Marshal.PtrToStructure(handle, typeof(MibUdpRow));
                                    int       port      = (structure.localPort1 << 8) | structure.localPort2;
                                    list.Add(new IPEndPoint((long)structure.localAddr, port));
                                    handle = (IntPtr)(((long)handle) + Marshal.SizeOf(structure));
                                }
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (pUdpTable != null)
                        {
                            pUdpTable.Close();
                        }
                    }
                }
                if ((num2 != 0) && (num2 != 0xe8))
                {
                    throw new NetworkInformationException((int)num2);
                }
            }
            if (Socket.OSSupportsIPv6)
            {
                dwOutBufLen = 0;
                num2        = UnsafeNetInfoNativeMethods.GetExtendedUdpTable(SafeLocalFree.Zero, ref dwOutBufLen, true, 0x17, UdpTableClass.UdpTableOwnerPid, 0);
                while (num2 == 0x7a)
                {
                    try
                    {
                        pUdpTable = SafeLocalFree.LocalAlloc((int)dwOutBufLen);
                        num2      = UnsafeNetInfoNativeMethods.GetExtendedUdpTable(pUdpTable, ref dwOutBufLen, true, 0x17, UdpTableClass.UdpTableOwnerPid, 0);
                        if (num2 == 0)
                        {
                            IntPtr ptr = pUdpTable.DangerousGetHandle();
                            MibUdp6TableOwnerPid pid = (MibUdp6TableOwnerPid)Marshal.PtrToStructure(ptr, typeof(MibUdp6TableOwnerPid));
                            if (pid.numberOfEntries > 0)
                            {
                                ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(pid.numberOfEntries));
                                for (int j = 0; j < pid.numberOfEntries; j++)
                                {
                                    MibUdp6RowOwnerPid pid2 = (MibUdp6RowOwnerPid)Marshal.PtrToStructure(ptr, typeof(MibUdp6RowOwnerPid));
                                    int num6 = (pid2.localPort1 << 8) | pid2.localPort2;
                                    list.Add(new IPEndPoint(new IPAddress(pid2.localAddr, (long)pid2.localScopeId), num6));
                                    ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(pid2));
                                }
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (pUdpTable != null)
                        {
                            pUdpTable.Close();
                        }
                    }
                }
                if ((num2 != 0) && (num2 != 0xe8))
                {
                    throw new NetworkInformationException((int)num2);
                }
            }
            return(list.ToArray());
        }