Esempio n. 1
0
        /// <summary>
        /// This function reads and parses the active UDP socket connections available
        /// and stores them in a list.
        /// </summary>
        /// <returns>
        /// It returns the current set of UDP socket connections which are active.
        /// </returns>
        /// <exception cref="OutOfMemoryException">
        /// This exception may be thrown by the function Marshal.AllocHGlobal when there
        /// is insufficient memory to satisfy the request.
        /// </exception>
        public static List <UdpProcessRecord> GetAllUdpConnections()
        {
            int bufferSize = 0;
            List <UdpProcessRecord> udpTableRecords = new List <UdpProcessRecord>(100);

            // Getting the size of UDP table, that is returned in 'bufferSize' variable.
            _ = WinAPIWrapper.GetExtendedUdpTable(IntPtr.Zero, ref bufferSize, true,
                                                  AF_INET, UdpTableClass.UDP_TABLE_OWNER_PID);

            // Allocating memory from the unmanaged memory of the process by using the
            // specified number of bytes in 'bufferSize' variable.
            IntPtr udpTableRecordPtr = Marshal.AllocHGlobal(bufferSize);

            try
            {
                // The size of the table returned in 'bufferSize' variable in previous
                // call must be used in this subsequent call to 'GetExtendedUdpTable'
                // function in order to successfully retrieve the table.
                uint result = WinAPIWrapper.GetExtendedUdpTable(udpTableRecordPtr, ref bufferSize, true,
                                                                AF_INET, UdpTableClass.UDP_TABLE_OWNER_PID);

                // Non-zero value represent the function 'GetExtendedUdpTable' failed,
                // hence empty list is returned to the caller function.
                if (result != 0)
                {
                    return(new List <UdpProcessRecord>());
                }

                // Marshals data from an unmanaged block of memory to a newly allocated
                // managed object 'udpRecordsTable' of type 'MIB_UDPTABLE_OWNER_PID'
                // to get number of entries of the specified TCP table structure.
                MIB_UDPTABLE_OWNER_PID udpRecordsTable = (MIB_UDPTABLE_OWNER_PID)
                                                         Marshal.PtrToStructure(udpTableRecordPtr, typeof(MIB_UDPTABLE_OWNER_PID));
                IntPtr tableRowPtr = (IntPtr)((long)udpTableRecordPtr +
                                              Marshal.SizeOf(udpRecordsTable.dwNumEntries));

                // Reading and parsing the UDP records one by one from the table and
                // storing them in a list of 'UdpProcessRecord' structure type objects.
                for (int i = 0; i < udpRecordsTable.dwNumEntries; i++)
                {
                    MIB_UDPROW_OWNER_PID udpRow = (MIB_UDPROW_OWNER_PID)
                                                  Marshal.PtrToStructure(tableRowPtr, typeof(MIB_UDPROW_OWNER_PID));
                    udpTableRecords.Add(new UdpProcessRecord(udpRow.localAddr,
                                                             BitConverter.ToUInt16(new byte[2] {
                        udpRow.localPort[1],
                        udpRow.localPort[0]
                    }, 0), udpRow.owningPid));
                    tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(udpRow));
                }
            }
            //catch (OutOfMemoryException outOfMemoryException)
            //{

            //}
            //catch (Exception exception)
            //{

            //}
            finally
            {
                Marshal.FreeHGlobal(udpTableRecordPtr);
            }
            return(udpTableRecords != null?udpTableRecords.Distinct()
                   .ToList <UdpProcessRecord>() : new List <UdpProcessRecord>());
        }