File: ec_session.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (281 lines) | stat: -rw-r--r-- 6,438 bytes parent folder | download | duplicates (4)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
    ettercap -- session management

    Copyright (C) ALoR & NaGA

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include <ec.h>
#include <ec_packet.h>
#include <ec_threads.h>
#include <ec_session.h>

#include <signal.h>

#define TABBIT    10             /* 2^10 bit tab entries: 1024 LISTS */
#define TABSIZE   (1 << TABBIT)
#define TABMASK   (TABSIZE - 1)

/* globals */

struct session_list {
   time_t ts;
   struct ec_session *s;
   LIST_ENTRY (session_list) next;
};

/* global data */

static LIST_HEAD(, session_list) session_list_head[TABSIZE];

/* protos */

u_int32 session_hash(void *ident, size_t ilen);

static pthread_mutex_t session_mutex = PTHREAD_MUTEX_INITIALIZER;
#define SESSION_LOCK     do{ pthread_mutex_lock(&session_mutex); } while(0)
#define SESSION_UNLOCK   do{ pthread_mutex_unlock(&session_mutex); } while(0)

/************************************************/

/*
 * create a session if it does not exits
 * update a session if it already exists
 *
 * also check for timeouted session and remove them
 */

void session_put(struct ec_session *s)
{
   struct session_list *sl, *tmp = NULL;
   time_t ti = time(NULL);
   u_int32 h;

   SESSION_LOCK;
   
   /* calculate the hash */
   h = session_hash(s->ident, s->ident_len);
   
   /* search if it already exist */
   LIST_FOREACH_SAFE(sl, &session_list_head[h], next, tmp) {
      if ( sl->s->match(sl->s->ident, s->ident) ) {

         DEBUG_MSG("session_put: [%p] updated", sl->s->ident);
         /* destroy the old session */
         session_free(sl->s);
         /* link the new session */
         sl->s = s;
         /* renew the timestamp */
         sl->ts = ti;
         
         SESSION_UNLOCK;
         return;
      }

      if (sl->ts < (ti - GBL_CONF->connection_timeout) ) {
         DEBUG_MSG("session_put: [%p] timeouted", sl->s->ident);
         session_free(sl->s);
         LIST_REMOVE(sl, next);
         SAFE_FREE(sl);
      }
   }
   
   /* sanity check */
   BUG_IF(s->match == NULL);
  
   /* create the element in the list */
   SAFE_CALLOC(sl, 1, sizeof(struct session_list));
   
   /* the timestamp */
   sl->ts = ti;

   /* link the session */
   sl->s = s;
   
   DEBUG_MSG("session_put: [%p] new session", sl->s->ident);

   /* 
    * put it in the head.
    * it is likely to be retrived early
    */
   LIST_INSERT_HEAD(&session_list_head[h], sl, next);

   SESSION_UNLOCK;
  
}


/*
 * get the info contained in a session
 */

int session_get(struct ec_session **s, void *ident, size_t ident_len)
{
   struct session_list *sl;
   time_t ti = time(NULL);
   u_int32 h;

   SESSION_LOCK;
   
   /* calculate the hash */
   h = session_hash(ident, ident_len);

   /* search if it already exist */
   LIST_FOREACH(sl, &session_list_head[h], next) {
      if ( sl->s->match(sl->s->ident, ident) ) {
   
         //DEBUG_MSG("session_get: [%p]", sl->s->ident);
         /* return the session */
         *s = sl->s;
         
         /* renew the timestamp */
         sl->ts = ti;

         SESSION_UNLOCK;
         return E_SUCCESS;
      }
   }
   
   SESSION_UNLOCK;
   
   return -E_NOTFOUND;
}


/*
 * delete a session
 */

int session_del(void *ident, size_t ident_len)
{
   struct session_list *sl;
   u_int32 h;

   SESSION_LOCK;
   
   /* calculate the hash */
   h = session_hash(ident, ident_len);
   
   /* search if it already exist */
   LIST_FOREACH(sl, &session_list_head[h], next) {
      if ( sl->s->match(sl->s->ident, ident) ) {
         
         DEBUG_MSG("session_del: [%p]", sl->s->ident);

         /* free the session */
         session_free(sl->s);
         /* remove the element in the list */
         LIST_REMOVE(sl, next);
         /* free the element in the list */
         SAFE_FREE(sl);

         SESSION_UNLOCK;
         return E_SUCCESS;
      }
   }
   
   SESSION_UNLOCK;
   
   return -E_NOTFOUND;
}


/*
 * get the info and delete the session
 * atomic operations
 */

int session_get_and_del(struct ec_session **s, void *ident, size_t ident_len)
{
   struct session_list *sl;
   u_int32 h;

   SESSION_LOCK;
   
   /* calculate the hash */
   h = session_hash(ident, ident_len);
   
   /* search if it already exist */
   LIST_FOREACH(sl, &session_list_head[h], next) {
      if ( sl->s->match(sl->s->ident, ident) ) {
         
         DEBUG_MSG("session_get_and_del: [%p]", sl->s->ident);
         
         /* return the session */
         *s = sl->s;
         /* remove the element in the list */
         LIST_REMOVE(sl, next);
         /* free the element in the list */
         SAFE_FREE(sl);

         SESSION_UNLOCK;
         return E_SUCCESS;
      }
   }
   
   SESSION_UNLOCK;
   
   return -E_NOTFOUND;
}

/*
 * free a session structure
 */

void session_free(struct ec_session *s)
{
   DEBUG_MSG("session_free: [%p] deleted", s->ident);
   SAFE_FREE(s->ident);
   /* call the cleanup function to free pointers in the data portion */
   if (s->free)
      s->free(s->data, s->data_len);
   /* data is free'd here, don't free it in the s->free function */
   SAFE_FREE(s->data);
   SAFE_FREE(s);
}

/*
 * calculate the hash for an ident.
 * use a IP-like checksum so if some word will be exchanged
 * the hash will be the same. it is useful for dissectors
 * to find the same session if the packet is goint to the
 * server or to the client.
 */
u_int32 session_hash(void *ident, size_t ilen)
{
   u_int32 hash = 0;
   u_int16 *buf = (u_int16 *)ident;

   while(ilen > 1) {
      hash += *buf++;
      ilen -= sizeof(u_int16);
   }

   if (ilen == 1) 
      hash += htons(*(u_char *)buf << 8);

   hash = (hash >> 16) + (hash & 0xffff);
   hash += (hash >> 16);

   /* the hash must be within the TABSIZE */
   return (u_int16)(~hash) & TABMASK;
}

/* EOF */

// vim:ts=3:expandtab