]>
Commit | Line | Data |
---|---|---|
347de8bd TL |
1 | /* cltest.c |
2 | ||
3 | Example program that uses the dhcpctl library. */ | |
4 | ||
98311e4b | 5 | /* |
a47d6336 | 6 | * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC") |
98311e4b | 7 | * Copyright (c) 2000-2003 by Internet Software Consortium |
49733f31 | 8 | * |
98311e4b DH |
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. | |
49733f31 | 12 | * |
98311e4b DH |
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. | |
49733f31 | 20 | * |
98311e4b DH |
21 | * Internet Systems Consortium, Inc. |
22 | * 950 Charter Street | |
23 | * Redwood City, CA 94063 | |
24 | * <info@isc.org> | |
2c85ac9b | 25 | * https://www.isc.org/ |
49733f31 | 26 | * |
98311e4b | 27 | * This software was contributed to Internet Systems Consortium |
49733f31 TL |
28 | * by Brian Murrell. |
29 | */ | |
30 | ||
74d67492 MA |
31 | #include "config.h" |
32 | ||
347de8bd | 33 | #include <time.h> |
0f6bb7e1 | 34 | #include <sys/time.h> |
347de8bd TL |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
06f0ed06 | 37 | #include <string.h> |
347de8bd | 38 | #include <stdarg.h> |
98bf1607 | 39 | #include "omapip/result.h" |
347de8bd | 40 | #include "dhcpctl.h" |
bf6f8fce FD |
41 | #include "dhcpd.h" |
42 | ||
43 | /* Fixups */ | |
44 | isc_result_t find_class (struct class **c, const char *n, const char *f, int l) | |
45 | { | |
46 | return 0; | |
47 | } | |
48 | int parse_allow_deny (struct option_cache **oc, struct parse *cfile, int flag) | |
49 | { | |
50 | return 0; | |
51 | } | |
52 | void dhcp (struct packet *packet) { } | |
53 | void bootp (struct packet *packet) { } | |
54 | ||
55 | #ifdef DHCPv6 | |
56 | /* XXX: should we warn or something here? */ | |
57 | void dhcpv6(struct packet *packet) { } | |
e4dcd805 FD |
58 | #ifdef DHCP4o6 |
59 | isc_result_t dhcpv4o6_handler(omapi_object_t *h) | |
60 | { | |
61 | return ISC_R_NOTIMPLEMENTED; | |
62 | } | |
63 | #endif /* DHCP4o6 */ | |
bf6f8fce FD |
64 | #endif /* DHCPv6 */ |
65 | ||
66 | int check_collection (struct packet *p, struct lease *l, struct collection *c) | |
67 | { | |
68 | return 0; | |
69 | } | |
70 | void classify (struct packet *packet, struct class *class) { } | |
71 | ||
72 | isc_result_t dhcp_set_control_state (control_object_state_t oldstate, | |
73 | control_object_state_t newstate) | |
74 | { | |
75 | return ISC_R_SUCCESS; | |
76 | } | |
347de8bd TL |
77 | |
78 | int main (int, char **); | |
79 | ||
49146f3c DN |
80 | enum modes { up, down, undefined }; |
81 | ||
82 | static void usage (char *s) { | |
83 | fprintf (stderr, | |
84 | "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]" | |
85 | "(-u | -d) <if>\n", s); | |
86 | exit (1); | |
87 | } | |
bdcaf7b9 | 88 | |
347de8bd TL |
89 | int main (argc, argv) |
90 | int argc; | |
91 | char **argv; | |
92 | { | |
93 | isc_result_t status, waitstatus; | |
49146f3c | 94 | dhcpctl_handle authenticator; |
347de8bd | 95 | dhcpctl_handle connection; |
28868515 SK |
96 | dhcpctl_handle interface_handle; |
97 | dhcpctl_data_string result; | |
347de8bd | 98 | int i; |
49146f3c | 99 | int mode = undefined; |
2cd44219 | 100 | const char *interface = 0; |
5e4477a9 | 101 | const char *action; |
49146f3c DN |
102 | |
103 | for (i = 1; i < argc; i++) { | |
104 | if (!strcmp (argv[i], "-u")) { | |
105 | mode = up; | |
f0b827ef | 106 | } else if (!strcmp (argv [i], "-d")) { |
49146f3c | 107 | mode = down; |
49146f3c DN |
108 | } else if (argv[i][0] == '-') { |
109 | usage(argv[0]); | |
110 | } else { | |
111 | interface = argv[i]; | |
112 | } | |
bdcaf7b9 | 113 | } |
347de8bd | 114 | |
49146f3c DN |
115 | if (!interface) |
116 | usage(argv[0]); | |
117 | if (mode == undefined) | |
118 | usage(argv[0]); | |
49146f3c | 119 | |
347de8bd TL |
120 | status = dhcpctl_initialize (); |
121 | if (status != ISC_R_SUCCESS) { | |
122 | fprintf (stderr, "dhcpctl_initialize: %s\n", | |
123 | isc_result_totext (status)); | |
124 | exit (1); | |
125 | } | |
126 | ||
49146f3c | 127 | authenticator = dhcpctl_null_handle; |
49146f3c | 128 | connection = dhcpctl_null_handle; |
88a14aea | 129 | |
347de8bd | 130 | status = dhcpctl_connect (&connection, "127.0.0.1", 7911, |
49146f3c | 131 | authenticator); |
347de8bd TL |
132 | if (status != ISC_R_SUCCESS) { |
133 | fprintf (stderr, "dhcpctl_connect: %s\n", | |
134 | isc_result_totext (status)); | |
135 | exit (1); | |
136 | } | |
137 | ||
49146f3c | 138 | interface_handle = dhcpctl_null_handle; |
5e4477a9 TL |
139 | status = dhcpctl_new_object (&interface_handle, |
140 | connection, "interface"); | |
347de8bd TL |
141 | if (status != ISC_R_SUCCESS) { |
142 | fprintf (stderr, "dhcpctl_new_object: %s\n", | |
143 | isc_result_totext (status)); | |
144 | exit (1); | |
145 | } | |
146 | ||
49146f3c DN |
147 | status = dhcpctl_set_string_value (interface_handle, |
148 | interface, "name"); | |
347de8bd TL |
149 | if (status != ISC_R_SUCCESS) { |
150 | fprintf (stderr, "dhcpctl_set_value: %s\n", | |
151 | isc_result_totext (status)); | |
152 | exit (1); | |
153 | } | |
154 | ||
bdcaf7b9 TL |
155 | if (mode == up) { |
156 | /* "up" the interface */ | |
49146f3c | 157 | printf ("upping interface %s\n", interface); |
bdcaf7b9 TL |
158 | action = "create"; |
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)); | |
164 | exit (1); | |
165 | } | |
166 | } else { | |
167 | /* down the interface */ | |
49146f3c | 168 | printf ("downing interface %s\n", interface); |
bdcaf7b9 TL |
169 | action = "remove"; |
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)); | |
174 | exit (1); | |
175 | } | |
176 | status = dhcpctl_wait_for_completion (interface_handle, | |
177 | &waitstatus); | |
178 | if (status != ISC_R_SUCCESS) { | |
179 | fprintf (stderr, "dhcpctl_wait_for_completion: %s\n", | |
180 | isc_result_totext (status)); | |
181 | exit (1); | |
182 | } | |
183 | if (waitstatus != ISC_R_SUCCESS) { | |
184 | fprintf (stderr, "dhcpctl_wait_for_completion: %s\n", | |
185 | isc_result_totext (waitstatus)); | |
186 | exit (1); | |
187 | } | |
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)); | |
192 | exit (1); | |
193 | } | |
347de8bd TL |
194 | } |
195 | ||
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)); | |
200 | exit (1); | |
201 | } | |
202 | if (waitstatus != ISC_R_SUCCESS) { | |
bdcaf7b9 | 203 | fprintf (stderr, "interface object %s: %s\n", action, |
347de8bd TL |
204 | isc_result_totext (waitstatus)); |
205 | exit (1); | |
206 | } | |
207 | ||
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)); | |
213 | exit (1); | |
214 | } | |
215 | ||
216 | exit (0); | |
217 | } |