natNetworkInterface.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // natNetworkInterface.cc
  2. /* Copyright (C) 2002 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #include <config.h>
  8. #include <platform.h>
  9. #ifdef WIN32
  10. #include <windows.h>
  11. #include <winsock.h>
  12. #undef STRICT
  13. #else /* WIN32 */
  14. #ifdef HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #ifdef HAVE_NETINET_IN_H
  23. #include <netinet/in.h>
  24. #endif
  25. #ifdef HAVE_ARPA_INET_H
  26. #include <arpa/inet.h>
  27. #endif
  28. #ifdef HAVE_NETDB_H
  29. #include <netdb.h>
  30. #endif
  31. #ifdef HAVE_SYS_IOCTL_H
  32. #define BSD_COMP /* Get FIONREAD on Solaris2. */
  33. #include <sys/ioctl.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #endif /* WIN32 */
  39. #include <gcj/cni.h>
  40. #include <jvm.h>
  41. #include <java/net/NetworkInterface.h>
  42. #include <java/net/Inet4Address.h>
  43. #include <java/net/SocketException.h>
  44. #include <java/util/Vector.h>
  45. #ifdef DISABLE_JAVA_NET
  46. ::java::util::Vector*
  47. java::net::NetworkInterface::getRealNetworkInterfaces ()
  48. {
  49. ::java::util::Vector* ht = new ::java::util::Vector();
  50. return ht;
  51. }
  52. #else /* DISABLE_JAVA_NET */
  53. ::java::util::Vector*
  54. java::net::NetworkInterface::getRealNetworkInterfaces ()
  55. {
  56. #ifdef WIN32
  57. throw new ::java::net::SocketException;
  58. #else
  59. int fd;
  60. int num_interfaces = 0;
  61. struct ifconf if_data;
  62. struct ifreq* if_record;
  63. ::java::util::Vector* ht = new ::java::util::Vector ();
  64. if_data.ifc_len = 0;
  65. if_data.ifc_buf = NULL;
  66. // Open a (random) socket to have a file descriptor for the ioctl calls.
  67. fd = _Jv_socket (PF_INET, SOCK_DGRAM, htons (IPPROTO_IP));
  68. if (fd < 0)
  69. throw new ::java::net::SocketException;
  70. // Get all interfaces. If not enough buffers are available try it
  71. // with a bigger buffer size.
  72. do
  73. {
  74. num_interfaces += 16;
  75. if_data.ifc_len = sizeof (struct ifreq) * num_interfaces;
  76. if_data.ifc_buf =
  77. (char*) _Jv_Realloc (if_data.ifc_buf, if_data.ifc_len);
  78. // Try to get all local interfaces.
  79. if (::ioctl (fd, SIOCGIFCONF, &if_data) < 0)
  80. throw new java::net::SocketException;
  81. }
  82. while (if_data.ifc_len >= (sizeof (struct ifreq) * num_interfaces));
  83. // Get addresses of all interfaces.
  84. if_record = if_data.ifc_req;
  85. for (int n = 0; n < if_data.ifc_len; n += sizeof (struct ifreq))
  86. {
  87. struct ifreq ifr;
  88. memset (&ifr, 0, sizeof (ifr));
  89. strcpy (ifr.ifr_name, if_record->ifr_name);
  90. // Try to get the IPv4-address of the local interface
  91. if (::ioctl (fd, SIOCGIFADDR, &ifr) < 0)
  92. throw new java::net::SocketException;
  93. int len = 4;
  94. struct sockaddr_in sa = *((sockaddr_in*) &(ifr.ifr_addr));
  95. jbyteArray baddr = JvNewByteArray (len);
  96. memcpy (elements (baddr), &(sa.sin_addr), len);
  97. jstring if_name = JvNewStringLatin1 (if_record->ifr_name);
  98. Inet4Address* address =
  99. new java::net::Inet4Address (baddr, JvNewStringLatin1 (""));
  100. ht->add (new NetworkInterface (if_name, address));
  101. if_record++;
  102. }
  103. #ifdef HAVE_INET6
  104. // FIXME: read /proc/net/if_inet6 (on Linux 2.4)
  105. #endif
  106. _Jv_Free (if_data.ifc_buf);
  107. if (fd >= 0)
  108. _Jv_close (fd);
  109. return ht;
  110. #endif /* WIN32 */
  111. }
  112. #endif // DISABLE_JAVA_NET //