]>
Commit | Line | Data |
---|---|---|
347de8bd TL |
1 | /* cltest.c |
2 | ||
3 | Example program that uses the dhcpctl library. */ | |
4 | ||
98311e4b | 5 | /* |
39725d92 | 6 | * Copyright (c) 2004,2007 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 | ||
347de8bd | 31 | #include <time.h> |
0f6bb7e1 | 32 | #include <sys/time.h> |
347de8bd TL |
33 | #include <stdio.h> |
34 | #include <stdlib.h> | |
06f0ed06 | 35 | #include <string.h> |
347de8bd | 36 | #include <stdarg.h> |
98bf1607 | 37 | #include "omapip/result.h" |
347de8bd TL |
38 | #include "dhcpctl.h" |
39 | ||
40 | int main (int, char **); | |
41 | ||
49146f3c DN |
42 | enum modes { up, down, undefined }; |
43 | ||
44 | static void usage (char *s) { | |
45 | fprintf (stderr, | |
46 | "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]" | |
47 | "(-u | -d) <if>\n", s); | |
48 | exit (1); | |
49 | } | |
bdcaf7b9 | 50 | |
347de8bd TL |
51 | int main (argc, argv) |
52 | int argc; | |
53 | char **argv; | |
54 | { | |
55 | isc_result_t status, waitstatus; | |
49146f3c | 56 | dhcpctl_handle authenticator; |
347de8bd | 57 | dhcpctl_handle connection; |
28868515 SK |
58 | dhcpctl_handle interface_handle; |
59 | dhcpctl_data_string result; | |
347de8bd | 60 | int i; |
49146f3c | 61 | int mode = undefined; |
2cd44219 | 62 | const char *interface = 0; |
5e4477a9 | 63 | const char *action; |
49146f3c DN |
64 | |
65 | for (i = 1; i < argc; i++) { | |
66 | if (!strcmp (argv[i], "-u")) { | |
67 | mode = up; | |
f0b827ef | 68 | } else if (!strcmp (argv [i], "-d")) { |
49146f3c | 69 | mode = down; |
49146f3c DN |
70 | } else if (argv[i][0] == '-') { |
71 | usage(argv[0]); | |
72 | } else { | |
73 | interface = argv[i]; | |
74 | } | |
bdcaf7b9 | 75 | } |
347de8bd | 76 | |
49146f3c DN |
77 | if (!interface) |
78 | usage(argv[0]); | |
79 | if (mode == undefined) | |
80 | usage(argv[0]); | |
49146f3c | 81 | |
347de8bd TL |
82 | status = dhcpctl_initialize (); |
83 | if (status != ISC_R_SUCCESS) { | |
84 | fprintf (stderr, "dhcpctl_initialize: %s\n", | |
85 | isc_result_totext (status)); | |
86 | exit (1); | |
87 | } | |
88 | ||
49146f3c | 89 | authenticator = dhcpctl_null_handle; |
49146f3c | 90 | connection = dhcpctl_null_handle; |
88a14aea | 91 | |
347de8bd | 92 | status = dhcpctl_connect (&connection, "127.0.0.1", 7911, |
49146f3c | 93 | authenticator); |
347de8bd TL |
94 | if (status != ISC_R_SUCCESS) { |
95 | fprintf (stderr, "dhcpctl_connect: %s\n", | |
96 | isc_result_totext (status)); | |
97 | exit (1); | |
98 | } | |
99 | ||
49146f3c | 100 | interface_handle = dhcpctl_null_handle; |
5e4477a9 TL |
101 | status = dhcpctl_new_object (&interface_handle, |
102 | connection, "interface"); | |
347de8bd TL |
103 | if (status != ISC_R_SUCCESS) { |
104 | fprintf (stderr, "dhcpctl_new_object: %s\n", | |
105 | isc_result_totext (status)); | |
106 | exit (1); | |
107 | } | |
108 | ||
49146f3c DN |
109 | status = dhcpctl_set_string_value (interface_handle, |
110 | interface, "name"); | |
347de8bd TL |
111 | if (status != ISC_R_SUCCESS) { |
112 | fprintf (stderr, "dhcpctl_set_value: %s\n", | |
113 | isc_result_totext (status)); | |
114 | exit (1); | |
115 | } | |
116 | ||
bdcaf7b9 TL |
117 | if (mode == up) { |
118 | /* "up" the interface */ | |
49146f3c | 119 | printf ("upping interface %s\n", interface); |
bdcaf7b9 TL |
120 | action = "create"; |
121 | status = dhcpctl_open_object (interface_handle, connection, | |
122 | DHCPCTL_CREATE | DHCPCTL_EXCL); | |
123 | if (status != ISC_R_SUCCESS) { | |
124 | fprintf (stderr, "dhcpctl_open_object: %s\n", | |
125 | isc_result_totext (status)); | |
126 | exit (1); | |
127 | } | |
128 | } else { | |
129 | /* down the interface */ | |
49146f3c | 130 | printf ("downing interface %s\n", interface); |
bdcaf7b9 TL |
131 | action = "remove"; |
132 | status = dhcpctl_open_object (interface_handle, connection, 0); | |
133 | if (status != ISC_R_SUCCESS) { | |
134 | fprintf (stderr, "dhcpctl_open_object: %s\n", | |
135 | isc_result_totext (status)); | |
136 | exit (1); | |
137 | } | |
138 | status = dhcpctl_wait_for_completion (interface_handle, | |
139 | &waitstatus); | |
140 | if (status != ISC_R_SUCCESS) { | |
141 | fprintf (stderr, "dhcpctl_wait_for_completion: %s\n", | |
142 | isc_result_totext (status)); | |
143 | exit (1); | |
144 | } | |
145 | if (waitstatus != ISC_R_SUCCESS) { | |
146 | fprintf (stderr, "dhcpctl_wait_for_completion: %s\n", | |
147 | isc_result_totext (waitstatus)); | |
148 | exit (1); | |
149 | } | |
150 | status = dhcpctl_object_remove (connection, interface_handle); | |
151 | if (status != ISC_R_SUCCESS) { | |
152 | fprintf (stderr, "dhcpctl_open_object: %s\n", | |
153 | isc_result_totext (status)); | |
154 | exit (1); | |
155 | } | |
347de8bd TL |
156 | } |
157 | ||
158 | status = dhcpctl_wait_for_completion (interface_handle, &waitstatus); | |
159 | if (status != ISC_R_SUCCESS) { | |
160 | fprintf (stderr, "dhcpctl_wait_for_completion: %s\n", | |
161 | isc_result_totext (status)); | |
162 | exit (1); | |
163 | } | |
164 | if (waitstatus != ISC_R_SUCCESS) { | |
bdcaf7b9 | 165 | fprintf (stderr, "interface object %s: %s\n", action, |
347de8bd TL |
166 | isc_result_totext (waitstatus)); |
167 | exit (1); | |
168 | } | |
169 | ||
170 | memset (&result, 0, sizeof result); | |
171 | status = dhcpctl_get_value (&result, interface_handle, "state"); | |
172 | if (status != ISC_R_SUCCESS) { | |
173 | fprintf (stderr, "dhcpctl_get_value: %s\n", | |
174 | isc_result_totext (status)); | |
175 | exit (1); | |
176 | } | |
177 | ||
178 | exit (0); | |
179 | } |