diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-29 01:17:06 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-29 01:17:06 -0400 |
commit | e92588d33b8025799a06dd248d512d774c99e887 (patch) | |
tree | 058b3cdea96df94dbdb0127dc809240b8633437c | |
parent | c237e0b558fa6ae2a1417f4b4df14212b8721ba8 (diff) |
- Add timeouts
- Clean up padding, add length define
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | examples/hello.c | 1 | ||||
-rw-r--r-- | src/dgram/dgram_link.c | 29 | ||||
-rw-r--r-- | src/include/dgram.h | 6 | ||||
-rw-r--r-- | src/include/link.h | 9 |
4 files changed, 42 insertions, 3 deletions
diff --git a/examples/hello.c b/examples/hello.c index fd44bdd..ac443ca 100644 --- a/examples/hello.c +++ b/examples/hello.c @@ -55,6 +55,7 @@ static int data_send(void) { struct onet_link link; + char pad[SQUEAK_NPAD]; int error; /* Open a link */ diff --git a/src/dgram/dgram_link.c b/src/dgram/dgram_link.c index c1f4845..d26b85e 100644 --- a/src/dgram/dgram_link.c +++ b/src/dgram/dgram_link.c @@ -35,6 +35,7 @@ #include <net/if.h> #include <stdlib.h> #include <string.h> +#include <poll.h> #include "if_ether.h" #include "dgram.h" #include "crc.h" @@ -159,12 +160,13 @@ tx_len_t dgram_squeak(struct onet_link *link, mac_addr_t dst) { struct dgram_params params; - uint8_t pad[8]; + uint8_t pad[SQUEAK_NPAD]; if (link == NULL) { return -EINVAL; } + memset(pad, 0, sizeof(pad)); params.dst = dst; params.buf = pad; params.len = sizeof(pad); @@ -175,11 +177,14 @@ dgram_squeak(struct onet_link *link, mac_addr_t dst) rx_len_t dgram_recv(struct onet_link *link, void *buf, uint16_t len) { + const size_t MAX_MISS = 8; socklen_t addr_len; struct sockaddr_ll saddr; struct onet_dgram *o1p_hdr; struct ether_hdr *hdr; - size_t dgram_len, recv_len; + struct pollfd pfd; + size_t dgram_len, miss_count = 0; + ssize_t recv_len; uint32_t crc; uint16_t proto; mac_addr_t dest_mac, src_mac; @@ -204,11 +209,25 @@ dgram_recv(struct onet_link *link, void *buf, uint16_t len) saddr.sll_ifindex = link->iface_idx; saddr.sll_halen = HW_ADDR_LEN; + /* Setup poll descriptor */ + pfd.fd = link->sockfd; + pfd.events = POLLIN; + /* * Wait until we get a packet for us with the right * protocol ID. */ for (;;) { + if (miss_count > MAX_MISS) { + free(p); + return -ETIMEDOUT; + } + + if (poll(&pfd, 1, RECV_POLL_MS) < 0) { + free(p); + return -ETIMEDOUT; + } + recv_len = recvfrom( link->sockfd, p, dgram_len, 0, (struct sockaddr *)&saddr, @@ -221,19 +240,23 @@ dgram_recv(struct onet_link *link, void *buf, uint16_t len) src_mac = mac_swap(hdr->source); if (proto != PROTO_ID) { + ++miss_count; continue; } o1p_hdr = DGRAM_HDR(p); crc = crc32(o1p_hdr, sizeof(*o1p_hdr) - sizeof(crc)); if (crc != o1p_hdr->crc32) { + ++miss_count; continue; } + link->last_recv = src_mac; + /* Is this a squeak? */ if (o1p_hdr->type == OTYPE_SQUEAK) { squeak_back(link, src_mac, dest_mac); - continue; + break; } /* If this is for everyone, take it */ diff --git a/src/include/dgram.h b/src/include/dgram.h index 779c7ef..81fd1dd 100644 --- a/src/include/dgram.h +++ b/src/include/dgram.h @@ -34,6 +34,12 @@ #include "if_ether.h" #include "link.h" +/* Number of padding bytes in a squeak */ +#define SQUEAK_NPAD 8 + +/* How many ms we should poll on recv */ +#define RECV_POLL_MS 250 + typedef int16_t tx_len_t; typedef tx_len_t rx_len_t; diff --git a/src/include/link.h b/src/include/link.h index a791c2f..908c4a9 100644 --- a/src/include/link.h +++ b/src/include/link.h @@ -33,10 +33,19 @@ #include <stdint.h> #include "if_ether.h" +/* + * Describes an ONET link + * + * @sockfd: Socket file descriptor + * @iface_idx: Interface index in use + * @hwaddr: Our hardware address + * @last_recv: Hardware address of last received node + */ struct onet_link { int sockfd; uint32_t iface_idx; mac_addr_t hwaddr; + mac_addr_t last_recv; }; /* |