xref: /minix3/external/bsd/dhcp/dist/omapip/handle.c (revision 83ee113ee0d94f3844d44065af2311604e9a30ad)
1*83ee113eSDavid van Moolenbroek /*	$NetBSD: handle.c,v 1.1.1.3 2014/07/12 11:57:59 spz Exp $	*/
2*83ee113eSDavid van Moolenbroek /* handle.c
3*83ee113eSDavid van Moolenbroek 
4*83ee113eSDavid van Moolenbroek    Functions for maintaining handles on objects. */
5*83ee113eSDavid van Moolenbroek 
6*83ee113eSDavid van Moolenbroek /*
7*83ee113eSDavid van Moolenbroek  * Copyright (c) 2009-2010,2012,2014 by Internet Systems Consortium, Inc. ("ISC")
8*83ee113eSDavid van Moolenbroek  * Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC")
9*83ee113eSDavid van Moolenbroek  * Copyright (c) 1999-2003 by Internet Software Consortium
10*83ee113eSDavid van Moolenbroek  *
11*83ee113eSDavid van Moolenbroek  * Permission to use, copy, modify, and distribute this software for any
12*83ee113eSDavid van Moolenbroek  * purpose with or without fee is hereby granted, provided that the above
13*83ee113eSDavid van Moolenbroek  * copyright notice and this permission notice appear in all copies.
14*83ee113eSDavid van Moolenbroek  *
15*83ee113eSDavid van Moolenbroek  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16*83ee113eSDavid van Moolenbroek  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17*83ee113eSDavid van Moolenbroek  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18*83ee113eSDavid van Moolenbroek  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*83ee113eSDavid van Moolenbroek  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20*83ee113eSDavid van Moolenbroek  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21*83ee113eSDavid van Moolenbroek  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22*83ee113eSDavid van Moolenbroek  *
23*83ee113eSDavid van Moolenbroek  *   Internet Systems Consortium, Inc.
24*83ee113eSDavid van Moolenbroek  *   950 Charter Street
25*83ee113eSDavid van Moolenbroek  *   Redwood City, CA 94063
26*83ee113eSDavid van Moolenbroek  *   <info@isc.org>
27*83ee113eSDavid van Moolenbroek  *   https://www.isc.org/
28*83ee113eSDavid van Moolenbroek  *
29*83ee113eSDavid van Moolenbroek  */
30*83ee113eSDavid van Moolenbroek 
31*83ee113eSDavid van Moolenbroek #include <sys/cdefs.h>
32*83ee113eSDavid van Moolenbroek __RCSID("$NetBSD: handle.c,v 1.1.1.3 2014/07/12 11:57:59 spz Exp $");
33*83ee113eSDavid van Moolenbroek 
34*83ee113eSDavid van Moolenbroek #include "dhcpd.h"
35*83ee113eSDavid van Moolenbroek 
36*83ee113eSDavid van Moolenbroek #include <omapip/omapip_p.h>
37*83ee113eSDavid van Moolenbroek 
38*83ee113eSDavid van Moolenbroek /* The handle table is a hierarchical tree designed for quick mapping
39*83ee113eSDavid van Moolenbroek    of handle identifiers to objects.  Objects contain their own handle
40*83ee113eSDavid van Moolenbroek    identifiers if they have them, so the reverse mapping is also
41*83ee113eSDavid van Moolenbroek    quick.  The hierarchy is made up of table objects, each of which
42*83ee113eSDavid van Moolenbroek    has 120 entries, a flag indicating whether the table is a leaf
43*83ee113eSDavid van Moolenbroek    table or an indirect table, the handle of the first object covered
44*83ee113eSDavid van Moolenbroek    by the table and the first object after that that's *not* covered
45*83ee113eSDavid van Moolenbroek    by the table, a count of how many objects of either type are
46*83ee113eSDavid van Moolenbroek    currently stored in the table, and an array of 120 entries pointing
47*83ee113eSDavid van Moolenbroek    either to objects or tables.
48*83ee113eSDavid van Moolenbroek 
49*83ee113eSDavid van Moolenbroek    When we go to add an object to the table, we look to see if the
50*83ee113eSDavid van Moolenbroek    next object handle to be assigned is covered by the outermost
51*83ee113eSDavid van Moolenbroek    table.  If it is, we find the place within that table where the
52*83ee113eSDavid van Moolenbroek    next handle should go, and if necessary create additional nodes in
53*83ee113eSDavid van Moolenbroek    the tree to contain the new handle.  The pointer to the object is
54*83ee113eSDavid van Moolenbroek    then stored in the correct position.
55*83ee113eSDavid van Moolenbroek 
56*83ee113eSDavid van Moolenbroek    Theoretically, we could have some code here to free up handle
57*83ee113eSDavid van Moolenbroek    tables as they go out of use, but by and large handle tables won't
58*83ee113eSDavid van Moolenbroek    go out of use, so this is being skipped for now.  It shouldn't be
59*83ee113eSDavid van Moolenbroek    too hard to implement in the future if there's a different
60*83ee113eSDavid van Moolenbroek    application. */
61*83ee113eSDavid van Moolenbroek 
62*83ee113eSDavid van Moolenbroek omapi_handle_table_t *omapi_handle_table;
63*83ee113eSDavid van Moolenbroek omapi_handle_t omapi_next_handle = 1;	/* Next handle to be assigned. */
64*83ee113eSDavid van Moolenbroek 
65*83ee113eSDavid van Moolenbroek #define FIND_HAND  0
66*83ee113eSDavid van Moolenbroek #define CLEAR_HAND 1
67*83ee113eSDavid van Moolenbroek 
68*83ee113eSDavid van Moolenbroek static isc_result_t omapi_handle_lookup_in (omapi_object_t **,
69*83ee113eSDavid van Moolenbroek 					    omapi_handle_t,
70*83ee113eSDavid van Moolenbroek 					    omapi_handle_table_t *,
71*83ee113eSDavid van Moolenbroek 					    int);
72*83ee113eSDavid van Moolenbroek static isc_result_t omapi_object_handle_in_table (omapi_handle_t,
73*83ee113eSDavid van Moolenbroek 						  omapi_handle_table_t *,
74*83ee113eSDavid van Moolenbroek 						  omapi_object_t *);
75*83ee113eSDavid van Moolenbroek static isc_result_t omapi_handle_table_enclose (omapi_handle_table_t **);
76*83ee113eSDavid van Moolenbroek 
omapi_object_handle(omapi_handle_t * h,omapi_object_t * o)77*83ee113eSDavid van Moolenbroek isc_result_t omapi_object_handle (omapi_handle_t *h, omapi_object_t *o)
78*83ee113eSDavid van Moolenbroek {
79*83ee113eSDavid van Moolenbroek 	isc_result_t status;
80*83ee113eSDavid van Moolenbroek 
81*83ee113eSDavid van Moolenbroek 	if (o -> handle) {
82*83ee113eSDavid van Moolenbroek 		*h = o -> handle;
83*83ee113eSDavid van Moolenbroek 		return ISC_R_SUCCESS;
84*83ee113eSDavid van Moolenbroek 	}
85*83ee113eSDavid van Moolenbroek 
86*83ee113eSDavid van Moolenbroek 	if (!omapi_handle_table) {
87*83ee113eSDavid van Moolenbroek 		omapi_handle_table = dmalloc (sizeof *omapi_handle_table, MDL);
88*83ee113eSDavid van Moolenbroek 		if (!omapi_handle_table)
89*83ee113eSDavid van Moolenbroek 			return ISC_R_NOMEMORY;
90*83ee113eSDavid van Moolenbroek 		memset (omapi_handle_table, 0, sizeof *omapi_handle_table);
91*83ee113eSDavid van Moolenbroek 		omapi_handle_table -> first = 0;
92*83ee113eSDavid van Moolenbroek 		omapi_handle_table -> limit = OMAPI_HANDLE_TABLE_SIZE;
93*83ee113eSDavid van Moolenbroek 		omapi_handle_table -> leafp = 1;
94*83ee113eSDavid van Moolenbroek 	}
95*83ee113eSDavid van Moolenbroek 
96*83ee113eSDavid van Moolenbroek 	/* If this handle doesn't fit in the outer table, we need to
97*83ee113eSDavid van Moolenbroek 	   make a new outer table.  This is a while loop in case for
98*83ee113eSDavid van Moolenbroek 	   some reason we decide to do disjoint handle allocation,
99*83ee113eSDavid van Moolenbroek 	   where the next level of indirection still isn't big enough
100*83ee113eSDavid van Moolenbroek 	   to enclose the next handle ID. */
101*83ee113eSDavid van Moolenbroek 
102*83ee113eSDavid van Moolenbroek 	while (omapi_next_handle >= omapi_handle_table -> limit) {
103*83ee113eSDavid van Moolenbroek 		omapi_handle_table_t *new;
104*83ee113eSDavid van Moolenbroek 
105*83ee113eSDavid van Moolenbroek 		new = dmalloc (sizeof *new, MDL);
106*83ee113eSDavid van Moolenbroek 		if (!new)
107*83ee113eSDavid van Moolenbroek 			return ISC_R_NOMEMORY;
108*83ee113eSDavid van Moolenbroek 		memset (new, 0, sizeof *new);
109*83ee113eSDavid van Moolenbroek 		new -> first = 0;
110*83ee113eSDavid van Moolenbroek 		new -> limit = (omapi_handle_table -> limit *
111*83ee113eSDavid van Moolenbroek 					       OMAPI_HANDLE_TABLE_SIZE);
112*83ee113eSDavid van Moolenbroek 		new -> leafp = 0;
113*83ee113eSDavid van Moolenbroek 		new -> children [0].table = omapi_handle_table;
114*83ee113eSDavid van Moolenbroek 		omapi_handle_table = new;
115*83ee113eSDavid van Moolenbroek 	}
116*83ee113eSDavid van Moolenbroek 
117*83ee113eSDavid van Moolenbroek 	/* Try to cram this handle into the existing table. */
118*83ee113eSDavid van Moolenbroek 	status = omapi_object_handle_in_table (omapi_next_handle,
119*83ee113eSDavid van Moolenbroek 					       omapi_handle_table, o);
120*83ee113eSDavid van Moolenbroek 	/* If it worked, return the next handle and increment it. */
121*83ee113eSDavid van Moolenbroek 	if (status == ISC_R_SUCCESS) {
122*83ee113eSDavid van Moolenbroek 		*h = omapi_next_handle;
123*83ee113eSDavid van Moolenbroek 		omapi_next_handle++;
124*83ee113eSDavid van Moolenbroek 		return ISC_R_SUCCESS;
125*83ee113eSDavid van Moolenbroek 	}
126*83ee113eSDavid van Moolenbroek 	if (status != ISC_R_NOSPACE)
127*83ee113eSDavid van Moolenbroek 		return status;
128*83ee113eSDavid van Moolenbroek 
129*83ee113eSDavid van Moolenbroek 	status = omapi_handle_table_enclose (&omapi_handle_table);
130*83ee113eSDavid van Moolenbroek 	if (status != ISC_R_SUCCESS)
131*83ee113eSDavid van Moolenbroek 		return status;
132*83ee113eSDavid van Moolenbroek 
133*83ee113eSDavid van Moolenbroek 	status = omapi_object_handle_in_table (omapi_next_handle,
134*83ee113eSDavid van Moolenbroek 					       omapi_handle_table, o);
135*83ee113eSDavid van Moolenbroek 	if (status != ISC_R_SUCCESS)
136*83ee113eSDavid van Moolenbroek 		return status;
137*83ee113eSDavid van Moolenbroek 	*h = omapi_next_handle;
138*83ee113eSDavid van Moolenbroek 	omapi_next_handle++;
139*83ee113eSDavid van Moolenbroek 
140*83ee113eSDavid van Moolenbroek 	return ISC_R_SUCCESS;
141*83ee113eSDavid van Moolenbroek }
142*83ee113eSDavid van Moolenbroek 
omapi_object_handle_in_table(omapi_handle_t h,omapi_handle_table_t * table,omapi_object_t * o)143*83ee113eSDavid van Moolenbroek static isc_result_t omapi_object_handle_in_table (omapi_handle_t h,
144*83ee113eSDavid van Moolenbroek 						  omapi_handle_table_t *table,
145*83ee113eSDavid van Moolenbroek 						  omapi_object_t *o)
146*83ee113eSDavid van Moolenbroek {
147*83ee113eSDavid van Moolenbroek 	omapi_handle_table_t *inner;
148*83ee113eSDavid van Moolenbroek 	omapi_handle_t scale, index;
149*83ee113eSDavid van Moolenbroek 	isc_result_t status;
150*83ee113eSDavid van Moolenbroek 
151*83ee113eSDavid van Moolenbroek 	if (table -> first > h || table -> limit <= h)
152*83ee113eSDavid van Moolenbroek 		return ISC_R_NOSPACE;
153*83ee113eSDavid van Moolenbroek 
154*83ee113eSDavid van Moolenbroek 	/* If this is a leaf table, just stash the object in the
155*83ee113eSDavid van Moolenbroek 	   appropriate place. */
156*83ee113eSDavid van Moolenbroek 	if (table -> leafp) {
157*83ee113eSDavid van Moolenbroek 		status = (omapi_object_reference
158*83ee113eSDavid van Moolenbroek 			  (&table -> children [h - table -> first].object,
159*83ee113eSDavid van Moolenbroek 			   o, MDL));
160*83ee113eSDavid van Moolenbroek 		if (status != ISC_R_SUCCESS)
161*83ee113eSDavid van Moolenbroek 			return status;
162*83ee113eSDavid van Moolenbroek 		o -> handle = h;
163*83ee113eSDavid van Moolenbroek 		return ISC_R_SUCCESS;
164*83ee113eSDavid van Moolenbroek 	}
165*83ee113eSDavid van Moolenbroek 
166*83ee113eSDavid van Moolenbroek 	/* Scale is the number of handles represented by each child of this
167*83ee113eSDavid van Moolenbroek 	   table.   For a leaf table, scale would be 1.   For a first level
168*83ee113eSDavid van Moolenbroek 	   of indirection, 120.   For a second, 120 * 120.   Et cetera. */
169*83ee113eSDavid van Moolenbroek 	scale = (table -> limit - table -> first) / OMAPI_HANDLE_TABLE_SIZE;
170*83ee113eSDavid van Moolenbroek 
171*83ee113eSDavid van Moolenbroek 	/* So the next most direct table from this one that contains the
172*83ee113eSDavid van Moolenbroek 	   handle must be the subtable of this table whose index into this
173*83ee113eSDavid van Moolenbroek 	   table's array of children is the handle divided by the scale. */
174*83ee113eSDavid van Moolenbroek 	index = (h - table -> first) / scale;
175*83ee113eSDavid van Moolenbroek 	inner = table -> children [index].table;
176*83ee113eSDavid van Moolenbroek 
177*83ee113eSDavid van Moolenbroek 	/* If there is no more direct table than this one in the slot
178*83ee113eSDavid van Moolenbroek 	   we came up with, make one. */
179*83ee113eSDavid van Moolenbroek 	if (!inner) {
180*83ee113eSDavid van Moolenbroek 		inner = dmalloc (sizeof *inner, MDL);
181*83ee113eSDavid van Moolenbroek 		if (!inner)
182*83ee113eSDavid van Moolenbroek 			return ISC_R_NOMEMORY;
183*83ee113eSDavid van Moolenbroek 		memset (inner, 0, sizeof *inner);
184*83ee113eSDavid van Moolenbroek 		inner -> first = index * scale + table -> first;
185*83ee113eSDavid van Moolenbroek 		inner -> limit = inner -> first + scale;
186*83ee113eSDavid van Moolenbroek 		if (scale == OMAPI_HANDLE_TABLE_SIZE)
187*83ee113eSDavid van Moolenbroek 			inner -> leafp = 1;
188*83ee113eSDavid van Moolenbroek 		table -> children [index].table = inner;
189*83ee113eSDavid van Moolenbroek 	}
190*83ee113eSDavid van Moolenbroek 
191*83ee113eSDavid van Moolenbroek 	status = omapi_object_handle_in_table (h, inner, o);
192*83ee113eSDavid van Moolenbroek 	if (status == ISC_R_NOSPACE) {
193*83ee113eSDavid van Moolenbroek 		status = (omapi_handle_table_enclose
194*83ee113eSDavid van Moolenbroek 			  (&table -> children [index].table));
195*83ee113eSDavid van Moolenbroek 		if (status != ISC_R_SUCCESS)
196*83ee113eSDavid van Moolenbroek 			return status;
197*83ee113eSDavid van Moolenbroek 
198*83ee113eSDavid van Moolenbroek 		return omapi_object_handle_in_table
199*83ee113eSDavid van Moolenbroek 			(h, table -> children [index].table, o);
200*83ee113eSDavid van Moolenbroek 	}
201*83ee113eSDavid van Moolenbroek 	return status;
202*83ee113eSDavid van Moolenbroek }
203*83ee113eSDavid van Moolenbroek 
omapi_handle_table_enclose(omapi_handle_table_t ** table)204*83ee113eSDavid van Moolenbroek static isc_result_t omapi_handle_table_enclose (omapi_handle_table_t **table)
205*83ee113eSDavid van Moolenbroek {
206*83ee113eSDavid van Moolenbroek 	omapi_handle_table_t *inner = *table;
207*83ee113eSDavid van Moolenbroek 	omapi_handle_table_t *new;
208*83ee113eSDavid van Moolenbroek 	int index, base, scale;
209*83ee113eSDavid van Moolenbroek 
210*83ee113eSDavid van Moolenbroek 	/* The scale of the table we're enclosing is going to be the
211*83ee113eSDavid van Moolenbroek 	   difference between its "first" and "limit" members.  So the
212*83ee113eSDavid van Moolenbroek 	   scale of the table enclosing it is going to be that multiplied
213*83ee113eSDavid van Moolenbroek 	   by the table size. */
214*83ee113eSDavid van Moolenbroek 	scale = (inner -> first - inner -> limit) * OMAPI_HANDLE_TABLE_SIZE;
215*83ee113eSDavid van Moolenbroek 
216*83ee113eSDavid van Moolenbroek 	/* The range that the enclosing table covers is going to be
217*83ee113eSDavid van Moolenbroek 	   the result of subtracting the remainder of dividing the
218*83ee113eSDavid van Moolenbroek 	   enclosed table's first entry number by the enclosing
219*83ee113eSDavid van Moolenbroek 	   table's scale.  If handle IDs are being allocated
220*83ee113eSDavid van Moolenbroek 	   sequentially, the enclosing table's "first" value will be
221*83ee113eSDavid van Moolenbroek 	   the same as the enclosed table's "first" value. */
222*83ee113eSDavid van Moolenbroek 	base = inner -> first - inner -> first % scale;
223*83ee113eSDavid van Moolenbroek 
224*83ee113eSDavid van Moolenbroek 	/* The index into the enclosing table at which the enclosed table
225*83ee113eSDavid van Moolenbroek 	   will be stored is going to be the difference between the "first"
226*83ee113eSDavid van Moolenbroek 	   value of the enclosing table and the enclosed table - zero, if
227*83ee113eSDavid van Moolenbroek 	   we are allocating sequentially. */
228*83ee113eSDavid van Moolenbroek 	index = (base - inner -> first) / OMAPI_HANDLE_TABLE_SIZE;
229*83ee113eSDavid van Moolenbroek 
230*83ee113eSDavid van Moolenbroek 	new = dmalloc (sizeof *new, MDL);
231*83ee113eSDavid van Moolenbroek 	if (!new)
232*83ee113eSDavid van Moolenbroek 		return ISC_R_NOMEMORY;
233*83ee113eSDavid van Moolenbroek 	memset (new, 0, sizeof *new);
234*83ee113eSDavid van Moolenbroek 	new -> first = base;
235*83ee113eSDavid van Moolenbroek 	new -> limit = base + scale;
236*83ee113eSDavid van Moolenbroek 	if (scale == OMAPI_HANDLE_TABLE_SIZE)
237*83ee113eSDavid van Moolenbroek 		new -> leafp = 0;
238*83ee113eSDavid van Moolenbroek 	new -> children [index].table = inner;
239*83ee113eSDavid van Moolenbroek 	*table = new;
240*83ee113eSDavid van Moolenbroek 	return ISC_R_SUCCESS;
241*83ee113eSDavid van Moolenbroek }
242*83ee113eSDavid van Moolenbroek 
omapi_handle_lookup(omapi_object_t ** o,omapi_handle_t h)243*83ee113eSDavid van Moolenbroek isc_result_t omapi_handle_lookup (omapi_object_t **o, omapi_handle_t h)
244*83ee113eSDavid van Moolenbroek {
245*83ee113eSDavid van Moolenbroek 	return(omapi_handle_lookup_in(o, h, omapi_handle_table, FIND_HAND));
246*83ee113eSDavid van Moolenbroek }
247*83ee113eSDavid van Moolenbroek 
omapi_handle_lookup_in(omapi_object_t ** o,omapi_handle_t h,omapi_handle_table_t * table,int op)248*83ee113eSDavid van Moolenbroek static isc_result_t omapi_handle_lookup_in (omapi_object_t **o,
249*83ee113eSDavid van Moolenbroek 					    omapi_handle_t h,
250*83ee113eSDavid van Moolenbroek 					    omapi_handle_table_t *table,
251*83ee113eSDavid van Moolenbroek 					    int op)
252*83ee113eSDavid van Moolenbroek {
253*83ee113eSDavid van Moolenbroek 	omapi_handle_t scale, index;
254*83ee113eSDavid van Moolenbroek 
255*83ee113eSDavid van Moolenbroek 	if (!table || table->first > h || table->limit <= h)
256*83ee113eSDavid van Moolenbroek 		return(ISC_R_NOTFOUND);
257*83ee113eSDavid van Moolenbroek 
258*83ee113eSDavid van Moolenbroek 	/* If this is a leaf table, just grab the object. */
259*83ee113eSDavid van Moolenbroek 	if (table->leafp) {
260*83ee113eSDavid van Moolenbroek 		/* Not there? */
261*83ee113eSDavid van Moolenbroek 		if (!table->children[h - table->first].object)
262*83ee113eSDavid van Moolenbroek 			return(ISC_R_NOTFOUND);
263*83ee113eSDavid van Moolenbroek 		if (op == CLEAR_HAND) {
264*83ee113eSDavid van Moolenbroek 			table->children[h - table->first].object = NULL;
265*83ee113eSDavid van Moolenbroek 			return(ISC_R_SUCCESS);
266*83ee113eSDavid van Moolenbroek 		} else {
267*83ee113eSDavid van Moolenbroek 			return(omapi_object_reference
268*83ee113eSDavid van Moolenbroek 			       (o, table->children[h - table->first].object,
269*83ee113eSDavid van Moolenbroek 				MDL));
270*83ee113eSDavid van Moolenbroek 		}
271*83ee113eSDavid van Moolenbroek 	}
272*83ee113eSDavid van Moolenbroek 
273*83ee113eSDavid van Moolenbroek 	/* Scale is the number of handles represented by each child of this
274*83ee113eSDavid van Moolenbroek 	   table.   For a leaf table, scale would be 1.   For a first level
275*83ee113eSDavid van Moolenbroek 	   of indirection, 120.   For a second, 120 * 120.   Et cetera. */
276*83ee113eSDavid van Moolenbroek 	scale = (table->limit - table->first) / OMAPI_HANDLE_TABLE_SIZE;
277*83ee113eSDavid van Moolenbroek 
278*83ee113eSDavid van Moolenbroek 	/* So the next most direct table from this one that contains the
279*83ee113eSDavid van Moolenbroek 	   handle must be the subtable of this table whose index into this
280*83ee113eSDavid van Moolenbroek 	   table's array of children is the handle divided by the scale. */
281*83ee113eSDavid van Moolenbroek 	index = (h - table->first) / scale;
282*83ee113eSDavid van Moolenbroek 
283*83ee113eSDavid van Moolenbroek 	return(omapi_handle_lookup_in(o, h, table->children[index].table, op));
284*83ee113eSDavid van Moolenbroek }
285*83ee113eSDavid van Moolenbroek 
286*83ee113eSDavid van Moolenbroek /* For looking up objects based on handles that have been sent on the wire. */
omapi_handle_td_lookup(omapi_object_t ** obj,omapi_typed_data_t * handle)287*83ee113eSDavid van Moolenbroek isc_result_t omapi_handle_td_lookup (omapi_object_t **obj,
288*83ee113eSDavid van Moolenbroek 				     omapi_typed_data_t *handle)
289*83ee113eSDavid van Moolenbroek {
290*83ee113eSDavid van Moolenbroek 	omapi_handle_t h;
291*83ee113eSDavid van Moolenbroek 
292*83ee113eSDavid van Moolenbroek 	if (handle->type == omapi_datatype_int)
293*83ee113eSDavid van Moolenbroek 		h = handle->u.integer;
294*83ee113eSDavid van Moolenbroek 	else if (handle->type == omapi_datatype_data &&
295*83ee113eSDavid van Moolenbroek 		 handle->u.buffer.len == sizeof h) {
296*83ee113eSDavid van Moolenbroek 		memcpy(&h, handle->u.buffer.value, sizeof h);
297*83ee113eSDavid van Moolenbroek 		h = ntohl(h);
298*83ee113eSDavid van Moolenbroek 	} else
299*83ee113eSDavid van Moolenbroek 		return(DHCP_R_INVALIDARG);
300*83ee113eSDavid van Moolenbroek 	return(omapi_handle_lookup(obj, h));
301*83ee113eSDavid van Moolenbroek }
302*83ee113eSDavid van Moolenbroek 
omapi_handle_clear(omapi_handle_t h)303*83ee113eSDavid van Moolenbroek isc_result_t omapi_handle_clear(omapi_handle_t h)
304*83ee113eSDavid van Moolenbroek {
305*83ee113eSDavid van Moolenbroek 	return(omapi_handle_lookup_in(NULL, h, omapi_handle_table, CLEAR_HAND));
306*83ee113eSDavid van Moolenbroek }
307