forked from libcsp/libcsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsp_io.c
495 lines (383 loc) · 12.1 KB
/
csp_io.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 GomSpace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/* CSP includes */
#include <csp/csp.h>
#include <csp/csp_error.h>
#include <csp/csp_endian.h>
#include <csp/csp_crc32.h>
#include <csp/csp_rtable.h>
#include <csp/interfaces/csp_if_lo.h>
#include <csp/arch/csp_thread.h>
#include <csp/arch/csp_queue.h>
#include <csp/arch/csp_semaphore.h>
#include <csp/arch/csp_time.h>
#include <csp/arch/csp_malloc.h>
#include "crypto/csp_hmac.h"
#include "crypto/csp_xtea.h"
#include "csp_io.h"
#include "csp_port.h"
#include "csp_conn.h"
#include "csp_route.h"
#include "csp_promisc.h"
#include "csp_qfifo.h"
#include "transport/csp_transport.h"
/** CSP address of this node */
static uint8_t csp_my_address;
/* Hostname, model and build revision */
static const char *csp_hostname = NULL;
static const char *csp_model = NULL;
static const char *csp_revision = GIT_REV;
#ifdef CSP_USE_PROMISC
extern csp_queue_handle_t csp_promisc_queue;
#endif
void csp_set_address(uint8_t addr)
{
csp_my_address = addr;
}
uint8_t csp_get_address(void)
{
return csp_my_address;
}
void csp_set_hostname(const char *hostname)
{
csp_hostname = hostname;
}
const char *csp_get_hostname(void)
{
return csp_hostname;
}
void csp_set_model(const char *model)
{
csp_model = model;
}
const char *csp_get_model(void)
{
return csp_model;
}
void csp_set_revision(const char *revision)
{
csp_revision = revision;
}
const char *csp_get_revision(void)
{
return csp_revision;
}
int csp_init(unsigned char address) {
int ret;
/* Initialize CSP */
csp_set_address(address);
ret = csp_conn_init();
if (ret != CSP_ERR_NONE)
return ret;
ret = csp_port_init();
if (ret != CSP_ERR_NONE)
return ret;
ret = csp_qfifo_init();
if (ret != CSP_ERR_NONE)
return ret;
/* Loopback */
csp_iflist_add(&csp_if_lo);
/* Register loopback route */
csp_route_set(csp_get_address(), &csp_if_lo, CSP_NODE_MAC);
/* Also register loopback as default, until user redefines default route */
csp_route_set(CSP_DEFAULT_ROUTE, &csp_if_lo, CSP_NODE_MAC);
return CSP_ERR_NONE;
}
csp_socket_t * csp_socket(uint32_t opts) {
/* Validate socket options */
#ifndef CSP_USE_RDP
if (opts & CSP_SO_RDPREQ) {
csp_log_error("Attempt to create socket that requires RDP, but CSP was compiled without RDP support");
return NULL;
}
#endif
#ifndef CSP_USE_XTEA
if (opts & CSP_SO_XTEAREQ) {
csp_log_error("Attempt to create socket that requires XTEA, but CSP was compiled without XTEA support");
return NULL;
}
#endif
#ifndef CSP_USE_HMAC
if (opts & CSP_SO_HMACREQ) {
csp_log_error("Attempt to create socket that requires HMAC, but CSP was compiled without HMAC support");
return NULL;
}
#endif
#ifndef CSP_USE_CRC32
if (opts & CSP_SO_CRC32REQ) {
csp_log_error("Attempt to create socket that requires CRC32, but CSP was compiled without CRC32 support");
return NULL;
}
#endif
/* Drop packet if reserved flags are set */
if (opts & ~(CSP_SO_RDPREQ | CSP_SO_XTEAREQ | CSP_SO_HMACREQ | CSP_SO_CRC32REQ | CSP_SO_CONN_LESS)) {
csp_log_error("Invalid socket option");
return NULL;
}
/* Use CSP buffers instead? */
csp_socket_t * sock = csp_conn_allocate(CONN_SERVER);
if (sock == NULL)
return NULL;
/* If connectionless, init the queue to a pre-defined size
* if not, the user must init the queue using csp_listen */
if (opts & CSP_SO_CONN_LESS) {
sock->socket = csp_queue_create(CSP_CONN_QUEUE_LENGTH, sizeof(csp_packet_t *));
if (sock->socket == NULL)
return NULL;
} else {
sock->socket = NULL;
}
sock->opts = opts;
return sock;
}
csp_conn_t * csp_accept(csp_socket_t * sock, uint32_t timeout) {
if (sock == NULL)
return NULL;
if (sock->socket == NULL)
return NULL;
csp_conn_t * conn;
if (csp_queue_dequeue(sock->socket, &conn, timeout) == CSP_QUEUE_OK)
return conn;
return NULL;
}
csp_packet_t * csp_read(csp_conn_t * conn, uint32_t timeout) {
csp_packet_t * packet = NULL;
if (conn == NULL || conn->state != CONN_OPEN)
return NULL;
#ifdef CSP_USE_QOS
int prio, event;
if (csp_queue_dequeue(conn->rx_event, &event, timeout) != CSP_QUEUE_OK)
return NULL;
for (prio = 0; prio < CSP_RX_QUEUES; prio++)
if (csp_queue_dequeue(conn->rx_queue[prio], &packet, 0) == CSP_QUEUE_OK)
break;
#else
if (csp_queue_dequeue(conn->rx_queue[0], &packet, timeout) != CSP_QUEUE_OK)
return NULL;
#endif
#ifdef CSP_USE_RDP
/* Packet read could trigger ACK transmission */
if (conn->idin.flags & CSP_FRDP)
csp_rdp_check_ack(conn);
#endif
return packet;
}
int csp_send_direct(csp_id_t idout, csp_packet_t * packet, csp_iface_t * ifout, uint32_t timeout) {
if (packet == NULL) {
csp_log_error("csp_send_direct called with NULL packet");
goto err;
}
if ((ifout == NULL) || (ifout->nexthop == NULL)) {
csp_log_error("No route to host: %#08x", idout.ext);
goto err;
}
csp_log_packet("OUT: S %u, D %u, Dp %u, Sp %u, Pr %u, Fl 0x%02X, Sz %u VIA: %s",
idout.src, idout.dst, idout.dport, idout.sport, idout.pri, idout.flags, packet->length, ifout->name);
/* Copy identifier to packet (before crc, xtea and hmac) */
packet->id.ext = idout.ext;
#ifdef CSP_USE_PROMISC
/* Loopback traffic is added to promisc queue by the router */
if (idout.dst != csp_get_address() && idout.src == csp_get_address()) {
packet->id.ext = idout.ext;
csp_promisc_add(packet);
}
#endif
/* Only encrypt packets from the current node */
if (idout.src == csp_get_address()) {
/* Append HMAC */
if (idout.flags & CSP_FHMAC) {
#ifdef CSP_USE_HMAC
/* Calculate and add HMAC (does not include header for backwards compatability with csp1.x) */
if (csp_hmac_append(packet, false) != 0) {
/* HMAC append failed */
csp_log_warn("HMAC append failed!");
goto tx_err;
}
#else
csp_log_warn("Attempt to send packet with HMAC, but CSP was compiled without HMAC support. Discarding packet");
goto tx_err;
#endif
}
/* Append CRC32 */
if (idout.flags & CSP_FCRC32) {
#ifdef CSP_USE_CRC32
/* Calculate and add CRC32 (does not include header for backwards compatability with csp1.x) */
if (csp_crc32_append(packet, false) != 0) {
/* CRC32 append failed */
csp_log_warn("CRC32 append failed!");
goto tx_err;
}
#else
csp_log_warn("Attempt to send packet with CRC32, but CSP was compiled without CRC32 support. Sending without CRC32r");
idout.flags &= ~(CSP_FCRC32);
#endif
}
if (idout.flags & CSP_FXTEA) {
#ifdef CSP_USE_XTEA
/* Create nonce */
uint32_t nonce, nonce_n;
nonce = (uint32_t)rand();
nonce_n = csp_hton32(nonce);
memcpy(&packet->data[packet->length], &nonce_n, sizeof(nonce_n));
/* Create initialization vector */
uint32_t iv[2] = {nonce, 1};
/* Encrypt data */
if (csp_xtea_encrypt(packet->data, packet->length, iv) != 0) {
/* Encryption failed */
csp_log_warn("Encryption failed! Discarding packet");
goto tx_err;
}
packet->length += sizeof(nonce_n);
#else
csp_log_warn("Attempt to send XTEA encrypted packet, but CSP was compiled without XTEA support. Discarding packet");
goto tx_err;
#endif
}
}
/* Store length before passing to interface */
uint16_t bytes = packet->length;
uint16_t mtu = ifout->mtu;
if (mtu > 0 && bytes > mtu)
goto tx_err;
if ((*ifout->nexthop)(ifout, packet, timeout) != CSP_ERR_NONE)
goto tx_err;
ifout->tx++;
ifout->txbytes += bytes;
return CSP_ERR_NONE;
tx_err:
ifout->tx_error++;
err:
return CSP_ERR_TX;
}
int csp_send(csp_conn_t * conn, csp_packet_t * packet, uint32_t timeout) {
int ret;
if ((conn == NULL) || (packet == NULL) || (conn->state != CONN_OPEN)) {
csp_log_error("Invalid call to csp_send");
return 0;
}
#ifdef CSP_USE_RDP
if (conn->idout.flags & CSP_FRDP) {
if (csp_rdp_send(conn, packet, timeout) != CSP_ERR_NONE) {
csp_iface_t * ifout = csp_rtable_find_iface(conn->idout.dst);
if (ifout != NULL)
ifout->tx_error++;
csp_log_warn("RDP send failed!");
return 0;
}
}
#endif
csp_iface_t * ifout = csp_rtable_find_iface(conn->idout.dst);
ret = csp_send_direct(conn->idout, packet, ifout, timeout);
return (ret == CSP_ERR_NONE) ? 1 : 0;
}
int csp_send_prio(uint8_t prio, csp_conn_t * conn, csp_packet_t * packet, uint32_t timeout) {
conn->idout.pri = prio;
return csp_send(conn, packet, timeout);
}
int csp_transaction_persistent(csp_conn_t * conn, uint32_t timeout, void * outbuf, int outlen, void * inbuf, int inlen) {
int size = (inlen > outlen) ? inlen : outlen;
csp_packet_t * packet = csp_buffer_get(size);
if (packet == NULL)
return 0;
/* Copy the request */
if (outlen > 0 && outbuf != NULL)
memcpy(packet->data, outbuf, outlen);
packet->length = outlen;
if (!csp_send(conn, packet, timeout)) {
csp_buffer_free(packet);
return 0;
}
/* If no reply is expected, return now */
if (inlen == 0)
return 1;
packet = csp_read(conn, timeout);
if (packet == NULL)
return 0;
if ((inlen != -1) && ((int)packet->length != inlen)) {
csp_log_error("Reply length %u expected %u", packet->length, inlen);
csp_buffer_free(packet);
return 0;
}
memcpy(inbuf, packet->data, packet->length);
int length = packet->length;
csp_buffer_free(packet);
return length;
}
int csp_transaction(uint8_t prio, uint8_t dest, uint8_t port, uint32_t timeout, void * outbuf, int outlen, void * inbuf, int inlen) {
csp_conn_t * conn = csp_connect(prio, dest, port, 0, CSP_CONNECTION_SO);
if (conn == NULL)
return 0;
int status = csp_transaction_persistent(conn, timeout, outbuf, outlen, inbuf, inlen);
csp_close(conn);
return status;
}
csp_packet_t * csp_recvfrom(csp_socket_t * socket, uint32_t timeout) {
if ((socket == NULL) || (!(socket->opts & CSP_SO_CONN_LESS)))
return NULL;
csp_packet_t * packet = NULL;
csp_queue_dequeue(socket->socket, &packet, timeout);
return packet;
}
int csp_sendto(uint8_t prio, uint8_t dest, uint8_t dport, uint8_t src_port, uint32_t opts, csp_packet_t * packet, uint32_t timeout) {
packet->id.flags = 0;
if (opts & CSP_O_RDP) {
csp_log_error("Attempt to create RDP packet on connection-less socket");
return CSP_ERR_INVAL;
}
if (opts & CSP_O_HMAC) {
#ifdef CSP_USE_HMAC
packet->id.flags |= CSP_FHMAC;
#else
csp_log_error("Attempt to create HMAC authenticated packet, but CSP was compiled without HMAC support");
return CSP_ERR_NOTSUP;
#endif
}
if (opts & CSP_O_XTEA) {
#ifdef CSP_USE_XTEA
packet->id.flags |= CSP_FXTEA;
#else
csp_log_error("Attempt to create XTEA encrypted packet, but CSP was compiled without XTEA support");
return CSP_ERR_NOTSUP;
#endif
}
if (opts & CSP_O_CRC32) {
#ifdef CSP_USE_CRC32
packet->id.flags |= CSP_FCRC32;
#else
csp_log_error("Attempt to create CRC32 validated packet, but CSP was compiled without CRC32 support");
return CSP_ERR_NOTSUP;
#endif
}
packet->id.dst = dest;
packet->id.dport = dport;
packet->id.src = csp_get_address();
packet->id.sport = src_port;
packet->id.pri = prio;
csp_iface_t * ifout = csp_rtable_find_iface(dest);
if (csp_send_direct(packet->id, packet, ifout, timeout) != CSP_ERR_NONE)
return CSP_ERR_NOTSUP;
return CSP_ERR_NONE;
}
int csp_sendto_reply(csp_packet_t * request_packet, csp_packet_t * reply_packet, uint32_t opts, uint32_t timeout) {
if (request_packet == NULL)
return CSP_ERR_INVAL;
return csp_sendto(request_packet->id.pri, request_packet->id.src, request_packet->id.sport, request_packet->id.dport, opts, reply_packet, timeout);
}