Branch data Line data Source code
1 : : /*
2 : : ** Copyright (C) 2023 Sylvain Fargier
3 : : **
4 : : ** This software is provided 'as-is', without any express or implied
5 : : ** warranty. In no event will the authors be held liable for any damages
6 : : ** arising from the use of this software.
7 : : **
8 : : ** Permission is granted to anyone to use this software for any purpose,
9 : : ** including commercial applications, and to alter it and redistribute it
10 : : ** freely, subject to the following restrictions:
11 : : **
12 : : ** 1. The origin of this software must not be misrepresented; you must not
13 : : ** claim that you wrote the original software. If you use this software
14 : : ** in a product, an acknowledgment in the product documentation would be
15 : : ** appreciated but is not required.
16 : : ** 2. Altered source versions must be plainly marked as such, and must not be
17 : : ** misrepresented as being the original software.
18 : : ** 3. This notice may not be removed or altered from any source distribution.
19 : : **
20 : : ** Created on: 2023-12-14T15:34:26
21 : : ** Author: Sylvain Fargier <fargier.sylvain@gmail.com>
22 : : */
23 : :
24 : : #ifndef MESSAGE_HPP__
25 : : #define MESSAGE_HPP__
26 : :
27 : : #include <vector>
28 : :
29 : : #include "../Buffer.hpp"
30 : : #include "../Cow.hpp"
31 : : #include "Addr.hpp"
32 : :
33 : : namespace ccut {
34 : : namespace net {
35 : :
36 : : /**
37 : : * @brief base message for IP based communication
38 : : *
39 : : */
40 : : struct Message
41 : : {
42 : : typedef buffer_view_t<cow_ptr<buffer_t>> data_t;
43 : :
44 : 7 : Message() = default;
45 : 2 : explicit Message(const net::address_t &to) : to{to} {}
46 : :
47 : 9 : Message(const net::address_t &from, const net::address_t &to) :
48 : 9 : from{from},
49 : 9 : to{to}
50 : 9 : {}
51 : 19 : Message(const Message &) = default;
52 : 5 : Message &operator=(const Message &) = default;
53 : 48 : virtual ~Message() = default;
54 : :
55 : 8 : Message(const net::address_t &from,
56 : : const net::address_t &to,
57 : 8 : const data_t &data) :
58 : 8 : from{from},
59 : 8 : to{to},
60 : 16 : data{data}
61 : 8 : {}
62 : :
63 : : template<typename iterator>
64 : 3 : Message(const net::address_t &from,
65 : : const net::address_t &to,
66 : : iterator start,
67 : : iterator end) :
68 : 3 : from{from},
69 : 3 : to{to},
70 : 6 : data{make_cow<buffer_t>(start, end)}
71 : 3 : {}
72 : : template<typename iterator>
73 : 3 : Message(const net::address_t &to, iterator start, iterator end) :
74 : 3 : Message(address_t(), to, start, end)
75 : 3 : {}
76 : :
77 : : inline Message makeReply() const { return Message{to, from}; }
78 : 1 : inline Message makeReply(const char *data, size_t len) const
79 : : {
80 : 1 : return Message{to, from, make_cow<buffer_t>(data, data + len)};
81 : : }
82 : :
83 : : /**
84 : : * @brief check message's sanity
85 : : * @details Messages are valid when both from/to are also valid
86 : : */
87 : : virtual bool isValid() const;
88 : 7 : inline operator bool() const { return isValid(); }
89 : :
90 : : static size_t getRawIPVersion(const Message::data_t &buffer);
91 : :
92 : : net::address_t from;
93 : : net::address_t to;
94 : : data_t data;
95 : : };
96 : :
97 : : /**
98 : : * @brief compute internet checksum over data.
99 : : * @details as described in RFC1071
100 : : * @param[in] data data to compute checksum on
101 : : * @param[in] len data length in bytes
102 : : * @param[in] initial initial cksum value
103 : : * @return uint16_t checksum
104 : : */
105 : : uint16_t cksum(const void *data, size_t len, uint16_t initial = 0xFFFF);
106 : :
107 : : /**
108 : : * @brief convenience function for internet checksum
109 : : */
110 : : template<typename iterator>
111 : 11 : uint16_t cksum(iterator begin, iterator end, uint16_t initial = 0xFFFF)
112 : : {
113 : 11 : return cksum(&(*begin), end - begin, initial);
114 : : }
115 : :
116 : : } // namespace net
117 : : } // namespace ccut
118 : :
119 : : #endif
|