File: sens_timeouts.c

package info (click to toggle)
arpalert 2.0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,508 kB
  • sloc: ansic: 4,452; sh: 517; makefile: 116; perl: 35
file content (224 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2005-2010 Thierry FOURNIER
 * $Id: sens_timeouts.c 690 2008-03-31 18:36:43Z  $
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>

#include "arpalert.h"
#include "data.h"
#include "sens_timeouts.h"
#include "log.h"
#include "loadconfig.h"
#include "func_time.h"

/* hash table size ; this number must be primary number */
#define HASH_SIZE 4096

// hash function
#define SENS_TIMEOUT_HASH(x, y) ({ \
   U_INT32_T a, b, c; \
   a = (*(U_INT16_T*)&(x)->ETHER_ADDR_OCTET[0]) ^ \
	    (*(U_INT16_T*)&(x)->ETHER_ADDR_OCTET[4]); \
   b = a + ( ( (x)->ETHER_ADDR_OCTET[2] ^ \
	    (x)->ETHER_ADDR_OCTET[3] ) << 16 ); \
   c = a ^ ( b >> 12 ); \
   a = ((U_INT16_T)(y)) ^ ( ((U_INT32_T)(y)) >> 20 ) ^ \
	    ( ((U_INT8_T)(y)) >> 12 ); \
   ( a ^ c ) & 0xfff; \
})

extern int errno;

// structurs
struct tmouts {
	struct ether_addr mac;
	struct in_addr ip_d;
	struct timeval last;
	struct capt *idcap;
	struct tmouts *prev_hash;
	struct tmouts *next_hash;
	struct tmouts *next_chain;
	struct tmouts *prev_chain;
};

// data allocation
#define MAX_DATA 2000
struct tmouts tmouts_table[MAX_DATA];

// hash
struct tmouts tmout_h[HASH_SIZE];

// root of free nodes chain
struct tmouts __free_start;
struct tmouts *free_start = &__free_start;

// root of used nodes chain
struct tmouts __used_start;
struct tmouts *used_start = &__used_start;

// data_init
void sens_timeout_init(void) {
	int i;
	struct tmouts *gen;

	// set NULL pointers in tmout_h table and init
	memset(&tmout_h, 0, HASH_SIZE * sizeof(struct tmouts));
	i = 0;
	while(i < HASH_SIZE){
		gen = &tmout_h[i];
		gen->prev_hash = gen;
		gen->next_hash = gen;
		i++;
	}
	
	// generate free nodes list
	free_start->next_chain = free_start;
	free_start->prev_chain = free_start;
	i = 0;
	while(i < MAX_DATA){
		gen = &tmouts_table[i];
		gen->next_chain = free_start->next_chain;
		gen->prev_chain = free_start;
		free_start->next_chain->prev_chain = gen;
		free_start->next_chain = gen;
		i++;
	}

	// set used chain
	used_start->next_chain = used_start;
	used_start->prev_chain = used_start;
}

// add new timeout
void sens_timeout_add(struct ether_addr *mac, struct in_addr ipb,
                      struct capt *idcap){
	struct tmouts *new_tmout;
	struct tmouts *base;
	int hash;

	// get free timeout node
	if(free_start->next_chain == free_start){
		logmsg(LOG_WARNING,
		       "[%s %d] Timeouts table full: more than %d timeouts used",
		       __FILE__, __LINE__, MAX_DATA);
		return;
	}

	// init values
	new_tmout = free_start->next_chain;
	DATA_CPY(&new_tmout->mac, mac);
	new_tmout->idcap = idcap;
	new_tmout->ip_d.s_addr = ipb.s_addr;
	new_tmout->last.tv_sec = current_t.tv_sec +
	                         config[CF_ANTIFLOOD_INTER].valeur.integer;
	new_tmout->last.tv_usec = current_t.tv_usec;

	// delete entrie fron free list
	new_tmout->prev_chain->next_chain = new_tmout->next_chain;
	new_tmout->next_chain->prev_chain = new_tmout->prev_chain;

	// add entrie in hash 
	hash = SENS_TIMEOUT_HASH(mac, ipb.s_addr);
	base = &tmout_h[hash];
	base->next_hash->prev_hash = new_tmout;
	new_tmout->next_hash = base->next_hash;
	base->next_hash = new_tmout;
	new_tmout->prev_hash = base;
	
	// add timeout at the end of chain list
	used_start->prev_chain->next_chain = new_tmout;
	new_tmout->prev_chain = used_start->prev_chain;
	used_start->prev_chain = new_tmout;
	new_tmout->next_chain = used_start;
}

// check if entry is present in timeout hash
int sens_timeout_exist(struct ether_addr *mac, struct in_addr ipb,
                       struct capt *idcap){
	int hash;
	struct tmouts *base;
	struct tmouts *find;

	// if timeout entrie exist: is not expired
	hash = SENS_TIMEOUT_HASH(mac, ipb.s_addr);
	base = &tmout_h[hash];
	find = base->next_hash;

	while(find != base){
		if(find->ip_d.s_addr == ipb.s_addr &&
		   find->idcap == idcap &&
		   DATA_CMP(&(find->mac), mac) == DATA_EQUAL){
			return(TRUE);
		}
		find = find->next_hash;
	}
	return(FALSE);
}

// delete timeouts expires
void sens_timeout_clean(void) {
	struct tmouts *look;
	struct tmouts *look_next;

	look = used_start->next_chain;

	while(look != used_start){ 

		// if entry expires
		if(time_comp(&current_t, &(look->last)) == BIGEST){

			// get next
			look_next = look->next_chain;

			// delete from hash
			look->prev_hash->next_hash = look->next_hash;
			look->next_hash->prev_hash = look->prev_hash;
		
			// delete from used list
			look->prev_chain->next_chain = look->next_chain;
			look->next_chain->prev_chain = look->prev_chain;
		
			// move the structur in free
			free_start->next_chain->prev_chain = look;
			look->next_chain = free_start->next_chain;
			free_start->next_chain = look;
			look->prev_chain = free_start;

			// set next
			look = look_next;
		}

		// else quit (the entry are sorted by expiration date)
		else {
			return;
		}
	}
}

// get next timeout
void *sens_timeout_next(struct timeval *tv){
	struct tmouts *look;
	
	look = used_start->next_chain;
	if(look != used_start){
		tv->tv_sec = look->last.tv_sec;
		tv->tv_usec = look->last.tv_usec;
	}

	else {
		tv->tv_sec = -1;
	}
	
	return sens_timeout_clean;
}