private void AddBroadcastInterface(Uri uri, AdapterInfo adapter, byte[] macAddrBytes, CoTMessageType[] messageTypes) { var broadcastInterface = _commo.addBroadcastInterface(macAddrBytes, messageTypes, uri.Host, uri.Port); if (broadcastInterface != null) { string key = uri.OriginalString + "::" + adapter.Name; _broadcastInterfaceDescriptions.Add(broadcastInterface, key); } }
private void AddInboundInterface(Uri uri, AdapterInfo adapter, byte[] macAddrBytes) { string[] mcastUris = { uri.Host }; var inboundInterface = _commo.addInboundInterface(macAddrBytes, uri.Port, mcastUris); if (inboundInterface != null) { string key = uri.OriginalString + "::" + adapter.Name; _inboundInterfaceDescriptions.Add(inboundInterface, key); } }
public static List <AdapterInfo> GetAdapters() { var adapters = new List <AdapterInfo>(); long structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO)); IntPtr pArray = Marshal.AllocHGlobal(new IntPtr(structSize)); int ret = GetAdaptersInfo(pArray, ref structSize); if (ret == ERROR_BUFFER_OVERFLOW) // ERROR_BUFFER_OVERFLOW == 111 { // Buffer was too small, reallocate the correct size for the buffer. pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize)); ret = GetAdaptersInfo(pArray, ref structSize); } if (ret == 0) { // Call Succeeded IntPtr pEntry = pArray; do { var adapter = new AdapterInfo(); // Retrieve the adapter info from the memory address var entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO)); // Adapter Type switch (entry.Type) { case MIB_IF_TYPE_ETHERNET: adapter.Type = "Ethernet"; break; case MIB_IF_TYPE_TOKENRING: adapter.Type = "Token Ring"; break; case MIB_IF_TYPE_FDDI: adapter.Type = "FDDI"; break; case MIB_IF_TYPE_PPP: adapter.Type = "PPP"; break; case MIB_IF_TYPE_LOOPBACK: adapter.Type = "Loopback"; break; case MIB_IF_TYPE_SLIP: adapter.Type = "Slip"; break; default: adapter.Type = "Other/Unknown"; break; } // switch adapter.Name = entry.AdapterName; adapter.Description = entry.AdapterDescription; // MAC Address (data is in a byte[]) adapter.MAC = entry.Address; //adapter.MAC = string.Join("-", Enumerable.Range(0, (int)entry.AddressLength).Select(s => string.Format("{0:X2}", entry.Address[s]))); // Get next adapter (if any) adapters.Add(adapter); pEntry = entry.Next; }while (pEntry != IntPtr.Zero); Marshal.FreeHGlobal(pArray); } else { Marshal.FreeHGlobal(pArray); throw new InvalidOperationException("GetAdaptersInfo failed: " + ret); } return(adapters); }