1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 *)(uintptr_t)(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 #define timespecadd(tsp, usp, vsp) \ 71 do { \ 72 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ 73 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ 74 if ((vsp)->tv_nsec >= 1000000000L) { \ 75 (vsp)->tv_sec++; \ 76 (vsp)->tv_nsec -= 1000000000L; \ 77 } \ 78 } while (0) 79 #define timespecsub(tsp, usp, vsp) \ 80 do { \ 81 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ 82 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ 83 if ((vsp)->tv_nsec < 0) { \ 84 (vsp)->tv_sec--; \ 85 (vsp)->tv_nsec += 1000000000L; \ 86 } \ 87 } while (0) 88 #endif 89 90 #if __GNUC__ > 2 || defined(__INTEL_COMPILER) 91 # ifndef __unused 92 # define __unused __attribute__((__unused__)) 93 # endif 94 #else 95 # ifndef __unused 96 # define __unused 97 # endif 98 #endif 99 100 /* Needed for rbtree(3) compat */ 101 #ifndef __RCSID 102 #define __RCSID(a) 103 #endif 104 #ifndef __predict_false 105 # if __GNUC__ > 2 106 # define __predict_true(exp) __builtin_expect((exp) != 0, 1) 107 # define __predict_false(exp) __builtin_expect((exp) != 0, 0) 108 #else 109 # define __predict_true(exp) (exp) 110 # define __predict_false(exp) (exp) 111 # endif 112 #endif 113 #ifndef __BEGIN_DECLS 114 # if defined(__cplusplus) 115 # define __BEGIN_DECLS extern "C" { 116 # define __END_DECLS }; 117 # else /* __BEGIN_DECLS */ 118 # define __BEGIN_DECLS 119 # define __END_DECLS 120 # endif /* __BEGIN_DECLS */ 121 #endif /* __BEGIN_DECLS */ 122 123 #ifndef __fallthrough 124 # if __GNUC__ >= 7 125 # define __fallthrough __attribute__((fallthrough)) 126 # else 127 # define __fallthrough 128 # endif 129 #endif 130 131 /* 132 * Compile Time Assertion. 133 */ 134 #ifndef __CTASSERT 135 # ifdef __COUNTER__ 136 # define __CTASSERT(x) __CTASSERT0(x, __ctassert, __COUNTER__) 137 # else 138 # define __CTASSERT(x) __CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__) 139 # define __CTASSERT99(x, a, b) __CTASSERT0(x, __CONCAT(__ctassert,a), \ 140 __CONCAT(_,b)) 141 # endif 142 # define __CTASSERT0(x, y, z) __CTASSERT1(x, y, z) 143 # define __CTASSERT1(x, y, z) typedef char y ## z[/*CONSTCOND*/(x) ? 1 : -1] __unused 144 #endif 145 146 #ifndef __arraycount 147 # define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 148 #endif 149 150 /* We don't really need this as our supported systems define __restrict 151 * automatically for us, but it is here for completeness. */ 152 #ifndef __restrict 153 # if defined(__lint__) 154 # define __restrict 155 # elif __STDC_VERSION__ >= 199901L 156 # define __restrict restrict 157 # elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__)) 158 # define __restrict 159 # endif 160 #endif 161 162 #define INFINITE_LIFETIME (~0U) 163 164 const char *hwaddr_ntoa(const void *, size_t, char *, size_t); 165 size_t hwaddr_aton(uint8_t *, const char *); 166 ssize_t readfile(const char *, void *, size_t); 167 ssize_t writefile(const char *, mode_t, const void *, size_t); 168 int filemtime(const char *, time_t *); 169 char *get_line(char ** __restrict, ssize_t * __restrict); 170 int is_root_local(void); 171 uint32_t lifetime_left(uint32_t, const struct timespec *, struct timespec *); 172 #endif 173