Esempio n. 1
0
        public static List <IAdapter> GetAdapters()
        {
            //To use Nicolas .NET wrapper for WinPcap:

            List <IAdapter> deviceList = new List <IAdapter>();

            foreach (Device d in WinPCapWrapper.FindAllDevs())
            {
                deviceList.Add(new WinPCapAdapter((Device)d));
            }
            return(deviceList);


            //To use the old dotNetPcap dll file:

            /*
             * System.Collections.ArrayList tmpList;
             * tmpList=dotnetWinpCap.FindAllDevs();
             *
             * List<IAdapter> devices=new List<IAdapter>(tmpList.Count);
             *
             * foreach(object d in tmpList) {
             *  devices.Add(new WinPCapAdapter((Device)d));
             * }
             * return devices;
             */
        }
        public static List <IAdapter> GetAdapters(int millisecondsTimeout = 1000)
        {
            if (millisecondsTimeout > 0)
            {
                //Let's wrap this peice of unmanaged code in a task in order to handle timeouts better
                var getAdapterTask = System.Threading.Tasks.Task.Factory.StartNew <List <IAdapter> >(() => {
                    //To use Nicolas .NET wrapper for WinPcap:
                    List <IAdapter> deviceList = new List <IAdapter>();
                    try
                    {
                        foreach (Device d in WinPCapWrapper.FindAllDevs())
                        {
                            deviceList.Add(new WinPCapAdapter((Device)d));
                        }
                    }
                    catch { }
                    return(deviceList);
                });

                //getAdapterTask.ContinueWith(t => { /* error handling */ }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);

                //problem: the task might throw an exception
                if (getAdapterTask.Wait(millisecondsTimeout))
                {
                    return(getAdapterTask.Result);
                }
                else
                {
                    throw new TimeoutException("Timeout while getting WinPCap Adapters");
                }
            }
            else
            {
                List <IAdapter> deviceList = new List <IAdapter>();
                foreach (Device d in WinPCapWrapper.FindAllDevs())
                {
                    deviceList.Add(new WinPCapAdapter((Device)d));
                }
                return(deviceList);
            }


            //To use the old dotNetPcap dll file:

            /*
             * System.Collections.ArrayList tmpList;
             * tmpList=dotnetWinpCap.FindAllDevs();
             *
             * List<IAdapter> devices=new List<IAdapter>(tmpList.Count);
             *
             * foreach(object d in tmpList) {
             *  devices.Add(new WinPCapAdapter((Device)d));
             * }
             * return devices;
             */
        }