public static IList <NetworkInterfaceInfo> GetInterfaces() { var nicInfos = new List <NetworkInterfaceInfo>(); var nics = NetworkInterface.GetAllNetworkInterfaces().Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback); foreach (var nic in nics) { var ipAddresses = nic.GetIPProperties().UnicastAddresses.Where(x => x.Address != null && x.Address.AddressFamily == AddressFamily.InterNetwork); foreach (var ipAddress in ipAddresses) { var nicInfo = new NetworkInterfaceInfo { Index = nicInfos.Count, Id = nic.Id, Name = nic.Name, IPAddress = ipAddress.Address }; nicInfos.Add(nicInfo); } } return(nicInfos); }
private Exception Receiving(NetworkInterfaceInfo networkInterface) { Socket socket = null; try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); socket.Bind(new IPEndPoint(networkInterface.IPAddress, 0)); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); socket.IOControl(IOControlCode.ReceiveAll, BitConverter.GetBytes(1), new byte[4]); byte[] bufferRaw = new byte[10000]; while (!TokenSource.IsCancellationRequested) { int ret = socket.Receive(bufferRaw); var buffer = new byte[ret]; Buffer.BlockCopy(bufferRaw, 0, buffer, 0, ret); outputQueue.Enqueue(new TimestampedData(DateTime.UtcNow, buffer, networkInterface)); this.PacketsObserved++; } } catch (Exception ex) { return(ex); } finally { socket?.Close(); } return(null); }
public Form1() { Iphlpapi = new ProgramFlowManager(); InitializeComponent(); listBox1.DrawItem += ListBox1_DrawItem; listBox1.DataSource = Iphlpapi.programFlowsList; var networkInterfaces = NetworkInterfaceInfo.GetInterfaces(); comboBox1.DataSource = networkInterfaces; sn = new SocketSniffer(Iphlpapi); sn.Error += Sn_Error; sn.init(networkInterfaces); }
public TimestampedData(DateTime timestamp, byte[] data, NetworkInterfaceInfo networkInterface) { Timestamp = timestamp; Data = data; NetworkInterface = networkInterface; }