-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth_parser.h
executable file
·121 lines (97 loc) · 2.22 KB
/
eth_parser.h
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
const u_char eth_max[] = {0x06, 0x00}; // 1536
// macro for referencing eth II header
#define EthII ((eth_2_h*)f->eth_header)
/*========== Ethernet Headers Definitions ==========*/
typedef struct eth_2_h
{
u_char dst_addr[6];
u_char src_addr[6];
u_short eth_type;
u_char extra[2]; // just for parsing purposes
}eth_2_h;
typedef struct eth_raw_h
{
u_char dst_addr[6];
u_char src_addr[6];
u_char length[2];
u_char ipx_hdr[3];
}eth_raw_h;
typedef struct eth_llc_h
{
u_char dst_addr[6];
u_char src_addr[6];
u_char length[2];
u_char dsap[1];
u_char ssap[1];
u_char control[1];
}eth_llc_h;
typedef struct eth_llc_snap_h
{
u_char dst_addr[6];
u_char src_addr[6];
u_char length[2];
u_char dsap[1];
u_char ssap[1];
u_char control[1];
u_char vendor[3];
u_char eth_type[2];
}eth_llc_snap_h;
/*----- End of Ethernet Header Declaration ------*/
void parse_l2(Frame * f);
void parse_l3(Frame * f);
void parse_l4(Frame * f);
void parse_l5(Frame * f);
char * get_src_mac(Frame * f){
eth_2_h * hdr;
hdr = (eth_2_h*)f->eth_header;
return get_hex(hdr->src_addr, 6, ':');
}
char * get_dst_mac(Frame * f){
eth_2_h * hdr;
hdr = (eth_2_h*)f->eth_header;
return get_hex(hdr->dst_addr, 6, ':');
}
int is_broadcast(Frame * f){
char * c;
c = get_dst_mac(f);
if (strcasecmp(c, "ff:ff:ff:ff:ff:ff") == 0) {
return 1;
}
return 0;
}
void parse_l2(Frame * f){
if( EthII->eth_type >= 0x0006) {
// we got eth2
f->l2 = ETH2_TYPE;
}
// add next l2 protocols..
if (f->l2){
parse_l3(f);
} else {
my_log("Failed to parse L2, header not supported");
}
}
void print_frame(Frame * f){
sprintf(log_b, "L2: %i\tL3: %i\tL4: %i\tL5: %i\t ",
f->l2, f->l3, f->l4, f->l5);
sprintf(log_b, "\tsrc: %s, dst: %s", get_src_mac(f), get_dst_mac(f));
my_log(log_b);
}
Frame * add_frame(u_char * data, int length, Port * p, int d){
struct eth_2_h * hdr = (eth_2_h *) data;
Frame * frame = calloc(sizeof(Frame), 1);
frame->length = length;
frame->data = malloc(length);
frame->eth_header = frame->data;
frame->parseble = 0;
frame->p = p;
frame->direction = d;
int k = memcmp(&(hdr->eth_type), eth_max, 2);
// if we have ETH II
if ( k >= 0){
frame->parseble = 1;
}
memcpy ( frame->data, data, length);
parse_l2(frame);
return frame;
}