-
Notifications
You must be signed in to change notification settings - Fork 45
/
status.c
320 lines (274 loc) · 7.36 KB
/
status.c
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
// Low-level encode/decode routines for TLV status packets from/to ka9q-radio's radiod
// Copyright 2020-2023 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <string.h>
#if defined(linux)
#include <bsd/string.h>
#endif
#include <math.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
#include <netdb.h>
#include "misc.h"
#include "status.h"
#include "radio.h"
extern int Mcast_ttl;
union result {
uint64_t ll;
uint32_t l;
float f;
double d;
};
// Encode 64-bit integer, big endian, leading zeroes suppressed
// The nice thing about big-endian encoding with suppressed leading zeroes
// is that all (unsigned) integer types can be easily encoded
// by simply casting them to uint64_t, without wasted space
int encode_int64(uint8_t **buf,enum status_type type,uint64_t x){
uint8_t *cp = *buf;
*cp++ = type;
if(x == 0){
// Compress zero value to zero length
*cp++ = 0;
*buf = cp;
return 2;
}
int len = sizeof(x);
while(len > 0 && ((x >> 56) == 0)){
x <<= 8;
len--;
}
*cp++ = len;
for(int i=0; i<len; i++){
*cp++ = x >> 56;
x <<= 8;
}
*buf = cp;
return 2+len;
}
// Special case: single null type byte means end of list
int encode_eol(uint8_t **buf){
uint8_t *bp = *buf;
*bp++ = EOL;
*buf = bp;
return 1;
}
int encode_byte(uint8_t **buf,enum status_type type,uint8_t x){
uint8_t *cp = *buf;
*cp++ = type;
if(x == 0){
// Compress zero value to zero length
*cp++ = 0;
*buf = cp;
return 2;
}
*cp++ = sizeof(x);
*cp++ = x;
*buf = cp;
return 2+sizeof(x);
}
int encode_int16(uint8_t **buf,enum status_type type,uint16_t x){
return encode_int64(buf,type,(uint64_t)x);
}
int encode_int32(uint8_t **buf,enum status_type type,uint32_t x){
return encode_int64(buf,type,(uint64_t)x);
}
int encode_int(uint8_t **buf,enum status_type type,int x){
return encode_int64(buf,type,(uint64_t)x);
}
// Floating types are also byte-swapped to big-endian order
int encode_float(uint8_t **buf,enum status_type type,float x){
if(isnan(x))
return 0; // Never encode a NAN
union result r;
r.f = x;
return encode_int32(buf,type,r.l);
}
int encode_double(uint8_t **buf,enum status_type type,double x){
if(isnan(x))
return 0; // Never encode a NAN
union result r;
r.d = x;
return encode_int64(buf,type,r.ll);
}
// Encode byte string without byte swapping
int encode_string(uint8_t **bp,enum status_type const type,void const *buf,unsigned int const buflen){
uint8_t const *orig_bpp = *bp;
uint8_t *cp = *bp;
*cp++ = type;
if(buflen < 128){
// send length directly
*cp++ = buflen;
} else if(buflen < 65536){
// Length is 2 bytes, big endian
*cp++ = 0x80 | 2;
*cp++ = buflen >> 8;
*cp++ = buflen;
} else if(buflen < 16777216){
*cp++ = 0x80 | 3;
*cp++ = buflen >> 16;
*cp++ = buflen >> 8;
*cp++ = buflen;
} else { // Handle more than 4 GB??
*cp++ = 0x80 | 4;
*cp++ = buflen >> 24;
*cp++ = buflen >> 16;
*cp++ = buflen >> 8;
*cp++ = buflen;
}
memcpy(cp,buf,buflen);
cp += buflen;
*bp = cp;
return cp - orig_bpp;
}
// Unique to spectrum energies
// array -> vector of 32-bit floats
// size = number of floats
// Sent in big endian order just like other floats
// Because it can be very long, handle large sizes
int encode_vector(uint8_t **bp,enum status_type type,float const *array,int size){
uint8_t const *orig_bp = *bp;
uint8_t *cp = *bp;
*cp++ = type;
int const bytes = sizeof(*array) * size; // Number of bytes in data
if(bytes < 128){
*cp++ = bytes; // Send length directly
} else if(bytes < 65536){
*cp++ = 0x80 | 2; // length takes 2 bytes
*cp++ = bytes >> 8;
*cp++ = bytes;
} else if(bytes < 16777216){
*cp++ = 0x80 | 3;
*cp++ = bytes >> 16;
*cp++ = bytes >> 8;
*cp++ = bytes;
} else {
*cp++ = 0x80 | 4;
*cp++ = bytes >> 24;
*cp++ = bytes >> 16;
*cp++ = bytes >> 8;
*cp++ = bytes;
}
// Encode the individual array elements
// Right now they're DC....maxpositive maxnegative...minnegative
for(int i=0;i < size;i++){
// Swap but don't bother compressing leading zeroes for now
union {
uint32_t i;
float f;
} foo;
foo.f = array[i];
*cp++ = foo.i >> 24;
*cp++ = foo.i >> 16;
*cp++ = foo.i >> 8;
*cp++ = foo.i;
}
*bp = cp;
return cp - orig_bp;
}
// Decode byte string without byte swapping
// NB! optlen has already been 'fixed' by the caller in case it's >= 128
// Now allocates the result from the heap, the caller must free.
// OTOH, the caller doesn't have to figure out a buffer size anymore
char *decode_string(uint8_t const *cp,int optlen){
char *result = malloc(optlen+1);
if(result != NULL)
memcpy(result,cp,optlen);
result[optlen] = '\0'; // force null termination
return result;
}
// Decode encoded variable-length UNSIGNED integers
// At entry, *bp -> length field (not type!)
// Works for byte, short/int16_t, long/int32_t, long long/int64_t
// If used for signed values, must be cast
uint64_t decode_int64(uint8_t const *cp,int len){
uint64_t result = 0;
// cp now points to beginning of abbreviated int
// Byte swap as we accumulate
while(len-- > 0)
result = (result << 8) | *cp++;
return result;
}
uint32_t decode_int32(uint8_t const *cp,int len){
return decode_int64(cp,len) & UINT32_MAX;
}
uint16_t decode_int16(uint8_t const *cp,int len){
return decode_int64(cp,len) & UINT16_MAX;
}
uint8_t decode_int8(uint8_t const *cp,int len){
return decode_int64(cp,len) & UINT8_MAX;
}
bool decode_bool(uint8_t const *cp,int len){
return decode_int64(cp,len) ? true : false;
}
int decode_int(uint8_t const *cp,int len){
return decode_int64(cp,len) & UINT_MAX; // mask to the size of an int
}
float decode_float(uint8_t const *cp,int len){
if(len == 0)
return 0;
if(len == 8)
return (float)decode_double(cp,len);
union result r;
r.ll = decode_int64(cp,len);
return r.f;
}
double decode_double(uint8_t const *cp,int len){
if(len == 0)
return 0;
if(len == 4)
return (double)decode_float(cp,len);
union result r;
r.ll = decode_int64(cp,len);
return r.d;
}
// The Linux/UNIX socket data structures are a real mess...
int encode_socket(uint8_t **buf,enum status_type type,void const *sock){
struct sockaddr_in const *sin = sock;
struct sockaddr_in6 const *sin6 = sock;
uint8_t *bp = *buf;
int optlen = 0;
switch(sin->sin_family){
case AF_INET:
optlen = 6;
*bp++ = type;
*bp++ = optlen;
memcpy(bp,&sin->sin_addr.s_addr,4); // Already in network order
bp += 4;
memcpy(bp,&sin->sin_port,2);
bp += 2;
break;
case AF_INET6:
optlen = 10;
*bp++ = type;
*bp++ = optlen;
memcpy(bp,&sin6->sin6_addr,8);
bp += 8;
memcpy(bp,&sin6->sin6_port,2);
bp += 2;
break;
default:
return 0; // Invalid, don't encode anything
}
*buf = bp;
return optlen;
}
struct sockaddr *decode_socket(void *sock,uint8_t const *val,int optlen){
struct sockaddr_in *sin = (struct sockaddr_in *)sock;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sock;
if(optlen == 6){
sin->sin_family = AF_INET;
memcpy(&sin->sin_addr.s_addr,val,4);
memcpy(&sin->sin_port,val+4,2);
return sock;
} else if(optlen == 10){
sin6->sin6_family = AF_INET6;
memcpy(&sin6->sin6_addr,val,8);
memcpy(&sin6->sin6_port,val+8,2);
return sock;
}
return NULL;
}