Host.hx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.net;
  23. import haxe.io.Bytes;
  24. import haxe.io.BytesInput;
  25. /**
  26. A given IP host name.
  27. **/
  28. @:coreapi
  29. class Host {
  30. /**
  31. The provided host string.
  32. **/
  33. public var host(default,null) : String;
  34. /**
  35. The actual IP corresponding to the host.
  36. **/
  37. public var ip(default, null) : Int;
  38. var _ip : String;
  39. /**
  40. 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
  41. the corresponding IP address is resolved using DNS. An exception occur if the host name could not be found.
  42. **/
  43. public function new( name : String ) : Void {
  44. host = name;
  45. if (lua.NativeStringTools.find(name, "(%d+)%.(%d+)%.(%d+)%.(%d+)").begin != null){
  46. _ip = name;
  47. } else {
  48. var res = lua.lib.luv.net.Dns.getaddrinfo(name);
  49. _ip = res.result[1].addr;
  50. if (res.result == null) throw "Unrecognized node name";
  51. }
  52. var num = 0;
  53. for (a in _ip.split(".")){
  54. num = num * 256 + lua.Lua.tonumber(a);
  55. }
  56. ip = num;
  57. }
  58. /**
  59. Returns the IP representation of the host
  60. **/
  61. public function toString() : String {
  62. return _ip;
  63. }
  64. /**
  65. Perform a reverse-DNS query to resolve a host name from an IP.
  66. **/
  67. public function reverse() : String {
  68. return lua.lib.luv.net.Dns.getnameinfo({ip : _ip}).result;
  69. }
  70. /**
  71. Returns the local computer host name
  72. **/
  73. static public function localhost() : String {
  74. return lua.lib.luasocket.socket.Dns.gethostname();
  75. }
  76. }