3 Subroutines providing general support for objects. */
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1999-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
36 static char copyright
[] =
37 "$Id: dhcpctl.c,v 1.5 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
40 #include <omapip/omapip_p.h>
43 omapi_object_type_t
*dhcpctl_callback_type
;
44 omapi_object_type_t
*dhcpctl_remote_type
;
46 /* dhcpctl_initialize ()
48 Must be called before any other dhcpctl function. */
50 dhcpctl_status
dhcpctl_initialize ()
54 status
= omapi_init();
55 if (status
!= ISC_R_SUCCESS
)
58 status
= omapi_object_type_register (&dhcpctl_callback_type
,
60 dhcpctl_callback_set_value
,
61 dhcpctl_callback_get_value
,
62 dhcpctl_callback_destroy
,
63 dhcpctl_callback_signal_handler
,
64 dhcpctl_callback_stuff_values
,
67 (dhcpctl_callback_object_t
), 0,
69 if (status
!= ISC_R_SUCCESS
)
72 status
= omapi_object_type_register (&dhcpctl_remote_type
,
74 dhcpctl_remote_set_value
,
75 dhcpctl_remote_get_value
,
76 dhcpctl_remote_destroy
,
77 dhcpctl_remote_signal_handler
,
78 dhcpctl_remote_stuff_values
,
80 sizeof (dhcpctl_remote_object_t
),
82 if (status
!= ISC_R_SUCCESS
)
91 returns nonzero status code if it didn't connect, zero otherwise
92 stores connection handle through connection, which can be used
93 for subsequent access to the specified server.
94 server_name is the name of the server, and port is the TCP
95 port on which it is listening.
96 authinfo is the handle to an object containing authentication
99 dhcpctl_status
dhcpctl_connect (dhcpctl_handle
*connection
,
100 const char *server_name
, int port
,
101 dhcpctl_handle authinfo
)
105 status
= omapi_generic_new (connection
, MDL
);
106 if (status
!= ISC_R_SUCCESS
) {
110 status
= omapi_protocol_connect (*connection
, server_name
,
111 (unsigned)port
, authinfo
);
112 if (status
== ISC_R_SUCCESS
)
114 if (status
!= ISC_R_INCOMPLETE
) {
115 omapi_object_dereference (connection
, MDL
);
119 status
= omapi_wait_for_completion (*connection
, 0);
120 if (status
!= ISC_R_SUCCESS
) {
121 omapi_object_dereference (connection
, MDL
);
128 /* dhcpctl_wait_for_completion
131 returns zero if the callback completes, a nonzero status if
132 there was some problem relating to the wait operation. The
133 status of the queued request will be stored through s, and
134 will also be either zero for success or nonzero for some kind
135 of failure. Never returns until completion or until the
136 connection to the server is lost. This performs the same
137 function as dhcpctl_set_callback and the subsequent callback,
138 for programs that want to do inline execution instead of using
141 dhcpctl_status
dhcpctl_wait_for_completion (dhcpctl_handle h
,
145 status
= omapi_wait_for_completion (h
, 0);
146 if (status
!= ISC_R_SUCCESS
)
148 if (h
-> type
== dhcpctl_remote_type
)
149 *s
= ((dhcpctl_remote_object_t
*)h
) -> waitstatus
;
150 return ISC_R_SUCCESS
;
156 returns zero if the call succeeded, a nonzero status code if
158 result is the address of an empty data string (initialized
159 with bzero or cleared with data_string_forget). On
160 successful completion, the addressed data string will contain
161 the value that was fetched.
162 dhcpctl_handle refers to some dhcpctl item
163 value_name refers to some value related to that item - e.g.,
164 for a handle associated with a completed host lookup, value
165 could be one of "hardware-address", "dhcp-client-identifier",
166 "known" or "client-hostname". */
168 dhcpctl_status
dhcpctl_get_value (dhcpctl_data_string
*result
,
169 dhcpctl_handle h
, const char *value_name
)
172 omapi_value_t
*tv
= (omapi_value_t
*)0;
176 status
= omapi_get_value_str (h
, (omapi_object_t
*)0, value_name
, &tv
);
177 if (status
!= ISC_R_SUCCESS
)
180 switch (tv
-> value
-> type
) {
181 case omapi_datatype_int
:
185 case omapi_datatype_string
:
186 case omapi_datatype_data
:
187 len
= tv
-> value
-> u
.buffer
.len
;
190 case omapi_datatype_object
:
191 len
= sizeof (omapi_handle_t
);
195 omapi_typed_data_dereference (&tv
-> value
, MDL
);
196 return ISC_R_UNEXPECTED
;
199 status
= omapi_data_string_new (result
, len
, MDL
);
200 if (status
!= ISC_R_SUCCESS
) {
201 omapi_typed_data_dereference (&tv
-> value
, MDL
);
205 switch (tv
-> value
-> type
) {
206 case omapi_datatype_int
:
207 ip
= htonl (tv
-> value
-> u
.integer
);
208 memcpy ((*result
) -> value
, &ip
, sizeof ip
);
211 case omapi_datatype_string
:
212 case omapi_datatype_data
:
213 memcpy ((*result
) -> value
,
214 tv
-> value
-> u
.buffer
.value
,
215 tv
-> value
-> u
.buffer
.len
);
218 case omapi_datatype_object
:
219 ip
= htonl (tv
-> value
-> u
.object
-> handle
);
220 memcpy ((*result
) -> value
, &ip
, sizeof ip
);
224 omapi_value_dereference (&tv
, MDL
);
225 return ISC_R_SUCCESS
;
228 /* dhcpctl_get_boolean
230 like dhcpctl_get_value, but more convenient for boolean
231 values, since no data_string needs to be dealt with. */
233 dhcpctl_status
dhcpctl_get_boolean (int *result
,
234 dhcpctl_handle h
, const char *value_name
)
237 dhcpctl_data_string data
= (dhcpctl_data_string
)0;
240 status
= dhcpctl_get_value (&data
, h
, value_name
);
241 if (status
!= ISC_R_SUCCESS
)
243 if (data
-> len
!= sizeof rv
) {
244 omapi_data_string_dereference (&data
, MDL
);
245 return ISC_R_UNEXPECTED
;
247 memcpy (&rv
, data
-> value
, sizeof rv
);
248 *result
= ntohl (rv
);
249 return ISC_R_SUCCESS
;
254 Sets a value on an object referred to by a dhcpctl_handle.
255 The opposite of dhcpctl_get_value. Does not update the
256 server - just sets the value on the handle. */
258 dhcpctl_status
dhcpctl_set_value (dhcpctl_handle h
, dhcpctl_data_string value
,
259 const char *value_name
)
262 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
263 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
265 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
266 if (status
!= ISC_R_SUCCESS
)
268 memcpy (name
-> value
, value_name
, strlen (value_name
));
270 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_data
,
272 if (status
!= ISC_R_SUCCESS
) {
273 omapi_data_string_dereference (&name
, MDL
);
276 memcpy (tv
-> u
.buffer
.value
, value
-> value
, value
-> len
);
278 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
279 omapi_data_string_dereference (&name
, MDL
);
280 omapi_typed_data_dereference (&tv
, MDL
);
284 /* dhcpctl_set_string_value
286 Sets a NUL-terminated ASCII value on an object referred to by
287 a dhcpctl_handle. like dhcpctl_set_value, but saves the
288 trouble of creating a data_string for a NUL-terminated string.
289 Does not update the server - just sets the value on the handle. */
291 dhcpctl_status
dhcpctl_set_string_value (dhcpctl_handle h
, const char *value
,
292 const char *value_name
)
295 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
296 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
298 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
299 if (status
!= ISC_R_SUCCESS
)
301 memcpy (name
-> value
, value_name
, strlen (value_name
));
303 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_string
, value
);
304 if (status
!= ISC_R_SUCCESS
) {
305 omapi_data_string_dereference (&name
, MDL
);
309 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
310 omapi_data_string_dereference (&name
, MDL
);
311 omapi_typed_data_dereference (&tv
, MDL
);
315 /* dhcpctl_set_buffer_value
317 Sets a value on an object referred to by a dhcpctl_handle. like
318 dhcpctl_set_value, but saves the trouble of creating a data_string
319 for string for which we have a buffer and length. Does not update
320 the server - just sets the value on the handle. */
322 dhcpctl_status
dhcpctl_set_data_value (dhcpctl_handle h
,
323 const char *value
, unsigned len
,
324 const char *value_name
)
327 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
328 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
331 ll
= strlen (value_name
);
332 status
= omapi_data_string_new (&name
, ll
, MDL
);
333 if (status
!= ISC_R_SUCCESS
)
335 memcpy (name
-> value
, value_name
, ll
);
337 status
= omapi_typed_data_new (MDL
, &tv
,
338 omapi_datatype_data
, len
, value
);
339 if (status
!= ISC_R_SUCCESS
) {
340 omapi_data_string_dereference (&name
, MDL
);
343 memcpy (tv
-> u
.buffer
.value
, value
, len
);
345 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
346 omapi_data_string_dereference (&name
, MDL
);
347 omapi_typed_data_dereference (&tv
, MDL
);
351 /* dhcpctl_set_null_value
353 Sets a null value on an object referred to by a dhcpctl_handle. */
355 dhcpctl_status
dhcpctl_set_null_value (dhcpctl_handle h
,
356 const char *value_name
)
359 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
362 ll
= strlen (value_name
);
363 status
= omapi_data_string_new (&name
, ll
, MDL
);
364 if (status
!= ISC_R_SUCCESS
)
366 memcpy (name
-> value
, value_name
, ll
);
368 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
,
369 (omapi_typed_data_t
*)0);
370 omapi_data_string_dereference (&name
, MDL
);
374 /* dhcpctl_set_boolean_value
376 Sets a boolean value on an object - like dhcpctl_set_value,
377 only more convenient for booleans. */
379 dhcpctl_status
dhcpctl_set_boolean_value (dhcpctl_handle h
, int value
,
380 const char *value_name
)
383 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
384 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
386 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
387 if (status
!= ISC_R_SUCCESS
)
389 memcpy (name
-> value
, value_name
, strlen (value_name
));
391 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_int
, value
);
392 if (status
!= ISC_R_SUCCESS
) {
393 omapi_data_string_dereference (&name
, MDL
);
397 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
398 omapi_data_string_dereference (&name
, MDL
);
399 omapi_typed_data_dereference (&tv
, MDL
);
403 /* dhcpctl_set_int_value
405 Sets a boolean value on an object - like dhcpctl_set_value,
406 only more convenient for booleans. */
408 dhcpctl_status
dhcpctl_set_int_value (dhcpctl_handle h
, int value
,
409 const char *value_name
)
412 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
413 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
415 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
416 if (status
!= ISC_R_SUCCESS
)
418 memcpy (name
-> value
, value_name
, strlen (value_name
));
420 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_int
, value
);
421 if (status
!= ISC_R_SUCCESS
) {
422 omapi_data_string_dereference (&name
, MDL
);
426 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
427 omapi_data_string_dereference (&name
, MDL
);
428 omapi_typed_data_dereference (&tv
, MDL
);
432 /* dhcpctl_object_update
434 Queues an update on the object referenced by the handle (there
435 can't be any other work in progress on the handle). An
436 update means local parameters will be sent to the server. */
438 dhcpctl_status
dhcpctl_object_update (dhcpctl_handle connection
,
442 omapi_object_t
*message
= (omapi_object_t
*)0;
443 dhcpctl_remote_object_t
*ro
;
445 if (h
-> type
!= dhcpctl_remote_type
)
446 return ISC_R_INVALIDARG
;
447 ro
= (dhcpctl_remote_object_t
*)h
;
449 status
= omapi_message_new (&message
, MDL
);
450 if (status
!= ISC_R_SUCCESS
) {
451 omapi_object_dereference (&message
, MDL
);
454 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
455 "op", OMAPI_OP_UPDATE
);
456 if (status
!= ISC_R_SUCCESS
) {
457 omapi_object_dereference (&message
, MDL
);
461 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
463 if (status
!= ISC_R_SUCCESS
) {
464 omapi_object_dereference (&message
, MDL
);
468 status
= omapi_set_int_value (message
, (omapi_object_t
*)0, "handle",
469 (int)(ro
-> remote_handle
));
470 if (status
!= ISC_R_SUCCESS
) {
471 omapi_object_dereference (&message
, MDL
);
475 omapi_message_register (message
);
476 status
= omapi_protocol_send_message (connection
-> outer
,
478 message
, (omapi_object_t
*)0);
479 omapi_object_dereference (&message
, MDL
);
483 /* Requests a refresh on the object referenced by the handle (there
484 can't be any other work in progress on the handle). A
485 refresh means local parameters are updated from the server. */
487 dhcpctl_status
dhcpctl_object_refresh (dhcpctl_handle connection
,
491 omapi_object_t
*message
= (omapi_object_t
*)0;
492 dhcpctl_remote_object_t
*ro
;
494 if (h
-> type
!= dhcpctl_remote_type
)
495 return ISC_R_INVALIDARG
;
496 ro
= (dhcpctl_remote_object_t
*)h
;
498 status
= omapi_message_new (&message
, MDL
);
499 if (status
!= ISC_R_SUCCESS
) {
500 omapi_object_dereference (&message
, MDL
);
503 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
504 "op", OMAPI_OP_REFRESH
);
505 if (status
!= ISC_R_SUCCESS
) {
506 omapi_object_dereference (&message
, MDL
);
509 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
510 "handle", (int)(ro
-> remote_handle
));
511 if (status
!= ISC_R_SUCCESS
) {
512 omapi_object_dereference (&message
, MDL
);
516 omapi_message_register (message
);
517 status
= omapi_protocol_send_message (connection
-> outer
,
519 message
, (omapi_object_t
*)0);
521 /* We don't want to send the contents of the object down the
522 wire, but we do need to reference it so that we know what
523 to do with the update. */
524 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
526 if (status
!= ISC_R_SUCCESS
) {
527 omapi_object_dereference (&message
, MDL
);
531 omapi_object_dereference (&message
, MDL
);
535 /* Requests the removal of the object referenced by the handle (there
536 can't be any other work in progress on the handle). A
537 removal means that all searchable references to the object on the
538 server are deleted. */
540 dhcpctl_status
dhcpctl_object_remove (dhcpctl_handle connection
,
544 omapi_object_t
*message
= (omapi_object_t
*)0;
545 dhcpctl_remote_object_t
*ro
;
547 if (h
-> type
!= dhcpctl_remote_type
)
548 return ISC_R_INVALIDARG
;
549 ro
= (dhcpctl_remote_object_t
*)h
;
551 status
= omapi_message_new (&message
, MDL
);
552 if (status
!= ISC_R_SUCCESS
) {
553 omapi_object_dereference (&message
, MDL
);
556 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
557 "op", OMAPI_OP_DELETE
);
558 if (status
!= ISC_R_SUCCESS
) {
559 omapi_object_dereference (&message
, MDL
);
563 status
= omapi_set_int_value (message
, (omapi_object_t
*)0, "handle",
564 (int)(ro
-> remote_handle
));
565 if (status
!= ISC_R_SUCCESS
) {
566 omapi_object_dereference (&message
, MDL
);
570 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
572 if (status
!= ISC_R_SUCCESS
) {
573 omapi_object_dereference (&message
, MDL
);
577 omapi_message_register (message
);
578 status
= omapi_protocol_send_message (connection
-> outer
,
580 message
, (omapi_object_t
*)0);
581 omapi_object_dereference (&message
, MDL
);
585 isc_result_t
dhcpctl_data_string_dereference (dhcpctl_data_string
*vp
,
586 const char *file
, int line
)
588 return omapi_data_string_dereference (vp
, file
, line
);