xref: /minix/external/bsd/dhcp/dist/dhcpctl/cltest.c (revision 83ee113e)
1 /*	$NetBSD: cltest.c,v 1.1.1.3 2014/07/12 11:57:51 spz Exp $	*/
2 /* cltest.c
3 
4    Example program that uses the dhcpctl library. */
5 
6 /*
7  * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 2000-2003 by Internet Software Consortium
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  *   Internet Systems Consortium, Inc.
23  *   950 Charter Street
24  *   Redwood City, CA 94063
25  *   <info@isc.org>
26  *   https://www.isc.org/
27  *
28  * This software was contributed to Internet Systems Consortium
29  * by Brian Murrell.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: cltest.c,v 1.1.1.3 2014/07/12 11:57:51 spz Exp $");
34 
35 #include "config.h"
36 
37 #include <time.h>
38 #include <sys/time.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include "omapip/result.h"
44 #include "dhcpctl.h"
45 
46 int main (int, char **);
47 
48 enum modes { up, down, undefined };
49 
usage(char * s)50 static void usage (char *s) {
51 	fprintf (stderr,
52 		 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
53 		 "(-u | -d) <if>\n", s);
54 	exit (1);
55 }
56 
main(argc,argv)57 int main (argc, argv)
58 	int argc;
59 	char **argv;
60 {
61 	isc_result_t status, waitstatus;
62 	dhcpctl_handle authenticator;
63 	dhcpctl_handle connection;
64 	dhcpctl_handle interface_handle;
65 	dhcpctl_data_string result;
66 	int i;
67 	int mode = undefined;
68 	const char *interface = 0;
69 	const char *action;
70 
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];
80 		}
81 	}
82 
83 	if (!interface)
84 		usage(argv[0]);
85 	if (mode == undefined)
86 		usage(argv[0]);
87 
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);
93 	}
94 
95 	authenticator = dhcpctl_null_handle;
96 	connection = dhcpctl_null_handle;
97 
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);
104 	}
105 
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);
113 	}
114 
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);
121 	}
122 
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);
133 		}
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);
143 		}
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);
150 		}
151 		if (waitstatus != ISC_R_SUCCESS) {
152 			fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
153 				 isc_result_totext (waitstatus));
154 			exit (1);
155 		}
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);
161 		}
162 	}
163 
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);
169 	}
170 	if (waitstatus != ISC_R_SUCCESS) {
171 		fprintf (stderr, "interface object %s: %s\n", action,
172 			 isc_result_totext (waitstatus));
173 		exit (1);
174 	}
175 
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);
182 	}
183 
184 	exit (0);
185 }
186