NetworkInterfaceHelper.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Net.NetworkInformation;
  12. using Utilities;
  13. namespace SMBServer
  14. {
  15. public class NetworkInterfaceHelper
  16. {
  17. public static List<IPAddress> GetHostIPAddresses()
  18. {
  19. List<IPAddress> result = new List<IPAddress>();
  20. foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  21. {
  22. IPInterfaceProperties ipProperties = netInterface.GetIPProperties();
  23. foreach (UnicastIPAddressInformation addressInfo in ipProperties.UnicastAddresses)
  24. {
  25. if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
  26. {
  27. result.Add(addressInfo.Address);
  28. }
  29. }
  30. }
  31. return result;
  32. }
  33. public static IPAddress GetSubnetMask(IPAddress ipAddress)
  34. {
  35. foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  36. {
  37. IPInterfaceProperties ipProperties = netInterface.GetIPProperties();
  38. foreach (UnicastIPAddressInformation addressInfo in ipProperties.UnicastAddresses)
  39. {
  40. if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
  41. {
  42. if (IPAddress.Equals(addressInfo.Address, ipAddress))
  43. {
  44. return addressInfo.IPv4Mask;
  45. }
  46. }
  47. }
  48. }
  49. return null;
  50. }
  51. }
  52. }