-
Notifications
You must be signed in to change notification settings - Fork 0
/
dts_net_ether.c
119 lines (92 loc) · 3.31 KB
/
dts_net_ether.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
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
/*
The MIT License (MIT)
Copyright (c) 2020 Doerthous
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Author: Doerthous <[email protected]>
*/
#include <dts/net/ether.h>
#include <dts/net/endian.h>
#include <dts/net/ether_arp.h>
#include <dts/net/arp.h>
#include <dts/net/ip.h>
#include <dts/net/dblk.h>
#include <string.h>
int ether_pack(ether_frame_t *frame)
{
uint8_t *ptr = frame->data;
memcpy(ptr, frame->dest_mac_addr, 6); ptr += 6;
memcpy(ptr, frame->src_mac_addr, 6); ptr += 6;
if (frame->type < 0x05DC) {
if (dblk_size(frame->payload) < 46) {
ptr = bw16(46, ptr);
}
}
else {
ptr = bw16(frame->type, ptr);
}
frame->data_size = dblk_copy_to(frame->payload, frame->data+14, frame->data_size-14)+14;
// TODO: if !auto_crc, then fill crc
return 1;
}
int ether_unpack(ether_frame_t *frame)
{
uint8_t *ptr = frame->data;
if (frame->data_size < 64) {
return 0;
}
frame->dest_mac_addr = ptr; ptr += 6;
frame->src_mac_addr = ptr; ptr += 6;
frame->type = br16(ptr); ptr += 2;
frame->payload->size = frame->data_size-12-2-4;
//memcpy(frame->payload, ptr, frame->payload_size);
frame->payload->data = ptr;
ptr += frame->payload->size;
frame->crc = br32(ptr);
return 1;
}
int ether_ll_recv(ether_t* ether, ether_frame_t *frame)
{
dblk_node_new_from_stack(&frame->payload, NULL, 0);
frame->data_size = ether->recv(ether->interface,
frame->data, frame->data_size);
if (frame->data_size > 0) {
if (!ether_unpack(frame)) {
return 0;
}
// dispatch
if (frame->type == ETHER_TYPE_ARP) {
arp_packet_t arp_pkt = {
.data = frame->payload->data,
.data_size = frame->payload->size,
};
ether_arp_ether_recv(ether, &arp_pkt);
}
if (frame->type == ETHER_TYPE_IP) {
ip_t *ip = ether_arp_get_ip(ether);
ip_datagram_t ip_datagram = { .raw_data = frame->payload, };
ip_ll_recv(ip, &ip_datagram);
}
return 1;
}
return 0;
}
int ether_hl_send(ether_t* ether, ether_frame_t *frame)
{
frame->src_mac_addr = ether->mac_address,
ether_pack(frame);
return ether->send(ether->interface, frame->data, frame->data_size);
}