Exemple #1
0
        /// <summary>
        /// Open
        /// </summary>
        /// <param name="flags">
        /// A <see cref="OpenFlags"/>
        /// </param>
        /// <param name="readTimeoutMilliseconds">
        /// A <see cref="int"/>
        /// </param>
        /// <param name="remoteAuthentication">
        /// A <see cref="RemoteAuthentication"/>
        /// </param>
        public void Open(OpenFlags flags,
                         int readTimeoutMilliseconds,
                         RemoteAuthentication remoteAuthentication)
        {
            if (!Opened)
            {
                var errbuf = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE); //will hold errors

                var auth = RemoteAuthentication.CreateAuth(Name, remoteAuthentication);

                PcapHandle = LibPcapSafeNativeMethods.pcap_open(Name,
                                                                Pcap.MAX_PACKET_SIZE, // portion of the packet to capture.
                                                                (int)flags,
                                                                readTimeoutMilliseconds,
                                                                ref auth,
                                                                errbuf);

                if (PcapHandle == IntPtr.Zero)
                {
                    string err = "Unable to open the adapter (" + Name + "). " + errbuf.ToString();
                    throw new PcapException(err);
                }

                Active = true;
            }
        }
Exemple #2
0
        /// <summary>
        /// Open a device with specific flags
        /// Npcap extension - Use of this method will exclude your application
        ///                   from working on Linux or Mac
        /// </summary>
        public virtual void Open(OpenFlags flags, int read_timeout)
        {
            ThrowIfNotNpcap();

            if (!Opened)
            {
                var errbuf = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE);
                var auth   = RemoteAuthentication.CreateAuth(Name, Interface.Credentials);

                PcapHandle = LibPcapSafeNativeMethods.pcap_open
                                 (Name,                // name of the device
                                 Pcap.MAX_PACKET_SIZE, // portion of the packet to capture.
                                                       // MAX_PACKET_SIZE (65536) grants that the whole packet will be captured on all the MACs.
                                 (short)flags,         // one or more flags
                                 (short)read_timeout,  // read timeout
                                 ref auth,             // no authentication right now
                                 errbuf);              // error buffer

                if (PcapHandle == IntPtr.Zero)
                {
                    string err = "Unable to open the adapter (" + Name + "). " + errbuf.ToString();
                    throw new PcapException(err);
                }

                Active = true;
            }
        }