xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sadm/dhcpmgr/lib/inittab.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <jni.h>
31*0Sstevel@tonic-gate #include <dhcp_inittab.h>
32*0Sstevel@tonic-gate #include <dhcp_symbol.h>
33*0Sstevel@tonic-gate #include <exception.h>
34*0Sstevel@tonic-gate #include <com_sun_dhcpmgr_bridge_Bridge.h>
35*0Sstevel@tonic-gate #include <dhcp_svc_private.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "class_cache.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Retrieve a list of DHCP options from the dhcp inittab.
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate /*ARGSUSED*/
43*0Sstevel@tonic-gate JNIEXPORT jobjectArray JNICALL
Java_com_sun_dhcpmgr_bridge_Bridge_getInittabOptions(JNIEnv * env,jobject obj,jbyte jcategory)44*0Sstevel@tonic-gate Java_com_sun_dhcpmgr_bridge_Bridge_getInittabOptions(
45*0Sstevel@tonic-gate     JNIEnv *env,
46*0Sstevel@tonic-gate     jobject obj,
47*0Sstevel@tonic-gate     jbyte jcategory) {
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	jclass opt_class;
50*0Sstevel@tonic-gate 	jmethodID opt_cons;
51*0Sstevel@tonic-gate 	jobjectArray jlist = NULL;
52*0Sstevel@tonic-gate 	jobject jobj;
53*0Sstevel@tonic-gate 	jstring jname;
54*0Sstevel@tonic-gate 	jshort jcode;
55*0Sstevel@tonic-gate 	jbyte jtype;
56*0Sstevel@tonic-gate 	jint jgran;
57*0Sstevel@tonic-gate 	jint jmax;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	uchar_t category;
60*0Sstevel@tonic-gate 	dhcp_symbol_t *entryptr;
61*0Sstevel@tonic-gate 	dhcp_symbol_t *list;
62*0Sstevel@tonic-gate 	dhcp_symbol_t internal;
63*0Sstevel@tonic-gate 	size_t num;
64*0Sstevel@tonic-gate 	int i;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	/* Make sure we have the classes & methods we need */
67*0Sstevel@tonic-gate 	opt_class = find_class(env, OPT_CLASS);
68*0Sstevel@tonic-gate 	if (opt_class == NULL) {
69*0Sstevel@tonic-gate 		/* exception thrown */
70*0Sstevel@tonic-gate 		return (NULL);
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 	opt_cons = get_methodID(env, opt_class, OPT_CONS);
73*0Sstevel@tonic-gate 	if (opt_cons == NULL) {
74*0Sstevel@tonic-gate 		/* exception thrown */
75*0Sstevel@tonic-gate 		return (NULL);
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	/* Translate the dhcpmgr category to the inittab category */
79*0Sstevel@tonic-gate 	if (jcategory == DSYM_STANDARD) {
80*0Sstevel@tonic-gate 		category = ITAB_CAT_STANDARD | ITAB_CAT_INTERNAL |
81*0Sstevel@tonic-gate 		    ITAB_CAT_FIELD;
82*0Sstevel@tonic-gate 	} else {
83*0Sstevel@tonic-gate 		category = jcategory;
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/* Get the list of options */
87*0Sstevel@tonic-gate 	list = inittab_load(category, ITAB_CONS_MANAGER, &num);
88*0Sstevel@tonic-gate 	if (list == NULL) {
89*0Sstevel@tonic-gate 		return (NULL);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* Construct the array */
93*0Sstevel@tonic-gate 	jlist = (*env)->NewObjectArray(env, num, opt_class, NULL);
94*0Sstevel@tonic-gate 	if (jlist == NULL) {
95*0Sstevel@tonic-gate 		/* exception thrown */
96*0Sstevel@tonic-gate 		free(list);
97*0Sstevel@tonic-gate 		return (NULL);
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/* For each option, create an object and add it to the array */
101*0Sstevel@tonic-gate 	for (i = 0; i < num; ++i) {
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		/* Verify the entry. Use the internal if necessary. */
104*0Sstevel@tonic-gate 		if (inittab_verify(&list[i], &internal) == ITAB_FAILURE) {
105*0Sstevel@tonic-gate 			entryptr = &internal;
106*0Sstevel@tonic-gate 		} else {
107*0Sstevel@tonic-gate 			entryptr = &list[i];
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 		jtype = entryptr->ds_type;
111*0Sstevel@tonic-gate 		jname = (*env)->NewStringUTF(env, entryptr->ds_name);
112*0Sstevel@tonic-gate 		if (jname == NULL) {
113*0Sstevel@tonic-gate 			/* exception thrown */
114*0Sstevel@tonic-gate 			break;
115*0Sstevel@tonic-gate 		}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		/* HACK. Since the codes for fields can overlap the	*/
118*0Sstevel@tonic-gate 		/* codes for STANDARD options, we will just set the	*/
119*0Sstevel@tonic-gate 		/* code to zero and ignore the need for these codes to	*/
120*0Sstevel@tonic-gate 		/* be unique. We do the same for internal but not	*/
121*0Sstevel@tonic-gate 		/* for the same reason. For internal we have no need	*/
122*0Sstevel@tonic-gate 		/* for the actual code and since we expect them to	*/
123*0Sstevel@tonic-gate 		/* change in the future, we'll just go ahead and	*/
124*0Sstevel@tonic-gate 		/* set them to zero too.				*/
125*0Sstevel@tonic-gate 		if (entryptr->ds_category == DSYM_INTERNAL ||
126*0Sstevel@tonic-gate 		    entryptr->ds_category == DSYM_FIELD) {
127*0Sstevel@tonic-gate 			jcode = (jshort)0;
128*0Sstevel@tonic-gate 		} else {
129*0Sstevel@tonic-gate 			jcode = entryptr->ds_code;
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		jmax = entryptr->ds_max;
133*0Sstevel@tonic-gate 		jgran = entryptr->ds_gran;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		/* Create an 'Option' */
136*0Sstevel@tonic-gate 		jobj = (*env)->NewObject(env, opt_class, opt_cons, jname,
137*0Sstevel@tonic-gate 		    jcategory, NULL, jcode, jtype, jgran, jmax, NULL,
138*0Sstevel@tonic-gate 		    JNI_TRUE);
139*0Sstevel@tonic-gate 		if (jobj == NULL) {
140*0Sstevel@tonic-gate 			/* exception thrown */
141*0Sstevel@tonic-gate 			break;
142*0Sstevel@tonic-gate 		}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		(*env)->SetObjectArrayElement(env, jlist, i, jobj);
145*0Sstevel@tonic-gate 		if ((*env)->ExceptionOccurred(env) != NULL) {
146*0Sstevel@tonic-gate 			break;
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	free(list);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	return (jlist);
153*0Sstevel@tonic-gate }
154