17
17
#include "blake2s-siv.h"
18
18
#include "halfsiphash.h"
19
19
#include <chacha20.h>
20
- #include "pbuf.h"
21
20
22
21
#define MODULE_NAME "nbus"
23
22
@@ -245,34 +244,25 @@ static nbus_ret_t nbus_pbuf_receive_data(struct nbus_pbuf *self, Nbus *nbus) {
245
244
}
246
245
247
246
248
- static nbus_ret_t nbus_pbuf_send (struct nbus_pbuf * self , void * buf , size_t len , uint8_t dst_id [4 ], uint8_t dst_ep ) {
249
- if ((len + 24 ) > self -> buf_size ) {
250
- return NBUS_RET_FAILED ;
251
- }
252
- memcpy (self -> buf + 24 , buf , len );
253
- self -> buf_len = len + 24 ;
247
+ static nbus_ret_t nbus_pbuf_transmit (struct nbus_pbuf * self , Nbus * nbus ) {
254
248
255
249
self -> buf [8 ] = 'n' ;
256
250
self -> buf [9 ] = '2' ;
257
251
258
- self -> buf [10 ] = len / 256 ;
259
- self -> buf [11 ] = len % 256 ;
252
+ self -> buf [10 ] = ( self -> buf_len - 24 ) / 256 ;
253
+ self -> buf [11 ] = ( self -> buf_len - 24 ) % 256 ;
260
254
261
255
self -> buf [12 ] = nbus_tx_counter / 256 ;
262
256
self -> buf [13 ] = nbus_tx_counter % 256 ;
263
257
nbus_tx_counter ++ ;
264
258
265
- /* Set IDs and endpoints. */
266
- self -> buf [14 ] = (dst_ep & 0xf ) << 4 | (nbus_my_ep & 0xf );
267
- self -> buf [15 ] = 0 ;
268
- memcpy (self -> buf + 16 , dst_id , 4 );
269
- memcpy (self -> buf + 20 , nbus_my_id , 4 );
270
-
271
- return NBUS_RET_OK ;
272
- }
259
+ /* buf[14] are endpoints. */
273
260
261
+ /* Clear flags */
262
+ /** @todo set flags */
263
+ self -> buf [15 ] = 0 ;
274
264
275
- static nbus_ret_t nbus_pbuf_transmit ( struct nbus_pbuf * self , Nbus * nbus ) {
265
+ /* buf[16-23] are destination and source id. */
276
266
277
267
/* Compute SIV first */
278
268
halfsiphash (self -> buf + 8 , self -> buf_len - 8 , self -> km , self -> buf , 8 );
@@ -302,6 +292,18 @@ static nbus_ret_t nbus_pbuf_dispatch(Nbus *self, struct nbus_pbuf *pbuf) {
302
292
/* Try to match the socket naively. Receive everything for now. */
303
293
for (size_t i = 0 ; i < NBUS_SOCKET_COUNT ; i ++ ) {
304
294
if (self -> sockets [i ].used && self -> sockets [i ].enabled ) {
295
+ /* If the socket is locally bound, check the destination ID. */
296
+ if (memcmp (self -> sockets [i ].local_id , (uint8_t [4 ]){0 , 0 , 0 , 0 }, 4 )) {
297
+ uint8_t dst_addr [4 ] = {0 };
298
+ uint8_t dst_ep = 0 ;
299
+ nbus_pbuf_get_destination (pbuf , dst_addr , & dst_ep );
300
+ if (memcmp (dst_addr , self -> sockets [i ].local_id , 4 ) || dst_ep != self -> sockets [i ].local_ep ) {
301
+ /* Not ours, not interested. */
302
+ continue ;
303
+ }
304
+
305
+ }
306
+
305
307
if (xQueueSend (self -> sockets [i ].rx_queue , & pbuf , 0 ) != pdTRUE ) {
306
308
/* Couldn't queue the pbuf, treat as failed reception. */
307
309
break ;
@@ -433,13 +435,35 @@ static datagram_ret_t nbus_socket_write(Datagram *datagram, const void *buf, siz
433
435
(void )len ;
434
436
(void )msg ;
435
437
438
+ /* Socket must be bound to a local ID and EP. */
439
+ if (!memcmp (self -> local_id , (uint8_t [4 ]){0 , 0 , 0 , 0 }, 4 )) {
440
+ return DATAGRAM_RET_FAILED ;
441
+ }
442
+
436
443
struct nbus_pbuf * pbuf = nbus_pbuf_allocate (self -> parent );
437
444
if (pbuf == NULL ) {
438
445
return DATAGRAM_RET_FAILED ;
439
446
}
440
447
441
- uint8_t dstid [4 ] = {0 , 0 , 0 , 1 };
442
- nbus_pbuf_send (pbuf , buf , len , dstid , 1 );
448
+ /* Check if there is space required for the whole datagram. */
449
+ if ((len + 24 ) > pbuf -> buf_size ) {
450
+ nbus_pbuf_release (self -> parent , pbuf );
451
+ return DATAGRAM_RET_FAILED ;
452
+ }
453
+
454
+ /* Set all required packet fields + copy data. */
455
+ nbus_pbuf_set_source (pbuf , self -> local_id , self -> local_ep );
456
+ if (memcmp (self -> remote_id , (uint8_t [4 ]){0 , 0 , 0 , 0 }, 4 )) {
457
+ nbus_pbuf_set_destination (pbuf , self -> remote_id , self -> remote_ep );
458
+ } else if (msg != NULL && msg -> addr_size == 4 && memcmp (msg -> dst_addr , (uint8_t [4 ]){0 , 0 , 0 , 0 }, 4 )) {
459
+ nbus_pbuf_set_destination (pbuf , msg -> dst_addr , msg -> dst_port );
460
+ } else {
461
+ /* Cannot determine destination ID. */
462
+ nbus_pbuf_release (self -> parent , pbuf );
463
+ return DATAGRAM_RET_FAILED ;
464
+ }
465
+ memcpy (pbuf -> buf + 24 , buf , len );
466
+ pbuf -> buf_len = len + 24 ;
443
467
444
468
if (xQueueSend (self -> tx_queue , & pbuf , 0 ) != pdTRUE ) {
445
469
nbus_pbuf_release (self -> parent , pbuf );
@@ -475,7 +499,13 @@ static datagram_ret_t nbus_socket_read(Datagram *datagram, void *buf, size_t *le
475
499
476
500
/* Interested in metadata? */
477
501
if (msg != NULL ) {
478
- /** @todo */
502
+ msg -> addr_size = 4 ;
503
+ uint8_t dst_ep = 0 ;
504
+ uint8_t src_ep = 0 ;
505
+ nbus_pbuf_get_destination (pbuf , msg -> dst_addr , & dst_ep );
506
+ msg -> dst_port = dst_ep ;
507
+ nbus_pbuf_get_source (pbuf , msg -> src_addr , & src_ep );
508
+ msg -> src_port = src_ep ;
479
509
}
480
510
481
511
/* Not needed anymore. */
@@ -617,6 +647,56 @@ nbus_ret_t nbus_pbuf_release(Nbus *self, struct nbus_pbuf *pbuf) {
617
647
}
618
648
619
649
650
+ nbus_ret_t nbus_pbuf_set_destination (struct nbus_pbuf * self , const uint8_t id [4 ], uint8_t ep ) {
651
+ self -> buf [14 ] &= ~0xf0 ;
652
+ self -> buf [14 ] |= (ep & 0x0f ) << 4 ;
653
+ memcpy (& (self -> buf [16 ]), id , 4 );
654
+
655
+ return NBUS_RET_OK ;
656
+ }
657
+
658
+
659
+ nbus_ret_t nbus_pbuf_set_source (struct nbus_pbuf * self , const uint8_t id [4 ], uint8_t ep ) {
660
+ self -> buf [14 ] &= ~0x0f ;
661
+ self -> buf [14 ] |= ep & 0x0f ;
662
+ memcpy (& (self -> buf [20 ]), id , 4 );
663
+
664
+ return NBUS_RET_OK ;
665
+ }
666
+
667
+
668
+ nbus_ret_t nbus_pbuf_get_destination (struct nbus_pbuf * self , uint8_t id [4 ], uint8_t * ep ) {
669
+ memcpy (id , & (self -> buf [16 ]), 4 );
670
+ * ep = self -> buf [14 ] >> 4 ;
671
+
672
+ return NBUS_RET_OK ;
673
+ }
674
+
675
+
676
+ nbus_ret_t nbus_pbuf_get_source (struct nbus_pbuf * self , uint8_t id [4 ], uint8_t * ep ) {
677
+ memcpy (id , & (self -> buf [20 ]), 4 );
678
+ * ep = self -> buf [14 ] >> 0x0f ;
679
+
680
+ return NBUS_RET_OK ;
681
+ }
682
+
683
+
684
+ static nbus_ret_t nbus_pbuf_send (struct nbus_pbuf * self , void * buf , size_t len , uint8_t dst_id [4 ], uint8_t dst_ep ) {
685
+ if ((len + 24 ) > self -> buf_size ) {
686
+ return NBUS_RET_FAILED ;
687
+ }
688
+ memcpy (self -> buf + 24 , buf , len );
689
+ self -> buf_len = len + 24 ;
690
+
691
+
692
+ /* Set IDs and endpoints. */
693
+ self -> buf [14 ] = (dst_ep & 0xf ) << 4 | (nbus_my_ep & 0xf );
694
+ memcpy (self -> buf + 16 , dst_id , 4 );
695
+ memcpy (self -> buf + 20 , nbus_my_id , 4 );
696
+
697
+ return NBUS_RET_OK ;
698
+ }
699
+
620
700
621
701
/* ******************************************** nbus socket manipulation **********************************************/
622
702
@@ -638,11 +718,11 @@ struct nbus_socket *nbus_socket_allocate(Nbus *self) {
638
718
/** @todo do not enable until bound */
639
719
self -> sockets [i ].enabled = true;
640
720
641
- self -> sockets [i ].local_id = 0 ;
642
- self -> sockets [i ].local_id_mask = 0 ;
721
+ memset ( self -> sockets [i ].local_id , 0 , 4 ) ;
722
+ memset ( self -> sockets [i ].local_id_mask , 0 , 4 ) ;
643
723
self -> sockets [i ].local_ep = 0 ;
644
- self -> sockets [i ].remote_id = 0 ;
645
- self -> sockets [i ].remote_id_mask = 0 ;
724
+ memset ( self -> sockets [i ].remote_id , 0 , 4 ) ;
725
+ memset ( self -> sockets [i ].remote_id_mask , 0 , 4 ) ;
646
726
self -> sockets [i ].remote_ep = 0 ;
647
727
648
728
self -> sockets [i ].datagram .vmt = & nbus_socket_vmt ;
@@ -679,6 +759,10 @@ nbus_ret_t nbus_socket_bind(struct nbus_socket *socket, const uint8_t *id, uint8
679
759
(void )id ;
680
760
(void )ep ;
681
761
762
+ memcpy (socket -> local_id , id , 4 );
763
+ memcpy (socket -> local_id_mask , (uint8_t [4 ]){0xff , 0xff , 0xff , 0xff }, 4 );
764
+ socket -> local_ep = ep ;
765
+
682
766
return NBUS_RET_OK ;
683
767
684
768
}
0 commit comments