InterfaceCollection.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. namespace NetMQ
  8. {
  9. /// <summary>
  10. /// class InterfaceItem provides the properties Address and BroadcastAddress (both are an IPAddress).
  11. /// This serves to convey information for each of the network interfaces present on a host.
  12. /// </summary>
  13. public class InterfaceItem
  14. {
  15. /// <summary>
  16. /// Create a new InterfaceItem from the given address and broadcast-address.
  17. /// </summary>
  18. /// <param name="address">an IPAddress that will comprise the 'Address' of the new InterfaceItem</param>
  19. /// <param name="broadcastAddress">an IPAddress that will comprise the 'BroadcastAddress' of the new InterfaceItem</param>
  20. public InterfaceItem(IPAddress address, IPAddress broadcastAddress)
  21. {
  22. Address = address;
  23. BroadcastAddress = broadcastAddress;
  24. }
  25. /// <summary>
  26. /// Get the 'address' of this network interface, as an IPAddress.
  27. /// </summary>
  28. public IPAddress Address { get; }
  29. /// <summary>
  30. /// Get the 'broadcast-address' of this network interface, as an IPAddress.
  31. /// </summary>
  32. public IPAddress BroadcastAddress { get; }
  33. }
  34. /// <summary>
  35. /// This is a list of InterfaceItems, each of which has an Address and BroadcastAddress,
  36. /// which is derived from all of the Network Interfaces present on this host at the time an instance of this class is created.
  37. /// </summary>
  38. public class InterfaceCollection : IEnumerable<InterfaceItem>
  39. {
  40. private readonly List<InterfaceItem> m_interfaceItems;
  41. /// <summary>
  42. /// Create a new InterfaceCollection that contains a list of InterfaceItems derived from all of the Network Interfaces present on this host.
  43. /// </summary>
  44. public InterfaceCollection()
  45. {
  46. // Get an array of all NetworkInterfaces that are running, and are not loopback nor Point-to-Point Protocol (PPP).
  47. var interfaces = NetworkInterface.GetAllNetworkInterfaces()
  48. .Where(i => i.OperationalStatus == OperationalStatus.Up &&
  49. i.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
  50. i.NetworkInterfaceType != NetworkInterfaceType.Ppp);
  51. // From that, get all the UnicastAddresses.
  52. var addresses = interfaces
  53. .SelectMany(i => i.GetIPProperties().UnicastAddresses
  54. .Where(a => a.Address.AddressFamily == AddressFamily.InterNetwork));
  55. // From that, compose our list of InterfaceItems each of which has the Address, and a computed broadcast-address.
  56. m_interfaceItems = new List<InterfaceItem>();
  57. foreach (var address in addresses)
  58. {
  59. byte[] broadcastBytes = address.Address.GetAddressBytes();
  60. byte[] mask = address.IPv4Mask.GetAddressBytes();
  61. broadcastBytes[0] |= (byte)~mask[0];
  62. broadcastBytes[1] |= (byte)~mask[1];
  63. broadcastBytes[2] |= (byte)~mask[2];
  64. broadcastBytes[3] |= (byte)~mask[3];
  65. var broadcastAddress = new IPAddress(broadcastBytes);
  66. m_interfaceItems.Add(new InterfaceItem(address.Address, broadcastAddress));
  67. }
  68. }
  69. /// <summary>
  70. /// Return an IEnumerator over the InterfaceItems that this InterfaceCollection contains,
  71. /// which are all of the network interfaces that were not running, nor loopback nor PPP interfaces.
  72. /// </summary>
  73. /// <returns>an IEnumerator over the InterfaceItems that this contains</returns>
  74. public IEnumerator<InterfaceItem> GetEnumerator()
  75. {
  76. return m_interfaceItems.GetEnumerator();
  77. }
  78. IEnumerator IEnumerable.GetEnumerator()
  79. {
  80. return m_interfaceItems.GetEnumerator();
  81. }
  82. }
  83. }