diff --git a/adsbus/Makefile b/adsbus/Makefile index 635b9ce..d6d1c7f 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -3,7 +3,7 @@ CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack- LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now LIBS ?= -ljansson -OBJ_NETWORK = incoming.o outgoing.o receive.o send.o +OBJ_NETWORK = incoming.o outgoing.o receive.o send.o peer.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o OBJ_UTIL = buf.o hex.o opts.o rand.o uuid.o wakeup.o common.o diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 6c0193b..209d769 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -1,24 +1,22 @@ -#include -#include +#include #include +#include +#include #include #include -#include - -#include "incoming.h" -#include "outgoing.h" - -#include "receive.h" -#include "send.h" #include "beast.h" -#include "json.h" -#include "stats.h" - #include "common.h" #include "hex.h" +#include "incoming.h" +#include "json.h" #include "opts.h" +#include "outgoing.h" +#include "peer.h" #include "rand.h" +#include "receive.h" +#include "send.h" +#include "stats.h" #include "wakeup.h" static void print_usage(const char *name) { diff --git a/adsbus/common.c b/adsbus/common.c index 000ec21..c6809f6 100644 --- a/adsbus/common.c +++ b/adsbus/common.c @@ -14,76 +14,6 @@ #include "uuid.h" #include "wakeup.h" -static char server_id[UUID_LEN]; -static int peer_epoll_fd; -static int peer_cancel_fd; -static bool peer_canceled = false; - -static void peer_cancel(int signal) { - assert(!close(peer_cancel_fd)); -} - -static void peer_cancel_handler(struct peer *peer) { - fprintf(stderr, "X %s: Shutting down\n", server_id); - assert(!close(peer->fd)); - free(peer); - peer_canceled = true; -} - -void peer_init() { - uuid_gen(server_id); - - peer_epoll_fd = epoll_create1(0); - assert(peer_epoll_fd >= 0); - - int cancel_fds[2]; - assert(!pipe2(cancel_fds, O_NONBLOCK)); - - struct peer *cancel_peer = malloc(sizeof(*cancel_peer)); - assert(cancel_peer); - cancel_peer->fd = cancel_fds[0]; - cancel_peer->event_handler = peer_cancel_handler; - peer_epoll_add(cancel_peer, EPOLLRDHUP); - - peer_cancel_fd = cancel_fds[1]; - signal(SIGINT, peer_cancel); -} - -void peer_epoll_add(struct peer *peer, uint32_t events) { - struct epoll_event ev = { - .events = events, - .data = { - .ptr = peer, - }, - }; - assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev)); -} - -void peer_epoll_del(struct peer *peer) { - assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL)); -} - -void peer_loop() { - fprintf(stderr, "X %s: Starting event loop\n", server_id); - while (!peer_canceled) { -#define MAX_EVENTS 10 - struct epoll_event events[MAX_EVENTS]; - int nfds = epoll_wait(peer_epoll_fd, events, MAX_EVENTS, wakeup_get_delay()); - if (nfds == -1 && errno == EINTR) { - continue; - } - assert(nfds >= 0); - - for (int n = 0; n < nfds; n++) { - struct peer *peer = events[n].data.ptr; - peer->event_handler(peer); - } - - wakeup_dispatch(); - } - assert(!close(peer_epoll_fd)); -} - char *packet_type_names[] = { "Mode-S short", diff --git a/adsbus/common.h b/adsbus/common.h index 3976bf2..d5c586a 100644 --- a/adsbus/common.h +++ b/adsbus/common.h @@ -1,22 +1,6 @@ #pragma once #include -#include - - -//////// peer - -// All specific peer structs must be castable to this. -struct peer; -typedef void (*peer_event_handler)(struct peer *); -struct peer { - int fd; - peer_event_handler event_handler; -}; -void peer_init(); -void peer_epoll_add(struct peer *, uint32_t); -void peer_epoll_del(struct peer *); -void peer_loop(); //////// packet diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 08ca3ca..a48ec80 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -11,6 +11,7 @@ #include #include "common.h" +#include "peer.h" #include "wakeup.h" #include "uuid.h" diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 1082026..056be89 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -9,6 +9,7 @@ #include #include "common.h" +#include "peer.h" #include "wakeup.h" #include "uuid.h" diff --git a/adsbus/peer.c b/adsbus/peer.c new file mode 100644 index 0000000..47c193d --- /dev/null +++ b/adsbus/peer.c @@ -0,0 +1,86 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "uuid.h" +#include "wakeup.h" + +#include "peer.h" + +static char server_id[UUID_LEN]; +static int peer_epoll_fd; +static int peer_cancel_fd; +static bool peer_canceled = false; + +static void peer_cancel(int signal) { + assert(!close(peer_cancel_fd)); +} + +static void peer_cancel_handler(struct peer *peer) { + fprintf(stderr, "X %s: Shutting down\n", server_id); + assert(!close(peer->fd)); + free(peer); + peer_canceled = true; +} + +void peer_init() { + uuid_gen(server_id); + + peer_epoll_fd = epoll_create1(0); + assert(peer_epoll_fd >= 0); + + int cancel_fds[2]; + assert(!pipe2(cancel_fds, O_NONBLOCK)); + + struct peer *cancel_peer = malloc(sizeof(*cancel_peer)); + assert(cancel_peer); + cancel_peer->fd = cancel_fds[0]; + cancel_peer->event_handler = peer_cancel_handler; + peer_epoll_add(cancel_peer, EPOLLRDHUP); + + peer_cancel_fd = cancel_fds[1]; + signal(SIGINT, peer_cancel); +} + +void peer_epoll_add(struct peer *peer, uint32_t events) { + struct epoll_event ev = { + .events = events, + .data = { + .ptr = peer, + }, + }; + assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev)); +} + +void peer_epoll_del(struct peer *peer) { + assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL)); +} + +void peer_loop() { + fprintf(stderr, "X %s: Starting event loop\n", server_id); + while (!peer_canceled) { +#define MAX_EVENTS 10 + struct epoll_event events[MAX_EVENTS]; + int nfds = epoll_wait(peer_epoll_fd, events, MAX_EVENTS, wakeup_get_delay()); + if (nfds == -1 && errno == EINTR) { + continue; + } + assert(nfds >= 0); + + for (int n = 0; n < nfds; n++) { + struct peer *peer = events[n].data.ptr; + peer->event_handler(peer); + } + + wakeup_dispatch(); + } + assert(!close(peer_epoll_fd)); +} diff --git a/adsbus/peer.h b/adsbus/peer.h new file mode 100644 index 0000000..11025c5 --- /dev/null +++ b/adsbus/peer.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +// All specific peer structs must be castable to this. +struct peer; +typedef void (*peer_event_handler)(struct peer *); +struct peer { + int fd; + peer_event_handler event_handler; +}; +void peer_init(); +void peer_epoll_add(struct peer *, uint32_t); +void peer_epoll_del(struct peer *); +void peer_loop(); diff --git a/adsbus/receive.c b/adsbus/receive.c index b361902..543a24c 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -7,6 +7,7 @@ #include "airspy_adsb.h" #include "beast.h" #include "buf.h" +#include "peer.h" #include "raw.h" #include "send.h" #include "uuid.h" diff --git a/adsbus/send.c b/adsbus/send.c index 41a6e70..4f5efac 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -12,6 +12,7 @@ #include "beast.h" #include "buf.h" #include "json.h" +#include "peer.h" #include "raw.h" #include "stats.h" #include "uuid.h" diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index d35f861..f3e5d60 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -11,6 +11,7 @@ #include #include "common.h" +#include "peer.h" #include "rand.h" #include "wakeup.h" diff --git a/adsbus/wakeup.h b/adsbus/wakeup.h index 66f4852..deed15e 100644 --- a/adsbus/wakeup.h +++ b/adsbus/wakeup.h @@ -1,5 +1,7 @@ #pragma once +#include + struct peer; void wakeup_init();