Esempio n. 1
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);
				}
			}
		}
Esempio n. 2
0
        /// <summary>
        /// Returns a list of devices
        /// </summary>
        /// <param name="address">
        /// A <see cref="IPAddress"/>
        /// </param>
        /// <param name="port">
        /// A <see cref="System.Int32"/>
        /// </param>
        /// <param name="remoteAuthentication">
        /// A <see cref="RemoteAuthentication"/>
        /// </param>
        /// <returns>
        /// A <see cref="List<WinPcapDevice>"/>
        /// </returns>
        public static List <WinPcapDevice> Devices(IPAddress address,
                                                   int port,
                                                   RemoteAuthentication remoteAuthentication)
        {
            // build the remote string
            var rmStr = string.Format("rpcap://{0}:{1}",
                                      address,
                                      port);

            return(Devices(rmStr,
                           remoteAuthentication));
        }
Esempio n. 3
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);
                }
            }
        }
Esempio n. 4
0
        private static List <WinPcapDevice> Devices(string rpcapString,
                                                    RemoteAuthentication remoteAuthentication)
        {
            var retval = new List <WinPcapDevice>();

            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
            IntPtr rmAuthPointer;

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

            int result = SafeNativeMethods.pcap_findalldevs_ex(rpcapString,
                                                               rmAuthPointer,
                                                               ref devicePtr,
                                                               errorBuffer);

            // 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;

            while (nextDevPtr != IntPtr.Zero)
            {
                // Marshal pointer into a struct
                var pcap_if_unmanaged =
                    (LibPcap.PcapUnmanagedStructures.pcap_if)Marshal.PtrToStructure(nextDevPtr,
                                                                                    typeof(LibPcap.PcapUnmanagedStructures.pcap_if));
                LibPcap.PcapInterface pcap_if = new LibPcap.PcapInterface(pcap_if_unmanaged);

                // create an airpcap device if the device appears to be a
                // airpcap device
                var winpcapDevice = new WinPcapDevice(pcap_if);
                if (winpcapDevice.Name.Contains("airpcap"))
                {
                    retval.Add(new AirPcap.AirPcapDevice(winpcapDevice));
                }
                else
                {
                    retval.Add(new WinPcapDevice(pcap_if));
                }
                nextDevPtr = pcap_if_unmanaged.Next;
            }

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

            return(retval);
        }