-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax25.cpp
361 lines (277 loc) · 7.57 KB
/
ax25.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// (C) 2022-2023 by folkert van heusden <[email protected]>, released under Apache License v2.0
#include <assert.h>
#include <chrono>
#include <map>
#include <stdint.h>
#include <string>
#include <string.h>
#include "ax25.h"
#include "str.h"
const std::map<uint8_t, std::string> pid_names = {
{ 0x10, "AX.25 layer 3 implemented" },
{ 0x20, "AX.25 layer 3 implemented" },
{ 0x01, "ISO 8208/CCiTT X.25 PLP" },
{ 0x06, "Compressed TCP/IP packet. Van Jacobson (RFC 1144)" },
{ 0x07, "Uncompressed TCP/IP packet. Van Jacobson (RFC 1144)" },
{ 0x08, "Segmentation fragment" },
{ 0xC3, "TEXNET datagram protocol" },
{ 0xC4, "Link Quality Protocol" },
{ 0xCA, "Appletalk" },
{ 0xCB, "Appletalk ARP" },
{ 0xCC, "ARPA Internet Protocol" },
{ 0xCD, "ARPA Address resolution" },
{ 0xCE, "FlexNet" },
{ 0xCF, "Net/ROM" },
{ 0xF0, "No layer 3 protocol implemented" },
{ 0xFF, "Escape character. Next octet contains more Level 3 protocol" },
};
ax25_address::ax25_address()
{
}
ax25_address::ax25_address(const std::vector<uint8_t> & from)
{
if (from.size() < 7) {
invalid_reason = myformat("address too short %zu < 7 ", from.size());
return;
}
bool end = false;
for(int i=0; i<6; i++) {
uint8_t b = from[i];
if ((b & 1) && i != 5) { // why not in byte 5?
invalid_reason = myformat("byte %d has lsb set", i);
return;
}
char c = char(b >> 1);
if (c == 0 || c == 32 || end)
end = true;
else
address += c;
}
end_mark = from[6] & 1;
repeated = (from[6] & 128) == 128;
ssid = (from[6] >> 1) & 0x0f;
valid = true;
}
ax25_address::ax25_address(const ax25_address & a)
{
valid = a.get_valid ();
invalid_reason = a.get_invalid_reason();
address = a.get_address ();
ssid = a.get_ssid ();
end_mark = a.get_end_mark();
repeated = a.get_repeated();
}
ax25_address::ax25_address(const std::string & a, const int ssid, const bool end_mark, const bool repeated)
{
this->address = a;
this->ssid = ssid;
this->end_mark = end_mark;
this->repeated = repeated;
this->valid = true;
}
ax25_address::ax25_address(const std::string & a, const bool end_mark, const bool repeated)
{
std::size_t dash = a.find("-");
if (dash != std::string::npos) {
this->address = a.substr(0, dash);
this->ssid = a[dash + 1] - '0';
}
else {
this->address = a;
this->ssid = 0;
}
this->end_mark = end_mark;
this->repeated = repeated;
this->valid = true;
}
ax25_address & ax25_address::operator=(const ax25_address & in)
{
address = in.get_address();
ssid = in.get_ssid();
end_mark = in.get_end_mark();
repeated = in.get_repeated();
valid = in.get_valid();
invalid_reason = in.get_invalid_reason();
return *this;
}
bool ax25_address::operator==(const ax25_address & other) const
{
if (other.get_valid() != valid)
return false;
if (other.get_address() != address)
return false;
if (other.get_ssid() != ssid)
return false;
return true;
}
void ax25_address::set_address(const std::string & address, const int ssid)
{
this->address = address;
this->ssid = ssid;
}
std::vector<uint8_t> ax25_address::generate_address() const
{
std::vector<uint8_t> out(7);
size_t put_n = std::min(size_t(6), address.size());
for(size_t i=0; i<std::min(size_t(6), address.size()); i++)
out[i] = address[i] << 1;
for(size_t i=put_n; i<6; i++)
out[i] = ' ' << 1;
out[6] = (ssid << 1) | end_mark | (repeated ? 128 : 0);
return out;
}
ax25_packet::ax25_packet()
{
}
ax25_packet::ax25_packet(const std::vector<uint8_t> & in)
{
if (in.size() < 14) {
invalid_reason = myformat("packet too short (%zu bytes)", in.size());
return;
}
to = ax25_address(std::vector<uint8_t>(in.begin() + 0, in.begin() + 7));
if (!to.get_valid()) {
invalid_reason = "to invalid: " + to.get_invalid_reason();
return;
}
from = ax25_address(std::vector<uint8_t>(in.begin() + 7, in.begin() + 14));
if (!from.get_valid()) {
invalid_reason = "from invalid: " + from.get_invalid_reason();
return;
}
bool end_mark = from.get_end_mark();
std::size_t offset = 14;
for(int i=0; i<2 && end_mark == false; i++) {
ax25_address a(std::vector<uint8_t>(in.begin() + offset, in.begin() + offset + 7));
offset += 7;
end_mark = a.get_end_mark();
if (!a.get_valid()) {
invalid_reason = "via invalid: " + a.get_invalid_reason();
return;
}
repeaters.push_back(a);
}
control = in[offset++];
if ((control & 1) == 0 || (control & 0xef) == 0x03) {
pid = in[offset++];
type = (control & 0xef) == 0x03 ? TYPE_UI : TYPE_I;
}
else {
if (control & 2)
type = TYPE_U;
else
type = TYPE_S;
}
if (offset < in.size())
data = std::vector<uint8_t>(in.data() + offset, in.data() + in.size() - offset);
valid = true;
}
ax25_packet::~ax25_packet()
{
}
ax25_address ax25_packet::get_from() const
{
return from;
}
ax25_address ax25_packet::get_to() const
{
return to;
}
std::vector<ax25_address> ax25_packet::get_repeaters() const
{
return repeaters;
}
void ax25_packet::add_repeater(const ax25_address & addr)
{
for(auto & repeater : repeaters) {
if (repeater == addr)
return;
}
repeaters.push_back(addr);
}
auto ax25_packet::get_data() const
{
return data;
}
void ax25_packet::set_from(const std::string & callsign, const int ssid, const bool end_mark, const bool repeated)
{
from = ax25_address(callsign, ssid, end_mark, repeated);
}
void ax25_packet::set_to(const std::string & callsign, const int ssid, const bool end_mark, const bool repeated)
{
to = ax25_address(callsign, ssid, end_mark, repeated);
}
void ax25_packet::set_data(const uint8_t *const p, const size_t size)
{
data = std::vector<uint8_t>(p, p + size);
}
void ax25_packet::set_control(const uint8_t control)
{
this->control = control;
}
void ax25_packet::set_type(const frame_type f)
{
if (f == TYPE_I)
control &= 254;
else {
control |= 1;
if (f == TYPE_S)
control &= ~2;
else
control |= 2;
}
}
void ax25_packet::set_pid(const uint8_t pid)
{
this->pid = pid;
}
std::optional<uint8_t> ax25_packet::get_pid() const
{
return pid;
}
std::pair<uint8_t *, size_t> ax25_packet::generate_packet() const
{
int data_size = data.size();
uint8_t *out = reinterpret_cast<uint8_t *>(calloc(1, data_size + 128 /* more than enough for an ax.25 header */));
auto addr_to = to.generate_address();
memcpy(&out[0], addr_to.data(), 7);
auto copy_from = from;
if (repeaters.empty() == false)
copy_from.reset_end_mark();
auto addr_from = copy_from.generate_address();
memcpy(&out[7], addr_from.data(), 7);
int offset = 14;
for(size_t i=0; i<repeaters.size(); i++) {
auto copy_repeater = repeaters.at(i);
if (i != repeaters.size() - 1)
copy_repeater.reset_end_mark();
else
copy_repeater.set_end_mark();
auto addr_repeater = copy_repeater.generate_address();
memcpy(&out[offset], addr_repeater.data(), 7);
offset += 7;
}
out[offset++] = control;
if ((control & 1) == 0 || (control & 0xef) == 0x03) // I or UI
out[offset++] = pid.has_value() ? pid.value() : 0;
memcpy(&out[offset], data.data(), data_size);
return { out, data_size + offset };
}
std::string ax25_packet::to_str() const
{
std::string repeaters_str;
for(auto & repeater : repeaters) {
if (repeaters_str.empty() == false)
repeaters_str += " / ";
else
repeaters_str += ", repeaters:";
repeaters_str += repeater.to_str();
}
std::string pid_str;
if (type == ax25_packet::TYPE_I && pid.has_value()) {
auto it = pid_names.find(pid.value());
if (it != pid_names.end())
pid_str = ", PID: " + it->second;
}
return myformat("valid:%d, from:%s, to:%s%s control:%02x%s", valid, from.to_str().c_str(), to.to_str().c_str(), repeaters_str.c_str(), control, pid_str.c_str());
}