using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace NetMQ
{
///
/// class InterfaceItem provides the properties Address and BroadcastAddress (both are an IPAddress).
/// This serves to convey information for each of the network interfaces present on a host.
///
public class InterfaceItem
{
///
/// Create a new InterfaceItem from the given address and broadcast-address.
///
/// an IPAddress that will comprise the 'Address' of the new InterfaceItem
/// an IPAddress that will comprise the 'BroadcastAddress' of the new InterfaceItem
public InterfaceItem(IPAddress address, IPAddress broadcastAddress)
{
Address = address;
BroadcastAddress = broadcastAddress;
}
///
/// Get the 'address' of this network interface, as an IPAddress.
///
public IPAddress Address { get; }
///
/// Get the 'broadcast-address' of this network interface, as an IPAddress.
///
public IPAddress BroadcastAddress { get; }
}
///
/// This is a list of InterfaceItems, each of which has an Address and BroadcastAddress,
/// which is derived from all of the Network Interfaces present on this host at the time an instance of this class is created.
///
public class InterfaceCollection : IEnumerable
{
private readonly List m_interfaceItems;
///
/// Create a new InterfaceCollection that contains a list of InterfaceItems derived from all of the Network Interfaces present on this host.
///
public InterfaceCollection()
{
// Get an array of all NetworkInterfaces that are running, and are not loopback nor Point-to-Point Protocol (PPP).
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(i => i.OperationalStatus == OperationalStatus.Up &&
i.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
i.NetworkInterfaceType != NetworkInterfaceType.Ppp);
// From that, get all the UnicastAddresses.
var addresses = interfaces
.SelectMany(i => i.GetIPProperties().UnicastAddresses
.Where(a => a.Address.AddressFamily == AddressFamily.InterNetwork));
// From that, compose our list of InterfaceItems each of which has the Address, and a computed broadcast-address.
m_interfaceItems = new List();
foreach (var address in addresses)
{
byte[] broadcastBytes = address.Address.GetAddressBytes();
byte[] mask = address.IPv4Mask.GetAddressBytes();
broadcastBytes[0] |= (byte)~mask[0];
broadcastBytes[1] |= (byte)~mask[1];
broadcastBytes[2] |= (byte)~mask[2];
broadcastBytes[3] |= (byte)~mask[3];
var broadcastAddress = new IPAddress(broadcastBytes);
m_interfaceItems.Add(new InterfaceItem(address.Address, broadcastAddress));
}
}
///
/// Return an IEnumerator over the InterfaceItems that this InterfaceCollection contains,
/// which are all of the network interfaces that were not running, nor loopback nor PPP interfaces.
///
/// an IEnumerator over the InterfaceItems that this contains
public IEnumerator GetEnumerator()
{
return m_interfaceItems.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return m_interfaceItems.GetEnumerator();
}
}
}