Add big-endian tests for our BSD layer

This commit is contained in:
Sergio R. Caprile committed 2026-07-10 18:28:40 -03:00
1 parent 72a0787161
commit 0aa25dbd18
6 files changed
+277 -106

No files matched your search

+26 -52
View File
@@ -4,39 +4,18 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <signal.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#define FRAME_SIZE 1540
#define FRAME_QUEUE_DEPTH 8
#define SHM_NAME "/mongoose-shm"
struct shm_frame {
size_t len;
uint8_t buf[FRAME_SIZE];
};
struct shm_queue {
sem_t full;
sem_t empty;
size_t head;
size_t tail;
struct shm_frame frames[FRAME_QUEUE_DEPTH];
};
struct shm_link {
struct shm_queue to_tap;
struct shm_queue from_tap;
};
#include "shm_queue.h"
static volatile sig_atomic_t s_signo;
void signal_handler(int signo) {
@@ -63,17 +42,22 @@ int main(void) {
shm_unlink(SHM_NAME);
shmfd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600);
if (shmfd < 0) return EXIT_FAILURE;
if (ftruncate(shmfd, sizeof(*shm)) != 0) return EXIT_FAILURE;
if (shmfd < 0) {
printf("shm_open failed: %d\n", errno);
return EXIT_FAILURE;
}
if (ftruncate(shmfd, sizeof(*shm)) != 0) {
printf("ftruncate failed: %d\n", errno);
return EXIT_FAILURE;
}
shm = (struct shm_link *) mmap(NULL, sizeof(*shm), PROT_READ | PROT_WRITE,
MAP_SHARED, shmfd, 0);
if (shm == MAP_FAILED) return EXIT_FAILURE;
if (shm == MAP_FAILED) {
printf("mmap failed: %d\n", errno);
return EXIT_FAILURE;
}
memset(shm, 0, sizeof(*shm));
sem_init(&shm->to_tap.full, 1, 0);
sem_init(&shm->to_tap.empty, 1, FRAME_QUEUE_DEPTH);
sem_init(&shm->from_tap.full, 1, 0);
sem_init(&shm->from_tap.empty, 1, FRAME_QUEUE_DEPTH);
printf("Opened shared memory: %s\n", SHM_NAME);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
@@ -94,41 +78,31 @@ int main(void) {
break;
default: // the number of fd/sockets with data available (see pfd.revents for each one);
if (pfd[0].revents & (POLLIN | POLLPRI)) {
struct shm_queue *q = &shm->from_tap;
ssize_t nread;
if (sem_trywait(&q->empty) == 0) {
struct shm_frame *f = &q->frames[q->tail];
nread = read(fd, f->buf, sizeof(f->buf));
uint8_t buf[FRAME_SIZE];
ssize_t nread = read(fd, buf, sizeof(buf));
// printf("Got at TAP: %d bytes\n", (int) nread);
if (nread > 0) {
f->len = (size_t) nread;
q->tail = (q->tail + 1) % FRAME_QUEUE_DEPTH;
sem_post(&q->full);
// printf("Sent to SHM\n");
} else {
sem_post(&q->empty);
if (nread > 0) {
if (!shm_queue_push(&shm->from_tap, buf, (size_t) nread)) {
printf("FRAME LOST: TAP -> SHM\n");
}
// printf("Sent to SHM\n");
}
}
break;
}
if (sem_trywait(&shm->to_tap.full) == 0) {
struct shm_queue *q = &shm->to_tap;
struct shm_frame *f = &q->frames[q->head];
{
uint8_t buf[FRAME_SIZE];
size_t n = shm_queue_pop(&shm->to_tap, buf, sizeof(buf));
if (n > 0) {
// printf("Got at SHM: %d bytes\n", (int) f->len);
write(fd, f->buf, f->len);
q->head = (q->head + 1) % FRAME_QUEUE_DEPTH;
sem_post(&q->empty);
write(fd, buf, n);
// printf("Sent to TAP\n");
}
}
} while (result >= 0 && s_signo == 0);
if (result < 0)
printf("looks like we got an error here... should I quit ? Waiting for CTRL-C\n");
}
sem_destroy(&shm->from_tap.empty);
sem_destroy(&shm->from_tap.full);
sem_destroy(&shm->to_tap.empty);
sem_destroy(&shm->to_tap.full);
munmap(shm, sizeof(*shm));
close(shmfd);
shm_unlink(SHM_NAME);