From 8ec7ad8f54b96866eb37da38efbc3e399dd2784f Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Wed, 7 May 2025 19:11:17 +0200 Subject: [PATCH] avoid alien packets --- morep.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/morep.c b/morep.c index f99bc5c..a75198f 100644 --- a/morep.c +++ b/morep.c @@ -64,6 +64,7 @@ typedef struct { uint8_t seqnum; uint8_t *tx_buffer; uint8_t *rx_buffer; + uint16_t n_etype; int used; } MOREP_descriptor_t ; @@ -335,8 +336,8 @@ int MOREP_Connect (char *url) MOREP_list[index]->sock = sock; memcpy (MOREP_list[index]->tx_buffer, (void *) addr.mac, 6); memcpy (MOREP_list[index]->tx_buffer + 6, sock_addr.sll_addr, 6); - uint16_t ether_type = htons (addr.ether_type); - memcpy (MOREP_list[index]->tx_buffer + 12, (void *) ðer_type, 2); + MOREP_list[index]->n_etype = htons (addr.ether_type); + memcpy (MOREP_list[index]->tx_buffer + 12, (void *) &(MOREP_list[index]->n_etype), 2); } return index; @@ -422,6 +423,16 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len) rxlen = recvfrom (morep->sock, morep->rx_buffer, ETHER_PREAMBLE + MAX_ETHER_SIZE, 0, NULL, 0); VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->rx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':')); + /* avoid alien packets */ + uint16_t rx_n_etype; + memcpy ((void *)&rx_n_etype, morep->rx_buffer + 12, 2); + if ((morep->n_etype != rx_n_etype) || + (memcmp (morep->tx_buffer, morep->rx_buffer + 6, 6) != 0) || + (memcmp (morep->tx_buffer + 6, morep->rx_buffer, 6) != 0)) { + VERBOSE (morep, DEBUG, PRINTF ("alien packet\n")); + continue; + } + /* sequence number */ int pkgseqnum = morep->rx_buffer[ETHER_PREAMBLE + 1]; if ((rxseqnum != -1) && (rxseqnum != pkgseqnum)) { -- 2.30.2