Add mg_crc16(), make PPP use it

This commit is contained in:
Sergio R. Caprile committed 2026-07-29 17:57:30 -03:00
1 parent 03be16803c
commit 7821fb9ab4
7 files changed
+65 -38

No files matched your search

+3 -19
View File
@@ -169,30 +169,14 @@ uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
s_id, src, dst, frame));
}
// Calculate FCS/CRC for PPP frames. Could be implemented faster using lookup
// tables.
static uint16_t fcs_do(uint8_t *frame, size_t len) {
uint32_t fcs = 0xffff;
unsigned int j;
for (j = 0; j < len; j++) {
unsigned int i;
uint8_t x = frame[j];
for (i = 0; i < 8; i++) {
fcs = ((fcs ^ x) & 1) ? (fcs >> 1) ^ 0x8408 : fcs >> 1;
x >>= 1;
}
}
return (uint16_t) (fcs & 0xffff);
}
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
uint16_t crc;
uint8_t *frame;
len += sizeof(struct ppp) + sizeof(struct hdlc_);
frame = cur - len;
crc = fcs_do(frame, len);
*cur++ = (uint8_t) ~crc; // add CRC, note the byte order
*cur++ = (uint8_t) (~crc >> 8);
crc = mg_crc16(0, (const char *) frame, len);
*cur++ = (uint8_t) crc; // add CRC, note the byte order
*cur++ = (uint8_t) (crc >> 8);
// there is no len field in PPP
(void) ifp;
return len + 2;