8273327
.\" -*- nroff -*-
8273327
.\"
8273327
.\" Project:      DHCP
8273327
.\" File:         dhcpctl.3
8273327
.\" RCSId:        $Id$
8273327
.\" 
8273327
.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8273327
.\" Copyright (c) 2000-2003 by Internet Software Consortium
8273327
.\" Copyright (c) 2000 Nominum, Inc.
8273327
.\"
8273327
.\" Permission to use, copy, modify, and distribute this software for any
8273327
.\" purpose with or without fee is hereby granted, provided that the above
8273327
.\" copyright notice and this permission notice appear in all copies.
8273327
.\"
8273327
.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
8273327
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
8273327
.\" MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
8273327
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
8273327
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
8273327
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
8273327
.\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8273327
.\"
8273327
.\"   Internet Systems Consortium, Inc.
8273327
.\"   950 Charter Street
8273327
.\"   Redwood City, CA 94063
8273327
.\"   <info@isc.org>
8273327
.\"   http://www.isc.org/
8273327
.\"     
8273327
.\" Description:	dhcpctl man page.
8273327
.\" 
8273327
.\"
8273327
.Dd Nov 15, 2000
8273327
.Dt DHCPCTL 3
8273327
.Os DHCP 3
8273327
.ds vT DHCP Programmer's Manual
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Sh NAME
8273327
.Nm dhcpctl_initialize
8273327
.Nd dhcpctl library initialization.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Sh SYNOPSIS
8273327
.Fd #include <dhcpctl.h>
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_initialize
8273327
.Fa void
8273327
.Fc
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_connect
8273327
.Fa "dhcpctl_handle *cxn"
8273327
.Fa "const char *host"
8273327
.Fa "int port"
8273327
.Fa "dhcpctl_handle auth"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_wait_for_completion
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "dhcpctl_status *status"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_get_value
8273327
.Fa "dhcpctl_data_string *value"
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_get_boolean
8273327
.Fa "int *value"
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_set_value
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "dhcpctl_data_string value"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_set_string_value
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "const char *value"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_set_boolean_value
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "int value"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_set_int_value
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "int value"
8273327
.Fa "const char *name"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_object_update
8273327
.Fa "dhcpctl_handle connection"
8273327
.Fa "dhcpctl_handle object"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_object_refresh
8273327
.Fa "dhcpctl_handle connection"
8273327
.Fa "dhcpctl_handle object"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_object_remove
8273327
.Fa "dhcpctl_handle connection"
8273327
.Fa "dhcpctl_handle object"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_set_callback
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "void *data"
8273327
.Fa "void (*function) (dhcpctl_handle, dhcpctl_status, void *)"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_new_authenticator
8273327
.Fa "dhcpctl_handle *object"
8273327
.Fa "const char *name"
8273327
.Fa "const char *algorithm"
8273327
.Fa "const char *secret"
8273327
.Fa "unsigned secret_len"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_new_object
8273327
.Fa "dhcpctl_handle *object"
8273327
.Fa "dhcpctl_handle connection"
8273327
.Fa "const char *object_type"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft dhcpctl_status
8273327
.Fo dhcpctl_open_object
8273327
.Fa "dhcpctl_handle object"
8273327
.Fa "dhcpctl_handle connection"
8273327
.Fa "int flags"
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft isc_result_t
8273327
.Fo omapi_data_string_new
8273327
.Fa dhcpctl_data_string *data
8273327
.Fa unsigned int length
8273327
.Fa const char *filename,
8273327
.Fa int lineno
8273327
.Fc
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Ft isc_result_t
8273327
.Fo dhcpctl_data_string_dereference
8273327
.Fa "dhcpctl_data_string *"
8273327
.Fa "const char *"
8273327
.Fa "int"
8273327
.Fc
8273327
.Sh DESCRIPTION
8273327
The dhcpctl set of functions provide an API that can be used to communicate
8273327
with and manipulate a running ISC DHCP server. All functions return a value of
8273327
.Dv isc_result_t .
8273327
The return values reflects the result of operations to local data
8273327
structures. If an operation fails on the server for any reason, then the error
8273327
result will be returned through the
8273327
second parameter of the 
8273327
.Fn dhcpctl_wait_for_completion 
8273327
call.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_initialize
8273327
sets up the data structures the library needs to do its work. This function
8273327
must be called once before any other.
8273327
.Pp
8273327
.Fn dhcpctl_connect
8273327
opens a connection to the DHCP server at the given host and port. If an
8273327
authenticator has been created for the connection, then it is given as the 4th
8273327
argument. On a successful return the address pointed at by the first
8273327
argument will have a new connection object assigned to it.
8273327
.Pp
8273327
For example:
8273327
.Bd -literal -offset indent
8273327
s = dhcpctl_connect(&cxn, "127.0.0.1", 7911, NULL);
8273327
.Ed
8273327
.Pp
8273327
connects to the DHCP server on the localhost via port 7911 (the standard
8273327
OMAPI port). No authentication is used for the connection.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_wait_for_completion
8273327
flushes a pending message to the server and waits for the response. The result
8273327
of the request as processed on the server is returned via the second
8273327
parameter.
8273327
.Bd -literal -offset indent
8273327
s = dhcpctl_wait_for_completion(cxn, &wv;;
8273327
if (s != ISC_R_SUCCESS) 
8273327
	local_failure(s);
8273327
else if (wv != ISC_R_SUCCESS)
8273327
	server_failure(wc);
8273327
.Ed
8273327
.Pp
8273327
The call to 
8273327
.Fn dhcpctl_wait_for_completion
8273327
won't return until the remote message processing completes or the connection
8273327
to the server is lost.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_get_value
8273327
extracts a value of an attribute from the handle. The value can be of any
8273327
length and is treated as a sequence of bytes.  The handle must have been
8273327
created first with
8273327
.Fn dhcpctl_new_object
8273327
and opened with
8273327
.Fn dhcpctl_open_object .
8273327
The value is returned via the parameter named
8273327
.Dq value .
8273327
The last parameter is the name of attribute to retrieve.
8273327
.Bd -literal -offset indent
8273327
dhcpctl_data_string value = NULL;
8273327
dhcpctl_handle lease;
8273327
time_t thetime;
8273327
8273327
s = dhcpctl_get_value (&value, lease, "ends");
8273327
assert(s == ISC_R_SUCCESS && value->len == sizeof(thetime));
8273327
memcpy(&thetime, value->value, value->len);
8273327
.Ed
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_get_boolean
8273327
extracts a boolean valued attribute from the object handle.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
The
8273327
.Fn dhcpctl_set_value ,
8273327
.Fn dhcpctl_set_string_value ,
8273327
.Fn dhcpctl_set_boolean_value ,
8273327
and
8273327
.Fn dhcpctl_set_int_value
8273327
functions all set a value on the object handle. 
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_object_update
8273327
function queues a request for
8273327
all the changes made to the object handle be be sent to the remote
8273327
for processing. The changes made to the atributes on the handle will be
8273327
applied to remote object if permitted.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_object_refresh
8273327
queues up a request for a fresh copy of all the attribute values to be sent
8273327
from the remote to
8273327
refresh the values in the local object handle.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_object_remove
8273327
queues a request for the removal on the server of the object referenced by the
8273327
handle.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
The 
8273327
.Fn dhcpctl_set_callback
8273327
function sets up a user-defined function to be called when an event completes
8273327
on the given object handle. This is needed for asynchronous handling of
8273327
events, versus the synchronous handling given by
8273327
.Fn dhcpctl_wait_for_completion .
8273327
When the function is called the first parameter is the object the event
8273327
arrived for, the second is the status of the message that was processed, the
8273327
third is the same value as the second parameter given to 
8273327
.Fn dhcpctl_set_callback .
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
The 
8273327
.Fn dhcpctl_new_authenticator
8273327
creates a new authenticator object to be used for signing the messages
8273327
that cross over the network. The 
8273327
.Dq name ,
8273327
.Dq algorithm ,
8273327
and 
8273327
.Dq secret
8273327
values must all match what the server uses and are defined in its
8273327
configuration file. The created object is returned through the first parameter
8273327
and must be used as the 4th parameter to 
8273327
.Fn dhcpctl_connect .
8273327
Note that the 'secret' value must not be base64 encoded, which is different
8273327
from how the value appears in the dhcpd.conf file.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_new_object
8273327
creates a local handle for an object on the the server. The 
8273327
.Dq object_type
8273327
parameter is the ascii name of the type of object being accessed. e.g. 
8273327
.Qq lease .
8273327
This function only sets up local data structures, it does not queue any 
8273327
messages
8273327
to be sent to the remote side,
8273327
.Fn dhcpctl_open_object
8273327
does that.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_open_object
8273327
builds and queues the request to the remote side. This function is used with
8273327
handle created via
8273327
.Fn dhcpctl_new_object .
8273327
The flags argument is a bit mask with the following values available for
8273327
setting:
8273327
.Bl -tag -offset indent -width 20
8273327
.It DHCPCTL_CREATE
8273327
if the object does not exist then the remote will create it
8273327
.It DHCPCTL_UPDATE
8273327
update the object on the remote side using the
8273327
attributes already set in the handle.
8273327
.It DHCPCTL_EXCL
8273327
return and error if the object exists and DHCPCTL_CREATE
8273327
was also specified
8273327
.El
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
The 
8273327
.Fn omapi_data_string_new
8273327
function allocates a new
8273327
.Ft dhcpctl_data_string
8273327
object. The data string will be large enough to hold 
8273327
.Dq length
8273327
bytes of data. The
8273327
.Dq file 
8273327
and
8273327
.Dq lineno
8273327
arguments are the source file location the call is made from, typically by
8273327
using the 
8273327
.Dv __FILE__
8273327
and
8273327
.Dv __LINE__
8273327
macros or the 
8273327
.Dv MDL
8273327
macro defined in
8273327
.
8273327
.\"
8273327
.\"
8273327
.\"
8273327
.Pp
8273327
.Fn dhcpctl_data_string_dereference
8273327
deallocates a data string created by
8273327
.Fn omapi_data_string_new .
8273327
The memory for the object won't be freed until the last reference is
8273327
released.
8273327
.Sh EXAMPLES
8273327
.Pp 
8273327
The following program will connect to the DHCP server running on the local
8273327
host and will get the details of the existing lease for IP address
8273327
10.0.0.101. It will then print out the time the lease is due to expire. Note
8273327
that most error checking has been ommitted for brevity.
8273327
.Bd -literal -offset indent
8273327
#include <stdarg.h>
8273327
#include <sys/time.h>
8273327
#include <sys/socket.h>
8273327
#include <stdio.h>
8273327
#include <netinet/in.h>
8273327
8273327
#include <isc/result.h>
8273327
#include <dhcpctl.h>
8273327
8273327
int main (int argc, char **argv) {
8273327
	dhcpctl_data_string ipaddrstring = NULL;
8273327
	dhcpctl_data_string value = NULL;
8273327
	dhcpctl_handle connection = NULL;
8273327
	dhcpctl_handle lease = NULL;
8273327
	isc_result_t waitstatus;
8273327
	struct in_addr convaddr;
8273327
	time_t thetime;
8273327
8273327
        dhcpctl_initialize ();
8273327
8273327
        dhcpctl_connect (&connection, "127.0.0.1",
8273327
			 7911, 0);
8273327
	
8273327
        dhcpctl_new_object (&lease, connection,
8273327
			    "lease");
8273327
8273327
        memset (&ipaddrstring, 0, sizeof
8273327
		ipaddrstring);
8273327
8273327
        inet_pton(AF_INET, "10.0.0.101",
8273327
		  &convaddr);
8273327
8273327
	omapi_data_string_new (&ipaddrstring,
8273327
			       4, MDL);
8273327
	memcpy(ipaddrstring->value, &convaddr.s_addr, 4);
8273327
8273327
	dhcpctl_set_value (lease, ipaddrstring,
8273327
			   "ip-address");
8273327
8273327
	dhcpctl_open_object (lease, connection, 0);
8273327
8273327
	dhcpctl_wait_for_completion (lease,
8273327
				     &waitstatus);
8273327
        if (waitstatus != ISC_R_SUCCESS) {
8273327
		/* server not authoritative */
8273327
		exit (0);
8273327
        }
8273327
8273327
	dhcpctl_data_string_dereference(&ipaddrstring,
8273327
					MDL);
8273327
8273327
        dhcpctl_get_value (&value, lease, "ends");
8273327
8273327
	memcpy(&thetime, value->value, value->len);
8273327
8273327
	dhcpctl_data_string_dereference(&value, MDL);
8273327
8273327
	fprintf (stdout, "ending time is %s",
8273327
		 ctime(&thetime));
8273327
}
8273327
.Ed
8273327
.Sh SEE ALSO
8273327
omapi(3), omshell(3), dhcpd(8), dhclient(8), dhcpd.conf(5), dhclient.conf(5).
8273327
.Sh AUTHOR
8273327
.Em dhcpctl
8273327
was written by Ted Lemon of Nominum, Inc.
8273327
This preliminary documentation was written by James Brister of Nominum, Inc.