Serenity Operating System
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 <Kernel/Net/IPv4.h>
30
31namespace Kernel {
32
33struct TCPFlags {
34 enum : u16 {
35 FIN = 0x01,
36 SYN = 0x02,
37 RST = 0x04,
38 PUSH = 0x08,
39 ACK = 0x10,
40 URG = 0x20
41 };
42};
43
44class [[gnu::packed]] TCPPacket
45{
46public:
47 TCPPacket() {}
48 ~TCPPacket() {}
49
50 size_t header_size() const { return data_offset() * sizeof(u32); }
51
52 u16 source_port() const { return m_source_port; }
53 void set_source_port(u16 port) { m_source_port = port; }
54
55 u16 destination_port() const { return m_destination_port; }
56 void set_destination_port(u16 port) { m_destination_port = port; }
57
58 u32 sequence_number() const { return m_sequence_number; }
59 void set_sequence_number(u32 number) { m_sequence_number = number; }
60
61 u32 ack_number() const { return m_ack_number; }
62 void set_ack_number(u32 number) { m_ack_number = number; }
63
64 u16 flags() const { return m_flags_and_data_offset & 0x1ff; }
65 void set_flags(u16 flags) { m_flags_and_data_offset = (m_flags_and_data_offset & ~0x1ff) | (flags & 0x1ff); }
66
67 bool has_syn() const { return flags() & TCPFlags::SYN; }
68 bool has_ack() const { return flags() & TCPFlags::ACK; }
69 bool has_fin() const { return flags() & TCPFlags::FIN; }
70 bool has_rst() const { return flags() & TCPFlags::RST; }
71
72 u8 data_offset() const { return (m_flags_and_data_offset & 0xf000) >> 12; }
73 void set_data_offset(u16 data_offset) { m_flags_and_data_offset = (m_flags_and_data_offset & ~0xf000) | data_offset << 12; }
74
75 u16 window_size() const { return m_window_size; }
76 void set_window_size(u16 window_size) { m_window_size = window_size; }
77
78 u16 checksum() const { return m_checksum; }
79 void set_checksum(u16 checksum) { m_checksum = checksum; }
80
81 u16 urgent() const { return m_urgent; }
82 void set_urgent(u16 urgent) { m_urgent = urgent; }
83
84 const void* payload() const { return ((const u8*)this) + header_size(); }
85 void* payload() { return ((u8*)this) + header_size(); }
86
87private:
88 NetworkOrdered<u16> m_source_port;
89 NetworkOrdered<u16> m_destination_port;
90 NetworkOrdered<u32> m_sequence_number;
91 NetworkOrdered<u32> m_ack_number;
92
93 NetworkOrdered<u16> m_flags_and_data_offset;
94 NetworkOrdered<u16> m_window_size;
95 NetworkOrdered<u16> m_checksum;
96 NetworkOrdered<u16> m_urgent;
97};
98
99static_assert(sizeof(TCPPacket) == 20);
100
101}