1 /*
2  *  Copyright (c) 2009-2020, Peter Haag
3  *  Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright notice,
12  *     this list of conditions and the following disclaimer in the documentation
13  *     and/or other materials provided with the distribution.
14  *   * Neither the name of the author nor the names of its contributors may be
15  *     used to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  *  POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #ifndef _UTIL_H
33 #define _UTIL_H 1
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #ifdef HAVE_STDINT_H
40 #include <stdint.h>
41 #endif
42 
43 #include <time.h>
44 
45 #ifdef DEVEL
46 #	include <stdio.h>
47 #	include <assert.h>
48 #	define dbg_printf(...) printf(__VA_ARGS__)
49 #	define dbg_assert(a) assert(a)
50 #   define dbg(a) a
51 #else
52 #	define dbg_printf(...) /* printf(__VA_ARGS__) */
53 #	define dbg_assert(a) /* assert(a) */
54 #   define dbg(a) /* a */
55 #endif
56 
57 #define UNUSED(expr) do { (void)(expr); } while (0)
58 
59 #define EBUFF_SIZE 256
60 
61 #ifndef HAVE_HTONLL
62 #ifdef WORDS_BIGENDIAN
63 #	define ntohll(n)	(n)
64 #	define htonll(n)	(n)
65 #else
66 #	define ntohll(n)	(((uint64_t)ntohl(n)) << 32) + ntohl((n) >> 32)
67 #	define htonll(n)	(((uint64_t)htonl(n)) << 32) + htonl((n) >> 32)
68 #endif
69 #endif
70 
71 #if ( SIZEOF_VOID_P == 8 )
72 typedef uint64_t	pointer_addr_t;
73 #else
74 typedef uint32_t	pointer_addr_t;
75 #endif
76 
77 #define _1KB (double)(1000.0)
78 #define _1MB (double)(1000.0 * 1000.0)
79 #define _1GB (double)(1000.0 * 1000.0 * 1000.0)
80 #define _1TB (double)(1000.0 * 1000.0 * 1000.0 * 1000.0)
81 
82 #define SetFlag(var, flag) 		(var |= flag)
83 #define ClearFlag(var, flag) 	(var &= ~flag)
84 #define TestFlag(var, flag)		(var & flag)
85 
86 
87 typedef struct stringlist_s {
88 	uint32_t	block_size;
89 	uint32_t	max_index;
90 	uint32_t	num_strings;
91 	char		**list;
92 } stringlist_t;
93 
94 
95 void xsleep(long sec);
96 
97 void EndLog(void);
98 
99 int InitLog(int use_syslog, char *name, char *facility, int verbose_log);
100 
101 void LogError(char *format, ...);
102 
103 void LogInfo(char *format, ...);
104 
105 void InitStringlist(stringlist_t *list, int block_size);
106 
107 void InsertString(stringlist_t *list, char *string);
108 
109 int ScanTimeFrame(char *tstring, time_t *t_start, time_t *t_end);
110 
111 char *TimeString(time_t start, time_t end);
112 
113 char *UNIX2ISO(time_t t);
114 
115 time_t ISO2UNIX(char *timestring);
116 
117 #define NUMBER_STRING_SIZE	32
118 #define FIXED_WIDTH 	  1
119 #define VAR_LENGTH  	  0
120 void format_number(uint64_t num, char *s, int printPlain, int fixed_width);
121 
122 void SetupInputFileSequence(char *multiple_dirs, char *single_file, char *multiple_files);
123 
124 char *GetCurrentFilename(void);
125 
126 void Setv6Mode(int mode);
127 
128 void inet_ntop_mask(uint32_t ipv4, int mask, char *s, size_t sSize);
129 
130 void inet6_ntop_mask(uint64_t ipv6[2], int mask, char *s, size_t sSize);
131 
132 #endif //_UTIL_H
133