annotate src/solaris/native/java/net/NetworkInterface.c @ 5888:7ca8a40795d8

8000724: Improve networking serialization Summary: delegate InetAddress fields to a holder object Reviewed-by: alanb, chegar
author michaelm
date Wed, 13 Feb 2013 10:47:15 +0000
parents 12819477ce5a
children eefd9678efbd
rev   line source
duke@0 1 /*
ohair@3909 2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
dsamersoff@2545 26
duke@0 27 #include <errno.h>
duke@0 28 #include <strings.h>
michaelm@4632 29 #if defined(_ALLBSD_SOURCE) && defined(__OpenBSD__)
michaelm@4632 30 #include <sys/types.h>
michaelm@4632 31 #endif
duke@0 32 #include <netinet/in.h>
duke@0 33 #include <stdlib.h>
duke@0 34 #include <string.h>
duke@0 35 #include <sys/types.h>
duke@0 36 #include <sys/socket.h>
duke@0 37 #include <arpa/inet.h>
duke@0 38 #include <net/if.h>
duke@0 39 #include <net/if_arp.h>
dsamersoff@2545 40
duke@0 41 #ifdef __solaris__
duke@0 42 #include <sys/dlpi.h>
duke@0 43 #include <fcntl.h>
duke@0 44 #include <stropts.h>
dsamersoff@2545 45 #include <sys/sockio.h>
duke@0 46 #endif
dsamersoff@2545 47
duke@0 48 #ifdef __linux__
duke@0 49 #include <sys/ioctl.h>
duke@0 50 #include <bits/ioctls.h>
duke@0 51 #include <sys/utsname.h>
duke@0 52 #include <stdio.h>
duke@0 53 #endif
duke@0 54
duke@0 55 #ifdef __linux__
duke@0 56 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
duke@0 57 #endif
duke@0 58
michaelm@4632 59 #if defined(_ALLBSD_SOURCE)
michaelm@4632 60 #include <sys/param.h>
michaelm@4632 61 #include <sys/ioctl.h>
michaelm@4632 62 #include <sys/sockio.h>
michaelm@4632 63 #if defined(__APPLE__)
michaelm@4632 64 #include <net/ethernet.h>
michaelm@4632 65 #include <net/if_var.h>
michaelm@4632 66 #include <net/if_dl.h>
michaelm@4632 67 #include <netinet/in_var.h>
michaelm@4632 68 #include <ifaddrs.h>
michaelm@4632 69 #endif
michaelm@4632 70 #endif
michaelm@4632 71
duke@0 72 #include "jvm.h"
duke@0 73 #include "jni_util.h"
duke@0 74 #include "net_util.h"
duke@0 75
duke@0 76 typedef struct _netaddr {
duke@0 77 struct sockaddr *addr;
duke@0 78 struct sockaddr *brdcast;
duke@0 79 short mask;
duke@0 80 int family; /* to make searches simple */
duke@0 81 struct _netaddr *next;
duke@0 82 } netaddr;
duke@0 83
duke@0 84 typedef struct _netif {
duke@0 85 char *name;
duke@0 86 int index;
duke@0 87 char virtual;
duke@0 88 netaddr *addr;
duke@0 89 struct _netif *childs;
duke@0 90 struct _netif *next;
duke@0 91 } netif;
duke@0 92
duke@0 93 /************************************************************************
duke@0 94 * NetworkInterface
duke@0 95 */
duke@0 96
duke@0 97 #include "java_net_NetworkInterface.h"
duke@0 98
duke@0 99 /************************************************************************
duke@0 100 * NetworkInterface
duke@0 101 */
duke@0 102 jclass ni_class;
duke@0 103 jfieldID ni_nameID;
duke@0 104 jfieldID ni_indexID;
duke@0 105 jfieldID ni_descID;
duke@0 106 jfieldID ni_addrsID;
duke@0 107 jfieldID ni_bindsID;
duke@0 108 jfieldID ni_virutalID;
duke@0 109 jfieldID ni_childsID;
duke@0 110 jfieldID ni_parentID;
michaelm@4638 111 jfieldID ni_defaultIndexID;
duke@0 112 jmethodID ni_ctrID;
duke@0 113
duke@0 114 static jclass ni_iacls;
duke@0 115 static jclass ni_ia4cls;
duke@0 116 static jclass ni_ia6cls;
duke@0 117 static jclass ni_ibcls;
duke@0 118 static jmethodID ni_ia4ctrID;
duke@0 119 static jmethodID ni_ia6ctrID;
duke@0 120 static jmethodID ni_ibctrID;
duke@0 121 static jfieldID ni_ia6ipaddressID;
duke@0 122 static jfieldID ni_ibaddressID;
duke@0 123 static jfieldID ni_ib4broadcastID;
duke@0 124 static jfieldID ni_ib4maskID;
duke@0 125
dsamersoff@2545 126 /** Private methods declarations **/
duke@0 127 static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
dsamersoff@2545 128 static int getFlags0(JNIEnv *env, jstring ifname);
duke@0 129
dsamersoff@2545 130 static netif *enumInterfaces(JNIEnv *env);
dsamersoff@2545 131 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
dsamersoff@2545 132
duke@0 133 #ifdef AF_INET6
dsamersoff@2545 134 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
duke@0 135 #endif
duke@0 136
dsamersoff@2545 137 static netif *addif(JNIEnv *env, int sock, const char * if_name, netif *ifs, struct sockaddr* ifr_addrP, int family, short prefix);
dsamersoff@2545 138 static void freeif(netif *ifs);
dsamersoff@2545 139
dsamersoff@2545 140 static int openSocket(JNIEnv *env, int proto);
dsamersoff@2545 141 static int openSocketWithFallback(JNIEnv *env, const char *ifname);
dsamersoff@2545 142
dsamersoff@2545 143
dsamersoff@2545 144 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *name, struct sockaddr *brdcast_store);
dsamersoff@2545 145 static short getSubnet(JNIEnv *env, int sock, const char *ifname);
chegar@2578 146 static int getIndex(int sock, const char *ifname);
dsamersoff@2545 147
chegar@2626 148 static int getFlags(int sock, const char *ifname);
dsamersoff@2545 149 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf);
dsamersoff@2545 150 static int getMTU(JNIEnv *env, int sock, const char *ifname);
dsamersoff@2545 151
dsamersoff@2545 152
dsamersoff@2545 153
dsamersoff@2545 154 #ifdef __solaris__
dsamersoff@2545 155 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family);
dsamersoff@2545 156 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf);
chegar@4501 157
chegar@4501 158 #ifndef SIOCGLIFHWADDR
chegar@4501 159 #define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
dsamersoff@2545 160 #endif
dsamersoff@2545 161
chegar@4501 162 #endif
dsamersoff@2545 163
dsamersoff@2545 164 /******************* Java entry points *****************************/
duke@0 165
duke@0 166 /*
duke@0 167 * Class: java_net_NetworkInterface
duke@0 168 * Method: init
duke@0 169 * Signature: ()V
duke@0 170 */
duke@0 171 JNIEXPORT void JNICALL
duke@0 172 Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
duke@0 173 ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
duke@0 174 ni_class = (*env)->NewGlobalRef(env, ni_class);
duke@0 175 ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
duke@0 176 ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
duke@0 177 ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
duke@0 178 ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
duke@0 179 ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
duke@0 180 ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
duke@0 181 ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
duke@0 182 ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
duke@0 183 ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
duke@0 184
duke@0 185 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
duke@0 186 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
duke@0 187 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
duke@0 188 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
duke@0 189 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
duke@0 190 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
duke@0 191 ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
duke@0 192 ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
duke@0 193 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
duke@0 194 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
duke@0 195 ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
duke@0 196 ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
duke@0 197 ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
duke@0 198 ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
duke@0 199 ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
michaelm@4638 200 ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex", "I");
duke@0 201 }
duke@0 202
duke@0 203
duke@0 204 /*
duke@0 205 * Class: java_net_NetworkInterface
duke@0 206 * Method: getByName0
duke@0 207 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
duke@0 208 */
duke@0 209 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
duke@0 210 (JNIEnv *env, jclass cls, jstring name) {
duke@0 211
duke@0 212 netif *ifs, *curr;
duke@0 213 jboolean isCopy;
dsamersoff@2545 214 const char* name_utf;
duke@0 215 jobject obj = NULL;
duke@0 216
duke@0 217 ifs = enumInterfaces(env);
duke@0 218 if (ifs == NULL) {
duke@0 219 return NULL;
duke@0 220 }
duke@0 221
dsamersoff@2545 222 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 223
duke@0 224 /*
duke@0 225 * Search the list of interface based on name
duke@0 226 */
duke@0 227 curr = ifs;
duke@0 228 while (curr != NULL) {
duke@0 229 if (strcmp(name_utf, curr->name) == 0) {
duke@0 230 break;
duke@0 231 }
duke@0 232 curr = curr->next;
duke@0 233 }
duke@0 234
duke@0 235 /* if found create a NetworkInterface */
duke@0 236 if (curr != NULL) {;
duke@0 237 obj = createNetworkInterface(env, curr);
duke@0 238 }
duke@0 239
duke@0 240 /* release the UTF string and interface list */
duke@0 241 (*env)->ReleaseStringUTFChars(env, name, name_utf);
duke@0 242 freeif(ifs);
duke@0 243
duke@0 244 return obj;
duke@0 245 }
duke@0 246
duke@0 247
duke@0 248 /*
duke@0 249 * Class: java_net_NetworkInterface
jccollet@509 250 * Method: getByIndex0
duke@0 251 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
duke@0 252 */
jccollet@509 253 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
duke@0 254 (JNIEnv *env, jclass cls, jint index) {
duke@0 255
duke@0 256 netif *ifs, *curr;
duke@0 257 jobject obj = NULL;
duke@0 258
duke@0 259 if (index <= 0) {
duke@0 260 return NULL;
duke@0 261 }
duke@0 262
duke@0 263 ifs = enumInterfaces(env);
duke@0 264 if (ifs == NULL) {
duke@0 265 return NULL;
duke@0 266 }
duke@0 267
duke@0 268 /*
duke@0 269 * Search the list of interface based on index
duke@0 270 */
duke@0 271 curr = ifs;
duke@0 272 while (curr != NULL) {
duke@0 273 if (index == curr->index) {
duke@0 274 break;
duke@0 275 }
duke@0 276 curr = curr->next;
duke@0 277 }
duke@0 278
duke@0 279 /* if found create a NetworkInterface */
duke@0 280 if (curr != NULL) {;
duke@0 281 obj = createNetworkInterface(env, curr);
duke@0 282 }
duke@0 283
duke@0 284 freeif(ifs);
duke@0 285 return obj;
duke@0 286 }
duke@0 287
duke@0 288 /*
duke@0 289 * Class: java_net_NetworkInterface
duke@0 290 * Method: getByInetAddress0
duke@0 291 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
duke@0 292 */
duke@0 293 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
duke@0 294 (JNIEnv *env, jclass cls, jobject iaObj) {
duke@0 295
duke@0 296 netif *ifs, *curr;
dsamersoff@2545 297
chegar@2112 298 #ifdef AF_INET6
michaelm@5888 299 int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
chegar@2112 300 #else
dsamersoff@2545 301 int family = AF_INET;
chegar@2112 302 #endif
dsamersoff@2545 303
duke@0 304 jobject obj = NULL;
duke@0 305 jboolean match = JNI_FALSE;
duke@0 306
duke@0 307 ifs = enumInterfaces(env);
duke@0 308 if (ifs == NULL) {
duke@0 309 return NULL;
duke@0 310 }
duke@0 311
duke@0 312 curr = ifs;
duke@0 313 while (curr != NULL) {
duke@0 314 netaddr *addrP = curr->addr;
duke@0 315
duke@0 316 /*
duke@0 317 * Iterate through each address on the interface
duke@0 318 */
duke@0 319 while (addrP != NULL) {
duke@0 320
duke@0 321 if (family == addrP->family) {
duke@0 322 if (family == AF_INET) {
duke@0 323 int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
michaelm@5888 324 int address2 = getInetAddress_addr(env, iaObj);
duke@0 325
duke@0 326 if (address1 == address2) {
duke@0 327 match = JNI_TRUE;
duke@0 328 break;
duke@0 329 }
duke@0 330 }
duke@0 331
duke@0 332 #ifdef AF_INET6
duke@0 333 if (family == AF_INET6) {
duke@0 334 jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
duke@0 335 jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ni_ia6ipaddressID);
duke@0 336 jbyte caddr[16];
duke@0 337 int i;
duke@0 338
duke@0 339 (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
duke@0 340 i = 0;
duke@0 341 while (i < 16) {
duke@0 342 if (caddr[i] != bytes[i]) {
duke@0 343 break;
duke@0 344 }
duke@0 345 i++;
duke@0 346 }
duke@0 347 if (i >= 16) {
duke@0 348 match = JNI_TRUE;
duke@0 349 break;
duke@0 350 }
duke@0 351 }
duke@0 352 #endif
duke@0 353
duke@0 354 }
duke@0 355
duke@0 356 if (match) {
duke@0 357 break;
duke@0 358 }
duke@0 359 addrP = addrP->next;
duke@0 360 }
duke@0 361
duke@0 362 if (match) {
duke@0 363 break;
duke@0 364 }
duke@0 365 curr = curr->next;
duke@0 366 }
duke@0 367
duke@0 368 /* if found create a NetworkInterface */
duke@0 369 if (match) {;
duke@0 370 obj = createNetworkInterface(env, curr);
duke@0 371 }
duke@0 372
duke@0 373 freeif(ifs);
duke@0 374 return obj;
duke@0 375 }
duke@0 376
duke@0 377
duke@0 378 /*
duke@0 379 * Class: java_net_NetworkInterface
duke@0 380 * Method: getAll
duke@0 381 * Signature: ()[Ljava/net/NetworkInterface;
duke@0 382 */
duke@0 383 JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
duke@0 384 (JNIEnv *env, jclass cls) {
duke@0 385
duke@0 386 netif *ifs, *curr;
duke@0 387 jobjectArray netIFArr;
duke@0 388 jint arr_index, ifCount;
duke@0 389
duke@0 390 ifs = enumInterfaces(env);
duke@0 391 if (ifs == NULL) {
duke@0 392 return NULL;
duke@0 393 }
duke@0 394
duke@0 395 /* count the interface */
duke@0 396 ifCount = 0;
duke@0 397 curr = ifs;
duke@0 398 while (curr != NULL) {
duke@0 399 ifCount++;
duke@0 400 curr = curr->next;
duke@0 401 }
duke@0 402
duke@0 403 /* allocate a NetworkInterface array */
duke@0 404 netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
duke@0 405 if (netIFArr == NULL) {
duke@0 406 freeif(ifs);
duke@0 407 return NULL;
duke@0 408 }
duke@0 409
duke@0 410 /*
duke@0 411 * Iterate through the interfaces, create a NetworkInterface instance
duke@0 412 * for each array element and populate the object.
duke@0 413 */
duke@0 414 curr = ifs;
duke@0 415 arr_index = 0;
duke@0 416 while (curr != NULL) {
duke@0 417 jobject netifObj;
duke@0 418
duke@0 419 netifObj = createNetworkInterface(env, curr);
duke@0 420 if (netifObj == NULL) {
duke@0 421 freeif(ifs);
duke@0 422 return NULL;
duke@0 423 }
duke@0 424
duke@0 425 /* put the NetworkInterface into the array */
duke@0 426 (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
duke@0 427
duke@0 428 curr = curr->next;
duke@0 429 }
duke@0 430
duke@0 431 freeif(ifs);
duke@0 432 return netIFArr;
duke@0 433 }
duke@0 434
dsamersoff@2545 435
dsamersoff@2545 436 /*
dsamersoff@2545 437 * Class: java_net_NetworkInterface
dsamersoff@2545 438 * Method: isUp0
dsamersoff@2545 439 * Signature: (Ljava/lang/String;I)Z
dsamersoff@2545 440 */
dsamersoff@2545 441 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0(JNIEnv *env, jclass cls, jstring name, jint index) {
dsamersoff@2545 442 int ret = getFlags0(env, name);
dsamersoff@2545 443 return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE : JNI_FALSE;
dsamersoff@2545 444 }
dsamersoff@2545 445
dsamersoff@2545 446 /*
dsamersoff@2545 447 * Class: java_net_NetworkInterface
dsamersoff@2545 448 * Method: isP2P0
dsamersoff@2545 449 * Signature: (Ljava/lang/String;I)Z
dsamersoff@2545 450 */
dsamersoff@2545 451 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0(JNIEnv *env, jclass cls, jstring name, jint index) {
dsamersoff@2545 452 int ret = getFlags0(env, name);
dsamersoff@2545 453 return (ret & IFF_POINTOPOINT) ? JNI_TRUE : JNI_FALSE;
dsamersoff@2545 454 }
dsamersoff@2545 455
dsamersoff@2545 456 /*
dsamersoff@2545 457 * Class: java_net_NetworkInterface
dsamersoff@2545 458 * Method: isLoopback0
dsamersoff@2545 459 * Signature: (Ljava/lang/String;I)Z
dsamersoff@2545 460 */
dsamersoff@2545 461 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0(JNIEnv *env, jclass cls, jstring name, jint index) {
dsamersoff@2545 462 int ret = getFlags0(env, name);
dsamersoff@2545 463 return (ret & IFF_LOOPBACK) ? JNI_TRUE : JNI_FALSE;
dsamersoff@2545 464 }
dsamersoff@2545 465
dsamersoff@2545 466 /*
dsamersoff@2545 467 * Class: java_net_NetworkInterface
dsamersoff@2545 468 * Method: supportsMulticast0
dsamersoff@2545 469 * Signature: (Ljava/lang/String;I)Z
dsamersoff@2545 470 */
dsamersoff@2545 471 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0(JNIEnv *env, jclass cls, jstring name, jint index) {
dsamersoff@2545 472 int ret = getFlags0(env, name);
dsamersoff@2545 473 return (ret & IFF_MULTICAST) ? JNI_TRUE : JNI_FALSE;
dsamersoff@2545 474 }
dsamersoff@2545 475
dsamersoff@2545 476 /*
dsamersoff@2545 477 * Class: java_net_NetworkInterface
dsamersoff@2545 478 * Method: getMacAddr0
dsamersoff@2545 479 * Signature: ([bLjava/lang/String;I)[b
dsamersoff@2545 480 */
dsamersoff@2545 481 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
dsamersoff@2545 482 jint addr;
dsamersoff@2545 483 jbyte caddr[4];
dsamersoff@2545 484 struct in_addr iaddr;
dsamersoff@2545 485 jbyteArray ret = NULL;
dsamersoff@2545 486 unsigned char mac[16];
dsamersoff@2545 487 int len;
dsamersoff@2545 488 int sock;
dsamersoff@2545 489 jboolean isCopy;
dsamersoff@2545 490 const char* name_utf;
dsamersoff@2545 491
dsamersoff@2545 492 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 493
dsamersoff@2545 494 if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
dsamersoff@2545 495 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 496 return JNI_FALSE;
dsamersoff@2545 497 }
dsamersoff@2545 498
dsamersoff@2545 499
dsamersoff@2545 500 if (!IS_NULL(addrArray)) {
dsamersoff@2545 501 (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
dsamersoff@2545 502 addr = ((caddr[0]<<24) & 0xff000000);
dsamersoff@2545 503 addr |= ((caddr[1] <<16) & 0xff0000);
dsamersoff@2545 504 addr |= ((caddr[2] <<8) & 0xff00);
dsamersoff@2545 505 addr |= (caddr[3] & 0xff);
dsamersoff@2545 506 iaddr.s_addr = htonl(addr);
dsamersoff@2545 507 len = getMacAddress(env, sock, name_utf, &iaddr, mac);
dsamersoff@2545 508 } else {
dsamersoff@2545 509 len = getMacAddress(env, sock, name_utf,NULL, mac);
dsamersoff@2545 510 }
dsamersoff@2545 511 if (len > 0) {
dsamersoff@2545 512 ret = (*env)->NewByteArray(env, len);
dsamersoff@2545 513 if (IS_NULL(ret)) {
dsamersoff@2545 514 /* we may have memory to free at the end of this */
dsamersoff@2545 515 goto fexit;
dsamersoff@2545 516 }
dsamersoff@2545 517 (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
dsamersoff@2545 518 }
dsamersoff@2545 519 fexit:
dsamersoff@2545 520 /* release the UTF string and interface list */
dsamersoff@2545 521 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 522
dsamersoff@2545 523 close(sock);
dsamersoff@2545 524 return ret;
dsamersoff@2545 525 }
dsamersoff@2545 526
dsamersoff@2545 527 /*
dsamersoff@2545 528 * Class: java_net_NetworkInterface
dsamersoff@2545 529 * Method: getMTU0
dsamersoff@2545 530 * Signature: ([bLjava/lang/String;I)I
dsamersoff@2545 531 */
dsamersoff@2545 532
dsamersoff@2545 533 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
dsamersoff@2545 534 jboolean isCopy;
dsamersoff@2545 535 int ret = -1;
dsamersoff@2545 536 int sock;
dsamersoff@2545 537 const char* name_utf;
dsamersoff@2545 538
dsamersoff@2545 539 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 540
dsamersoff@2545 541 if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
dsamersoff@2545 542 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 543 return JNI_FALSE;
dsamersoff@2545 544 }
dsamersoff@2545 545
dsamersoff@2545 546 ret = getMTU(env, sock, name_utf);
dsamersoff@2545 547
dsamersoff@2545 548 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 549
dsamersoff@2545 550 close(sock);
dsamersoff@2545 551 return ret;
dsamersoff@2545 552 }
dsamersoff@2545 553
dsamersoff@2545 554 /*** Private methods definitions ****/
dsamersoff@2545 555
dsamersoff@2545 556 static int getFlags0(JNIEnv *env, jstring name) {
dsamersoff@2545 557 jboolean isCopy;
dsamersoff@2545 558 int ret, sock;
dsamersoff@2545 559 const char* name_utf;
dsamersoff@2545 560
dsamersoff@2545 561 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 562
dsamersoff@2545 563 if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
dsamersoff@2545 564 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 565 return -1;
dsamersoff@2545 566 }
dsamersoff@2545 567
dsamersoff@2545 568 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 569
chegar@2626 570 ret = getFlags(sock, name_utf);
dsamersoff@2545 571
dsamersoff@2545 572 close(sock);
dsamersoff@2545 573 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 574
dsamersoff@2545 575 if (ret < 0) {
dsamersoff@2545 576 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 577 return -1;
dsamersoff@2545 578 }
dsamersoff@2545 579
dsamersoff@2545 580 return ret;
dsamersoff@2545 581 }
dsamersoff@2545 582
dsamersoff@2545 583
dsamersoff@2545 584
dsamersoff@2545 585
duke@0 586 /*
duke@0 587 * Create a NetworkInterface object, populate the name and index, and
duke@0 588 * populate the InetAddress array based on the IP addresses for this
duke@0 589 * interface.
duke@0 590 */
dsamersoff@2545 591 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
duke@0 592 jobject netifObj;
duke@0 593 jobject name;
duke@0 594 jobjectArray addrArr;
duke@0 595 jobjectArray bindArr;
duke@0 596 jobjectArray childArr;
dsamersoff@2545 597 netaddr *addrs;
duke@0 598 jint addr_index, addr_count, bind_index;
duke@0 599 jint child_count, child_index;
duke@0 600 netaddr *addrP;
duke@0 601 netif *childP;
duke@0 602 jobject tmp;
duke@0 603
duke@0 604 /*
duke@0 605 * Create a NetworkInterface object and populate it
duke@0 606 */
duke@0 607 netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
duke@0 608 name = (*env)->NewStringUTF(env, ifs->name);
duke@0 609 if (netifObj == NULL || name == NULL) {
duke@0 610 return NULL;
duke@0 611 }
duke@0 612 (*env)->SetObjectField(env, netifObj, ni_nameID, name);
duke@0 613 (*env)->SetObjectField(env, netifObj, ni_descID, name);
duke@0 614 (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
duke@0 615 (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
duke@0 616
duke@0 617 /*
duke@0 618 * Count the number of address on this interface
duke@0 619 */
duke@0 620 addr_count = 0;
duke@0 621 addrP = ifs->addr;
duke@0 622 while (addrP != NULL) {
duke@0 623 addr_count++;
duke@0 624 addrP = addrP->next;
duke@0 625 }
duke@0 626
duke@0 627 /*
duke@0 628 * Create the array of InetAddresses
duke@0 629 */
duke@0 630 addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
duke@0 631 if (addrArr == NULL) {
duke@0 632 return NULL;
duke@0 633 }
duke@0 634
duke@0 635 bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
duke@0 636 if (bindArr == NULL) {
dsamersoff@2545 637 return NULL;
duke@0 638 }
duke@0 639 addrP = ifs->addr;
duke@0 640 addr_index = 0;
duke@0 641 bind_index = 0;
duke@0 642 while (addrP != NULL) {
duke@0 643 jobject iaObj = NULL;
duke@0 644 jobject ibObj = NULL;
duke@0 645
duke@0 646 if (addrP->family == AF_INET) {
duke@0 647 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
duke@0 648 if (iaObj) {
michaelm@5888 649 setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
duke@0 650 }
duke@0 651 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
duke@0 652 if (ibObj) {
dsamersoff@2545 653 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
dsamersoff@2545 654 if (addrP->brdcast) {
dsamersoff@2545 655 jobject ia2Obj = NULL;
dsamersoff@2545 656 ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
dsamersoff@2545 657 if (ia2Obj) {
michaelm@5888 658 setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
dsamersoff@2545 659 (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
dsamersoff@2545 660 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
dsamersoff@2545 661 }
dsamersoff@2545 662 }
dsamersoff@2545 663 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
duke@0 664 }
duke@0 665 }
duke@0 666
duke@0 667 #ifdef AF_INET6
duke@0 668 if (addrP->family == AF_INET6) {
duke@0 669 int scope=0;
duke@0 670 iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
duke@0 671 if (iaObj) {
duke@0 672 jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
duke@0 673 if (ipaddress == NULL) {
duke@0 674 return NULL;
duke@0 675 }
duke@0 676 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
dsamersoff@2545 677 (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
dsamersoff@2545 678
duke@0 679 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
dsamersoff@2545 680
duke@0 681 if (scope != 0) { /* zero is default value, no need to set */
duke@0 682 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
duke@0 683 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
duke@0 684 (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
duke@0 685 }
duke@0 686 (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
duke@0 687 }
duke@0 688 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
duke@0 689 if (ibObj) {
dsamersoff@2545 690 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
dsamersoff@2545 691 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
dsamersoff@2545 692 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
duke@0 693 }
duke@0 694 }
duke@0 695 #endif
duke@0 696
duke@0 697 if (iaObj == NULL) {
duke@0 698 return NULL;
duke@0 699 }
duke@0 700
duke@0 701 (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
duke@0 702 addrP = addrP->next;
duke@0 703 }
duke@0 704
duke@0 705 /*
duke@0 706 * See if there is any virtual interface attached to this one.
duke@0 707 */
duke@0 708 child_count = 0;
duke@0 709 childP = ifs->childs;
duke@0 710 while (childP) {
dsamersoff@2545 711 child_count++;
dsamersoff@2545 712 childP = childP->next;
duke@0 713 }
duke@0 714
duke@0 715 childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
duke@0 716 if (childArr == NULL) {
dsamersoff@2545 717 return NULL;
duke@0 718 }
duke@0 719
duke@0 720 /*
duke@0 721 * Create the NetworkInterface instances for the sub-interfaces as
duke@0 722 * well.
duke@0 723 */
duke@0 724 child_index = 0;
duke@0 725 childP = ifs->childs;
duke@0 726 while(childP) {
duke@0 727 tmp = createNetworkInterface(env, childP);
duke@0 728 if (tmp == NULL) {
dsamersoff@2545 729 return NULL;
duke@0 730 }
duke@0 731 (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
duke@0 732 (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
duke@0 733 childP = childP->next;
duke@0 734 }
duke@0 735 (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
duke@0 736 (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
duke@0 737 (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
duke@0 738
duke@0 739 /* return the NetworkInterface */
duke@0 740 return netifObj;
duke@0 741 }
duke@0 742
duke@0 743 /*
duke@0 744 * Enumerates all interfaces
duke@0 745 */
duke@0 746 static netif *enumInterfaces(JNIEnv *env) {
duke@0 747 netif *ifs;
dsamersoff@2545 748 int sock;
duke@0 749
duke@0 750 /*
duke@0 751 * Enumerate IPv4 addresses
duke@0 752 */
dsamersoff@2545 753
dsamersoff@2545 754 sock = openSocket(env, AF_INET);
dsamersoff@2545 755 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
dsamersoff@2545 756 return NULL;
duke@0 757 }
duke@0 758
dsamersoff@2545 759 ifs = enumIPv4Interfaces(env, sock, NULL);
dsamersoff@2545 760 close(sock);
dsamersoff@2545 761
dsamersoff@2545 762 if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
dsamersoff@2545 763 return NULL;
dsamersoff@2545 764 }
dsamersoff@2545 765
dsamersoff@2545 766 /* return partial list if exception occure in the middle of process ???*/
dsamersoff@2545 767
duke@0 768 /*
duke@0 769 * If IPv6 is available then enumerate IPv6 addresses.
duke@0 770 */
duke@0 771 #ifdef AF_INET6
dsamersoff@2545 772
dsamersoff@2605 773 /* User can disable ipv6 expicitly by -Djava.net.preferIPv4Stack=true,
dsamersoff@2605 774 * so we have to call ipv6_available()
dsamersoff@2605 775 */
dsamersoff@2605 776 if (ipv6_available()) {
duke@0 777
dsamersoff@2605 778 sock = openSocket(env, AF_INET6);
dsamersoff@2605 779 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
dsamersoff@2605 780 freeif(ifs);
dsamersoff@2605 781 return NULL;
dsamersoff@2605 782 }
dsamersoff@2605 783
dsamersoff@2605 784 ifs = enumIPv6Interfaces(env, sock, ifs);
dsamersoff@2605 785 close(sock);
dsamersoff@2605 786
dsamersoff@2605 787 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2605 788 freeif(ifs);
dsamersoff@2605 789 return NULL;
dsamersoff@2605 790 }
dsamersoff@2605 791
dsamersoff@2605 792 }
duke@0 793 #endif
duke@0 794
duke@0 795 return ifs;
duke@0 796 }
duke@0 797
dsamersoff@2545 798 #define CHECKED_MALLOC3(_pointer,_type,_size) \
dsamersoff@2545 799 do{ \
dsamersoff@2545 800 _pointer = (_type)malloc( _size ); \
dsamersoff@2545 801 if (_pointer == NULL) { \
dsamersoff@2545 802 JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); \
dsamersoff@2545 803 return ifs; /* return untouched list */ \
dsamersoff@2545 804 } \
dsamersoff@2545 805 } while(0)
duke@0 806
duke@0 807
duke@0 808 /*
duke@0 809 * Free an interface list (including any attached addresses)
duke@0 810 */
duke@0 811 void freeif(netif *ifs) {
duke@0 812 netif *currif = ifs;
dsamersoff@2545 813 netif *child = NULL;
duke@0 814
duke@0 815 while (currif != NULL) {
duke@0 816 netaddr *addrP = currif->addr;
duke@0 817 while (addrP != NULL) {
duke@0 818 netaddr *next = addrP->next;
duke@0 819 free(addrP);
duke@0 820 addrP = next;
dsamersoff@2545 821 }
duke@0 822
dsamersoff@2545 823 /*
dsamersoff@2545 824 * Don't forget to free the sub-interfaces.
dsamersoff@2545 825 */
dsamersoff@2545 826 if (currif->childs != NULL) {
dsamersoff@2545 827 freeif(currif->childs);
dsamersoff@2545 828 }
duke@0 829
dsamersoff@2545 830 ifs = currif->next;
dsamersoff@2545 831 free(currif);
dsamersoff@2545 832 currif = ifs;
duke@0 833 }
duke@0 834 }
duke@0 835
dsamersoff@2545 836 netif *addif(JNIEnv *env, int sock, const char * if_name, netif *ifs, struct sockaddr* ifr_addrP, int family, short prefix) {
dsamersoff@2545 837 netif *currif = ifs, *parent;
duke@0 838 netaddr *addrP;
dsamersoff@2545 839
michaelm@4632 840 #ifdef LIFNAMSIZ
dsamersoff@2545 841 char name[LIFNAMSIZ], vname[LIFNAMSIZ];
michaelm@4632 842 #else
dsamersoff@2545 843 char name[IFNAMSIZ], vname[IFNAMSIZ];
michaelm@4632 844 #endif
dsamersoff@2545 845
dsamersoff@2545 846 char *name_colonP;
dsamersoff@2545 847 int mask;
duke@0 848 int isVirtual = 0;
dsamersoff@2545 849 int addr_size;
duke@0 850
duke@0 851 /*
duke@0 852 * If the interface name is a logical interface then we
duke@0 853 * remove the unit number so that we have the physical
duke@0 854 * interface (eg: hme0:1 -> hme0). NetworkInterface
duke@0 855 * currently doesn't have any concept of physical vs.
duke@0 856 * logical interfaces.
duke@0 857 */
duke@0 858 strcpy(name, if_name);
dsamersoff@2545 859 *vname = 0;
duke@0 860
duke@0 861 /*
duke@0 862 * Create and populate the netaddr node. If allocation fails
duke@0 863 * return an un-updated list.
duke@0 864 */
dsamersoff@2545 865 /*Allocate for addr and brdcast at once*/
dsamersoff@2545 866
dsamersoff@2545 867 #ifdef AF_INET6
dsamersoff@2545 868 addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
dsamersoff@2545 869 #else
dsamersoff@2545 870 addr_size = sizeof(struct sockaddr_in);
dsamersoff@2545 871 #endif
dsamersoff@2545 872
dsamersoff@2545 873 CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 874 addrP->addr = (struct sockaddr *)( (char *) addrP+sizeof(netaddr) );
dsamersoff@2545 875 memcpy(addrP->addr, ifr_addrP, addr_size);
dsamersoff@2545 876
duke@0 877 addrP->family = family;
duke@0 878 addrP->brdcast = NULL;
duke@0 879 addrP->mask = prefix;
dsamersoff@2545 880 addrP->next = 0;
duke@0 881 if (family == AF_INET) {
duke@0 882 /*
duke@0 883 * Deal with brodcast addr & subnet mask
duke@0 884 */
dsamersoff@2545 885 struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size);
dsamersoff@2545 886 addrP->brdcast = getBroadcast(env, sock, name, brdcast_to );
duke@0 887
dsamersoff@2545 888 if (addrP->brdcast && (mask = getSubnet(env, sock, name)) != -1) {
dsamersoff@2545 889 addrP->mask = mask;
dsamersoff@2545 890 }
dsamersoff@2545 891 }
dsamersoff@2545 892
dsamersoff@2545 893 /**
dsamersoff@2545 894 * Deal with virtual interface with colon notaion e.g. eth0:1
dsamersoff@2545 895 */
dsamersoff@2545 896 name_colonP = strchr(name, ':');
dsamersoff@2545 897 if (name_colonP != NULL) {
duke@0 898 /**
duke@0 899 * This is a virtual interface. If we are able to access the parent
duke@0 900 * we need to create a new entry if it doesn't exist yet *and* update
duke@0 901 * the 'parent' interface with the new records.
duke@0 902 */
dsamersoff@2545 903 *name_colonP = 0;
chegar@2626 904 if (getFlags(sock, name) < 0) {
chegar@894 905 // failed to access parent interface do not create parent.
chegar@894 906 // We are a virtual interface with no parent.
chegar@894 907 isVirtual = 1;
dsamersoff@2545 908 *name_colonP = ':';
duke@0 909 }
dsamersoff@2545 910 else{
dsamersoff@2545 911 // Got access to parent, so create it if necessary.
dsamersoff@2545 912 // Save original name to vname and truncate name by ':'
dsamersoff@2545 913 memcpy(vname, name, sizeof(vname) );
dsamersoff@2545 914 vname[name_colonP - name] = ':';
dsamersoff@2545 915 }
duke@0 916 }
duke@0 917
duke@0 918 /*
duke@0 919 * Check if this is a "new" interface. Use the interface
duke@0 920 * name for matching because index isn't supported on
duke@0 921 * Solaris 2.6 & 7.
duke@0 922 */
duke@0 923 while (currif != NULL) {
duke@0 924 if (strcmp(name, currif->name) == 0) {
duke@0 925 break;
duke@0 926 }
duke@0 927 currif = currif->next;
duke@0 928 }
duke@0 929
duke@0 930 /*
duke@0 931 * If "new" then create an netif structure and
duke@0 932 * insert it onto the list.
duke@0 933 */
duke@0 934 if (currif == NULL) {
dsamersoff@2545 935 CHECKED_MALLOC3(currif, netif *, sizeof(netif)+IFNAMSIZ );
dsamersoff@2545 936 currif->name = (char *) currif+sizeof(netif);
dsamersoff@2545 937 strcpy(currif->name, name);
chegar@2578 938 currif->index = getIndex(sock, name);
dsamersoff@2545 939 currif->addr = NULL;
dsamersoff@2545 940 currif->childs = NULL;
dsamersoff@2545 941 currif->virtual = isVirtual;
dsamersoff@2545 942 currif->next = ifs;
dsamersoff@2545 943 ifs = currif;
duke@0 944 }
duke@0 945
duke@0 946 /*
duke@0 947 * Finally insert the address on the interface
duke@0 948 */
duke@0 949 addrP->next = currif->addr;
duke@0 950 currif->addr = addrP;
duke@0 951
duke@0 952 parent = currif;
duke@0 953
duke@0 954 /**
duke@0 955 * Let's deal with the virtual interface now.
duke@0 956 */
duke@0 957 if (vname[0]) {
dsamersoff@2545 958 netaddr *tmpaddr;
duke@0 959
dsamersoff@2545 960 currif = parent->childs;
duke@0 961
dsamersoff@2545 962 while (currif != NULL) {
dsamersoff@2545 963 if (strcmp(vname, currif->name) == 0) {
dsamersoff@2545 964 break;
dsamersoff@2545 965 }
dsamersoff@2545 966 currif = currif->next;
duke@0 967 }
dsamersoff@2545 968
dsamersoff@2545 969 if (currif == NULL) {
dsamersoff@2545 970 CHECKED_MALLOC3(currif, netif *, sizeof(netif)+ IFNAMSIZ );
dsamersoff@2545 971 currif->name = (char *) currif + sizeof(netif);
dsamersoff@2545 972 strcpy(currif->name, vname);
chegar@2578 973 currif->index = getIndex(sock, vname);
dsamersoff@2545 974 currif->addr = NULL;
dsamersoff@2545 975 /* Need to duplicate the addr entry? */
dsamersoff@2545 976 currif->virtual = 1;
dsamersoff@2545 977 currif->childs = NULL;
dsamersoff@2545 978 currif->next = parent->childs;
dsamersoff@2545 979 parent->childs = currif;
duke@0 980 }
dsamersoff@2545 981
dsamersoff@2545 982 CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 983 memcpy(tmpaddr, addrP, sizeof(netaddr));
dsamersoff@2545 984 if (addrP->addr != NULL) {
dsamersoff@2545 985 tmpaddr->addr = (struct sockaddr *) ( (char*)tmpaddr + sizeof(netaddr) ) ;
dsamersoff@2545 986 memcpy(tmpaddr->addr, addrP->addr, addr_size);
duke@0 987 }
duke@0 988
dsamersoff@2545 989 if (addrP->brdcast != NULL) {
dsamersoff@2545 990 tmpaddr->brdcast = (struct sockaddr *) ((char *) tmpaddr + sizeof(netaddr)+addr_size);
dsamersoff@2545 991 memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
dsamersoff@2545 992 }
dsamersoff@2545 993
dsamersoff@2545 994 tmpaddr->next = currif->addr;
dsamersoff@2545 995 currif->addr = tmpaddr;
duke@0 996 }
duke@0 997
duke@0 998 return ifs;
duke@0 999 }
duke@0 1000
dsamersoff@2545 1001 /* Open socket for further ioct calls
dsamersoff@2545 1002 * proto is AF_INET/AF_INET6
duke@0 1003 */
dsamersoff@2545 1004 static int openSocket(JNIEnv *env, int proto){
dsamersoff@2545 1005 int sock;
duke@0 1006
dsamersoff@2545 1007 if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1008 /*
dsamersoff@2545 1009 * If EPROTONOSUPPORT is returned it means we don't have
dsamersoff@2545 1010 * support for this proto so don't throw an exception.
dsamersoff@2545 1011 */
dsamersoff@2545 1012 if (errno != EPROTONOSUPPORT) {
dsamersoff@2545 1013 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
dsamersoff@2545 1014 }
dsamersoff@2545 1015 return -1;
chegar@375 1016 }
chegar@375 1017
dsamersoff@2545 1018 return sock;
dsamersoff@2545 1019 }
chegar@375 1020
dsamersoff@2545 1021
dsamersoff@2545 1022 /** Linux **/
dsamersoff@2545 1023 #ifdef __linux__
dsamersoff@2545 1024 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1025 * if it falls return v6 socket
dsamersoff@2545 1026 */
dsamersoff@2545 1027
dsamersoff@2545 1028 #ifdef AF_INET6
dsamersoff@2545 1029 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1030 int sock;
dsamersoff@2545 1031 struct ifreq if2;
dsamersoff@2545 1032
dsamersoff@2545 1033 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1034 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1035 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1036 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1037 return -1;
dsamersoff@2545 1038 }
dsamersoff@2545 1039 }
dsamersoff@2545 1040 else{ // errno is not NOSUPPORT
dsamersoff@2545 1041 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1042 return -1;
dsamersoff@2545 1043 }
dsamersoff@2545 1044 }
dsamersoff@2545 1045
dsamersoff@2545 1046 /* Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or IPv6 socket regardless of type
dsamersoff@2545 1047 of address of an interface */
dsamersoff@2545 1048
dsamersoff@2545 1049 return sock;
dsamersoff@2545 1050 }
dsamersoff@2545 1051
dsamersoff@2545 1052 #else
dsamersoff@2545 1053 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1054 return openSocket(env,AF_INET);
dsamersoff@2545 1055 }
dsamersoff@2545 1056 #endif
dsamersoff@2545 1057
dsamersoff@2545 1058 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1059 struct ifconf ifc;
dsamersoff@2545 1060 struct ifreq *ifreqP;
dsamersoff@2545 1061 char *buf;
dsamersoff@2545 1062 int numifs;
dsamersoff@2545 1063 unsigned i;
dsamersoff@2545 1064
dsamersoff@2545 1065
dsamersoff@2545 1066 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
dsamersoff@2545 1067 * SIOCGIFCOUNT doesn't work
dsamersoff@2545 1068 */
dsamersoff@2545 1069 ifc.ifc_buf = NULL;
dsamersoff@2545 1070 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1071 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
dsamersoff@2545 1072 return ifs;
chegar@375 1073 }
dsamersoff@2545 1074
dsamersoff@2545 1075 CHECKED_MALLOC3(buf,char *, ifc.ifc_len);
dsamersoff@2545 1076
dsamersoff@2545 1077 ifc.ifc_buf = buf;
dsamersoff@2545 1078 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1079 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
dsamersoff@2545 1080 (void) free(buf);
dsamersoff@2545 1081 return ifs;
dsamersoff@2545 1082 }
dsamersoff@2545 1083
dsamersoff@2545 1084 /*
dsamersoff@2545 1085 * Iterate through each interface
dsamersoff@2545 1086 */
dsamersoff@2545 1087 ifreqP = ifc.ifc_req;
dsamersoff@2545 1088 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
dsamersoff@2545 1089 /*
dsamersoff@2545 1090 * Add to the list
dsamersoff@2545 1091 */
dsamersoff@2545 1092 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
dsamersoff@2545 1093
dsamersoff@2545 1094 /*
dsamersoff@2545 1095 * If an exception occurred then free the list
dsamersoff@2545 1096 */
dsamersoff@2545 1097 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1098 free(buf);
dsamersoff@2545 1099 freeif(ifs);
dsamersoff@2545 1100 return NULL;
dsamersoff@2545 1101 }
dsamersoff@2545 1102 }
dsamersoff@2545 1103
dsamersoff@2545 1104 /*
dsamersoff@2545 1105 * Free socket and buffer
dsamersoff@2545 1106 */
dsamersoff@2545 1107 free(buf);
dsamersoff@2545 1108 return ifs;
dsamersoff@2545 1109 }
dsamersoff@2545 1110
dsamersoff@2545 1111
dsamersoff@2545 1112 /*
dsamersoff@2545 1113 * Enumerates and returns all IPv6 interfaces on Linux
dsamersoff@2545 1114 */
dsamersoff@2545 1115
dsamersoff@2545 1116 #ifdef AF_INET6
dsamersoff@2545 1117 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1118 FILE *f;
chegar@3657 1119 char addr6[40], devname[21];
dsamersoff@2545 1120 char addr6p[8][5];
dsamersoff@2545 1121 int plen, scope, dad_status, if_idx;
dsamersoff@2545 1122 uint8_t ipv6addr[16];
dsamersoff@2545 1123
dsamersoff@2545 1124 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
dsamersoff@2545 1125 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
dsamersoff@2545 1126 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7],
dsamersoff@2545 1127 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
dsamersoff@2545 1128
dsamersoff@2545 1129 struct netif *ifs_ptr = NULL;
dsamersoff@2545 1130 struct netif *last_ptr = NULL;
dsamersoff@2545 1131 struct sockaddr_in6 addr;
dsamersoff@2545 1132
dsamersoff@2545 1133 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
dsamersoff@2545 1134 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
dsamersoff@2545 1135 inet_pton(AF_INET6, addr6, ipv6addr);
dsamersoff@2545 1136
dsamersoff@2545 1137 memset(&addr, 0, sizeof(struct sockaddr_in6));
dsamersoff@2545 1138 memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
dsamersoff@2545 1139
dsamersoff@2545 1140 addr.sin6_scope_id = if_idx;
dsamersoff@2545 1141
dsamersoff@2545 1142 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
dsamersoff@2545 1143
dsamersoff@2545 1144
dsamersoff@2545 1145 /*
dsamersoff@2545 1146 * If an exception occurred then return the list as is.
dsamersoff@2545 1147 */
dsamersoff@2545 1148 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1149 fclose(f);
dsamersoff@2545 1150 return ifs;
dsamersoff@2545 1151 }
dsamersoff@2545 1152 }
dsamersoff@2545 1153 fclose(f);
dsamersoff@2545 1154 }
dsamersoff@2545 1155 return ifs;
dsamersoff@2545 1156 }
chegar@375 1157 #endif
duke@0 1158
dsamersoff@2545 1159
chegar@2578 1160 static int getIndex(int sock, const char *name){
dsamersoff@2545 1161 /*
dsamersoff@2545 1162 * Try to get the interface index
dsamersoff@2545 1163 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1164 */
dsamersoff@2545 1165 struct ifreq if2;
dsamersoff@2545 1166 strcpy(if2.ifr_name, name);
dsamersoff@2545 1167
dsamersoff@2545 1168 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1169 return -1;
dsamersoff@2545 1170 }
dsamersoff@2545 1171
dsamersoff@2545 1172 return if2.ifr_ifindex;
duke@0 1173 }
duke@0 1174
duke@0 1175 /**
duke@0 1176 * Returns the IPv4 broadcast address of a named interface, if it exists.
duke@0 1177 * Returns 0 if it doesn't have one.
duke@0 1178 */
dsamersoff@2545 1179 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
duke@0 1180 struct sockaddr *ret = NULL;
duke@0 1181 struct ifreq if2;
duke@0 1182
duke@0 1183 memset((char *) &if2, 0, sizeof(if2));
duke@0 1184 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1185
duke@0 1186 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1187 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1188 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
dsamersoff@2545 1189 return ret;
duke@0 1190 }
dsamersoff@2545 1191
dsamersoff@2545 1192 if (if2.ifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1193 /* It does, let's retrieve it*/
dsamersoff@2545 1194 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1195 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
dsamersoff@2545 1196 return ret;
dsamersoff@2545 1197 }
dsamersoff@2545 1198
dsamersoff@2545 1199 ret = brdcast_store;
duke@0 1200 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
duke@0 1201 }
dsamersoff@2545 1202
duke@0 1203 return ret;
duke@0 1204 }
duke@0 1205
duke@0 1206 /**
duke@0 1207 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
duke@0 1208 * interface, if it has one, otherwise return -1.
duke@0 1209 */
dsamersoff@2545 1210 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1211 unsigned int mask;
dsamersoff@2545 1212 short ret;
dsamersoff@2545 1213 struct ifreq if2;
dsamersoff@2545 1214
dsamersoff@2545 1215 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1216 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1217
dsamersoff@2545 1218 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1219 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
dsamersoff@2545 1220 return -1;
dsamersoff@2545 1221 }
dsamersoff@2545 1222
dsamersoff@2545 1223 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1224 ret = 0;
dsamersoff@2545 1225 while (mask) {
dsamersoff@2545 1226 mask <<= 1;
dsamersoff@2545 1227 ret++;
dsamersoff@2545 1228 }
dsamersoff@2545 1229
dsamersoff@2545 1230 return ret;
dsamersoff@2545 1231 }
dsamersoff@2545 1232
dsamersoff@2545 1233 /**
dsamersoff@2545 1234 * Get the Hardware address (usually MAC address) for the named interface.
dsamersoff@2545 1235 * return puts the data in buf, and returns the length, in byte, of the
dsamersoff@2545 1236 * MAC address. Returns -1 if there is no hardware address on that interface.
dsamersoff@2545 1237 */
dsamersoff@2545 1238 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1239 static struct ifreq ifr;
dsamersoff@2545 1240 int i;
dsamersoff@2545 1241
dsamersoff@2545 1242 strcpy(ifr.ifr_name, ifname);
dsamersoff@2545 1243 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
dsamersoff@2545 1244 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
dsamersoff@2545 1245 return -1;
dsamersoff@2545 1246 }
dsamersoff@2545 1247
dsamersoff@2545 1248 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
dsamersoff@2545 1249
dsamersoff@2545 1250 /*
dsamersoff@2545 1251 * All bytes to 0 means no hardware address.
dsamersoff@2545 1252 */
dsamersoff@2545 1253
dsamersoff@2545 1254 for (i = 0; i < IFHWADDRLEN; i++) {
dsamersoff@2545 1255 if (buf[i] != 0)
dsamersoff@2545 1256 return IFHWADDRLEN;
dsamersoff@2545 1257 }
dsamersoff@2545 1258
dsamersoff@2545 1259 return -1;
dsamersoff@2545 1260 }
dsamersoff@2545 1261
dsamersoff@2545 1262 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1263 struct ifreq if2;
dsamersoff@2545 1264
dsamersoff@2545 1265 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1266 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1267
dsamersoff@2545 1268 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1269 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
dsamersoff@2545 1270 return -1;
dsamersoff@2545 1271 }
dsamersoff@2545 1272
dsamersoff@2545 1273 return if2.ifr_mtu;
dsamersoff@2545 1274 }
dsamersoff@2545 1275
chegar@2626 1276 static int getFlags(int sock, const char *ifname) {
duke@0 1277 struct ifreq if2;
dsamersoff@2545 1278 int ret = -1;
duke@0 1279
duke@0 1280 memset((char *) &if2, 0, sizeof(if2));
duke@0 1281 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1282
dsamersoff@2545 1283 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
dsamersoff@2545 1284 return -1;
duke@0 1285 }
dsamersoff@2545 1286
dsamersoff@2545 1287 return if2.ifr_flags;
duke@0 1288 }
duke@0 1289
dsamersoff@2545 1290 #endif
dsamersoff@2545 1291
dsamersoff@2545 1292 /** Solaris **/
duke@0 1293 #ifdef __solaris__
dsamersoff@2545 1294 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1295 * if it falls return v6 socket
dsamersoff@2545 1296 */
dsamersoff@2545 1297
dsamersoff@2545 1298 #ifdef AF_INET6
dsamersoff@2545 1299 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1300 int sock, alreadyV6 = 0;
dsamersoff@2545 1301 struct lifreq if2;
dsamersoff@2545 1302
dsamersoff@2545 1303 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1304 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1305 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1306 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1307 return -1;
dsamersoff@2545 1308 }
dsamersoff@2545 1309
dsamersoff@2545 1310 alreadyV6=1;
dsamersoff@2545 1311 }
dsamersoff@2545 1312 else{ // errno is not NOSUPPORT
dsamersoff@2545 1313 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1314 return -1;
dsamersoff@2545 1315 }
dsamersoff@2545 1316 }
dsamersoff@2545 1317
dsamersoff@2545 1318 /**
dsamersoff@2545 1319 * Solaris requires that we have IPv6 socket to query an
dsamersoff@2545 1320 * interface without IPv4 address - check it here
dsamersoff@2545 1321 * POSIX 1 require the kernell to return ENOTTY if the call is
dsamersoff@2545 1322 * unappropriate for device e.g. NETMASK for device having IPv6
dsamersoff@2545 1323 * only address but not all devices follows the standart so
dsamersoff@2545 1324 * fallback on any error. It's not an ecology friendly but more
dsamersoff@2545 1325 * reliable.
dsamersoff@2545 1326 */
dsamersoff@2545 1327
dsamersoff@2545 1328 if (! alreadyV6 ){
dsamersoff@2545 1329 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1330 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1331 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1332 close(sock);
dsamersoff@2545 1333 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1334 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1335 return -1;
dsamersoff@2545 1336 }
dsamersoff@2545 1337 }
dsamersoff@2545 1338 }
dsamersoff@2545 1339
dsamersoff@2545 1340 return sock;
dsamersoff@2545 1341 }
dsamersoff@2545 1342
dsamersoff@2545 1343 #else
dsamersoff@2545 1344 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1345 return openSocket(env,AF_INET);
dsamersoff@2545 1346 }
dsamersoff@2545 1347 #endif
dsamersoff@2545 1348
dsamersoff@2545 1349 /*
dsamersoff@2545 1350 * Enumerates and returns all IPv4 interfaces
dsamersoff@2545 1351 * (linux verison)
dsamersoff@2545 1352 */
dsamersoff@2545 1353
dsamersoff@2545 1354 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1355 return enumIPvXInterfaces(env,sock, ifs, AF_INET);
dsamersoff@2545 1356 }
dsamersoff@2545 1357
dsamersoff@2545 1358 #ifdef AF_INET6
dsamersoff@2545 1359 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1360 return enumIPvXInterfaces(env,sock, ifs, AF_INET6);
dsamersoff@2545 1361 }
dsamersoff@2545 1362 #endif
dsamersoff@2545 1363
dsamersoff@2545 1364 /*
dsamersoff@2545 1365 Enumerates and returns all interfaces on Solaris
dsamersoff@2545 1366 use the same code for IPv4 and IPv6
dsamersoff@2545 1367 */
dsamersoff@2545 1368 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family) {
dsamersoff@2545 1369 struct lifconf ifc;
dsamersoff@2545 1370 struct lifreq *ifr;
dsamersoff@2545 1371 int n;
dsamersoff@2545 1372 char *buf;
dsamersoff@2545 1373 struct lifnum numifs;
dsamersoff@2545 1374 unsigned bufsize;
dsamersoff@2545 1375
dsamersoff@2545 1376 /*
dsamersoff@2545 1377 * Get the interface count
dsamersoff@2545 1378 */
dsamersoff@2545 1379 numifs.lifn_family = family;
dsamersoff@2545 1380 numifs.lifn_flags = 0;
dsamersoff@2545 1381 if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
dsamersoff@2545 1382 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFNUM failed");
dsamersoff@2545 1383 return ifs;
dsamersoff@2545 1384 }
dsamersoff@2545 1385
dsamersoff@2545 1386 /*
dsamersoff@2545 1387 * Enumerate the interface configurations
dsamersoff@2545 1388 */
dsamersoff@2545 1389 bufsize = numifs.lifn_count * sizeof (struct lifreq);
dsamersoff@2545 1390 CHECKED_MALLOC3(buf, char *, bufsize);
dsamersoff@2545 1391
dsamersoff@2545 1392 ifc.lifc_family = family;
dsamersoff@2545 1393 ifc.lifc_flags = 0;
dsamersoff@2545 1394 ifc.lifc_len = bufsize;
dsamersoff@2545 1395 ifc.lifc_buf = buf;
dsamersoff@2545 1396 if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1397 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFCONF failed");
dsamersoff@2545 1398 free(buf);
dsamersoff@2545 1399 return ifs;
dsamersoff@2545 1400 }
dsamersoff@2545 1401
dsamersoff@2545 1402 /*
dsamersoff@2545 1403 * Iterate through each interface
dsamersoff@2545 1404 */
dsamersoff@2545 1405 ifr = ifc.lifc_req;
dsamersoff@2545 1406 for (n=0; n<numifs.lifn_count; n++, ifr++) {
dsamersoff@2545 1407 int index = -1;
dsamersoff@2545 1408 struct lifreq if2;
dsamersoff@2545 1409
dsamersoff@2545 1410 /*
dsamersoff@2545 1411 * Ignore either IPv4 or IPv6 addresses
dsamersoff@2545 1412 */
dsamersoff@2545 1413 if (ifr->lifr_addr.ss_family != family) {
dsamersoff@2545 1414 continue;
dsamersoff@2545 1415 }
dsamersoff@2545 1416
chegar@2578 1417 #ifdef AF_INET6
chegar@2578 1418 if (ifr->lifr_addr.ss_family == AF_INET6) {
chegar@2578 1419 struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
chegar@2578 1420 s6->sin6_scope_id = getIndex(sock, ifr->lifr_name);
chegar@2578 1421 }
chegar@2578 1422 #endif
chegar@2578 1423
dsamersoff@2545 1424 /* add to the list */
dsamersoff@2545 1425 ifs = addif(env, sock,ifr->lifr_name, ifs, (struct sockaddr *)&(ifr->lifr_addr),family, (short) ifr->lifr_addrlen);
dsamersoff@2545 1426
dsamersoff@2545 1427 /*
dsamersoff@2545 1428 * If an exception occurred we return immediately
dsamersoff@2545 1429 */
dsamersoff@2545 1430 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1431 free(buf);
dsamersoff@2545 1432 return ifs;
dsamersoff@2545 1433 }
dsamersoff@2545 1434
dsamersoff@2545 1435 }
dsamersoff@2545 1436
dsamersoff@2545 1437 free(buf);
dsamersoff@2545 1438 return ifs;
dsamersoff@2545 1439 }
dsamersoff@2545 1440
chegar@2578 1441 static int getIndex(int sock, const char *name){
dsamersoff@2545 1442 /*
dsamersoff@2545 1443 * Try to get the interface index
dsamersoff@2545 1444 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1445 */
dsamersoff@2545 1446 struct lifreq if2;
dsamersoff@2545 1447 strcpy(if2.lifr_name, name);
dsamersoff@2545 1448
dsamersoff@2545 1449 if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1450 return -1;
dsamersoff@2545 1451 }
dsamersoff@2545 1452
dsamersoff@2545 1453 return if2.lifr_index;
dsamersoff@2545 1454 }
dsamersoff@2545 1455
dsamersoff@2545 1456 /**
dsamersoff@2545 1457 * Returns the IPv4 broadcast address of a named interface, if it exists.
dsamersoff@2545 1458 * Returns 0 if it doesn't have one.
dsamersoff@2545 1459 */
dsamersoff@2545 1460 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
dsamersoff@2545 1461 struct sockaddr *ret = NULL;
dsamersoff@2545 1462 struct lifreq if2;
dsamersoff@2545 1463
dsamersoff@2545 1464 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1465 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1466
dsamersoff@2545 1467 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1468 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1469 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 1470 return ret;
dsamersoff@2545 1471 }
dsamersoff@2545 1472
dsamersoff@2545 1473 if (if2.lifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1474 /* It does, let's retrieve it*/
dsamersoff@2545 1475 if (ioctl(sock, SIOCGLIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1476 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFBRDADDR failed");
dsamersoff@2545 1477 return ret;
dsamersoff@2545 1478 }
dsamersoff@2545 1479
dsamersoff@2545 1480 ret = brdcast_store;
dsamersoff@2545 1481 memcpy(ret, &if2.lifr_broadaddr, sizeof(struct sockaddr));
dsamersoff@2545 1482 }
dsamersoff@2545 1483
dsamersoff@2545 1484 return ret;
dsamersoff@2545 1485 }
dsamersoff@2545 1486
dsamersoff@2545 1487 /**
dsamersoff@2545 1488 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
dsamersoff@2545 1489 * interface, if it has one, otherwise return -1.
dsamersoff@2545 1490 */
dsamersoff@2545 1491 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1492 unsigned int mask;
dsamersoff@2545 1493 short ret;
dsamersoff@2545 1494 struct lifreq if2;
dsamersoff@2545 1495
dsamersoff@2545 1496 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1497 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1498
dsamersoff@2545 1499 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1500 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFNETMASK failed");
dsamersoff@2545 1501 return -1;
dsamersoff@2545 1502 }
dsamersoff@2545 1503
dsamersoff@2545 1504 mask = ntohl(((struct sockaddr_in*)&(if2.lifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1505 ret = 0;
dsamersoff@2545 1506
dsamersoff@2545 1507 while (mask) {
dsamersoff@2545 1508 mask <<= 1;
dsamersoff@2545 1509 ret++;
dsamersoff@2545 1510 }
dsamersoff@2545 1511
dsamersoff@2545 1512 return ret;
dsamersoff@2545 1513 }
dsamersoff@2545 1514
dsamersoff@2545 1515
dsamersoff@2545 1516
dsamersoff@2545 1517 #define DEV_PREFIX "/dev/"
duke@0 1518
duke@0 1519 /**
duke@0 1520 * Solaris specific DLPI code to get hardware address from a device.
duke@0 1521 * Unfortunately, at least up to Solaris X, you have to have special
duke@0 1522 * privileges (i.e. be root).
duke@0 1523 */
duke@0 1524 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
dsamersoff@2545 1525 char style1dev[MAXPATHLEN];
dsamersoff@2545 1526 int fd;
dsamersoff@2545 1527 dl_phys_addr_req_t dlpareq;
dsamersoff@2545 1528 dl_phys_addr_ack_t *dlpaack;
dsamersoff@2545 1529 struct strbuf msg;
dsamersoff@2545 1530 char buf[128];
dsamersoff@2545 1531 int flags = 0;
duke@0 1532
dsamersoff@2545 1533 /**
dsamersoff@2545 1534 * Device is in /dev
dsamersoff@2545 1535 * e.g.: /dev/bge0
dsamersoff@2545 1536 */
dsamersoff@2545 1537 strcpy(style1dev, DEV_PREFIX);
dsamersoff@2545 1538 strcat(style1dev, ifname);
dsamersoff@2545 1539 if ((fd = open(style1dev, O_RDWR)) < 0) {
dsamersoff@2545 1540 /*
dsamersoff@2545 1541 * Can't open it. We probably are missing the privilege.
dsamersoff@2545 1542 * We'll have to try something else
dsamersoff@2545 1543 */
dsamersoff@2545 1544 return 0;
dsamersoff@2545 1545 }
duke@0 1546
dsamersoff@2545 1547 dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dsamersoff@2545 1548 dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
dsamersoff@2545 1549
dsamersoff@2545 1550 msg.buf = (char *)&dlpareq;
dsamersoff@2545 1551 msg.len = DL_PHYS_ADDR_REQ_SIZE;
dsamersoff@2545 1552
dsamersoff@2545 1553 if (putmsg(fd, &msg, NULL, 0) < 0) {
dsamersoff@2545 1554 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "putmsg failed");
dsamersoff@2545 1555 return -1;
dsamersoff@2545 1556 }
dsamersoff@2545 1557
dsamersoff@2545 1558 dlpaack = (dl_phys_addr_ack_t *)buf;
dsamersoff@2545 1559
dsamersoff@2545 1560 msg.buf = (char *)buf;
dsamersoff@2545 1561 msg.len = 0;
dsamersoff@2545 1562 msg.maxlen = sizeof (buf);
dsamersoff@2545 1563 if (getmsg(fd, &msg, NULL, &flags) < 0) {
dsamersoff@2545 1564 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "getmsg failed");
dsamersoff@2545 1565 return -1;
dsamersoff@2545 1566 }
dsamersoff@2545 1567
dsamersoff@2545 1568 if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
dsamersoff@2545 1569 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Couldn't obtain phys addr\n");
dsamersoff@2545 1570 return -1;
dsamersoff@2545 1571 }
dsamersoff@2545 1572
dsamersoff@2545 1573 memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
dsamersoff@2545 1574 return dlpaack->dl_addr_length;
duke@0 1575 }
duke@0 1576
duke@0 1577 /**
duke@0 1578 * Get the Hardware address (usually MAC address) for the named interface.
duke@0 1579 * return puts the data in buf, and returns the length, in byte, of the
duke@0 1580 * MAC address. Returns -1 if there is no hardware address on that interface.
duke@0 1581 */
dsamersoff@2545 1582 static int getMacAddress(JNIEnv *env, int sock, const char *ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1583 struct arpreq arpreq;
dsamersoff@2545 1584 struct sockaddr_in* sin;
dsamersoff@2545 1585 struct sockaddr_in ipAddr;
dsamersoff@2545 1586 int len, i;
chegar@4501 1587 struct lifreq lif;
chegar@4501 1588
chegar@4501 1589 /* First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
chegar@4501 1590 * try the old way.
chegar@4501 1591 */
chegar@4501 1592 memset(&lif, 0, sizeof(lif));
chegar@4501 1593 strlcpy(lif.lifr_name, ifname, sizeof(lif.lifr_name));
chegar@4501 1594
chegar@4501 1595 if (ioctl(sock, SIOCGLIFHWADDR, &lif) != -1) {
chegar@4501 1596 struct sockaddr_dl *sp;
chegar@4501 1597 sp = (struct sockaddr_dl *)&lif.lifr_addr;
chegar@4501 1598 memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
chegar@4501 1599 return sp->sdl_alen;
chegar@4501 1600 }
duke@0 1601
dsamersoff@2545 1602 /**
dsamersoff@2545 1603 * On Solaris we have to use DLPI, but it will only work if we have
dsamersoff@2545 1604 * privileged access (i.e. root). If that fails, we try a lookup
dsamersoff@2545 1605 * in the ARP table, which requires an IPv4 address.
dsamersoff@2545 1606 */
dsamersoff@2545 1607 if ((len = getMacFromDevice(env, ifname, buf)) == 0) {
dsamersoff@2545 1608 /*DLPI failed - trying to do arp lookup*/
duke@0 1609
chegar@4501 1610 if (addr == NULL) {
chegar@4501 1611 /**
chegar@4501 1612 * No IPv4 address for that interface, so can't do an ARP lookup.
chegar@4501 1613 */
chegar@4501 1614 return -1;
chegar@4501 1615 }
dsamersoff@2545 1616
chegar@4501 1617 len = 6; //???
dsamersoff@2545 1618
chegar@4501 1619 sin = (struct sockaddr_in *) &arpreq.arp_pa;
chegar@4501 1620 memset((char *) &arpreq, 0, sizeof(struct arpreq));
chegar@4501 1621 ipAddr.sin_port = 0;
chegar@4501 1622 ipAddr.sin_family = AF_INET;
chegar@4501 1623 memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
chegar@4501 1624 memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
chegar@4501 1625 arpreq.arp_flags= ATF_PUBL;
dsamersoff@2545 1626
chegar@4501 1627 if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
chegar@4501 1628 return -1;
chegar@4501 1629 }
dsamersoff@2545 1630
chegar@4501 1631 memcpy(buf, &arpreq.arp_ha.sa_data[0], len );
chegar@4501 1632 }
duke@0 1633
dsamersoff@2545 1634 /*
dsamersoff@2545 1635 * All bytes to 0 means no hardware address.
dsamersoff@2545 1636 */
duke@0 1637
dsamersoff@2545 1638 for (i = 0; i < len; i++) {
dsamersoff@2545 1639 if (buf[i] != 0)
dsamersoff@2545 1640 return len;
dsamersoff@2545 1641 }
dsamersoff@2545 1642
duke@0 1643 return -1;
duke@0 1644 }
duke@0 1645
dsamersoff@2545 1646 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1647 struct lifreq if2;
duke@0 1648
dsamersoff@2545 1649 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1650 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1651
dsamersoff@2545 1652 if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1653 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFMTU failed");
dsamersoff@2545 1654 return -1;
dsamersoff@2545 1655 }
dsamersoff@2545 1656
dsamersoff@2545 1657 return if2.lifr_mtu;
duke@0 1658 }
duke@0 1659
dsamersoff@2545 1660
chegar@2626 1661 static int getFlags(int sock, const char *ifname) {
dsamersoff@2545 1662 struct lifreq lifr;
dsamersoff@2545 1663 memset((caddr_t)&lifr, 0, sizeof(lifr));
dsamersoff@2545 1664 strcpy((caddr_t)&(lifr.lifr_name), ifname);
dsamersoff@2545 1665
dsamersoff@2545 1666 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
dsamersoff@2545 1667 return -1;
dsamersoff@2545 1668 }
dsamersoff@2545 1669
dsamersoff@2545 1670 return lifr.lifr_flags;
duke@0 1671 }
duke@0 1672
duke@0 1673
dsamersoff@2545 1674 #endif
duke@0 1675
duke@0 1676
michaelm@4632 1677 /** BSD **/
michaelm@4632 1678 #ifdef _ALLBSD_SOURCE
michaelm@4632 1679 /* Open socket for further ioct calls, try v4 socket first and
michaelm@4632 1680 * if it falls return v6 socket
michaelm@4632 1681 */
michaelm@4632 1682
michaelm@4632 1683 #ifdef AF_INET6
michaelm@4632 1684 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
michaelm@4632 1685 int sock;
michaelm@4632 1686 struct ifreq if2;
michaelm@4632 1687
michaelm@4632 1688 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
michaelm@4632 1689 if (errno == EPROTONOSUPPORT){
michaelm@4632 1690 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
michaelm@4632 1691 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
michaelm@4632 1692 return -1;
michaelm@4632 1693 }
michaelm@4632 1694 }
michaelm@4632 1695 else{ // errno is not NOSUPPORT
michaelm@4632 1696 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
michaelm@4632 1697 return -1;
michaelm@4632 1698 }
michaelm@4632 1699 }
michaelm@4632 1700
michaelm@4632 1701 return sock;
michaelm@4632 1702 }
michaelm@4632 1703
michaelm@4632 1704 #else
michaelm@4632 1705 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
michaelm@4632 1706 return openSocket(env,AF_INET);
michaelm@4632 1707 }
michaelm@4632 1708 #endif
michaelm@4632 1709
michaelm@4632 1710 /*
michaelm@4632 1711 * Enumerates and returns all IPv4 interfaces
michaelm@4632 1712 */
michaelm@4632 1713 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
michaelm@4632 1714 struct ifaddrs *ifa, *origifa;
michaelm@4632 1715
michaelm@4632 1716 if (getifaddrs(&origifa) != 0) {
michaelm@4632 1717 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1718 "getifaddrs() function failed");
michaelm@4632 1719 return ifs;
michaelm@4632 1720 }
michaelm@4632 1721
michaelm@4632 1722 for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
michaelm@4632 1723
michaelm@4632 1724 /*
michaelm@4632 1725 * Skip non-AF_INET entries.
michaelm@4632 1726 */
michaelm@4632 1727 if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
michaelm@4632 1728 continue;
michaelm@4632 1729
michaelm@4632 1730 /*
michaelm@4632 1731 * Add to the list.
michaelm@4632 1732 */
michaelm@4632 1733 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0);
michaelm@4632 1734
michaelm@4632 1735 /*
michaelm@4632 1736 * If an exception occurred then free the list.
michaelm@4632 1737 */
michaelm@4632 1738 if ((*env)->ExceptionOccurred(env)) {
michaelm@4632 1739 freeifaddrs(origifa);
michaelm@4632 1740 freeif(ifs);
michaelm@4632 1741 return NULL;
michaelm@4632 1742 }
michaelm@4632 1743 }
michaelm@4632 1744
michaelm@4632 1745 /*
michaelm@4632 1746 * Free socket and buffer
michaelm@4632 1747 */
michaelm@4632 1748 freeifaddrs(origifa);
michaelm@4632 1749 return ifs;
michaelm@4632 1750 }
michaelm@4632 1751
michaelm@4632 1752
michaelm@4632 1753 /*
michaelm@4632 1754 * Enumerates and returns all IPv6 interfaces on Linux
michaelm@4632 1755 */
michaelm@4632 1756
michaelm@4632 1757 #ifdef AF_INET6
michaelm@4632 1758 /*
michaelm@4632 1759 * Determines the prefix on BSD for IPv6 interfaces.
michaelm@4632 1760 */
michaelm@4632 1761 static
michaelm@4632 1762 int prefix(void *val, int size) {
michaelm@4632 1763 u_char *name = (u_char *)val;
michaelm@4632 1764 int byte, bit, plen = 0;
michaelm@4632 1765
michaelm@4632 1766 for (byte = 0; byte < size; byte++, plen += 8)
michaelm@4632 1767 if (name[byte] != 0xff)
michaelm@4632 1768 break;
michaelm@4632 1769 if (byte == size)
michaelm@4632 1770 return (plen);
michaelm@4632 1771 for (bit = 7; bit != 0; bit--, plen++)
michaelm@4632 1772 if (!(name[byte] & (1 << bit)))
michaelm@4632 1773 break;
michaelm@4632 1774 for (; bit != 0; bit--)
michaelm@4632 1775 if (name[byte] & (1 << bit))
michaelm@4632 1776 return (0);
michaelm@4632 1777 byte++;
michaelm@4632 1778 for (; byte < size; byte++)
michaelm@4632 1779 if (name[byte])
michaelm@4632 1780 return (0);
michaelm@4632 1781 return (plen);
michaelm@4632 1782 }
michaelm@4632 1783
michaelm@4632 1784 /*
michaelm@4632 1785 * Enumerates and returns all IPv6 interfaces on BSD
michaelm@4632 1786 */
michaelm@4632 1787 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
michaelm@4632 1788 struct ifaddrs *ifa, *origifa;
michaelm@4632 1789 struct sockaddr_in6 *sin6;
michaelm@4632 1790 struct in6_ifreq ifr6;
michaelm@4632 1791
michaelm@4632 1792 if (getifaddrs(&origifa) != 0) {
michaelm@4632 1793 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1794 "getifaddrs() function failed");
michaelm@4632 1795 return ifs;
michaelm@4632 1796 }
michaelm@4632 1797
michaelm@4632 1798 for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
michaelm@4632 1799
michaelm@4632 1800 /*
michaelm@4632 1801 * Skip non-AF_INET6 entries.
michaelm@4632 1802 */
michaelm@4632 1803 if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
michaelm@4632 1804 continue;
michaelm@4632 1805
michaelm@4632 1806 memset(&ifr6, 0, sizeof(ifr6));
michaelm@4632 1807 strlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
michaelm@4632 1808 memcpy(&ifr6.ifr_addr, ifa->ifa_addr, MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len));
michaelm@4632 1809
michaelm@4632 1810 if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
michaelm@4632 1811 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1812 "ioctl SIOCGIFNETMASK_IN6 failed");
michaelm@4632 1813 freeifaddrs(origifa);
michaelm@4632 1814 freeif(ifs);
michaelm@4632 1815 return NULL;
michaelm@4632 1816 }
michaelm@4632 1817
michaelm@4632 1818 /* Add to the list. */
michaelm@4632 1819 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
michaelm@4632 1820 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6,
michaelm@4632 1821 prefix(&sin6->sin6_addr, sizeof(struct in6_addr)));
michaelm@4632 1822
michaelm@4632 1823 /* If an exception occurred then free the list. */
michaelm@4632 1824 if ((*env)->ExceptionOccurred(env)) {
michaelm@4632 1825 freeifaddrs(origifa);
michaelm@4632 1826 freeif(ifs);
michaelm@4632 1827 return NULL;
michaelm@4632 1828 }
michaelm@4632 1829 }
michaelm@4632 1830
michaelm@4632 1831 /*
michaelm@4632 1832 * Free socket and ifaddrs buffer
michaelm@4632 1833 */
michaelm@4632 1834 freeifaddrs(origifa);
michaelm@4632 1835 return ifs;
michaelm@4632 1836 }
michaelm@4632 1837 #endif
michaelm@4632 1838
michaelm@4632 1839 static int getIndex(int sock, const char *name){
michaelm@4632 1840 #ifdef __FreeBSD__
michaelm@4632 1841 /*
michaelm@4632 1842 * Try to get the interface index
michaelm@4632 1843 * (Not supported on Solaris 2.6 or 7)
michaelm@4632 1844 */
michaelm@4632 1845 struct ifreq if2;
michaelm@4632 1846 strcpy(if2.ifr_name, name);
michaelm@4632 1847
michaelm@4632 1848 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
michaelm@4632 1849 return -1;
michaelm@4632 1850 }
michaelm@4632 1851
michaelm@4632 1852 return if2.ifr_index;
michaelm@4632 1853 #else
michaelm@4632 1854 /*
michaelm@4632 1855 * Try to get the interface index using BSD specific if_nametoindex
michaelm@4632 1856 */
michaelm@4632 1857 int index = if_nametoindex(name);
michaelm@4632 1858 return (index == 0) ? -1 : index;
michaelm@4632 1859 #endif
michaelm@4632 1860 }
michaelm@4632 1861
michaelm@4632 1862 /**
michaelm@4632 1863 * Returns the IPv4 broadcast address of a named interface, if it exists.
michaelm@4632 1864 * Returns 0 if it doesn't have one.
michaelm@4632 1865 */
michaelm@4632 1866 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
michaelm@4632 1867 struct sockaddr *ret = NULL;
michaelm@4632 1868 struct ifreq if2;
michaelm@4632 1869
michaelm@4632 1870 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1871 strcpy(if2.ifr_name, ifname);
michaelm@4632 1872
michaelm@4632 1873 /* Let's make sure the interface does have a broadcast address */
michaelm@4632 1874 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
michaelm@4632 1875 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
michaelm@4632 1876 return ret;
michaelm@4632 1877 }
michaelm@4632 1878
michaelm@4632 1879 if (if2.ifr_flags & IFF_BROADCAST) {
michaelm@4632 1880 /* It does, let's retrieve it*/
michaelm@4632 1881 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
michaelm@4632 1882 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
michaelm@4632 1883 return ret;
michaelm@4632 1884 }
michaelm@4632 1885
michaelm@4632 1886 ret = brdcast_store;
michaelm@4632 1887 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
michaelm@4632 1888 }
michaelm@4632 1889
michaelm@4632 1890 return ret;
michaelm@4632 1891 }
michaelm@4632 1892
michaelm@4632 1893 /**
michaelm@4632 1894 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
michaelm@4632 1895 * interface, if it has one, otherwise return -1.
michaelm@4632 1896 */
michaelm@4632 1897 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
michaelm@4632 1898 unsigned int mask;
michaelm@4632 1899 short ret;
michaelm@4632 1900 struct ifreq if2;
michaelm@4632 1901
michaelm@4632 1902 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1903 strcpy(if2.ifr_name, ifname);
michaelm@4632 1904
michaelm@4632 1905 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
michaelm@4632 1906 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
michaelm@4632 1907 return -1;
michaelm@4632 1908 }
michaelm@4632 1909
michaelm@4632 1910 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
michaelm@4632 1911 ret = 0;
michaelm@4632 1912 while (mask) {
michaelm@4632 1913 mask <<= 1;
michaelm@4632 1914 ret++;
michaelm@4632 1915 }
michaelm@4632 1916
michaelm@4632 1917 return ret;
michaelm@4632 1918 }
michaelm@4632 1919
michaelm@4632 1920 /**
michaelm@4632 1921 * Get the Hardware address (usually MAC address) for the named interface.
michaelm@4632 1922 * return puts the data in buf, and returns the length, in byte, of the
michaelm@4632 1923 * MAC address. Returns -1 if there is no hardware address on that interface.
michaelm@4632 1924 */
michaelm@4632 1925 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
michaelm@4632 1926 struct ifaddrs *ifa0, *ifa;
michaelm@4632 1927 struct sockaddr *saddr;
michaelm@4632 1928 int i;
michaelm@4632 1929
michaelm@4632 1930 /* Grab the interface list */
michaelm@4632 1931 if (!getifaddrs(&ifa0)) {
michaelm@4632 1932 /* Cycle through the interfaces */
michaelm@4632 1933 for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
michaelm@4632 1934 saddr = ifa->ifa_addr;
michaelm@4632 1935 /* Link layer contains the MAC address */
michaelm@4632 1936 if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
michaelm@4632 1937 struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
michaelm@4632 1938 /* Check the address is the correct length */
michaelm@4632 1939 if (sadl->sdl_alen == ETHER_ADDR_LEN) {
michaelm@4632 1940 memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
michaelm@4632 1941 freeifaddrs(ifa0);
michaelm@4632 1942 return ETHER_ADDR_LEN;
michaelm@4632 1943 }
michaelm@4632 1944 }
michaelm@4632 1945 }
michaelm@4632 1946 freeifaddrs(ifa0);
michaelm@4632 1947 }
michaelm@4632 1948
michaelm@4632 1949 return -1;
michaelm@4632 1950 }
michaelm@4632 1951
michaelm@4632 1952 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
michaelm@4632 1953 struct ifreq if2;
michaelm@4632 1954
michaelm@4632 1955 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1956 strcpy(if2.ifr_name, ifname);
michaelm@4632 1957
michaelm@4632 1958 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
michaelm@4632 1959 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
michaelm@4632 1960 return -1;
michaelm@4632 1961 }
michaelm@4632 1962
michaelm@4632 1963 return if2.ifr_mtu;
michaelm@4632 1964 }
michaelm@4632 1965
michaelm@4632 1966 static int getFlags(int sock, const char *ifname) {
michaelm@4632 1967 struct ifreq if2;
michaelm@4632 1968 int ret = -1;
michaelm@4632 1969
michaelm@4632 1970 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1971 strcpy(if2.ifr_name, ifname);
michaelm@4632 1972
michaelm@4632 1973 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
michaelm@4632 1974 return -1;
michaelm@4632 1975 }
michaelm@4632 1976
michaelm@4632 1977 return (((int) if2.ifr_flags) & 0xffff);
michaelm@4632 1978 }
michaelm@4632 1979
michaelm@4632 1980 #endif