1 /**
2  *    ______      ___
3  *   / ____/___  /   | _____________  __________
4  *  / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
5  * / /_/ / /_/ / ___ / /__/ /__/  __(__  |__  )
6  * \____/\____/_/  |_\___/\___/\___/____/____/
7  *
8  * The MIT License (MIT)
9  * Copyright (c) 2009-2020 Gerardo Orellana <hello @ goaccess.io>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  */
29 
30 #if HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #ifndef COMMONS_H_INCLUDED
35 #define COMMONS_H_INCLUDED
36 
37 #include <time.h>
38 #include <stdint.h>
39 #include "gslist.h"
40 
41 /* Remove the __attribute__ stuff when the compiler is not GCC. */
42 #if !__GNUC__
43 #define __attribute__(x) /**/
44 #endif
45 #define GO_UNUSED __attribute__((unused))
46 #define GO_VERSION 		"1.4.6"
47 #define GO_WEBSITE 		"https://goaccess.io/"
48 extern struct tm now_tm;
49 
50 /* common char array buffer size */
51 #define INIT_BUF_SIZE 1024
52 
53 /* total number of modules */
54 #ifdef HAVE_GEOLOCATION
55 #define TOTAL_MODULES    18
56 #else
57 #define TOTAL_MODULES    17
58 #endif
59 
60 /* maximum number of items within a panel */
61 #define MAX_CHOICES      366
62 /* real-time */
63 #define MAX_CHOICES_RT    50
64 
65 /* date and time length - e.g., 2016/12/12 12:12:12 -0600 */
66 #define DATE_TIME     25 + 1
67 /* date length -  e.g., 2016/12/12 */
68 #define DATE_LEN      10 + 1
69 /* date length -  e.g., 12:12:12 */
70 #define TIME_LEN       8 + 1
71 /* hour + ':' + min length - e.g., 12:12 */
72 #define HRMI_LEN   4 + 1 + 1
73 
74 #define YR_FMT "%Y"
75 #define MO_FMT "%M"
76 #define DT_FMT "%d"
77 
78 /* maximum protocol string length */
79 #define REQ_PROTO_LEN     9
80 
81 #define IGNORE_LEVEL_PANEL 1
82 #define IGNORE_LEVEL_REQ 2
83 
84 /* Type of IP */
85 typedef enum {
86   TYPE_IPINV,
87   TYPE_IPV4,
88   TYPE_IPV6
89 } GTypeIP;
90 
91 /* Type of Modules */
92 typedef enum MODULES {
93   VISITORS,
94   REQUESTS,
95   REQUESTS_STATIC,
96   NOT_FOUND,
97   HOSTS,
98   OS,
99   BROWSERS,
100   VISIT_TIMES,
101   VIRTUAL_HOSTS,
102   REFERRERS,
103   REFERRING_SITES,
104   KEYPHRASES,
105   STATUS_CODES,
106   REMOTE_USER,
107   CACHE_STATUS,
108 #ifdef HAVE_GEOLOCATION
109   GEO_LOCATION,
110 #endif
111   MIME_TYPE,
112   TLS_TYPE,
113 } GModule;
114 
115 /* Metric totals. These are metrics that have a percent value and are
116  * calculated values. */
117 typedef struct GPercTotals_ {
118   uint32_t hits;                /* total valid hits */
119   uint32_t visitors;            /* total visitors */
120   uint64_t bw;                  /* total bandwidth */
121 } GPercTotals;
122 
123 /* Metrics within GHolder or GDashData */
124 typedef struct GMetrics {
125   /* metric id can be used to identify
126    * a specific data field */
127   uint8_t id;
128   char *data;
129   char *method;
130   char *protocol;
131 
132   float hits_perc;
133   float visitors_perc;
134   float bw_perc;
135 
136   uint64_t hits;
137   uint64_t visitors;
138 
139   /* holder has a numeric value, while
140    * dashboard has a displayable string value */
141   union {
142     char *sbw;
143     uint64_t nbw;
144   } bw;
145 
146   /* holder has a numeric value, while
147    * dashboard has a displayable string value */
148   union {
149     char *sts;
150     uint64_t nts;
151   } avgts;
152 
153   /* holder has a numeric value, while
154    * dashboard has a displayable string value */
155   union {
156     char *sts;
157     uint64_t nts;
158   } cumts;
159 
160   /* holder has a numeric value, while
161    * dashboard has a displayable string value */
162   union {
163     char *sts;
164     uint64_t nts;
165   } maxts;
166 } GMetrics;
167 
168 /* Holder sub item */
169 typedef struct GSubItem_ {
170   GModule module;
171   GMetrics *metrics;
172   struct GSubItem_ *prev;
173   struct GSubItem_ *next;
174 } GSubItem;
175 
176 /* Double linked-list of sub items */
177 typedef struct GSubList_ {
178   int size;
179   struct GSubItem_ *head;
180   struct GSubItem_ *tail;
181 } GSubList;
182 
183 /* Holder item */
184 typedef struct GHolderItem_ {
185   GSubList *sub_list;
186   GMetrics *metrics;
187 } GHolderItem;
188 
189 /* Holder of GRawData */
190 typedef struct GHolder_ {
191   GHolderItem *items;           /* holder items */
192   GModule module;               /* current module  */
193   int idx;                      /* holder index  */
194   int holder_size;              /* number of allocated items */
195   uint32_t ht_size;             /* size of the hash table/store */
196   int sub_items_size;           /* number of sub items  */
197 } GHolder;
198 
199 /* Enum-to-string */
200 typedef struct GEnum_ {
201   const char *str;
202   int idx;
203 } GEnum;
204 
205 /* A metric can contain a root/data/uniq node id */
206 typedef struct GDataMap_ {
207   int data;
208   int root;
209 } GDataMap;
210 
211 typedef struct GAgentItem_ {
212   char *agent;
213 } GAgentItem;
214 
215 typedef struct GAgents_ {
216   int size;
217   int idx;
218   struct GAgentItem_ *items;
219 } GAgents;
220 
221 #define FOREACH_MODULE(item, array) \
222   for (; (item < ARRAY_SIZE(array)) && array[item] != -1; ++item)
223 
224 /* Processing time */
225 extern time_t end_proc;
226 extern time_t timestamp;
227 extern time_t start_proc;
228 
229 /* list of available modules/panels */
230 extern int module_list[TOTAL_MODULES];
231 
232 /* *INDENT-OFF* */
233 GAgents *new_gagents (uint32_t size);
234 void free_agents_array (GAgents *agents);
235 
236 float get_percentage (unsigned long long total, unsigned long long hit);
237 int get_max_choices (void);
238 int get_module_enum (const char *str);
239 char *get_module_str (GModule module);
240 int has_timestamp (const char *fmt);
241 int str2enum (const GEnum map[], int len, const char *str);
242 char *enum2str (const GEnum map[], int len, int idx);
243 
244 int enable_panel (GModule mod);
245 int get_module_index (int module);
246 int get_next_module (GModule module);
247 int get_prev_module (GModule module);
248 int ignore_panel (GModule mod);
249 int init_modules (void);
250 int remove_module(GModule module);
251 uint32_t get_num_modules (void);
252 void verify_panels (void);
253 
254 char *get_log_source_str (int max_len);
255 intmax_t get_log_sizes (void);
256 
257 void display_default_config_file (void);
258 void display_storage (void);
259 void display_version (void);
260 /* *INDENT-ON* */
261 
262 #endif
263