Exemplo n.º 1
0
        /// <summary>
        /// </summary>
        public void StartScanning()
        {
            var interfaces = GetNetworkInterfaces();

            foreach (var item in interfaces.Where(x => !String.IsNullOrWhiteSpace(x)))
            {
                Sockets.Add(new SocketObject
                {
                    IPAddress = item
                });
            }
            UpdateConnectionList();

            ValidateNetworkAccess();

            if (Settings.Default.NetworkUseWinPCap)
            {
                // ISSUE: method pointer
                WinPcapWrapper.DataReceived += WinPcapWrapper_DataReceived;
            }

            foreach (var stateObject in Sockets)
            {
                try
                {
                    if (Settings.Default.NetworkUseWinPCap)
                    {
                        var allDevices = WinPcapWrapper.GetAllDevices();
                        stateObject.device = allDevices.FirstOrDefault(x => x.Addresses.Contains(stateObject.IPAddress));
                        if (!string.IsNullOrWhiteSpace(stateObject.device.Name))
                        {
                            WinPcapWrapper.StartCapture(stateObject);
                        }
                    }
                    else
                    {
                        stateObject.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
                        stateObject.Socket.Bind(new IPEndPoint(IPAddress.Parse(stateObject.IPAddress), 0));
                        stateObject.Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AcceptConnection, true);
                        var inFlags = new byte[]
                        {
                            1, 0, 0, 0
                        };
                        var outFlags = new byte[4];
                        stateObject.Socket.IOControl(IOControlCode.ReceiveAll, inFlags, outFlags);
                        stateObject.Socket.ReceiveBufferSize = 0x7D000;
                        stateObject.Socket.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, OnReceive, stateObject);
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
Exemplo n.º 2
0
 private void WinPcapWrapper_DataReceived(object sender, WinPcapWrapper.DataReceivedEventArgs e)
 {
     try
     {
         ParseData(e.Device.State, e.Data, e.Data.Length);
     }
     catch (Exception ex)
     {
         throw new ApplicationException("Error processing Network Data.", ex);
     }
 }