natVMInetAddressWin32.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 2003, 2006 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. #include <config.h>
  7. #include <platform.h>
  8. #undef STRICT
  9. #include <java/net/VMInetAddress.h>
  10. #include <java/net/UnknownHostException.h>
  11. jstring
  12. java::net::VMInetAddress::getLocalHostname ()
  13. {
  14. char buffer[400];
  15. if (gethostname (buffer, sizeof(buffer)))
  16. return NULL;
  17. // It is admittedly non-optimal to convert the hostname to Unicode
  18. // only to convert it back in getByName, but simplicity wins.
  19. return JvNewStringUTF (buffer);
  20. }
  21. jbyteArray
  22. java::net::VMInetAddress::lookupInaddrAny ()
  23. {
  24. unsigned long laddr = INADDR_ANY;
  25. char *bytes = (char *) &laddr;
  26. int blen = sizeof (laddr);
  27. jbyteArray result = JvNewByteArray (blen);
  28. memcpy (elements (result), bytes, blen);
  29. return result;
  30. }
  31. jstring
  32. java::net::VMInetAddress::getHostByAddr (jbyteArray addr)
  33. {
  34. struct hostent *hptr = NULL;
  35. char *bytes = (char*) elements (addr);
  36. int len = addr->length;
  37. int type;
  38. char *val;
  39. if (len == 4)
  40. {
  41. val = bytes;
  42. type = AF_INET;
  43. }
  44. #ifdef HAVE_INET6
  45. else if (len == 16)
  46. {
  47. val = (char *) &bytes;
  48. type = AF_INET6;
  49. }
  50. #endif /* HAVE_INET6 */
  51. else
  52. JvFail ("unrecognized size");
  53. // FIXME: this is insufficient if some other piece of code calls
  54. // this gethostbyaddr.
  55. JvSynchronize sync (&java::net::VMInetAddress::class$);
  56. hptr = gethostbyaddr (val, len, type);
  57. if (hptr == NULL)
  58. throw new java::net::UnknownHostException ();
  59. return JvNewStringUTF (hptr->h_name);
  60. }
  61. JArray<jbyteArray> *
  62. java::net::VMInetAddress::getHostByName (jstring host)
  63. {
  64. struct hostent *hptr = NULL;
  65. JV_TEMP_UTF_STRING (hostname, host);
  66. // FIXME: this is insufficient if some other piece of code calls
  67. // this gethostbyname.
  68. JvSynchronize sync (&java::net::VMInetAddress::class$);
  69. hptr = gethostbyname (hostname);
  70. if (hptr == NULL)
  71. throw new java::net::UnknownHostException (host);
  72. int count = 0;
  73. char ** ptr = hptr->h_addr_list;
  74. while (*ptr++) count++;
  75. JArray<jbyteArray> *result =
  76. (JArray<jbyteArray> *) _Jv_NewObjectArray (
  77. count, _Jv_GetArrayClass(JvPrimClass(byte), NULL), NULL);
  78. jbyteArray* addrs = elements (result);
  79. for (int i = 0; i < count; i++)
  80. {
  81. addrs[i] = JvNewByteArray (hptr->h_length);
  82. memcpy (elements (addrs[i]), hptr->h_addr_list[i], hptr->h_length);
  83. }
  84. return result;
  85. }
  86. jbyteArray
  87. java::net::VMInetAddress::aton (jstring host)
  88. {
  89. JV_TEMP_UTF_STRING (hostname, host);
  90. char* bytes = NULL;
  91. int blen = 0;
  92. unsigned long laddr = inet_addr (hostname);
  93. if (laddr != INADDR_NONE)
  94. {
  95. bytes = (char *) &laddr;
  96. blen = 4;
  97. }
  98. if (blen == 0)
  99. return NULL;
  100. jbyteArray result = JvNewByteArray (blen);
  101. memcpy (elements (result), bytes, blen);
  102. return result;
  103. }