Leosac  0.8.0
Open Source Access Control
rplethprotocol.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "rplethprotocol.hpp"
27 #include <tools/log.hpp>
28 #include <vector>
29 
30 using namespace Leosac::Module::Rpleth;
31 
33  bool from_server /* = false */)
34 {
36  if (from_server)
37  {
38  // this is used for unit testing
39  buffer.fastForward(1);
41  }
42  std::size_t toRead = buffer.toRead();
43 
44  packet.status = Success;
45  packet.isGood = false;
46  if (toRead < PacketMinSize)
47  return (packet);
48 
49  packet.dataLen = buffer[SizeByteIdx];
50  if (toRead < packet.dataLen + 4U)
51  return (packet);
52  if (packet.dataLen)
53  {
54  packet.data = std::vector<Byte>(packet.dataLen);
55  for (unsigned int i = 0; i < packet.dataLen; ++i)
56  packet.data[i] = buffer[SizeByteIdx + 1 + i];
57  }
58  packet.type = buffer[TypeByteIdx];
59  packet.command = buffer[CommandByteIdx];
60  packet.sum = buffer[SizeByteIdx + packet.dataLen + 1];
61  packet.isGood =
62  true; // The packet has enough information to be interpreted by the protocol
63  buffer.fastForward(4 + packet.dataLen); // Circular buffer was actually read but
64  // indexes were not updated
65  if (packet.type >= MaxType)
66  packet.status = BadType;
67  else if (packet.sum != packet.checksum())
68  packet.status = BadChecksum;
69  return (packet);
70 }
71 
72 std::size_t RplethProtocol::encodeCommand(const RplethPacket &packet, Byte *buffer,
73  std::size_t size)
74 {
75  if (size < packet.dataLen + 5U) // Buffer is too small
76  return (0);
78  {
79  buffer[0] = packet.status;
80  ++buffer;
81  }
82  buffer[TypeByteIdx] = packet.type;
83  buffer[CommandByteIdx] = packet.command;
84  buffer[SizeByteIdx] = packet.dataLen;
85  for (int i = 0; i < packet.dataLen; ++i)
86  buffer[SizeByteIdx + i + 1] = packet.data[i];
87  buffer[SizeByteIdx + packet.dataLen + 1] = packet.checksum();
88 
90  return (packet.dataLen + 4 + 1);
91  else
92  return (packet.dataLen + 4);
93 }
Leosac::Module::Rpleth::RplethProtocol::BadChecksum
@ BadChecksum
Definition: rplethprotocol.hpp:95
Leosac::Module::Rpleth::RplethPacket::sender
Sender sender
Definition: rplethpacket.hpp:68
Leosac::Module::Rpleth::RplethProtocol::CommandByteIdx
static const std::size_t CommandByteIdx
Definition: rplethprotocol.hpp:44
Leosac::Module::Rpleth::RplethProtocol::decodeCommand
static RplethPacket decodeCommand(CircularBuffer &buffer, bool from_server=false)
Decode a packet from a circular buffer object.
Definition: rplethprotocol.cpp:32
Leosac::Module::Rpleth::RplethPacket
Definition: rplethpacket.hpp:39
Leosac::Module::Rpleth::RplethPacket::Sender::Client
@ Client
Leosac::Module::Rpleth::RplethProtocol::encodeCommand
static std::size_t encodeCommand(const RplethPacket &packet, Byte *buffer, std::size_t size)
Definition: rplethprotocol.cpp:72
Leosac::Module::Rpleth::RplethProtocol::Success
@ Success
Definition: rplethprotocol.hpp:93
Leosac::Module::Rpleth::RplethPacket::Sender::Server
@ Server
Leosac::Module::Rpleth::RplethPacket::type
Byte type
Definition: rplethpacket.hpp:62
Leosac::Module::Rpleth::RplethPacket::sum
Byte sum
Definition: rplethpacket.hpp:66
Leosac::Module::Rpleth::CircularBuffer::fastForward
void fastForward(std::size_t offset)
Definition: circularbuffer.cpp:81
Leosac::Module::Rpleth::RplethPacket::dataLen
Byte dataLen
Definition: rplethpacket.hpp:64
Leosac::Module::Rpleth
Namespace where implementation for Rpleth support takes place.
Definition: circularbuffer.hpp:38
Leosac::Module::Rpleth::RplethPacket::status
Byte status
Definition: rplethpacket.hpp:61
Leosac::Module::Rpleth::RplethProtocol::TypeByteIdx
static const std::size_t TypeByteIdx
Definition: rplethprotocol.hpp:43
Leosac::Module::Rpleth::RplethProtocol::MaxType
@ MaxType
Definition: rplethprotocol.hpp:56
Leosac::Module::Rpleth::CircularBuffer::toRead
std::size_t toRead() const
Definition: circularbuffer.cpp:101
log.hpp
rplethprotocol.hpp
Rpleth protocol implementation.
Leosac::Module::Rpleth::RplethProtocol::PacketMinSize
static const std::size_t PacketMinSize
Definition: rplethprotocol.hpp:46
Leosac::Module::Rpleth::CircularBuffer
Implementation of a ring buffer.
Definition: circularbuffer.hpp:43
Leosac::Module::Rpleth::RplethProtocol::BadType
@ BadType
Definition: rplethprotocol.hpp:98
Leosac::Module::Rpleth::RplethPacket::command
Byte command
Definition: rplethpacket.hpp:63
Leosac::Module::Rpleth::RplethPacket::data
std::vector< Byte > data
Definition: rplethpacket.hpp:65
Leosac::Module::Rpleth::RplethPacket::isGood
bool isGood
Definition: rplethpacket.hpp:67
Byte
std::uint8_t Byte
Definition: bufferutils.hpp:31
Leosac::Module::Rpleth::RplethProtocol::SizeByteIdx
static const std::size_t SizeByteIdx
Definition: rplethprotocol.hpp:45
Leosac::Module::Rpleth::RplethPacket::checksum
Byte checksum() const
Definition: rplethpacket.cpp:66