xref: /dragonfly/contrib/dhcpcd/src/common.h (revision 335b9e93)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef COMMON_H
30 #define COMMON_H
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 
37 /* Define eloop queues here, as other apps share eloop.h */
38 #define	ELOOP_DHCPCD		1 /* default queue */
39 #define	ELOOP_DHCP		2
40 #define	ELOOP_ARP		3
41 #define	ELOOP_IPV4LL		4
42 #define	ELOOP_IPV6		5
43 #define	ELOOP_IPV6ND		6
44 #define	ELOOP_IPV6RA_EXPIRE	7
45 #define	ELOOP_DHCP6		8
46 
47 #ifndef HOSTNAME_MAX_LEN
48 #define HOSTNAME_MAX_LEN	250	/* 255 - 3 (FQDN) - 2 (DNS enc) */
49 #endif
50 
51 #ifndef MIN
52 #define MIN(a,b)		((/*CONSTCOND*/(a)<(b))?(a):(b))
53 #define MAX(a,b)		((/*CONSTCOND*/(a)>(b))?(a):(b))
54 #endif
55 
56 #define UNCONST(a)		((void *)(unsigned long)(const void *)(a))
57 #define STRINGIFY(a)		#a
58 #define TOSTRING(a)		STRINGIFY(a)
59 #define UNUSED(a)		(void)(a)
60 
61 #define ROUNDUP4(a)		(1 + (((a) - 1) |  3))
62 #define ROUNDUP8(a)		(1 + (((a) - 1) |  7))
63 
64 /* Some systems don't define timespec macros */
65 #ifndef timespecclear
66 #define timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
67 #define timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
68 #endif
69 
70 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
71 # ifndef __packed
72 #  define __packed __attribute__((__packed__))
73 # endif
74 # ifndef __unused
75 #  define __unused __attribute__((__unused__))
76 # endif
77 #else
78 # ifndef __packed
79 #  define __packed
80 # endif
81 # ifndef __unused
82 #  define __unused
83 # endif
84 #endif
85 
86 /* Needed for rbtree(3) compat */
87 #ifndef __RCSID
88 #define __RCSID(a)
89 #endif
90 #ifndef __predict_false
91 # if __GNUC__ > 2
92 #  define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
93 #  define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
94 #else
95 #  define	__predict_true(exp)	(exp)
96 #  define	__predict_false(exp)	(exp)
97 # endif
98 #endif
99 #ifndef __BEGIN_DECLS
100 # if defined(__cplusplus)
101 #  define	__BEGIN_DECLS		extern "C" {
102 #  define	__END_DECLS		};
103 # else /* __BEGIN_DECLS */
104 #  define	__BEGIN_DECLS
105 #  define	__END_DECLS
106 # endif /* __BEGIN_DECLS */
107 #endif /* __BEGIN_DECLS */
108 
109 #ifndef __fallthrough
110 # if __GNUC__ >= 7
111 #  define __fallthrough __attribute__((fallthrough))
112 # else
113 #  define __fallthrough
114 # endif
115 #endif
116 
117 /*
118  * Compile Time Assertion.
119  */
120 #ifndef __CTASSERT
121 # ifdef __COUNTER__
122 #   define	__CTASSERT(x)		__CTASSERT0(x, __ctassert, __COUNTER__)
123 # else
124 #  define	__CTASSERT(x)		__CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__)
125 #  define	__CTASSERT99(x, a, b)	__CTASSERT0(x, __CONCAT(__ctassert,a), \
126 					       __CONCAT(_,b))
127 # endif
128 # define	__CTASSERT0(x, y, z)	__CTASSERT1(x, y, z)
129 # define	__CTASSERT1(x, y, z)	typedef char y ## z[/*CONSTCOND*/(x) ? 1 : -1] __unused
130 #endif
131 
132 #ifndef __arraycount
133 #  define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
134 #endif
135 
136 /* We don't really need this as our supported systems define __restrict
137  * automatically for us, but it is here for completeness. */
138 #ifndef __restrict
139 # if defined(__lint__)
140 #  define __restrict
141 # elif __STDC_VERSION__ >= 199901L
142 #  define __restrict restrict
143 # elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__))
144 #  define __restrict
145 # endif
146 #endif
147 
148 const char *hwaddr_ntoa(const void *, size_t, char *, size_t);
149 size_t hwaddr_aton(uint8_t *, const char *);
150 ssize_t readfile(const char *, void *, size_t);
151 ssize_t writefile(const char *, mode_t, const void *, size_t);
152 int filemtime(const char *, time_t *);
153 char *get_line(char ** __restrict, ssize_t * __restrict);
154 int is_root_local(void);
155 #endif
156