/*ARGSUSED*/ JNIEXPORT jobject JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getDataStore( JNIEnv *env, jobject obj, jstring jresource) { jclass ds_class; jmethodID ds_cons; jobject dsObject; jboolean avail; jint version; dsvc_datastore_t datastore; char *resource; /* Make sure we have the classes & methods we need */ ds_class = find_class(env, DS_CLASS); if (ds_class == NULL) { /* exception thrown */ return (NULL); } ds_cons = get_methodID(env, ds_class, DS_CONS); if (ds_cons == NULL) { /* exception thrown */ return (NULL); } /* Retrieve the resource argument */ if (!dd_jstring_to_UTF(env, jresource, &resource)) { /* exception thrown */ return (NULL); } datastore.d_conver = DSVC_CUR_CONVER; datastore.d_resource = resource; datastore.d_location = NULL; avail = JNI_FALSE; if (status_dd(&datastore) == DSVC_SUCCESS) { avail = JNI_TRUE; version = datastore.d_conver; } dsObject = (*env)->NewObject(env, ds_class, ds_cons, jresource, version, avail); free(resource); return (dsObject); }
/*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 jobjectArray JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getInterfaces( JNIEnv *env, jobject obj) { jclass ipif_class; jmethodID ipif_cons; jobjectArray jlist = NULL; jobject jobj; jsize len; struct ip_interface **list; int i; /* Locate the class and constructor we need */ ipif_class = find_class(env, IPIF_CLASS); if (ipif_class == NULL) { /* exception thrown */ return (NULL); } ipif_cons = get_methodID(env, ipif_class, IPIF_CONS); if (ipif_cons == NULL) { return (NULL); } /* Retrieve interface list */ list = dd_get_interfaces(); if (list == NULL) { throw_bridge_exception(env, gettext("Error in dd_get_interfaces")); return (NULL); } /* Compute length of list */ ARRAY_LENGTH(list, len); /* Construct the array */ jlist = (*env)->NewObjectArray(env, len, ipif_class, NULL); if (jlist == NULL) { /* exception thrown */ for (i = 0; i < len; i++) { free(list[i]); } free(list); return (NULL); } /* For each interface, construct an object and add to the array */ for (i = 0; i < len; ++i) { jobj = (*env)->NewObject(env, ipif_class, ipif_cons, (*env)->NewStringUTF(env, list[i]->name), (*env)->NewStringUTF(env, inet_ntoa(list[i]->addr)), (*env)->NewStringUTF(env, inet_ntoa(list[i]->mask))); if (jobj == NULL) { /* exception thrown */ break; } (*env)->SetObjectArrayElement(env, jlist, i, jobj); if ((*env)->ExceptionOccurred(env) != NULL) { break; } } for (i = 0; i < len; i++) { free(list[i]); } free(list); return (jlist); }
/*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); }
/*ARGSUSED*/ JNIEXPORT jobject JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_readDefaults( JNIEnv *env, jobject obj) { jclass cfg_class; jmethodID cfg_cons; jmethodID cfg_set; jobject cfgobj = NULL; dhcp_confopt_t *cfgs, *tcfgs; /* Make sure we have the classes & methods we need */ cfg_class = find_class(env, CFG_CLASS); if (cfg_class == NULL) { /* exception thrown */ return (NULL); } cfg_cons = get_methodID(env, cfg_class, CFG_CONS); if (cfg_cons == NULL) { /* exception thrown */ return (NULL); } cfg_set = get_methodID(env, cfg_class, CFG_SET); if (cfg_set == NULL) { /* exception thrown */ return (NULL); } /* Get the data */ if (read_dsvc_conf(&cfgs) != 0) { throw_bridge_exception(env, strerror(errno)); } else { /* Construct returned options object */ cfgobj = (*env)->NewObject(env, cfg_class, cfg_cons); if (cfgobj == NULL) { /* exception thrown */ free_dsvc_conf(cfgs); return (NULL); } /* Load the option settings into the options object */ tcfgs = cfgs; for (;;) { if (cfgs->co_type == DHCP_COMMENT) { (*env)->CallVoidMethod(env, cfgobj, cfg_set, (*env)->NewStringUTF(env, cfgs->co_key), (*env)->NewStringUTF(env, ""), JNI_TRUE); } else { if (cfgs->co_key == NULL) { break; } (*env)->CallVoidMethod(env, cfgobj, cfg_set, (*env)->NewStringUTF(env, cfgs->co_key), (*env)->NewStringUTF(env, cfgs->co_value), JNI_FALSE); } if ((*env)->ExceptionOccurred(env) != NULL) { free_dsvc_conf(tcfgs); return (NULL); } ++cfgs; } free_dsvc_conf(tcfgs); } return (cfgobj); }
/*ARGSUSED*/ JNIEXPORT jobjectArray JNICALL Java_com_sun_dhcpmgr_bridge_Bridge_getDataStores( JNIEnv *env, jobject obj) { jclass ds_class; jmethodID ds_cons; jobjectArray jlist = NULL; jobject jobj; jstring jstr; jboolean avail; jint version; char **list; dsvc_datastore_t datastore; int i, len; /* Make sure we have the classes & methods we need */ ds_class = find_class(env, DS_CLASS); if (ds_class == NULL) { /* exception thrown */ return (NULL); } ds_cons = get_methodID(env, ds_class, DS_CONS); if (ds_cons == NULL) { /* exception thrown */ return (NULL); } /* Get the list */ list = dd_data_stores(env); if ((*env)->ExceptionOccurred(env) != NULL) { return (NULL); } /* Compute the length of the array, store in len */ ARRAY_LENGTH(list, len); /* Construct the array */ jlist = (*env)->NewObjectArray(env, len, ds_class, NULL); if (jlist == NULL) { /* exception thrown */ dd_free_data_stores(list); return (NULL); } /* For each store, create an object and add it to the array */ for (i = 0; i < len; ++i) { jstr = (*env)->NewStringUTF(env, list[i]); if (jstr == NULL) { /* exception thrown */ break; } datastore.d_conver = DSVC_CUR_CONVER; datastore.d_resource = list[i]; datastore.d_location = NULL; avail = JNI_FALSE; if (status_dd(&datastore) == DSVC_SUCCESS) { avail = JNI_TRUE; version = datastore.d_conver; } jobj = (*env)->NewObject(env, ds_class, ds_cons, jstr, version, avail); if (jobj == NULL) { /* exception thrown */ break; } (*env)->SetObjectArrayElement(env, jlist, i, jobj); if ((*env)->ExceptionOccurred(env) != NULL) { break; } } dd_free_data_stores(list); return (jlist); }