File: ip_6.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 (120 lines) | stat: -rw-r--r-- 2,936 bytes parent folder | download
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
#include <stdio.h>
#include <inttypes.h>
#include <dlfcn.h>
#include "libpacketdump.h"
#include <assert.h>
#include <netdb.h>

#define SAFE(x) \
	((unsigned int)len>=((char*)&tcp->x-(char*)tcp+sizeof(tcp->x))) 
#define DISPLAY_EXP(x,fmt,exp) \
	if (SAFE(x)) \
		printf(fmt,exp); \
	else \
		return; 

#define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,tcp->x)

#define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(tcp->x))
#define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(tcp->x))
#define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&tcp->x))

DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
{
	unsigned char *pkt = NULL;
	unsigned char type,optlen,*data;
	int plen, i;
	libtrace_tcp_t *tcp = (libtrace_tcp_t *)packet;
	printf(" TCP:");
	if (SAFE(source)) {
		struct servent *ent=getservbyport(tcp->source,"tcp");
		if(ent) {
			printf(" Source %i (%s)",htons(tcp->source),ent->s_name);
		} else {
			printf(" Source %i",htons(tcp->source));
		}
	}
	else {
		printf("\n");
		return;
	}
	if (SAFE(dest)) {
		struct servent *ent=getservbyport(tcp->dest,"tcp");
		if(ent) {
			printf(" Dest %i (%s)",htons(tcp->dest),ent->s_name);
		} else {
			printf(" Dest %i",htons(tcp->dest));
		}
	}
	else {
		printf("\n");
		return;
	}
	printf("\n TCP:");
	DISPLAYL(seq," Seq %u");
	printf("\n TCP:");
	DISPLAYL(ack_seq," Ack %u");
	if ((char*)&tcp->window-(char *)tcp>len) {
		printf("\n");
		return;
	}
	printf("\n TCP:");
	printf(" DOFF %i",tcp->doff);
	printf(" Flags:");
	if (tcp->fin) printf(" FIN");
	if (tcp->syn) printf(" SYN");
	if (tcp->rst) printf(" RST");
	if (tcp->psh) printf(" PSH");
	if (tcp->ack) printf(" ACK");
	if (tcp->urg) printf(" URG");
	DISPLAYS(window," Window %i");
	printf("\n TCP:");
	DISPLAYS(check," Checksum %i");
	DISPLAYS(urg_ptr," Urgent %i");
	pkt = (unsigned char*)packet+sizeof(*tcp);
	plen = (len-sizeof *tcp) < (tcp->doff*4-sizeof(*tcp))?(len-sizeof(*tcp)):(tcp->doff*4-sizeof *tcp);
	while(trace_get_next_option(&pkt,&plen,&type,&optlen,&data)) {
		printf("\n TCP: ");
		switch(type) {
			case 0:
				printf("End of options");
				break;
			case 1:
				printf("NOP");
				break;
			case 2:
				printf("MSS %i",htons(*(uint32_t *)(data)));
				break;
			case 3:
				printf("Winscale %i",data[0]);
				break;
			case 4:
				printf("SACK");
				break;
			case 5:
				printf("SACK Information");
				i=0;
				while(i+8<optlen) {
					printf("\n TCP:  %u-%u",
						htonl(*(uint32_t*)&data[i]),
						htonl(*(uint32_t*)&data[i+4]));
					i+=8;
				}
				break;
			case 8:
				printf("Timestamp %u %u",
						htonl(*(uint32_t *)&data[0]),
						htonl(*(uint32_t *)&data[4])
				      );
				break;
			default:
				printf("Unknown option %i",type);
		}
	}
	printf("\n");
	if (htons(tcp->source) < htons(tcp->dest)) 
		decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->source));
	else
		decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->dest));
	return;
}