Commit 6de7a562 authored by Martin Vítek's avatar Martin Vítek

Updated ARP_protocol class

parent 1ebee1a3
......@@ -6,40 +6,42 @@
#include <machine/endian.h>
#include "ethernet_frame.h"
#include "NetworkTypes.h"
#include "Debug.h"
ARP_class::ARP_class(const uint8_t* mac): cache_index(0),
my_mac(mac)
ARP_protocol::ARP_protocol(const MAC& local_mac, const IP& local_ip): cache_index(0),
local_mac(local_mac),
local_ip(local_ip)
{
}
void ARP_class::save(const uint8_t (&IP)[4], const uint8_t (&MAC)[6])
void ARP_protocol::save(const IP& ip, const MAC& mac)
{
memcpy(cache[cache_index].IP, IP, 4);
memcpy(cache[cache_index].MAC, MAC, 6);
cache[cache_index].ip = ip;
cache[cache_index].mac = mac;
cache_index++;
if (cache_index == CACHE_SIZE) cache_index = 0;
}
bool ARP_class::get(const uint8_t (&IP)[4], uint8_t (&MAC)[6])
bool ARP_protocol::get(const IP& ip, MAC& mac)
{
if (search(IP, MAC)) return true;
if (search(ip, mac)) return true;
//TODO: Send ARP request? (or deal wit it in NixieStack)
return false;
}
bool ARP_class::search(const uint8_t (&IP)[4], uint8_t (&MAC)[6])
bool ARP_protocol::search(const IP& ip, MAC& mac)
{
for (uint8_t i=0; i<CACHE_SIZE; i++)
{
//IP found in cache
if (!memcmp(IP, cache[i].IP, 4))
if (cache[i].ip == ip)
{
memcpy(MAC, cache[i].MAC, 6);
mac = cache[i].mac;
return true;
}
......@@ -48,7 +50,7 @@ bool ARP_class::search(const uint8_t (&IP)[4], uint8_t (&MAC)[6])
return false;
}
void ARP_class::assembly_request(const uint8_t (&IP)[4], ethernet_payload& payload)
void ARP_protocol::assembly_request(const IP& ip, ethernet_payload& payload)
{
arp_packet* arp = (arp_packet*)payload.data;
......@@ -59,28 +61,26 @@ void ARP_class::assembly_request(const uint8_t (&IP)[4], ethernet_payload& paylo
arp->hw_size = 6;
arp->protocol_size = 4;
arp->operation_code = __htons(ARP_REQUEST);
uint8_t myIP[4] = {10, 20, 70, 145};
//My MAC
memcpy(arp->MAC_sender, my_mac, 6);
memcpy(arp->MAC_sender, local_mac.mac, 6);
//My IP (it can be 0.0.0.0 if IP address is not assigned yet)
memcpy(arp->IP_sender, myIP, 4);
memcpy(arp->IP_sender, local_ip, 4);
//Target MAC in question -> 00:00:00:00:00:00
memset(arp->MAC_target, 0, 6);
//Target IP
memcpy(arp->IP_target, IP, 4);
memcpy(arp->IP_target, ip, 4);
}
void ARP_class::assembly_reply(const uint8_t (&IP)[4], ethernet_payload& payload)
void ARP_protocol::assembly_reply(const IP& ip, ethernet_payload& payload)
{
//TODO: implementation
}
bool ARP_class::decode(ethernet_payload& payload)
bool ARP_protocol::decode(ethernet_payload& payload)
{
arp_packet* arp = (arp_packet*)payload.data;
......
......@@ -3,11 +3,12 @@
#include <cstdint>
#include "ethernet_frame.h"
#include "NetworkTypes.h"
struct arp_cache_item
{
uint8_t MAC[6];
uint8_t IP[4];
MAC mac;
IP ip;
};
#pragma pack(push,1)
......@@ -25,7 +26,7 @@ struct arp_packet
};
#pragma pack(pop)
class ARP_class
class ARP_protocol
{
public:
enum hardware_types : uint16_t
......@@ -63,18 +64,18 @@ class ARP_class
arp_cache_item cache[CACHE_SIZE];
uint8_t cache_index;
//const uint8_t (&my_mac)[6];
const uint8_t* my_mac;
const MAC& local_mac;
const IP& local_ip;
public:
ARP_class(const uint8_t* my_mac);
ARP_protocol(const MAC& local_mac, const IP& local_ip);
void save(const uint8_t (&IP)[4], const uint8_t (&MAC)[6]);
bool get(const uint8_t (&IP)[4], uint8_t (&MAC)[6]);
bool search(const uint8_t (&IP)[4], uint8_t (&MAC)[6]);
void save(const IP& ip, const MAC& mac);
bool get(const IP& ip, MAC& mac);
bool search(const IP& ip, MAC& mac);
void assembly_request(const uint8_t (&IP)[4], ethernet_payload& payload);
void assembly_reply(const uint8_t (&IP)[4], ethernet_payload& payload);
void assembly_request(const IP& ip, ethernet_payload& payload);
void assembly_reply(const IP& ip, ethernet_payload& payload);
bool decode(ethernet_payload& payload);
};
......@@ -6,7 +6,7 @@
class MAC
{
private:
public:
uint8_t mac[6];
public:
......@@ -27,6 +27,13 @@ class MAC
return this->mac;
}
uint8_t* operator=(const uint8_t* mac)
{
memcpy(this->mac, mac, sizeof(mac));
return this->mac;
}
uint8_t operator[](const uint8_t index)
{
if (index >= sizeof(mac)) return 0;
......@@ -57,7 +64,7 @@ class MAC
class IP
{
private:
public:
union
{
uint32_t value;
......
......@@ -3,10 +3,22 @@
#include <cstdint>
#include <machine/endian.h>
#include "NetworkTypes.h"
#include "ARP.h"
class NixieStack
{
private:
MAC mac;
IP ip;
ARP_protocol arp;
public:
NixieStack();
void set_MAC(const uint8_t (&mac)[6]);
void set_IP(const uint8_t (&ip)[4]);
};
......@@ -83,11 +83,13 @@ int main(void)
time_t old = 0;
bool up = true;
uint8_t my_mac[6] = {0x54, 0x10, 0xEC, 0x20, 0xE5, 0xBD};
uint8_t pc_mac[6] = {0x00, 0x30, 0x4f, 0x08, 0x35, 0x9c};
MAC my_mac({0x54, 0x10, 0xEC, 0x20, 0xE5, 0xBD});
MAC pc_mac({0x00, 0x30, 0x4f, 0x08, 0x35, 0x9c});
IP my_ip({192, 168, 1, 1});
ethernet_frame arp_frame;
ARP_class arp(my_mac);
ARP_protocol arp(my_mac, my_ip);
IP ip, ip2;
ip = {192, 168, 1, 1};
......@@ -103,7 +105,7 @@ int main(void)
uart.send_new_line();
arp.assembly_request({10, 20, 70, 113}, arp_frame.payload);
arp.assembly_request(MAC({10, 20, 70, 113}), arp_frame.payload);
memcpy(arp_frame.dest_address, broadcast_mac, 6);
memcpy(arp_frame.src_address, my_mac, 6);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment