1 | #include <netinet/ether.h> |
---|
2 | #include <netinet/in.h> |
---|
3 | #include <stdio.h> |
---|
4 | #include <inttypes.h> |
---|
5 | #include <dlfcn.h> |
---|
6 | #include <map> |
---|
7 | #include "libpacketdump.h" |
---|
8 | #include <sys/socket.h> |
---|
9 | #include <netinet/in.h> |
---|
10 | #include <netinet/ip.h> |
---|
11 | #include <arpa/inet.h> |
---|
12 | #include <netdb.h> |
---|
13 | |
---|
14 | #define DISPLAY_EXP(x,fmt,exp) \ |
---|
15 | if ((unsigned int)len>=((char*)&ip->x-(char*)ip+sizeof(ip->x))) \ |
---|
16 | printf(fmt,exp); \ |
---|
17 | else \ |
---|
18 | return; |
---|
19 | |
---|
20 | #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,ip->x) |
---|
21 | |
---|
22 | #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(ip->x)) |
---|
23 | #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&ip->x)) |
---|
24 | |
---|
25 | extern "C" |
---|
26 | void decode(int link_type,char *packet,int len) |
---|
27 | { |
---|
28 | struct iphdr *ip = (struct iphdr*)packet; |
---|
29 | if (len>=1) { |
---|
30 | printf(" IP: Header Len %i",ip->ihl*4); |
---|
31 | printf(" Ver %i",ip->version); |
---|
32 | } |
---|
33 | DISPLAY(tos," TOS %02x") |
---|
34 | DISPLAYS(tot_len," Total Length %i") |
---|
35 | printf("\n IP:"); |
---|
36 | DISPLAY(id," Id %i"); |
---|
37 | DISPLAY(frag_off," Fragoff %i"); |
---|
38 | //printf("\n IP:"); |
---|
39 | DISPLAY(ttl," TTL %i"); |
---|
40 | if ((unsigned int)len>=((char*)&ip->protocol-(char*)ip+sizeof(ip->protocol))) { |
---|
41 | struct protoent *ent=getprotobynumber(ip->protocol); |
---|
42 | if (ent) { |
---|
43 | printf(" Proto %i (%s)",ip->protocol,ent->p_name); |
---|
44 | } |
---|
45 | else { |
---|
46 | printf(" Proto %i",ip->protocol); |
---|
47 | } |
---|
48 | } else { |
---|
49 | printf("\n"); |
---|
50 | return; |
---|
51 | } |
---|
52 | DISPLAYS(check," Checksum %i\n"); |
---|
53 | DISPLAYIP(saddr," IP: Source %s "); |
---|
54 | DISPLAYIP(daddr,"Destination %s\n"); |
---|
55 | decode_next(packet+sizeof(*ip),len-sizeof(*ip),"ip",ip->protocol); |
---|
56 | return; |
---|
57 | } |
---|