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