Ejemplo n.º 1
0
        public static BpfProgram TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
        {
            var bpfProgram = new BpfProgram();
            int result;

            // Compile the expressions
            if (ThreadSafeCompile)
            {
                result = LibPcapSafeNativeMethods.pcap_compile(pcapHandle,
                                                               bpfProgram,
                                                               filter,
                                                               optimize,
                                                               netmask);
            }
            else
            {
                lock (SyncCompile)
                {
                    result = LibPcapSafeNativeMethods.pcap_compile(pcapHandle,
                                                                   bpfProgram,
                                                                   filter,
                                                                   optimize,
                                                                   netmask);
                }
            }
            if (result < 0)
            {
                // Don't use Dispose since we don't want pcap_freecode to be called here
                Marshal.FreeHGlobal(bpfProgram.handle);
                bpfProgram.SetHandle(IntPtr.Zero);
                bpfProgram = null;
            }
            return(bpfProgram);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Available since libpcap 1.5
 /// </summary>
 /// <param name="adapter"></param>
 /// <param name="precision"></param>
 /// <returns></returns>
 internal static int pcap_set_tstamp_precision(PcapHandle /* pcap_t* p */ adapter, int precision)
 {
     if (Pcap.LibpcapVersion < Libpcap_1_5)
     {
         return((int)PcapError.TimestampPrecisionNotSupported);
     }
     return(_pcap_set_tstamp_precision(adapter, precision));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Available since libpcap 1.5
 /// </summary>
 /// <param name="adapter"></param>
 internal static int pcap_get_tstamp_precision(PcapHandle /* pcap_t* p */ adapter)
 {
     if (Pcap.LibpcapVersion < Libpcap_1_5)
     {
         return((int)TimestampResolution.Microsecond);
     }
     return(_pcap_get_tstamp_precision(adapter));
 }
Ejemplo n.º 4
0
        public static BpfProgram Create(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
        {
            var bpfProgram = TryCreate(pcapHandle, filter, optimize, netmask);

            if (bpfProgram == null)
            {
                throw new PcapException(PcapDevice.GetLastError(pcapHandle));
            }
            return(bpfProgram);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// pcap_set_rfmon() sets whether monitor mode should be set on a capture handle when the handle is activated.
 /// If rfmon is non-zero, monitor mode will be set, otherwise it will not be set.
 /// </summary>
 /// <param name="p">A <see cref="IntPtr"/></param>
 /// <param name="rfmon">A <see cref="int"/></param>
 /// <returns>Returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.</returns>
 internal static int pcap_set_rfmon(PcapHandle /* pcap_t* */ p, int rfmon)
 {
     try
     {
         return(_pcap_set_rfmon(p, rfmon));
     }
     catch (EntryPointNotFoundException)
     {
         return((int)PcapError.RfmonNotSupported);
     }
 }
 internal extern static int pcap_setmode(PcapHandle /* pcap_t * */ p, int mode);
 private extern static int _pcap_setmintocopy(PcapHandle /* pcap_t */ adapter, int sizeInBytes);
 private extern static int _pcap_setbuff(PcapHandle /* pcap_t */ adapter, int bufferSizeInBytes);
 internal extern static int pcap_list_tstamp_types(PcapHandle /* pcap_t* p */ adapter, ref IntPtr types_pointer_pointer);
 internal extern static int pcap_getnonblock(
     PcapHandle /* pcap_if_t** */ adaptHandle,
     [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] StringBuilder /* char* */ errbuf
     );
 internal extern static int pcap_snapshot(PcapHandle /* pcap_t * */ adapter);
Ejemplo n.º 12
0
 internal static PcapError pcap_setbuff(PcapHandle /* pcap_t */ adapter, int bufferSizeInBytes)
 {
     return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
         ? _pcap_setbuff(adapter, bufferSizeInBytes)
         : PcapError.PlatformNotSupported);
 }
 internal extern static int pcap_fileno(PcapHandle /* pcap_t* p */ adapter);
 internal extern static int pcap_breakloop(PcapHandle /* pcap_t* */ p);
 internal extern static int pcap_activate(PcapHandle /* pcap_t* */ p);
 internal extern static int pcap_set_timeout(PcapHandle /* pcap_t* */ p, int to_ms);
 internal extern static int pcap_set_promisc(PcapHandle /* pcap_t* */ p, int promisc);
 internal extern static int pcap_set_snaplen(PcapHandle /* pcap_t* */ p, int snaplen);
 private extern static int _pcap_set_rfmon(PcapHandle /* pcap_t* */ p, int rfmon);
 internal extern static int pcap_set_buffer_size(PcapHandle /* pcap_t */ adapter, int bufferSizeInBytes);
 internal extern static int pcap_set_immediate_mode(PcapHandle /* pcap_t */ adapter, int immediate_mode);
 internal extern static int pcap_get_selectable_fd(PcapHandle /* pcap_t* */ adaptHandle);
Ejemplo n.º 23
0
 internal static int pcap_setmintocopy(PcapHandle /* pcap_t */ adapter, int sizeInBytes)
 {
     return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
         ? _pcap_setmintocopy(adapter, sizeInBytes)
         : (int)PcapError.PlatformNotSupported);
 }
 internal extern static int pcap_stats(PcapHandle /* pcap_t* */ adapter, IntPtr /* struct pcap_stat* */ stat);
 internal extern static int pcap_sendqueue_transmit(PcapHandle /*pcap_t * */ p, ref pcap_send_queue queue, int sync);
 internal extern static int pcap_dispatch(PcapHandle /* pcap_t* */ adaptHandle, int count, pcap_handler callback, IntPtr ptr);
 private extern static int _pcap_get_tstamp_precision(PcapHandle /* pcap_t* p */ adapter);
 internal extern static int pcap_set_tstamp_type(PcapHandle /* pcap_t* p */ adapter, int tstamp_type);
Ejemplo n.º 29
0
 /// <summary>
 /// Retrieve the last error string for a given pcap_t* device
 /// </summary>
 /// <param name="deviceHandle">
 /// A <see cref="IntPtr"/>
 /// </param>
 /// <returns>
 /// A <see cref="string"/>
 /// </returns>
 internal static string GetLastError(PcapHandle deviceHandle)
 {
     return(LibPcapSafeNativeMethods.pcap_geterr(deviceHandle));
 }
 internal extern static int pcap_datalink(PcapHandle /* pcap_t* */ adaptHandle);