]> git.ipfire.org Git - thirdparty/dhcp.git/blob - dhcpctl/cltest.c
[master] Update auto files as part of moving docs systems
[thirdparty/dhcp.git] / dhcpctl / cltest.c
1 /* cltest.c
2
3 Example program that uses the dhcpctl library. */
4
5 /*
6 * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2000-2003 by Internet Software Consortium
8 *
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.
12 *
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.
20 *
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * https://www.isc.org/
26 *
27 * This software was contributed to Internet Systems Consortium
28 * by Brian Murrell.
29 */
30
31 #include "config.h"
32
33 #include <time.h>
34 #include <sys/time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include "omapip/result.h"
40 #include "dhcpctl.h"
41
42 int main (int, char **);
43
44 enum modes { up, down, undefined };
45
46 static void usage (char *s) {
47 fprintf (stderr,
48 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
49 "(-u | -d) <if>\n", s);
50 exit (1);
51 }
52
53 int main (argc, argv)
54 int argc;
55 char **argv;
56 {
57 isc_result_t status, waitstatus;
58 dhcpctl_handle authenticator;
59 dhcpctl_handle connection;
60 dhcpctl_handle interface_handle;
61 dhcpctl_data_string result;
62 int i;
63 int mode = undefined;
64 const char *interface = 0;
65 const char *action;
66
67 for (i = 1; i < argc; i++) {
68 if (!strcmp (argv[i], "-u")) {
69 mode = up;
70 } else if (!strcmp (argv [i], "-d")) {
71 mode = down;
72 } else if (argv[i][0] == '-') {
73 usage(argv[0]);
74 } else {
75 interface = argv[i];
76 }
77 }
78
79 if (!interface)
80 usage(argv[0]);
81 if (mode == undefined)
82 usage(argv[0]);
83
84 status = dhcpctl_initialize ();
85 if (status != ISC_R_SUCCESS) {
86 fprintf (stderr, "dhcpctl_initialize: %s\n",
87 isc_result_totext (status));
88 exit (1);
89 }
90
91 authenticator = dhcpctl_null_handle;
92 connection = dhcpctl_null_handle;
93
94 status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
95 authenticator);
96 if (status != ISC_R_SUCCESS) {
97 fprintf (stderr, "dhcpctl_connect: %s\n",
98 isc_result_totext (status));
99 exit (1);
100 }
101
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));
108 exit (1);
109 }
110
111 status = dhcpctl_set_string_value (interface_handle,
112 interface, "name");
113 if (status != ISC_R_SUCCESS) {
114 fprintf (stderr, "dhcpctl_set_value: %s\n",
115 isc_result_totext (status));
116 exit (1);
117 }
118
119 if (mode == up) {
120 /* "up" the interface */
121 printf ("upping interface %s\n", interface);
122 action = "create";
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));
128 exit (1);
129 }
130 } else {
131 /* down the interface */
132 printf ("downing interface %s\n", interface);
133 action = "remove";
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));
138 exit (1);
139 }
140 status = dhcpctl_wait_for_completion (interface_handle,
141 &waitstatus);
142 if (status != ISC_R_SUCCESS) {
143 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
144 isc_result_totext (status));
145 exit (1);
146 }
147 if (waitstatus != ISC_R_SUCCESS) {
148 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
149 isc_result_totext (waitstatus));
150 exit (1);
151 }
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));
156 exit (1);
157 }
158 }
159
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));
164 exit (1);
165 }
166 if (waitstatus != ISC_R_SUCCESS) {
167 fprintf (stderr, "interface object %s: %s\n", action,
168 isc_result_totext (waitstatus));
169 exit (1);
170 }
171
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));
177 exit (1);
178 }
179
180 exit (0);
181 }