Example #1
0
        /// <summary>
        /// Finds MAC address of the gateway, when its IP address is in ipOfgateway.
        /// The MAC address is stored in macOfGateway.
        /// </summary>
        /// <returns>
        ///     <c>true</c> if MAC address is found, otherwise <c>false</c>.
        /// </returns>
        protected bool findMACofGateway()
        {
            ARP arp;

            if (ipOfGateway == null)
            {
                return(false);
            }
            //get list of all available interfaces
            SharpPcap.LibPcap.LibPcapLiveDeviceList lldl = SharpPcap.LibPcap.LibPcapLiveDeviceList.Instance;
            //find MAC address of gateway
            foreach (SharpPcap.LibPcap.LibPcapLiveDevice lld in lldl)
            {
                arp = new ARP(lld);
                try
                {
                    macOfGateway = arp.Resolve(ipOfGateway);
                    Console.WriteLine("MAC of gateway: " + macOfGateway.ToString());
                    liveInterface = lld;
                }
                catch (NullReferenceException)
                {
                    continue;
                }
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// finds the devices that are available in the network
        /// </summary>
        private void findDevices()
        {
            try
            {
                // Print SharpPcap version
                string ver = SharpPcap.Version.VersionString;
                string s   = "SharpPcap" + ver + "Example1.IfList.cs";

                // Retrieve the device list
                SharpPcap.LibPcap.LibPcapLiveDeviceList devices = SharpPcap.LibPcap.LibPcapLiveDeviceList.Instance;

                // If no devices were found print an error
                if (devices.Count < 1)
                {
                    s += "No devices were found on this machine";
                    return;
                }

                s += "\nThe following devices are available on this machine:\n----------------------------------------------------\n";

                // Print out the available network devices
                foreach (ICaptureDevice dev in devices)
                {
                    string      descr     = dev.ToString();
                    string      HostName  = Dns.GetHostName();
                    IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
                    string      ipToUse   = ipaddress[1].ToString();
                    Debug.WriteLine("using the ip: " + ipToUse);
                    if (descr.Contains(ipToUse))
                    {
                        s     += "Will use this one for test: \n" + descr;
                        device = dev;
                        break;
                    }
                    s += dev.ToString() + "\n";
                }
                int index = 1;
                if (device == null)
                {
                    device = devices[index];
                }
                //MessageBox.Show(s);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error creating raw socket: " + e.Message);
            }
        }