3 Example program that uses the dhcpctl library. */
6 * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2000-2003 by Internet Software Consortium
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
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 * Newmarket, NH 03857 USA
25 * https://www.isc.org/
27 * This software was contributed to Internet Systems Consortium
39 #include "omapip/result.h"
44 isc_result_t
find_class (struct class **c
, const char *n
, const char *f
, int l
)
48 int parse_allow_deny (struct option_cache
**oc
, struct parse
*cfile
, int flag
)
52 void dhcp (struct packet
*packet
) { }
53 void bootp (struct packet
*packet
) { }
56 /* XXX: should we warn or something here? */
57 void dhcpv6(struct packet
*packet
) { }
59 isc_result_t
dhcpv4o6_handler(omapi_object_t
*h
)
61 return ISC_R_NOTIMPLEMENTED
;
66 int check_collection (struct packet
*p
, struct lease
*l
, struct collection
*c
)
70 void classify (struct packet
*packet
, struct class *class) { }
72 isc_result_t
dhcp_set_control_state (control_object_state_t oldstate
,
73 control_object_state_t newstate
)
78 int main (int, char **);
80 enum modes
{ up
, down
, undefined
};
82 static void usage (char *s
) {
84 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
85 "(-u | -d) <if>\n", s
);
93 isc_result_t status
, waitstatus
;
94 dhcpctl_handle authenticator
;
95 dhcpctl_handle connection
;
96 dhcpctl_handle interface_handle
;
97 dhcpctl_data_string result
;
100 const char *interface
= 0;
103 for (i
= 1; i
< argc
; i
++) {
104 if (!strcmp (argv
[i
], "-u")) {
106 } else if (!strcmp (argv
[i
], "-d")) {
108 } else if (argv
[i
][0] == '-') {
117 if (mode
== undefined
)
120 status
= dhcpctl_initialize ();
121 if (status
!= ISC_R_SUCCESS
) {
122 fprintf (stderr
, "dhcpctl_initialize: %s\n",
123 isc_result_totext (status
));
127 authenticator
= dhcpctl_null_handle
;
128 connection
= dhcpctl_null_handle
;
130 status
= dhcpctl_connect (&connection
, "127.0.0.1", 7911,
132 if (status
!= ISC_R_SUCCESS
) {
133 fprintf (stderr
, "dhcpctl_connect: %s\n",
134 isc_result_totext (status
));
138 interface_handle
= dhcpctl_null_handle
;
139 status
= dhcpctl_new_object (&interface_handle
,
140 connection
, "interface");
141 if (status
!= ISC_R_SUCCESS
) {
142 fprintf (stderr
, "dhcpctl_new_object: %s\n",
143 isc_result_totext (status
));
147 status
= dhcpctl_set_string_value (interface_handle
,
149 if (status
!= ISC_R_SUCCESS
) {
150 fprintf (stderr
, "dhcpctl_set_value: %s\n",
151 isc_result_totext (status
));
156 /* "up" the interface */
157 printf ("upping interface %s\n", interface
);
159 status
= dhcpctl_open_object (interface_handle
, connection
,
160 DHCPCTL_CREATE
| DHCPCTL_EXCL
);
161 if (status
!= ISC_R_SUCCESS
) {
162 fprintf (stderr
, "dhcpctl_open_object: %s\n",
163 isc_result_totext (status
));
167 /* down the interface */
168 printf ("downing interface %s\n", interface
);
170 status
= dhcpctl_open_object (interface_handle
, connection
, 0);
171 if (status
!= ISC_R_SUCCESS
) {
172 fprintf (stderr
, "dhcpctl_open_object: %s\n",
173 isc_result_totext (status
));
176 status
= dhcpctl_wait_for_completion (interface_handle
,
178 if (status
!= ISC_R_SUCCESS
) {
179 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
180 isc_result_totext (status
));
183 if (waitstatus
!= ISC_R_SUCCESS
) {
184 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
185 isc_result_totext (waitstatus
));
188 status
= dhcpctl_object_remove (connection
, interface_handle
);
189 if (status
!= ISC_R_SUCCESS
) {
190 fprintf (stderr
, "dhcpctl_open_object: %s\n",
191 isc_result_totext (status
));
196 status
= dhcpctl_wait_for_completion (interface_handle
, &waitstatus
);
197 if (status
!= ISC_R_SUCCESS
) {
198 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
199 isc_result_totext (status
));
202 if (waitstatus
!= ISC_R_SUCCESS
) {
203 fprintf (stderr
, "interface object %s: %s\n", action
,
204 isc_result_totext (waitstatus
));
208 memset (&result
, 0, sizeof result
);
209 status
= dhcpctl_get_value (&result
, interface_handle
, "state");
210 if (status
!= ISC_R_SUCCESS
) {
211 fprintf (stderr
, "dhcpctl_get_value: %s\n",
212 isc_result_totext (status
));