diff --git a/ethermess-backend.c b/ethermess-backend.c index baaf062..6bca040 100644 --- a/ethermess-backend.c +++ b/ethermess-backend.c @@ -40,9 +40,6 @@ #define EMS_UNAVAILABLE 1 #define EMS_OFFLINE 2 -unsigned const char veth0a_mac[6] = {0xb2, 0xc8, 0x5b, 0x78, 0xb4, 0xef}; //debg -unsigned const char veth0b_mac[6] = {0xf6, 0x18, 0xfd, 0x2a, 0x80, 0xf3}; //debg - bool running = true; int packet_socket; @@ -451,28 +448,23 @@ void read_status(void) { } void read_command(void) { - int cmd = getchar(); - if (cmd == EOF) { - err(1, "getchar"); - } - - unsigned const char *other_mac; - if (memcmp(own_mac, veth0a_mac, 6) == 0) { - other_mac = veth0b_mac; - } else { - other_mac = veth0a_mac; - } + unsigned char cmd; + readallx(0, &cmd, 1); if (cmd == 'q') { running = false; } else if (cmd == 's') { - send_status_request(other_mac); + unsigned char mac[6]; + readallx(0, mac, sizeof(mac)); + send_status_request(mac); } else if (cmd == 'i') { - send_msgid_request(other_mac); + unsigned char mac[6]; + readallx(0, mac, sizeof(mac)); + send_msgid_request(mac); } else if (cmd == 'm') { + readallx(0, own_message_destination_mac, sizeof(own_message_destination_mac)); memcpy(own_message, "Hello, world!", 13); own_message_length = 13; - memcpy(own_message_destination_mac, other_mac, 6); own_message_send_state = QUEUED; } else if (cmd == '\n') { // Ignore diff --git a/ethermess.py b/ethermess.py index 0090101..588c43b 100644 --- a/ethermess.py +++ b/ethermess.py @@ -5,22 +5,51 @@ import subprocess import sys import time +def writeall(f, b): + written = 0 + while written < len(b): + written += f.write(b[written:]) + +def parse_mac(text): + parts = text.split(':') + if len(parts) != 6: + raise ValueError('Invalid MAC format: %s' % text) + + try: + parsed = bytes(int(field, 16) for field in parts) + except ValueError: + raise ValueError('Invalid MAC format %s' % text) + + return parsed + nick = input('nick> ').encode('utf-8') -proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', *sys.argv[1:]], stdin = subprocess.PIPE, stdout = sys.stdout) +proc = subprocess.Popen(['sudo', libexec_dir + '/ethermess-backend', *sys.argv[1:]], stdin = subprocess.PIPE, stdout = sys.stdout, stderr = sys.stderr, bufsize = 0) -proc.stdin.write(bytes([0, len(nick)]) + nick) -proc.stdin.flush() +writeall(proc.stdin, (bytes([0, len(nick)]) + nick)) print('s - request status, i - request msgid, m - send message, ^D - quit') try: while True: - proc.stdin.write(input('').encode('utf-8')) - proc.stdin.flush() + command = input('') + + if command == 's': + mac = parse_mac(input('mac> ')) + writeall(proc.stdin, b's' + mac) + + elif command == 'i': + mac = parse_mac(input('mac> ')) + writeall(proc.stdin, b'i' + mac) + + elif command == 'm': + mac = parse_mac(input('mac> ')) + writeall(proc.stdin, b'm' + mac) + + else: + print('s - request status, i - request msgid, m - send message, ^D - quit') except EOFError: - proc.stdin.write(b'q') - proc.stdin.flush() + writeall(proc.stdin, b'q') sys.exit(proc.wait())