3 Example program that uses the dhcpctl library. */
5 /* Copyright (C) 2000-2002 Internet Software Consortium
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of The Internet Software Consortium nor the names
18 * of its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This software was contributed to the Internet Software Consortium
45 #include <isc-dhcp/result.h>
48 int main (int, char **);
50 enum modes { up, down, undefined };
52 static void usage (char *s) {
54 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
55 "(-u | -d) <if>\n", s);
63 isc_result_t status, waitstatus;
64 dhcpctl_handle authenticator;
65 dhcpctl_handle connection;
66 dhcpctl_handle host_handle, group_handle, interface_handle;
67 dhcpctl_data_string cid;
68 dhcpctl_data_string result, groupname, identifier;
71 const char *interface = 0;
74 for (i = 1; i < argc; i++) {
75 if (!strcmp (argv[i], "-u")) {
77 } else if (!strcmp (argv [i], "-d")) {
79 } else if (argv[i][0] == '-') {
88 if (mode == undefined)
91 status = dhcpctl_initialize ();
92 if (status != ISC_R_SUCCESS) {
93 fprintf (stderr, "dhcpctl_initialize: %s\n",
94 isc_result_totext (status));
98 authenticator = dhcpctl_null_handle;
99 connection = dhcpctl_null_handle;
101 status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
103 if (status != ISC_R_SUCCESS) {
104 fprintf (stderr, "dhcpctl_connect: %s\n",
105 isc_result_totext (status));
109 interface_handle = dhcpctl_null_handle;
110 status = dhcpctl_new_object (&interface_handle,
111 connection, "interface");
112 if (status != ISC_R_SUCCESS) {
113 fprintf (stderr, "dhcpctl_new_object: %s\n",
114 isc_result_totext (status));
118 status = dhcpctl_set_string_value (interface_handle,
120 if (status != ISC_R_SUCCESS) {
121 fprintf (stderr, "dhcpctl_set_value: %s\n",
122 isc_result_totext (status));
127 /* "up" the interface */
128 printf ("upping interface %s\n", interface);
130 status = dhcpctl_open_object (interface_handle, connection,
131 DHCPCTL_CREATE | DHCPCTL_EXCL);
132 if (status != ISC_R_SUCCESS) {
133 fprintf (stderr, "dhcpctl_open_object: %s\n",
134 isc_result_totext (status));
138 /* down the interface */
139 printf ("downing interface %s\n", interface);
141 status = dhcpctl_open_object (interface_handle, connection, 0);
142 if (status != ISC_R_SUCCESS) {
143 fprintf (stderr, "dhcpctl_open_object: %s\n",
144 isc_result_totext (status));
147 status = dhcpctl_wait_for_completion (interface_handle,
149 if (status != ISC_R_SUCCESS) {
150 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
151 isc_result_totext (status));
154 if (waitstatus != ISC_R_SUCCESS) {
155 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
156 isc_result_totext (waitstatus));
159 status = dhcpctl_object_remove (connection, interface_handle);
160 if (status != ISC_R_SUCCESS) {
161 fprintf (stderr, "dhcpctl_open_object: %s\n",
162 isc_result_totext (status));
167 status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
168 if (status != ISC_R_SUCCESS) {
169 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
170 isc_result_totext (status));
173 if (waitstatus != ISC_R_SUCCESS) {
174 fprintf (stderr, "interface object %s: %s\n", action,
175 isc_result_totext (waitstatus));
179 memset (&result, 0, sizeof result);
180 status = dhcpctl_get_value (&result, interface_handle, "state");
181 if (status != ISC_R_SUCCESS) {
182 fprintf (stderr, "dhcpctl_get_value: %s\n",
183 isc_result_totext (status));