1 /* VMNetworkInterface.c - Native methods for NetworkInterface class
2 Copyright (C) 2003, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 #endif /* HAVE_CONFIG_H */
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 #include <netinet/in.h>
56 #include "java_net_VMNetworkInterface.h"
59 static jmethodID java_net_VMNetworkInterface_init;
60 static jmethodID java_net_VMNetworkInterface_addAddress;
63 * Initialize our static method ID's.
65 * Class: java_net_VMNetworkInterface
69 JNIEXPORT void JNICALL
70 Java_java_net_VMNetworkInterface_initIds (JNIEnv *env, jclass clazz)
72 java_net_VMNetworkInterface_init =
73 (*env)->GetMethodID (env, clazz, "<init>", "(Ljava/lang/String;)V");
74 if (java_net_VMNetworkInterface_init == NULL)
76 if (!(*env)->ExceptionCheck (env))
77 JCL_ThrowException (env, "java/lang/NoSuchMethodError",
78 "VMNetworkinterface.addAddress");
81 java_net_VMNetworkInterface_addAddress =
82 (*env)->GetMethodID (env, clazz, "addAddress", "(Ljava/nio/ByteBuffer;)V");
83 if (java_net_VMNetworkInterface_addAddress == NULL)
85 if (!(*env)->ExceptionCheck (env))
86 JCL_ThrowException (env, "java/lang/NoSuchMethodError",
87 "VMNetworkinterface.addAddress");
96 struct netif_entry *next;
100 free_netif_list (JNIEnv *env, struct netif_entry *list)
104 struct netif_entry *e = list->next;
105 JCL_free (env, list);
111 * Returns all local network interfaces as an array.
113 JNIEXPORT jobjectArray JNICALL
114 Java_java_net_VMNetworkInterface_getVMInterfaces (JNIEnv * env, jclass clazz)
116 #if defined (HAVE_IFADDRS_H) && defined (HAVE_GETIFADDRS)
117 struct ifaddrs *ifaddrs, *i;
118 struct netif_entry *iflist = NULL, *e;
123 if (getifaddrs (&ifaddrs) == -1)
125 JCL_ThrowException (env, "java/net/SocketException", strerror (errno));
129 for (i = ifaddrs; i != NULL; i = i->ifa_next)
133 iflist = JCL_malloc (env, sizeof (struct netif_entry));
136 freeifaddrs (ifaddrs);
139 iflist->name = i->ifa_name;
140 iflist->numaddrs = 0;
142 iflist->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
143 (*env)->NewStringUTF (env, i->ifa_name));
144 if (iflist->netif == NULL)
146 freeifaddrs (ifaddrs);
147 JCL_free (env, iflist);
154 struct netif_entry *p = NULL;
155 for (e = iflist; e != NULL; e = e->next)
157 if (strcmp (e->name, i->ifa_name) == 0)
164 p->next = (struct netif_entry *) JCL_malloc (env, sizeof (struct netif_entry));
167 free_netif_list (env, iflist);
168 freeifaddrs (ifaddrs);
172 e->name = i->ifa_name;
175 e->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
176 (*env)->NewStringUTF (env, i->ifa_name));
177 if (e->netif == NULL)
179 free_netif_list (env, iflist);
180 freeifaddrs (ifaddrs);
186 if (i->ifa_addr == NULL)
189 if (i->ifa_addr->sa_family == AF_INET)
191 struct sockaddr_in *sin = (struct sockaddr_in *) i->ifa_addr;
192 jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin_addr.s_addr), 4);
193 (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
195 if ((*env)->ExceptionCheck (env))
197 free_netif_list (env, iflist);
198 freeifaddrs (ifaddrs);
201 (*env)->DeleteLocalRef (env, buffer);
205 else if (i->ifa_addr->sa_family == AF_INET6)
207 struct sockaddr_in6 *sin = (struct sockaddr_in6 *) i->ifa_addr;
208 jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin6_addr.s6_addr), 16);
209 (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
211 if ((*env)->ExceptionCheck (env))
213 free_netif_list (env, iflist);
214 freeifaddrs (ifaddrs);
217 (*env)->DeleteLocalRef (env, buffer);
220 #endif /* HAVE_INET6 */
223 /* Count how many interfaces we have that have addresses. */
224 for (e = iflist; e != NULL; e = e->next)
226 if (e->numaddrs != 0)
230 netifs = (*env)->NewObjectArray (env, numifs, clazz, NULL);
232 for (e = iflist; e != NULL && k < numifs; e = e->next)
234 if (e->numaddrs != 0)
236 (*env)->SetObjectArrayElement (env, netifs, k, e->netif);
237 (*env)->DeleteLocalRef (env, e->netif);
242 free_netif_list (env, iflist);
243 freeifaddrs (ifaddrs);
246 JCL_ThrowException (env, "java/net/SocketException", "getifaddrs not supported");
248 #endif /* HAVE_GETIFADDRS */