PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Sarnata.Net.BareFtp/Protocol/HostNameResolver.cs

#
C# | 59 lines | 29 code | 4 blank | 26 comment | 7 complexity | 333de309436783369f221b94f03b6589 MD5 | raw file
  1. // HostNameResolver.cs
  2. //
  3. // Modified slightly for bareFTP by Christian Eide <christian@bareftp.org>
  4. //
  5. // Original copyright notice:
  6. // -------------------
  7. //
  8. // Copyright (C) 2004 Enterprise Distributed Technologies Ltd
  9. //
  10. // www.enterprisedt.com
  11. //
  12. // This library is free software; you can redistribute it and/or
  13. // modify it under the terms of the GNU Lesser General Public
  14. // License as published by the Free Software Foundation; either
  15. // version 2.1 of the License, or (at your option) any later version.
  16. //
  17. // This library is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. // Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public
  23. // License along with this library; if not, write to the Free Software
  24. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. using System;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. using System.Text.RegularExpressions;
  29. namespace Sarnata.Net.BareFtp.Protocol
  30. {
  31. public class HostNameResolver
  32. {
  33. private const string IP_ADDRESS_REGEX = @"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}";
  34. public static IPAddress GetAddress(string hostName)
  35. {
  36. if (hostName == null)
  37. throw new ArgumentNullException();
  38. IPAddress address = null;
  39. if (Regex.IsMatch(hostName, IP_ADDRESS_REGEX))
  40. address = IPAddress.Parse(hostName);
  41. else
  42. {
  43. IPAddress[] addresses = Dns.GetHostEntry(hostName).AddressList;
  44. // see if there's an IPv4 address
  45. foreach (IPAddress a in addresses)
  46. if (a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  47. address = a;
  48. // otherwise throw an exception coz we don't handle IPv6 yet.
  49. if (address == null)
  50. throw new ArgumentException(hostName + " resolves to an unsupported protocol.");
  51. }
  52. return address;
  53. }
  54. }
  55. }