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