コード例 #1
0
 private static NativeMethods.SocketData CreateSocketData(object tcpStructure)
 {
     NativeMethods.SocketData socketData = new NativeMethods.SocketData();
     if (tcpStructure.GetType() == typeof(NativeMethods.MIB_TCPROW_OWNER_PID))
     {
         NativeMethods.MIB_TCPROW_OWNER_PID mib_TCPROW_OWNER_PID = (NativeMethods.MIB_TCPROW_OWNER_PID)tcpStructure;
         socketData.LocalAddress  = new IPAddress((long)((ulong)mib_TCPROW_OWNER_PID.dwLocalAddr));
         socketData.LocalPort     = NativeMethods.ntohs(mib_TCPROW_OWNER_PID.dwLocalPort);
         socketData.RemoteAddress = new IPAddress((long)((ulong)mib_TCPROW_OWNER_PID.dwRemoteAddr));
         socketData.RemotePort    = NativeMethods.ntohs(mib_TCPROW_OWNER_PID.dwRemotePort);
         socketData.OwnerPid      = mib_TCPROW_OWNER_PID.dwOwningPid;
         socketData.State         = mib_TCPROW_OWNER_PID.dwState;
     }
     else
     {
         if (!(tcpStructure.GetType() == typeof(NativeMethods.MIB_TCP6ROW_OWNER_PID)))
         {
             throw new ArgumentException("Type not supported: " + tcpStructure.GetType().FullName);
         }
         NativeMethods.MIB_TCP6ROW_OWNER_PID mib_TCP6ROW_OWNER_PID = (NativeMethods.MIB_TCP6ROW_OWNER_PID)tcpStructure;
         socketData.LocalAddress  = new IPAddress(mib_TCP6ROW_OWNER_PID.ucLocalAddr, (long)((ulong)mib_TCP6ROW_OWNER_PID.dwLocalScopeId));
         socketData.LocalPort     = NativeMethods.ntohs(mib_TCP6ROW_OWNER_PID.dwLocalPort);
         socketData.RemoteAddress = new IPAddress(mib_TCP6ROW_OWNER_PID.ucRemoteAddr, (long)((ulong)mib_TCP6ROW_OWNER_PID.dwRemoteScopeId));
         socketData.RemotePort    = NativeMethods.ntohs(mib_TCP6ROW_OWNER_PID.dwRemotePort);
         socketData.OwnerPid      = mib_TCP6ROW_OWNER_PID.dwOwningPid;
         socketData.State         = mib_TCP6ROW_OWNER_PID.dwState;
     }
     return(socketData);
 }
コード例 #2
0
        private static List <NativeMethods.SocketData> ListActiveSockets <T>(T tcpStructure)
        {
            if (typeof(T) != typeof(NativeMethods.MIB_TCPROW_OWNER_PID) && typeof(T) != typeof(NativeMethods.MIB_TCP6ROW_OWNER_PID))
            {
                throw new ArgumentException(typeof(T).FullName + " not supported for method GetSocketsOpenedByProcess<T>");
            }
            IntPtr intPtr = IntPtr.Zero;
            List <NativeMethods.SocketData> list = new List <NativeMethods.SocketData>();
            List <NativeMethods.SocketData> result;

            try
            {
                int  cb = 0;
                uint ulAf;
                if (typeof(T) == typeof(NativeMethods.MIB_TCPROW_OWNER_PID))
                {
                    ulAf = 2U;
                }
                else
                {
                    ulAf = 23U;
                }
                NativeMethods.GetExtendedTcpTable(IntPtr.Zero, ref cb, true, ulAf, NativeMethods.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0U);
                intPtr = Marshal.AllocCoTaskMem(cb);
                NativeMethods.GetExtendedTcpTable(intPtr, ref cb, true, ulAf, NativeMethods.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0U);
                int    num     = Marshal.ReadInt32(intPtr);
                int    offset  = Marshal.SizeOf(tcpStructure);
                IntPtr intPtr2 = IntPtr.Add(intPtr, 4);
                for (int i = 0; i < num; i++)
                {
                    tcpStructure = (T)((object)Marshal.PtrToStructure(intPtr2, tcpStructure.GetType()));
                    NativeMethods.SocketData item = NativeMethods.CreateSocketData(tcpStructure);
                    list.Add(item);
                    intPtr2 = IntPtr.Add(intPtr2, offset);
                }
                result = list;
            }
            finally
            {
                if (intPtr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(intPtr);
                }
            }
            return(result);
        }
コード例 #3
0
        // Token: 0x0600216D RID: 8557 RVA: 0x0009B40C File Offset: 0x0009960C
        private Exception GenerateDiagnosticException(string errorMessage, Exception inner)
        {
            Exception ex = null;

            NativeMethods.SocketData openSocketByPort = NativeMethods.GetOpenSocketByPort(RegistryParameters.HighAvailabilityWebServicePort);
            if (openSocketByPort != null)
            {
                try
                {
                    try
                    {
                        using (Process processById = Process.GetProcessById(openSocketByPort.OwnerPid))
                        {
                            using (Process currentProcess = Process.GetCurrentProcess())
                            {
                                ReplayEventLogConstants.Tuple_ServerLocatorServiceAnotherProcessUsingPort.LogEvent(this.Name, new object[]
                                {
                                    RegistryParameters.HighAvailabilityWebServicePort,
                                    processById.ProcessName,
                                    openSocketByPort.OwnerPid,
                                    currentProcess.ProcessName,
                                    currentProcess.Id
                                });
                                ex = new Exception(string.Format("Server locator manager error: {0}. Process using port is: '{1}' (pid: {2}). Current process: '{3}' (pid: {4})", new object[]
                                {
                                    errorMessage,
                                    processById.ProcessName,
                                    openSocketByPort.OwnerPid,
                                    currentProcess.ProcessName,
                                    currentProcess.Id
                                }), inner);
                            }
                        }
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (InvalidOperationException)
                    {
                    }
                    catch (Win32Exception)
                    {
                    }
                    return(ex);
                }
                finally
                {
                    if (ex == null)
                    {
                        try
                        {
                            using (Process currentProcess2 = Process.GetCurrentProcess())
                            {
                                ex = new Exception(string.Format("Server locator manager error: {0}. Process using port is: '<unknown>' (pid: {1}). Current process: {2} (pid: {3})", new object[]
                                {
                                    errorMessage,
                                    openSocketByPort.OwnerPid,
                                    currentProcess2.ProcessName,
                                    currentProcess2.Id
                                }), inner);
                            }
                        }
                        catch (Win32Exception)
                        {
                            ex = new Exception(string.Format("Server locator manager error: {0}. Process using port is: '<unknown>' (pid: {1}).", errorMessage, openSocketByPort.OwnerPid), inner);
                        }
                    }
                }
            }
            try
            {
                using (Process currentProcess3 = Process.GetCurrentProcess())
                {
                    ex = new Exception(string.Format("Server locator manager error: {0}. No other process using the port was found. Current process: {1} (pid: {2})", errorMessage, currentProcess3.ProcessName, currentProcess3.Id), inner);
                }
            }
            catch (Win32Exception)
            {
                ex = new Exception(string.Format("Server locator manager error: {0}. No other process using the port was found.", errorMessage), inner);
            }
            return(ex);
        }