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

/libjava/classpath/native/jni/java-net/java_net_VMNetworkInterface.c

https://bitbucket.org/pizzafactory/pf-gcc
C | 399 lines | 304 code | 46 blank | 49 comment | 54 complexity | 1f5593d6da9a914bfb9e97e09bee7f64 MD5 | raw file
  1. /* VMNetworkInterface.c - Native methods for NetworkInterface class
  2. Copyright (C) 2003, 2005, 2006, 2008 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif /* HAVE_CONFIG_H */
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #ifdef HAVE_IFADDRS_H
  37. #include <ifaddrs.h>
  38. #endif
  39. #include <netinet/in.h>
  40. #include <errno.h>
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <net/if.h>
  45. #include <sys/ioctl.h>
  46. /* Required on Solaris. */
  47. #include <unistd.h>
  48. #ifdef HAVE_SYS_SOCKIO_H
  49. # include <sys/sockio.h>
  50. #endif
  51. #include <jni.h>
  52. #include <jcl.h>
  53. #include <cpnative.h>
  54. #include <cpnet.h>
  55. #include "java_net_VMNetworkInterface.h"
  56. int iff_flags(JNIEnv *, jstring, jint *);
  57. static jmethodID java_net_VMNetworkInterface_init;
  58. static jmethodID java_net_VMNetworkInterface_addAddress;
  59. /*
  60. * Initialize our static method ID's.
  61. *
  62. * Class: java_net_VMNetworkInterface
  63. * Method: initIds
  64. * Signature: ()V
  65. */
  66. JNIEXPORT void JNICALL
  67. Java_java_net_VMNetworkInterface_initIds (JNIEnv *env, jclass clazz)
  68. {
  69. java_net_VMNetworkInterface_init =
  70. (*env)->GetMethodID (env, clazz, "<init>", "(Ljava/lang/String;)V");
  71. if (java_net_VMNetworkInterface_init == NULL)
  72. {
  73. if (!(*env)->ExceptionCheck (env))
  74. JCL_ThrowException (env, "java/lang/NoSuchMethodError",
  75. "VMNetworkinterface.addAddress");
  76. return;
  77. }
  78. java_net_VMNetworkInterface_addAddress =
  79. (*env)->GetMethodID (env, clazz, "addAddress", "(Ljava/nio/ByteBuffer;)V");
  80. if (java_net_VMNetworkInterface_addAddress == NULL)
  81. {
  82. if (!(*env)->ExceptionCheck (env))
  83. JCL_ThrowException (env, "java/lang/NoSuchMethodError",
  84. "VMNetworkinterface.addAddress");
  85. }
  86. }
  87. struct netif_entry
  88. {
  89. char *name;
  90. jobject netif;
  91. int numaddrs;
  92. struct netif_entry *next;
  93. };
  94. #if defined (HAVE_IFADDRS_H) && defined (HAVE_GETIFADDRS)
  95. static void
  96. free_netif_list (JNIEnv *env, struct netif_entry *list)
  97. {
  98. while (list != NULL)
  99. {
  100. struct netif_entry *e = list->next;
  101. JCL_free (env, list);
  102. list = e;
  103. }
  104. }
  105. #endif
  106. /*
  107. * Returns all local network interfaces as an array.
  108. */
  109. JNIEXPORT jobjectArray JNICALL
  110. Java_java_net_VMNetworkInterface_getVMInterfaces (JNIEnv * env,
  111. jclass clazz UNUSED)
  112. {
  113. #if defined (HAVE_IFADDRS_H) && defined (HAVE_GETIFADDRS)
  114. struct ifaddrs *ifaddrs, *i;
  115. struct netif_entry *iflist = NULL, *e;
  116. jobjectArray netifs;
  117. int numifs = 0;
  118. int k;
  119. if (getifaddrs (&ifaddrs) == -1)
  120. {
  121. JCL_ThrowException (env, "java/net/SocketException", strerror (errno));
  122. return NULL;
  123. }
  124. for (i = ifaddrs; i != NULL; i = i->ifa_next)
  125. {
  126. if (iflist == NULL)
  127. {
  128. iflist = JCL_malloc (env, sizeof (struct netif_entry));
  129. if (iflist == NULL)
  130. {
  131. freeifaddrs (ifaddrs);
  132. return NULL;
  133. }
  134. iflist->name = i->ifa_name;
  135. iflist->numaddrs = 0;
  136. iflist->next = NULL;
  137. iflist->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
  138. (*env)->NewStringUTF (env, i->ifa_name));
  139. if (iflist->netif == NULL)
  140. {
  141. freeifaddrs (ifaddrs);
  142. JCL_free (env, iflist);
  143. return NULL;
  144. }
  145. e = iflist;
  146. }
  147. else
  148. {
  149. struct netif_entry *p = NULL;
  150. for (e = iflist; e != NULL; e = e->next)
  151. {
  152. if (strcmp (e->name, i->ifa_name) == 0)
  153. break;
  154. p = e;
  155. }
  156. if (e == NULL)
  157. {
  158. p->next = (struct netif_entry *) JCL_malloc (env, sizeof (struct netif_entry));
  159. if (p->next == NULL)
  160. {
  161. free_netif_list (env, iflist);
  162. freeifaddrs (ifaddrs);
  163. return NULL;
  164. }
  165. e = p->next;
  166. e->name = i->ifa_name;
  167. e->numaddrs = 0;
  168. e->next = NULL;
  169. e->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
  170. (*env)->NewStringUTF (env, i->ifa_name));
  171. if (e->netif == NULL)
  172. {
  173. free_netif_list (env, iflist);
  174. freeifaddrs (ifaddrs);
  175. return NULL;
  176. }
  177. }
  178. }
  179. if (i->ifa_addr == NULL)
  180. continue;
  181. if (i->ifa_addr->sa_family == AF_INET)
  182. {
  183. struct sockaddr_in *sin = (struct sockaddr_in *) i->ifa_addr;
  184. jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin_addr.s_addr), 4);
  185. (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
  186. buffer);
  187. if ((*env)->ExceptionCheck (env))
  188. {
  189. free_netif_list (env, iflist);
  190. freeifaddrs (ifaddrs);
  191. return NULL;
  192. }
  193. (*env)->DeleteLocalRef (env, buffer);
  194. e->numaddrs++;
  195. }
  196. #ifdef HAVE_INET6
  197. else if (i->ifa_addr->sa_family == AF_INET6)
  198. {
  199. struct sockaddr_in6 *sin = (struct sockaddr_in6 *) i->ifa_addr;
  200. jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin6_addr.s6_addr), 16);
  201. (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
  202. buffer);
  203. if ((*env)->ExceptionCheck (env))
  204. {
  205. free_netif_list (env, iflist);
  206. freeifaddrs (ifaddrs);
  207. return NULL;
  208. }
  209. (*env)->DeleteLocalRef (env, buffer);
  210. e->numaddrs++;
  211. }
  212. #endif /* HAVE_INET6 */
  213. }
  214. /* Count how many interfaces we have that have addresses. */
  215. for (e = iflist; e != NULL; e = e->next)
  216. {
  217. if (e->numaddrs != 0)
  218. numifs++;
  219. }
  220. netifs = (*env)->NewObjectArray (env, numifs, clazz, NULL);
  221. k = 0;
  222. for (e = iflist; e != NULL && k < numifs; e = e->next)
  223. {
  224. if (e->numaddrs != 0)
  225. {
  226. (*env)->SetObjectArrayElement (env, netifs, k, e->netif);
  227. (*env)->DeleteLocalRef (env, e->netif);
  228. k++;
  229. }
  230. }
  231. free_netif_list (env, iflist);
  232. freeifaddrs (ifaddrs);
  233. return netifs;
  234. #else
  235. JCL_ThrowException (env, "java/net/SocketException", "getifaddrs not supported");
  236. return NULL;
  237. #endif /* HAVE_IFADDRS_H && HAVE_GETIFADDRS */
  238. }
  239. int iff_flags(JNIEnv *env, jstring name, jint *flags)
  240. {
  241. struct ifreq iff;
  242. const char *iff_name;
  243. jint socket;
  244. int error, retval;
  245. if ((error = cpnet_openSocketDatagram(env, &socket, AF_INET)))
  246. {
  247. return error;
  248. }
  249. iff_name = JCL_jstring_to_cstring(env, name);
  250. memset(&iff, 0, sizeof(iff));
  251. strcpy(iff.ifr_name, iff_name);
  252. if (ioctl(socket, SIOCGIFFLAGS, &iff) >= 0)
  253. {
  254. *flags = (jint) iff.ifr_flags;
  255. retval = 0;
  256. }
  257. else
  258. {
  259. retval = errno;
  260. }
  261. cpnet_close(env, socket);
  262. JCL_free_cstring(env, name, iff_name);
  263. return retval;
  264. }
  265. JNIEXPORT jboolean JNICALL
  266. Java_java_net_VMNetworkInterface_isUp (JNIEnv *env, jclass class UNUSED,
  267. jstring name)
  268. {
  269. jint flags;
  270. int error;
  271. jboolean retval;
  272. if ((error = iff_flags(env, name, &flags)))
  273. {
  274. JCL_ThrowException(env, "java/net/SocketException",
  275. cpnative_getErrorString(error));
  276. retval = JNI_FALSE;
  277. }
  278. else
  279. {
  280. retval = (flags & (IFF_UP | IFF_RUNNING))
  281. ? JNI_TRUE
  282. : JNI_FALSE;
  283. }
  284. return retval;
  285. }
  286. JNIEXPORT jboolean JNICALL
  287. Java_java_net_VMNetworkInterface_isPointToPoint (JNIEnv *env,
  288. jclass class UNUSED,
  289. jstring name)
  290. {
  291. jint flags;
  292. int error;
  293. jboolean retval;
  294. if ((error = iff_flags(env, name, &flags)))
  295. {
  296. JCL_ThrowException(env, "java/net/SocketException",
  297. cpnative_getErrorString(error));
  298. retval = JNI_FALSE;
  299. }
  300. else
  301. {
  302. retval = (flags & IFF_POINTOPOINT) ? JNI_TRUE
  303. : JNI_FALSE;
  304. }
  305. return retval;
  306. }
  307. JNIEXPORT jboolean JNICALL
  308. Java_java_net_VMNetworkInterface_isLoopback (JNIEnv *env,
  309. jclass class UNUSED,
  310. jstring name)
  311. {
  312. jint flags;
  313. int error;
  314. jboolean retval;
  315. if ((error = iff_flags(env, name, &flags)))
  316. {
  317. JCL_ThrowException(env, "java/net/SocketException",
  318. cpnative_getErrorString(error));
  319. retval = JNI_FALSE;
  320. }
  321. else
  322. {
  323. retval = (flags & IFF_LOOPBACK) ? JNI_TRUE : JNI_FALSE;
  324. }
  325. return retval;
  326. }
  327. JNIEXPORT jboolean JNICALL
  328. Java_java_net_VMNetworkInterface_supportsMulticast (JNIEnv *env,
  329. jclass class UNUSED,
  330. jstring name)
  331. {
  332. jint flags;
  333. int error;
  334. jboolean retval;
  335. if ((error = iff_flags(env, name, &flags)))
  336. {
  337. JCL_ThrowException(env, "java/net/SocketException",
  338. cpnative_getErrorString(error));
  339. retval = JNI_FALSE;
  340. }
  341. else
  342. {
  343. retval = (flags & IFF_MULTICAST) ? JNI_TRUE : JNI_FALSE;
  344. }
  345. return retval;
  346. }
  347. /* end of file */