]> git.ipfire.org Git - thirdparty/dhcp.git/blame - dhcpctl/dhcpctl.3
[#76] Added new functions to man page dhcpctl/dhcpctl.3
[thirdparty/dhcp.git] / dhcpctl / dhcpctl.3
CommitLineData
45cf20c6
BC
1.\" -*- nroff -*-
2.\"
3.\" Project: DHCP
4.\" File: dhcpctl.3
fc06ee4f 5.\" RCSId: $Id: dhcpctl.3,v 1.9 2011/04/25 23:43:16 sar Exp $
f6b8f48d 6.\"
9121bf67 7.\" Copyright (c) 2004-2022 by Internet Systems Consortium, Inc. ("ISC")
98311e4b
DH
8.\" Copyright (c) 2000-2003 by Internet Software Consortium
9.\" Copyright (c) 2000 Nominum, Inc.
10.\"
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.
14.\"
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.
22.\"
23.\" Internet Systems Consortium, Inc.
429a56d7
TM
24.\" PO Box 360
25.\" Newmarket, NH 03857 USA
98311e4b 26.\" <info@isc.org>
2c85ac9b 27.\" https://www.isc.org/
f6b8f48d 28.\"
45cf20c6 29.\" Description: dhcpctl man page.
f6b8f48d 30.\"
45cf20c6
BC
31.\"
32.Dd Nov 15, 2000
33.Dt DHCPCTL 3
34.Os DHCP 3
35.ds vT DHCP Programmer's Manual
36.\"
37.\"
38.\"
39.Sh NAME
40.Nm dhcpctl_initialize
41.Nd dhcpctl library initialization.
42.\"
43.\"
44.\"
45.Sh SYNOPSIS
46.Fd #include <dhcpctl/dhcpctl.h>
45cf20c6
BC
47.Ft dhcpctl_status
48.Fo dhcpctl_initialize
49.Fa void
50.Fc
51.\"
52.Ft dhcpctl_status
53.Fo dhcpctl_connect
54.Fa "dhcpctl_handle *cxn"
55.Fa "const char *host"
56.Fa "int port"
57.Fa "dhcpctl_handle auth"
58.Fc
59.\"
60.\"
61.\"
62.Ft dhcpctl_status
9121bf67
TM
63.Fo dhcpctl_disconnect
64.Fa "dhcpctl_handle *cxn"
65.Fa "int force"
66.Fc
67.\"
68.\"
69.\"
70.Ft dhcpctl_status
45cf20c6
BC
71.Fo dhcpctl_wait_for_completion
72.Fa "dhcpctl_handle object"
73.Fa "dhcpctl_status *status"
74.Fc
75.\"
76.\"
77.\"
78.Ft dhcpctl_status
9121bf67
TM
79.Fo dhcpctl_timed_wait_for_completion
80.Fa "dhcpctl_handle object"
81.Fa "struct timeval *timeout"
82.Fa "dhcpctl_status *status"
83.Fc
84.\"
85.\"
86.\"
87.Ft dhcpctl_status
45cf20c6
BC
88.Fo dhcpctl_get_value
89.Fa "dhcpctl_data_string *value"
90.Fa "dhcpctl_handle object"
91.Fa "const char *name"
92.Fc
93.\"
94.\"
95.\"
96.Ft dhcpctl_status
97.Fo dhcpctl_get_boolean
98.Fa "int *value"
99.Fa "dhcpctl_handle object"
100.Fa "const char *name"
101.Fc
102.\"
103.\"
104.\"
105.Ft dhcpctl_status
106.Fo dhcpctl_set_value
107.Fa "dhcpctl_handle object"
108.Fa "dhcpctl_data_string value"
109.Fa "const char *name"
110.Fc
111.\"
112.\"
113.\"
114.Ft dhcpctl_status
115.Fo dhcpctl_set_string_value
116.Fa "dhcpctl_handle object"
117.Fa "const char *value"
118.Fa "const char *name"
119.Fc
120.\"
121.\"
122.\"
123.Ft dhcpctl_status
124.Fo dhcpctl_set_boolean_value
125.Fa "dhcpctl_handle object"
126.Fa "int value"
127.Fa "const char *name"
128.Fc
129.\"
130.\"
131.\"
132.Ft dhcpctl_status
133.Fo dhcpctl_set_int_value
134.Fa "dhcpctl_handle object"
135.Fa "int value"
136.Fa "const char *name"
137.Fc
138.\"
139.\"
140.\"
141.Ft dhcpctl_status
142.Fo dhcpctl_object_update
143.Fa "dhcpctl_handle connection"
144.Fa "dhcpctl_handle object"
145.Fc
146.\"
147.\"
148.\"
149.Ft dhcpctl_status
150.Fo dhcpctl_object_refresh
151.Fa "dhcpctl_handle connection"
152.Fa "dhcpctl_handle object"
153.Fc
154.\"
155.\"
156.\"
157.Ft dhcpctl_status
158.Fo dhcpctl_object_remove
159.Fa "dhcpctl_handle connection"
160.Fa "dhcpctl_handle object"
161.Fc
162.\"
163.\"
164.\"
165.Ft dhcpctl_status
166.Fo dhcpctl_set_callback
167.Fa "dhcpctl_handle object"
168.Fa "void *data"
169.Fa "void (*function) (dhcpctl_handle, dhcpctl_status, void *)"
170.Fc
171.\"
172.\"
173.\"
174.Ft dhcpctl_status
175.Fo dhcpctl_new_authenticator
176.Fa "dhcpctl_handle *object"
177.Fa "const char *name"
178.Fa "const char *algorithm"
179.Fa "const char *secret"
180.Fa "unsigned secret_len"
181.Fc
182.\"
183.\"
184.\"
185.Ft dhcpctl_status
186.Fo dhcpctl_new_object
187.Fa "dhcpctl_handle *object"
188.Fa "dhcpctl_handle connection"
189.Fa "const char *object_type"
190.Fc
191.\"
192.\"
193.\"
194.Ft dhcpctl_status
195.Fo dhcpctl_open_object
196.Fa "dhcpctl_handle object"
197.Fa "dhcpctl_handle connection"
198.Fa "int flags"
199.Fc
200.\"
201.\"
202.\"
203.Ft isc_result_t
204.Fo omapi_data_string_new
205.Fa dhcpctl_data_string *data
206.Fa unsigned int length
207.Fa const char *filename,
208.Fa int lineno
209.Fc
210.\"
211.\"
212.\"
213.Ft isc_result_t
214.Fo dhcpctl_data_string_dereference
215.Fa "dhcpctl_data_string *"
216.Fa "const char *"
217.Fa "int"
218.Fc
219.Sh DESCRIPTION
220The dhcpctl set of functions provide an API that can be used to communicate
221with and manipulate a running ISC DHCP server. All functions return a value of
222.Dv isc_result_t .
223The return values reflects the result of operations to local data
224structures. If an operation fails on the server for any reason, then the error
225result will be returned through the
f6b8f48d
TM
226second parameter of the
227.Fn dhcpctl_wait_for_completion
45cf20c6
BC
228call.
229.\"
230.\"
231.\"
232.Pp
233.Fn dhcpctl_initialize
234sets up the data structures the library needs to do its work. This function
235must be called once before any other.
236.Pp
237.Fn dhcpctl_connect
238opens a connection to the DHCP server at the given host and port. If an
239authenticator has been created for the connection, then it is given as the 4th
240argument. On a successful return the address pointed at by the first
241argument will have a new connection object assigned to it.
242.Pp
243For example:
244.Bd -literal -offset indent
245s = dhcpctl_connect(&cxn, "127.0.0.1", 7911, NULL);
246.Ed
247.Pp
248connects to the DHCP server on the localhost via port 7911 (the standard
249OMAPI port). No authentication is used for the connection.
250.\"
251.\"
252.\"
253.Pp
9121bf67
TM
254
255.Fn dhcpctl_disconnect
256closes the open connection specified by the first parameter, \fBcxn\fR. Note
257that this call will free the connection object and \fBcxn\fR will be set to
258 nul. If the second parameter,\fBforce\fR, is nonzero, the connection will be
259closed immediately. Otherwise the receiving end will be shut down but any
260unsent data will be sent before actually closing the socket. Note that
261disconnecting only destroys the connection object, any other objects previously
262created will still exist.
263.Pp
264For example:
265.Bd -literal -offset indent
266s = dhcpctl_disconnect(&cxn, 1);
267.Ed
268.Pp
269will close the connection immediately. This funcion should be considered
270\fBEXPERIMENTAL\fR.
271.\"
272.\"
273.\"
274.Pp
45cf20c6
BC
275.Fn dhcpctl_wait_for_completion
276flushes a pending message to the server and waits for the response. The result
277of the request as processed on the server is returned via the second
278parameter.
279.Bd -literal -offset indent
280s = dhcpctl_wait_for_completion(cxn, &wv);
f6b8f48d 281if (s != ISC_R_SUCCESS)
45cf20c6
BC
282 local_failure(s);
283else if (wv != ISC_R_SUCCESS)
284 server_failure(wc);
285.Ed
286.Pp
f6b8f48d 287The call to
45cf20c6
BC
288.Fn dhcpctl_wait_for_completion
289won't return until the remote message processing completes or the connection
290to the server is lost.
9121bf67
TM
291.\"
292.\"
293.\"
294.Pp
295
296.Fn dhcpctl_timed_wait_for_completion
297flushes a pending message to the server and waits for the response. How long
298the function waits for a response is dictated by the value of the second
299parameter, \fBtimeout\fR. If the value is null, it will wait indefinetly or
300until the connection is lost. Otherwise it will wait for the amount of time
301specified by \fBtimeout\fR (tv_sec:tv_usec). Values of zero for both fields
302are valid but not recommended. The result of the request as processed on the
303server is returned via the third parameter. An example is shown below:
304.Bd -literal -offset indent
305
306struct timeval timeout;
307timeout.tv_sec = 5; /* wait for 5 seconds */
308timeout.tv_usec = 0;
309
310s = dhcpctl_wait_for_completion(cxn, &timeout, &wv);
311if (s != ISC_R_SUCCESS) {
312 local_failure(s);
313} else if (wv != ISC_R_SUCCESS) {
314 server_failure(wc);
315}
316.Ed
317.Pp
318If the function times out, the status returned will be ISC_R_TIMEDOUT. Please
319note that even though the function is no longer waiting for a response, the
320server does not abandon the request and may still respond by writing the
321response to the socket. A subsequent call to either this function or
322\fBdhcpctl_wait_for_completion()\fR will see that data and read it. Depending
323on the application logic flow this may or may not be desired. Currently though
324only mechanism for "flushing" this data is to close the connection by calling
325\fBdisconnet()\fR, and then reconnecting via \fBconnect()\fR. Please note
326this function should be considered \fBEXPERIMENTAL\fR.
327
45cf20c6
BC
328.\"
329.\"
330.\"
331.Pp
332.Fn dhcpctl_get_value
333extracts a value of an attribute from the handle. The value can be of any
334length and is treated as a sequence of bytes. The handle must have been
335created first with
336.Fn dhcpctl_new_object
337and opened with
338.Fn dhcpctl_open_object .
339The value is returned via the parameter named
340.Dq value .
341The last parameter is the name of attribute to retrieve.
342.Bd -literal -offset indent
343dhcpctl_data_string value = NULL;
344dhcpctl_handle lease;
345time_t thetime;
346
347s = dhcpctl_get_value (&value, lease, "ends");
348assert(s == ISC_R_SUCCESS && value->len == sizeof(thetime));
349memcpy(&thetime, value->value, value->len);
350.Ed
351.\"
352.\"
353.\"
354.Pp
355.Fn dhcpctl_get_boolean
356extracts a boolean valued attribute from the object handle.
357.\"
358.\"
359.\"
360.Pp
361The
362.Fn dhcpctl_set_value ,
363.Fn dhcpctl_set_string_value ,
364.Fn dhcpctl_set_boolean_value ,
365and
366.Fn dhcpctl_set_int_value
f6b8f48d 367functions all set a value on the object handle.
45cf20c6
BC
368.\"
369.\"
370.\"
371.Pp
372.Fn dhcpctl_object_update
373function queues a request for
8e112e2b
JR
374all the changes made to the object handle be sent to the remote
375for processing. The changes made to the attributes on the handle will be
45cf20c6
BC
376applied to remote object if permitted.
377.\"
378.\"
379.\"
380.Pp
381.Fn dhcpctl_object_refresh
382queues up a request for a fresh copy of all the attribute values to be sent
383from the remote to
384refresh the values in the local object handle.
385.\"
386.\"
387.\"
388.Pp
389.Fn dhcpctl_object_remove
390queues a request for the removal on the server of the object referenced by the
391handle.
392.\"
393.\"
394.\"
395.Pp
f6b8f48d 396The
45cf20c6
BC
397.Fn dhcpctl_set_callback
398function sets up a user-defined function to be called when an event completes
399on the given object handle. This is needed for asynchronous handling of
400events, versus the synchronous handling given by
401.Fn dhcpctl_wait_for_completion .
402When the function is called the first parameter is the object the event
403arrived for, the second is the status of the message that was processed, the
f6b8f48d 404third is the same value as the second parameter given to
45cf20c6
BC
405.Fn dhcpctl_set_callback .
406.\"
407.\"
408.\"
409.Pp
f6b8f48d 410The
45cf20c6
BC
411.Fn dhcpctl_new_authenticator
412creates a new authenticator object to be used for signing the messages
f6b8f48d 413that cross over the network. The
45cf20c6
BC
414.Dq name ,
415.Dq algorithm ,
f6b8f48d 416and
45cf20c6
BC
417.Dq secret
418values must all match what the server uses and are defined in its
419configuration file. The created object is returned through the first parameter
f6b8f48d 420and must be used as the 4th parameter to
45cf20c6 421.Fn dhcpctl_connect .
98311e4b
DH
422Note that the 'secret' value must not be base64 encoded, which is different
423from how the value appears in the dhcpd.conf file.
45cf20c6
BC
424.\"
425.\"
426.\"
427.Pp
428.Fn dhcpctl_new_object
f6b8f48d 429creates a local handle for an object on the server. The
45cf20c6 430.Dq object_type
f6b8f48d 431parameter is the ascii name of the type of object being accessed. e.g.
45cf20c6 432.Qq lease .
f6b8f48d 433This function only sets up local data structures, it does not queue any
45cf20c6
BC
434messages
435to be sent to the remote side,
436.Fn dhcpctl_open_object
437does that.
438.\"
439.\"
440.\"
441.Pp
442.Fn dhcpctl_open_object
443builds and queues the request to the remote side. This function is used with
444handle created via
445.Fn dhcpctl_new_object .
446The flags argument is a bit mask with the following values available for
447setting:
448.Bl -tag -offset indent -width 20
449.It DHCPCTL_CREATE
450if the object does not exist then the remote will create it
451.It DHCPCTL_UPDATE
452update the object on the remote side using the
453attributes already set in the handle.
454.It DHCPCTL_EXCL
455return and error if the object exists and DHCPCTL_CREATE
456was also specified
457.El
458.\"
459.\"
460.\"
461.Pp
f6b8f48d 462The
45cf20c6
BC
463.Fn omapi_data_string_new
464function allocates a new
465.Ft dhcpctl_data_string
f6b8f48d 466object. The data string will be large enough to hold
45cf20c6
BC
467.Dq length
468bytes of data. The
f6b8f48d 469.Dq file
45cf20c6
BC
470and
471.Dq lineno
472arguments are the source file location the call is made from, typically by
f6b8f48d 473using the
45cf20c6
BC
474.Dv __FILE__
475and
476.Dv __LINE__
f6b8f48d 477macros or the
45cf20c6
BC
478.Dv MDL
479macro defined in
480.
481.\"
482.\"
483.\"
484.Pp
485.Fn dhcpctl_data_string_dereference
486deallocates a data string created by
487.Fn omapi_data_string_new .
d758ad8c
TL
488The memory for the object won't be freed until the last reference is
489released.
45cf20c6 490.Sh EXAMPLES
f6b8f48d 491.Pp
45cf20c6
BC
492The following program will connect to the DHCP server running on the local
493host and will get the details of the existing lease for IP address
49410.0.0.101. It will then print out the time the lease is due to expire. Note
f6b8f48d 495that most error checking has been omitted for brevity.
45cf20c6 496.Bd -literal -offset indent
45cf20c6 497#include <sys/time.h>
45cf20c6 498#include <stdio.h>
fc06ee4f
SR
499#include <stdlib.h>
500#include <string.h>
501#include <stdarg.h>
502
503#include <sys/socket.h>
45cf20c6 504#include <netinet/in.h>
fc06ee4f 505#include <arpa/inet.h>
45cf20c6 506
fc06ee4f
SR
507#include "omapip/result.h"
508#include "dhcpctl.h"
45cf20c6
BC
509
510int main (int argc, char **argv) {
511 dhcpctl_data_string ipaddrstring = NULL;
512 dhcpctl_data_string value = NULL;
513 dhcpctl_handle connection = NULL;
514 dhcpctl_handle lease = NULL;
515 isc_result_t waitstatus;
516 struct in_addr convaddr;
517 time_t thetime;
518
519 dhcpctl_initialize ();
520
521 dhcpctl_connect (&connection, "127.0.0.1",
522 7911, 0);
f6b8f48d 523
45cf20c6
BC
524 dhcpctl_new_object (&lease, connection,
525 "lease");
526
527 memset (&ipaddrstring, 0, sizeof
528 ipaddrstring);
529
530 inet_pton(AF_INET, "10.0.0.101",
531 &convaddr);
532
533 omapi_data_string_new (&ipaddrstring,
534 4, MDL);
535 memcpy(ipaddrstring->value, &convaddr.s_addr, 4);
536
537 dhcpctl_set_value (lease, ipaddrstring,
538 "ip-address");
539
540 dhcpctl_open_object (lease, connection, 0);
541
542 dhcpctl_wait_for_completion (lease,
543 &waitstatus);
544 if (waitstatus != ISC_R_SUCCESS) {
545 /* server not authoritative */
546 exit (0);
547 }
548
549 dhcpctl_data_string_dereference(&ipaddrstring,
550 MDL);
551
552 dhcpctl_get_value (&value, lease, "ends");
553
554 memcpy(&thetime, value->value, value->len);
555
556 dhcpctl_data_string_dereference(&value, MDL);
557
558 fprintf (stdout, "ending time is %s",
559 ctime(&thetime));
560}
561.Ed
562.Sh SEE ALSO
fac2bbb0 563omapi(3), omshell(1), dhcpd(8), dhclient(8), dhcpd.conf(5), dhclient.conf(5).
98311e4b
DH
564.Sh AUTHOR
565.Em dhcpctl
edad9be5
SR
566is maintained by ISC. To learn more about Internet Systems Consortium,
567see
568.B https://www.isc.org