OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / java-net / java_net_VMNetworkInterface.c
1 /* VMNetworkInterface.c - Native methods for NetworkInterface class
2    Copyright (C) 2003, 2005, 2006  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10  
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /* HAVE_CONFIG_H */
41
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #ifdef HAVE_IFADDRS_H
45 #include <ifaddrs.h>
46 #endif
47 #include <netinet/in.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52
53 #include <jni.h>
54 #include <jcl.h>
55
56 #include "java_net_VMNetworkInterface.h"
57
58
59 static jmethodID java_net_VMNetworkInterface_init;
60 static jmethodID java_net_VMNetworkInterface_addAddress;
61
62 /*
63  * Initialize our static method ID's.
64  *
65  * Class:     java_net_VMNetworkInterface
66  * Method:    initIds
67  * Signature: ()V
68  */
69 JNIEXPORT void JNICALL
70 Java_java_net_VMNetworkInterface_initIds (JNIEnv *env, jclass clazz)
71 {
72   java_net_VMNetworkInterface_init =
73     (*env)->GetMethodID (env, clazz, "<init>", "(Ljava/lang/String;)V");
74   if (java_net_VMNetworkInterface_init == NULL)
75     {
76       if (!(*env)->ExceptionCheck (env))
77         JCL_ThrowException (env, "java/lang/NoSuchMethodError",
78                             "VMNetworkinterface.addAddress");
79       return;
80     }
81   java_net_VMNetworkInterface_addAddress =
82     (*env)->GetMethodID (env, clazz, "addAddress", "(Ljava/nio/ByteBuffer;)V");
83   if (java_net_VMNetworkInterface_addAddress == NULL)
84     {
85       if (!(*env)->ExceptionCheck (env))
86         JCL_ThrowException (env, "java/lang/NoSuchMethodError",
87                             "VMNetworkinterface.addAddress");
88     }
89 }
90
91 struct netif_entry
92 {
93   char *name;
94   jobject netif;
95   int numaddrs;
96   struct netif_entry *next;
97 };
98
99 static void
100 free_netif_list (JNIEnv *env, struct netif_entry *list)
101 {
102   while (list != NULL)
103     {
104       struct netif_entry *e = list->next;
105       JCL_free (env, list);
106       list = e;
107     }
108 }
109
110 /*
111  * Returns all local network interfaces as an array.
112  */
113 JNIEXPORT jobjectArray JNICALL
114 Java_java_net_VMNetworkInterface_getVMInterfaces (JNIEnv * env, jclass clazz)
115 {
116 #if defined (HAVE_IFADDRS_H) && defined (HAVE_GETIFADDRS)
117   struct ifaddrs *ifaddrs, *i;
118   struct netif_entry *iflist = NULL, *e;
119   jobjectArray netifs;
120   int numifs = 0;
121   int k;
122
123   if (getifaddrs (&ifaddrs) == -1)
124     {
125       JCL_ThrowException (env, "java/net/SocketException", strerror (errno));
126       return NULL;
127     }
128
129   for (i = ifaddrs; i != NULL; i = i->ifa_next)
130     {
131       if (iflist == NULL)
132         {
133           iflist = JCL_malloc (env, sizeof (struct netif_entry));
134           if (iflist == NULL)
135             {
136               freeifaddrs (ifaddrs);
137               return NULL;
138             }
139           iflist->name = i->ifa_name;
140           iflist->numaddrs = 0;
141           iflist->next = NULL;
142           iflist->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
143                                              (*env)->NewStringUTF (env, i->ifa_name));
144           if (iflist->netif == NULL)
145             {
146               freeifaddrs (ifaddrs);
147               JCL_free (env, iflist);
148               return NULL;
149             }
150           e = iflist;
151         }
152       else
153         {
154           struct netif_entry *p = NULL;
155           for (e = iflist; e != NULL; e = e->next)
156             {
157               if (strcmp (e->name, i->ifa_name) == 0)
158                 break;
159               p = e;
160             }
161
162           if (e == NULL)
163             {
164               p->next = (struct netif_entry *) JCL_malloc (env, sizeof (struct netif_entry));
165               if (p->next == NULL)
166                 {
167                   free_netif_list (env, iflist);
168                   freeifaddrs (ifaddrs);
169                   return NULL;
170                 }
171               e = p->next;
172               e->name = i->ifa_name;
173               e->numaddrs = 0;
174               e->next = NULL;
175               e->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
176                                             (*env)->NewStringUTF (env, i->ifa_name));
177               if (e->netif == NULL)
178                 {
179                   free_netif_list (env, iflist);
180                   freeifaddrs (ifaddrs);
181                   return NULL;
182                 }
183             }
184         }
185
186       if (i->ifa_addr == NULL)
187         continue;
188
189       if (i->ifa_addr->sa_family == AF_INET)
190         {
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,
194                                   buffer);
195           if ((*env)->ExceptionCheck (env))
196             {
197               free_netif_list (env, iflist);
198               freeifaddrs (ifaddrs);
199               return NULL;
200             }
201           (*env)->DeleteLocalRef (env, buffer);
202           e->numaddrs++;
203         }
204 #ifdef HAVE_INET6
205       else if (i->ifa_addr->sa_family == AF_INET6)
206         {
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,
210                                   buffer);
211           if ((*env)->ExceptionCheck (env))
212             {
213               free_netif_list (env, iflist);
214               freeifaddrs (ifaddrs);
215               return NULL;
216             }
217           (*env)->DeleteLocalRef (env, buffer);
218           e->numaddrs++;
219         }
220 #endif /* HAVE_INET6 */
221     }
222
223   /* Count how many interfaces we have that have addresses. */
224   for (e = iflist; e != NULL; e = e->next)
225     {
226       if (e->numaddrs != 0)
227         numifs++;
228     }
229
230   netifs = (*env)->NewObjectArray (env, numifs, clazz, NULL);
231   k = 0;
232   for (e = iflist; e != NULL && k < numifs; e = e->next)
233     {
234       if (e->numaddrs != 0)
235         {
236           (*env)->SetObjectArrayElement (env, netifs, k, e->netif);
237           (*env)->DeleteLocalRef (env, e->netif);
238           k++;
239         }
240     }
241
242   free_netif_list (env, iflist);
243   freeifaddrs (ifaddrs);
244   return netifs;
245 #else
246   JCL_ThrowException (env, "java/net/SocketException", "getifaddrs not supported");
247   return NULL;
248 #endif /* HAVE_GETIFADDRS */
249 }
250
251 /* end of file */