Permalink
Browse files

Files for Appendix A06, https://www.youtube.com/watch?v=zrM-Ql7zTqo

  • Loading branch information...
AlgorithMan-de committed Nov 9, 2016
1 parent 239b804 commit c07440b8626640d46a48e089844ce33148ea3633
Showing with 351 additions and 0 deletions.
  1. +127 −0 include/net/tcp.h
  2. +224 −0 src/net/tcp.cpp
View
@@ -0,0 +1,127 @@
#ifndef __MYOS__NET__TCP_H
#define __MYOS__NET__TCP_H
namespace myos
{
namespace net
{
enum TransmissionControlProtocolSocketState
{
CLOSED,
LISTEN,
SYN_SENT,
SYN_RECEIVED,
ESTABLISHED,
FIN_WAIT1,
FIN_WAIT2,
CLOSING,
TIME_WAIT,
CLOSE_WAIT,
LAST_ACK,
};
enum TransmissionControlProtocolFlag
{
FIN = 1,
SYN = 2,
RST = 4,
PSH = 8,
ACK = 16,
URG = 32,
ECE = 64,
CWR = 128,
NS = 256
};
struct TransmissionControlProtocolHeader
{
common::uint16_t srcPort;
common::uint16_t dstPort;
common::uint32_t sequenceNumber;
common::uint32_t acknowledgementNumber;
common::uint8_t headerSize32 : 4;
common::uint8_t reserved : 3;
common::uint16_t flags : 9;
common::uint16_t windowSize;
common::uint16_t checksum;
common::uint16_t urgentPtr;
} __attribute__((packed));
class TransmissionControlProtocolSocket;
class TransmissionControlProtocolProvider;
class TransmissionControlProtocolHandler
{
public:
TransmissionControlProtocolHandler();
~TransmissionControlProtocolHandler();
virtual void HandleTransmissionControlProtocolMessage(TransmissionControlProtocolSocket* socket, common::uint8_t* data, common::uint16_t size);
};
class TransmissionControlProtocolSocket
{
friend class TransmissionControlProtocolProvider;
protected:
common::uint16_t remotePort;
common::uint32_t remoteIP;
common::uint16_t localPort;
common::uint32_t localIP;
common::uint32_t sequenceNumber;
TransmissionControlProtocolProvider* backend;
TransmissionControlProtocolHandler* handler;
TransmissionControlProtocolSocketState state;
public:
TransmissionControlProtocolSocket(TransmissionControlProtocolProvider* backend);
~TransmissionControlProtocolSocket();
virtual void HandleTransmissionControlProtocolMessage(common::uint8_t* data, common::uint16_t size);
virtual void Send(common::uint8_t* data, common::uint16_t size);
virtual void Disconnect();
};
class TransmissionControlProtocolProvider : InternetProtocolHandler
{
protected:
TransmissionControlProtocolSocket* sockets[65535];
common::uint16_t numSockets;
common::uint16_t freePort;
public:
TransmissionControlProtocolProvider(InternetProtocolProvider* backend);
~TransmissionControlProtocolProvider();
virtual bool OnInternetProtocolReceived(common::uint32_t srcIP_BE, common::uint32_t dstIP_BE,
common::uint8_t* internetprotocolPayload, common::uint32_t size);
virtual TransmissionControlProtocolSocket* Connect(common::uint32_t ip, common::uint16_t port);
virtual void Disconnect(TransmissionControlProtocolSocket* socket);
virtual void Send(TransmissionControlProtocolSocket* socket, common::uint8_t* data, common::uint16_t size);
virtual TransmissionControlProtocolSocket* Listen(common::uint16_t port);
virtual void Bind(TransmissionControlProtocolSocket* socket, TransmissionControlProtocolHandler* handler);
};
}
}
#endif
View
@@ -0,0 +1,224 @@
#include <net/tcp.h>
using namespace myos;
using namespace myos::common;
using namespace myos::net;
TransmissionControlProtocolHandler::TransmissionControlProtocolHandler()
{
}
TransmissionControlProtocolHandler::~TransmissionControlProtocolHandler()
{
}
void TransmissionControlProtocolHandler::HandleTransmissionControlProtocolMessage(TransmissionControlProtocolSocket* socket, uint8_t* data, uint16_t size)
{
}
TransmissionControlProtocolSocket::TransmissionControlProtocolSocket(TransmissionControlProtocolProvider* backend)
{
this->backend = backend;
handler = 0;
}
TransmissionControlProtocolSocket::~TransmissionControlProtocolSocket()
{
}
void TransmissionControlProtocolSocket::HandleTransmissionControlProtocolMessage(uint8_t* data, uint16_t size)
{
if(handler != 0)
handler->HandleTransmissionControlProtocolMessage(this, data, size);
}
void TransmissionControlProtocolSocket::Send(uint8_t* data, uint16_t size)
{
backend->Send(this, data, size);
}
void TransmissionControlProtocolSocket::Disconnect()
{
backend->Disconnect(this);
}
TransmissionControlProtocolProvider::TransmissionControlProtocolProvider(InternetProtocolProvider* backend)
: InternetProtocolHandler(backend, 0x06)
{
for(int i = 0; i < 65535; i++)
sockets[i] = 0;
numSockets = 0;
freePort = 1024;
}
TransmissionControlProtocolProvider::~TransmissionControlProtocolProvider()
{
}
bool TransmissionControlProtocolProvider::OnInternetProtocolReceived(uint32_t srcIP_BE, uint32_t dstIP_BE,
uint8_t* internetprotocolPayload, uint32_t size)
{
/*
for(uint16_t i = 0; i < numSockets && socket == 0; i++)
if(sockets[i] == socket)
{
sockets[i] = sockets[--numSockets];
MemoryManager::activeMemoryManager->free(socket);
break;
}
*/
if(size < sizeof(TransmissionControlProtocolHeader))
return false;
TransmissionControlProtocolHeader* msg = (TransmissionControlProtocolHeader*)internetprotocolPayload;
uint16_t localPort = msg->dstPort;
uint16_t remotePort = msg->srcPort;
TransmissionControlProtocolSocket* 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->HandleTransmissionControlProtocolMessage(internetprotocolPayload + sizeof(TransmissionControlProtocolHeader),
size - sizeof(TransmissionControlProtocolHeader));
return false;
}
// ------------------------------------------------------------------------------------------
void TransmissionControlProtocolProvider::Send(TransmissionControlProtocolSocket* socket, uint8_t* data, uint16_t size, uint16_t flags)
{
uint16_t totalLength = size + sizeof(TransmissionControlProtocolHeader);
uint8_t* buffer = (uint8_t*)MemoryManager::activeMemoryManager->malloc(totalLength);
uint8_t* buffer2 = buffer + sizeof(TransmissionControlProtocolHeader);
TransmissionControlProtocolHeader* msg = (TransmissionControlProtocolHeader*)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);
}
TransmissionControlProtocolSocket* TransmissionControlProtocolProvider::Connect(uint32_t ip, uint16_t port)
{
TransmissionControlProtocolSocket* socket = (TransmissionControlProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(TransmissionControlProtocolSocket));
if(socket != 0)
{
new (socket) TransmissionControlProtocolSocket(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;
socket -> state = SYN_SENT;
socket -> sequenceNumber = 0xbeefcafe;
Send(socket, 0,0, SYN);
}
return socket;
}
void TransmissionControlProtocolProvider::Disconnect(TransmissionControlProtocolSocket* socket)
{
socket->state = FIN_WAIT1;
Send(socket, 0,0, FIN + ACK);
socket->sequenceNumber++;
}
TransmissionControlProtocolSocket* TransmissionControlProtocolProvider::Listen(uint16_t port)
{
TransmissionControlProtocolSocket* socket = (TransmissionControlProtocolSocket*)MemoryManager::activeMemoryManager->malloc(sizeof(TransmissionControlProtocolSocket));
if(socket != 0)
{
new (socket) TransmissionControlProtocolSocket(this);
socket -> state = LISTENING;
socket -> localIP = backend->GetIPAddress();
socket -> localPort = ((port & 0xFF00)>>8) | ((port & 0x00FF) << 8);
sockets[numSockets++] = socket;
}
return socket;
}
void TransmissionControlProtocolProvider::Bind(TransmissionControlProtocolSocket* socket, TransmissionControlProtocolHandler* handler)
{
socket->handler = handler;
}

0 comments on commit c07440b

Please sign in to comment.