Example #1
0
        /// <returns>A dictionary mapping Adapter IDs to their NetMon/.Net information structures</returns>
        public Dictionary <uint, AdapterInformation> GetAdapters()
        {
            var output =
                new Dictionary <uint, AdapterInformation>();

            uint errno = InitIfRequired();

            if (errno != 0)
            {
                Console.WriteLine("Failed to get adapters. Initialization of NetAPI returned: " + errno);
                return(null);
            }

            using (NetMonEngine eng = NetmonAPI.NmOpenCaptureEngineManaged())
            {
                NM_NIC_ADAPTER_INFO[] netmonAdapters = NetmonAPI.NmGetAdaptersManaged(eng);

                NetworkInterface[] dotnetAdapters = NetworkInterface.GetAllNetworkInterfaces();

                // Iterating the NetMon adapters we found and matching 'NetworkInterface' objects to them.
                for (uint i = 0; i < netmonAdapters.Length; i++)
                {
                    NM_NIC_ADAPTER_INFO adapterInfo = netmonAdapters[i];
                    String           nameStr        = new String(adapterInfo.ConnectionName);
                    NetworkInterface ni             = dotnetAdapters.Single(iface => nameStr.Contains(iface.Id));

                    // Add the adapter to output
                    output[i] = new AdapterInformation(adapterInfo, ni);
                }
            }

            return(output);
        }
Example #2
0
        public void Start(uint adapterId)
        {
            // TODO: Support multiple adapters
            uint errno;

            errno = InitIfRequired();
            if (errno != 0)
            {
                throw new Exception("Failed to initialize capture. NmApiInitalize returned: " + errno);
            }

            _engine = NetmonAPI.NmOpenCaptureEngineManaged();

            errno = NetmonAPI.NmConfigAdapter(_engine, adapterId, captureCallback, IntPtr.Zero,
                                              NmCaptureCallbackExitMode.DiscardRemainFrames);
            if (errno != 0)
            {
                throw new Exception($"Error configuring adapter #{adapterId}. Errno: " + errno);
            }

            uint ret = NetmonAPI.NmStartCapture(_engine, adapterId, NmCaptureMode.Promiscuous);

            if (ret != 0)
            {
                throw new Exception($"Failed to start capturing adapter #{adapterId}. NmStartCapture returned: {ret}");
            }
        }
Example #3
0
        public void Stop()
        {
            uint errno = NetmonAPI.NmStopCapture(_engine, 1);

            if (errno != 0)
            {
                throw new Exception($"Error stopping Capture. NmStopCapture returned: {errno}");
            }

            _engine.Dispose();
            _engine = null;
        }