new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / eventinfo_list.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation.
8  */
9
10 #include "shared.h"
11 #include "eventinfo.h"
12 #include "rules.h"
13
14 /* Local variables */
15 static EventNode *eventnode;
16 static EventNode *lastnode;
17
18 static int _memoryused = 0;
19 static int _memorymaxsize = 0;
20 int _max_freq = 0;
21
22
23 /* Create the Event List */
24 void OS_CreateEventList(int maxsize)
25 {
26     eventnode = NULL;
27     _memorymaxsize = maxsize;
28     _memoryused = 0;
29
30     debug1("%s: OS_CreateEventList completed.", ARGV0);
31     return;
32 }
33
34 /* Get the last event -- or first node */
35 EventNode *OS_GetLastEvent()
36 {
37     EventNode *eventnode_pt = eventnode;
38
39     return (eventnode_pt);
40 }
41
42 /* Add an event to the list -- always to the beginning */
43 void OS_AddEvent(Eventinfo *lf)
44 {
45     EventNode *tmp_node = eventnode;
46
47     if (tmp_node) {
48         EventNode *new_node;
49         new_node = (EventNode *)calloc(1, sizeof(EventNode));
50
51         if (new_node == NULL) {
52             ErrorExit(MEM_ERROR, ARGV0, errno, strerror(errno));
53         }
54
55         /* Always add to the beginning of the list
56          * The new node will become the first node and
57          * new_node->next will be the previous first node
58          */
59         new_node->next = tmp_node;
60         new_node->prev = NULL;
61         tmp_node->prev = new_node;
62
63         eventnode = new_node;
64
65         /* Add the event to the node */
66         new_node->event = lf;
67
68         _memoryused++;
69
70         /* Need to remove the last nodes */
71         if (_memoryused > _memorymaxsize) {
72             int i = 0;
73             EventNode *oldlast;
74
75             /* Remove at least the last 10 events
76              * or the events that will not match anymore
77              * (higher than max frequency)
78              */
79             while ((i < 10) || ((lf->time - lastnode->event->time) > _max_freq)) {
80                 oldlast = lastnode;
81                 lastnode = lastnode->prev;
82                 lastnode->next = NULL;
83
84                 /* Free event info */
85                 Free_Eventinfo(oldlast->event);
86                 free(oldlast);
87
88                 _memoryused--;
89                 i++;
90             }
91         }
92     }
93
94     else {
95         /* Add first node */
96         eventnode = (EventNode *)calloc(1, sizeof(EventNode));
97         if (eventnode == NULL) {
98             ErrorExit(MEM_ERROR, ARGV0, errno, strerror(errno));
99         }
100
101         eventnode->prev = NULL;
102         eventnode->next = NULL;
103         eventnode->event = lf;
104
105         lastnode = eventnode;
106     }
107
108     return;
109 }
110