请求的地址在其上下文中无效 - 将IP绑定到套接字 - 网络嗅探

时间:2016-06-14 03:51:29

标签: c# sockets networking ip

所以我设法检索当前网络上的所有IP地址。现在我正在尝试捕获从我的每个网络ip(他们访问的网站/地址)连接的所有地址。

对于我网络中的几个IP,我将返回此错误"请求的地址在其上下文中无效",但它似乎只适用于我的地址。

目前,它只显示我在 MY PC上连接的网站/地址,而不显示其他网站/地址。

目标 回显我网络上任何已连接的IP请求的所有地址。

例如:
从[192.168.0.3]到=> [52.321.132.2]
从[192.168.0.9]到=> [79.21.23.2]

以下是我正在使用的代码,任何帮助都将不胜感激!

namespace ConsoleApplication2
{
  class Program
   {
    static CountdownEvent countdown;
    static int upCount = 0;
    static object lockObj = new object();
    const bool resolveNames = true;
    static List<string> ADDRESSES = new List<string>();


    static void Main(string[] args)
    {
        #region P_1
        countdown = new CountdownEvent(1);
        Stopwatch sw = new Stopwatch();
        sw.Start();
        string ipBase = "192.168.0.";
        for (int i = 1; i < 255; i++)
        {
            string ip = ipBase + i.ToString();

            Ping p = new Ping();
            p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
            countdown.AddCount();
            p.SendAsync(ip, 100, ip);
        }
        countdown.Signal();
        countdown.Wait();
        sw.Stop();
        TimeSpan span = new TimeSpan(sw.ElapsedTicks);
        Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);
        #endregion

        foreach (String ip in ADDRESSES)
        {
            IPAddress y = IPAddress.Parse(ip);
            try
            {
                Sniff(y);
            }
            catch (Exception i)
            {
                Console.WriteLine("Error for " + y + i.Message);
            }
        }
        Console.Read();

    }

    static void p_PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply != null && e.Reply.Status == IPStatus.Success)
        {
            if (resolveNames)
            {
                string name;
                try
                {
                    IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                    name = hostEntry.HostName;
                }
                catch (SocketException ex)
                {
                    name = "?";
                }
                Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
                ADDRESSES.Add(ip);
            }
            else
            {
                Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
            }
            lock (lockObj)
            {
                upCount++;
            }
        }
        else if (e.Reply == null)
        {
            Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
        }
        countdown.Signal();
    }

    static void Sniff(IPAddress ip)
    {

        Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
        sck.Bind(new IPEndPoint(ip, 0));
        sck.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
        sck.IOControl(IOControlCode.ReceiveAll, new byte[4] { 1, 0, 0, 0 }, null);
        byte[] buffer = new byte[24];
        Action<IAsyncResult> OnReceive = null;
        OnReceive = (ar) =>
        {
            string x = new IPAddress(BitConverter.ToUInt32(buffer, 16)).ToString();
            Console.WriteLine("From [" + ip + "] To => " + x, sck.BeginReceive(buffer, 0, 24, SocketFlags.None, new AsyncCallback(OnReceive), null)); 
        };
        sck.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
                new AsyncCallback(OnReceive), null);
    }

   }
 }

0 个答案:

没有答案