/*ARGSUSED*/ JNIEXPORT jstring JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getStringOption( JNIEnv *env, jobject obj, jshort code, jstring jarg) { jstring jstr; struct dhcp_option *opt; ushort_t scode = (ushort_t)code; const char *arg; /* Get the option whose default value we want to generate. */ arg = (*env)->GetStringUTFChars(env, jarg, NULL); if (arg == NULL) { /* exception thrown */ return (NULL); } /* Get the option data */ opt = dd_getopt(scode, arg, NULL); (*env)->ReleaseStringUTFChars(env, jarg, arg); if (opt == NULL) { throw_memory_exception(env); return (NULL); } if (opt->error_code != 0) { throw_bridge_exception(env, opt->u.msg); dd_freeopt(opt); return (NULL); } /* Set the return value */ jstr = (*env)->NewStringUTF(env, opt->u.ret.data.strings[0]); dd_freeopt(opt); return (jstr); }
/*ARGSUSED*/ JNIEXPORT jobjectArray JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getIPOption( JNIEnv *env, jobject obj, jshort code, jstring jarg) { jclass ip_class; jmethodID ip_cons; jobjectArray jlist = NULL; jobject jaddr; jstring jstr; struct dhcp_option *opt; ushort_t scode = (ushort_t)code; int i; const char *arg; /* Get classes and methods we need */ ip_class = find_class(env, IP_CLASS); if (ip_class == NULL) { /* exception thrown */ return (NULL); } ip_cons = get_methodID(env, ip_class, IP_CONS); if (ip_cons == NULL) { /* exception thrown */ return (NULL); } /* Retrieve option to generate value for */ arg = (*env)->GetStringUTFChars(env, jarg, NULL); if (arg == NULL) { /* exception thrown */ return (NULL); } /* Go get the default value */ opt = dd_getopt(scode, arg, NULL); (*env)->ReleaseStringUTFChars(env, jarg, arg); if (opt == NULL) { throw_memory_exception(env); return (NULL); } if (opt->error_code != 0) { throw_bridge_exception(env, opt->u.msg); dd_freeopt(opt); return (NULL); } /* Construct the array */ jlist = (*env)->NewObjectArray(env, opt->u.ret.count, ip_class, NULL); if (jlist == NULL) { /* exception thrown */ dd_freeopt(opt); return (NULL); } /* For each address, create an object and add it to the array */ for (i = 0; i < opt->u.ret.count; ++i) { jstr = (*env)->NewStringUTF(env, inet_ntoa(*opt->u.ret.data.addrs[i])); if (jstr == NULL) { /* exception thrown */ break; } jaddr = (*env)->NewObject(env, ip_class, ip_cons, jstr); if (jaddr == NULL) { /* exception thrown */ break; } (*env)->SetObjectArrayElement(env, jlist, i, jaddr); if ((*env)->ExceptionOccurred(env) != NULL) { break; } } dd_freeopt(opt); return (jlist); }
/*ARGSUSED*/ JNIEXPORT jlongArray JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getNumberOption( JNIEnv *env, jobject obj, jshort code, jstring jarg) { jlongArray list; struct dhcp_option *opt; const char *arg; ushort_t scode = (ushort_t)code; jlong *listel; int i; /* Get option to retrieve */ arg = (*env)->GetStringUTFChars(env, jarg, NULL); if (arg == NULL) { /* exception thrown */ return (NULL); } opt = dd_getopt(scode, arg, NULL); (*env)->ReleaseStringUTFChars(env, jarg, arg); if (opt == NULL) { throw_memory_exception(env); return (NULL); } if (opt->error_code != 0) { throw_bridge_exception(env, opt->u.msg); dd_freeopt(opt); return (NULL); } /* Allocate return array */ list = (*env)->NewLongArray(env, opt->u.ret.count); if (list == NULL) { /* exception thrown */ dd_freeopt(opt); return (NULL); } /* Get access to elements of return array, then copy data in */ listel = (*env)->GetLongArrayElements(env, list, NULL); if (listel == NULL) { /* exception thrown */ dd_freeopt(opt); return (NULL); } for (i = 0; i < opt->u.ret.count; ++i) { listel[i] = opt->u.ret.data.numbers[i]; } /* Tell VM we're done so it can finish putting data back */ (*env)->ReleaseLongArrayElements(env, list, listel, 0); dd_freeopt(opt); return (list); }
/*ARGSUSED*/ JNIEXPORT void JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_writeDefaults( JNIEnv *env, jobject obj, jobject jcfgs) { jclass cfg_class; jmethodID cfg_getall; jclass res_class; jmethodID res_getkey; jmethodID res_getval; jmethodID res_iscom; jobjectArray resArray; jsize reslen; jobject jobj, resobj; dhcp_confopt_t *cfgs; int i; jboolean comment; const char *tmpstr; /* Make sure we can get at the classes we need */ cfg_class = find_class(env, CFG_CLASS); if (cfg_class == NULL) { /* exception thrown */ return; } cfg_getall = get_methodID(env, cfg_class, CFG_GETALL); if (cfg_getall == NULL) { /* exception thrown */ return; } res_class = find_class(env, RES_CLASS); if (res_class == NULL) { /* exception thrown */ return; } res_getkey = get_methodID(env, res_class, RES_GETKEY); res_getval = get_methodID(env, res_class, RES_GETVAL); res_iscom = get_methodID(env, res_class, RES_ISCOM); if (res_getkey == NULL || res_getval == NULL || res_iscom == NULL) { /* exception thrown */ return; } /* Get the resource array from the config object */ resArray = (*env)->CallObjectMethod(env, jcfgs, cfg_getall); if ((*env)->ExceptionOccurred(env) != NULL) { return; } reslen = (*env)->GetArrayLength(env, resArray); /* Allocate array to convert into; extra zero'd item to signal end */ cfgs = calloc(reslen+1, sizeof (dhcp_confopt_t)); if (cfgs == NULL) { throw_memory_exception(env); return; } /* Now copy data into local array */ for (i = 0; i < reslen; ++i) { jobj = (*env)->GetObjectArrayElement(env, resArray, i); if (jobj == NULL) { /* exception thrown */ free_dsvc_conf(cfgs); return; } /* Set record type */ comment = (*env)->CallBooleanMethod(env, jobj, res_iscom); if ((*env)->ExceptionOccurred(env) != NULL) { return; } if (comment == JNI_TRUE) { cfgs[i].co_type = DHCP_COMMENT; } else { cfgs[i].co_type = DHCP_KEY; } /* * Get the key from the object, convert to a char *, * and then duplicate into the cfgs array so that * free_dsvc_conf can be used correctly. * Do the same thing for the value. */ resobj = (*env)->CallObjectMethod(env, jobj, res_getkey); tmpstr = (*env)->GetStringUTFChars(env, resobj, NULL); if (tmpstr == NULL) { /* exception thrown */ free_dsvc_conf(cfgs); throw_bridge_exception(env, gettext("Error converting key")); return; } cfgs[i].co_key = strdup(tmpstr); (*env)->ReleaseStringUTFChars(env, resobj, tmpstr); if (cfgs[i].co_key == NULL) { /* Out of memory, fail */ free_dsvc_conf(cfgs); throw_memory_exception(env); return; } resobj = (*env)->CallObjectMethod(env, jobj, res_getval); tmpstr = (*env)->GetStringUTFChars(env, resobj, NULL); if (tmpstr == NULL) { free_dsvc_conf(cfgs); throw_bridge_exception(env, gettext("Error converting value")); return; } cfgs[i].co_value = strdup(tmpstr); (*env)->ReleaseStringUTFChars(env, resobj, tmpstr); if (cfgs[i].co_value == NULL) { /* Out of memory, fail */ free_dsvc_conf(cfgs); throw_memory_exception(env); return; } } /* Now write the new data */ if (write_dsvc_conf(cfgs, CONFOPT_MODE) != 0) { throw_bridge_exception(env, strerror(errno)); } free_dsvc_conf(cfgs); }