1 #ifndef COMMON_H
2 #define COMMON_H
3 
4 #include <config.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <unistd.h>
9 #include <locale.h>
10 #include <time.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <dirent.h>
15 #include <ctype.h>
16 #include <signal.h>
17 #include <math.h>
18 #include <errno.h>
19 #include <sys/file.h>
20 #include <inttypes.h>
21 #include <syslog.h>
22 #include <sys/statvfs.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <libgen.h>
26 #include <fcntl.h>
27 #include <sys/time.h>
28 
29 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
30 #include <sys/param.h>
31 #include <sys/mount.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 #include <net/if.h>
35 #include <ifaddrs.h>
36 #define BSD_VNSTAT
37 #endif
38 
39 /* OpenBSD and NetBSD don't support the ' character (decimal conversion) in printf formatting */
40 #if !defined(__OpenBSD__) && !defined(__NetBSD__)
41 #define DECCONV "'"
42 #else
43 #define DECCONV
44 #endif
45 
46 /* used in debug to get function name */
47 #if __STDC_VERSION__ < 199901L
48 #if __GNUC__ >= 2
49 #define __func__ __FUNCTION__
50 #else
51 #define __func__ "function"
52 #endif
53 #endif
54 
55 /*
56 
57 Note! These are only the default values for settings
58 and most can be changed later from the config file.
59 
60 */
61 
62 /* location of the database directory */
63 #ifndef DATABASEDIR
64 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__DragonFly__)
65 #define DATABASEDIR "/var/db/vnstat"
66 #else
67 #define DATABASEDIR "/var/lib/vnstat"
68 #endif
69 #endif
70 
71 /* database file name */
72 #define DATABASEFILE "vnstat.db"
73 
74 /* modifier used for sqlite date and time functions */
75 #define DATABASELOCALTIMEMODIFIER ", 'localtime'"
76 
77 /* on which day should months change */
78 #define MONTHROTATE 1
79 #define MONTHROTATEYEARS 0
80 
81 /* date output formats for -d, -m, -t and image header*/
82 /* see 'man date' for control codes      1.x values     <1.8 values */
83 #define DFORMAT "%Y-%m-%d"		 /* "%x"         "%d.%m." */
84 #define MFORMAT "%Y-%m"			 /* "%b '%y"     "%b '%y" */
85 #define TFORMAT "%Y-%m-%d"		 /* "%x"         "%d.%m.%y" */
86 #define HFORMAT "%Y-%m-%d %H:%M" /* "%x %H:%M"   "%d.%m.%Y %H:%M" */
87 
88 #ifndef DATETIMEFORMAT
89 #define DATETIMEFORMAT "%Y-%m-%d %H:%M:%S"
90 #endif
91 
92 /* characters used for visuals */
93 #define RXCHAR "%"
94 #define TXCHAR ":"
95 #define RXHOURCHAR "r"
96 #define TXHOURCHAR "t"
97 
98 /* unit mode */
99 /* 0 = KiB/MiB/GiB/TiB, 1 = KB/MB/GB/TB */
100 #define UNITMODE 0
101 
102 /* rate unit mode */
103 /* 0 = Kibit/s..., 1 = kbit/s... */
104 #define RATEUNITMODE 1
105 
106 /* output style */
107 /* 0 = minimal/narrow, 1 = bars everywhere */
108 /* 2 = same as 1 + rate in summary */
109 /* 3 = rate everywhere */
110 #define OSTYLE 3
111 #define ESTIMATEBARVISIBLE 1
112 
113 /* rate in vnstati summary output */
114 #define SUMMARYRATE 1
115 
116 /* rate in vnstati hourly output */
117 #define HOURLYRATE 1
118 
119 /* rate unit */
120 /* 0 = bytes, 1 = bits */
121 #define RATEUNIT 1
122 
123 /* number of decimals */
124 #define DEFAULTDECIMALS 2
125 #define HOURLYDECIMALS 1
126 
127 /* hourly section style */
128 #define HOURLYSTYLE 2
129 
130 /* default interface */
131 #ifndef DEFIFACE
132 #define DEFIFACE ""
133 #endif
134 
135 /* default locale */
136 #define LOCALE "-"
137 
138 /* bandwidth detection, 0 = feature disabled */
139 #define BWDETECT 1
140 #define BWDETECTINTERVAL 5
141 
142 /* default maximum bandwidth (Mbit) for all interfaces */
143 /* 0 = feature disabled */
144 #define DEFMAXBW 1000
145 
146 /* maximum allowed config value for bandwidth */
147 #define BWMAX 50000
148 
149 /* how many seconds should sampling take by default */
150 #define DEFSAMPTIME 5
151 
152 /* default query mode */
153 /* 0 = normal, 1 = days, 2 = months, 3 = top, 5 = short */
154 /* 7 = hours, 8 = xml, 9 = one line, 10 = json */
155 #define DEFQMODE 0
156 
157 /* how much the boot time can variate between updates (seconds) */
158 #define BVAR 15
159 
160 /* check disk space by default */
161 #define USESPACECHECK 1
162 
163 /* create trafficless entries by default */
164 #define TRAFFICLESSENTRIES 1
165 
166 /* list outputs */
167 #define LISTFIVEMINS 24
168 #define LISTHOURS 24
169 #define LISTDAYS 30
170 #define LISTMONTHS 12
171 #define LISTYEARS 0
172 #define LISTTOP 10
173 #define LISTJSONXML 0
174 
175 /* data retention defaults */
176 #define FIVEMINUTEHOURS 48
177 #define HOURLYDAYS 4
178 #define DAILYDAYS 62
179 #define MONTHLYMONTHS 25
180 #define YEARLYYEARS -1
181 #define TOPDAYENTRIES 20
182 
183 /* assume that locale can be UTF-n when enabled */
184 #define UTFLOCALE 1
185 
186 /* 1 = 2.0 */
187 #define SQLDBVERSION "1"
188 
189 /* xml format version */
190 /* 1 = 1.7-1.16, 2 = 2.0 */
191 #define XMLVERSION 2
192 
193 /* json format version */
194 /* 1 = 1.13-1.16, 2 = 2.0 */
195 #define JSONVERSION 2
196 
197 /* json format version, -tr */
198 /* 1 = 1.18- */
199 #define JSONVERSION_TR 1
200 
201 /* json format version, --live */
202 /* 1 = 1.18- */
203 #define JSONVERSION_LIVE 1
204 
205 /* --oneline format version */
206 #define ONELINEVERSION 1
207 
208 /* integer limits */
209 #define MAX32 4294967295ULL
210 #define MAX64 18446744073709551615ULL
211 
212 /* sampletime in seconds for live traffic */
213 /* don't use values below 2 */
214 #define LIVETIME 2
215 
216 /* /proc/net/dev */
217 #ifndef PROCNETDEV
218 #define PROCNETDEV "/proc/net/dev"
219 #endif
220 
221 /* /sys/class/net */
222 #ifndef SYSCLASSNET
223 #define SYSCLASSNET "/sys/class/net"
224 #endif
225 
226 /* daemon defaults */
227 #define UPDATEINTERVAL 20
228 #define TIMESYNCWAIT 5
229 #define POLLINTERVAL 5
230 #define SAVEINTERVAL 5
231 #define OFFSAVEINTERVAL 30
232 #define RESCANONSAVE 1
233 #define ALWAYSADD 0
234 #define SAVESTATUS 1
235 #define USELOGGING 2
236 #define CREATEDIRS 1
237 #define UPDATEFILEOWNER 1
238 #define LOGFILE "/var/log/vnstat/vnstat.log"
239 #define PIDFILE "/var/run/vnstat/vnstat.pid"
240 #define IS64BIT -2
241 #define WALDB 0
242 #define WALDBCHECKPOINTINTERVALMINS 240
243 #define SLOWDBWARNLIMIT 4.0 // needs to be less than DBREADTIMEOUTSECS
244 #define DBSYNCHRONOUS -1
245 #define USEUTC 0
246 
247 /* database read timeout */
248 #define DBREADTIMEOUTSECS 5
249 
250 /* no transparency by default */
251 #define TRANSBG 0
252 
253 /* small fonts by default */
254 #define LARGEFONTS 0
255 
256 /* no extra space between lines by default */
257 #define LINESPACEADJUST 0
258 
259 /* no image scaling by default */
260 #define IMAGESCALE 100
261 
262 /* image output estimate bar style */
263 /* 0 = not shown, 1 = continuation of existing bar, 2 = separate bar */
264 #define ESTIMATESTYLE 1
265 
266 /* bar column in list outputs shows rate when rate column is visible */
267 #define BARSHOWSRATE 0
268 
269 /* 5 minute graph size */
270 #define FIVEGRESULTCOUNT 576
271 #define FIVEGHEIGHT 300
272 #define FIVEGMINRESULTCOUNT 288
273 #define FIVEGMINHEIGHT 150
274 
275 /* graph used in extended summary, 0 = hours, 1 = 5 minutes*/
276 #define SUMMARYGRAPH 0
277 
278 /* default colors */
279 #define CBACKGROUND "FFFFFF"
280 #define CEDGE "AEAEAE"
281 #define CHEADER "606060"
282 #define CHEADERTITLE "FFFFFF"
283 #define CHEADERDATE "FFFFFF"
284 #define CTEXT "000000"
285 #define CLINE "B0B0B0"
286 #define CLINEL "-"
287 #define CRX "92CF00"
288 #define CRXD "-"
289 #define CTX "606060"
290 #define CTXD "-"
291 
292 /* number of retries after non-fatal database errors, */
293 /* will result in given number + 1 tries in total before exit, */
294 /* a full disk (as reported by sqlite) will no cause retries or exit */
295 #define DBRETRYLIMIT 5
296 
297 /* internal config structure */
298 typedef struct {
299 	char dformat[64], mformat[64], tformat[64], hformat[64];
300 	char iface[32];
301 	char locale[32];
302 	char dbdir[512], dbtzmodifier[14];
303 	char rxchar[2], txchar[2], rxhourchar[2], txhourchar[2];
304 	char cbg[8], cedge[8], cheader[8], cheadertitle[8], cheaderdate[8], ctext[8];
305 	char cline[8], clinel[8], cvnstat[8], crx[8], crxd[8], ctx[8], ctxd[8];
306 	int32_t unitmode, rateunitmode, rateunit, bvar, qmode, sampletime, hourlyrate, summaryrate;
307 	int32_t monthrotate, monthrotateyears, maxbw, spacecheck, trafficlessentries, transbg, ostyle;
308 	int32_t defaultdecimals, hourlydecimals, hourlystyle, is64bit, waldb, dbsynchronous, useutc, imagescale;
309 	int32_t largefonts, linespaceadjust, estimatebarvisible, estimatestyle, barshowsrate, fivegresultcount;
310 	int32_t fivegheight, summarygraph, alwaysadd;
311 	char cfgfile[512], logfile[512], pidfile[512];
312 	char daemonuser[33], daemongroup[33];
313 	int32_t timesyncwait, updateinterval, pollinterval, saveinterval, offsaveinterval, rescanonsave, savestatus;
314 	int32_t uselogging, createdirs, updatefileowner, bwdetection, bwdetectioninterval, utflocale;
315 	int32_t fiveminutehours, hourlydays, dailydays, monthlymonths, yearlyyears, topdayentries;
316 	int32_t listfivemins, listhours, listdays, listmonths, listyears, listtop, listjsonxml;
317 	int32_t experimental;
318 } CFG;
319 
320 /* internal interface information structure */
321 typedef struct {
322 	char name[32];
323 	short filled;
324 	short is64bit;
325 	uint64_t rx;
326 	uint64_t tx;
327 	uint64_t rxp;
328 	uint64_t txp;
329 	time_t timestamp;
330 } IFINFO;
331 
332 typedef struct ibwnode {
333 	char interface[32];
334 	uint32_t limit;
335 	uint32_t fallback;
336 	short retries;
337 	time_t detected;
338 	struct ibwnode *next;
339 } ibwnode;
340 
341 typedef enum PrintType {
342 	PT_Info = 0,
343 	PT_Infoless,
344 	PT_Warning,
345 	PT_Error,
346 	PT_Config,
347 	PT_Multiline,
348 	PT_ShortMultiline
349 } PrintType;
350 
351 /* common functions */
352 int printe(const PrintType type);
353 int logprint(const PrintType type);
354 int verifylogaccess(void);
355 int dmonth(const int month);
356 int isleapyear(const int year);
357 time_t mosecs(time_t month, time_t updated);
358 uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit);
359 char *strncpy_nt(char *dest, const char *src, size_t n);
360 int isnumeric(const char *s);
361 void panicexit(const char *sourcefile, const int sourceline) __attribute__((noreturn));
362 char *getversion(void);
363 double timeused(const char *func, const int reset);
364 void timeused_debug(const char *func, const int reset);
365 
366 /* global variables */
367 extern CFG cfg;
368 extern IFINFO ifinfo;
369 extern char errorstring[1024];
370 extern ibwnode *ifacebw;
371 extern int debug;
372 extern int noexit; /* = running as daemon if 2 */
373 extern int intsignal;
374 extern int pidfile;
375 extern int disableprints;
376 
377 #endif
378