File: ec_streambuf.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 (253 lines) | stat: -rw-r--r-- 6,223 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
/*
    ettercap -- stream 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_streambuf.h>

/* mutexes */

#define STREAMBUF_INIT_LOCK(x)  do{ pthread_mutex_init(&x, NULL); }while(0)
#define STREAMBUF_LOCK(x)       do{ pthread_mutex_lock(&x); }while(0)
#define STREAMBUF_UNLOCK(x)     do{ pthread_mutex_unlock(&x); }while(0)

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

/*
 * initialize the buffer
 */
void streambuf_init(struct stream_buf *sb)
{
   //DEBUG_MSG("streambuf_init");
 
   /* init the size */
   sb->size = 0;
   
   /* init the tail */
   TAILQ_INIT(&sb->streambuf_tail);
   
   /* init the mutex */
   STREAMBUF_INIT_LOCK(sb->streambuf_mutex);
}


/* 
 * add the packet to the stream_buf.
 */
int streambuf_add(struct stream_buf *sb, struct packet_object *po)
{
   struct stream_pck_list *p;

   SAFE_CALLOC(p, 1, sizeof(struct stream_pck_list));
   
   /* fill the struct */
   p->size = po->DATA.len;
   p->ptr = 0;
  
   /* copy the buffer */
   SAFE_CALLOC(p->buf, po->DATA.len, sizeof(u_char));
   
   memcpy(p->buf, po->DATA.data, po->DATA.len);

   STREAMBUF_LOCK(sb->streambuf_mutex);
   
   /* insert the packet in the tail */
   TAILQ_INSERT_TAIL(&sb->streambuf_tail, p, next);

   /* update the total size */
   sb->size += p->size;
      
   STREAMBUF_UNLOCK(sb->streambuf_mutex);

   return 0;
}

/* Insert data into the buffer only if it matches the correct
 * TCP sequence. It is a simple workaround used to avoid duplicated
 * packets in TCP streaming. If we see the same sequence twice, this
 * is a duplicated packet.
 */
int streambuf_seq_add(struct stream_buf *sb, struct packet_object *po)
{
   /* Same seq twice? a duplicated pck */
   if( sb->tcp_seq == po->L4.seq )
      return 0;

   sb->tcp_seq = po->L4.seq;
      
   return streambuf_add(sb, po);
}

/*
 * copies in the 'buf' the first 'len' bytes 
 * of the stream buffer
 *
 * STREAM_ATOMIC  returns an error if there is not enough
 *                data to fill 'len' bytes in 'buf'
 *
 * STREAM_PARTIAL will fill the buffer 'buf' with the data
 *                contained in the streambuf even if it is
 *                less than 'len'. size is returned accordingly
 */
int streambuf_get(struct stream_buf *sb, u_char *buf, size_t len, int mode)
{
   struct stream_pck_list *p;
   struct stream_pck_list *tmp = NULL;
   size_t size = 0, to_copy = 0;

   /* check if we have enough data */
   if (mode == STREAM_ATOMIC && sb->size < len)
      return -E_INVALID;

   STREAMBUF_LOCK(sb->streambuf_mutex);
   
   /* packets in the tail */
   TAILQ_FOREACH_SAFE(p, &sb->streambuf_tail, next, tmp) {

      /* we have copied all the needed bytes */
      if (size >= len)
         break;
     
      /* calculate the length to be copied */
      if (len - size < p->size)
         to_copy = len - size;
      else
         to_copy = p->size;
     
      if (p->ptr + to_copy > p->size)
         to_copy = p->size - p->ptr;

      /* 
       * copy the data in the buffer
       * p->ptr is the pointer to last read 
       * byte if the buffer was read partially
       */
      memcpy(buf + size, p->buf + p->ptr, to_copy);

      /* bytes in the buffer 'buf' */
      size += to_copy;
      
      /* remember how may byte we have read */
      p->ptr += to_copy;

      /* packet not completed */
      if (p->ptr < p->size) {
         break;
      }
      
      /* remove the entry from the tail */
      SAFE_FREE(p->buf);
      TAILQ_REMOVE(&sb->streambuf_tail, p, next);
      SAFE_FREE(p);
   }

   /* update the total size */
   sb->size -= size;
      
   STREAMBUF_UNLOCK(sb->streambuf_mutex);

   
   return size;
}

/* Same as streambuf_get(), but this will not erase 
 * read data from the buffer
 */
int streambuf_read(struct stream_buf *sb, u_char *buf, size_t len, int mode)
{
   struct stream_pck_list *p;
   size_t size = 0, to_copy = 0;

   /* check if we have enough data */
   if (mode == STREAM_ATOMIC && sb->size < len)
      return -E_INVALID;

   STREAMBUF_LOCK(sb->streambuf_mutex);
   
   /* packets in the tail */
   TAILQ_FOREACH(p, &sb->streambuf_tail, next) {
      
      /* we have copied all the needed bytes */
      if (size >= len)
         break;
     
      /* calculate the length to be copied */
      if (len - size < p->size)
         to_copy = len - size;
      else
         to_copy = p->size;
     
      if (p->ptr + to_copy > p->size)
         to_copy = p->size - p->ptr;

      /* 
       * copy the data in the buffer
       * p->ptr is the pointer to last read 
       * byte if the buffer was read partially
       */
      memcpy(buf + size, p->buf + p->ptr, to_copy);

      /* bytes in the buffer 'buf' */
      size += to_copy;
      
      /* packet not completed */
      if (p->ptr + to_copy < p->size) {
         break;
      }
   }

   STREAMBUF_UNLOCK(sb->streambuf_mutex);

   
   return size;
}


/*
 * empty a give buffer.
 * all the elements in the list are deleted
 */
void streambuf_wipe(struct stream_buf *sb)
{
   struct stream_pck_list *e;

   DEBUG_MSG("streambuf_wipe");
   
   STREAMBUF_LOCK(sb->streambuf_mutex);
   
   /* delete the list */
   while ((e = TAILQ_FIRST(&sb->streambuf_tail)) != TAILQ_END(&sb->streambuf_tail)) {
      TAILQ_REMOVE(&sb->streambuf_tail, e, next);
      SAFE_FREE(e->buf);
      SAFE_FREE(e);
   }

   /* reset the buffer */
   TAILQ_INIT(&sb->streambuf_tail);

   STREAMBUF_UNLOCK(sb->streambuf_mutex);
}


/* EOF */

// vim:ts=3:expandtab