Goby3  3.1.4
2024.02.22
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 
38 namespace goby
39 {
40 namespace acomms
41 {
42 // 32 bit IPv4 address
43 class 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
59 class 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 
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  }
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
110 template <unsigned Id>
111 class 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 
120 uint16_t net_checksum(const std::string& data);
121 
122 } // namespace acomms
123 } // namespace goby
124 
125 #endif
goby::acomms::Bitset
dccl::Bitset Bitset
Definition: dccl.h:126
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::set_dont_fragment
void set_dont_fragment(bool value)
Definition: network_header.pb.h:1949
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::fragment_offset
::google::protobuf::uint32 fragment_offset() const
Definition: network_header.pb.h:1993
goby::acomms::IPGatewayEmptyIdentifierCodec
Definition: ip_codecs.h:111
goby::acomms::protobuf::IPv4Header_FlagsFragOffset
Definition: network_header.pb.h:653
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::dont_fragment
bool dont_fragment() const
Definition: network_header.pb.h:1945
goby::acomms::net_checksum
uint16_t net_checksum(const std::string &data)
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::set_more_fragments
void set_more_fragments(bool value)
Definition: network_header.pb.h:1973
goby::acomms::NetShortCodec
Definition: ip_codecs.h:59
network_header.pb.h
goby::acomms::IPv4AddressCodec
Definition: ip_codecs.h:43
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::set_fragment_offset
void set_fragment_offset(::google::protobuf::uint32 value)
Definition: network_header.pb.h:1997
goby::acomms::protobuf::IPv4Header_FlagsFragOffset::more_fragments
bool more_fragments() const
Definition: network_header.pb.h:1969
goby::acomms::IPv4FlagsFragOffsetCodec
Definition: ip_codecs.h:79