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