Start working on the frontend

This commit is contained in:
Juhani Krekelä 2019-07-10 22:10:14 +03:00
parent 05b4f87597
commit 664a6eb838
4 changed files with 61 additions and 13 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@
*.o
ethertype-dump
arp-request
ethermess-backend
ethermess

View File

@ -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

View File

@ -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, "<EFBFBD>", 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);

2
ethermess.py Normal file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3
libexec_dir = __LIBEXECDIR__