Beispiel #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 = RemotePcap.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;
            }
        }
Beispiel #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;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns a list of devices
        /// </summary>
        /// <param name="address">
        /// A <see cref="IPAddress"/>
        /// </param>
        /// <param name="port">
        /// A <see cref="int"/>
        /// </param>
        /// <param name="remoteAuthentication">
        /// A <see cref="RemoteAuthentication"/>
        /// </param>
        /// <returns>
        /// A <see cref="List<NpcapDevice>"/>
        /// </returns>
        public static List <NpcapDevice> Devices(IPAddress address,
                                                 int port,
                                                 RemoteAuthentication remoteAuthentication)
        {
            var source = new IPEndPoint(address, port);

            return(BuildDeviceList(PcapInterface.GetAllPcapInterfaces(source, remoteAuthentication)));
        }
Beispiel #4
0
        /// <summary>
        /// Returns a list of devices
        /// </summary>
        /// <param name="address">
        /// A <see cref="IPAddress"/>
        /// </param>
        /// <param name="port">
        /// A <see cref="int"/>
        /// </param>
        /// <param name="remoteAuthentication">
        /// A <see cref="RemoteAuthentication"/>
        /// </param>
        /// <returns>
        /// A <see cref="List<NpcapDevice>"/>
        /// </returns>
        public static List <NpcapDevice> Devices(IPAddress address,
                                                 int port,
                                                 RemoteAuthentication remoteAuthentication)
        {
            // build the remote string
            var rmStr = string.Format("rpcap://{0}:{1}",
                                      address,
                                      port);

            return(Devices(rmStr,
                           remoteAuthentication));
        }
Beispiel #5
0
        private static List <NpcapDevice> Devices(string rpcapString,
                                                  RemoteAuthentication remoteAuthentication)
        {
            var devicePtr   = IntPtr.Zero;
            var errorBuffer = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE);             //will hold errors

            // convert the remote authentication structure to unmanaged memory if
            // one was specified
            int    result;
            IntPtr rmAuthPointer;

            if (remoteAuthentication == null)
            {
                rmAuthPointer = IntPtr.Zero;
            }
            else
            {
                rmAuthPointer = remoteAuthentication.GetUnmanaged();
            }

            try
            {
                result = SafeNativeMethods.pcap_findalldevs_ex(rpcapString, rmAuthPointer, ref devicePtr, errorBuffer);
            }
            finally
            {
                // free the memory if any was allocated
                if (rmAuthPointer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(rmAuthPointer);
                }
            }

            if (result < 0)
            {
                throw new PcapException(errorBuffer.ToString());
            }

            IntPtr nextDevPtr = devicePtr;

            var retval = BuildDeviceList(devicePtr);

            LibPcap.LibPcapSafeNativeMethods.pcap_freealldevs(devicePtr);              // Free unmanaged memory allocation.

            return(retval);
        }
Beispiel #6
0
        /// <summary>
        /// Open
        /// </summary>
        /// <param name="flags">
        /// A <see cref="OpenFlags"/>
        /// </param>
        /// <param name="readTimeoutMilliseconds">
        /// A <see cref="System.Int32"/>
        /// </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

                IntPtr rmAuthPointer;
                if (remoteAuthentication == null)
                {
                    rmAuthPointer = IntPtr.Zero;
                }
                else
                {
                    rmAuthPointer = remoteAuthentication.GetUnmanaged();
                }

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

                if (rmAuthPointer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(rmAuthPointer);
                }

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

                Active = true;
            }
        }