File: bitbuffer.c

package info (click to toggle)
libtrace3 3.0.7-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,676 kB
  • ctags: 3,140
  • sloc: ansic: 20,551; sh: 10,125; cpp: 1,384; makefile: 415; yacc: 96; lex: 50
file content (347 lines) | stat: -rw-r--r-- 7,926 bytes parent folder | download | duplicates (2)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "bitbuffer.h"
#include <inttypes.h>
#include "parser.h"
#include <stdio.h>
#include <netinet/in.h>
#include <assert.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "libpacketdump.h"

uint16_t bits;
/* "the largest possible type the compiler supports" */
bitbuffer_t buffer;

static bitbuffer_t getbit(void **packet, int *packlen, uint64_t numbits)
{
    bitbuffer_t ret;
    bitbuffer_t mask;
    
    char *pktptr = NULL;

    /* While the buffer is not filled up and there is still
     * data in the packet to read, read a byte...
     * 
     * The buffer gets filled from right to left
     */
    while(bits < numbits && *packlen > 0)
    {
	uint8_t byte;
	/* read in one byte from the packet */
	byte=(*((bitbuffer_t*)*packet))&0xff;
	buffer |= (bitbuffer_t)byte << (sizeof(bitbuffer_t)*8-(bits+sizeof(byte)*8));
	/* update the position within the packet */
	pktptr = (char *)*packet;
	pktptr += 1;
	*packet = pktptr;

	//*packet = ((char*)*packet) + 1;

	bits += sizeof(byte)*8;
	*packlen -= 1;
    }

    /* our return value is the first <numbits> of the buffer */
    mask = ~((1ULL<<((sizeof(bitbuffer_t)*8-numbits)))-1);
    ret = buffer & mask;
    ret >>=(sizeof(bitbuffer_t)*8-numbits);
    
    /* remove the bits that are being returned from out buffer */
    buffer <<= numbits;

    /* and update our position inside this buffer */
    bits -= numbits;

    return ret;
}

int yyparse(void);

element_t* parse_protocol_file(char *filename)
{
    /* hold onto this so we can put it in any error messages */
    file = filename;

    /* if the protocol file doesn't exist, we return null and
     * it will fall back to using the generic_decode function
     */
    yyin = fopen(filename, "r");
    if(!yyin)
	return NULL;

    el_list = NULL;
    lines = 1;

    yyparse();
    fclose(yyin);
    return el_list;
}


static bitbuffer_t fix_byteorder(bitbuffer_t value,
		enum byte_order_t order, uint64_t size)
{
    bitbuffer_t one = 1;
    bitbuffer_t lhs;
    bitbuffer_t rhs;;

    /*
     * XXX trial and error seems to show these numbers to work.
     * I've tried fields of length 1,2,3,4,8,13,16,32 and they seem to work.
     * Others are untested...
     */
    switch(order)
    {
	case BIGENDIAN: 
	    if(size < 16)
		return value;
	    if(size < 32)
		return ntohs(value);
	    if(size <= 32)
		return ntohl(value);
	    
	    lhs = ntohl(value& ((one<<32)-1));
	    rhs = ntohl(value >> 32);
	    return ((lhs<<32) | rhs);

	case LITTLEENDIAN: 
	    return value;

    };

    /* should never get here */
    assert(0);
    return 0;
}



