3 Example program that uses the dhcpctl library. */
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2000-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
27 * This software was contributed to Internet Systems Consortium
32 static char ocopyright
[] =
33 "$Id: cltest.c,v 1.3 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
42 #include <isc-dhcp/result.h>
45 int main (int, char **);
47 enum modes
{ up
, down
, undefined
};
49 static void usage (char *s
) {
51 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
52 "(-u | -d) <if>\n", s
);
60 isc_result_t status
, waitstatus
;
61 dhcpctl_handle authenticator
;
62 dhcpctl_handle connection
;
63 dhcpctl_handle host_handle
, group_handle
, interface_handle
;
64 dhcpctl_data_string cid
;
65 dhcpctl_data_string result
, groupname
, identifier
;
68 const char *interface
= 0;
71 for (i
= 1; i
< argc
; i
++) {
72 if (!strcmp (argv
[i
], "-u")) {
74 } else if (!strcmp (argv
[i
], "-d")) {
76 } else if (argv
[i
][0] == '-') {
85 if (mode
== undefined
)
88 status
= dhcpctl_initialize ();
89 if (status
!= ISC_R_SUCCESS
) {
90 fprintf (stderr
, "dhcpctl_initialize: %s\n",
91 isc_result_totext (status
));
95 authenticator
= dhcpctl_null_handle
;
96 connection
= dhcpctl_null_handle
;
98 status
= dhcpctl_connect (&connection
, "127.0.0.1", 7911,
100 if (status
!= ISC_R_SUCCESS
) {
101 fprintf (stderr
, "dhcpctl_connect: %s\n",
102 isc_result_totext (status
));
106 interface_handle
= dhcpctl_null_handle
;
107 status
= dhcpctl_new_object (&interface_handle
,
108 connection
, "interface");
109 if (status
!= ISC_R_SUCCESS
) {
110 fprintf (stderr
, "dhcpctl_new_object: %s\n",
111 isc_result_totext (status
));
115 status
= dhcpctl_set_string_value (interface_handle
,
117 if (status
!= ISC_R_SUCCESS
) {
118 fprintf (stderr
, "dhcpctl_set_value: %s\n",
119 isc_result_totext (status
));
124 /* "up" the interface */
125 printf ("upping interface %s\n", interface
);
127 status
= dhcpctl_open_object (interface_handle
, connection
,
128 DHCPCTL_CREATE
| DHCPCTL_EXCL
);
129 if (status
!= ISC_R_SUCCESS
) {
130 fprintf (stderr
, "dhcpctl_open_object: %s\n",
131 isc_result_totext (status
));
135 /* down the interface */
136 printf ("downing interface %s\n", interface
);
138 status
= dhcpctl_open_object (interface_handle
, connection
, 0);
139 if (status
!= ISC_R_SUCCESS
) {
140 fprintf (stderr
, "dhcpctl_open_object: %s\n",
141 isc_result_totext (status
));
144 status
= dhcpctl_wait_for_completion (interface_handle
,
146 if (status
!= ISC_R_SUCCESS
) {
147 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
148 isc_result_totext (status
));
151 if (waitstatus
!= ISC_R_SUCCESS
) {
152 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
153 isc_result_totext (waitstatus
));
156 status
= dhcpctl_object_remove (connection
, interface_handle
);
157 if (status
!= ISC_R_SUCCESS
) {
158 fprintf (stderr
, "dhcpctl_open_object: %s\n",
159 isc_result_totext (status
));
164 status
= dhcpctl_wait_for_completion (interface_handle
, &waitstatus
);
165 if (status
!= ISC_R_SUCCESS
) {
166 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
167 isc_result_totext (status
));
170 if (waitstatus
!= ISC_R_SUCCESS
) {
171 fprintf (stderr
, "interface object %s: %s\n", action
,
172 isc_result_totext (waitstatus
));
176 memset (&result
, 0, sizeof result
);
177 status
= dhcpctl_get_value (&result
, interface_handle
, "state");
178 if (status
!= ISC_R_SUCCESS
) {
179 fprintf (stderr
, "dhcpctl_get_value: %s\n",
180 isc_result_totext (status
));