diff --git a/.gitignore b/.gitignore index 579b7fd..8d38876 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.o ethertype-dump arp-request +ethermess-backend ethermess diff --git a/Makefile b/Makefile index b6c8103..a8b5e8b 100644 --- a/Makefile +++ b/Makefile @@ -2,19 +2,23 @@ DESTDIR ?= PREFIX ?= /usr/local EXEC_PREFIX ?= $(PREFIX) BINDIR ?= $(DESTDIR)$(EXEC_PREFIX)/bin +LIBEXECDIR ?= $(DESTDIR)$(EXEC_PREFIX)/libexec CFLAGS += -Os -g -Wall -Wextra -pedantic CPPFLAGS += LDFLAGS += +SED ?= sed + BINS := ethertype-dump arp-request ethermess +LIBEXECS := ethermess-backend .SUFFIXES: .SUFFIXES: .c .o .PHONY: all install uninstall clean distclean -all: $(BINS) +all: $(BINS) $(LIBEXECS) ethertype-dump: ethertype-dump.o $(CC) -o $@ $< $(LDFLAGS) @@ -22,19 +26,29 @@ ethertype-dump: ethertype-dump.o arp-request: arp-request.o $(CC) -o $@ $< $(LDFLAGS) -ethermess: ethermess.o +ethermess: ethermess.py + # I'm not sure you can make this work 100% with traditional make + #$(SED) "2s/__LIBEXECDIR__/'`printf "%s" "$(LIBEXECDIR)" | sed "s,\\\\\\\\,\\\\\\\\\\\\\\\\,g;s,/,\\\\\\\\/,g;s,"'"'",\\\\\\\\"'"'",g"`'/" < $< > $@ + $(SED) "2s/__LIBEXECDIR__/'.'/" < $< > $@ #debg + chmod +x $@ + +ethermess-backend: ethermess-backend.o $(CC) -o $@ $< $(LDFLAGS) .c.o: $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< -install: $(BINS) +install: $(BINS) $(LIBEXECS) + mkdir -p $(BINDIR) + mkdir -p $(LIBEXECDIR) install $(BINS) $(BINDIR) + install $(LIBEXECS) $(LIBEXECDIR) uninstall: cd $(BINDIR) && rm $(BINS) + cs $(LIBEXECDIR) && rm $(LIBEXECS) clean: - rm -f ethertype-dump *.o + rm -f $(BINS) $(LIBEXECS) *.o distclean: clean diff --git a/ethermess.c b/ethermess-backend.c similarity index 96% rename from ethermess.c rename to ethermess-backend.c index 0602727..737a72c 100644 --- a/ethermess.c +++ b/ethermess-backend.c @@ -405,6 +405,43 @@ void send_ack(const unsigned char destination[6], uint16_t msgid) { send_frame(frame, sizeof(frame)); } +void readallx(int fd, unsigned char *buf, size_t length) { + size_t completed = 0; + while (completed < length) { + ssize_t res = read(fd, &buf[completed], length - completed); + if (res == -1) { + err(1, "read"); + } + completed += res; + } +} + +void writeallx(int fd, unsigned char *buf, size_t length) { + size_t completed = 0; + while (completed < length) { + ssize_t res = write(fd, &buf[completed], length - completed); + if (res == -1) { + err(1, "write"); + } + completed += res; + } +} + +void read_status(void) { + unsigned char status; + readallx(0, &status, 1); + if (status != EMS_AVAILABLE && status != EMS_UNAVAILABLE && status != EMS_OFFLINE) { + errx(1, "Frontend sent a status %u that we don't understand", status); + } + + unsigned char length; + readallx(0, &length, 1); + + readallx(0, own_nick, length); + + own_nick_length = length; +} + void read_command(void) { int cmd = getchar(); if (cmd == EOF) { @@ -1068,18 +1105,12 @@ int main(int argc, char **argv) { err(1, "fflush"); } - // Set our nick - if (memcmp(own_mac, veth0a_mac, 6) == 0) { - memcpy(own_nick, "𐀀ab", 6); - own_nick_length = 6; - } else { - memcpy(own_nick, "ࠀ�", 6); - own_nick_length = 6; - } - // Initialize the message id cache memset(msgid_cache, 0, sizeof(msgid_cache)); + // Set our status and nick + read_status(); + // Broadcast our status to the network to let them know we're here send_status(broadcast_mac); diff --git a/ethermess.py b/ethermess.py new file mode 100644 index 0000000..0eb255f --- /dev/null +++ b/ethermess.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +libexec_dir = __LIBEXECDIR__