annotate src/solaris/native/java/net/NetworkInterface.c @ 6568:861e489158ef

Added tag jdk7u60-b00 for changeset db5a29c812ee
author katleman
date Thu, 12 Sep 2013 17:17:40 -0700
parents eefd9678efbd
children 95eeca6dc691
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
shirishk@5523 148 static int getFlags(int sock, const char *ifname, int *flags);
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;
shirishk@5523 560 int flags = 0;
dsamersoff@2545 561
dsamersoff@2545 562 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 563
dsamersoff@2545 564 if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
dsamersoff@2545 565 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 566 return -1;
dsamersoff@2545 567 }
dsamersoff@2545 568
dsamersoff@2545 569 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
dsamersoff@2545 570
shirishk@5523 571 ret = getFlags(sock, name_utf, &flags);
dsamersoff@2545 572
dsamersoff@2545 573 close(sock);
dsamersoff@2545 574 (*env)->ReleaseStringUTFChars(env, name, name_utf);
dsamersoff@2545 575
dsamersoff@2545 576 if (ret < 0) {
dsamersoff@2545 577 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 578 return -1;
dsamersoff@2545 579 }
dsamersoff@2545 580
shirishk@5523 581 return flags;
dsamersoff@2545 582 }
dsamersoff@2545 583
dsamersoff@2545 584
dsamersoff@2545 585
dsamersoff@2545 586
duke@0 587 /*
duke@0 588 * Create a NetworkInterface object, populate the name and index, and
duke@0 589 * populate the InetAddress array based on the IP addresses for this
duke@0 590 * interface.
duke@0 591 */
dsamersoff@2545 592 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
duke@0 593 jobject netifObj;
duke@0 594 jobject name;
duke@0 595 jobjectArray addrArr;
duke@0 596 jobjectArray bindArr;
duke@0 597 jobjectArray childArr;
dsamersoff@2545 598 netaddr *addrs;
duke@0 599 jint addr_index, addr_count, bind_index;
duke@0 600 jint child_count, child_index;
duke@0 601 netaddr *addrP;
duke@0 602 netif *childP;
duke@0 603 jobject tmp;
duke@0 604
duke@0 605 /*
duke@0 606 * Create a NetworkInterface object and populate it
duke@0 607 */
duke@0 608 netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
duke@0 609 name = (*env)->NewStringUTF(env, ifs->name);
duke@0 610 if (netifObj == NULL || name == NULL) {
duke@0 611 return NULL;
duke@0 612 }
duke@0 613 (*env)->SetObjectField(env, netifObj, ni_nameID, name);
duke@0 614 (*env)->SetObjectField(env, netifObj, ni_descID, name);
duke@0 615 (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
duke@0 616 (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
duke@0 617
duke@0 618 /*
duke@0 619 * Count the number of address on this interface
duke@0 620 */
duke@0 621 addr_count = 0;
duke@0 622 addrP = ifs->addr;
duke@0 623 while (addrP != NULL) {
duke@0 624 addr_count++;
duke@0 625 addrP = addrP->next;
duke@0 626 }
duke@0 627
duke@0 628 /*
duke@0 629 * Create the array of InetAddresses
duke@0 630 */
duke@0 631 addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
duke@0 632 if (addrArr == NULL) {
duke@0 633 return NULL;
duke@0 634 }
duke@0 635
duke@0 636 bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
duke@0 637 if (bindArr == NULL) {
dsamersoff@2545 638 return NULL;
duke@0 639 }
duke@0 640 addrP = ifs->addr;
duke@0 641 addr_index = 0;
duke@0 642 bind_index = 0;
duke@0 643 while (addrP != NULL) {
duke@0 644 jobject iaObj = NULL;
duke@0 645 jobject ibObj = NULL;
duke@0 646
duke@0 647 if (addrP->family == AF_INET) {
duke@0 648 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
duke@0 649 if (iaObj) {
michaelm@5888 650 setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
duke@0 651 }
duke@0 652 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
duke@0 653 if (ibObj) {
dsamersoff@2545 654 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
dsamersoff@2545 655 if (addrP->brdcast) {
dsamersoff@2545 656 jobject ia2Obj = NULL;
dsamersoff@2545 657 ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
dsamersoff@2545 658 if (ia2Obj) {
michaelm@5888 659 setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
dsamersoff@2545 660 (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
dsamersoff@2545 661 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
dsamersoff@2545 662 }
dsamersoff@2545 663 }
dsamersoff@2545 664 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
duke@0 665 }
duke@0 666 }
duke@0 667
duke@0 668 #ifdef AF_INET6
duke@0 669 if (addrP->family == AF_INET6) {
duke@0 670 int scope=0;
duke@0 671 iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
duke@0 672 if (iaObj) {
duke@0 673 jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
duke@0 674 if (ipaddress == NULL) {
duke@0 675 return NULL;
duke@0 676 }
duke@0 677 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
dsamersoff@2545 678 (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
dsamersoff@2545 679
duke@0 680 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
dsamersoff@2545 681
duke@0 682 if (scope != 0) { /* zero is default value, no need to set */
duke@0 683 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
duke@0 684 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
duke@0 685 (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
duke@0 686 }
duke@0 687 (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
duke@0 688 }
duke@0 689 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
duke@0 690 if (ibObj) {
dsamersoff@2545 691 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
dsamersoff@2545 692 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
dsamersoff@2545 693 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
duke@0 694 }
duke@0 695 }
duke@0 696 #endif
duke@0 697
duke@0 698 if (iaObj == NULL) {
duke@0 699 return NULL;
duke@0 700 }
duke@0 701
duke@0 702 (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
duke@0 703 addrP = addrP->next;
duke@0 704 }
duke@0 705
duke@0 706 /*
duke@0 707 * See if there is any virtual interface attached to this one.
duke@0 708 */
duke@0 709 child_count = 0;
duke@0 710 childP = ifs->childs;
duke@0 711 while (childP) {
dsamersoff@2545 712 child_count++;
dsamersoff@2545 713 childP = childP->next;
duke@0 714 }
duke@0 715
duke@0 716 childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
duke@0 717 if (childArr == NULL) {
dsamersoff@2545 718 return NULL;
duke@0 719 }
duke@0 720
duke@0 721 /*
duke@0 722 * Create the NetworkInterface instances for the sub-interfaces as
duke@0 723 * well.
duke@0 724 */
duke@0 725 child_index = 0;
duke@0 726 childP = ifs->childs;
duke@0 727 while(childP) {
duke@0 728 tmp = createNetworkInterface(env, childP);
duke@0 729 if (tmp == NULL) {
dsamersoff@2545 730 return NULL;
duke@0 731 }
duke@0 732 (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
duke@0 733 (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
duke@0 734 childP = childP->next;
duke@0 735 }
duke@0 736 (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
duke@0 737 (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
duke@0 738 (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
duke@0 739
duke@0 740 /* return the NetworkInterface */
duke@0 741 return netifObj;
duke@0 742 }
duke@0 743
duke@0 744 /*
duke@0 745 * Enumerates all interfaces
duke@0 746 */
duke@0 747 static netif *enumInterfaces(JNIEnv *env) {
duke@0 748 netif *ifs;
dsamersoff@2545 749 int sock;
duke@0 750
duke@0 751 /*
duke@0 752 * Enumerate IPv4 addresses
duke@0 753 */
dsamersoff@2545 754
dsamersoff@2545 755 sock = openSocket(env, AF_INET);
dsamersoff@2545 756 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
dsamersoff@2545 757 return NULL;
duke@0 758 }
duke@0 759
dsamersoff@2545 760 ifs = enumIPv4Interfaces(env, sock, NULL);
dsamersoff@2545 761 close(sock);
dsamersoff@2545 762
dsamersoff@2545 763 if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
dsamersoff@2545 764 return NULL;
dsamersoff@2545 765 }
dsamersoff@2545 766
dsamersoff@2545 767 /* return partial list if exception occure in the middle of process ???*/
dsamersoff@2545 768
duke@0 769 /*
duke@0 770 * If IPv6 is available then enumerate IPv6 addresses.
duke@0 771 */
duke@0 772 #ifdef AF_INET6
dsamersoff@2545 773
dsamersoff@2605 774 /* User can disable ipv6 expicitly by -Djava.net.preferIPv4Stack=true,
dsamersoff@2605 775 * so we have to call ipv6_available()
dsamersoff@2605 776 */
dsamersoff@2605 777 if (ipv6_available()) {
duke@0 778
dsamersoff@2605 779 sock = openSocket(env, AF_INET6);
dsamersoff@2605 780 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
dsamersoff@2605 781 freeif(ifs);
dsamersoff@2605 782 return NULL;
dsamersoff@2605 783 }
dsamersoff@2605 784
dsamersoff@2605 785 ifs = enumIPv6Interfaces(env, sock, ifs);
dsamersoff@2605 786 close(sock);
dsamersoff@2605 787
dsamersoff@2605 788 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2605 789 freeif(ifs);
dsamersoff@2605 790 return NULL;
dsamersoff@2605 791 }
dsamersoff@2605 792
dsamersoff@2605 793 }
duke@0 794 #endif
duke@0 795
duke@0 796 return ifs;
duke@0 797 }
duke@0 798
dsamersoff@2545 799 #define CHECKED_MALLOC3(_pointer,_type,_size) \
dsamersoff@2545 800 do{ \
dsamersoff@2545 801 _pointer = (_type)malloc( _size ); \
dsamersoff@2545 802 if (_pointer == NULL) { \
zhouyx@5476 803 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
dsamersoff@2545 804 return ifs; /* return untouched list */ \
dsamersoff@2545 805 } \
dsamersoff@2545 806 } while(0)
duke@0 807
duke@0 808
duke@0 809 /*
duke@0 810 * Free an interface list (including any attached addresses)
duke@0 811 */
duke@0 812 void freeif(netif *ifs) {
duke@0 813 netif *currif = ifs;
dsamersoff@2545 814 netif *child = NULL;
duke@0 815
duke@0 816 while (currif != NULL) {
duke@0 817 netaddr *addrP = currif->addr;
duke@0 818 while (addrP != NULL) {
duke@0 819 netaddr *next = addrP->next;
duke@0 820 free(addrP);
duke@0 821 addrP = next;
dsamersoff@2545 822 }
duke@0 823
dsamersoff@2545 824 /*
dsamersoff@2545 825 * Don't forget to free the sub-interfaces.
dsamersoff@2545 826 */
dsamersoff@2545 827 if (currif->childs != NULL) {
dsamersoff@2545 828 freeif(currif->childs);
dsamersoff@2545 829 }
duke@0 830
dsamersoff@2545 831 ifs = currif->next;
dsamersoff@2545 832 free(currif);
dsamersoff@2545 833 currif = ifs;
duke@0 834 }
duke@0 835 }
duke@0 836
khazra@6071 837 netif *addif(JNIEnv *env, int sock, const char * if_name,
khazra@6071 838 netif *ifs, struct sockaddr* ifr_addrP, int family,
khazra@6071 839 short prefix)
khazra@6071 840 {
dsamersoff@2545 841 netif *currif = ifs, *parent;
duke@0 842 netaddr *addrP;
dsamersoff@2545 843
michaelm@4632 844 #ifdef LIFNAMSIZ
khazra@6071 845 int ifnam_size = LIFNAMSIZ;
khazra@6071 846 char name[LIFNAMSIZ], vname[LIFNAMSIZ];
michaelm@4632 847 #else
khazra@6071 848 int ifnam_size = IFNAMSIZ;
khazra@6071 849 char name[IFNAMSIZ], vname[IFNAMSIZ];
michaelm@4632 850 #endif
dsamersoff@2545 851
dsamersoff@2545 852 char *name_colonP;
dsamersoff@2545 853 int mask;
duke@0 854 int isVirtual = 0;
dsamersoff@2545 855 int addr_size;
shirishk@5523 856 int flags = 0;
duke@0 857
duke@0 858 /*
duke@0 859 * If the interface name is a logical interface then we
duke@0 860 * remove the unit number so that we have the physical
duke@0 861 * interface (eg: hme0:1 -> hme0). NetworkInterface
duke@0 862 * currently doesn't have any concept of physical vs.
duke@0 863 * logical interfaces.
duke@0 864 */
khazra@6071 865 strncpy(name, if_name, ifnam_size);
khazra@6071 866 name[ifnam_size - 1] = '\0';
dsamersoff@2545 867 *vname = 0;
duke@0 868
duke@0 869 /*
duke@0 870 * Create and populate the netaddr node. If allocation fails
duke@0 871 * return an un-updated list.
duke@0 872 */
dsamersoff@2545 873 /*Allocate for addr and brdcast at once*/
dsamersoff@2545 874
dsamersoff@2545 875 #ifdef AF_INET6
dsamersoff@2545 876 addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
dsamersoff@2545 877 #else
dsamersoff@2545 878 addr_size = sizeof(struct sockaddr_in);
dsamersoff@2545 879 #endif
dsamersoff@2545 880
dsamersoff@2545 881 CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 882 addrP->addr = (struct sockaddr *)( (char *) addrP+sizeof(netaddr) );
dsamersoff@2545 883 memcpy(addrP->addr, ifr_addrP, addr_size);
dsamersoff@2545 884
duke@0 885 addrP->family = family;
duke@0 886 addrP->brdcast = NULL;
duke@0 887 addrP->mask = prefix;
dsamersoff@2545 888 addrP->next = 0;
duke@0 889 if (family == AF_INET) {
duke@0 890 /*
duke@0 891 * Deal with brodcast addr & subnet mask
duke@0 892 */
dsamersoff@2545 893 struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size);
dsamersoff@2545 894 addrP->brdcast = getBroadcast(env, sock, name, brdcast_to );
duke@0 895
dsamersoff@2545 896 if (addrP->brdcast && (mask = getSubnet(env, sock, name)) != -1) {
dsamersoff@2545 897 addrP->mask = mask;
dsamersoff@2545 898 }
dsamersoff@2545 899 }
dsamersoff@2545 900
dsamersoff@2545 901 /**
dsamersoff@2545 902 * Deal with virtual interface with colon notaion e.g. eth0:1
dsamersoff@2545 903 */
dsamersoff@2545 904 name_colonP = strchr(name, ':');
dsamersoff@2545 905 if (name_colonP != NULL) {
duke@0 906 /**
duke@0 907 * This is a virtual interface. If we are able to access the parent
duke@0 908 * we need to create a new entry if it doesn't exist yet *and* update
duke@0 909 * the 'parent' interface with the new records.
duke@0 910 */
dsamersoff@2545 911 *name_colonP = 0;
shirishk@5523 912 if (getFlags(sock, name, &flags) < 0 || flags < 0) {
chegar@894 913 // failed to access parent interface do not create parent.
chegar@894 914 // We are a virtual interface with no parent.
chegar@894 915 isVirtual = 1;
dsamersoff@2545 916 *name_colonP = ':';
duke@0 917 }
dsamersoff@2545 918 else{
dsamersoff@2545 919 // Got access to parent, so create it if necessary.
dsamersoff@2545 920 // Save original name to vname and truncate name by ':'
dsamersoff@2545 921 memcpy(vname, name, sizeof(vname) );
dsamersoff@2545 922 vname[name_colonP - name] = ':';
dsamersoff@2545 923 }
duke@0 924 }
duke@0 925
duke@0 926 /*
duke@0 927 * Check if this is a "new" interface. Use the interface
duke@0 928 * name for matching because index isn't supported on
duke@0 929 * Solaris 2.6 & 7.
duke@0 930 */
duke@0 931 while (currif != NULL) {
duke@0 932 if (strcmp(name, currif->name) == 0) {
duke@0 933 break;
duke@0 934 }
duke@0 935 currif = currif->next;
duke@0 936 }
duke@0 937
duke@0 938 /*
duke@0 939 * If "new" then create an netif structure and
duke@0 940 * insert it onto the list.
duke@0 941 */
duke@0 942 if (currif == NULL) {
khazra@6071 943 CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
dsamersoff@2545 944 currif->name = (char *) currif+sizeof(netif);
khazra@6071 945 strncpy(currif->name, name, ifnam_size);
khazra@6071 946 currif->name[ifnam_size - 1] = '\0';
chegar@2578 947 currif->index = getIndex(sock, name);
dsamersoff@2545 948 currif->addr = NULL;
dsamersoff@2545 949 currif->childs = NULL;
dsamersoff@2545 950 currif->virtual = isVirtual;
dsamersoff@2545 951 currif->next = ifs;
dsamersoff@2545 952 ifs = currif;
duke@0 953 }
duke@0 954
duke@0 955 /*
duke@0 956 * Finally insert the address on the interface
duke@0 957 */
duke@0 958 addrP->next = currif->addr;
duke@0 959 currif->addr = addrP;
duke@0 960
duke@0 961 parent = currif;
duke@0 962
duke@0 963 /**
duke@0 964 * Let's deal with the virtual interface now.
duke@0 965 */
duke@0 966 if (vname[0]) {
dsamersoff@2545 967 netaddr *tmpaddr;
duke@0 968
dsamersoff@2545 969 currif = parent->childs;
duke@0 970
dsamersoff@2545 971 while (currif != NULL) {
dsamersoff@2545 972 if (strcmp(vname, currif->name) == 0) {
dsamersoff@2545 973 break;
dsamersoff@2545 974 }
dsamersoff@2545 975 currif = currif->next;
duke@0 976 }
dsamersoff@2545 977
dsamersoff@2545 978 if (currif == NULL) {
khazra@6071 979 CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
dsamersoff@2545 980 currif->name = (char *) currif + sizeof(netif);
khazra@6071 981 strncpy(currif->name, vname, ifnam_size);
khazra@6071 982 currif->name[ifnam_size - 1] = '\0';
chegar@2578 983 currif->index = getIndex(sock, vname);
dsamersoff@2545 984 currif->addr = NULL;
dsamersoff@2545 985 /* Need to duplicate the addr entry? */
dsamersoff@2545 986 currif->virtual = 1;
dsamersoff@2545 987 currif->childs = NULL;
dsamersoff@2545 988 currif->next = parent->childs;
dsamersoff@2545 989 parent->childs = currif;
duke@0 990 }
dsamersoff@2545 991
dsamersoff@2545 992 CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 993 memcpy(tmpaddr, addrP, sizeof(netaddr));
dsamersoff@2545 994 if (addrP->addr != NULL) {
dsamersoff@2545 995 tmpaddr->addr = (struct sockaddr *) ( (char*)tmpaddr + sizeof(netaddr) ) ;
dsamersoff@2545 996 memcpy(tmpaddr->addr, addrP->addr, addr_size);
duke@0 997 }
duke@0 998
dsamersoff@2545 999 if (addrP->brdcast != NULL) {
dsamersoff@2545 1000 tmpaddr->brdcast = (struct sockaddr *) ((char *) tmpaddr + sizeof(netaddr)+addr_size);
dsamersoff@2545 1001 memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
dsamersoff@2545 1002 }
dsamersoff@2545 1003
dsamersoff@2545 1004 tmpaddr->next = currif->addr;
dsamersoff@2545 1005 currif->addr = tmpaddr;
duke@0 1006 }
duke@0 1007
duke@0 1008 return ifs;
duke@0 1009 }
duke@0 1010
dsamersoff@2545 1011 /* Open socket for further ioct calls
dsamersoff@2545 1012 * proto is AF_INET/AF_INET6
duke@0 1013 */
dsamersoff@2545 1014 static int openSocket(JNIEnv *env, int proto){
dsamersoff@2545 1015 int sock;
duke@0 1016
dsamersoff@2545 1017 if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1018 /*
dsamersoff@2545 1019 * If EPROTONOSUPPORT is returned it means we don't have
dsamersoff@2545 1020 * support for this proto so don't throw an exception.
dsamersoff@2545 1021 */
dsamersoff@2545 1022 if (errno != EPROTONOSUPPORT) {
dsamersoff@2545 1023 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
dsamersoff@2545 1024 }
dsamersoff@2545 1025 return -1;
chegar@375 1026 }
chegar@375 1027
dsamersoff@2545 1028 return sock;
dsamersoff@2545 1029 }
chegar@375 1030
dsamersoff@2545 1031
dsamersoff@2545 1032 /** Linux **/
dsamersoff@2545 1033 #ifdef __linux__
dsamersoff@2545 1034 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1035 * if it falls return v6 socket
dsamersoff@2545 1036 */
dsamersoff@2545 1037
dsamersoff@2545 1038 #ifdef AF_INET6
dsamersoff@2545 1039 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1040 int sock;
dsamersoff@2545 1041 struct ifreq if2;
dsamersoff@2545 1042
dsamersoff@2545 1043 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1044 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1045 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1046 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1047 return -1;
dsamersoff@2545 1048 }
dsamersoff@2545 1049 }
dsamersoff@2545 1050 else{ // errno is not NOSUPPORT
dsamersoff@2545 1051 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1052 return -1;
dsamersoff@2545 1053 }
dsamersoff@2545 1054 }
dsamersoff@2545 1055
dsamersoff@2545 1056 /* Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or IPv6 socket regardless of type
dsamersoff@2545 1057 of address of an interface */
dsamersoff@2545 1058
dsamersoff@2545 1059 return sock;
dsamersoff@2545 1060 }
dsamersoff@2545 1061
dsamersoff@2545 1062 #else
dsamersoff@2545 1063 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1064 return openSocket(env,AF_INET);
dsamersoff@2545 1065 }
dsamersoff@2545 1066 #endif
dsamersoff@2545 1067
dsamersoff@2545 1068 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1069 struct ifconf ifc;
dsamersoff@2545 1070 struct ifreq *ifreqP;
dsamersoff@2545 1071 char *buf;
dsamersoff@2545 1072 int numifs;
dsamersoff@2545 1073 unsigned i;
dsamersoff@2545 1074
dsamersoff@2545 1075
dsamersoff@2545 1076 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
dsamersoff@2545 1077 * SIOCGIFCOUNT doesn't work
dsamersoff@2545 1078 */
dsamersoff@2545 1079 ifc.ifc_buf = NULL;
dsamersoff@2545 1080 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1081 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
dsamersoff@2545 1082 return ifs;
chegar@375 1083 }
dsamersoff@2545 1084
dsamersoff@2545 1085 CHECKED_MALLOC3(buf,char *, ifc.ifc_len);
dsamersoff@2545 1086
dsamersoff@2545 1087 ifc.ifc_buf = buf;
dsamersoff@2545 1088 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1089 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
dsamersoff@2545 1090 (void) free(buf);
dsamersoff@2545 1091 return ifs;
dsamersoff@2545 1092 }
dsamersoff@2545 1093
dsamersoff@2545 1094 /*
dsamersoff@2545 1095 * Iterate through each interface
dsamersoff@2545 1096 */
dsamersoff@2545 1097 ifreqP = ifc.ifc_req;
dsamersoff@2545 1098 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
dsamersoff@2545 1099 /*
dsamersoff@2545 1100 * Add to the list
dsamersoff@2545 1101 */
dsamersoff@2545 1102 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
dsamersoff@2545 1103
dsamersoff@2545 1104 /*
dsamersoff@2545 1105 * If an exception occurred then free the list
dsamersoff@2545 1106 */
dsamersoff@2545 1107 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1108 free(buf);
dsamersoff@2545 1109 freeif(ifs);
dsamersoff@2545 1110 return NULL;
dsamersoff@2545 1111 }
dsamersoff@2545 1112 }
dsamersoff@2545 1113
dsamersoff@2545 1114 /*
dsamersoff@2545 1115 * Free socket and buffer
dsamersoff@2545 1116 */
dsamersoff@2545 1117 free(buf);
dsamersoff@2545 1118 return ifs;
dsamersoff@2545 1119 }
dsamersoff@2545 1120
dsamersoff@2545 1121
dsamersoff@2545 1122 /*
dsamersoff@2545 1123 * Enumerates and returns all IPv6 interfaces on Linux
dsamersoff@2545 1124 */
dsamersoff@2545 1125
dsamersoff@2545 1126 #ifdef AF_INET6
dsamersoff@2545 1127 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1128 FILE *f;
chegar@3657 1129 char addr6[40], devname[21];
dsamersoff@2545 1130 char addr6p[8][5];
dsamersoff@2545 1131 int plen, scope, dad_status, if_idx;
dsamersoff@2545 1132 uint8_t ipv6addr[16];
dsamersoff@2545 1133
dsamersoff@2545 1134 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
michaelm@5826 1135 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
dsamersoff@2545 1136 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7],
dsamersoff@2545 1137 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
dsamersoff@2545 1138
dsamersoff@2545 1139 struct netif *ifs_ptr = NULL;
dsamersoff@2545 1140 struct netif *last_ptr = NULL;
dsamersoff@2545 1141 struct sockaddr_in6 addr;
dsamersoff@2545 1142
dsamersoff@2545 1143 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
dsamersoff@2545 1144 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
dsamersoff@2545 1145 inet_pton(AF_INET6, addr6, ipv6addr);
dsamersoff@2545 1146
dsamersoff@2545 1147 memset(&addr, 0, sizeof(struct sockaddr_in6));
dsamersoff@2545 1148 memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
dsamersoff@2545 1149
dsamersoff@2545 1150 addr.sin6_scope_id = if_idx;
dsamersoff@2545 1151
dsamersoff@2545 1152 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
dsamersoff@2545 1153
dsamersoff@2545 1154
dsamersoff@2545 1155 /*
dsamersoff@2545 1156 * If an exception occurred then return the list as is.
dsamersoff@2545 1157 */
dsamersoff@2545 1158 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1159 fclose(f);
dsamersoff@2545 1160 return ifs;
dsamersoff@2545 1161 }
dsamersoff@2545 1162 }
dsamersoff@2545 1163 fclose(f);
dsamersoff@2545 1164 }
dsamersoff@2545 1165 return ifs;
dsamersoff@2545 1166 }
chegar@375 1167 #endif
duke@0 1168
dsamersoff@2545 1169
chegar@2578 1170 static int getIndex(int sock, const char *name){
dsamersoff@2545 1171 /*
dsamersoff@2545 1172 * Try to get the interface index
dsamersoff@2545 1173 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1174 */
dsamersoff@2545 1175 struct ifreq if2;
dsamersoff@2545 1176 strcpy(if2.ifr_name, name);
dsamersoff@2545 1177
dsamersoff@2545 1178 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1179 return -1;
dsamersoff@2545 1180 }
dsamersoff@2545 1181
dsamersoff@2545 1182 return if2.ifr_ifindex;
duke@0 1183 }
duke@0 1184
duke@0 1185 /**
duke@0 1186 * Returns the IPv4 broadcast address of a named interface, if it exists.
duke@0 1187 * Returns 0 if it doesn't have one.
duke@0 1188 */
dsamersoff@2545 1189 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
duke@0 1190 struct sockaddr *ret = NULL;
duke@0 1191 struct ifreq if2;
duke@0 1192
duke@0 1193 memset((char *) &if2, 0, sizeof(if2));
duke@0 1194 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1195
duke@0 1196 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1197 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1198 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
dsamersoff@2545 1199 return ret;
duke@0 1200 }
dsamersoff@2545 1201
dsamersoff@2545 1202 if (if2.ifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1203 /* It does, let's retrieve it*/
dsamersoff@2545 1204 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1205 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
dsamersoff@2545 1206 return ret;
dsamersoff@2545 1207 }
dsamersoff@2545 1208
dsamersoff@2545 1209 ret = brdcast_store;
duke@0 1210 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
duke@0 1211 }
dsamersoff@2545 1212
duke@0 1213 return ret;
duke@0 1214 }
duke@0 1215
duke@0 1216 /**
duke@0 1217 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
duke@0 1218 * interface, if it has one, otherwise return -1.
duke@0 1219 */
dsamersoff@2545 1220 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1221 unsigned int mask;
dsamersoff@2545 1222 short ret;
dsamersoff@2545 1223 struct ifreq if2;
dsamersoff@2545 1224
dsamersoff@2545 1225 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1226 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1227
dsamersoff@2545 1228 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1229 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
dsamersoff@2545 1230 return -1;
dsamersoff@2545 1231 }
dsamersoff@2545 1232
dsamersoff@2545 1233 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1234 ret = 0;
dsamersoff@2545 1235 while (mask) {
dsamersoff@2545 1236 mask <<= 1;
dsamersoff@2545 1237 ret++;
dsamersoff@2545 1238 }
dsamersoff@2545 1239
dsamersoff@2545 1240 return ret;
dsamersoff@2545 1241 }
dsamersoff@2545 1242
dsamersoff@2545 1243 /**
dsamersoff@2545 1244 * Get the Hardware address (usually MAC address) for the named interface.
dsamersoff@2545 1245 * return puts the data in buf, and returns the length, in byte, of the
dsamersoff@2545 1246 * MAC address. Returns -1 if there is no hardware address on that interface.
dsamersoff@2545 1247 */
dsamersoff@2545 1248 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1249 static struct ifreq ifr;
dsamersoff@2545 1250 int i;
dsamersoff@2545 1251
dsamersoff@2545 1252 strcpy(ifr.ifr_name, ifname);
dsamersoff@2545 1253 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
dsamersoff@2545 1254 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
dsamersoff@2545 1255 return -1;
dsamersoff@2545 1256 }
dsamersoff@2545 1257
dsamersoff@2545 1258 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
dsamersoff@2545 1259
dsamersoff@2545 1260 /*
dsamersoff@2545 1261 * All bytes to 0 means no hardware address.
dsamersoff@2545 1262 */
dsamersoff@2545 1263
dsamersoff@2545 1264 for (i = 0; i < IFHWADDRLEN; i++) {
dsamersoff@2545 1265 if (buf[i] != 0)
dsamersoff@2545 1266 return IFHWADDRLEN;
dsamersoff@2545 1267 }
dsamersoff@2545 1268
dsamersoff@2545 1269 return -1;
dsamersoff@2545 1270 }
dsamersoff@2545 1271
dsamersoff@2545 1272 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1273 struct ifreq if2;
dsamersoff@2545 1274
dsamersoff@2545 1275 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1276 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1277
dsamersoff@2545 1278 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1279 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
dsamersoff@2545 1280 return -1;
dsamersoff@2545 1281 }
dsamersoff@2545 1282
dsamersoff@2545 1283 return if2.ifr_mtu;
dsamersoff@2545 1284 }
dsamersoff@2545 1285
shirishk@5523 1286 static int getFlags(int sock, const char *ifname, int *flags) {
duke@0 1287 struct ifreq if2;
duke@0 1288
duke@0 1289 memset((char *) &if2, 0, sizeof(if2));
duke@0 1290 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1291
dsamersoff@2545 1292 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
dsamersoff@2545 1293 return -1;
duke@0 1294 }
dsamersoff@2545 1295
shirishk@5523 1296 if (sizeof(if2.ifr_flags) == sizeof(short)) {
shirishk@5523 1297 *flags = (if2.ifr_flags & 0xffff);
shirishk@5523 1298 } else {
shirishk@5523 1299 *flags = if2.ifr_flags;
shirishk@5523 1300 }
shirishk@5523 1301 return 0;
duke@0 1302 }
duke@0 1303
dsamersoff@2545 1304 #endif
dsamersoff@2545 1305
dsamersoff@2545 1306 /** Solaris **/
duke@0 1307 #ifdef __solaris__
dsamersoff@2545 1308 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1309 * if it falls return v6 socket
dsamersoff@2545 1310 */
dsamersoff@2545 1311
dsamersoff@2545 1312 #ifdef AF_INET6
dsamersoff@2545 1313 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1314 int sock, alreadyV6 = 0;
dsamersoff@2545 1315 struct lifreq if2;
dsamersoff@2545 1316
dsamersoff@2545 1317 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1318 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1319 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1320 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1321 return -1;
dsamersoff@2545 1322 }
dsamersoff@2545 1323
dsamersoff@2545 1324 alreadyV6=1;
dsamersoff@2545 1325 }
dsamersoff@2545 1326 else{ // errno is not NOSUPPORT
dsamersoff@2545 1327 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1328 return -1;
dsamersoff@2545 1329 }
dsamersoff@2545 1330 }
dsamersoff@2545 1331
dsamersoff@2545 1332 /**
dsamersoff@2545 1333 * Solaris requires that we have IPv6 socket to query an
dsamersoff@2545 1334 * interface without IPv4 address - check it here
dsamersoff@2545 1335 * POSIX 1 require the kernell to return ENOTTY if the call is
dsamersoff@2545 1336 * unappropriate for device e.g. NETMASK for device having IPv6
dsamersoff@2545 1337 * only address but not all devices follows the standart so
dsamersoff@2545 1338 * fallback on any error. It's not an ecology friendly but more
dsamersoff@2545 1339 * reliable.
dsamersoff@2545 1340 */
dsamersoff@2545 1341
dsamersoff@2545 1342 if (! alreadyV6 ){
dsamersoff@2545 1343 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1344 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1345 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1346 close(sock);
dsamersoff@2545 1347 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1348 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1349 return -1;
dsamersoff@2545 1350 }
dsamersoff@2545 1351 }
dsamersoff@2545 1352 }
dsamersoff@2545 1353
dsamersoff@2545 1354 return sock;
dsamersoff@2545 1355 }
dsamersoff@2545 1356
dsamersoff@2545 1357 #else
dsamersoff@2545 1358 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1359 return openSocket(env,AF_INET);
dsamersoff@2545 1360 }
dsamersoff@2545 1361 #endif
dsamersoff@2545 1362
dsamersoff@2545 1363 /*
dsamersoff@2545 1364 * Enumerates and returns all IPv4 interfaces
dsamersoff@2545 1365 * (linux verison)
dsamersoff@2545 1366 */
dsamersoff@2545 1367
dsamersoff@2545 1368 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1369 return enumIPvXInterfaces(env,sock, ifs, AF_INET);
dsamersoff@2545 1370 }
dsamersoff@2545 1371
dsamersoff@2545 1372 #ifdef AF_INET6
dsamersoff@2545 1373 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1374 return enumIPvXInterfaces(env,sock, ifs, AF_INET6);
dsamersoff@2545 1375 }
dsamersoff@2545 1376 #endif
dsamersoff@2545 1377
dsamersoff@2545 1378 /*
dsamersoff@2545 1379 Enumerates and returns all interfaces on Solaris
dsamersoff@2545 1380 use the same code for IPv4 and IPv6
dsamersoff@2545 1381 */
dsamersoff@2545 1382 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family) {
dsamersoff@2545 1383 struct lifconf ifc;
dsamersoff@2545 1384 struct lifreq *ifr;
dsamersoff@2545 1385 int n;
dsamersoff@2545 1386 char *buf;
dsamersoff@2545 1387 struct lifnum numifs;
dsamersoff@2545 1388 unsigned bufsize;
dsamersoff@2545 1389
dsamersoff@2545 1390 /*
dsamersoff@2545 1391 * Get the interface count
dsamersoff@2545 1392 */
dsamersoff@2545 1393 numifs.lifn_family = family;
dsamersoff@2545 1394 numifs.lifn_flags = 0;
dsamersoff@2545 1395 if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
dsamersoff@2545 1396 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFNUM failed");
dsamersoff@2545 1397 return ifs;
dsamersoff@2545 1398 }
dsamersoff@2545 1399
dsamersoff@2545 1400 /*
dsamersoff@2545 1401 * Enumerate the interface configurations
dsamersoff@2545 1402 */
dsamersoff@2545 1403 bufsize = numifs.lifn_count * sizeof (struct lifreq);
dsamersoff@2545 1404 CHECKED_MALLOC3(buf, char *, bufsize);
dsamersoff@2545 1405
dsamersoff@2545 1406 ifc.lifc_family = family;
dsamersoff@2545 1407 ifc.lifc_flags = 0;
dsamersoff@2545 1408 ifc.lifc_len = bufsize;
dsamersoff@2545 1409 ifc.lifc_buf = buf;
dsamersoff@2545 1410 if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1411 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFCONF failed");
dsamersoff@2545 1412 free(buf);
dsamersoff@2545 1413 return ifs;
dsamersoff@2545 1414 }
dsamersoff@2545 1415
dsamersoff@2545 1416 /*
dsamersoff@2545 1417 * Iterate through each interface
dsamersoff@2545 1418 */
dsamersoff@2545 1419 ifr = ifc.lifc_req;
dsamersoff@2545 1420 for (n=0; n<numifs.lifn_count; n++, ifr++) {
dsamersoff@2545 1421 int index = -1;
dsamersoff@2545 1422 struct lifreq if2;
dsamersoff@2545 1423
dsamersoff@2545 1424 /*
dsamersoff@2545 1425 * Ignore either IPv4 or IPv6 addresses
dsamersoff@2545 1426 */
dsamersoff@2545 1427 if (ifr->lifr_addr.ss_family != family) {
dsamersoff@2545 1428 continue;
dsamersoff@2545 1429 }
dsamersoff@2545 1430
chegar@2578 1431 #ifdef AF_INET6
chegar@2578 1432 if (ifr->lifr_addr.ss_family == AF_INET6) {
chegar@2578 1433 struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
chegar@2578 1434 s6->sin6_scope_id = getIndex(sock, ifr->lifr_name);
chegar@2578 1435 }
chegar@2578 1436 #endif
chegar@2578 1437
dsamersoff@2545 1438 /* add to the list */
dsamersoff@2545 1439 ifs = addif(env, sock,ifr->lifr_name, ifs, (struct sockaddr *)&(ifr->lifr_addr),family, (short) ifr->lifr_addrlen);
dsamersoff@2545 1440
dsamersoff@2545 1441 /*
dsamersoff@2545 1442 * If an exception occurred we return immediately
dsamersoff@2545 1443 */
dsamersoff@2545 1444 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1445 free(buf);
dsamersoff@2545 1446 return ifs;
dsamersoff@2545 1447 }
dsamersoff@2545 1448
dsamersoff@2545 1449 }
dsamersoff@2545 1450
dsamersoff@2545 1451 free(buf);
dsamersoff@2545 1452 return ifs;
dsamersoff@2545 1453 }
dsamersoff@2545 1454
chegar@2578 1455 static int getIndex(int sock, const char *name){
dsamersoff@2545 1456 /*
dsamersoff@2545 1457 * Try to get the interface index
dsamersoff@2545 1458 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1459 */
dsamersoff@2545 1460 struct lifreq if2;
dsamersoff@2545 1461 strcpy(if2.lifr_name, name);
dsamersoff@2545 1462
dsamersoff@2545 1463 if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1464 return -1;
dsamersoff@2545 1465 }
dsamersoff@2545 1466
dsamersoff@2545 1467 return if2.lifr_index;
dsamersoff@2545 1468 }
dsamersoff@2545 1469
dsamersoff@2545 1470 /**
dsamersoff@2545 1471 * Returns the IPv4 broadcast address of a named interface, if it exists.
dsamersoff@2545 1472 * Returns 0 if it doesn't have one.
dsamersoff@2545 1473 */
dsamersoff@2545 1474 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
dsamersoff@2545 1475 struct sockaddr *ret = NULL;
dsamersoff@2545 1476 struct lifreq if2;
dsamersoff@2545 1477
dsamersoff@2545 1478 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1479 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1480
dsamersoff@2545 1481 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1482 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1483 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 1484 return ret;
dsamersoff@2545 1485 }
dsamersoff@2545 1486
dsamersoff@2545 1487 if (if2.lifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1488 /* It does, let's retrieve it*/
dsamersoff@2545 1489 if (ioctl(sock, SIOCGLIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1490 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFBRDADDR failed");
dsamersoff@2545 1491 return ret;
dsamersoff@2545 1492 }
dsamersoff@2545 1493
dsamersoff@2545 1494 ret = brdcast_store;
dsamersoff@2545 1495 memcpy(ret, &if2.lifr_broadaddr, sizeof(struct sockaddr));
dsamersoff@2545 1496 }
dsamersoff@2545 1497
dsamersoff@2545 1498 return ret;
dsamersoff@2545 1499 }
dsamersoff@2545 1500
dsamersoff@2545 1501 /**
dsamersoff@2545 1502 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
dsamersoff@2545 1503 * interface, if it has one, otherwise return -1.
dsamersoff@2545 1504 */
dsamersoff@2545 1505 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1506 unsigned int mask;
dsamersoff@2545 1507 short ret;
dsamersoff@2545 1508 struct lifreq if2;
dsamersoff@2545 1509
dsamersoff@2545 1510 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1511 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1512
dsamersoff@2545 1513 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1514 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFNETMASK failed");
dsamersoff@2545 1515 return -1;
dsamersoff@2545 1516 }
dsamersoff@2545 1517
dsamersoff@2545 1518 mask = ntohl(((struct sockaddr_in*)&(if2.lifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1519 ret = 0;
dsamersoff@2545 1520
dsamersoff@2545 1521 while (mask) {
dsamersoff@2545 1522 mask <<= 1;
dsamersoff@2545 1523 ret++;
dsamersoff@2545 1524 }
dsamersoff@2545 1525
dsamersoff@2545 1526 return ret;
dsamersoff@2545 1527 }
dsamersoff@2545 1528
dsamersoff@2545 1529
dsamersoff@2545 1530
dsamersoff@2545 1531 #define DEV_PREFIX "/dev/"
duke@0 1532
duke@0 1533 /**
duke@0 1534 * Solaris specific DLPI code to get hardware address from a device.
duke@0 1535 * Unfortunately, at least up to Solaris X, you have to have special
duke@0 1536 * privileges (i.e. be root).
duke@0 1537 */
duke@0 1538 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
dsamersoff@2545 1539 char style1dev[MAXPATHLEN];
dsamersoff@2545 1540 int fd;
dsamersoff@2545 1541 dl_phys_addr_req_t dlpareq;
dsamersoff@2545 1542 dl_phys_addr_ack_t *dlpaack;
dsamersoff@2545 1543 struct strbuf msg;
dsamersoff@2545 1544 char buf[128];
dsamersoff@2545 1545 int flags = 0;
duke@0 1546
dsamersoff@2545 1547 /**
dsamersoff@2545 1548 * Device is in /dev
dsamersoff@2545 1549 * e.g.: /dev/bge0
dsamersoff@2545 1550 */
dsamersoff@2545 1551 strcpy(style1dev, DEV_PREFIX);
dsamersoff@2545 1552 strcat(style1dev, ifname);
dsamersoff@2545 1553 if ((fd = open(style1dev, O_RDWR)) < 0) {
dsamersoff@2545 1554 /*
dsamersoff@2545 1555 * Can't open it. We probably are missing the privilege.
dsamersoff@2545 1556 * We'll have to try something else
dsamersoff@2545 1557 */
dsamersoff@2545 1558 return 0;
dsamersoff@2545 1559 }
duke@0 1560
dsamersoff@2545 1561 dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dsamersoff@2545 1562 dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
dsamersoff@2545 1563
dsamersoff@2545 1564 msg.buf = (char *)&dlpareq;
dsamersoff@2545 1565 msg.len = DL_PHYS_ADDR_REQ_SIZE;
dsamersoff@2545 1566
dsamersoff@2545 1567 if (putmsg(fd, &msg, NULL, 0) < 0) {
dsamersoff@2545 1568 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "putmsg failed");
dsamersoff@2545 1569 return -1;
dsamersoff@2545 1570 }
dsamersoff@2545 1571
dsamersoff@2545 1572 dlpaack = (dl_phys_addr_ack_t *)buf;
dsamersoff@2545 1573
dsamersoff@2545 1574 msg.buf = (char *)buf;
dsamersoff@2545 1575 msg.len = 0;
dsamersoff@2545 1576 msg.maxlen = sizeof (buf);
dsamersoff@2545 1577 if (getmsg(fd, &msg, NULL, &flags) < 0) {
dsamersoff@2545 1578 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "getmsg failed");
dsamersoff@2545 1579 return -1;
dsamersoff@2545 1580 }
dsamersoff@2545 1581
dsamersoff@2545 1582 if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
dsamersoff@2545 1583 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Couldn't obtain phys addr\n");
dsamersoff@2545 1584 return -1;
dsamersoff@2545 1585 }
dsamersoff@2545 1586
dsamersoff@2545 1587 memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
dsamersoff@2545 1588 return dlpaack->dl_addr_length;
duke@0 1589 }
duke@0 1590
duke@0 1591 /**
duke@0 1592 * Get the Hardware address (usually MAC address) for the named interface.
duke@0 1593 * return puts the data in buf, and returns the length, in byte, of the
duke@0 1594 * MAC address. Returns -1 if there is no hardware address on that interface.
duke@0 1595 */
dsamersoff@2545 1596 static int getMacAddress(JNIEnv *env, int sock, const char *ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1597 struct arpreq arpreq;
dsamersoff@2545 1598 struct sockaddr_in* sin;
dsamersoff@2545 1599 struct sockaddr_in ipAddr;
dsamersoff@2545 1600 int len, i;
chegar@4501 1601 struct lifreq lif;
chegar@4501 1602
chegar@4501 1603 /* First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
chegar@4501 1604 * try the old way.
chegar@4501 1605 */
chegar@4501 1606 memset(&lif, 0, sizeof(lif));
chegar@4501 1607 strlcpy(lif.lifr_name, ifname, sizeof(lif.lifr_name));
chegar@4501 1608
chegar@4501 1609 if (ioctl(sock, SIOCGLIFHWADDR, &lif) != -1) {
chegar@4501 1610 struct sockaddr_dl *sp;
chegar@4501 1611 sp = (struct sockaddr_dl *)&lif.lifr_addr;
chegar@4501 1612 memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
chegar@4501 1613 return sp->sdl_alen;
chegar@4501 1614 }
duke@0 1615
dsamersoff@2545 1616 /**
dsamersoff@2545 1617 * On Solaris we have to use DLPI, but it will only work if we have
dsamersoff@2545 1618 * privileged access (i.e. root). If that fails, we try a lookup
dsamersoff@2545 1619 * in the ARP table, which requires an IPv4 address.
dsamersoff@2545 1620 */
dsamersoff@2545 1621 if ((len = getMacFromDevice(env, ifname, buf)) == 0) {
dsamersoff@2545 1622 /*DLPI failed - trying to do arp lookup*/
duke@0 1623
chegar@4501 1624 if (addr == NULL) {
chegar@4501 1625 /**
chegar@4501 1626 * No IPv4 address for that interface, so can't do an ARP lookup.
chegar@4501 1627 */
chegar@4501 1628 return -1;
chegar@4501 1629 }
dsamersoff@2545 1630
chegar@4501 1631 len = 6; //???
dsamersoff@2545 1632
chegar@4501 1633 sin = (struct sockaddr_in *) &arpreq.arp_pa;
chegar@4501 1634 memset((char *) &arpreq, 0, sizeof(struct arpreq));
chegar@4501 1635 ipAddr.sin_port = 0;
chegar@4501 1636 ipAddr.sin_family = AF_INET;
chegar@4501 1637 memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
chegar@4501 1638 memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
chegar@4501 1639 arpreq.arp_flags= ATF_PUBL;
dsamersoff@2545 1640
chegar@4501 1641 if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
chegar@4501 1642 return -1;
chegar@4501 1643 }
dsamersoff@2545 1644
chegar@4501 1645 memcpy(buf, &arpreq.arp_ha.sa_data[0], len );
chegar@4501 1646 }
duke@0 1647
dsamersoff@2545 1648 /*
dsamersoff@2545 1649 * All bytes to 0 means no hardware address.
dsamersoff@2545 1650 */
duke@0 1651
dsamersoff@2545 1652 for (i = 0; i < len; i++) {
dsamersoff@2545 1653 if (buf[i] != 0)
dsamersoff@2545 1654 return len;
dsamersoff@2545 1655 }
dsamersoff@2545 1656
duke@0 1657 return -1;
duke@0 1658 }
duke@0 1659
dsamersoff@2545 1660 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1661 struct lifreq if2;
duke@0 1662
dsamersoff@2545 1663 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1664 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1665
dsamersoff@2545 1666 if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1667 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFMTU failed");
dsamersoff@2545 1668 return -1;
dsamersoff@2545 1669 }
dsamersoff@2545 1670
dsamersoff@2545 1671 return if2.lifr_mtu;
duke@0 1672 }
duke@0 1673
dsamersoff@2545 1674
shirishk@5523 1675 static int getFlags(int sock, const char *ifname, int *flags) {
dsamersoff@2545 1676 struct lifreq lifr;
dsamersoff@2545 1677 memset((caddr_t)&lifr, 0, sizeof(lifr));
dsamersoff@2545 1678 strcpy((caddr_t)&(lifr.lifr_name), ifname);
dsamersoff@2545 1679
dsamersoff@2545 1680 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
dsamersoff@2545 1681 return -1;
dsamersoff@2545 1682 }
dsamersoff@2545 1683
shirishk@5523 1684 *flags = lifr.lifr_flags;
shirishk@5523 1685 return 0;
duke@0 1686 }
duke@0 1687
duke@0 1688
dsamersoff@2545 1689 #endif
duke@0 1690
duke@0 1691
michaelm@4632 1692 /** BSD **/
michaelm@4632 1693 #ifdef _ALLBSD_SOURCE
michaelm@4632 1694 /* Open socket for further ioct calls, try v4 socket first and
michaelm@4632 1695 * if it falls return v6 socket
michaelm@4632 1696 */
michaelm@4632 1697
michaelm@4632 1698 #ifdef AF_INET6
michaelm@4632 1699 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
michaelm@4632 1700 int sock;
michaelm@4632 1701 struct ifreq if2;
michaelm@4632 1702
michaelm@4632 1703 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
michaelm@4632 1704 if (errno == EPROTONOSUPPORT){
michaelm@4632 1705 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
michaelm@4632 1706 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
michaelm@4632 1707 return -1;
michaelm@4632 1708 }
michaelm@4632 1709 }
michaelm@4632 1710 else{ // errno is not NOSUPPORT
michaelm@4632 1711 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
michaelm@4632 1712 return -1;
michaelm@4632 1713 }
michaelm@4632 1714 }
michaelm@4632 1715
michaelm@4632 1716 return sock;
michaelm@4632 1717 }
michaelm@4632 1718
michaelm@4632 1719 #else
michaelm@4632 1720 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
michaelm@4632 1721 return openSocket(env,AF_INET);
michaelm@4632 1722 }
michaelm@4632 1723 #endif
michaelm@4632 1724
michaelm@4632 1725 /*
michaelm@4632 1726 * Enumerates and returns all IPv4 interfaces
michaelm@4632 1727 */
michaelm@4632 1728 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
michaelm@4632 1729 struct ifaddrs *ifa, *origifa;
michaelm@4632 1730
michaelm@4632 1731 if (getifaddrs(&origifa) != 0) {
michaelm@4632 1732 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1733 "getifaddrs() function failed");
michaelm@4632 1734 return ifs;
michaelm@4632 1735 }
michaelm@4632 1736
michaelm@4632 1737 for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
michaelm@4632 1738
michaelm@4632 1739 /*
michaelm@4632 1740 * Skip non-AF_INET entries.
michaelm@4632 1741 */
michaelm@4632 1742 if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
michaelm@4632 1743 continue;
michaelm@4632 1744
michaelm@4632 1745 /*
michaelm@4632 1746 * Add to the list.
michaelm@4632 1747 */
michaelm@4632 1748 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0);
michaelm@4632 1749
michaelm@4632 1750 /*
michaelm@4632 1751 * If an exception occurred then free the list.
michaelm@4632 1752 */
michaelm@4632 1753 if ((*env)->ExceptionOccurred(env)) {
michaelm@4632 1754 freeifaddrs(origifa);
michaelm@4632 1755 freeif(ifs);
michaelm@4632 1756 return NULL;
michaelm@4632 1757 }
michaelm@4632 1758 }
michaelm@4632 1759
michaelm@4632 1760 /*
michaelm@4632 1761 * Free socket and buffer
michaelm@4632 1762 */
michaelm@4632 1763 freeifaddrs(origifa);
michaelm@4632 1764 return ifs;
michaelm@4632 1765 }
michaelm@4632 1766
michaelm@4632 1767
michaelm@4632 1768 /*
michaelm@4632 1769 * Enumerates and returns all IPv6 interfaces on Linux
michaelm@4632 1770 */
michaelm@4632 1771
michaelm@4632 1772 #ifdef AF_INET6
michaelm@4632 1773 /*
michaelm@4632 1774 * Determines the prefix on BSD for IPv6 interfaces.
michaelm@4632 1775 */
michaelm@4632 1776 static
michaelm@4632 1777 int prefix(void *val, int size) {
michaelm@4632 1778 u_char *name = (u_char *)val;
michaelm@4632 1779 int byte, bit, plen = 0;
michaelm@4632 1780
michaelm@4632 1781 for (byte = 0; byte < size; byte++, plen += 8)
michaelm@4632 1782 if (name[byte] != 0xff)
michaelm@4632 1783 break;
michaelm@4632 1784 if (byte == size)
michaelm@4632 1785 return (plen);
michaelm@4632 1786 for (bit = 7; bit != 0; bit--, plen++)
michaelm@4632 1787 if (!(name[byte] & (1 << bit)))
michaelm@4632 1788 break;
michaelm@4632 1789 for (; bit != 0; bit--)
michaelm@4632 1790 if (name[byte] & (1 << bit))
michaelm@4632 1791 return (0);
michaelm@4632 1792 byte++;
michaelm@4632 1793 for (; byte < size; byte++)
michaelm@4632 1794 if (name[byte])
michaelm@4632 1795 return (0);
michaelm@4632 1796 return (plen);
michaelm@4632 1797 }
michaelm@4632 1798
michaelm@4632 1799 /*
michaelm@4632 1800 * Enumerates and returns all IPv6 interfaces on BSD
michaelm@4632 1801 */
michaelm@4632 1802 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
michaelm@4632 1803 struct ifaddrs *ifa, *origifa;
michaelm@4632 1804 struct sockaddr_in6 *sin6;
michaelm@4632 1805 struct in6_ifreq ifr6;
michaelm@4632 1806
michaelm@4632 1807 if (getifaddrs(&origifa) != 0) {
michaelm@4632 1808 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1809 "getifaddrs() function failed");
michaelm@4632 1810 return ifs;
michaelm@4632 1811 }
michaelm@4632 1812
michaelm@4632 1813 for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
michaelm@4632 1814
michaelm@4632 1815 /*
michaelm@4632 1816 * Skip non-AF_INET6 entries.
michaelm@4632 1817 */
michaelm@4632 1818 if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
michaelm@4632 1819 continue;
michaelm@4632 1820
michaelm@4632 1821 memset(&ifr6, 0, sizeof(ifr6));
michaelm@4632 1822 strlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
michaelm@4632 1823 memcpy(&ifr6.ifr_addr, ifa->ifa_addr, MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len));
michaelm@4632 1824
michaelm@4632 1825 if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
michaelm@4632 1826 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
michaelm@4632 1827 "ioctl SIOCGIFNETMASK_IN6 failed");
michaelm@4632 1828 freeifaddrs(origifa);
michaelm@4632 1829 freeif(ifs);
michaelm@4632 1830 return NULL;
michaelm@4632 1831 }
michaelm@4632 1832
michaelm@4632 1833 /* Add to the list. */
michaelm@4632 1834 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
michaelm@4632 1835 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6,
michaelm@4632 1836 prefix(&sin6->sin6_addr, sizeof(struct in6_addr)));
michaelm@4632 1837
michaelm@4632 1838 /* If an exception occurred then free the list. */
michaelm@4632 1839 if ((*env)->ExceptionOccurred(env)) {
michaelm@4632 1840 freeifaddrs(origifa);
michaelm@4632 1841 freeif(ifs);
michaelm@4632 1842 return NULL;
michaelm@4632 1843 }
michaelm@4632 1844 }
michaelm@4632 1845
michaelm@4632 1846 /*
michaelm@4632 1847 * Free socket and ifaddrs buffer
michaelm@4632 1848 */
michaelm@4632 1849 freeifaddrs(origifa);
michaelm@4632 1850 return ifs;
michaelm@4632 1851 }
michaelm@4632 1852 #endif
michaelm@4632 1853
michaelm@4632 1854 static int getIndex(int sock, const char *name){
michaelm@4632 1855 #ifdef __FreeBSD__
michaelm@4632 1856 /*
michaelm@4632 1857 * Try to get the interface index
michaelm@4632 1858 * (Not supported on Solaris 2.6 or 7)
michaelm@4632 1859 */
michaelm@4632 1860 struct ifreq if2;
michaelm@4632 1861 strcpy(if2.ifr_name, name);
michaelm@4632 1862
michaelm@4632 1863 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
michaelm@4632 1864 return -1;
michaelm@4632 1865 }
michaelm@4632 1866
michaelm@4632 1867 return if2.ifr_index;
michaelm@4632 1868 #else
michaelm@4632 1869 /*
michaelm@4632 1870 * Try to get the interface index using BSD specific if_nametoindex
michaelm@4632 1871 */
michaelm@4632 1872 int index = if_nametoindex(name);
michaelm@4632 1873 return (index == 0) ? -1 : index;
michaelm@4632 1874 #endif
michaelm@4632 1875 }
michaelm@4632 1876
michaelm@4632 1877 /**
michaelm@4632 1878 * Returns the IPv4 broadcast address of a named interface, if it exists.
michaelm@4632 1879 * Returns 0 if it doesn't have one.
michaelm@4632 1880 */
michaelm@4632 1881 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
michaelm@4632 1882 struct sockaddr *ret = NULL;
michaelm@4632 1883 struct ifreq if2;
michaelm@4632 1884
michaelm@4632 1885 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1886 strcpy(if2.ifr_name, ifname);
michaelm@4632 1887
michaelm@4632 1888 /* Let's make sure the interface does have a broadcast address */
michaelm@4632 1889 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
michaelm@4632 1890 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
michaelm@4632 1891 return ret;
michaelm@4632 1892 }
michaelm@4632 1893
michaelm@4632 1894 if (if2.ifr_flags & IFF_BROADCAST) {
michaelm@4632 1895 /* It does, let's retrieve it*/
michaelm@4632 1896 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
michaelm@4632 1897 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
michaelm@4632 1898 return ret;
michaelm@4632 1899 }
michaelm@4632 1900
michaelm@4632 1901 ret = brdcast_store;
michaelm@4632 1902 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
michaelm@4632 1903 }
michaelm@4632 1904
michaelm@4632 1905 return ret;
michaelm@4632 1906 }
michaelm@4632 1907
michaelm@4632 1908 /**
michaelm@4632 1909 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
michaelm@4632 1910 * interface, if it has one, otherwise return -1.
michaelm@4632 1911 */
michaelm@4632 1912 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
michaelm@4632 1913 unsigned int mask;
michaelm@4632 1914 short ret;
michaelm@4632 1915 struct ifreq if2;
michaelm@4632 1916
michaelm@4632 1917 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1918 strcpy(if2.ifr_name, ifname);
michaelm@4632 1919
michaelm@4632 1920 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
michaelm@4632 1921 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
michaelm@4632 1922 return -1;
michaelm@4632 1923 }
michaelm@4632 1924
michaelm@4632 1925 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
michaelm@4632 1926 ret = 0;
michaelm@4632 1927 while (mask) {
michaelm@4632 1928 mask <<= 1;
michaelm@4632 1929 ret++;
michaelm@4632 1930 }
michaelm@4632 1931
michaelm@4632 1932 return ret;
michaelm@4632 1933 }
michaelm@4632 1934
michaelm@4632 1935 /**
michaelm@4632 1936 * Get the Hardware address (usually MAC address) for the named interface.
michaelm@4632 1937 * return puts the data in buf, and returns the length, in byte, of the
michaelm@4632 1938 * MAC address. Returns -1 if there is no hardware address on that interface.
michaelm@4632 1939 */
michaelm@4632 1940 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
michaelm@4632 1941 struct ifaddrs *ifa0, *ifa;
michaelm@4632 1942 struct sockaddr *saddr;
michaelm@4632 1943 int i;
michaelm@4632 1944
michaelm@4632 1945 /* Grab the interface list */
michaelm@4632 1946 if (!getifaddrs(&ifa0)) {
michaelm@4632 1947 /* Cycle through the interfaces */
michaelm@4632 1948 for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
michaelm@4632 1949 saddr = ifa->ifa_addr;
michaelm@4632 1950 /* Link layer contains the MAC address */
michaelm@4632 1951 if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
michaelm@4632 1952 struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
michaelm@4632 1953 /* Check the address is the correct length */
michaelm@4632 1954 if (sadl->sdl_alen == ETHER_ADDR_LEN) {
michaelm@4632 1955 memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
michaelm@4632 1956 freeifaddrs(ifa0);
michaelm@4632 1957 return ETHER_ADDR_LEN;
michaelm@4632 1958 }
michaelm@4632 1959 }
michaelm@4632 1960 }
michaelm@4632 1961 freeifaddrs(ifa0);
michaelm@4632 1962 }
michaelm@4632 1963
michaelm@4632 1964 return -1;
michaelm@4632 1965 }
michaelm@4632 1966
michaelm@4632 1967 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
michaelm@4632 1968 struct ifreq if2;
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, SIOCGIFMTU, (char *)&if2) < 0) {
michaelm@4632 1974 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
michaelm@4632 1975 return -1;
michaelm@4632 1976 }
michaelm@4632 1977
michaelm@4632 1978 return if2.ifr_mtu;
michaelm@4632 1979 }
michaelm@4632 1980
shirishk@5523 1981 static int getFlags(int sock, const char *ifname, int *flags) {
michaelm@4632 1982 struct ifreq if2;
michaelm@4632 1983 int ret = -1;
michaelm@4632 1984
michaelm@4632 1985 memset((char *) &if2, 0, sizeof(if2));
michaelm@4632 1986 strcpy(if2.ifr_name, ifname);
michaelm@4632 1987
michaelm@4632 1988 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
michaelm@4632 1989 return -1;
michaelm@4632 1990 }
michaelm@4632 1991
shirishk@5523 1992 if (sizeof(if2.ifr_flags) == sizeof(short)) {
shirishk@5523 1993 *flags = (if2.ifr_flags & 0xffff);
shirishk@5523 1994 } else {
shirishk@5523 1995 *flags = if2.ifr_flags;
shirishk@5523 1996 }
shirishk@5523 1997 return 0;
michaelm@4632 1998 }
michaelm@4632 1999
michaelm@4632 2000 #endif