/// <summary> /// Assign a filter to this device given a filterExpression /// </summary> /// <param name="filterExpression">The filter expression to compile</param> protected void SetFilter(string filterExpression) { // save the filter string _filterString = filterExpression; int res; // pcap_setfilter() requires a valid pcap_t which isn't present if // the device hasn't been opened ThrowIfNotOpen("device is not open"); // attempt to compile the program if (!CompileFilter(PcapHandle, filterExpression, 0, out IntPtr bpfProgram, out string errorString)) { string err = string.Format("Can't compile filter ({0}) : {1} ", filterExpression, errorString); throw new PcapException(err); } //associate the filter with this device res = LibPcapSafeNativeMethods.pcap_setfilter(PcapHandle, bpfProgram); // Free the program whether or not we were successful in setting the filter // we don't want to leak unmanaged memory if we throw an exception. FreeBpfProgram(bpfProgram); //watch for errors if (res < 0) { errorString = string.Format("Can't set filter ({0}) : {1}", filterExpression, LastError); throw new PcapException(errorString); } }