1 /* $NetBSD: dhcpctl.c,v 1.1.1.2 2014/07/12 11:57:52 spz Exp $ */
4 Subroutines providing general support for objects. */
7 * Copyright (c) 2009,2013,2014 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1999-2003 by Internet Software Consortium
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Internet Systems Consortium, Inc.
25 * Redwood City, CA 94063
27 * https://www.isc.org/
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: dhcpctl.c,v 1.1.1.2 2014/07/12 11:57:52 spz Exp $");
35 #include <omapip/omapip_p.h>
38 omapi_object_type_t
*dhcpctl_callback_type
;
39 omapi_object_type_t
*dhcpctl_remote_type
;
41 /* dhcpctl_initialize ()
43 Must be called before any other dhcpctl function. */
45 dhcpctl_status
dhcpctl_initialize ()
49 /* Set up the isc and dns library managers */
50 status
= dhcp_context_create(DHCP_CONTEXT_PRE_DB
| DHCP_CONTEXT_POST_DB
,
52 if (status
!= ISC_R_SUCCESS
)
55 status
= omapi_init();
56 if (status
!= ISC_R_SUCCESS
)
59 status
= omapi_object_type_register (&dhcpctl_callback_type
,
61 dhcpctl_callback_set_value
,
62 dhcpctl_callback_get_value
,
63 dhcpctl_callback_destroy
,
64 dhcpctl_callback_signal_handler
,
65 dhcpctl_callback_stuff_values
,
68 (dhcpctl_callback_object_t
), 0,
70 if (status
!= ISC_R_SUCCESS
)
73 status
= omapi_object_type_register (&dhcpctl_remote_type
,
75 dhcpctl_remote_set_value
,
76 dhcpctl_remote_get_value
,
77 dhcpctl_remote_destroy
,
78 dhcpctl_remote_signal_handler
,
79 dhcpctl_remote_stuff_values
,
81 sizeof (dhcpctl_remote_object_t
),
83 if (status
!= ISC_R_SUCCESS
)
92 returns nonzero status code if it didn't connect, zero otherwise
93 stores connection handle through connection, which can be used
94 for subsequent access to the specified server.
95 server_name is the name of the server, and port is the TCP
96 port on which it is listening.
97 authinfo is the handle to an object containing authentication
100 dhcpctl_status
dhcpctl_connect (dhcpctl_handle
*connection
,
101 const char *server_name
, int port
,
102 dhcpctl_handle authinfo
)
106 status
= omapi_generic_new (connection
, MDL
);
107 if (status
!= ISC_R_SUCCESS
) {
111 status
= omapi_protocol_connect (*connection
, server_name
,
112 (unsigned)port
, authinfo
);
113 if (status
== ISC_R_SUCCESS
)
115 if (status
!= DHCP_R_INCOMPLETE
) {
116 omapi_object_dereference (connection
, MDL
);
120 status
= omapi_wait_for_completion (*connection
, 0);
121 if (status
!= ISC_R_SUCCESS
) {
122 omapi_object_dereference (connection
, MDL
);
129 /* dhcpctl_wait_for_completion
132 returns zero if the callback completes, a nonzero status if
133 there was some problem relating to the wait operation. The
134 status of the queued request will be stored through s, and
135 will also be either zero for success or nonzero for some kind
136 of failure. Never returns until completion or until the
137 connection to the server is lost. This performs the same
138 function as dhcpctl_set_callback and the subsequent callback,
139 for programs that want to do inline execution instead of using
142 dhcpctl_status
dhcpctl_wait_for_completion (dhcpctl_handle h
,
146 status
= omapi_wait_for_completion (h
, 0);
147 if (status
!= ISC_R_SUCCESS
)
149 if (h
-> type
== dhcpctl_remote_type
)
150 *s
= ((dhcpctl_remote_object_t
*)h
) -> waitstatus
;
151 return ISC_R_SUCCESS
;
157 returns zero if the call succeeded, a nonzero status code if
159 result is the address of an empty data string (initialized
160 with bzero or cleared with data_string_forget). On
161 successful completion, the addressed data string will contain
162 the value that was fetched.
163 dhcpctl_handle refers to some dhcpctl item
164 value_name refers to some value related to that item - e.g.,
165 for a handle associated with a completed host lookup, value
166 could be one of "hardware-address", "dhcp-client-identifier",
167 "known" or "client-hostname". */
169 dhcpctl_status
dhcpctl_get_value (dhcpctl_data_string
*result
,
170 dhcpctl_handle h
, const char *value_name
)
173 omapi_value_t
*tv
= (omapi_value_t
*)0;
177 status
= omapi_get_value_str (h
, (omapi_object_t
*)0, value_name
, &tv
);
178 if (status
!= ISC_R_SUCCESS
)
181 switch (tv
-> value
-> type
) {
182 case omapi_datatype_int
:
186 case omapi_datatype_string
:
187 case omapi_datatype_data
:
188 len
= tv
-> value
-> u
.buffer
.len
;
191 case omapi_datatype_object
:
192 len
= sizeof (omapi_handle_t
);
196 omapi_typed_data_dereference (&tv
-> value
, MDL
);
197 return ISC_R_UNEXPECTED
;
200 status
= omapi_data_string_new (result
, len
, MDL
);
201 if (status
!= ISC_R_SUCCESS
) {
202 omapi_typed_data_dereference (&tv
-> value
, MDL
);
206 switch (tv
-> value
-> type
) {
207 case omapi_datatype_int
:
208 ip
= htonl (tv
-> value
-> u
.integer
);
209 memcpy ((*result
) -> value
, &ip
, sizeof ip
);
212 case omapi_datatype_string
:
213 case omapi_datatype_data
:
214 memcpy ((*result
) -> value
,
215 tv
-> value
-> u
.buffer
.value
,
216 tv
-> value
-> u
.buffer
.len
);
219 case omapi_datatype_object
:
220 ip
= htonl (tv
-> value
-> u
.object
-> handle
);
221 memcpy ((*result
) -> value
, &ip
, sizeof ip
);
225 omapi_value_dereference (&tv
, MDL
);
226 return ISC_R_SUCCESS
;
229 /* dhcpctl_get_boolean
231 like dhcpctl_get_value, but more convenient for boolean
232 values, since no data_string needs to be dealt with. */
234 dhcpctl_status
dhcpctl_get_boolean (int *result
,
235 dhcpctl_handle h
, const char *value_name
)
238 dhcpctl_data_string data
= (dhcpctl_data_string
)0;
241 status
= dhcpctl_get_value (&data
, h
, value_name
);
242 if (status
!= ISC_R_SUCCESS
)
244 if (data
-> len
!= sizeof rv
) {
245 omapi_data_string_dereference (&data
, MDL
);
246 return ISC_R_UNEXPECTED
;
248 memcpy (&rv
, data
-> value
, sizeof rv
);
249 *result
= ntohl (rv
);
250 return ISC_R_SUCCESS
;
255 Sets a value on an object referred to by a dhcpctl_handle.
256 The opposite of dhcpctl_get_value. Does not update the
257 server - just sets the value on the handle. */
259 dhcpctl_status
dhcpctl_set_value (dhcpctl_handle h
, dhcpctl_data_string value
,
260 const char *value_name
)
263 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
264 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
266 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
267 if (status
!= ISC_R_SUCCESS
)
269 memcpy (name
-> value
, value_name
, strlen (value_name
));
271 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_data
,
273 if (status
!= ISC_R_SUCCESS
) {
274 omapi_data_string_dereference (&name
, MDL
);
277 memcpy (tv
-> u
.buffer
.value
, value
-> value
, value
-> len
);
279 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
280 omapi_data_string_dereference (&name
, MDL
);
281 omapi_typed_data_dereference (&tv
, MDL
);
285 /* dhcpctl_set_string_value
287 Sets a NUL-terminated ASCII value on an object referred to by
288 a dhcpctl_handle. like dhcpctl_set_value, but saves the
289 trouble of creating a data_string for a NUL-terminated string.
290 Does not update the server - just sets the value on the handle. */
292 dhcpctl_status
dhcpctl_set_string_value (dhcpctl_handle h
, const char *value
,
293 const char *value_name
)
296 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
297 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
299 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
300 if (status
!= ISC_R_SUCCESS
)
302 memcpy (name
-> value
, value_name
, strlen (value_name
));
304 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_string
, value
);
305 if (status
!= ISC_R_SUCCESS
) {
306 omapi_data_string_dereference (&name
, MDL
);
310 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
311 omapi_data_string_dereference (&name
, MDL
);
312 omapi_typed_data_dereference (&tv
, MDL
);
316 /* dhcpctl_set_buffer_value
318 Sets a value on an object referred to by a dhcpctl_handle. like
319 dhcpctl_set_value, but saves the trouble of creating a data_string
320 for string for which we have a buffer and length. Does not update
321 the server - just sets the value on the handle. */
323 dhcpctl_status
dhcpctl_set_data_value (dhcpctl_handle h
,
324 const char *value
, unsigned len
,
325 const char *value_name
)
328 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
329 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
332 ll
= strlen (value_name
);
333 status
= omapi_data_string_new (&name
, ll
, MDL
);
334 if (status
!= ISC_R_SUCCESS
)
336 memcpy (name
-> value
, value_name
, ll
);
338 status
= omapi_typed_data_new (MDL
, &tv
,
339 omapi_datatype_data
, len
, value
);
340 if (status
!= ISC_R_SUCCESS
) {
341 omapi_data_string_dereference (&name
, MDL
);
344 memcpy (tv
-> u
.buffer
.value
, value
, len
);
346 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
347 omapi_data_string_dereference (&name
, MDL
);
348 omapi_typed_data_dereference (&tv
, MDL
);
352 /* dhcpctl_set_null_value
354 Sets a null value on an object referred to by a dhcpctl_handle. */
356 dhcpctl_status
dhcpctl_set_null_value (dhcpctl_handle h
,
357 const char *value_name
)
360 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
363 ll
= strlen (value_name
);
364 status
= omapi_data_string_new (&name
, ll
, MDL
);
365 if (status
!= ISC_R_SUCCESS
)
367 memcpy (name
-> value
, value_name
, ll
);
369 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
,
370 (omapi_typed_data_t
*)0);
371 omapi_data_string_dereference (&name
, MDL
);
375 /* dhcpctl_set_boolean_value
377 Sets a boolean value on an object - like dhcpctl_set_value,
378 only more convenient for booleans. */
380 dhcpctl_status
dhcpctl_set_boolean_value (dhcpctl_handle h
, int value
,
381 const char *value_name
)
384 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
385 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
387 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
388 if (status
!= ISC_R_SUCCESS
)
390 memcpy (name
-> value
, value_name
, strlen (value_name
));
392 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_int
, value
);
393 if (status
!= ISC_R_SUCCESS
) {
394 omapi_data_string_dereference (&name
, MDL
);
398 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
399 omapi_data_string_dereference (&name
, MDL
);
400 omapi_typed_data_dereference (&tv
, MDL
);
404 /* dhcpctl_set_int_value
406 Sets a boolean value on an object - like dhcpctl_set_value,
407 only more convenient for booleans. */
409 dhcpctl_status
dhcpctl_set_int_value (dhcpctl_handle h
, int value
,
410 const char *value_name
)
413 omapi_typed_data_t
*tv
= (omapi_typed_data_t
*)0;
414 omapi_data_string_t
*name
= (omapi_data_string_t
*)0;
416 status
= omapi_data_string_new (&name
, strlen (value_name
), MDL
);
417 if (status
!= ISC_R_SUCCESS
)
419 memcpy (name
-> value
, value_name
, strlen (value_name
));
421 status
= omapi_typed_data_new (MDL
, &tv
, omapi_datatype_int
, value
);
422 if (status
!= ISC_R_SUCCESS
) {
423 omapi_data_string_dereference (&name
, MDL
);
427 status
= omapi_set_value (h
, (omapi_object_t
*)0, name
, tv
);
428 omapi_data_string_dereference (&name
, MDL
);
429 omapi_typed_data_dereference (&tv
, MDL
);
433 /* dhcpctl_object_update
435 Queues an update on the object referenced by the handle (there
436 can't be any other work in progress on the handle). An
437 update means local parameters will be sent to the server. */
439 dhcpctl_status
dhcpctl_object_update (dhcpctl_handle connection
,
443 omapi_object_t
*message
= (omapi_object_t
*)0;
444 dhcpctl_remote_object_t
*ro
;
446 if (h
-> type
!= dhcpctl_remote_type
)
447 return DHCP_R_INVALIDARG
;
448 ro
= (dhcpctl_remote_object_t
*)h
;
450 status
= omapi_message_new (&message
, MDL
);
451 if (status
!= ISC_R_SUCCESS
) {
452 omapi_object_dereference (&message
, MDL
);
455 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
456 "op", OMAPI_OP_UPDATE
);
457 if (status
!= ISC_R_SUCCESS
) {
458 omapi_object_dereference (&message
, MDL
);
462 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
464 if (status
!= ISC_R_SUCCESS
) {
465 omapi_object_dereference (&message
, MDL
);
469 status
= omapi_set_int_value (message
, (omapi_object_t
*)0, "handle",
470 (int)(ro
-> remote_handle
));
471 if (status
!= ISC_R_SUCCESS
) {
472 omapi_object_dereference (&message
, MDL
);
476 omapi_message_register (message
);
477 status
= omapi_protocol_send_message (connection
-> outer
,
479 message
, (omapi_object_t
*)0);
480 omapi_object_dereference (&message
, MDL
);
484 /* Requests a refresh on the object referenced by the handle (there
485 can't be any other work in progress on the handle). A
486 refresh means local parameters are updated from the server. */
488 dhcpctl_status
dhcpctl_object_refresh (dhcpctl_handle connection
,
492 omapi_object_t
*message
= (omapi_object_t
*)0;
493 dhcpctl_remote_object_t
*ro
;
495 if (h
-> type
!= dhcpctl_remote_type
)
496 return DHCP_R_INVALIDARG
;
497 ro
= (dhcpctl_remote_object_t
*)h
;
499 status
= omapi_message_new (&message
, MDL
);
500 if (status
!= ISC_R_SUCCESS
) {
501 omapi_object_dereference (&message
, MDL
);
504 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
505 "op", OMAPI_OP_REFRESH
);
506 if (status
!= ISC_R_SUCCESS
) {
507 omapi_object_dereference (&message
, MDL
);
510 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
511 "handle", (int)(ro
-> remote_handle
));
512 if (status
!= ISC_R_SUCCESS
) {
513 omapi_object_dereference (&message
, MDL
);
517 omapi_message_register (message
);
518 status
= omapi_protocol_send_message (connection
-> outer
,
520 message
, (omapi_object_t
*)0);
522 /* We don't want to send the contents of the object down the
523 wire, but we do need to reference it so that we know what
524 to do with the update. */
525 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
527 if (status
!= ISC_R_SUCCESS
) {
528 omapi_object_dereference (&message
, MDL
);
532 omapi_object_dereference (&message
, MDL
);
536 /* Requests the removal of the object referenced by the handle (there
537 can't be any other work in progress on the handle). A
538 removal means that all searchable references to the object on the
539 server are deleted. */
541 dhcpctl_status
dhcpctl_object_remove (dhcpctl_handle connection
,
545 omapi_object_t
*message
= (omapi_object_t
*)0;
546 dhcpctl_remote_object_t
*ro
;
548 if (h
-> type
!= dhcpctl_remote_type
)
549 return DHCP_R_INVALIDARG
;
550 ro
= (dhcpctl_remote_object_t
*)h
;
552 status
= omapi_message_new (&message
, MDL
);
553 if (status
!= ISC_R_SUCCESS
) {
554 omapi_object_dereference (&message
, MDL
);
557 status
= omapi_set_int_value (message
, (omapi_object_t
*)0,
558 "op", OMAPI_OP_DELETE
);
559 if (status
!= ISC_R_SUCCESS
) {
560 omapi_object_dereference (&message
, MDL
);
564 status
= omapi_set_int_value (message
, (omapi_object_t
*)0, "handle",
565 (int)(ro
-> remote_handle
));
566 if (status
!= ISC_R_SUCCESS
) {
567 omapi_object_dereference (&message
, MDL
);
571 status
= omapi_set_object_value (message
, (omapi_object_t
*)0,
573 if (status
!= ISC_R_SUCCESS
) {
574 omapi_object_dereference (&message
, MDL
);
578 omapi_message_register (message
);
579 status
= omapi_protocol_send_message (connection
-> outer
,
581 message
, (omapi_object_t
*)0);
582 omapi_object_dereference (&message
, MDL
);
586 isc_result_t
dhcpctl_data_string_dereference (dhcpctl_data_string
*vp
,
587 const char *file
, int line
)
589 return omapi_data_string_dereference (vp
, file
, line
);