< prev index next >

src/java.base/unix/native/libnet/NetworkInterface.c

Print this page




 202     CHECK_NULL(ni_ib4broadcastID);
 203     ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
 204     CHECK_NULL(ni_ib4maskID);
 205     ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
 206                                                  "I");
 207     CHECK_NULL(ni_defaultIndexID);
 208     initInetAddressIDs(env);
 209 }
 210 
 211 /*
 212  * Class:     java_net_NetworkInterface
 213  * Method:    getByName0
 214  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 215  */
 216 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 217   (JNIEnv *env, jclass cls, jstring name)
 218 {
 219     netif *ifs, *curr;
 220     jboolean isCopy;
 221     const char* name_utf;


 222     jobject obj = NULL;
 223 
 224     if (name != NULL) {
 225         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 226     } else {
 227         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 228         return NULL;
 229     }
 230 
 231     if (name_utf == NULL) {
 232         if (!(*env)->ExceptionCheck(env))
 233             JNU_ThrowOutOfMemoryError(env, NULL);
 234         return NULL;
 235     }
 236 
 237     ifs = enumInterfaces(env);
 238     if (ifs == NULL) {
 239         return NULL;
 240     }
 241 
 242     // search the list of interfaces based on name







 243     curr = ifs;
 244     while (curr != NULL) {










 245         if (strcmp(name_utf, curr->name) == 0) {
 246             break;
 247         }
 248         curr = curr->next;
 249     }

 250 
 251     // if found create a NetworkInterface
 252     if (curr != NULL) {
 253         obj = createNetworkInterface(env, curr);
 254     }
 255 
 256     // release the UTF string and interface list
 257     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 258     freeif(ifs);
 259 
 260     return obj;
 261 }
 262 
 263 /*
 264  * Class:     java_net_NetworkInterface
 265  * Method:    getByIndex0
 266  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 267  */
 268 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
 269   (JNIEnv *env, jclass cls, jint index)




 202     CHECK_NULL(ni_ib4broadcastID);
 203     ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
 204     CHECK_NULL(ni_ib4maskID);
 205     ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
 206                                                  "I");
 207     CHECK_NULL(ni_defaultIndexID);
 208     initInetAddressIDs(env);
 209 }
 210 
 211 /*
 212  * Class:     java_net_NetworkInterface
 213  * Method:    getByName0
 214  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 215  */
 216 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 217   (JNIEnv *env, jclass cls, jstring name)
 218 {
 219     netif *ifs, *curr;
 220     jboolean isCopy;
 221     const char* name_utf;
 222     char *colonP;
 223     char searchName[IFNAMESIZE];
 224     jobject obj = NULL;
 225 
 226     if (name != NULL) {
 227         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 228     } else {
 229         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 230         return NULL;
 231     }
 232 
 233     if (name_utf == NULL) {
 234         if (!(*env)->ExceptionCheck(env))
 235             JNU_ThrowOutOfMemoryError(env, NULL);
 236         return NULL;
 237     }
 238 
 239     ifs = enumInterfaces(env);
 240     if (ifs == NULL) {
 241         return NULL;
 242     }
 243 
 244     // search the list of interfaces based on name,
 245     // if it is virtual sub interface search with parent first.
 246     strncpy(searchName, name_utf, IFNAMESIZE);
 247     searchName[IFNAMESIZE - 1] = '\0';
 248     colonP = strchr(searchName, ':');
 249     if (colonP != NULL) {
 250         *colonP = 0;
 251     }
 252     curr = ifs;
 253     while (curr != NULL) {
 254         if (strcmp(searchName, curr->name) == 0) {
 255             break;
 256         }
 257         curr = curr->next;
 258     }
 259     
 260     // search the child list
 261     if (colonP != NULL && curr != NULL) {
 262         curr = curr->childs;
 263         while (curr != NULL) {
 264             if (strcmp(name_utf, curr->name) == 0) {
 265                 break;
 266             }
 267             curr = curr->next;
 268         }
 269     }
 270     
 271     // if found create a NetworkInterface
 272     if (curr != NULL) {
 273         obj = createNetworkInterface(env, curr);
 274     }
 275 
 276     // release the UTF string and interface list
 277     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 278     freeif(ifs);
 279 
 280     return obj;
 281 }
 282 
 283 /*
 284  * Class:     java_net_NetworkInterface
 285  * Method:    getByIndex0
 286  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 287  */
 288 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
 289   (JNIEnv *env, jclass cls, jint index)


< prev index next >