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