Permalink
Browse files

Files for Appendix A05, https://www.youtube.com/watch?v=5gllcRuq8Mw

  • Loading branch information...
AlgorithMan-de committed Sep 12, 2016
1 parent 58e5ccd commit 239b80458ba14a04ac9a61a8d64d2b509cbd5a8a
Showing with 313 additions and 3 deletions.
  1. +1 −1 include/net/etherframe.h
  2. +1 −1 include/net/ipv4.h
  3. +88 −0 include/net/udp.h
  4. +1 −0 makefile
  5. +24 −1 src/kernel.cpp
  6. +4 −0 src/net/etherframe.cpp
  7. +194 −0 src/net/udp.cpp
View
@@ -38,7 +38,7 @@ namespace myos
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);
common::uint32_t GetIPAddress();
};
View
@@ -49,7 +49,7 @@ namespace myos
};
class InternetProtocolProvider : EtherFrameHandler
class InternetProtocolProvider : public EtherFrameHandler
{
friend class InternetProtocolHandler;
protected:
View
@@ -0,0 +1,88 @@
#ifndef __MYOS__NET__UDP_H
#define __MYOS__NET__UDP_H
#include <common/types.h>
#include <net/ipv4.h>
#include <memorymanagement.h>
namespace myos
{
namespace net
{
struct UserDatagramProtocolHeader
{
common::uint16_t srcPort;
common::uint16_t dstPort;
common::uint16_t length;
common::uint16_t checksum;
} __attribute__((packed));
class UserDatagramProtocolSocket;
class UserDatagramProtocolProvider;
class UserDatagramProtocolHandler
{
public:
UserDatagramProtocolHandler();
~UserDatagramProtocolHandler();
virtual void HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size);
};
class UserDatagramProtocolSocket
{
friend class UserDatagramProtocolProvider;
protected:
common::uint16_t remotePort;
common::uint32_t remoteIP;
common::uint16_t localPort;
common::uint32_t localIP;
UserDatagramProtocolProvider* backend;
UserDatagramProtocolHandler* handler;
bool listening;
public:
UserDatagramProtocolSocket(UserDatagramProtocolProvider* backend);
~UserDatagramProtocolSocket();
virtual void HandleUserDatagramProtocolMessage(common::uint8_t* data, common::uint16_t size);
virtual void Send(common::uint8_t* data, common::uint16_t size);
virtual void Disconnect();
};
class UserDatagramProtocolProvider : InternetProtocolHandler
{
protected:
UserDatagramProtocolSocket* sockets[65535];
common::uint16_t numSockets;
common::uint16_t freePort;
public:
UserDatagramProtocolProvider(InternetProtocolProvider* backend);
~UserDatagramProtocolProvider();
virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE,
common::uint8_t* internetprotocolPayload, common::uint32_t size);
virtual UserDatagramProtocolSocket* Connect(common::uint32_t ip, common::uint16_t port);
virtual UserDatagramProtocolSocket* Listen(common::uint16_t port);
virtual void Disconnect(UserDatagramProtocolSocket* socket);
virtual void Send(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size);
virtual void Bind(UserDatagramProtocolSocket* socket, UserDatagramProtocolHandler* handler);
};
}
}
#endif
View
@@ -28,6 +28,7 @@ objects = obj/loader.o \
obj/net/arp.o \
obj/net/ipv4.o \
obj/net/icmp.o \
obj/net/udp.o \
obj/kernel.o
View
@@ -19,6 +19,7 @@
#include <net/arp.h>
#include <net/ipv4.h>
#include <net/icmp.h>
#include <net/udp.h>
// #define GRAPHICSMODE
@@ -141,7 +142,19 @@ class MouseToConsole : public MouseEventHandler
};
class PrintfUDPHandler : public UserDatagramProtocolHandler
{
public:
void HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, common::uint8_t* data, common::uint16_t size)
{
char* foo = " ";
for(int i = 0; i < size; i++)
{
foo[0] = data[i];
printf(foo);
}
}
};
void sysprintf(char* str)
@@ -315,6 +328,8 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
InternetProtocolProvider ipv4(&etherframe, &arp, gip_be, subnet_be);
InternetControlMessageProtocol icmp(&ipv4);
UserDatagramProtocolProvider udp(&ipv4);
interrupts.Activate();
@@ -323,7 +338,15 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
arp.BroadcastMACAddress(gip_be);
icmp.RequestEchoReply(gip_be);
PrintfUDPHandler udphandler;
//UserDatagramProtocolSocket* udpsocket = udp.Connect(gip_be, 1234);
//udp.Bind(udpsocket, &udphandler);
//udpsocket->Send((uint8_t*)"Hello UDP!", 10);
UserDatagramProtocolSocket* udpsocket = udp.Listen(1234);
udp.Bind(udpsocket, &udphandler);
while(1)
{
#ifdef GRAPHICSMODE
View
@@ -31,6 +31,10 @@ void EtherFrameHandler::Send(common::uint64_t dstMAC_BE, common::uint8_t* data,
backend->Send(dstMAC_BE, etherType_BE, data, size);
}
uint32_t EtherFrameHandler::GetIPAddress()
{
return backend->GetIPAddress();
}
View
@@ -0,0 +1,194 @@
#include <net/udp.h>
using namespace myos;
using namespace myos::common;
using namespace myos::net;
UserDatagramProtocolHandler::UserDatagramProtocolHandler()
{
}
UserDatagramProtocolHandler::~UserDatagramProtocolHandler()
{
}
void UserDatagramProtocolHandler::HandleUserDatagramProtocolMessage(UserDatagramProtocolSocket* socket, uint8_t* data, uint16_t size)
{
}
UserDatagramProtocolSocket::UserDatagramProtocolSocket(UserDatagramProtocolProvider* backend)
{
this->backend = backend;
handler = 0;
listening = false;
}
UserDatagramProtocolSocket::~UserDatagramProtocolSocket()
{
}
void UserDatagramProtocolSocket::HandleUserDatagramProtocolMessage(uint8_t* data, uint16_t size)
{
if(handler != 0)
handler->HandleUserDatagramProtocolMessage(this, data, size);
}
void UserDatagramProtocolSocket::Send(uint8_t* data, uint16_t size)
{
backend->Send(this, data, size);
}
void UserDatagramProtocolSocket::Disconnect()
{
backend->Disconnect(this);
}
UserDatagramProtocolProvider::UserDatagramProtocolProvider(InternetProtocolProvider* backend)
: InternetProtocolHandler(backend, 0x11)
{
for(int i = 0; i < 65535; i++)
sockets[i] = 0;
numSockets = 0;
freePort = 1024;
}
UserDatagramProtocolProvider::~UserDatagramProtocolProvider()
{
}
bool UserDatagramProtocolProvider::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE,
uint8_t* internetprotocolPayload, uint32_t size)
{
if(size < sizeof(UserDatagramProtocolHeader))
return false;
UserDatagramProtocolHeader* msg = (UserDatagramProtocolHeader*)internetprotocolPayload;
uint16_t localPort = msg->dstPort;
uint16_t remotePort = msg->srcPort;
UserDatagramProtocolSocket* socket = 0;
for(uint16_t i = 0; i < numSockets && socket == 0; i++)
{
if( sockets[i]->localPort == msg->dstPort
&& sockets[i]->localIP == dstIP_BE
&& sockets[i]->listening)
{
socket = sockets[i];
socket->listening = false;
socket->remotePort = msg->srcPort;
socket->remoteIP = srcIP_BE;
}
else if( sockets[i]->localPort == msg->dstPort
&& sockets[i]->localIP == dstIP_BE
&& sockets[i]->remotePort == msg->srcPort
&& sockets[i]->remoteIP == srcIP_BE)
socket = sockets[i];
}
if(socket != 0)
socket->HandleUserDatagramProtocolMessage(internetprotocolPayload + sizeof(UserDatagramProtocolHeader),
size - sizeof(UserDatagramProtocolHeader));
return false;
}
UserDatagramProtocolSocket* UserDatagramProtocolProvider::Connect(uint32_t ip, uint16_t port)
{
UserDatagramProtocolSocket* socket = (UserDatagramProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(UserDatagramProtocolSocket));
if(socket != 0)
{
new (socket) UserDatagramProtocolSocket(this);
socket -> remotePort = port;
socket -> remoteIP = ip;
socket -> localPort = freePort++;
socket -> localIP = backend->GetIPAddress();
socket -> remotePort = ((socket -> remotePort & 0xFF00)>>8) | ((socket -> remotePort & 0x00FF) << 8);
socket -> localPort = ((socket -> localPort & 0xFF00)>>8) | ((socket -> localPort & 0x00FF) << 8);
sockets[numSockets++] = socket;
}
return socket;
}
UserDatagramProtocolSocket* UserDatagramProtocolProvider::Listen(uint16_t port)
{
UserDatagramProtocolSocket* socket = (UserDatagramProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(UserDatagramProtocolSocket));
if(socket != 0)
{
new (socket) UserDatagramProtocolSocket(this);
socket -> listening = true;
socket -> localPort = port;
socket -> localIP = backend->GetIPAddress();
socket -> localPort = ((socket -> localPort & 0xFF00)>>8) | ((socket -> localPort & 0x00FF) << 8);
sockets[numSockets++] = socket;
}
return socket;
}
void UserDatagramProtocolProvider::Disconnect(UserDatagramProtocolSocket* socket)
{
for(uint16_t i = 0; i < numSockets && socket == 0; i++)
if(sockets[i] == socket)
{
sockets[i] = sockets[--numSockets];
MemoryManager::activeMemoryManager->free(socket);
break;
}
}
void UserDatagramProtocolProvider::Send(UserDatagramProtocolSocket* socket, uint8_t* data, uint16_t size)
{
uint16_t totalLength = size + sizeof(UserDatagramProtocolHeader);
uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(totalLength);
uint8_t* buffer2 = buffer + sizeof(UserDatagramProtocolHeader);
UserDatagramProtocolHeader* msg = (UserDatagramProtocolHeader*)buffer;
msg->srcPort = socket->localPort;
msg->dstPort = socket->remotePort;
msg->length = ((totalLength & 0x00FF) << 8) | ((totalLength & 0xFF00) >> 8);
for(int i = 0; i < size; i++)
buffer2[i] = data[i];
msg -> checksum = 0;
InternetProtocolHandler::Send(socket->remoteIP, buffer, totalLength);
MemoryManager::activeMemoryManager->free(buffer);
}
void UserDatagramProtocolProvider::Bind(UserDatagramProtocolSocket* socket, UserDatagramProtocolHandler* handler)
{
socket->handler = handler;
}

0 comments on commit 239b804

Please sign in to comment.