rogue
Loading...
Searching...
No Matches
Header.cpp
Go to the documentation of this file.
1
18
19#include <arpa/inet.h>
20#include <inttypes.h>
21#include <stdint.h>
22#include <sys/time.h>
23
24#include <cstring>
25#include <iomanip>
26#include <iostream>
27#include <memory>
28#include <sstream>
29#include <string>
30
31#include "rogue/GeneralError.h"
32#include "rogue/GilRelease.h"
34
35namespace rpr = rogue::protocols::rssi;
37
39void rpr::Header::setUInt16(uint8_t* data, uint8_t byte, uint16_t value) {
40 *reinterpret_cast<uint16_t*>(&data[byte]) = htons(value);
41}
42
44uint16_t rpr::Header::getUInt16(uint8_t* data, uint8_t byte) {
45 return ntohs(*reinterpret_cast<uint16_t*>(&data[byte]));
46}
47
49void rpr::Header::setUInt32(uint8_t* data, uint8_t byte, uint32_t value) {
50 *reinterpret_cast<uint32_t*>(&(data[byte])) = htonl(value);
51}
52
54uint32_t rpr::Header::getUInt32(uint8_t* data, uint8_t byte) {
55 return ntohl(*reinterpret_cast<uint32_t*>(&(data[byte])));
56}
57
59uint16_t rpr::Header::compSum(uint8_t* data, uint8_t size) {
60 uint8_t x;
61 uint32_t sum;
62
63 sum = 0;
64 for (x = 0; x < size - 2; x = x + 2) sum += getUInt16(data, x);
65
66 sum = (sum % 0x10000) + (sum / 0x10000);
67 sum = sum ^ 0xFFFF;
68 return (sum);
69}
70
72rpr::HeaderPtr rpr::Header::create(ris::FramePtr frame) {
73 rpr::HeaderPtr r = std::make_shared<rpr::Header>(frame);
74 return (r);
75}
76
78rpr::Header::Header(ris::FramePtr frame) {
79 frame_ = frame;
80 count_ = 0;
81
82 syn = false;
83 ack = false;
84 rst = false;
85 nul = false;
86 busy = false;
87 // sequence = 0;
88 // acknowledge = 0;
89 // version = 0;
90 // chk = false;
91 // maxOutstandingSegments = 0;
92 // maxSegmentSize = 0;
93 // retransmissionTimeout = 0;
94 // cumulativeAckTimeout = 0;
95 // nullTimeout = 0;
96 // maxRetransmissions = 0;
97 // maxCumulativeAck = 0;
98 // timeoutUnit = 0;
99 // connectionId = 0;
100}
101
103rpr::Header::~Header() {}
104
106ris::FramePtr rpr::Header::getFrame() {
107 return (frame_);
108}
109
111bool rpr::Header::verify() {
112 uint8_t size;
113
114 if (frame_->isEmpty()) return (false);
115
116 ris::BufferPtr buff = *(frame_->beginBuffer());
117 uint8_t* data = buff->begin();
118
119 if (buff->getPayload() < HeaderSize) return (false);
120
121 syn = data[0] & 0x80;
122 ack = data[0] & 0x40;
123 rst = data[0] & 0x10;
124 nul = data[0] & 0x08;
125 busy = data[0] & 0x01;
126
127 size = (syn) ? SynSize : HeaderSize;
128
129 if ((data[1] != size) || (buff->getPayload() < size) || (getUInt16(data, size - 2) != compSum(data, size)))
130 return false;
131
132 sequence = data[2];
133 acknowledge = data[3];
134
135 if (!syn) return true;
136
137 version = data[4] >> 4;
138 chk = data[4] & 0x04;
139
140 maxOutstandingSegments = data[5];
141 maxSegmentSize = getUInt16(data, 6);
142 retransmissionTimeout = getUInt16(data, 8);
143 cumulativeAckTimeout = getUInt16(data, 10);
144 nullTimeout = getUInt16(data, 12);
145 maxRetransmissions = data[14];
146 maxCumulativeAck = data[15];
147 timeoutUnit = data[17];
148 connectionId = data[18];
149
150 return (true);
151}
152
154void rpr::Header::update() {
155 uint8_t size;
156
157 if (frame_->isEmpty()) throw(rogue::GeneralError("Header::update", "Frame is empty!"));
158
159 ris::BufferPtr buff = *(frame_->beginBuffer());
160 uint8_t* data = buff->begin();
161
162 size = (syn) ? SynSize : HeaderSize;
163
164 if (buff->getSize() < size)
165 throw(rogue::GeneralError::create("rssi::Header::update",
166 "Buffer size %" PRIu32 " is less size indicated in header %" PRIu8,
167 buff->getSize(),
168 size));
169
170 buff->minPayload(size);
171
172 memset(data, 0, size);
173 data[1] = size;
174
175 if (ack) data[0] |= 0x40;
176 if (rst) data[0] |= 0x10;
177 if (nul) data[0] |= 0x08;
178 if (busy) data[0] |= 0x01;
179
180 data[2] = sequence;
181 data[3] = acknowledge;
182
183 if (syn) {
184 data[0] |= 0x80;
185 data[4] |= 0x08;
186 data[4] |= (version << 4);
187 if (chk) data[4] |= 0x04;
188
189 data[5] = maxOutstandingSegments;
190
191 setUInt16(data, 6, maxSegmentSize);
192 setUInt16(data, 8, retransmissionTimeout);
193 setUInt16(data, 10, cumulativeAckTimeout);
194 setUInt16(data, 12, nullTimeout);
195
196 data[14] = maxRetransmissions;
197 data[15] = maxCumulativeAck;
198 data[17] = timeoutUnit;
199 data[18] = connectionId;
200 }
201
202 setUInt16(data, size - 2, compSum(data, size));
203 gettimeofday(&time_, NULL);
204 count_++;
205}
206
208struct timeval& rpr::Header::getTime() {
209 return (time_);
210}
211
213uint32_t rpr::Header::count() {
214 return (count_);
215}
216
218void rpr::Header::rstTime() {
219 gettimeofday(&time_, NULL);
220}
221
223std::string rpr::Header::dump() {
224 uint32_t x;
225
226 ris::BufferPtr buff = *(frame_->beginBuffer());
227 uint8_t* data = buff->begin();
228
229 std::stringstream ret("");
230
231 ret << " Total Size : " << std::dec << (*(frame_->beginBuffer()))->getPayload() << std::endl;
232 ret << " Raw Size : " << std::dec << (*(frame_->beginBuffer()))->getSize() << std::endl;
233 ret << " Raw Header : ";
234
235 for (x = 0; x < data[1]; x++) {
236 ret << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<uint32_t>(data[x]) << " ";
237 if ((x % 8) == 7 && (x + 1) != data[1]) ret << std::endl << " ";
238 }
239 ret << std::endl;
240
241 ret << " Syn : " << std::dec << syn << std::endl;
242 ret << " Ack : " << std::dec << ack << std::endl;
243 ret << " Rst : " << std::dec << rst << std::endl;
244 ret << " Nul : " << std::dec << nul << std::endl;
245 ret << " Busy : " << std::dec << busy << std::endl;
246 ret << " Sequence : " << std::dec << static_cast<uint32_t>(sequence) << std::endl;
247 ret << " Acknowledge : " << std::dec << static_cast<uint32_t>(acknowledge) << std::endl;
248
249 if (!syn) return (ret.str());
250
251 ret << " Version : " << std::dec << static_cast<uint32_t>(version) << std::endl;
252 ret << " Chk : " << std::dec << chk << std::endl;
253 ret << " Max Out Seg : " << std::dec << static_cast<uint32_t>(maxOutstandingSegments) << std::endl;
254 ret << " Max Seg Size : " << std::dec << static_cast<uint32_t>(maxSegmentSize) << std::endl;
255 ret << " Retran Tout : " << std::dec << static_cast<uint32_t>(retransmissionTimeout) << std::endl;
256 ret << " Cum Ack Tout : " << std::dec << static_cast<uint32_t>(cumulativeAckTimeout) << std::endl;
257 ret << " Null Tout : " << std::dec << static_cast<uint32_t>(nullTimeout) << std::endl;
258 ret << " Max Retrans : " << std::dec << static_cast<uint32_t>(maxRetransmissions) << std::endl;
259 ret << " Max Cum Ack : " << std::dec << static_cast<uint32_t>(maxCumulativeAck) << std::endl;
260 ret << " Timeout Unit : " << std::dec << static_cast<uint32_t>(timeoutUnit) << std::endl;
261 ret << " Conn Id : " << std::dec << static_cast<uint32_t>(connectionId) << std::endl;
262
263 return (ret.str());
264}
Generic Rogue exception type.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::protocols::rssi::Header > HeaderPtr
Definition Header.h:201