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