-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
157 lines (121 loc) · 4.37 KB
/
main.c
File metadata and controls
157 lines (121 loc) · 4.37 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <uart.h>
#include <network_controller.h>
#include <udp.h>
#include <utils.h>
#include <ethernet.h>
#include <arp.h>
#include <string.h>
#include <ip.h>
#include <net_utils.h>
#include <avr/io.h>
uint8_t buffer[900-67];
uint8_t tx_buffer[100];
uint8_t mac_addr[] = {0x80,0xe6,0x00,0x00,0x02,0xff};
uint8_t ip_addr[] = {192,168,1,230};
EthernetHeader eth_header_rcv;
EthernetHeader eth_header_tx;
ArpHeader arp_header_rcv;
ArpHeader arp_header_tx;
IpHeader ip_header_rcv;
IpHeader ip_header_tx;
void handleIcmp()
{
//ipPrintHeader(&ip_header_rcv);
if (ip_header_rcv.payload_ptr[0] != 8)
return;
uartWriteString("Echo(ping) recieved from: ");
utilsPrintIpAddress(ip_header_rcv.src_addr);
uartWriteString("\r\n");
uint8_t icmp_size = icmpPrepareResponce(ip_header_rcv.payload_ptr, ip_header_rcv.payload_size, tx_buffer, sizeof(tx_buffer));
ethernetBuildHeader(ð_header_tx, eth_header_rcv.src_mac, mac_addr, ETH_TYPE_IPv4);
ipPrepareHeader(&ip_header_tx, ip_header_rcv.src_addr, ip_addr, 1, icmp_size);
uint8_t *tmp_buffer = buffer;
tmp_buffer += ethernetHeaderToBuffer(ð_header_tx, tmp_buffer);
tmp_buffer += ipHeaderToBuffer(&ip_header_tx, tmp_buffer);
memcpy(tmp_buffer, tx_buffer, icmp_size);
tmp_buffer += icmp_size;
uint16_t data_size = (uint16_t)(tmp_buffer - buffer);
//uartWriteString("Data to write: ");
//utilsPrintHex(buffer, data_size);
networkControllerWriteByteStream(buffer, data_size);
}
void handleUdp()
{
ipPrintHeader(&ip_header_rcv);
uint8_t *udp_data = ip_header_rcv.payload_ptr;
uint16_t src_port = udp_data[0] << 8 | udp_data[1];
uint16_t dst_port = udp_data[2] << 8 | udp_data[3];
uint16_t size = udp_data[4] << 8 | udp_data[5];
uint16_t checksum = udp_data[6] << 8 | udp_data[7];
uartWriteString("\r\nSrc Port: ");
utilsPrintInt(src_port);
uartWriteString("\r\nDst Port: ");
utilsPrintInt(dst_port);
uartWriteString("\r\nSize: ");
utilsPrintInt(size);
uartWriteString("\r\nChecksum: ");
utilsPrintUint16(checksum);
uartWriteString("\r\n");
uint8_t * payload_data = udp_data + 8;
uint16_t payload_size = size - 8;
utilsWriteChars((const char*)payload_data, payload_size);
DDRA |= _BV(4);
if(memcmp(payload_data,"ON",2) == 0)
{
uartWriteString("led on\r\n");
PORTA |= _BV(4);
}
if(memcmp(payload_data,"OFF",3) == 0)
{
uartWriteString("led off\r\n");
PORTA &= ~_BV(4);
}
}
int main()
{
uartInit();
utilsWriteLine("Startup Net code");
networkControllerInit(mac_addr);
utilsWriteLine("NC init fin...");
while(1)
{
uint16_t data_size = networkControllerReadByteStream(buffer, sizeof(buffer));
if(data_size == 0)
continue;
ethernetParseHeader(ð_header_rcv, buffer, data_size);
if(eth_header_rcv.eth_type == ETH_TYPE_ARP)
{
arpParseHeader(&arp_header_rcv, eth_header_rcv.payload_ptr, eth_header_rcv.payload_size);
if(memcmp(arp_header_rcv.target_ip, ip_addr, 4))
continue;
utilsPrintSeperator();
ethernetPrintHeader(ð_header_rcv);
arpPrintHeader(&arp_header_rcv);
arpPrepareResponce(&arp_header_rcv, &arp_header_tx, mac_addr);
arpPrintHeader(&arp_header_tx);
ethernetBuildHeader(ð_header_tx, eth_header_rcv.src_mac, mac_addr, ETH_TYPE_ARP);
ethernetPrintHeader(ð_header_tx);
uint8_t * tmp_buffer = buffer;
tmp_buffer += ethernetHeaderToBuffer(ð_header_tx, tmp_buffer);
tmp_buffer += arpHeaderToBuffer(&arp_header_tx, tmp_buffer);
uint16_t data_size = (uint16_t)(tmp_buffer - buffer);
uartWriteString("Data to write: ");
utilsPrintHex(buffer, data_size);
networkControllerWriteByteStream(buffer, data_size);
}
if(eth_header_rcv.eth_type == ETH_TYPE_IPv4)
{
ipParseHeader(&ip_header_rcv, eth_header_rcv.payload_ptr, eth_header_rcv.payload_size);
//ICMP
if(ip_header_rcv.protocol == 0x01)
{
handleIcmp();
}
if(ip_header_rcv.protocol == 17)
{
handleUdp();
}
}
}
return 0;
}