-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathethernet.c
74 lines (59 loc) · 1.87 KB
/
ethernet.c
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
#include "ethernet.h"
#include "net.h"
#include "stats.h"
#include "arp.h"
#include "ipv4.h"
const mac_addr_t mac_addr_broadcast = {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }};
uint8_t ethernet_address_match(const mac_addr_t *address1, const mac_addr_t *address2)
{
uint8_t ix;
for(ix = 0; ix < sizeof(mac_addr_t); ix++)
if(address1->byte[ix] != address2->byte[ix])
return(0);
return(1);
}
uint16_t ethernet_process_frame(const uint8_t *frame_in, uint16_t frame_in_length,
uint8_t *frame_out, uint16_t frame_out_size,
const mac_addr_t *my_mac_address, ipv4_addr_t *my_ipv4_address)
{
etherframe_t *etherframe_in;
etherframe_t *etherframe_out;
uint16_t payload_length_out;
etherframe_in = (etherframe_t *)frame_in;
etherframe_out = (etherframe_t *)frame_out;
payload_length_out = 0;
switch(ntohs(etherframe_in->ethertype))
{
case(et_arp):
{
ip_arp_pkt_in++;
payload_length_out = process_arp(ðerframe_in->payload[0], frame_in_length - sizeof(etherframe_t),
ðerframe_out->payload[0], my_mac_address, my_ipv4_address);
if(payload_length_out)
ip_arp_pkt_out++;
break;
}
case(et_ipv4):
{
ip_ipv4_pkt_in++;
payload_length_out = process_ipv4(ðerframe_in->payload[0], frame_in_length - sizeof(etherframe_t),
ðerframe_out->payload[0], frame_out_size - sizeof(etherframe_t),
my_mac_address, my_ipv4_address);
if(payload_length_out)
ip_ipv4_pkt_out++;
break;
}
}
etherframe_out->destination = etherframe_in->source;
etherframe_out->source = *my_mac_address;
etherframe_out->ethertype = etherframe_in->ethertype;
return(sizeof(etherframe_t) + payload_length_out);
}
void ethernet_add_frame_header(etherframe_t *ethernet_frame,
const mac_addr_t *src, const mac_addr_t *dst,
uint16_t ethertype)
{
ethernet_frame->source = *src;
ethernet_frame->destination = *dst;
ethernet_frame->ethertype = htons(ethertype);
}