xaizek / tos (License: GPLv3 only) (since 2018-12-07)
This is an alternative version of sources presented as part of Write Your Own OS video tutorial by Viktor Engelmann.
<root> / src / net / etherframe.hpp (ba4a5f7f824db99857385ee304f4f8c464bcf1b3) (1,751B) (mode 100644) [raw]
#ifndef TOS__NET__ETHERFRAME_HPP__
#define TOS__NET__ETHERFRAME_HPP__

#include "drv/drivers.hpp"
#include "utils/span.hpp"
#include "utils/struct.hpp"

namespace net {

static constexpr NetOrder<std::uint64_t> broadcastMAC(0xffffffffffff);

struct EtherFrameHeader
{
    NetField<+0,  6, std::uint64_t> dstMAC;
    NetField<+6,  6, std::uint64_t> srcMAC;
    NetField<+12, 2, std::uint16_t> etherType;

    explicit EtherFrameHeader(std::uint8_t data[])
        : dstMAC(data), srcMAC(data), etherType(data)
    {
    }

    static constexpr std::ptrdiff_t size()
    {
        using lastField = decltype(etherType);
        return lastField::offset + lastField::size;
    }
};

using EtherFrameFooter = std::uint32_t;

class EtherFrameProvider;

class EtherFrameHandler
{
public:
    EtherFrameHandler(EtherFrameProvider &backend, std::uint16_t etherType);
    ~EtherFrameHandler();

public:
    void send(NetOrder<std::uint64_t> dstMAC, span<const std::uint8_t> msg);

    NetOrder<std::uint32_t> getIPAddress() const;

public:
    virtual bool onEtherFrameReceived(span<std::uint8_t> msg) = 0;

protected:
    EtherFrameProvider &backend;
    NetOrder<std::uint16_t> etherType;
};

class EtherFrameProvider : public drv::RawDataHandler
{
    friend class EtherFrameHandler;

public:
    EtherFrameProvider(drv::EthernetDriver &backend);

public:
    void send(NetOrder<std::uint64_t> dstMAC, NetOrder<std::uint16_t> etherType,
              span<const std::uint8_t> msg);

    NetOrder<std::uint64_t> getMACAddress() const;
    NetOrder<std::uint32_t> getIPAddress() const;

private:
    virtual bool onRawDataReceived(span<std::uint8_t> msg) override;

private:
    EtherFrameHandler *handlers[65535];
};

}

#endif // TOS__NET__ETHERFRAME_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/tos

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/tos

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master