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

Print this page




 236  * Method:    getByName0
 237  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 238  */
 239 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 240     (JNIEnv *env, jclass cls, jstring name) {
 241 
 242     netif *ifs, *curr;
 243     jboolean isCopy;
 244     const char* name_utf;
 245     jobject obj = NULL;
 246 
 247     ifs = enumInterfaces(env);
 248     if (ifs == NULL) {
 249         return NULL;
 250     }
 251 
 252     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 253     if (name_utf == NULL) {
 254        if (!(*env)->ExceptionCheck(env))
 255            JNU_ThrowOutOfMemoryError(env, NULL);

 256        return NULL;
 257     }
 258     /*
 259      * Search the list of interface based on name
 260      */
 261     curr = ifs;
 262     while (curr != NULL) {
 263         if (strcmp(name_utf, curr->name) == 0) {
 264             break;
 265         }
 266         curr = curr->next;
 267     }
 268 
 269     /* if found create a NetworkInterface */
 270     if (curr != NULL) {;
 271         obj = createNetworkInterface(env, curr);
 272     }
 273 
 274     /* release the UTF string and interface list */
 275     (*env)->ReleaseStringUTFChars(env, name, name_utf);


 510  * Method:    getMacAddr0
 511  * Signature: ([bLjava/lang/String;I)[b
 512  */
 513 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
 514     jint addr;
 515     jbyte caddr[4];
 516     struct in_addr iaddr;
 517     jbyteArray ret = NULL;
 518     unsigned char mac[16];
 519     int len;
 520     int sock;
 521     jboolean isCopy;
 522     const char* name_utf;
 523 
 524     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 525     if (name_utf == NULL) {
 526        if (!(*env)->ExceptionCheck(env))
 527            JNU_ThrowOutOfMemoryError(env, NULL);
 528        return NULL;
 529     }
 530     if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
 531        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 532        return JNI_FALSE;
 533     }
 534 
 535 
 536     if (!IS_NULL(addrArray)) {
 537        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 538        addr = ((caddr[0]<<24) & 0xff000000);
 539        addr |= ((caddr[1] <<16) & 0xff0000);
 540        addr |= ((caddr[2] <<8) & 0xff00);
 541        addr |= (caddr[3] & 0xff);
 542        iaddr.s_addr = htonl(addr);
 543        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 544     } else {
 545        len = getMacAddress(env, sock, name_utf,NULL, mac);
 546     }
 547     if (len > 0) {
 548        ret = (*env)->NewByteArray(env, len);
 549        if (IS_NULL(ret)) {
 550           /* we may have memory to free at the end of this */
 551           goto fexit;
 552        }




 236  * Method:    getByName0
 237  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 238  */
 239 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 240     (JNIEnv *env, jclass cls, jstring name) {
 241 
 242     netif *ifs, *curr;
 243     jboolean isCopy;
 244     const char* name_utf;
 245     jobject obj = NULL;
 246 
 247     ifs = enumInterfaces(env);
 248     if (ifs == NULL) {
 249         return NULL;
 250     }
 251 
 252     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 253     if (name_utf == NULL) {
 254        if (!(*env)->ExceptionCheck(env))
 255            JNU_ThrowOutOfMemoryError(env, NULL);
 256        freeif(ifs);
 257        return NULL;
 258     }
 259     /*
 260      * Search the list of interface based on name
 261      */
 262     curr = ifs;
 263     while (curr != NULL) {
 264         if (strcmp(name_utf, curr->name) == 0) {
 265             break;
 266         }
 267         curr = curr->next;
 268     }
 269 
 270     /* if found create a NetworkInterface */
 271     if (curr != NULL) {;
 272         obj = createNetworkInterface(env, curr);
 273     }
 274 
 275     /* release the UTF string and interface list */
 276     (*env)->ReleaseStringUTFChars(env, name, name_utf);


 511  * Method:    getMacAddr0
 512  * Signature: ([bLjava/lang/String;I)[b
 513  */
 514 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
 515     jint addr;
 516     jbyte caddr[4];
 517     struct in_addr iaddr;
 518     jbyteArray ret = NULL;
 519     unsigned char mac[16];
 520     int len;
 521     int sock;
 522     jboolean isCopy;
 523     const char* name_utf;
 524 
 525     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 526     if (name_utf == NULL) {
 527        if (!(*env)->ExceptionCheck(env))
 528            JNU_ThrowOutOfMemoryError(env, NULL);
 529        return NULL;
 530     }
 531     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 532        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 533        return NULL;
 534     }
 535 
 536 
 537     if (!IS_NULL(addrArray)) {
 538        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 539        addr = ((caddr[0]<<24) & 0xff000000);
 540        addr |= ((caddr[1] <<16) & 0xff0000);
 541        addr |= ((caddr[2] <<8) & 0xff00);
 542        addr |= (caddr[3] & 0xff);
 543        iaddr.s_addr = htonl(addr);
 544        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 545     } else {
 546        len = getMacAddress(env, sock, name_utf,NULL, mac);
 547     }
 548     if (len > 0) {
 549        ret = (*env)->NewByteArray(env, len);
 550        if (IS_NULL(ret)) {
 551           /* we may have memory to free at the end of this */
 552           goto fexit;
 553        }