3 Example program that uses the dhcpctl library. */
6 * Copyright (c) 2004,2007,2009 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
25 * https://www.isc.org/
27 * This software was contributed to Internet Systems Consortium
39 #include "omapip/result.h"
42 int main (int, char **);
44 enum modes
{ up
, down
, undefined
};
46 static void usage (char *s
) {
48 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
49 "(-u | -d) <if>\n", s
);
57 isc_result_t status
, waitstatus
;
58 dhcpctl_handle authenticator
;
59 dhcpctl_handle connection
;
60 dhcpctl_handle interface_handle
;
61 dhcpctl_data_string result
;
64 const char *interface
= 0;
67 for (i
= 1; i
< argc
; i
++) {
68 if (!strcmp (argv
[i
], "-u")) {
70 } else if (!strcmp (argv
[i
], "-d")) {
72 } else if (argv
[i
][0] == '-') {
81 if (mode
== undefined
)
84 status
= dhcpctl_initialize ();
85 if (status
!= ISC_R_SUCCESS
) {
86 fprintf (stderr
, "dhcpctl_initialize: %s\n",
87 isc_result_totext (status
));
91 authenticator
= dhcpctl_null_handle
;
92 connection
= dhcpctl_null_handle
;
94 status
= dhcpctl_connect (&connection
, "127.0.0.1", 7911,
96 if (status
!= ISC_R_SUCCESS
) {
97 fprintf (stderr
, "dhcpctl_connect: %s\n",
98 isc_result_totext (status
));
102 interface_handle
= dhcpctl_null_handle
;
103 status
= dhcpctl_new_object (&interface_handle
,
104 connection
, "interface");
105 if (status
!= ISC_R_SUCCESS
) {
106 fprintf (stderr
, "dhcpctl_new_object: %s\n",
107 isc_result_totext (status
));
111 status
= dhcpctl_set_string_value (interface_handle
,
113 if (status
!= ISC_R_SUCCESS
) {
114 fprintf (stderr
, "dhcpctl_set_value: %s\n",
115 isc_result_totext (status
));
120 /* "up" the interface */
121 printf ("upping interface %s\n", interface
);
123 status
= dhcpctl_open_object (interface_handle
, connection
,
124 DHCPCTL_CREATE
| DHCPCTL_EXCL
);
125 if (status
!= ISC_R_SUCCESS
) {
126 fprintf (stderr
, "dhcpctl_open_object: %s\n",
127 isc_result_totext (status
));
131 /* down the interface */
132 printf ("downing interface %s\n", interface
);
134 status
= dhcpctl_open_object (interface_handle
, connection
, 0);
135 if (status
!= ISC_R_SUCCESS
) {
136 fprintf (stderr
, "dhcpctl_open_object: %s\n",
137 isc_result_totext (status
));
140 status
= dhcpctl_wait_for_completion (interface_handle
,
142 if (status
!= ISC_R_SUCCESS
) {
143 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
144 isc_result_totext (status
));
147 if (waitstatus
!= ISC_R_SUCCESS
) {
148 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
149 isc_result_totext (waitstatus
));
152 status
= dhcpctl_object_remove (connection
, interface_handle
);
153 if (status
!= ISC_R_SUCCESS
) {
154 fprintf (stderr
, "dhcpctl_open_object: %s\n",
155 isc_result_totext (status
));
160 status
= dhcpctl_wait_for_completion (interface_handle
, &waitstatus
);
161 if (status
!= ISC_R_SUCCESS
) {
162 fprintf (stderr
, "dhcpctl_wait_for_completion: %s\n",
163 isc_result_totext (status
));
166 if (waitstatus
!= ISC_R_SUCCESS
) {
167 fprintf (stderr
, "interface object %s: %s\n", action
,
168 isc_result_totext (waitstatus
));
172 memset (&result
, 0, sizeof result
);
173 status
= dhcpctl_get_value (&result
, interface_handle
, "state");
174 if (status
!= ISC_R_SUCCESS
) {
175 fprintf (stderr
, "dhcpctl_get_value: %s\n",
176 isc_result_totext (status
));