Permalink
Browse files

Files for Appendix A03, https://www.youtube.com/watch?v=swHIUtgdo5U

  • Loading branch information...
AlgorithMan-de committed Sep 12, 2016
1 parent f49f8a2 commit 7c4d8c3f646d6885da118a34557fb635519a8c0f
Showing with 249 additions and 13 deletions.
  1. +1 −1 include/drivers/amd_am79c973.h
  2. +1 −1 include/net/etherframe.h
  3. +77 −0 include/net/ipv4.h
  4. +1 −0 makefile
  5. +22 −10 src/kernel.cpp
  6. +7 −1 src/net/etherframe.cpp
  7. +140 −0 src/net/ipv4.cpp
@@ -25,7 +25,7 @@ namespace myos
RawDataHandler(amd_am79c973* backend);
~RawDataHandler();
bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size);
virtual bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size);
void Send(common::uint8_t* buffer, common::uint32_t size);
};
View
@@ -36,7 +36,7 @@ namespace myos
EtherFrameHandler(EtherFrameProvider* backend, common::uint16_t etherType);
~EtherFrameHandler();
bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size);
virtual bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size);
void Send(common::uint64_t dstMAC_BE, common::uint8_t* etherframePayload, common::uint32_t size);
};
View
@@ -0,0 +1,77 @@
#ifndef __MYOS__NET__IPV4_H
#define __MYOS__NET__IPV4_H
#include <common/types.h>
#include <net/etherframe.h>
#include <net/arp.h>
namespace myos
{
namespace net
{
struct InternetProtocolV4Message
{
common::uint8_t headerLength : 4;
common::uint8_t version : 4;
common::uint8_t tos;
common::uint16_t totalLength;
common::uint16_t ident;
common::uint16_t flagsAndOffset;
common::uint8_t timeToLive;
common::uint8_t protocol;
common::uint16_t checksum;
common::uint32_t srcIP;
common::uint32_t dstIP;
} __attribute__((packed));
class InternetProtocolProvider;
class InternetProtocolHandler
{
protected:
InternetProtocolProvider* backend;
common::uint8_t ip_protocol;
public:
InternetProtocolHandler(InternetProtocolProvider* backend, common::uint8_t protocol);
~InternetProtocolHandler();
virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE,
common::uint8_t* internetprotocolPayload, common::uint32_t size);
void Send(common::uint32_t dstIP_BE, common::uint8_t* internetprotocolPayload, common::uint32_t size);
};
class InternetProtocolProvider : EtherFrameHandler
{
friend class InternetProtocolHandler;
protected:
InternetProtocolHandler* handlers[255];
AddressResolutionProtocol* arp;
common::uint32_t gatewayIP;
common::uint32_t subnetMask;
public:
InternetProtocolProvider(EtherFrameProvider* backend,
AddressResolutionProtocol* arp,
common::uint32_t gatewayIP, common::uint32_t subnetMask);
~InternetProtocolProvider();
bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size);
void Send(common::uint32_t dstIP_BE, common::uint8_t protocol, common::uint8_t* buffer, common::uint32_t size);
static common::uint16_t Checksum(common::uint16_t* data, common::uint32_t lengthInBytes);
};
}
}
#endif
View
@@ -26,6 +26,7 @@ objects = obj/loader.o \
obj/gui/desktop.o \
obj/net/etherframe.o \
obj/net/arp.o \
obj/net/ipv4.o \
obj/kernel.o
View
@@ -17,6 +17,7 @@
#include <drivers/amd_am79c973.h>
#include <net/etherframe.h>
#include <net/arp.h>
#include <net/ipv4.h>
// #define GRAPHICSMODE
@@ -281,27 +282,37 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
*/
// IP 10.0.2.15
amd_am79c973* eth0 = (amd_am79c973*)(drvManager.drivers[2]);
// IP Address
uint8_t ip1 = 10, ip2 = 0, ip3 = 2, ip4 = 15;
uint32_t ip_be = ((uint32_t)ip4 << 24)
| ((uint32_t)ip3 << 16)
| ((uint32_t)ip2 << 8)
| (uint32_t)ip1;
eth0->SetIPAddress(ip_be);
EtherFrameProvider etherframe(eth0);
AddressResolutionProtocol arp(&etherframe);
// IP 10.0.2.2
// IP Address of the default gateway
uint8_t gip1 = 10, gip2 = 0, gip3 = 2, gip4 = 2;
uint32_t gip_be = ((uint32_t)gip4 << 24)
| ((uint32_t)gip3 << 16)
| ((uint32_t)gip2 << 8)
| (uint32_t)gip1;
uint8_t subnet1 = 255, subnet2 = 255, subnet3 = 255, subnet4 = 0;
uint32_t subnet_be = ((uint32_t)subnet4 << 24)
| ((uint32_t)subnet3 << 16)
| ((uint32_t)subnet2 << 8)
| (uint32_t)subnet1;
amd_am79c973* eth0 = (amd_am79c973*)(drvManager.drivers[2]);
eth0->SetIPAddress(ip_be);
EtherFrameProvider etherframe(eth0);
AddressResolutionProtocol arp(&etherframe);
InternetProtocolProvider ipv4(&etherframe, &arp, gip_be, subnet_be);
//etherframe.Send(0xFFFFFFFFFFFF, 0x0608, (uint8_t*)"FOO", 3);
//eth0->Send((uint8_t*)"Hello Network", 13);
@@ -310,7 +321,8 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
interrupts.Activate();
printf("\n\n\n\n\n\n\n\n");
arp.Resolve(gip_be);
//arp.Resolve(gip_be);
ipv4.Send(gip_be, 0x0008, (uint8_t*) "foobar", 6);
while(1)
View
@@ -17,7 +17,8 @@ EtherFrameHandler::EtherFrameHandler(EtherFrameProvider* backend, uint16_t ether
EtherFrameHandler::~EtherFrameHandler()
{
backend->handlers[etherType_BE] = 0;
if(backend->handlers[etherType_BE] == this)
backend->handlers[etherType_BE] = 0;
}
bool EtherFrameHandler::OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size)
@@ -47,6 +48,9 @@ EtherFrameProvider::~EtherFrameProvider()
bool EtherFrameProvider::OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size)
{
if(size < sizeof(EtherFrameHeader))
return false;
EtherFrameHeader* frame = (EtherFrameHeader*)buffer;
bool sendBack = false;
@@ -83,6 +87,8 @@ void EtherFrameProvider::Send(common::uint64_t dstMAC_BE, common::uint16_t ether
dst[i] = src[i];
backend->Send(buffer2, size + sizeof(EtherFrameHeader));
MemoryManager::activeMemoryManager->free(buffer2);
}
uint32_t EtherFrameProvider::GetIPAddress()
View
@@ -0,0 +1,140 @@
#include <net/ipv4.h>
using namespace myos;
using namespace myos::common;
using namespace myos::net;
InternetProtocolHandler::InternetProtocolHandler(InternetProtocolProvider* backend, uint8_t protocol)
{
this->backend = backend;
this->ip_protocol = protocol;
backend->handlers[protocol] = this;
}
InternetProtocolHandler::~InternetProtocolHandler()
{
if(backend->handlers[ip_protocol] == this)
backend->handlers[ip_protocol] = 0;
}
bool InternetProtocolHandler::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE,
uint8_t* internetprotocolPayload, uint32_t size)
{
return false;
}
void InternetProtocolHandler::Send(uint32_t dstIP_BE, uint8_t* internetprotocolPayload, uint32_t size)
{
backend->Send(dstIP_BE, ip_protocol, internetprotocolPayload, size);
}
InternetProtocolProvider::InternetProtocolProvider(EtherFrameProvider* backend,
AddressResolutionProtocol* arp,
uint32_t gatewayIP, uint32_t subnetMask)
: EtherFrameHandler(backend, 0x800)
{
for(int i = 0; i < 255; i++)
handlers[i] = 0;
this->arp = arp;
this->gatewayIP = gatewayIP;
this->subnetMask = subnetMask;
}
InternetProtocolProvider::~InternetProtocolProvider()
{
}
bool InternetProtocolProvider::OnEtherFrameReceived(uint8_t* etherframePayload, uint32_t size)
{
if(size < sizeof(InternetProtocolV4Message))
return false;
InternetProtocolV4Message* ipmessage = (InternetProtocolV4Message*)etherframePayload;
bool sendBack = false;
if(ipmessage->dstIP == backend->GetIPAddress())
{
int length = ipmessage->totalLength;
if(length > size)
length = size;
if(handlers[ipmessage->protocol] != 0)
sendBack = handlers[ipmessage->protocol]->OnInternetProtocolReceived(
ipmessage->srcIP, ipmessage->dstIP,
etherframePayload + 4*ipmessage->headerLength, length - 4*ipmessage->headerLength);
}
if(sendBack)
{
uint32_t temp = ipmessage->dstIP;
ipmessage->dstIP = ipmessage->srcIP;
ipmessage->srcIP = temp;
ipmessage->timeToLive = 0x40;
ipmessage->checksum = Checksum((uint16_t*)ipmessage, 4*ipmessage->headerLength);
}
return sendBack;
}
void InternetProtocolProvider::Send(uint32_t dstIP_BE, uint8_t protocol, uint8_t* data, uint32_t size)
{
uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(sizeof(InternetProtocolV4Message) + size);
InternetProtocolV4Message *message = (InternetProtocolV4Message*)buffer;
message->version = 4;
message->headerLength = sizeof(InternetProtocolV4Message)/4;
message->tos = 0;
message->totalLength = size + sizeof(InternetProtocolV4Message);
message->totalLength = ((message->totalLength & 0xFF00) >> 8)
| ((message->totalLength & 0x00FF) << 8);
message->ident = 0x0100;
message->flagsAndOffset = 0x0040;
message->timeToLive = 0x40;
message->protocol = protocol;
message->dstIP = dstIP_BE;
message->srcIP = backend->GetIPAddress();
message->checksum = 0;
message->checksum = Checksum((uint16_t*)message, sizeof(InternetProtocolV4Message));
uint8_t* databuffer = buffer + sizeof(InternetProtocolV4Message);
for(int i = 0; i < size; i++)
databuffer[i] = data[i];
uint32_t route = dstIP_BE;
if((dstIP_BE & subnetMask) != (message->srcIP & subnetMask))
route = gatewayIP;
backend->Send(arp->Resolve(route), this->etherType_BE, buffer, sizeof(InternetProtocolV4Message) + size);
MemoryManager::activeMemoryManager->free(buffer);
}
uint16_t InternetProtocolProvider::Checksum(uint16_t* data, uint32_t lengthInBytes)
{
uint32_t temp = 0;
for(int i = 0; i < lengthInBytes/2; i++)
temp += ((data[i] & 0xFF00) >> 8) | ((data[i] & 0x00FF) << 8);
if(lengthInBytes % 2)
temp += ((uint16_t)((char*)data)[lengthInBytes-1]) << 8;
while(temp & 0xFFFF0000)
temp = (temp & 0xFFFF) + (temp >> 16);
return ((temp & 0xFF00) >> 8) | ((temp & 0x00FF) << 8);
}

0 comments on commit 7c4d8c3

Please sign in to comment.