5 .\" RCSId: $Id: dhcpctl.3,v 1.3.2.1 2001/06/26 18:35:35 mellon Exp $
7 .\" Copyright (C) 2000 Nominum, Inc.
9 .\" Description: dhcpctl man page.
15 .ds vT DHCP Programmer's Manual
20 .Nm dhcpctl_initialize
21 .Nd dhcpctl library initialization.
26 .Fd #include <dhcpctl/dhcpctl.h>
29 .Fo dhcpctl_initialize
35 .Fa "dhcpctl_handle *cxn"
36 .Fa "const char *host"
38 .Fa "dhcpctl_handle auth"
44 .Fo dhcpctl_wait_for_completion
45 .Fa "dhcpctl_handle object"
46 .Fa "dhcpctl_status *status"
53 .Fa "dhcpctl_data_string *value"
54 .Fa "dhcpctl_handle object"
55 .Fa "const char *name"
61 .Fo dhcpctl_get_boolean
63 .Fa "dhcpctl_handle object"
64 .Fa "const char *name"
71 .Fa "dhcpctl_handle object"
72 .Fa "dhcpctl_data_string value"
73 .Fa "const char *name"
79 .Fo dhcpctl_set_string_value
80 .Fa "dhcpctl_handle object"
81 .Fa "const char *value"
82 .Fa "const char *name"
88 .Fo dhcpctl_set_boolean_value
89 .Fa "dhcpctl_handle object"
91 .Fa "const char *name"
97 .Fo dhcpctl_set_int_value
98 .Fa "dhcpctl_handle object"
100 .Fa "const char *name"
106 .Fo dhcpctl_object_update
107 .Fa "dhcpctl_handle connection"
108 .Fa "dhcpctl_handle object"
114 .Fo dhcpctl_object_refresh
115 .Fa "dhcpctl_handle connection"
116 .Fa "dhcpctl_handle object"
122 .Fo dhcpctl_object_remove
123 .Fa "dhcpctl_handle connection"
124 .Fa "dhcpctl_handle object"
130 .Fo dhcpctl_set_callback
131 .Fa "dhcpctl_handle object"
133 .Fa "void (*function) (dhcpctl_handle, dhcpctl_status, void *)"
139 .Fo dhcpctl_new_authenticator
140 .Fa "dhcpctl_handle *object"
141 .Fa "const char *name"
142 .Fa "const char *algorithm"
143 .Fa "const char *secret"
144 .Fa "unsigned secret_len"
150 .Fo dhcpctl_new_object
151 .Fa "dhcpctl_handle *object"
152 .Fa "dhcpctl_handle connection"
153 .Fa "const char *object_type"
159 .Fo dhcpctl_open_object
160 .Fa "dhcpctl_handle object"
161 .Fa "dhcpctl_handle connection"
168 .Fo omapi_data_string_new
169 .Fa dhcpctl_data_string *data
170 .Fa unsigned int length
171 .Fa const char *filename,
178 .Fo dhcpctl_data_string_dereference
179 .Fa "dhcpctl_data_string *"
184 The dhcpctl set of functions provide an API that can be used to communicate
185 with and manipulate a running ISC DHCP server. All functions return a value of
187 The return values reflects the result of operations to local data
188 structures. If an operation fails on the server for any reason, then the error
189 result will be returned through the
190 second parameter of the
191 .Fn dhcpctl_wait_for_completion
197 .Fn dhcpctl_initialize
198 sets up the data structures the library needs to do its work. This function
199 must be called once before any other.
202 opens a connection to the DHCP server at the given host and port. If an
203 authenticator has been created for the connection, then it is given as the 4th
204 argument. On a successful return the address pointed at by the first
205 argument will have a new connection object assigned to it.
208 .Bd -literal -offset indent
209 s = dhcpctl_connect(&cxn, "127.0.0.1", 7911, NULL);
212 connects to the DHCP server on the localhost via port 7911 (the standard
213 OMAPI port). No authentication is used for the connection.
218 .Fn dhcpctl_wait_for_completion
219 flushes a pending message to the server and waits for the response. The result
220 of the request as processed on the server is returned via the second
222 .Bd -literal -offset indent
223 s = dhcpctl_wait_for_completion(cxn, &wv);
224 if (s != ISC_R_SUCCESS)
226 else if (wv != ISC_R_SUCCESS)
231 .Fn dhcpctl_wait_for_completion
232 won't return until the remote message processing completes or the connection
233 to the server is lost.
238 .Fn dhcpctl_get_value
239 extracts a value of an attribute from the handle. The value can be of any
240 length and is treated as a sequence of bytes. The handle must have been
242 .Fn dhcpctl_new_object
244 .Fn dhcpctl_open_object .
245 The value is returned via the parameter named
247 The last parameter is the name of attribute to retrieve.
248 .Bd -literal -offset indent
249 dhcpctl_data_string value = NULL;
250 dhcpctl_handle lease;
253 s = dhcpctl_get_value (&value, lease, "ends");
254 assert(s == ISC_R_SUCCESS && value->len == sizeof(thetime));
255 memcpy(&thetime, value->value, value->len);
261 .Fn dhcpctl_get_boolean
262 extracts a boolean valued attribute from the object handle.
268 .Fn dhcpctl_set_value ,
269 .Fn dhcpctl_set_string_value ,
270 .Fn dhcpctl_set_boolean_value ,
272 .Fn dhcpctl_set_int_value
273 functions all set a value on the object handle.
278 .Fn dhcpctl_object_update
279 function queues a request for
280 all the changes made to the object handle be be sent to the remote
281 for processing. The changes made to the atributes on the handle will be
282 applied to remote object if permitted.
287 .Fn dhcpctl_object_refresh
288 queues up a request for a fresh copy of all the attribute values to be sent
290 refresh the values in the local object handle.
295 .Fn dhcpctl_object_remove
296 queues a request for the removal on the server of the object referenced by the
303 .Fn dhcpctl_set_callback
304 function sets up a user-defined function to be called when an event completes
305 on the given object handle. This is needed for asynchronous handling of
306 events, versus the synchronous handling given by
307 .Fn dhcpctl_wait_for_completion .
308 When the function is called the first parameter is the object the event
309 arrived for, the second is the status of the message that was processed, the
310 third is the same value as the second parameter given to
311 .Fn dhcpctl_set_callback .
317 .Fn dhcpctl_new_authenticator
318 creates a new authenticator object to be used for signing the messages
319 that cross over the network. The
324 values must all match what the server uses and are defined in its
325 configuration file. The created object is returned through the first parameter
326 and must be used as the 4th parameter to
327 .Fn dhcpctl_connect .
332 .Fn dhcpctl_new_object
333 creates a local handle for an object on the the server. The
335 parameter is the ascii name of the type of object being accessed. e.g.
337 This function only sets up local data structures, it does not queue any
339 to be sent to the remote side,
340 .Fn dhcpctl_open_object
346 .Fn dhcpctl_open_object
347 builds and queues the request to the remote side. This function is used with
349 .Fn dhcpctl_new_object .
350 The flags argument is a bit mask with the following values available for
352 .Bl -tag -offset indent -width 20
354 if the object does not exist then the remote will create it
356 update the object on the remote side using the
357 attributes already set in the handle.
359 return and error if the object exists and DHCPCTL_CREATE
367 .Fn omapi_data_string_new
368 function allocates a new
369 .Ft dhcpctl_data_string
370 object. The data string will be large enough to hold
376 arguments are the source file location the call is made from, typically by
389 .Fn dhcpctl_data_string_dereference
390 deallocates a data string created by
391 .Fn omapi_data_string_new .
392 The memory for the object won't be freed until the last reference is
396 The following program will connect to the DHCP server running on the local
397 host and will get the details of the existing lease for IP address
398 10.0.0.101. It will then print out the time the lease is due to expire. Note
399 that most error checking has been ommitted for brevity.
400 .Bd -literal -offset indent
402 #include <sys/time.h>
403 #include <sys/socket.h>
405 #include <netinet/in.h>
407 #include <isc/result.h>
408 #include <dhcpctl/dhcpctl.h>
410 int main (int argc, char **argv) {
411 dhcpctl_data_string ipaddrstring = NULL;
412 dhcpctl_data_string value = NULL;
413 dhcpctl_handle connection = NULL;
414 dhcpctl_handle lease = NULL;
415 isc_result_t waitstatus;
416 struct in_addr convaddr;
419 dhcpctl_initialize ();
421 dhcpctl_connect (&connection, "127.0.0.1",
424 dhcpctl_new_object (&lease, connection,
427 memset (&ipaddrstring, 0, sizeof
430 inet_pton(AF_INET, "10.0.0.101",
433 omapi_data_string_new (&ipaddrstring,
435 memcpy(ipaddrstring->value, &convaddr.s_addr, 4);
437 dhcpctl_set_value (lease, ipaddrstring,
440 dhcpctl_open_object (lease, connection, 0);
442 dhcpctl_wait_for_completion (lease,
444 if (waitstatus != ISC_R_SUCCESS) {
445 /* server not authoritative */
449 dhcpctl_data_string_dereference(&ipaddrstring,
452 dhcpctl_get_value (&value, lease, "ends");
454 memcpy(&thetime, value->value, value->len);
456 dhcpctl_data_string_dereference(&value, MDL);
458 fprintf (stdout, "ending time is %s",
464 omapi(3), omshell(3), dhcpd(8), dhclient(8), dhcpd.conf(5), dhclient.conf(5).
467 was written by Ted Lemon of Nominum, Inc. Information about Nominum
468 and support contracts for DHCP and BIND can be found at
469 .B http://www.nominum.com. This preliminary documentation was
470 written by James Brister of Nominum, Inc.