Serenity Operating System
at hosted 73 lines 2.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/MACAddress.h> 30#include <Kernel/Net/IPv4.h> 31 32struct ICMPType { 33 enum { 34 EchoReply = 0, 35 EchoRequest = 8, 36 }; 37}; 38 39class [[gnu::packed]] ICMPHeader 40{ 41public: 42 ICMPHeader() {} 43 ~ICMPHeader() {} 44 45 u8 type() const { return m_type; } 46 void set_type(u8 b) { m_type = b; } 47 48 u8 code() const { return m_code; } 49 void set_code(u8 b) { m_code = b; } 50 51 u16 checksum() const { return m_checksum; } 52 void set_checksum(u16 w) { m_checksum = w; } 53 54 const void* payload() const { return this + 1; } 55 void* payload() { return this + 1; } 56 57private: 58 u8 m_type { 0 }; 59 u8 m_code { 0 }; 60 NetworkOrdered<u16> m_checksum { 0 }; 61 // NOTE: The rest of the header is 4 bytes 62}; 63 64static_assert(sizeof(ICMPHeader) == 4); 65 66struct [[gnu::packed]] ICMPEchoPacket 67{ 68 ICMPHeader header; 69 NetworkOrdered<u16> identifier; 70 NetworkOrdered<u16> sequence_number; 71 void* payload() { return this + 1; } 72 const void* payload() const { return this + 1; } 73};