Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
ip_codecs.h
Go to the documentation of this file.
1// Copyright 2016-2021:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_ACOMMS_IP_CODECS_H
25#define GOBY_ACOMMS_IP_CODECS_H
26
27#include <cstdint> // for uint32_t, uint16_t
28#include <string> // for string
29
30#include <boost/any.hpp> // for bad_any_cast
31#include <boost/cstdint.hpp> // for uint32_t
32#include <dccl/bitset.h> // for Bitset
33#include <dccl/exception.h> // for NullValueException
34#include <dccl/field_codec_fixed.h> // for TypedFixedFieldC...
35
36#include "goby/acomms/protobuf/network_header.pb.h" // for IPv4Header::Flag...
37
38namespace goby
39{
40namespace acomms
41{
42// 32 bit IPv4 address
43class IPv4AddressCodec : public dccl::TypedFixedFieldCodec<std::uint32_t, std::string>
44{
45 private:
46 std::uint32_t pre_encode(const std::string& field_value) override;
47 std::string post_decode(const std::uint32_t& wire_value) override;
48
49 dccl::Bitset encode() override { return dccl::Bitset(size(), 0); }
50 dccl::Bitset encode(const std::uint32_t& wire_value) override
51 {
52 return dccl::Bitset(size(), wire_value);
53 }
54 std::uint32_t decode(dccl::Bitset* bits) override { return bits->to<std::uint32_t>(); }
55 unsigned size() override { return 32; }
56};
57
58// 16 bit IP unsigned short codec
59class NetShortCodec : public dccl::TypedFixedFieldCodec<std::uint32_t>
60{
61 private:
62 dccl::Bitset encode() override { return dccl::Bitset(size(), 0); }
63
64 dccl::Bitset encode(const std::uint32_t& wire_value) override
65 {
66 unsigned short val = wire_value & 0xFFFF;
67 unsigned short val_le = ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
68 return dccl::Bitset(size(), val_le);
69 }
70 std::uint32_t decode(dccl::Bitset* bits) override
71 {
72 unsigned short val = bits->to<std::uint32_t>();
73 return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
74 }
75 unsigned size() override { return 16; }
76};
77
78// 16 bit IP unsigned short codec
80 : public dccl::TypedFixedFieldCodec<goby::acomms::protobuf::IPv4Header::FlagsFragOffset>
81{
82 private:
83 dccl::Bitset encode() override { return dccl::Bitset(size(), 0); }
84
85 dccl::Bitset
86 encode(const goby::acomms::protobuf::IPv4Header::FlagsFragOffset& wire_value) override
87 {
88 unsigned short val = wire_value.fragment_offset() & 0x1FFF; // 13 bits
89 if (wire_value.dont_fragment())
90 val |= 0x4000;
91 if (wire_value.more_fragments())
92 val |= 0x2000;
93 unsigned short val_le = ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
94 return dccl::Bitset(size(), val_le);
95 }
96 goby::acomms::protobuf::IPv4Header::FlagsFragOffset decode(dccl::Bitset* bits) override
97 {
98 unsigned short val_le = bits->to<std::uint32_t>();
99 unsigned short val = ((val_le & 0xFF) << 8) | ((val_le >> 8) & 0xFF);
101 ret.set_fragment_offset(val & 0x1FFF);
102 ret.set_dont_fragment(val & 0x4000);
103 ret.set_more_fragments(val & 0x2000);
104 return ret;
105 }
106 unsigned size() override { return 16; }
107};
108
109// no-op identifier codec
110template <unsigned Id>
111class IPGatewayEmptyIdentifierCodec : public dccl::TypedFixedFieldCodec<std::uint32_t>
112{
113 private:
114 dccl::Bitset encode() override { return dccl::Bitset(0, 0); }
115 dccl::Bitset encode(const std::uint32_t& /*wire_value*/) override { return dccl::Bitset(0, 0); }
116 std::uint32_t decode(dccl::Bitset* /*bits*/) override { return Id; }
117 unsigned size() override { return 0; }
118};
119
120uint16_t net_checksum(const std::string& data);
121
122} // namespace acomms
123} // namespace goby
124
125#endif
uint16_t net_checksum(const std::string &data)
The global namespace for the Goby project.