Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / dhcp / dhcpctl / cltest.c
blobdd6fca8ab5ebcd6defc425de6b703c90d02626cc
1 /* cltest.c
3 Example program that uses the dhcpctl library. */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2000-2003 by Internet Software Consortium
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.
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.
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software was contributed to Internet Systems Consortium
28 * by Brian Murrell.
31 #ifndef lint
32 static char ocopyright[] =
33 "$Id: cltest.c,v 1.3 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
34 #endif /* not lint */
36 #include <time.h>
37 #include <sys/time.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <isc-dhcp/result.h>
43 #include "dhcpctl.h"
45 int main (int, char **);
47 enum modes { up, down, undefined };
49 static void usage (char *s) {
50 fprintf (stderr,
51 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
52 "(-u | -d) <if>\n", s);
53 exit (1);
56 int main (argc, argv)
57 int argc;
58 char **argv;
60 isc_result_t status, waitstatus;
61 dhcpctl_handle authenticator;
62 dhcpctl_handle connection;
63 dhcpctl_handle host_handle, group_handle, interface_handle;
64 dhcpctl_data_string cid;
65 dhcpctl_data_string result, groupname, identifier;
66 int i;
67 int mode = undefined;
68 const char *interface = 0;
69 const char *action;
71 for (i = 1; i < argc; i++) {
72 if (!strcmp (argv[i], "-u")) {
73 mode = up;
74 } else if (!strcmp (argv [i], "-d")) {
75 mode = down;
76 } else if (argv[i][0] == '-') {
77 usage(argv[0]);
78 } else {
79 interface = argv[i];
83 if (!interface)
84 usage(argv[0]);
85 if (mode == undefined)
86 usage(argv[0]);
88 status = dhcpctl_initialize ();
89 if (status != ISC_R_SUCCESS) {
90 fprintf (stderr, "dhcpctl_initialize: %s\n",
91 isc_result_totext (status));
92 exit (1);
95 authenticator = dhcpctl_null_handle;
96 connection = dhcpctl_null_handle;
98 status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
99 authenticator);
100 if (status != ISC_R_SUCCESS) {
101 fprintf (stderr, "dhcpctl_connect: %s\n",
102 isc_result_totext (status));
103 exit (1);
106 interface_handle = dhcpctl_null_handle;
107 status = dhcpctl_new_object (&interface_handle,
108 connection, "interface");
109 if (status != ISC_R_SUCCESS) {
110 fprintf (stderr, "dhcpctl_new_object: %s\n",
111 isc_result_totext (status));
112 exit (1);
115 status = dhcpctl_set_string_value (interface_handle,
116 interface, "name");
117 if (status != ISC_R_SUCCESS) {
118 fprintf (stderr, "dhcpctl_set_value: %s\n",
119 isc_result_totext (status));
120 exit (1);
123 if (mode == up) {
124 /* "up" the interface */
125 printf ("upping interface %s\n", interface);
126 action = "create";
127 status = dhcpctl_open_object (interface_handle, connection,
128 DHCPCTL_CREATE | DHCPCTL_EXCL);
129 if (status != ISC_R_SUCCESS) {
130 fprintf (stderr, "dhcpctl_open_object: %s\n",
131 isc_result_totext (status));
132 exit (1);
134 } else {
135 /* down the interface */
136 printf ("downing interface %s\n", interface);
137 action = "remove";
138 status = dhcpctl_open_object (interface_handle, connection, 0);
139 if (status != ISC_R_SUCCESS) {
140 fprintf (stderr, "dhcpctl_open_object: %s\n",
141 isc_result_totext (status));
142 exit (1);
144 status = dhcpctl_wait_for_completion (interface_handle,
145 &waitstatus);
146 if (status != ISC_R_SUCCESS) {
147 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
148 isc_result_totext (status));
149 exit (1);
151 if (waitstatus != ISC_R_SUCCESS) {
152 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
153 isc_result_totext (waitstatus));
154 exit (1);
156 status = dhcpctl_object_remove (connection, interface_handle);
157 if (status != ISC_R_SUCCESS) {
158 fprintf (stderr, "dhcpctl_open_object: %s\n",
159 isc_result_totext (status));
160 exit (1);
164 status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
165 if (status != ISC_R_SUCCESS) {
166 fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
167 isc_result_totext (status));
168 exit (1);
170 if (waitstatus != ISC_R_SUCCESS) {
171 fprintf (stderr, "interface object %s: %s\n", action,
172 isc_result_totext (waitstatus));
173 exit (1);
176 memset (&result, 0, sizeof result);
177 status = dhcpctl_get_value (&result, interface_handle, "state");
178 if (status != ISC_R_SUCCESS) {
179 fprintf (stderr, "dhcpctl_get_value: %s\n",
180 isc_result_totext (status));
181 exit (1);
184 exit (0);