xref: /dragonfly/contrib/dhcpcd/src/dhcp-common.h (revision 193f58b8)
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2018 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef DHCPCOMMON_H
29 #define	DHCPCOMMON_H
30 
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 
34 #include <stdint.h>
35 
36 #include "common.h"
37 #include "dhcpcd.h"
38 
39 /* Max MTU - defines dhcp option length */
40 #define	IP_UDP_SIZE		  28
41 #define	MTU_MAX			1500 - IP_UDP_SIZE
42 #define	MTU_MIN			 576 + IP_UDP_SIZE
43 
44 #define	OT_REQUEST		(1 << 0)
45 #define	OT_UINT8		(1 << 1)
46 #define	OT_INT8			(1 << 2)
47 #define	OT_UINT16		(1 << 3)
48 #define	OT_INT16		(1 << 4)
49 #define	OT_UINT32		(1 << 5)
50 #define	OT_INT32		(1 << 6)
51 #define	OT_ADDRIPV4		(1 << 7)
52 #define	OT_STRING		(1 << 8)
53 #define	OT_ARRAY		(1 << 9)
54 #define	OT_RFC3361		(1 << 10)
55 #define	OT_RFC1035		(1 << 11)
56 #define	OT_RFC3442		(1 << 12)
57 #define	OT_OPTIONAL		(1 << 13)
58 #define	OT_ADDRIPV6		(1 << 14)
59 #define	OT_BINHEX		(1 << 15)
60 #define	OT_FLAG			(1 << 16)
61 #define	OT_NOREQ		(1 << 17)
62 #define	OT_EMBED		(1 << 18)
63 #define	OT_ENCAP		(1 << 19)
64 #define	OT_INDEX		(1 << 20)
65 #define	OT_OPTION		(1 << 21)
66 #define	OT_DOMAIN		(1 << 22)
67 #define	OT_ASCII		(1 << 23)
68 #define	OT_RAW			(1 << 24)
69 #define	OT_ESCSTRING		(1 << 25)
70 #define	OT_ESCFILE		(1 << 26)
71 #define	OT_BITFLAG		(1 << 27)
72 #define	OT_RESERVED		(1 << 28)
73 
74 struct dhcp_opt {
75 	uint32_t option; /* Also used for IANA Enterpise Number */
76 	int type;
77 	size_t len;
78 	char *var;
79 
80 	int index; /* Index counter for many instances of the same option */
81 	char bitflags[8];
82 
83 	/* Embedded options.
84 	 * The option code is irrelevant here. */
85 	struct dhcp_opt *embopts;
86 	size_t embopts_len;
87 
88 	/* Encapsulated options */
89 	struct dhcp_opt *encopts;
90 	size_t encopts_len;
91 };
92 
93 const char *dhcp_get_hostname(char *, size_t, const struct if_options *);
94 struct dhcp_opt *vivso_find(uint32_t, const void *);
95 
96 ssize_t dhcp_vendor(char *, size_t);
97 
98 void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols);
99 #define	add_option_mask(var, val) \
100 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] | 1 << ((val) & 7)))
101 #define	del_option_mask(var, val) \
102 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] & ~(1 << ((val) & 7))))
103 #define	has_option_mask(var, val) \
104 	((var)[(val) >> 3] & (uint8_t)(1 << ((val) & 7)))
105 int make_option_mask(const struct dhcp_opt *, size_t,
106     const struct dhcp_opt *, size_t,
107     uint8_t *, const char *, int);
108 
109 size_t encode_rfc1035(const char *src, uint8_t *dst);
110 ssize_t decode_rfc1035(char *, size_t, const uint8_t *, size_t);
111 ssize_t print_string(char *, size_t, int, const uint8_t *, size_t);
112 int dhcp_set_leasefile(char *, size_t, int, const struct interface *);
113 
114 size_t dhcp_envoption(struct dhcpcd_ctx *,
115     char **, const char *, const char *, struct dhcp_opt *,
116     const uint8_t *(*dgetopt)(struct dhcpcd_ctx *,
117     size_t *, unsigned int *, size_t *,
118     const uint8_t *, size_t, struct dhcp_opt **),
119     const uint8_t *od, size_t ol);
120 void dhcp_zero_index(struct dhcp_opt *);
121 size_t dhcp_read_lease_fd(int, void **);
122 
123 #endif
124