void decode_protocol_file(uint16_t link_type UNUSED,const char *packet,int len,element_t *el)
{
    bitbuffer_t result;

    while(el != NULL)
    {
	switch(el->type)
	{
	    case FIELD:
	    	if (len*8+bits<el->data->field->size) {
			printf(" [Truncated]\n");
			return;
		}
		result = getbit((void*)&packet, &len, el->data->field->size); 

		switch(el->data->field->display)
		{
		    /* integers get byteswapped if needed and displayed */
		    case DISPLAY_INT: 
		    {
			result = fix_byteorder(result, 
				el->data->field->order, 
				el->data->field->size);
				
			el->data->field->value = result;
			printf(" %s %" PRIi64 "\n", 
				el->data->field->identifier,
				result);
		    }
		    break;

		    /* 
		     * hex numbers get byteswapped if needed and displayed 
		     * without being padded with zeroes
		     */
		    case DISPLAY_HEX: 
		    { 
			result = fix_byteorder(result, 
				el->data->field->order, 
				el->data->field->size);
			
			el->data->field->value = result;
			printf(" %s 0x%" PRIx64 "\n", 
				el->data->field->identifier,
				result);
		    }
		    break;
		    
		    /* 
		     * ipv4 addresses stay in network byte order and are
		     * given to inet_ntoa() to deal with
		     */
		    case DISPLAY_IPV4: 
		    {
			/* assumes all ipv4 addresses are 32bit fields */
			struct in_addr address;
			address.s_addr = (uint32_t)result;
			el->data->field->value = result;
		    
			printf(" %s %s\n", 
				el->data->field->identifier,
				inet_ntoa(address));
		    }
		    break;

		    /* 
		     * mac addresses stay in network byte order and are
		     * displayed byte by byte with zero padding
		     */
		    case DISPLAY_MAC: 
		    {
			/* assumes all mac addresses are 48bit fields */
			uint8_t *ptr = (uint8_t*)&result;
			el->data->field->value = result;
			printf(" %s %02x:%02x:%02x:%02x:%02x:%02x\n",
				el->data->field->identifier,
				ptr[0], ptr[1], ptr[2], 
				ptr[3], ptr[4], ptr[5]);
		    }
		    break;
		    
		    /*
		     * Flag values are only displayed if their value is true
		     * otherwise they are ignored
		     */
		    case DISPLAY_FLAG: 
		    {
			el->data->field->value = result;
			if(result)
			    printf(" %s\n", el->data->field->identifier);
		    }
		    break;

		    /*
		     * Hidden values are not displayed at all. This is useful
		     * for reserved fields or information that you don't care
		     * about but need to read in order to get to the rest of
		     * the header
		     */
		    case DISPLAY_NONE: 
		    {
			result = fix_byteorder(result, 
				el->data->field->order, 
				el->data->field->size);
			el->data->field->value = result;
		    }
		    break;
		};

		break;

	    case NEXTHEADER:
		/* 
		 * Before we move on to the next header, make sure our packet
		 * pointer is pointing to the first unused bytes. This may
		 * mean we have to backtrack to some that were put into the
		 * buffer but weren't used.
		 * - This wouldn't be a problem if all future output came
		 * from this buffer, but there is a good chance we will use
		 * some code from a shared library to output packet info
		 * instead, and this doesn't have access to the buffer.
		 */
		packet = packet - (bits / 8);
		len = len + (bits / 8);
		bits = 0;
		buffer = 0;

		decode_next(packet, len, el->data->nextheader->prefix, 
			ntohs(el->data->nextheader->target->value));
		break;
	};
	
	el = el->next;
    }
    buffer = 0;
    bits = 0;

}








int yyerror(char *s)
{
    element_t *tmp;
    
    fprintf(stderr, "XXX %s\n"
		    "XXX %s on line %d\n"
		    "XXX Falling back to generic_decode()\n", 
		    file, s, lines);
    /* 
     * Clear the list so we don't do partial matching...makes it a bit
     * more obvious that something is broken perhaps.
     * XXX Not sure if it is better to parse none of the packet, or part 
     * of the packet in the event of error? Feel free to remove this if
     * that is desired.
     */

    while(el_list != NULL)
    {
	tmp = el_list;
	el_list = el_list->next;

	switch(tmp->type)
	{
	    case FIELD: free(tmp->data->field); break;
	    case NEXTHEADER: free(tmp->data->nextheader); break;
	}
	free(tmp->data);	
	free(tmp);
	printf("deleting...\n");
    }

    return 0;
}

/*
 * Could be shortcut with a pointer to the tail...
 */
element_t* append(element_t *list, element_t *item)
{
    if(list == NULL)
	return item;

    list->next = append(list->next, item);
    return list;
}
/*
 * Testing...
 */
void print_list(element_t *list)
{
    if(list == NULL)
	return;
	
    switch(list->type)
    {
	case NEXTHEADER: printf("*Nextheader, prefix='%s', target='%s'\n", 
			    list->data->nextheader->prefix, 
			    list->data->nextheader->fieldname);
			    break;
	
	case FIELD: printf("*Field, order = '%d', size = '%d', "
			    "display='%d', name='%s'\n",
			    list->data->field->order, 
			    list->data->field->size, 
			    list->data->field->display,
			    list->data->field->identifier);
			    break;
    };
    /*printf("%s\n", list->data->identifier); */
    print_list(list->next);
}
#ifdef TEST
#include <stdio.h>
int main(void)
{
	unsigned char mybuffer[] = { 0x01, 0x82, 0x03, 0x04, 0x05, 0x06 };
	void *buf = mybuffer;
	int len=sizeof(buffer);
	printf("8bits=%"PRIx64"\n",getbit(&buf,&len,8));
	printf("2bits=%"PRIx64"\n",getbit(&buf,&len,2));
	return 0;
}
#endif