xref: /dragonfly/contrib/dhcpcd/src/common.h (revision f984587a)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2023 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 <sys/types.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 
38 /* Define eloop queues here, as other apps share eloop.h */
39 #define	ELOOP_DHCPCD		1 /* default queue */
40 #define	ELOOP_DHCP		2
41 #define	ELOOP_ARP		3
42 #define	ELOOP_IPV4LL		4
43 #define	ELOOP_IPV6		5
44 #define	ELOOP_IPV6ND		6
45 #define	ELOOP_IPV6RA_EXPIRE	7
46 #define	ELOOP_DHCP6		8
47 #define	ELOOP_IF		9
48 
49 #ifndef HOSTNAME_MAX_LEN
50 #define HOSTNAME_MAX_LEN	250	/* 255 - 3 (FQDN) - 2 (DNS enc) */
51 #endif
52 
53 #ifndef MIN
54 #define MIN(a,b)		((/*CONSTCOND*/(a)<(b))?(a):(b))
55 #define MAX(a,b)		((/*CONSTCOND*/(a)>(b))?(a):(b))
56 #endif
57 
58 #define UNCONST(a)		((void *)(unsigned long)(const void *)(a))
59 #define STRINGIFY(a)		#a
60 #define TOSTRING(a)		STRINGIFY(a)
61 #define UNUSED(a)		(void)(a)
62 
63 #define ROUNDUP4(a)		(1 + (((a) - 1) |  3))
64 #define ROUNDUP8(a)		(1 + (((a) - 1) |  7))
65 
66 /* Some systems don't define timespec macros */
67 #ifndef timespecclear
68 #define timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
69 #define timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
70 #endif
71 
72 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
73 # ifndef __packed
74 #  define __packed __attribute__((__packed__))
75 # endif
76 # ifndef __unused
77 #  define __unused __attribute__((__unused__))
78 # endif
79 #else
80 # ifndef __packed
81 #  define __packed
82 # endif
83 # ifndef __unused
84 #  define __unused
85 # endif
86 #endif
87 
88 /* Needed for rbtree(3) compat */
89 #ifndef __RCSID
90 #define __RCSID(a)
91 #endif
92 #ifndef __predict_false
93 # if __GNUC__ > 2
94 #  define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
95 #  define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
96 #else
97 #  define	__predict_true(exp)	(exp)
98 #  define	__predict_false(exp)	(exp)
99 # endif
100 #endif
101 #ifndef __BEGIN_DECLS
102 # if defined(__cplusplus)
103 #  define	__BEGIN_DECLS		extern "C" {
104 #  define	__END_DECLS		};
105 # else /* __BEGIN_DECLS */
106 #  define	__BEGIN_DECLS
107 #  define	__END_DECLS
108 # endif /* __BEGIN_DECLS */
109 #endif /* __BEGIN_DECLS */
110 
111 #ifndef __fallthrough
112 # if __GNUC__ >= 7
113 #  define __fallthrough __attribute__((fallthrough))
114 # else
115 #  define __fallthrough
116 # endif
117 #endif
118 
119 /*
120  * Compile Time Assertion.
121  */
122 #ifndef __CTASSERT
123 # ifdef __COUNTER__
124 #   define	__CTASSERT(x)		__CTASSERT0(x, __ctassert, __COUNTER__)
125 # else
126 #  define	__CTASSERT(x)		__CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__)
127 #  define	__CTASSERT99(x, a, b)	__CTASSERT0(x, __CONCAT(__ctassert,a), \
128 					       __CONCAT(_,b))
129 # endif
130 # define	__CTASSERT0(x, y, z)	__CTASSERT1(x, y, z)
131 # define	__CTASSERT1(x, y, z)	typedef char y ## z[/*CONSTCOND*/(x) ? 1 : -1] __unused
132 #endif
133 
134 #ifndef __arraycount
135 #  define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
136 #endif
137 
138 /* We don't really need this as our supported systems define __restrict
139  * automatically for us, but it is here for completeness. */
140 #ifndef __restrict
141 # if defined(__lint__)
142 #  define __restrict
143 # elif __STDC_VERSION__ >= 199901L
144 #  define __restrict restrict
145 # elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__))
146 #  define __restrict
147 # endif
148 #endif
149 
150 const char *hwaddr_ntoa(const void *, size_t, char *, size_t);
151 size_t hwaddr_aton(uint8_t *, const char *);
152 ssize_t readfile(const char *, void *, size_t);
153 ssize_t writefile(const char *, mode_t, const void *, size_t);
154 int filemtime(const char *, time_t *);
155 char *get_line(char ** __restrict, ssize_t * __restrict);
156 int is_root_local(void);
157 #endif
158