src/solaris/native/java/net/NetworkInterface.c

Print this page




 202 
 203 
 204 /*
 205  * Class:     java_net_NetworkInterface
 206  * Method:    getByName0
 207  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 208  */
 209 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 210     (JNIEnv *env, jclass cls, jstring name) {
 211 
 212     netif *ifs, *curr;
 213     jboolean isCopy;
 214     const char* name_utf;
 215     jobject obj = NULL;
 216 
 217     ifs = enumInterfaces(env);
 218     if (ifs == NULL) {
 219         return NULL;
 220     }
 221 
 222     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);


 223 
 224     /*
 225      * Search the list of interface based on name
 226      */
 227     curr = ifs;
 228     while (curr != NULL) {
 229         if (strcmp(name_utf, curr->name) == 0) {
 230             break;
 231         }
 232         curr = curr->next;
 233     }
 234 
 235     /* if found create a NetworkInterface */
 236     if (curr != NULL) {;
 237         obj = createNetworkInterface(env, curr);
 238     }
 239 
 240     /* release the UTF string and interface list */
 241     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 242     freeif(ifs);


 472     int ret = getFlags0(env, name);
 473     return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
 474 }
 475 
 476 /*
 477  * Class:     java_net_NetworkInterface
 478  * Method:    getMacAddr0
 479  * Signature: ([bLjava/lang/String;I)[b
 480  */
 481 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
 482     jint addr;
 483     jbyte caddr[4];
 484     struct in_addr iaddr;
 485     jbyteArray ret = NULL;
 486     unsigned char mac[16];
 487     int len;
 488     int sock;
 489     jboolean isCopy;
 490     const char* name_utf;
 491 
 492     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);


 493 
 494     if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
 495        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 496        return JNI_FALSE;
 497     }
 498 
 499 
 500     if (!IS_NULL(addrArray)) {
 501        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 502        addr = ((caddr[0]<<24) & 0xff000000);
 503        addr |= ((caddr[1] <<16) & 0xff0000);
 504        addr |= ((caddr[2] <<8) & 0xff00);
 505        addr |= (caddr[3] & 0xff);
 506        iaddr.s_addr = htonl(addr);
 507        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 508     } else {
 509        len = getMacAddress(env, sock, name_utf,NULL, mac);
 510     }
 511     if (len > 0) {
 512        ret = (*env)->NewByteArray(env, len);
 513        if (IS_NULL(ret)) {
 514           /* we may have memory to free at the end of this */
 515           goto fexit;
 516        }
 517        (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
 518     }
 519  fexit:
 520    /* release the UTF string and interface list */
 521    (*env)->ReleaseStringUTFChars(env, name, name_utf);
 522 
 523    close(sock);
 524    return ret;
 525 }
 526 
 527 /*
 528  * Class:       java_net_NetworkInterface
 529  * Method:      getMTU0
 530  * Signature:   ([bLjava/lang/String;I)I
 531  */
 532 
 533 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
 534     jboolean isCopy;
 535     int ret = -1;
 536     int sock;
 537     const char* name_utf;
 538 
 539     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);


 540 
 541     if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
 542        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 543        return JNI_FALSE;
 544     }
 545 
 546     ret = getMTU(env, sock, name_utf);
 547 
 548     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 549 
 550     close(sock);
 551     return ret;
 552 }
 553 
 554 /*** Private methods definitions ****/
 555 
 556 static int getFlags0(JNIEnv *env, jstring name) {
 557     jboolean isCopy;
 558     int ret, sock;
 559     const char* name_utf;
 560     int flags = 0;
 561 
 562     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);


 563 
 564     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 565         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 566          return -1;
 567     }
 568 
 569     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 570 
 571     ret = getFlags(sock, name_utf, &flags);
 572 
 573     close(sock);
 574     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 575 
 576     if (ret < 0) {
 577         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL  SIOCGLIFFLAGS failed");
 578         return -1;
 579     }
 580 
 581     return flags;
 582 }
 583 
 584 
 585 
 586 
 587 /*
 588  * Create a NetworkInterface object, populate the name and index, and
 589  * populate the InetAddress array based on the IP addresses for this




 202 
 203 
 204 /*
 205  * Class:     java_net_NetworkInterface
 206  * Method:    getByName0
 207  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 208  */
 209 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 210     (JNIEnv *env, jclass cls, jstring name) {
 211 
 212     netif *ifs, *curr;
 213     jboolean isCopy;
 214     const char* name_utf;
 215     jobject obj = NULL;
 216 
 217     ifs = enumInterfaces(env);
 218     if (ifs == NULL) {
 219         return NULL;
 220     }
 221 
 222     if ((name_utf = (*env)->GetStringUTFChars(env, name, &isCopy)) == NULL) {
 223         return NULL;
 224     }
 225 
 226     /*
 227      * Search the list of interface based on name
 228      */
 229     curr = ifs;
 230     while (curr != NULL) {
 231         if (strcmp(name_utf, curr->name) == 0) {
 232             break;
 233         }
 234         curr = curr->next;
 235     }
 236 
 237     /* if found create a NetworkInterface */
 238     if (curr != NULL) {;
 239         obj = createNetworkInterface(env, curr);
 240     }
 241 
 242     /* release the UTF string and interface list */
 243     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 244     freeif(ifs);


 474     int ret = getFlags0(env, name);
 475     return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
 476 }
 477 
 478 /*
 479  * Class:     java_net_NetworkInterface
 480  * Method:    getMacAddr0
 481  * Signature: ([bLjava/lang/String;I)[b
 482  */
 483 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
 484     jint addr;
 485     jbyte caddr[4];
 486     struct in_addr iaddr;
 487     jbyteArray ret = NULL;
 488     unsigned char mac[16];
 489     int len;
 490     int sock;
 491     jboolean isCopy;
 492     const char* name_utf;
 493 
 494     if ((name_utf = (*env)->GetStringUTFChars(env, name, &isCopy)) == NULL) {
 495         return JNI_FALSE;
 496     }
 497 
 498     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 499         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 500         return JNI_FALSE;
 501     }
 502 

 503     if (!IS_NULL(addrArray)) {
 504        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 505        addr = ((caddr[0]<<24) & 0xff000000);
 506        addr |= ((caddr[1] <<16) & 0xff0000);
 507        addr |= ((caddr[2] <<8) & 0xff00);
 508        addr |= (caddr[3] & 0xff);
 509        iaddr.s_addr = htonl(addr);
 510        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 511     } else {
 512        len = getMacAddress(env, sock, name_utf,NULL, mac);
 513     }
 514     if (len > 0) {
 515        ret = (*env)->NewByteArray(env, len);
 516        if (IS_NULL(ret)) {
 517           /* we may have memory to free at the end of this */
 518           goto fexit;
 519        }
 520        (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
 521     }
 522  fexit:
 523    /* release the UTF string and interface list */
 524    (*env)->ReleaseStringUTFChars(env, name, name_utf);
 525 
 526    close(sock);
 527    return ret;
 528 }
 529 
 530 /*
 531  * Class:       java_net_NetworkInterface
 532  * Method:      getMTU0
 533  * Signature:   ([bLjava/lang/String;I)I
 534  */
 535 
 536 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
 537     jboolean isCopy;
 538     int ret = -1;
 539     int sock;
 540     const char* name_utf;
 541 
 542     if ((name_utf = (*env)->GetStringUTFChars(env, name, &isCopy)) == NULL) {
 543         return JNI_FALSE;
 544     }
 545 
 546     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 547         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 548         return JNI_FALSE;
 549     }
 550 
 551     ret = getMTU(env, sock, name_utf);
 552 
 553     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 554 
 555     close(sock);
 556     return ret;
 557 }
 558 
 559 /*** Private methods definitions ****/
 560 
 561 static int getFlags0(JNIEnv *env, jstring name) {
 562     jboolean isCopy;
 563     int ret, sock;
 564     const char* name_utf;
 565     int flags = 0;
 566 
 567     if ((name_utf = (*env)->GetStringUTFChars(env, name, &isCopy)) == NULL) {
 568         return -1;
 569     }
 570 
 571     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 572         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 573         return -1;
 574     }


 575 
 576     ret = getFlags(sock, name_utf, &flags);
 577 
 578     close(sock);
 579     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 580 
 581     if (ret < 0) {
 582         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL  SIOCGLIFFLAGS failed");
 583         return -1;
 584     }
 585 
 586     return flags;
 587 }
 588 
 589 
 590 
 591 
 592 /*
 593  * Create a NetworkInterface object, populate the name and index, and
 594  * populate the InetAddress array based on the IP addresses for this