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 / icmp.cpp (465cc951e8775492f5f8e82bbb13d0e247a808fb) (1,169B) (mode 100644) [raw]
#include "net/icmp.hpp"

#include "print.hpp"

using namespace net;

ICMP::ICMP(IPProvider &backend) : IPHandler(backend, 0x01)
{
}

bool
ICMP::onIPReceived(NetOrder<std::uint32_t> srcIP,
                   NetOrder<std::uint32_t> /*dstIP*/,
                   span<std::uint8_t> msg)
{
    if (msg.size() < ICMPMsg::size()) {
        return false;
    }

    ICMPMsg icmpMsg(msg.data());

    switch (icmpMsg.type.BE) {
        case 0:
            kprint("ping response from ", srcIP.BE, "\n");
            break;

        case 8:
            icmpMsg.type = netOrder<std::uint8_t>(0);
            icmpMsg.checksum = netOrder<std::uint16_t>(0);
            icmpMsg.checksum = IPProvider::checksum(msg.first(ICMPMsg::size()));
            return true;
    }

    return false;
}

void
ICMP::requestEchoReply(NetOrder<std::uint32_t> ip)
{
    std::uint8_t data[ICMPMsg::size()];
    ICMPMsg icmp(data);
    icmp.type = netOrder<std::uint8_t>(8); // ping
    icmp.code = netOrder<std::uint8_t>(0);
    icmp.data = toNetOrder<std::uint32_t>(0x13370000);
    icmp.checksum = netOrder<std::uint16_t>(0);
    icmp.checksum = IPProvider::checksum(data);

    send(ip, data);
}
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