Esempio n. 1
0
/*ARGSUSED*/
JNIEXPORT void JNICALL
Java_com_sun_dhcpmgr_bridge_Bridge_startup(
    JNIEnv *env,
    jobject obj)
{
	char *s;
	int ret;

	/*
	 * We first get the current state of the server according to
	 * svc.startd; if it's "disabled", we can just enable it.
	 * In any other case, we want to send a refresh so that
	 * dependencies are re-evaluated, which will be the case if the
	 * service was marked enabled by the profile, yet the
	 * config file didn't exist to allow it to run.
	 */
	if ((s = smf_get_state(DHCP_SERVER_INST)) != NULL) {
		if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
			ret = smf_enable_instance(DHCP_SERVER_INST, 0);
		else
			ret = smf_refresh_instance(DHCP_SERVER_INST);
		free(s);
		if (ret == 0)
			return;
	}

	/* Something wasn't right, return exception with error from smf */
	throw_bridge_exception(env, scf_strerror(scf_error()));
}
Esempio n. 2
0
/*ARGSUSED*/
JNIEXPORT void JNICALL
Java_com_sun_dhcpmgr_bridge_Bridge_shutdown(
    JNIEnv *env,
    jobject obj)
{
	if (smf_disable_instance(DHCP_SERVER_INST, 0) != 0) {
		throw_bridge_exception(env, scf_strerror(scf_error()));
	}
}
Esempio n. 3
0
/*ARGSUSED*/
JNIEXPORT void JNICALL
Java_com_sun_dhcpmgr_bridge_Bridge_removeDefaults(
    JNIEnv *env,
    jobject obj)
{
	if (delete_dsvc_conf() != 0) {
		throw_bridge_exception(env, strerror(errno));
	}
}
Esempio n. 4
0
/*ARGSUSED*/
JNIEXPORT void JNICALL
Java_com_sun_dhcpmgr_bridge_Bridge_reload(
    JNIEnv *env,
    jobject obj)
{
	int err;

	if ((err = dd_signal(DHCPD_FNAME, SIGHUP)) != 0) {
		if (err == -1) {
			/* dd_signal couldn't find in.dhcpd running */
			throw_not_running_exception(env);
		} else {
			throw_bridge_exception(env, strerror(err));
		}
	}
}
Esempio n. 5
0
/*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);
}
Esempio n. 6
0
/*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);
}
Esempio n. 7
0
/*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);
}
Esempio n. 8
0
/*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);
}
Esempio n. 9
0
/*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);
}
Esempio n. 10
0
/*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);
}