xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/util.h (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
24  * Copyright 2019 Joshua M. Clulow <josh@sysmgr.org>
25  */
26 
27 #ifndef	UTIL_H
28 #define	UTIL_H
29 
30 #include <sys/types.h>
31 #include <sys/avl.h>
32 #include <netinet/in.h>
33 #include <netinet/dhcp.h>
34 #include <netinet/dhcp6.h>
35 #include <libinetutil.h>
36 #include <dhcpagent_ipc.h>
37 
38 #include "common.h"
39 #include "packet.h"
40 
41 /*
42  * general utility functions which have no better home.  see util.c
43  * for documentation on how to use the exported functions.
44  */
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 struct dhcp_timer_s {
51 	iu_timer_id_t	dt_id;
52 	lease_t		dt_start;		/* Initial timer value */
53 };
54 
55 typedef enum dhcp_route_source {
56 	DHR_SRC_CLASSLESS_ROUTES =	(1ULL << 0),
57 	DHR_SRC_DEFAULT_GATEWAY =	(1ULL << 1),
58 } dhcp_route_source_t;
59 
60 typedef struct dhcp_route_s {
61 	uint8_t			dhr_prefix;
62 	struct in_addr		dhr_network;
63 	struct in_addr		dhr_nexthop;
64 	avl_node_t		dhr_node;
65 	boolean_t		dhr_installed;
66 	boolean_t		dhr_interface;
67 	dhcp_route_source_t	dhr_source;
68 } dhcp_route_t;
69 
70 /* conversion functions */
71 const char	*pkt_type_to_string(uchar_t, boolean_t);
72 const char	*monosec_to_string(monosec_t);
73 time_t		monosec_to_time(monosec_t);
74 monosec_t	hrtime_to_monosec(hrtime_t);
75 
76 /* shutdown handlers */
77 void		graceful_shutdown(int);
78 void		inactivity_shutdown(iu_tq_t *, void *);
79 
80 /* timer functions */
81 void		init_timer(dhcp_timer_t *, lease_t);
82 boolean_t	cancel_timer(dhcp_timer_t *);
83 boolean_t	schedule_timer(dhcp_timer_t *, iu_tq_callback_t *, void *);
84 
85 /* miscellaneous */
86 boolean_t	add_route(uint32_t, dhcp_route_t *);
87 boolean_t	del_route(uint32_t, dhcp_route_t *);
88 int		daemonize(void);
89 monosec_t	monosec(void);
90 void		print_server_msg(dhcp_smach_t *, const char *, uint_t);
91 boolean_t	bind_sock(int, in_port_t, in_addr_t);
92 boolean_t	bind_sock_v6(int, in_port_t, const in6_addr_t *);
93 const char	*iffile_to_hostname(const char *);
94 int		dhcpv6_status_code(const dhcpv6_option_t *, uint_t,
95     const char **, const char **, uint_t *);
96 void		write_lease_to_hostconf(dhcp_smach_t *);
97 boolean_t	dhcp_add_hostname_opt(dhcp_pkt_t *, dhcp_smach_t *);
98 boolean_t	dhcp_add_fqdn_opt(dhcp_pkt_t *, dhcp_smach_t *);
99 void		save_domainname(dhcp_smach_t *, PKT_LIST *);
100 
101 #ifdef	__cplusplus
102 }
103 #endif
104 
105 #endif	/* UTIL_H */
106