Permalink
Browse files

Files for Appendix A04, https://www.youtube.com/watch?v=HDhG5Vzzxjk

  • Loading branch information...
AlgorithMan-de committed Sep 12, 2016
1 parent f8d8b49 commit 58e5ccd03d1e27c2d1ff3ba36008007de7df0ea6
Showing with 151 additions and 27 deletions.
  1. +1 −0 include/net/arp.h
  2. +41 −0 include/net/icmp.h
  3. +1 −0 makefile
  4. +9 −13 src/drivers/amd_am79c973.cpp
  5. +6 −7 src/kernel.cpp
  6. +22 −1 src/net/arp.cpp
  7. +1 −2 src/net/etherframe.cpp
  8. +61 −0 src/net/icmp.cpp
  9. +9 −4 src/net/ipv4.cpp
View
@@ -46,6 +46,7 @@ namespace myos
void RequestMACAddress(common::uint32_t IP_BE);
common::uint64_t GetMACFromCache(common::uint32_t IP_BE);
common::uint64_t Resolve(common::uint32_t IP_BE);
void BroadcastMACAddress(common::uint32_t IP_BE);
};
View
@@ -0,0 +1,41 @@
#ifndef __MYOS__NET__ICMP_H
#define __MYOS__NET__ICMP_H
#include <common/types.h>
#include <net/ipv4.h>
namespace myos
{
namespace net
{
struct InternetControlMessageProtocolMessage
{
common::uint8_t type;
common::uint8_t code;
common::uint16_t checksum;
common::uint32_t data;
} __attribute__((packed));
class InternetControlMessageProtocol : InternetProtocolHandler
{
public:
InternetControlMessageProtocol(InternetProtocolProvider* backend);
~InternetControlMessageProtocol();
bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE,
common::uint8_t* internetprotocolPayload, common::uint32_t size);
void RequestEchoReply(common::uint32_t ip_be);
};
}
}
#endif
View
@@ -27,6 +27,7 @@ objects = obj/loader.o \
obj/net/etherframe.o \
obj/net/arp.o \
obj/net/ipv4.o \
obj/net/icmp.o \
obj/kernel.o
@@ -140,8 +140,6 @@ int amd_am79c973::Reset()
uint32_t amd_am79c973::HandleInterrupt(common::uint32_t esp)
{
printf("INTERRUPT FROM AMD am79c973\n");
registerAddressPort.Write(0);
uint32_t temp = registerDataPort.Read();
@@ -150,7 +148,7 @@ uint32_t amd_am79c973::HandleInterrupt(common::uint32_t esp)
if((temp & 0x1000) == 0x1000) printf("AMD am79c973 MISSED FRAME\n");
if((temp & 0x0800) == 0x0800) printf("AMD am79c973 MEMORY ERROR\n");
if((temp & 0x0400) == 0x0400) Receive();
if((temp & 0x0200) == 0x0200) printf("AMD am79c973 DATA SENT\n");
if((temp & 0x0200) == 0x0200) printf(" SENT");
// acknoledge
registerAddressPort.Write(0);
@@ -175,8 +173,8 @@ void amd_am79c973::Send(uint8_t* buffer, int size)
src >= buffer; src--, dst--)
*dst = *src;
printf("Sending: ");
for(int i = 0; i < size; i++)
printf("\nSENDING: ");
for(int i = 0; i < (size>64?64:size); i++)
{
printfHex(buffer[i]);
printf(" ");
@@ -192,7 +190,7 @@ void amd_am79c973::Send(uint8_t* buffer, int size)
void amd_am79c973::Receive()
{
printf("AMD am79c973 DATA RECEIVED\n");
printf("\nRECEIVING: ");
for(; (recvBufferDescr[currentRecvBuffer].flags & 0x80000000) == 0;
currentRecvBuffer = (currentRecvBuffer + 1) % 8)
@@ -206,18 +204,16 @@ void amd_am79c973::Receive()
size -= 4;
uint8_t* buffer = (uint8_t*)(recvBufferDescr[currentRecvBuffer].address);
if(handler != 0)
if(handler->OnRawDataReceived(buffer, size))
Send(buffer, size);
size = 64;
for(int i = 0; i < size; i++)
for(int i = 0; i < (size>64?64:size); i++)
{
printfHex(buffer[i]);
printf(" ");
}
if(handler != 0)
if(handler->OnRawDataReceived(buffer, size))
Send(buffer, size);
}
recvBufferDescr[currentRecvBuffer].flags2 = 0;
View
@@ -18,6 +18,7 @@
#include <net/etherframe.h>
#include <net/arp.h>
#include <net/ipv4.h>
#include <net/icmp.h>
// #define GRAPHICSMODE
@@ -313,16 +314,14 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
| (uint32_t)subnet1;
InternetProtocolProvider ipv4(&etherframe, &arp, gip_be, subnet_be);
InternetControlMessageProtocol icmp(&ipv4);
//etherframe.Send(0xFFFFFFFFFFFF, 0x0608, (uint8_t*)"FOO", 3);
//eth0->Send((uint8_t*)"Hello Network", 13);
interrupts.Activate();
printf("\n\n\n\n\n\n\n\n");
//arp.Resolve(gip_be);
ipv4.Send(gip_be, 0x0008, (uint8_t*) "foobar", 6);
printf("\n\n\n\n\n\n\n\n\n\n");
arp.BroadcastMACAddress(gip_be);
icmp.RequestEchoReply(gip_be);
while(1)
View
@@ -15,7 +15,8 @@ AddressResolutionProtocol::AddressResolutionProtocol(EtherFrameProvider* backend
AddressResolutionProtocol::~AddressResolutionProtocol()
{
}
bool AddressResolutionProtocol::OnEtherFrameReceived(uint8_t* etherframePayload, uint32_t size)
{
if(size < sizeof(AddressResolutionProtocolMessage))
@@ -59,6 +60,26 @@ bool AddressResolutionProtocol::OnEtherFrameReceived(uint8_t* etherframePayload,
return false;
}
void AddressResolutionProtocol::BroadcastMACAddress(uint32_t IP_BE)
{
AddressResolutionProtocolMessage arp;
arp.hardwareType = 0x0100; // ethernet
arp.protocol = 0x0008; // ipv4
arp.hardwareAddressSize = 6; // mac
arp.protocolAddressSize = 4; // ipv4
arp.command = 0x0200; // "response"
arp.srcMAC = backend->GetMACAddress();
arp.srcIP = backend->GetIPAddress();
arp.dstMAC = Resolve(IP_BE);
arp.dstIP = IP_BE;
this->Send(arp.dstMAC, (uint8_t*)&arp, sizeof(AddressResolutionProtocolMessage));
}
void AddressResolutionProtocol::RequestMACAddress(uint32_t IP_BE)
{
View
@@ -45,7 +45,7 @@ EtherFrameProvider::EtherFrameProvider(amd_am79c973* backend)
EtherFrameProvider::~EtherFrameProvider()
{
}
bool EtherFrameProvider::OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size)
{
if(size < sizeof(EtherFrameHeader))
@@ -60,7 +60,6 @@ bool EtherFrameProvider::OnRawDataReceived(common::uint8_t* buffer, common::uint
if(handlers[frame->etherType_BE] != 0)
sendBack = handlers[frame->etherType_BE]->OnEtherFrameReceived(
buffer + sizeof(EtherFrameHeader), size - sizeof(EtherFrameHeader));
}
if(sendBack)
View
@@ -0,0 +1,61 @@
#include <net/icmp.h>
using namespace myos;
using namespace myos::common;
using namespace myos::net;
InternetControlMessageProtocol::InternetControlMessageProtocol(InternetProtocolProvider* backend)
: InternetProtocolHandler(backend, 0x01)
{
}
InternetControlMessageProtocol::~InternetControlMessageProtocol()
{
}
void printf(char*);
void printfHex(uint8_t);
bool InternetControlMessageProtocol::OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE,
common::uint8_t* internetprotocolPayload, common::uint32_t size)
{
if(size < sizeof(InternetControlMessageProtocolMessage))
return false;
InternetControlMessageProtocolMessage* msg = (InternetControlMessageProtocolMessage*)internetprotocolPayload;
switch(msg->type)
{
case 0:
printf("ping response from "); printfHex(srcIP_BE & 0xFF);
printf("."); printfHex((srcIP_BE >> 8) & 0xFF);
printf("."); printfHex((srcIP_BE >> 16) & 0xFF);
printf("."); printfHex((srcIP_BE >> 24) & 0xFF);
printf("\n");
break;
case 8:
msg->type = 0;
msg->checksum = 0;
msg->checksum = InternetProtocolProvider::Checksum((uint16_t*)msg,
sizeof(InternetControlMessageProtocolMessage));
return true;
}
return false;
}
void InternetControlMessageProtocol::RequestEchoReply(uint32_t ip_be)
{
InternetControlMessageProtocolMessage icmp;
icmp.type = 8; // ping
icmp.code = 0;
icmp.data = 0x3713; // 1337
icmp.checksum = 0;
icmp.checksum = InternetProtocolProvider::Checksum((uint16_t*)&icmp,
sizeof(InternetControlMessageProtocolMessage));
InternetProtocolHandler::Send(ip_be, (uint8_t*)&icmp, sizeof(InternetControlMessageProtocolMessage));
}
View
@@ -78,12 +78,14 @@ bool InternetProtocolProvider::OnEtherFrameReceived(uint8_t* etherframePayload,
ipmessage->srcIP = temp;
ipmessage->timeToLive = 0x40;
ipmessage->checksum = 0;
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)
{
@@ -115,25 +117,28 @@ void InternetProtocolProvider::Send(uint32_t dstIP_BE, uint8_t protocol, uint8_t
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);
return ((~temp & 0xFF00) >> 8) | ((~temp & 0x00FF) << 8);
}

0 comments on commit 58e5ccd

Please sign in to comment.