示例#1
0
        public static IList <NetworkInterfaceInfo> GetInterfaces()
        {
            var nicInfos = new List <NetworkInterfaceInfo>();
            var nics     = NetworkInterface.GetAllNetworkInterfaces();

            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);
        }
示例#2
0
        public SocketSniffer(NetworkInterfaceInfo nic, Filters <IPPacket> filters, IOutput output)
        {
            this.outputQueue = new BlockingCollection <TimestampedData>();
            this.filters     = filters;
            this.output      = output;

            this.bufferManager = new BufferManager(BUFFER_SIZE, MAX_RECEIVE);
            this.receivePool   = new ConcurrentStack <SocketAsyncEventArgs>();
            var endPoint = new IPEndPoint(nic.IPAddress, 0);

            // Capturing at the IP level is not supported on Linux
            // https://github.com/dotnet/corefx/issues/25115
            // https://github.com/dotnet/corefx/issues/30197
            var protocolType = ProtocolType.IP;

            // IPv4
            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, protocolType);
            this.socket.Bind(endPoint);
            this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

            // Enter promiscuous mode on Windows only
            EnterPromiscuousMode();
        }