xref: /openbsd-src/sbin/dhcp6leased/printconf.c (revision 3ae4b9df79285457e898ae3ce0dd69c842f0a5b2)
1 /*	$OpenBSD: printconf.c,v 1.3 2024/06/03 11:08:31 florian Exp $	*/
2 
3 /*
4  * Copyright (c) 2024 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/uio.h>
24 
25 #include <net/if.h>
26 
27 #include <netinet/in.h>
28 
29 #include <arpa/inet.h>
30 
31 #include <event.h>
32 #include <imsg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "dhcp6leased.h"
38 #include "log.h"
39 
40 void	print_iface_conf(struct iface_conf *, int);
41 void	print_iface_ia_conf(struct iface_ia_conf *, int);
42 void	print_iface_pd_conf(char *, struct iface_pd_conf *, int);
43 
44 void
print_iface_pd_conf(char * indent,struct iface_pd_conf * pd_conf,int verbose)45 print_iface_pd_conf(char *indent, struct iface_pd_conf *pd_conf, int verbose)
46 {
47 	if (verbose > 1) {
48 		struct in6_addr	 ia6;
49 		int		 i;
50 		char		 ntopbuf[INET6_ADDRSTRLEN];
51 
52 		memset(&ia6, 0, sizeof(ia6));
53 		inet_pton(AF_INET6, "2001:db8::", &ia6);
54 
55 		for (i = 0; i < 16; i++)
56 			ia6.s6_addr[i] |= pd_conf->prefix_mask.s6_addr[i];
57 
58 		inet_ntop(AF_INET6, &ia6, ntopbuf, INET6_ADDRSTRLEN);
59 		printf("%s%s/%d\t# %s/%d\n", indent, pd_conf->name,
60 		    pd_conf->prefix_len, ntopbuf, pd_conf->prefix_len);
61 	} else
62 		printf("%s%s/%d\n", indent, pd_conf->name, pd_conf->prefix_len);
63 }
64 
65 void
print_iface_ia_conf(struct iface_ia_conf * ia_conf,int verbose)66 print_iface_ia_conf(struct iface_ia_conf *ia_conf, int verbose)
67 {
68 	struct iface_pd_conf	*pd_conf;
69 
70 	SIMPLEQ_FOREACH(pd_conf, &ia_conf->iface_pd_list, entry)
71 	    print_iface_pd_conf("\t", pd_conf,
72 		ia_conf->prefix_len >= 32 ? verbose : 1);
73 }
74 
75 void
print_iface_conf(struct iface_conf * iface,int verbose)76 print_iface_conf(struct iface_conf *iface, int verbose)
77 {
78 	struct iface_ia_conf	*ia_conf;
79 	int			 first = 1;
80 
81 	SIMPLEQ_FOREACH(ia_conf, &iface->iface_ia_list, entry) {
82 		if (!first)
83 			printf("\n");
84 		first = 0;
85 
86 		if (verbose > 1) {
87 			printf("request prefix delegation on %s for {"
88 			    "\t# prefix length = %d\n", iface->name,
89 			    ia_conf->prefix_len);
90 		} else {
91 			printf("request prefix delegation on %s for {\n",
92 			    iface->name);
93 		}
94 		print_iface_ia_conf(ia_conf, verbose);
95 		printf("}\n");
96 	}
97 }
98 void
print_config(struct dhcp6leased_conf * conf,int verbose)99 print_config(struct dhcp6leased_conf *conf, int verbose)
100 {
101 	struct iface_conf	*iface;
102 
103 	if (conf->rapid_commit)
104 		printf("request rapid commit\n\n");
105 
106 	SIMPLEQ_FOREACH(iface, &conf->iface_list, entry)
107 		print_iface_conf(iface, verbose);
108 }
109