File: ec_connbuf.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (184 lines) | stat: -rw-r--r-- 4,809 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
/*
    ettercap -- connection buffer module

    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_connbuf.h>

/* mutexes */

#define CONNBUF_INIT_LOCK(x)  do{ pthread_mutex_init(&x, NULL); }while(0)
#define CONNBUF_LOCK(x)       do{ pthread_mutex_lock(&x); }while(0)
#define CONNBUF_UNLOCK(x)     do{ pthread_mutex_unlock(&x); }while(0)

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

/*
 * initialize the buffer
 */
void connbuf_init(struct conn_buf *cb, size_t size)
{
   DEBUG_MSG("connbuf_init");
  
   /* init the size */
   cb->size = 0;
   cb->max_size = size;
   /* init the tail */
   TAILQ_INIT(&cb->connbuf_tail);
   /* init the mutex */
   CONNBUF_INIT_LOCK(cb->connbuf_mutex);
}

/* 
 * add the packet to the conn_buf.
 * check if the buffer has reached the max size
 * and in case delete the oldest elements to 
 * fit the predefined size.
 *
 * the tail has the newer packet in the head and
 * older in the tail.
 */
int connbuf_add(struct conn_buf *cb, struct packet_object *po)
{
   struct conn_pck_list *p;
   struct conn_pck_list *e;

   SAFE_CALLOC(p, 1, sizeof(struct conn_pck_list));
   
   /* 
    * we add the sizeof because if the packets have 0 length
    * (ack packets) the real memory occupation will overflow
    * the max_size
    */
   p->size = sizeof(struct conn_pck_list) + po->DATA.disp_len;
  
   memcpy(&p->L3_src, &po->L3.src, sizeof(struct ip_addr));

   /* 
    * we cant handle the packet, the buffer
    * is too small
    */
   if (p->size > cb->max_size) {
      DEBUG_MSG("connbuf_add: buffer too small %d %d\n", (int)cb->max_size, (int)p->size);      
      SAFE_FREE(p);
      return 0;
   }
      
   /* copy the buffer */
   SAFE_CALLOC(p->buf, po->DATA.disp_len, sizeof(u_char));
   
   memcpy(p->buf, po->DATA.disp_data, po->DATA.disp_len);

   CONNBUF_LOCK(cb->connbuf_mutex);
   
   /* 
    * check the total size and make adjustment 
    * if we have to free some packets
    */
   if (cb->size + p->size > cb->max_size) {
      struct conn_pck_list *tmp = NULL;
      
      TAILQ_FOREACH_REVERSE_SAFE(e, &cb->connbuf_tail, next, connbuf_head, tmp) {
         
         /* we have freed enough bytes */
         if (cb->size + p->size <= cb->max_size)
            break;
         
         /* calculate the new size */
         cb->size -= e->size;
         /* remove the elemnt */
         SAFE_FREE(e->buf);
         TAILQ_REMOVE(&cb->connbuf_tail, e, next);
         SAFE_FREE(e);
      }
   }
   
   /* insert the packet in the tail */
   TAILQ_INSERT_HEAD(&cb->connbuf_tail, p, next);
      
   /* update the total buffer size */
   cb->size += p->size;

   CONNBUF_UNLOCK(cb->connbuf_mutex);

   return 0;
}

/*
 * empty a give buffer.
 * all the elements in the list are deleted
 */
void connbuf_wipe(struct conn_buf *cb)
{
   struct conn_pck_list *e;

   DEBUG_MSG("connbuf_wipe");
   
   CONNBUF_LOCK(cb->connbuf_mutex);
   
   /* delete the list */
   while ((e = TAILQ_FIRST(&cb->connbuf_tail)) != TAILQ_END(&cb->connbuf_tail)) {
      TAILQ_REMOVE(&cb->connbuf_tail, e, next);
      SAFE_FREE(e->buf);
      SAFE_FREE(e);
   }

   /* reset the buffer */
   cb->size = 0;
   TAILQ_INIT(&cb->connbuf_tail);
   
   CONNBUF_UNLOCK(cb->connbuf_mutex);
}

/* 
 * print the content of a buffer
 * you can print only one side of the communication
 * by specifying the L3_src address, or NULL to 
 * print all the packet in order (joined view).
 *
 * returns the number of printed chars
 */
int connbuf_print(struct conn_buf *cb, void (*func)(u_char *, size_t, struct ip_addr *L3_src))
{
   struct conn_pck_list *e;
   int n = 0;
  
   CONNBUF_LOCK(cb->connbuf_mutex);
   
   /* print the buffer */
   TAILQ_FOREACH_REVERSE(e, &cb->connbuf_tail, next, connbuf_head) {
      /* 
       * remember that the size is comprehensive
       * of the struct size
       */
      func(e->buf, e->size - sizeof(struct conn_pck_list), &e->L3_src);
      n += e->size - sizeof(struct conn_pck_list);
   }
   
   CONNBUF_UNLOCK(cb->connbuf_mutex);
   
   return n;
}

/* EOF */

// vim:ts=3:expandtab