/// <summary> /// Bind the driver to an adapter for use. Note that an adapter /// can only be bound to by one driver at a time. /// </summary> /// <param name="networkInterface"> /// The network interface to bind to. /// </param> /// <exception cref="SystemException"> /// If the driver cannot be bound to, a system exception is thrown. /// </exception> public void BindAdapter (NetworkInterface networkInterface) { for (int i = 0; i < m_adapters.Length; i++) { for (int j = 0; j < m_adapters[i].Interfaces.Length; j++) { if (m_adapters[i].Interfaces[j].Address.Equals (networkInterface.Address)) { BindAdapter (m_adapters[i].AdapterID); return; } } } throw new SystemException ("Could not find the specified adapter"); }
/// <summary> /// Create a new NetworkInterfaceList class. /// </summary> /// <exception cref="Exception"> /// Thrown when the network interface list could not be read. /// </exception> public NetworkInterfaceList() { // the length of a single interface is 76 bytes const int interfaceBufferLength = 76; // allocate enough space in the out buffer for up to 8 interfaces. // there should be no more than this byte[] outBuffer = new byte[interfaceBufferLength * 8]; byte[] inBuffer = new byte[4]; // get the interface list int bytesRecieved = m_socket.IOControl (SIO_GET_INTERFACE_LIST, inBuffer, outBuffer); // calculate the number of interfaces int numInterfaces = bytesRecieved / interfaceBufferLength; interfaces = new NetworkInterface [numInterfaces]; // buffer pointer int position = 0; // loop through each interface for (int i = 0; i < numInterfaces; i++) { interfaces[i] = new NetworkInterface (); position = i*interfaceBufferLength; // extract the flags from the first 4 bytes int flags = BitConverter.ToInt32 (outBuffer, position); position += 4; // fill out the interface info flags interfaces[i].IsEnabled = ((flags & (int)InterfaceInfoFlags.IIF_UP) != 0); interfaces[i].isLoopback = ((flags & (int)InterfaceInfoFlags.IIF_LOOPBACK) != 0); interfaces[i].IsPointToPointLink = ((flags & (int)InterfaceInfoFlags.IIF_POINTTOPOINT)!= 0); interfaces[i].SupportMulticast = ((flags & (int)InterfaceInfoFlags.IIF_MULTICAST) != 0); interfaces[i].SupportsBroadcast = ((flags & (int)InterfaceInfoFlags.IIF_BROADCAST) != 0); // skip over the next 4 bytes. These are the address family and port fields. // they are unused in this case. position += 4; uint address = BitConverter.ToUInt32 (outBuffer, position); position += 4; // skip over the next 16 bytes. These are all reserved for other address types. position += 16; // skip over the next 4 bytes. These are the address family and port fields. // they are unused in this case. position += 4; uint broadcastAddress = BitConverter.ToUInt32 (outBuffer, position); position += 4; // skip over the next 16 bytes. These are all reserved for other address types. position += 16; // skip over the next 4 bytes. These are the address family and port fields. // they are unused in this case. position += 4; uint netMaskAddress = BitConverter.ToUInt32 (outBuffer, position); position += 4; // skip over the next 16 bytes. These are all reserved for other address types. position += 16; // store the addresses in the interfaces interfaces[i].Address = new IPAddress (address); interfaces[i].BroadcastAddress = new IPAddress (broadcastAddress); interfaces[i].SubnetMask = new IPAddress (netMaskAddress); } // pointer to buffer IntPtr pBuffer = IntPtr.Zero; // buffer size int bufferSize = 0; // return value int ret; // call the method once so we know the buffer size ret = GetIpAddrTable (pBuffer, ref bufferSize, true); // check for other errors if (ret != ERROR_INSUFFICIENT_BUFFER) { Marshal.FreeHGlobal (pBuffer); throw new Exception ("Unable to read IP address table"); } // allocate enough memory to hold the ip table pBuffer = Marshal.AllocHGlobal (bufferSize); // call the method again for real this time ret = GetIpAddrTable (pBuffer, ref bufferSize, true); // check for error if (ret != 0) { Marshal.FreeHGlobal (pBuffer); throw new Exception ("Unable to read IP address table"); } // read the number of entries int nEntries = Marshal.ReadInt32 (pBuffer); // loop through each entry for (int i = 0; i < nEntries; i++) { try { // read the address IPAddress ip = new IPAddress(Marshal.ReadInt32(new IntPtr(pBuffer.ToInt32() + 4 + i * 24))); // find the interface which this matches for(int j = 0; j < interfaces.Length; j++) { if(interfaces[j].Address.Equals(ip)) { // read the interface index interfaces[j].InterfaceIndex = Marshal.ReadInt32(new IntPtr(pBuffer.ToInt32() + 8 + i * 24)); } } } catch(Exception exc) { //System.Diagnostics.Debug.Fail(exc.Message); } } // free the allocated memory Marshal.FreeHGlobal (pBuffer); }