public static LocalConnection ToLocalConnection(this MibTcp6RowOwnerPid tcpRow) { return(new LocalConnection { LocalAddress = new IPEndPoint(tcpRow.LocalAddress, tcpRow.LocalPort), Destination = new IPEndPoint(tcpRow.RemoteAddress, tcpRow.RemotePort), ProcessId = tcpRow.ProcessId, Protocol = ProtocolType.Tcp }); }
/// <summary> /// Gets the network connections currently active. /// </summary> /// <returns>A dictionary of network connections.</returns> public static Dictionary <int, List <NetworkConnection> > GetNetworkConnections() { var retDict = new Dictionary <int, List <NetworkConnection> >(); int length = 0; // TCP IPv4 Win32.GetExtendedTcpTable(IntPtr.Zero, ref length, false, AiFamily.INet, TcpTableClass.OwnerPidAll, 0); using (MemoryAlloc mem = new MemoryAlloc(length)) { if (Win32.GetExtendedTcpTable(mem, ref length, false, AiFamily.INet, TcpTableClass.OwnerPidAll, 0) != 0) { Win32.Throw(); } int count = mem.ReadInt32(0); for (int i = 0; i < count; i++) { MibTcpRowOwnerPid struc = mem.ReadStruct <MibTcpRowOwnerPid>(sizeof(int), MibTcpRowOwnerPid.SizeOf, i); if (!retDict.ContainsKey(struc.OwningProcessId)) { retDict.Add(struc.OwningProcessId, new List <NetworkConnection>()); } retDict[struc.OwningProcessId].Add(new NetworkConnection { Protocol = NetworkProtocol.Tcp, Local = new IPEndPoint(struc.LocalAddress, ((ushort)struc.LocalPort).Reverse()), Remote = new IPEndPoint(struc.RemoteAddress, ((ushort)struc.RemotePort).Reverse()), State = struc.State, Pid = struc.OwningProcessId }); } } // UDP IPv4 length = 0; Win32.GetExtendedUdpTable(IntPtr.Zero, ref length, false, AiFamily.INet, UdpTableClass.OwnerPid, 0); using (MemoryAlloc mem = new MemoryAlloc(length)) { if (Win32.GetExtendedUdpTable(mem, ref length, false, AiFamily.INet, UdpTableClass.OwnerPid, 0) != 0) { Win32.Throw(); } int count = mem.ReadInt32(0); for (int i = 0; i < count; i++) { MibUdpRowOwnerPid struc = mem.ReadStruct <MibUdpRowOwnerPid>(sizeof(int), MibUdpRowOwnerPid.SizeOf, i); if (!retDict.ContainsKey(struc.OwningProcessId)) { retDict.Add(struc.OwningProcessId, new List <NetworkConnection>()); } retDict[struc.OwningProcessId].Add(new NetworkConnection { Protocol = NetworkProtocol.Udp, Local = new IPEndPoint(struc.LocalAddress, ((ushort)struc.LocalPort).Reverse()), Pid = struc.OwningProcessId }); } } // TCP IPv6 length = 0; Win32.GetExtendedTcpTable(IntPtr.Zero, ref length, false, AiFamily.INet6, TcpTableClass.OwnerPidAll, 0); using (MemoryAlloc mem = new MemoryAlloc(length)) { if (Win32.GetExtendedTcpTable(mem, ref length, false, AiFamily.INet6, TcpTableClass.OwnerPidAll, 0) == 0) { int count = mem.ReadInt32(0); for (int i = 0; i < count; i++) { MibTcp6RowOwnerPid struc = mem.ReadStruct <MibTcp6RowOwnerPid>(sizeof(int), MibTcp6RowOwnerPid.SizeOf, i); if (!retDict.ContainsKey(struc.OwningProcessId)) { retDict.Add(struc.OwningProcessId, new List <NetworkConnection>()); } retDict[struc.OwningProcessId].Add(new NetworkConnection { Protocol = NetworkProtocol.Tcp6, Local = new IPEndPoint(new IPAddress(struc.LocalAddress, struc.LocalScopeId), ((ushort)struc.LocalPort).Reverse()), Remote = new IPEndPoint(new IPAddress(struc.RemoteAddress, struc.RemoteScopeId), ((ushort)struc.RemotePort).Reverse()), State = struc.State, Pid = struc.OwningProcessId }); } } } // UDP IPv6 length = 0; Win32.GetExtendedUdpTable(IntPtr.Zero, ref length, false, AiFamily.INet6, UdpTableClass.OwnerPid, 0); using (MemoryAlloc mem = new MemoryAlloc(length)) { if (Win32.GetExtendedUdpTable(mem, ref length, false, AiFamily.INet6, UdpTableClass.OwnerPid, 0) == 0) { int count = mem.ReadInt32(0); for (int i = 0; i < count; i++) { MibUdp6RowOwnerPid struc = mem.ReadStruct <MibUdp6RowOwnerPid>(sizeof(int), MibUdp6RowOwnerPid.SizeOf, i); if (!retDict.ContainsKey(struc.OwningProcessId)) { retDict.Add(struc.OwningProcessId, new List <NetworkConnection>()); } retDict[struc.OwningProcessId].Add(new NetworkConnection { Protocol = NetworkProtocol.Udp6, Local = new IPEndPoint(new IPAddress(struc.LocalAddress, struc.LocalScopeId), ((ushort)struc.LocalPort).Reverse()), Pid = struc.OwningProcessId }); } } } return(retDict); }