Initial import from FreeBSD RELENG_4:
[games.git] / contrib / isc-dhcp / dhcpctl / cltest.c
1 /* cltest.c
2
3    Example program that uses the dhcpctl library. */
4
5 /* Copyright (C) 2000-2002 Internet Software Consortium
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
20  *
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
33  * SUCH DAMAGE.
34  *
35  * This software was contributed to the Internet Software Consortium
36  * by Brian Murrell.
37  */
38
39 #include <time.h>
40 #include <sys/time.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <isc-dhcp/result.h>
46 #include "dhcpctl.h"
47
48 int main (int, char **);
49
50 enum modes { up, down, undefined };
51
52 static void usage (char *s) {
53         fprintf (stderr,
54                  "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
55                  "(-u | -d) <if>\n", s);
56         exit (1);
57 }
58
59 int main (argc, argv)
60         int argc;
61         char **argv;
62 {
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;
69         int i;
70         int mode = undefined;
71         const char *interface = 0;
72         const char *action;
73         
74         for (i = 1; i < argc; i++) {
75                 if (!strcmp (argv[i], "-u")) {
76                         mode = up;
77                 } else if (!strcmp (argv [i], "-d")) {
78                         mode = down;
79                 } else if (argv[i][0] == '-') {
80                         usage(argv[0]);
81                 } else {
82                         interface = argv[i];
83                 }
84         }
85
86         if (!interface)
87                 usage(argv[0]);
88         if (mode == undefined)
89                 usage(argv[0]);
90
91         status = dhcpctl_initialize ();
92         if (status != ISC_R_SUCCESS) {
93                 fprintf (stderr, "dhcpctl_initialize: %s\n",
94                          isc_result_totext (status));
95                 exit (1);
96         }
97
98         authenticator = dhcpctl_null_handle;
99         connection = dhcpctl_null_handle;
100
101         status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
102                                   authenticator);
103         if (status != ISC_R_SUCCESS) {
104                 fprintf (stderr, "dhcpctl_connect: %s\n",
105                          isc_result_totext (status));
106                 exit (1);
107         }
108
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));
115                 exit (1);
116         }
117
118         status = dhcpctl_set_string_value (interface_handle,
119                                            interface, "name");
120         if (status != ISC_R_SUCCESS) {
121                 fprintf (stderr, "dhcpctl_set_value: %s\n",
122                          isc_result_totext (status));
123                 exit (1);
124         }
125
126         if (mode == up) {
127                 /* "up" the interface */
128                 printf ("upping interface %s\n", interface);
129                 action = "create";
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));
135                         exit (1);
136                 }
137         } else {
138                 /* down the interface */
139                 printf ("downing interface %s\n", interface);
140                 action = "remove";
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));
145                         exit (1);
146                 }
147                 status = dhcpctl_wait_for_completion (interface_handle,
148                                                       &waitstatus);
149                 if (status != ISC_R_SUCCESS) {
150                         fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
151                                  isc_result_totext (status));
152                         exit (1);
153                 }
154                 if (waitstatus != ISC_R_SUCCESS) {
155                         fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
156                                  isc_result_totext (waitstatus));
157                         exit (1);
158                 }
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));
163                         exit (1);
164                 }
165         }
166
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));
171                 exit (1);
172         }
173         if (waitstatus != ISC_R_SUCCESS) {
174                 fprintf (stderr, "interface object %s: %s\n", action,
175                          isc_result_totext (waitstatus));
176                 exit (1);
177         }
178
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));
184                 exit (1);
185         }
186
187         exit (0);
188 }