Skip to content

Commit

Permalink
socket_zep: only report size of single datagram
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 10, 2023
1 parent 8a80e70 commit f051baa
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions cpu/native/socket_zep/socket_zep.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static void _socket_isr(int fd, void *arg)
} else {
/* discard frame */
uint8_t tmp;
real_read(fd, &tmp, sizeof(tmp));
real_recv(fd, &tmp, sizeof(tmp), 0);
_continue_reading(zepdev);
}
}
Expand Down Expand Up @@ -445,23 +445,25 @@ static int _confirm_transmit(ieee802154_dev_t *dev, ieee802154_tx_info_t *info)

int _len(ieee802154_dev_t *dev)
{
size_t size;
socket_zep_t *zepdev = dev->priv;

int res = real_ioctl(zepdev->sock_fd, FIONREAD, &size);
zep_v2_data_hdr_t hdr;

int res = real_recv(zepdev->sock_fd, &hdr, sizeof(hdr), MSG_TRUNC | MSG_PEEK);
if (res < 0) {
DEBUG("socket_zep::len: error reading FIONREAD: %s", strerror(errno));
return 0;
}

DEBUG("socket_zep::len %zu bytes on %d\n", size, zepdev->sock_fd);

if (size < sizeof(zep_v2_data_hdr_t)) {
if (res < (int)sizeof(zep_v2_data_hdr_t)) {
DEBUG("socket_zep::len discard short frame (%zu bytes)\n", res);
return 0;
}

DEBUG("socket_zep::len %u bytes on %d\n", hdr.length, zepdev->sock_fd);

/* report size without ZEP header and checksum */
return size - (sizeof(zep_v2_data_hdr_t) + 2);
return hdr.length - 2;
}

static void _send_ack(socket_zep_t *zepdev, const void *frame)
Expand Down Expand Up @@ -495,18 +497,21 @@ static int _read(ieee802154_dev_t *dev, void *buf, size_t max_size,
{
int res;
socket_zep_t *zepdev = dev->priv;
size_t frame_len = max_size + sizeof(zep_v2_data_hdr_t) + 2;

DEBUG("socket_zep::read: reading up to %u bytes into %p\n", max_size, buf);

if (max_size + sizeof(zep_v2_data_hdr_t) > sizeof(zepdev->rcv_buf)) {
if (frame_len > sizeof(zepdev->rcv_buf)) {
DEBUG("socket_zep::read: frame size (%zu) exceeds RX buffer (%zu bytes)\n",
frame_len, sizeof(zepdev->rcv_buf));
return 0;
}

res = real_read(zepdev->sock_fd, zepdev->rcv_buf, max_size + sizeof(zep_v2_data_hdr_t));
res = real_recv(zepdev->sock_fd, zepdev->rcv_buf, frame_len, MSG_TRUNC);

DEBUG("socket_zep::read: got %d bytes\n", res);
DEBUG("socket_zep::read: got %d/%zu bytes\n", res, frame_len);

if (res <= (int)sizeof(zep_v2_data_hdr_t)) {
if (res <= (int)sizeof(zep_v2_data_hdr_t) || res > (int)frame_len) {
res = 0;
goto out;
}
Expand Down

0 comments on commit f051baa

Please # to comment.