1 /* $NetBSD: cltest.c,v 1.1.1.3 2014/07/12 11:57:51 spz Exp $ */
4 Example program that uses the dhcpctl library. */
7 * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 2000-2003 by Internet Software Consortium
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Internet Systems Consortium, Inc.
24 * Redwood City, CA 94063
26 * https://www.isc.org/
28 * This software was contributed to Internet Systems Consortium
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: cltest.c,v 1.1.1.3 2014/07/12 11:57:51 spz Exp $");
43 #include "omapip/result.h"
46 int main (int, char **);
48 enum modes
{ up
, down
, undefined
};
50 static void usage (char *s
) {
52 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
53 "(-u | -d) <if>\n", s
);
61 isc_result_t status
, waitstatus
;
62 dhcpctl_handle authenticator
;
63 dhcpctl_handle connection
;
64 dhcpctl_handle interface_handle
;
65 dhcpctl_data_string result
;
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
));