test.c 7.09 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* test.c

   Example program that uses the dhcpctl library. */

/*
 * Copyright (c) 1996-1999 Internet Software Consortium.
 * Use is subject to license terms which appear in the file named
 * ISC-LICENSE that should have accompanied this file when you
 * received it.   If a file named ISC-LICENSE did not accompany this
 * file, or you are not sure the one you have is correct, you may
 * obtain an applicable copy of the license at:
 *
 *             http://www.isc.org/isc-license-1.0.html. 
 *
 * This file is part of the ISC DHCP distribution.   The documentation
 * associated with this file is listed in the file DOCUMENTATION,
 * included in the top-level directory of this release.
 *
 * Support and other services are available for ISC products - see
 * http://www.isc.org for more information.
 */

23 24 25 26 27
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <isc/result.h>
Ted Lemon's avatar
Ted Lemon committed
28 29 30 31 32 33 34 35 36 37
#include "dhcpctl.h"

int main (int, char **);

int main (argc, argv)
	int argc;
	char **argv;
{
	isc_result_t status, waitstatus;
	dhcpctl_handle connection;
38
	dhcpctl_handle host_handle, group_handle;
39
	dhcpctl_data_string cid, ip_addr;
40
	dhcpctl_data_string result, groupname;
Ted Lemon's avatar
Ted Lemon committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

	status = dhcpctl_initialize ();
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_initialize: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	memset (&connection, 0, sizeof connection);
	status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
				  (dhcpctl_handle)0);
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_connect: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

58 59 60 61 62 63 64 65 66 67
	/* Create a named group that contains the values we want to assign
	   to the host. */
	memset (&group_handle, 0, sizeof group_handle);
	status = dhcpctl_new_object (&group_handle, connection, "group");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_new_object: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

68 69
	status = dhcpctl_set_string_value (group_handle, "\n\
option domain-name \"foo.org\";\n\
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
option domain-name-servers 10.0.0.1, 10.0.0.2;",
					   "statements");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_set_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	status = dhcpctl_open_object (group_handle, connection,
				      DHCPCTL_CREATE | DHCPCTL_EXCL);
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_open_object: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	status = dhcpctl_wait_for_completion (group_handle, &waitstatus);
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
			 isc_result_totext (status));
		exit (1);
	}
	if (waitstatus != ISC_R_SUCCESS) {
		fprintf (stderr, "group object create: %s\n",
			 isc_result_totext (waitstatus));
		exit (1);
	}

	memset (&groupname, 0, sizeof groupname);
	status = dhcpctl_get_value (&groupname, group_handle, "name");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_get_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	printf ("group name = %*s\n", groupname -> len, groupname -> value);

Ted Lemon's avatar
Ted Lemon committed
108 109 110 111 112 113 114 115
	memset (&host_handle, 0, sizeof host_handle);
	status = dhcpctl_new_object (&host_handle, connection, "host");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_new_object: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

116
#if 1
Ted Lemon's avatar
Ted Lemon committed
117
	memset (&cid, 0, sizeof cid);
118
	status = omapi_data_string_new (&cid, 6, "main");
Ted Lemon's avatar
Ted Lemon committed
119 120 121 122 123 124
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "omapi_data_string_new: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

125 126 127
	cid -> value [0] = 0; cid -> value [1] = 0x10;
	cid -> value [2] = 0x5a; cid -> value [3] = 0xf8;
	cid -> value [4] = 0x00; cid -> value [5] = 0xbb;
Ted Lemon's avatar
Ted Lemon committed
128

129 130 131
	status = dhcpctl_set_value (host_handle,
				    cid, "dhcp-client-identifier");
#else
132 133 134 135 136 137 138
	status = dhcpctl_set_string_value (host_handle, "grosse",
					   "dhcp-client-identifier");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_set_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}
139
#endif
140

141
	status = dhcpctl_set_value (host_handle, groupname, "group");
142 143 144 145 146 147
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_set_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

148
#if 0
149 150 151 152 153 154 155 156 157 158 159 160
	memset (&ip_addr, 0, sizeof ip_addr);
	status = omapi_data_string_new (&ip_addr, 4, "main");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "omapi_data_string_new: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	ip_addr -> value [0] = 10; ip_addr -> value [1] = 0;
	ip_addr -> value [2] = 0; ip_addr -> value [3] = 2;

	status = dhcpctl_set_value (host_handle, ip_addr, "ip-address");
Ted Lemon's avatar
Ted Lemon committed
161 162 163 164 165
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_set_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}
166
#endif
Ted Lemon's avatar
Ted Lemon committed
167 168 169 170 171 172 173 174 175

	/* Set the known flag to 1. */
	status = dhcpctl_set_boolean_value (host_handle, 1, "known");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_set_boolean_value: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

176 177
       status = dhcpctl_set_string_value (host_handle, "\n\
option host-name \"bar\";\n\
178 179 180 181 182 183 184 185
option smtp-server 10.0.0.1;",
                                          "statements");
       if (status != ISC_R_SUCCESS) {
               fprintf (stderr, "dhcpctl_set_value: %s\n",
                        isc_result_totext (status));
               exit (1);
       }

Ted Lemon's avatar
Ted Lemon committed
186 187 188 189 190 191 192 193 194 195 196
	status = dhcpctl_open_object (host_handle, connection,
				      DHCPCTL_CREATE | DHCPCTL_EXCL);
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_open_object: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	status = dhcpctl_wait_for_completion (host_handle, &waitstatus);
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
197
			 isc_result_totext (status));
Ted Lemon's avatar
Ted Lemon committed
198 199 200 201
		exit (1);
	}

	if (waitstatus != ISC_R_SUCCESS) {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		status = dhcpctl_open_object (host_handle, connection, 0);
		if (status != ISC_R_SUCCESS) {
			fprintf (stderr, "dhcpctl_open_object: %s\n",
				 isc_result_totext (status));
			exit (1);
		}
		status = dhcpctl_wait_for_completion (host_handle,
						      &waitstatus);
		if (status != ISC_R_SUCCESS) {
			fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
				 isc_result_totext (status));
			exit (1);
		}
		if (waitstatus != ISC_R_SUCCESS) {
			fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
217
				 isc_result_totext (waitstatus));
218 219 220 221 222 223 224 225 226
			exit (1);
		}
	}

	memset (&result, 0, sizeof result);
	status = dhcpctl_get_value (&result, host_handle, "name");
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_get_value: %s\n",
			 isc_result_totext (status));
Ted Lemon's avatar
Ted Lemon committed
227 228
		exit (1);
	}
229

230
	printf ("host name = %*s\n", result -> len, result -> value);
231

232
#if 0
233
	status = dhcpctl_object_remove (connection, host_handle);
234
	if (status != ISC_R_SUCCESS) {
235
		fprintf (stderr, "dhcpctl_object_remove: %s\n",
236 237 238 239 240 241
			 isc_result_totext (status));
		exit (1);
	}
	status = dhcpctl_wait_for_completion (host_handle,
					      &waitstatus);
	if (status != ISC_R_SUCCESS) {
242
		fprintf (stderr, "remove: dhcpctl_wait_for_completion: %s\n",
243 244 245 246
			 isc_result_totext (status));
		exit (1);
	}
	if (waitstatus != ISC_R_SUCCESS) {
247
		fprintf (stderr, "remove: dhcpctl_wait_for_completion: %s\n",
248 249 250
			 isc_result_totext (waitstatus));
		exit (1);
	}
251
#endif
Ted Lemon's avatar
Ted Lemon committed
252 253
	exit (0);
}