xref: /minix/external/bsd/dhcpcd/dist/common.h (revision 9f20bfa6)
1 /* $NetBSD: common.h,v 1.10 2015/07/09 10:15:34 roy Exp $ */
2 
3 /*
4  * dhcpcd - DHCP client daemon
5  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
6  * All rights reserved
7 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef COMMON_H
31 #define COMMON_H
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <stdio.h>
36 #include <syslog.h>
37 
38 #include "config.h"
39 #include "defs.h"
40 #include "dhcpcd.h"
41 
42 #ifndef HOSTNAME_MAX_LEN
43 #define HOSTNAME_MAX_LEN	250	/* 255 - 3 (FQDN) - 2 (DNS enc) */
44 #endif
45 
46 #ifndef MIN
47 #define MIN(a,b)		((/*CONSTCOND*/(a)<(b))?(a):(b))
48 #define MAX(a,b)		((/*CONSTCOND*/(a)>(b))?(a):(b))
49 #endif
50 
51 #define UNCONST(a)		((void *)(unsigned long)(const void *)(a))
52 #define STRINGIFY(a)		#a
53 #define TOSTRING(a)		STRINGIFY(a)
54 #define UNUSED(a)		(void)(a)
55 
56 #define USEC_PER_SEC		1000000L
57 #define USEC_PER_NSEC		1000L
58 #define NSEC_PER_SEC		1000000000L
59 #define NSEC_PER_MSEC		1000000L
60 #define MSEC_PER_SEC		1000L
61 #define CSEC_PER_SEC		100L
62 #define NSEC_PER_CSEC		10000000L
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 #define timespeccmp(tsp, usp, cmp)                                      \
69         (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
70             ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
71             ((tsp)->tv_sec cmp (usp)->tv_sec))
72 #define timespecadd(tsp, usp, vsp)                                      \
73         do {                                                            \
74                 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
75                 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
76                 if ((vsp)->tv_nsec >= 1000000000L) {                    \
77                         (vsp)->tv_sec++;                                \
78                         (vsp)->tv_nsec -= 1000000000L;                  \
79                 }                                                       \
80         } while (/* CONSTCOND */ 0)
81 #define timespecsub(tsp, usp, vsp)                                      \
82         do {                                                            \
83                 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
84                 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
85                 if ((vsp)->tv_nsec < 0) {                               \
86                         (vsp)->tv_sec--;                                \
87                         (vsp)->tv_nsec += 1000000000L;                  \
88                 }                                                       \
89         } while (/* CONSTCOND */ 0)
90 #endif
91 
92 #define timespec_to_double(tv)						     \
93 	((double)(tv)->tv_sec + (double)((tv)->tv_nsec) / 1000000000.0)
94 #define timespecnorm(tv) do {						     \
95 	while ((tv)->tv_nsec >=  NSEC_PER_SEC) {			     \
96 		(tv)->tv_sec++;						     \
97 		(tv)->tv_nsec -= NSEC_PER_SEC;				     \
98 	}								     \
99 } while (0 /* CONSTCOND */);
100 #define ts_to_ms(ms, tv) do {						     \
101 	ms = (tv)->tv_sec * MSEC_PER_SEC;				     \
102 	ms += (tv)->tv_nsec / NSEC_PER_MSEC;				     \
103 } while (0 /* CONSTCOND */);
104 #define ms_to_ts(tv, ms) do {						     \
105 	(tv)->tv_sec = ms / MSEC_PER_SEC;				     \
106 	(tv)->tv_nsec = (suseconds_t)(ms - ((tv)->tv_sec * MSEC_PER_SEC))    \
107 	    * NSEC_PER_MSEC;						     \
108 } while (0 /* CONSTCOND */);
109 
110 #ifndef TIMEVAL_TO_TIMESPEC
111 #define	TIMEVAL_TO_TIMESPEC(tv, ts) do {				\
112 	(ts)->tv_sec = (tv)->tv_sec;					\
113 	(ts)->tv_nsec = (tv)->tv_usec * USEC_PER_NSEC;			\
114 } while (0 /* CONSTCOND */)
115 #endif
116 
117 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
118 # ifndef __dead
119 #  define __dead __attribute__((__noreturn__))
120 # endif
121 # ifndef __packed
122 #  define __packed   __attribute__((__packed__))
123 # endif
124 # ifndef __printflike
125 #  define __printflike(a, b) __attribute__((format(printf, a, b)))
126 # endif
127 # ifndef __unused
128 #  define __unused   __attribute__((__unused__))
129 # endif
130 #else
131 # ifndef __dead
132 #  define __dead
133 # endif
134 # ifndef __packed
135 #  define __packed
136 # endif
137 # ifndef __printflike
138 #  define __printflike
139 # endif
140 # ifndef __unused
141 #  define __unused
142 # endif
143 #endif
144 
145 #ifndef __arraycount
146 #define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
147 #endif
148 
149 /* We don't really need this as our supported systems define __restrict
150  * automatically for us, but it is here for completeness. */
151 #ifndef __restrict
152 # if defined(__lint__)
153 #  define __restrict
154 # elif __STDC_VERSION__ >= 199901L
155 #  define __restrict restrict
156 # elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__))
157 #  define __restrict
158 # endif
159 #endif
160 
161 void get_line_free(void);
162 const char *get_hostname(char *, size_t, int);
163 extern int clock_monotonic;
164 int get_monotonic(struct timespec *);
165 
166 /* We could shave a few k off the binary size by just using the
167  * syslog(3) interface.
168  * However, this results in a ugly output on the command line
169  * and relies on syslogd(8) starting before dhcpcd which is not
170  * always the case. */
171 #ifndef USE_LOGFILE
172 # define USE_LOGFILE 1
173 #endif
174 #if USE_LOGFILE
175 void logger_open(struct dhcpcd_ctx *);
176 #define logger_mask(ctx, lvl) setlogmask((lvl))
177 __printflike(3, 4) void logger(struct dhcpcd_ctx *, int, const char *, ...);
178 void logger_close(struct dhcpcd_ctx *);
179 #else
180 #define logger_open(ctx) openlog(PACKAGE, LOG_PERROR | LOG_PID, LOG_DAEMON)
181 #define logger_mask(ctx, lvl) setlogmask((lvl))
182 #define logger(ctx, pri, fmt, ...)			\
183 	do {						\
184 		UNUSED((ctx));				\
185 		syslog((pri), (fmt), ##__VA_ARGS__);	\
186 	} while (0 /*CONSTCOND */)
187 #define logger_close(ctx) closelog()
188 #endif
189 
190 ssize_t setvar(struct dhcpcd_ctx *,
191     char **, const char *, const char *, const char *);
192 ssize_t setvard(struct dhcpcd_ctx *,
193     char **, const char *, const char *, size_t);
194 ssize_t addvar(struct dhcpcd_ctx *,
195     char ***, const char *, const char *, const char *);
196 ssize_t addvard(struct dhcpcd_ctx *,
197     char ***, const char *, const char *, size_t);
198 
199 char *hwaddr_ntoa(const unsigned char *, size_t, char *, size_t);
200 size_t hwaddr_aton(unsigned char *, const char *);
201 #endif
202