1 .\" $NetBSD: dhcpctl.3,v 1.1.1.3 2014/07/12 11:57:51 spz Exp $
7 .\" RCSId: Id: dhcpctl.3,v 1.9 2011/04/25 23:43:16 sar Exp
9 .\" Copyright (c) 2011,2014 by Internet Systems Consortium, Inc. ("ISC")
10 .\" Copyright (c) 2004,2009 by Internet Systems Consortium, Inc. ("ISC")
11 .\" Copyright (c) 2000-2003 by Internet Software Consortium
12 .\" Copyright (c) 2000 Nominum, Inc.
14 .\" Permission to use, copy, modify, and distribute this software for any
15 .\" purpose with or without fee is hereby granted, provided that the above
16 .\" copyright notice and this permission notice appear in all copies.
18 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
19 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
21 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
24 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 .\" Internet Systems Consortium, Inc.
27 .\" 950 Charter Street
28 .\" Redwood City, CA 94063
30 .\" https://www.isc.org/
32 .\" Description: dhcpctl man page.
38 .ds vT DHCP Programmer's Manual
43 .Nm dhcpctl_initialize
44 .Nd dhcpctl library initialization.
49 .Fd #include <dhcpctl/dhcpctl.h>
51 .Fo dhcpctl_initialize
57 .Fa "dhcpctl_handle *cxn"
58 .Fa "const char *host"
60 .Fa "dhcpctl_handle auth"
66 .Fo dhcpctl_wait_for_completion
67 .Fa "dhcpctl_handle object"
68 .Fa "dhcpctl_status *status"
75 .Fa "dhcpctl_data_string *value"
76 .Fa "dhcpctl_handle object"
77 .Fa "const char *name"
83 .Fo dhcpctl_get_boolean
85 .Fa "dhcpctl_handle object"
86 .Fa "const char *name"
93 .Fa "dhcpctl_handle object"
94 .Fa "dhcpctl_data_string value"
95 .Fa "const char *name"
101 .Fo dhcpctl_set_string_value
102 .Fa "dhcpctl_handle object"
103 .Fa "const char *value"
104 .Fa "const char *name"
110 .Fo dhcpctl_set_boolean_value
111 .Fa "dhcpctl_handle object"
113 .Fa "const char *name"
119 .Fo dhcpctl_set_int_value
120 .Fa "dhcpctl_handle object"
122 .Fa "const char *name"
128 .Fo dhcpctl_object_update
129 .Fa "dhcpctl_handle connection"
130 .Fa "dhcpctl_handle object"
136 .Fo dhcpctl_object_refresh
137 .Fa "dhcpctl_handle connection"
138 .Fa "dhcpctl_handle object"
144 .Fo dhcpctl_object_remove
145 .Fa "dhcpctl_handle connection"
146 .Fa "dhcpctl_handle object"
152 .Fo dhcpctl_set_callback
153 .Fa "dhcpctl_handle object"
155 .Fa "void (*function) (dhcpctl_handle, dhcpctl_status, void *)"
161 .Fo dhcpctl_new_authenticator
162 .Fa "dhcpctl_handle *object"
163 .Fa "const char *name"
164 .Fa "const char *algorithm"
165 .Fa "const char *secret"
166 .Fa "unsigned secret_len"
172 .Fo dhcpctl_new_object
173 .Fa "dhcpctl_handle *object"
174 .Fa "dhcpctl_handle connection"
175 .Fa "const char *object_type"
181 .Fo dhcpctl_open_object
182 .Fa "dhcpctl_handle object"
183 .Fa "dhcpctl_handle connection"
190 .Fo omapi_data_string_new
191 .Fa dhcpctl_data_string *data
192 .Fa unsigned int length
193 .Fa const char *filename,
200 .Fo dhcpctl_data_string_dereference
201 .Fa "dhcpctl_data_string *"
206 The dhcpctl set of functions provide an API that can be used to communicate
207 with and manipulate a running ISC DHCP server. All functions return a value of
209 The return values reflects the result of operations to local data
210 structures. If an operation fails on the server for any reason, then the error
211 result will be returned through the
212 second parameter of the
213 .Fn dhcpctl_wait_for_completion
219 .Fn dhcpctl_initialize
220 sets up the data structures the library needs to do its work. This function
221 must be called once before any other.
224 opens a connection to the DHCP server at the given host and port. If an
225 authenticator has been created for the connection, then it is given as the 4th
226 argument. On a successful return the address pointed at by the first
227 argument will have a new connection object assigned to it.
230 .Bd -literal -offset indent
231 s = dhcpctl_connect(&cxn, "127.0.0.1", 7911, NULL);
234 connects to the DHCP server on the localhost via port 7911 (the standard
235 OMAPI port). No authentication is used for the connection.
240 .Fn dhcpctl_wait_for_completion
241 flushes a pending message to the server and waits for the response. The result
242 of the request as processed on the server is returned via the second
244 .Bd -literal -offset indent
245 s = dhcpctl_wait_for_completion(cxn, &wv);
246 if (s != ISC_R_SUCCESS)
248 else if (wv != ISC_R_SUCCESS)
253 .Fn dhcpctl_wait_for_completion
254 won't return until the remote message processing completes or the connection
255 to the server is lost.
260 .Fn dhcpctl_get_value
261 extracts a value of an attribute from the handle. The value can be of any
262 length and is treated as a sequence of bytes. The handle must have been
264 .Fn dhcpctl_new_object
266 .Fn dhcpctl_open_object .
267 The value is returned via the parameter named
269 The last parameter is the name of attribute to retrieve.
270 .Bd -literal -offset indent
271 dhcpctl_data_string value = NULL;
272 dhcpctl_handle lease;
275 s = dhcpctl_get_value (&value, lease, "ends");
276 assert(s == ISC_R_SUCCESS && value->len == sizeof(thetime));
277 memcpy(&thetime, value->value, value->len);
283 .Fn dhcpctl_get_boolean
284 extracts a boolean valued attribute from the object handle.
290 .Fn dhcpctl_set_value ,
291 .Fn dhcpctl_set_string_value ,
292 .Fn dhcpctl_set_boolean_value ,
294 .Fn dhcpctl_set_int_value
295 functions all set a value on the object handle.
300 .Fn dhcpctl_object_update
301 function queues a request for
302 all the changes made to the object handle be sent to the remote
303 for processing. The changes made to the attributes on the handle will be
304 applied to remote object if permitted.
309 .Fn dhcpctl_object_refresh
310 queues up a request for a fresh copy of all the attribute values to be sent
312 refresh the values in the local object handle.
317 .Fn dhcpctl_object_remove
318 queues a request for the removal on the server of the object referenced by the
325 .Fn dhcpctl_set_callback
326 function sets up a user-defined function to be called when an event completes
327 on the given object handle. This is needed for asynchronous handling of
328 events, versus the synchronous handling given by
329 .Fn dhcpctl_wait_for_completion .
330 When the function is called the first parameter is the object the event
331 arrived for, the second is the status of the message that was processed, the
332 third is the same value as the second parameter given to
333 .Fn dhcpctl_set_callback .
339 .Fn dhcpctl_new_authenticator
340 creates a new authenticator object to be used for signing the messages
341 that cross over the network. The
346 values must all match what the server uses and are defined in its
347 configuration file. The created object is returned through the first parameter
348 and must be used as the 4th parameter to
349 .Fn dhcpctl_connect .
350 Note that the 'secret' value must not be base64 encoded, which is different
351 from how the value appears in the dhcpd.conf file.
356 .Fn dhcpctl_new_object
357 creates a local handle for an object on the server. The
359 parameter is the ascii name of the type of object being accessed. e.g.
361 This function only sets up local data structures, it does not queue any
363 to be sent to the remote side,
364 .Fn dhcpctl_open_object
370 .Fn dhcpctl_open_object
371 builds and queues the request to the remote side. This function is used with
373 .Fn dhcpctl_new_object .
374 The flags argument is a bit mask with the following values available for
376 .Bl -tag -offset indent -width 20
378 if the object does not exist then the remote will create it
380 update the object on the remote side using the
381 attributes already set in the handle.
383 return and error if the object exists and DHCPCTL_CREATE
391 .Fn omapi_data_string_new
392 function allocates a new
393 .Ft dhcpctl_data_string
394 object. The data string will be large enough to hold
400 arguments are the source file location the call is made from, typically by
413 .Fn dhcpctl_data_string_dereference
414 deallocates a data string created by
415 .Fn omapi_data_string_new .
416 The memory for the object won't be freed until the last reference is
420 The following program will connect to the DHCP server running on the local
421 host and will get the details of the existing lease for IP address
422 10.0.0.101. It will then print out the time the lease is due to expire. Note
423 that most error checking has been omitted for brevity.
424 .Bd -literal -offset indent
425 #include <sys/time.h>
431 #include <sys/socket.h>
432 #include <netinet/in.h>
433 #include <arpa/inet.h>
435 #include "omapip/result.h"
438 int main (int argc, char **argv) {
439 dhcpctl_data_string ipaddrstring = NULL;
440 dhcpctl_data_string value = NULL;
441 dhcpctl_handle connection = NULL;
442 dhcpctl_handle lease = NULL;
443 isc_result_t waitstatus;
444 struct in_addr convaddr;
447 dhcpctl_initialize ();
449 dhcpctl_connect (&connection, "127.0.0.1",
452 dhcpctl_new_object (&lease, connection,
455 memset (&ipaddrstring, 0, sizeof
458 inet_pton(AF_INET, "10.0.0.101",
461 omapi_data_string_new (&ipaddrstring,
463 memcpy(ipaddrstring->value, &convaddr.s_addr, 4);
465 dhcpctl_set_value (lease, ipaddrstring,
468 dhcpctl_open_object (lease, connection, 0);
470 dhcpctl_wait_for_completion (lease,
472 if (waitstatus != ISC_R_SUCCESS) {
473 /* server not authoritative */
477 dhcpctl_data_string_dereference(&ipaddrstring,
480 dhcpctl_get_value (&value, lease, "ends");
482 memcpy(&thetime, value->value, value->len);
484 dhcpctl_data_string_dereference(&value, MDL);
486 fprintf (stdout, "ending time is %s",
491 omapi(3), omshell(1), dhcpd(8), dhclient(8), dhcpd.conf(5), dhclient.conf(5).
494 is maintained by ISC. To learn more about Internet Systems Consortium,
496 .B https://www.isc.org