Host.hx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package sys.net;
  2. import cs.system.Array;
  3. import cs.system.net.Dns;
  4. import cs.system.net.IPAddress;
  5. import cs.system.net.IPHostEntry;
  6. import cs.system.net.sockets.AddressFamily;
  7. import haxe.io.Bytes;
  8. import haxe.io.BytesInput;
  9. /**
  10. A given IP host name.
  11. **/
  12. @:coreapi
  13. class Host {
  14. public var hostEntry(default, null) : IPHostEntry;
  15. public var ipAddress(default, null) : IPAddress;
  16. /**
  17. The actual IP corresponding to the host.
  18. **/
  19. public var ip(get, null) : Int;
  20. private function get_ip() : Int {
  21. return new BytesInput(Bytes.ofData( ipAddress.GetAddressBytes() )).readInt32();
  22. }
  23. /**
  24. Creates a new Host : the name can be an IP in the form "127.0.0.1" or an host name such as "google.com", in which case
  25. the corresponding IP address is resolved using DNS. An exception occur if the host name could not be found.
  26. **/
  27. public function new( name : String ) : Void {
  28. hostEntry = Dns.GetHostEntry(name);
  29. for (i in 0...hostEntry.AddressList.Length) {
  30. if (hostEntry.AddressList[i].AddressFamily == InterNetwork) {
  31. ipAddress = hostEntry.AddressList[i];
  32. break;
  33. }
  34. }
  35. }
  36. /**
  37. Returns the IP representation of the host
  38. **/
  39. public function toString() : String {
  40. return ipAddress.ToString();
  41. }
  42. /**
  43. Perform a reverse-DNS query to resolve a host name from an IP.
  44. **/
  45. public function reverse() : String {
  46. return hostEntry.HostName;
  47. }
  48. /**
  49. Returns the local computer host name
  50. **/
  51. static public function localhost() : String {
  52. return Dns.GetHostName();
  53. }
  54. }