]> git.ipfire.org Git - thirdparty/dhcp.git/blame - dhcpctl/test.c
Fix up one mistake from last night's fixes.
[thirdparty/dhcp.git] / dhcpctl / test.c
CommitLineData
d142e03f
TL
1/* test.c
2
3 Example program that uses the dhcpctl library. */
4
5/*
6 * Copyright (c) 1996-1999 Internet Software Consortium.
7 * Use is subject to license terms which appear in the file named
8 * ISC-LICENSE that should have accompanied this file when you
9 * received it. If a file named ISC-LICENSE did not accompany this
10 * file, or you are not sure the one you have is correct, you may
11 * obtain an applicable copy of the license at:
12 *
13 * http://www.isc.org/isc-license-1.0.html.
14 *
15 * This file is part of the ISC DHCP distribution. The documentation
16 * associated with this file is listed in the file DOCUMENTATION,
17 * included in the top-level directory of this release.
18 *
19 * Support and other services are available for ISC products - see
20 * http://www.isc.org for more information.
21 */
22
23#include "dhcpctl.h"
24
25int main (int, char **);
26
27int main (argc, argv)
28 int argc;
29 char **argv;
30{
31 isc_result_t status, waitstatus;
32 dhcpctl_handle connection;
4577d30c 33 dhcpctl_handle host_handle, group_handle;
bfacc53f 34 dhcpctl_data_string cid, ip_addr;
4577d30c 35 dhcpctl_data_string result, groupname;
d142e03f
TL
36
37 status = dhcpctl_initialize ();
38 if (status != ISC_R_SUCCESS) {
39 fprintf (stderr, "dhcpctl_initialize: %s\n",
40 isc_result_totext (status));
41 exit (1);
42 }
43
44 memset (&connection, 0, sizeof connection);
45 status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
46 (dhcpctl_handle)0);
47 if (status != ISC_R_SUCCESS) {
48 fprintf (stderr, "dhcpctl_connect: %s\n",
49 isc_result_totext (status));
50 exit (1);
51 }
52
4577d30c
TL
53 /* Create a named group that contains the values we want to assign
54 to the host. */
55 memset (&group_handle, 0, sizeof group_handle);
56 status = dhcpctl_new_object (&group_handle, connection, "group");
57 if (status != ISC_R_SUCCESS) {
58 fprintf (stderr, "dhcpctl_new_object: %s\n",
59 isc_result_totext (status));
60 exit (1);
61 }
62
a9fbbc8c
TL
63 status = dhcpctl_set_string_value (group_handle, "\n\
64option domain-name \"foo.org\";\n\
4577d30c
TL
65option domain-name-servers 10.0.0.1, 10.0.0.2;",
66 "statements");
67 if (status != ISC_R_SUCCESS) {
68 fprintf (stderr, "dhcpctl_set_value: %s\n",
69 isc_result_totext (status));
70 exit (1);
71 }
72
73 status = dhcpctl_open_object (group_handle, connection,
74 DHCPCTL_CREATE | DHCPCTL_EXCL);
75 if (status != ISC_R_SUCCESS) {
76 fprintf (stderr, "dhcpctl_open_object: %s\n",
77 isc_result_totext (status));
78 exit (1);
79 }
80
81 status = dhcpctl_wait_for_completion (group_handle, &waitstatus);
82 if (status != ISC_R_SUCCESS) {
83 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
84 isc_result_totext (status));
85 exit (1);
86 }
87 if (waitstatus != ISC_R_SUCCESS) {
88 fprintf (stderr, "group object create: %s\n",
89 isc_result_totext (waitstatus));
90 exit (1);
91 }
92
93 memset (&groupname, 0, sizeof groupname);
94 status = dhcpctl_get_value (&groupname, group_handle, "name");
95 if (status != ISC_R_SUCCESS) {
96 fprintf (stderr, "dhcpctl_get_value: %s\n",
97 isc_result_totext (status));
98 exit (1);
99 }
100
101 printf ("group name = %*s\n", groupname -> len, groupname -> value);
102
d142e03f
TL
103 memset (&host_handle, 0, sizeof host_handle);
104 status = dhcpctl_new_object (&host_handle, connection, "host");
105 if (status != ISC_R_SUCCESS) {
106 fprintf (stderr, "dhcpctl_new_object: %s\n",
107 isc_result_totext (status));
108 exit (1);
109 }
110
4577d30c 111#if 1
d142e03f 112 memset (&cid, 0, sizeof cid);
a0d8a7de 113 status = omapi_data_string_new (&cid, 6, "main");
d142e03f
TL
114 if (status != ISC_R_SUCCESS) {
115 fprintf (stderr, "omapi_data_string_new: %s\n",
116 isc_result_totext (status));
117 exit (1);
118 }
119
a0d8a7de
TL
120 cid -> value [0] = 0; cid -> value [1] = 0x10;
121 cid -> value [2] = 0x5a; cid -> value [3] = 0xf8;
122 cid -> value [4] = 0x00; cid -> value [5] = 0xbb;
d142e03f 123
4577d30c
TL
124 status = dhcpctl_set_value (host_handle,
125 cid, "dhcp-client-identifier");
126#else
bfacc53f
TL
127 status = dhcpctl_set_string_value (host_handle, "grosse",
128 "dhcp-client-identifier");
129 if (status != ISC_R_SUCCESS) {
130 fprintf (stderr, "dhcpctl_set_value: %s\n",
131 isc_result_totext (status));
132 exit (1);
133 }
4577d30c 134#endif
bfacc53f 135
4577d30c 136 status = dhcpctl_set_value (host_handle, groupname, "group");
bfacc53f
TL
137 if (status != ISC_R_SUCCESS) {
138 fprintf (stderr, "dhcpctl_set_value: %s\n",
139 isc_result_totext (status));
140 exit (1);
141 }
142
4577d30c 143#if 0
bfacc53f
TL
144 memset (&ip_addr, 0, sizeof ip_addr);
145 status = omapi_data_string_new (&ip_addr, 4, "main");
146 if (status != ISC_R_SUCCESS) {
147 fprintf (stderr, "omapi_data_string_new: %s\n",
148 isc_result_totext (status));
149 exit (1);
150 }
151
152 ip_addr -> value [0] = 10; ip_addr -> value [1] = 0;
153 ip_addr -> value [2] = 0; ip_addr -> value [3] = 2;
154
155 status = dhcpctl_set_value (host_handle, ip_addr, "ip-address");
d142e03f
TL
156 if (status != ISC_R_SUCCESS) {
157 fprintf (stderr, "dhcpctl_set_value: %s\n",
158 isc_result_totext (status));
159 exit (1);
160 }
4577d30c 161#endif
d142e03f
TL
162
163 /* Set the known flag to 1. */
164 status = dhcpctl_set_boolean_value (host_handle, 1, "known");
165 if (status != ISC_R_SUCCESS) {
166 fprintf (stderr, "dhcpctl_set_boolean_value: %s\n",
167 isc_result_totext (status));
168 exit (1);
169 }
170
a9fbbc8c
TL
171 status = dhcpctl_set_string_value (host_handle, "\n\
172option host-name \"bar\";\n\
d70dcd50
TL
173option smtp-server 10.0.0.1;",
174 "statements");
175 if (status != ISC_R_SUCCESS) {
176 fprintf (stderr, "dhcpctl_set_value: %s\n",
177 isc_result_totext (status));
178 exit (1);
179 }
180
d142e03f
TL
181 status = dhcpctl_open_object (host_handle, connection,
182 DHCPCTL_CREATE | DHCPCTL_EXCL);
183 if (status != ISC_R_SUCCESS) {
184 fprintf (stderr, "dhcpctl_open_object: %s\n",
185 isc_result_totext (status));
186 exit (1);
187 }
188
189 status = dhcpctl_wait_for_completion (host_handle, &waitstatus);
190 if (status != ISC_R_SUCCESS) {
191 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
a0d8a7de 192 isc_result_totext (status));
d142e03f
TL
193 exit (1);
194 }
195
196 if (waitstatus != ISC_R_SUCCESS) {
472c048a
TL
197 status = dhcpctl_open_object (host_handle, connection, 0);
198 if (status != ISC_R_SUCCESS) {
199 fprintf (stderr, "dhcpctl_open_object: %s\n",
200 isc_result_totext (status));
201 exit (1);
202 }
203 status = dhcpctl_wait_for_completion (host_handle,
204 &waitstatus);
205 if (status != ISC_R_SUCCESS) {
206 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
207 isc_result_totext (status));
208 exit (1);
209 }
210 if (waitstatus != ISC_R_SUCCESS) {
211 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
a0d8a7de 212 isc_result_totext (waitstatus));
472c048a
TL
213 exit (1);
214 }
215 }
216
217 memset (&result, 0, sizeof result);
218 status = dhcpctl_get_value (&result, host_handle, "name");
219 if (status != ISC_R_SUCCESS) {
220 fprintf (stderr, "dhcpctl_get_value: %s\n",
221 isc_result_totext (status));
d142e03f
TL
222 exit (1);
223 }
472c048a 224
b1b7b521 225 printf ("host name = %*s\n", result -> len, result -> value);
472c048a 226
bfacc53f 227#if 0
69633708 228 status = dhcpctl_object_remove (connection, host_handle);
a0d8a7de 229 if (status != ISC_R_SUCCESS) {
69633708 230 fprintf (stderr, "dhcpctl_object_remove: %s\n",
a0d8a7de
TL
231 isc_result_totext (status));
232 exit (1);
233 }
234 status = dhcpctl_wait_for_completion (host_handle,
235 &waitstatus);
236 if (status != ISC_R_SUCCESS) {
69633708 237 fprintf (stderr, "remove: dhcpctl_wait_for_completion: %s\n",
a0d8a7de
TL
238 isc_result_totext (status));
239 exit (1);
240 }
241 if (waitstatus != ISC_R_SUCCESS) {
69633708 242 fprintf (stderr, "remove: dhcpctl_wait_for_completion: %s\n",
a0d8a7de
TL
243 isc_result_totext (waitstatus));
244 exit (1);
245 }
bfacc53f 246#endif
d142e03f
TL
247 exit (0);
248}