1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2019 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 #ifndef HOSTNAME_MAX_LEN 38 #define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */ 39 #endif 40 41 #ifndef MIN 42 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b)) 43 #define MAX(a,b) ((/*CONSTCOND*/(a)>(b))?(a):(b)) 44 #endif 45 46 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) 47 #define STRINGIFY(a) #a 48 #define TOSTRING(a) STRINGIFY(a) 49 #define UNUSED(a) (void)(a) 50 51 #define ROUNDUP4(a) (1 + (((a) - 1) | 3)) 52 #define ROUNDUP8(a) (1 + (((a) - 1) | 7)) 53 54 #define USEC_PER_SEC 1000000L 55 #define USEC_PER_NSEC 1000L 56 #define NSEC_PER_SEC 1000000000L 57 #define NSEC_PER_MSEC 1000000L 58 #define MSEC_PER_SEC 1000L 59 #define CSEC_PER_SEC 100L 60 #define NSEC_PER_CSEC 10000000L 61 62 /* Some systems don't define timespec macros */ 63 #ifndef timespecclear 64 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L) 65 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec) 66 #define timespeccmp(tsp, usp, cmp) \ 67 (((tsp)->tv_sec == (usp)->tv_sec) ? \ 68 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \ 69 ((tsp)->tv_sec cmp (usp)->tv_sec)) 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 (/* CONSTCOND */ 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 (/* CONSTCOND */ 0) 88 #endif 89 90 #define timespec_to_double(tv) \ 91 ((double)(tv)->tv_sec + (double)((tv)->tv_nsec) / 1000000000.0) 92 #define timespecnorm(tv) do { \ 93 while ((tv)->tv_nsec >= NSEC_PER_SEC) { \ 94 (tv)->tv_sec++; \ 95 (tv)->tv_nsec -= NSEC_PER_SEC; \ 96 } \ 97 } while (0 /* CONSTCOND */); 98 #define ts_to_ms(ms, tv) do { \ 99 ms = (tv)->tv_sec * MSEC_PER_SEC; \ 100 ms += (tv)->tv_nsec / NSEC_PER_MSEC; \ 101 } while (0 /* CONSTCOND */); 102 #define ms_to_ts(tv, ms) do { \ 103 (tv)->tv_sec = ms / MSEC_PER_SEC; \ 104 (tv)->tv_nsec = (suseconds_t)(ms - ((tv)->tv_sec * MSEC_PER_SEC)) \ 105 * NSEC_PER_MSEC; \ 106 } while (0 /* CONSTCOND */); 107 108 #ifndef TIMEVAL_TO_TIMESPEC 109 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ 110 (ts)->tv_sec = (tv)->tv_sec; \ 111 (ts)->tv_nsec = (tv)->tv_usec * USEC_PER_NSEC; \ 112 } while (0 /* CONSTCOND */) 113 #endif 114 115 #if __GNUC__ > 2 || defined(__INTEL_COMPILER) 116 # ifndef __packed 117 # define __packed __attribute__((__packed__)) 118 # endif 119 # ifndef __unused 120 # define __unused __attribute__((__unused__)) 121 # endif 122 #else 123 # ifndef __packed 124 # define __packed 125 # endif 126 # ifndef __unused 127 # define __unused 128 # endif 129 #endif 130 131 /* Needed for rbtree(3) compat */ 132 #ifndef __RCSID 133 #define __RCSID(a) 134 #endif 135 #ifndef __predict_false 136 # if __GNUC__ > 2 137 # define __predict_true(exp) __builtin_expect((exp) != 0, 1) 138 # define __predict_false(exp) __builtin_expect((exp) != 0, 0) 139 #else 140 # define __predict_true(exp) (exp) 141 # define __predict_false(exp) (exp) 142 # endif 143 #endif 144 #ifndef __BEGIN_DECLS 145 # if defined(__cplusplus) 146 # define __BEGIN_DECLS extern "C" { 147 # define __END_DECLS }; 148 # else /* __BEGIN_DECLS */ 149 # define __BEGIN_DECLS 150 # define __END_DECLS 151 # endif /* __BEGIN_DECLS */ 152 #endif /* __BEGIN_DECLS */ 153 154 #ifndef __fallthrough 155 # if __GNUC__ >= 7 156 # define __fallthrough __attribute__((fallthrough)) 157 # else 158 # define __fallthrough 159 # endif 160 #endif 161 162 /* 163 * Compile Time Assertion. 164 */ 165 #ifndef __CTASSERT 166 # ifdef __COUNTER__ 167 # define __CTASSERT(x) __CTASSERT0(x, __ctassert, __COUNTER__) 168 # else 169 # define __CTASSERT(x) __CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__) 170 # define __CTASSERT99(x, a, b) __CTASSERT0(x, __CONCAT(__ctassert,a), \ 171 __CONCAT(_,b)) 172 # endif 173 # define __CTASSERT0(x, y, z) __CTASSERT1(x, y, z) 174 # define __CTASSERT1(x, y, z) typedef char y ## z[/*CONSTCOND*/(x) ? 1 : -1] __unused 175 #endif 176 177 #ifndef __arraycount 178 # define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 179 #endif 180 181 /* We don't really need this as our supported systems define __restrict 182 * automatically for us, but it is here for completeness. */ 183 #ifndef __restrict 184 # if defined(__lint__) 185 # define __restrict 186 # elif __STDC_VERSION__ >= 199901L 187 # define __restrict restrict 188 # elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__)) 189 # define __restrict 190 # endif 191 #endif 192 193 void get_line_free(void); 194 extern int clock_monotonic; 195 int get_monotonic(struct timespec *); 196 197 const char *hwaddr_ntoa(const void *, size_t, char *, size_t); 198 size_t hwaddr_aton(uint8_t *, const char *); 199 size_t read_hwaddr_aton(uint8_t **, const char *); 200 int is_root_local(void); 201 #endif 202