xref: /netbsd/external/mpl/dhcp/dist/dhcpctl/cltest.c (revision 13df4856)
1*13df4856Schristos /*	$NetBSD: cltest.c,v 1.3 2022/04/03 01:10:58 christos Exp $	*/
23965be93Schristos 
33965be93Schristos /* cltest.c
43965be93Schristos 
53965be93Schristos    Example program that uses the dhcpctl library. */
63965be93Schristos 
73965be93Schristos /*
8*13df4856Schristos  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
93965be93Schristos  * Copyright (c) 2000-2003 by Internet Software Consortium
103965be93Schristos  *
113965be93Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
123965be93Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
133965be93Schristos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
143965be93Schristos  *
153965be93Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
163965be93Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
173965be93Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
183965be93Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
193965be93Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
203965be93Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
213965be93Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
223965be93Schristos  *
233965be93Schristos  *   Internet Systems Consortium, Inc.
24*13df4856Schristos  *   PO Box 360
25*13df4856Schristos  *   Newmarket, NH 03857 USA
263965be93Schristos  *   <info@isc.org>
273965be93Schristos  *   https://www.isc.org/
283965be93Schristos  *
293965be93Schristos  * This software was contributed to Internet Systems Consortium
303965be93Schristos  * by Brian Murrell.
313965be93Schristos  */
323965be93Schristos 
333965be93Schristos #include <sys/cdefs.h>
34*13df4856Schristos __RCSID("$NetBSD: cltest.c,v 1.3 2022/04/03 01:10:58 christos Exp $");
353965be93Schristos 
363965be93Schristos #include "config.h"
373965be93Schristos 
383965be93Schristos #include <time.h>
393965be93Schristos #include <sys/time.h>
403965be93Schristos #include <stdio.h>
413965be93Schristos #include <stdlib.h>
423965be93Schristos #include <string.h>
433965be93Schristos #include <stdarg.h>
443965be93Schristos #include "omapip/result.h"
453965be93Schristos #include "dhcpctl.h"
463965be93Schristos #include "dhcpd.h"
473965be93Schristos 
483965be93Schristos /* Fixups */
find_class(struct class ** c,const char * n,const char * f,int l)493965be93Schristos isc_result_t find_class (struct class **c, const char *n, const char *f, int l)
503965be93Schristos {
513965be93Schristos 	return 0;
523965be93Schristos }
parse_allow_deny(struct option_cache ** oc,struct parse * cfile,int flag)533965be93Schristos int parse_allow_deny (struct option_cache **oc, struct parse *cfile, int flag)
543965be93Schristos {
553965be93Schristos 	return 0;
563965be93Schristos }
dhcp(struct packet * packet)573965be93Schristos void dhcp (struct packet *packet) { }
bootp(struct packet * packet)583965be93Schristos void bootp (struct packet *packet) { }
593965be93Schristos 
603965be93Schristos #ifdef DHCPv6
613965be93Schristos /* XXX: should we warn or something here? */
dhcpv6(struct packet * packet)623965be93Schristos void dhcpv6(struct packet *packet) { }
633965be93Schristos #ifdef DHCP4o6
dhcpv4o6_handler(omapi_object_t * h)643965be93Schristos isc_result_t dhcpv4o6_handler(omapi_object_t *h)
653965be93Schristos {
663965be93Schristos 	return ISC_R_NOTIMPLEMENTED;
673965be93Schristos }
683965be93Schristos #endif /* DHCP4o6 */
693965be93Schristos #endif /* DHCPv6 */
703965be93Schristos 
check_collection(struct packet * p,struct lease * l,struct collection * c)713965be93Schristos int check_collection (struct packet *p, struct lease *l, struct collection *c)
723965be93Schristos {
733965be93Schristos 	return 0;
743965be93Schristos }
classify(struct packet * packet,struct class * class)753965be93Schristos void classify (struct packet *packet, struct class *class) { }
763965be93Schristos 
dhcp_set_control_state(control_object_state_t oldstate,control_object_state_t newstate)773965be93Schristos isc_result_t dhcp_set_control_state (control_object_state_t oldstate,
783965be93Schristos 				     control_object_state_t newstate)
793965be93Schristos {
803965be93Schristos 	return ISC_R_SUCCESS;
813965be93Schristos }
823965be93Schristos 
8307146da4Schristos uint16_t local_port = 0;
8407146da4Schristos uint16_t remote_port = 0;
8507146da4Schristos libdhcp_callbacks_t cltest_callbacks = {
8607146da4Schristos 	&local_port,
8707146da4Schristos 	&remote_port,
8807146da4Schristos 	classify,
8907146da4Schristos 	check_collection,
9007146da4Schristos 	dhcp,
9107146da4Schristos #ifdef DHCPv6
9207146da4Schristos 	dhcpv6,
9307146da4Schristos #endif /* DHCPv6 */
9407146da4Schristos 	bootp,
9507146da4Schristos 	find_class,
9607146da4Schristos 	parse_allow_deny,
9707146da4Schristos 	dhcp_set_control_state,
9807146da4Schristos };
9907146da4Schristos 
1003965be93Schristos int main (int, char **);
1013965be93Schristos 
1023965be93Schristos enum modes { up, down, undefined };
1033965be93Schristos 
usage(char * s)1043965be93Schristos static void usage (char *s) {
1053965be93Schristos 	fprintf (stderr,
1063965be93Schristos 		 "Usage: %s [-n <username>] [-p <password>] [-a <algorithm>]"
1073965be93Schristos 		 "(-u | -d) <if>\n", s);
1083965be93Schristos 	exit (1);
1093965be93Schristos }
1103965be93Schristos 
main(argc,argv)1113965be93Schristos int main (argc, argv)
1123965be93Schristos 	int argc;
1133965be93Schristos 	char **argv;
1143965be93Schristos {
1153965be93Schristos 	isc_result_t status, waitstatus;
1163965be93Schristos 	dhcpctl_handle authenticator;
1173965be93Schristos 	dhcpctl_handle connection;
1183965be93Schristos 	dhcpctl_handle interface_handle;
1193965be93Schristos 	dhcpctl_data_string result;
1203965be93Schristos 	int i;
1213965be93Schristos 	int mode = undefined;
1223965be93Schristos 	const char *interface = 0;
1233965be93Schristos 	const char *action;
1243965be93Schristos 
12507146da4Schristos 	libdhcp_callbacks_register(&cltest_callbacks);
12607146da4Schristos 
1273965be93Schristos 	for (i = 1; i < argc; i++) {
1283965be93Schristos 		if (!strcmp (argv[i], "-u")) {
1293965be93Schristos 			mode = up;
1303965be93Schristos 		} else if (!strcmp (argv [i], "-d")) {
1313965be93Schristos 			mode = down;
1323965be93Schristos 		} else if (argv[i][0] == '-') {
1333965be93Schristos 			usage(argv[0]);
1343965be93Schristos 		} else {
1353965be93Schristos 			interface = argv[i];
1363965be93Schristos 		}
1373965be93Schristos 	}
1383965be93Schristos 
1393965be93Schristos 	if (!interface)
1403965be93Schristos 		usage(argv[0]);
1413965be93Schristos 	if (mode == undefined)
1423965be93Schristos 		usage(argv[0]);
1433965be93Schristos 
1443965be93Schristos 	status = dhcpctl_initialize ();
1453965be93Schristos 	if (status != ISC_R_SUCCESS) {
1463965be93Schristos 		fprintf (stderr, "dhcpctl_initialize: %s\n",
1473965be93Schristos 			 isc_result_totext (status));
1483965be93Schristos 		exit (1);
1493965be93Schristos 	}
1503965be93Schristos 
1513965be93Schristos 	authenticator = dhcpctl_null_handle;
1523965be93Schristos 	connection = dhcpctl_null_handle;
1533965be93Schristos 
1543965be93Schristos 	status = dhcpctl_connect (&connection, "127.0.0.1", 7911,
1553965be93Schristos 				  authenticator);
1563965be93Schristos 	if (status != ISC_R_SUCCESS) {
1573965be93Schristos 		fprintf (stderr, "dhcpctl_connect: %s\n",
1583965be93Schristos 			 isc_result_totext (status));
1593965be93Schristos 		exit (1);
1603965be93Schristos 	}
1613965be93Schristos 
1623965be93Schristos 	interface_handle = dhcpctl_null_handle;
1633965be93Schristos 	status = dhcpctl_new_object (&interface_handle,
1643965be93Schristos 				     connection, "interface");
1653965be93Schristos 	if (status != ISC_R_SUCCESS) {
1663965be93Schristos 		fprintf (stderr, "dhcpctl_new_object: %s\n",
1673965be93Schristos 			 isc_result_totext (status));
1683965be93Schristos 		exit (1);
1693965be93Schristos 	}
1703965be93Schristos 
1713965be93Schristos 	status = dhcpctl_set_string_value (interface_handle,
1723965be93Schristos 					   interface, "name");
1733965be93Schristos 	if (status != ISC_R_SUCCESS) {
1743965be93Schristos 		fprintf (stderr, "dhcpctl_set_value: %s\n",
1753965be93Schristos 			 isc_result_totext (status));
1763965be93Schristos 		exit (1);
1773965be93Schristos 	}
1783965be93Schristos 
1793965be93Schristos 	if (mode == up) {
1803965be93Schristos 		/* "up" the interface */
1813965be93Schristos 		printf ("upping interface %s\n", interface);
1823965be93Schristos 		action = "create";
1833965be93Schristos 		status = dhcpctl_open_object (interface_handle, connection,
1843965be93Schristos 					      DHCPCTL_CREATE | DHCPCTL_EXCL);
1853965be93Schristos 		if (status != ISC_R_SUCCESS) {
1863965be93Schristos 			fprintf (stderr, "dhcpctl_open_object: %s\n",
1873965be93Schristos 				 isc_result_totext (status));
1883965be93Schristos 			exit (1);
1893965be93Schristos 		}
1903965be93Schristos 	} else {
1913965be93Schristos 		/* down the interface */
1923965be93Schristos 		printf ("downing interface %s\n", interface);
1933965be93Schristos 		action = "remove";
1943965be93Schristos 		status = dhcpctl_open_object (interface_handle, connection, 0);
1953965be93Schristos 		if (status != ISC_R_SUCCESS) {
1963965be93Schristos 			fprintf (stderr, "dhcpctl_open_object: %s\n",
1973965be93Schristos 				 isc_result_totext (status));
1983965be93Schristos 			exit (1);
1993965be93Schristos 		}
2003965be93Schristos 		status = dhcpctl_wait_for_completion (interface_handle,
2013965be93Schristos 						      &waitstatus);
2023965be93Schristos 		if (status != ISC_R_SUCCESS) {
2033965be93Schristos 			fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
2043965be93Schristos 				 isc_result_totext (status));
2053965be93Schristos 			exit (1);
2063965be93Schristos 		}
2073965be93Schristos 		if (waitstatus != ISC_R_SUCCESS) {
2083965be93Schristos 			fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
2093965be93Schristos 				 isc_result_totext (waitstatus));
2103965be93Schristos 			exit (1);
2113965be93Schristos 		}
2123965be93Schristos 		status = dhcpctl_object_remove (connection, interface_handle);
2133965be93Schristos 		if (status != ISC_R_SUCCESS) {
2143965be93Schristos 			fprintf (stderr, "dhcpctl_open_object: %s\n",
2153965be93Schristos 				 isc_result_totext (status));
2163965be93Schristos 			exit (1);
2173965be93Schristos 		}
2183965be93Schristos 	}
2193965be93Schristos 
2203965be93Schristos 	status = dhcpctl_wait_for_completion (interface_handle, &waitstatus);
2213965be93Schristos 	if (status != ISC_R_SUCCESS) {
2223965be93Schristos 		fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",
2233965be93Schristos 			 isc_result_totext (status));
2243965be93Schristos 		exit (1);
2253965be93Schristos 	}
2263965be93Schristos 	if (waitstatus != ISC_R_SUCCESS) {
2273965be93Schristos 		fprintf (stderr, "interface object %s: %s\n", action,
2283965be93Schristos 			 isc_result_totext (waitstatus));
2293965be93Schristos 		exit (1);
2303965be93Schristos 	}
2313965be93Schristos 
2323965be93Schristos 	memset (&result, 0, sizeof result);
2333965be93Schristos 	status = dhcpctl_get_value (&result, interface_handle, "state");
2343965be93Schristos 	if (status != ISC_R_SUCCESS) {
2353965be93Schristos 		fprintf (stderr, "dhcpctl_get_value: %s\n",
2363965be93Schristos 			 isc_result_totext (status));
2373965be93Schristos 		exit (1);
2383965be93Schristos 	}
2393965be93Schristos 
2403965be93Schristos 	exit (0);
2413965be93Schristos }
242