//NICの一覧取得 public static List <pcap_if> GetDeviceList() { var result = new List <pcap_if>(); //デバイス一覧 IntPtr alldevs = new IntPtr(); //情報取得用のバッファ StringBuilder errbuf = new StringBuilder(PCAP_ERRBUF_SIZE); //エラー用バッファ if (pcap_findalldevs(ref alldevs, errbuf) != -1) { IntPtr p = alldevs; while (!p.Equals(IntPtr.Zero)) { // pcap_if構造体で参照する pcap_if i = (pcap_if)Marshal.PtrToStructure(p, typeof(pcap_if)); result.Add(i); p = i.next; // 次のアダプタ情報にポインタを移動 } // 情報取得用のバッファの開放 pcap_freealldevs(alldevs); } else { // エラー (エラーの詳細は、errbufに格納されている) } return(result); }
static List <string[]> PcapListAdapters() { List <string[]> devices = new List <string[]>(); IntPtr rawPcapAdapter = IntPtr.Zero; StringBuilder errbuf = new StringBuilder(PCAP_ERRBUF_SIZE); if (NativeMethods.pcap_findalldevs(ref rawPcapAdapter, errbuf) == -1) { return(null); } if (rawPcapAdapter == IntPtr.Zero) { return(null); } try { pcap_if d_0 = (pcap_if)Marshal.PtrToStructure(rawPcapAdapter, typeof(pcap_if)); foreach (pcap_if d in d_0) { errbuf.Clear(); IntPtr handle; if ((handle = NativeMethods.pcap_open_live(d.name, 0, 0, 1000, errbuf)) == IntPtr.Zero) { //PSE.CLR_PSE_PluginLog.WriteLine(TraceEventType.Error, (int)DEV9LogSources.WinPcap, // errbuf.ToString()); //PSE.CLR_PSE_PluginLog.WriteLine(TraceEventType.Error, (int)DEV9LogSources.WinPcap, // "Unable to open the adapter. " + d.name + " is not supported by WinPcap"); continue; } if (PcapIsValid(handle, false)) { devices.Add(new string[] { d.description, d.name }); } NativeMethods.pcap_close(handle); } } catch { PSE.CLR_PSE_PluginLog.WriteLine(TraceEventType.Error, (int)DEV9LogSources.WinPcap, "Error Finding devices, halted search"); } finally { NativeMethods.pcap_freealldevs(rawPcapAdapter); } return(devices); }
internal PcapInterface(pcap_if pcapIf, NetworkInterface networkInterface, ICredentials credentials) { Name = pcapIf.Name; Description = pcapIf.Description; Flags = pcapIf.Flags; Addresses = new List <PcapAddress>(); GatewayAddresses = new List <IPAddress>(); Credentials = credentials; // attempt to populate the mac address, // friendly name etc of this device if (networkInterface != null) { var ipProperties = networkInterface.GetIPProperties(); int gatewayAddressCount = ipProperties.GatewayAddresses.Count; if (gatewayAddressCount != 0) { foreach (GatewayIPAddressInformation gatewayInfo in ipProperties.GatewayAddresses) { GatewayAddresses.Add(gatewayInfo.Address); } } FriendlyName = networkInterface.Name; } // retrieve addresses var address = pcapIf.Addresses; while (address != IntPtr.Zero) { // Marshal memory pointer into a sockaddr struct var addr = Marshal.PtrToStructure <pcap_addr>(address); PcapAddress newAddress = new PcapAddress(addr); Addresses.Add(newAddress); // is this a hardware address? // if so we should set our MacAddress if (newAddress.Addr?.type == Sockaddr.AddressTypes.HARDWARE) { if (MacAddress == null) { MacAddress = newAddress.Addr.hardwareAddress; } else if (!MacAddress.Equals(newAddress.Addr.hardwareAddress)) { throw new InvalidOperationException("found multiple hardware addresses, existing addr " + MacAddress.ToString() + ", new address " + newAddress.Addr.hardwareAddress.ToString()); } } address = addr.Next; // move to the next address } }
public static List <string> GetWinPcapDevs() { List <string> devices = new List <string>(); if (File.Exists(Environment.GetEnvironmentVariable("windir") + @"\System32\wpcap.dll") && File.Exists(Environment.GetEnvironmentVariable("windir") + @"\System32\Packet.dll")) { IntPtr pFirstDev = IntPtr.Zero; IntPtr pErrBuff = Marshal.AllocHGlobal(PCAP_ERRBUF_SIZE); if (pcap_findalldevs(ref pFirstDev, pErrBuff) == 0) { if (pFirstDev != IntPtr.Zero) { IntPtr pDev = pFirstDev; while (pDev != IntPtr.Zero) { pcap_if pcap_if = (pcap_if)Marshal.PtrToStructure(pDev, typeof(pcap_if)); devices.Add(Regex.Replace(pcap_if.Name, @"^.*({.*})$", "$1")); pDev = pcap_if.Next; } pcap_freealldevs(pFirstDev); } } else { if (pErrBuff != IntPtr.Zero) { MessageBox.Show(Marshal.PtrToStringAnsi(pErrBuff), "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Marshal.FreeHGlobal(pErrBuff); } else { if (File.Exists(Environment.GetEnvironmentVariable("windir") + @"\System32\wpcap.dll")) { MessageBox.Show(@"""" + Environment.GetEnvironmentVariable("windir") + @"\System32\wpcap.dll"" is missing", "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (File.Exists(Environment.GetEnvironmentVariable("windir") + @"\System32\Packet.dll")) { MessageBox.Show(@"""" + Environment.GetEnvironmentVariable("windir") + @"\System32\Packet.dll"" is missing", "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(devices); }
static pcap_if get_if(string ifname) { var allif = pcap.pcap_if.findalldevs(); pcap_if.dumpalldevs(Console.Out); pcap_if ifpc = null; foreach (var it in allif) { if (it.name.Equals(ifname)) { ifpc = it; break; } } return(ifpc); }
private unsafe IList <Device> GetAllDevices() { List <Device> deviceList = new List <Device>(); IntPtr deviceListPtr = IntPtr.Zero; IntPtr currentAddress; try { StringBuilder errorBuffer = new StringBuilder(PCAP_ERRBUF_SIZE); int returnCode = pcap_findalldevs(ref deviceListPtr, errorBuffer); if (returnCode != 0) { throw new ApplicationException("Cannot enumerate devices: [" + errorBuffer.ToString() + "]."); } IntPtr ip = deviceListPtr; while (ip != IntPtr.Zero) { pcap_if dev = (pcap_if)Marshal.PtrToStructure(ip, typeof(pcap_if)); Device device = new Device(); device.Name = dev.name; device.Description = dev.description; device.Addresses = new List <uint>(); currentAddress = dev.addresses; while (currentAddress != IntPtr.Zero) { pcap_addr address = *(pcap_addr *)currentAddress; if (address.addr != IntPtr.Zero) { sockaddr_in sockaddress = *(sockaddr_in *)address.addr; if (sockaddress.sin_family == AF_INET) { device.Addresses.Add(sockaddress.sin_addr); } } currentAddress = address.next; } deviceList.Add(device); ip = dev.next; } } catch (Exception ex) { throw new ApplicationException("Unable to get WinPcap device list.", ex); } finally { // always release memory after getting device list. if (deviceListPtr != IntPtr.Zero) { pcap_freealldevs(deviceListPtr); } } return(deviceList); }