changeset 1514:4f94e0f90c83

Added support for MegaWiFi command IP_CURRENT
author Michael Pavone <pavone@retrodev.com>
date Tue, 16 Jan 2018 09:31:00 -0800
parents 8f3b6a64b658
children 3fc129eb0653
files Makefile megawifi.c net.c net.h
diffstat 4 files changed, 110 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Jan 15 09:04:43 2018 -0800
+++ b/Makefile	Tue Jan 16 09:31:00 2018 -0800
@@ -127,7 +127,7 @@
 AUDIOOBJS=ym2612.o psg.o wave.o
 CONFIGOBJS=config.o tern.o util.o
 
-MAINOBJS=blastem.o system.o genesis.o debug.o gdb_remote.o vdp.o render_sdl.o ppm.o io.o romdb.o hash.o menu.o xband.o realtec.o i2c.o nor.o sega_mapper.o multi_game.o megawifi.o serialize.o $(TERMINAL) $(CONFIGOBJS) gst.o $(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS)
+MAINOBJS=blastem.o system.o genesis.o debug.o gdb_remote.o vdp.o render_sdl.o ppm.o io.o romdb.o hash.o menu.o xband.o realtec.o i2c.o nor.o sega_mapper.o multi_game.o megawifi.o net.o serialize.o $(TERMINAL) $(CONFIGOBJS) gst.o $(M68KOBJS) $(TRANSOBJS) $(AUDIOOBJS)
 
 ifeq ($(CPU),x86_64)
 CFLAGS+=-DX86_64 -m64
--- a/megawifi.c	Mon Jan 15 09:04:43 2018 -0800
+++ b/megawifi.c	Tue Jan 16 09:31:00 2018 -0800
@@ -2,6 +2,7 @@
 #include <stdint.h>
 #include <string.h>
 #include "genesis.h"
+#include "net.h"
 
 enum {
 	TX_IDLE,
@@ -88,6 +89,7 @@
 			size = mw->transmit_bytes - 4;
 		}
 		mw->receive_read = mw->receive_bytes = 0;
+		printf("Received MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size);
 		switch (command)
 		{
 		case CMD_VERSION:
@@ -142,6 +144,52 @@
 			mw_putc(mw, mw->channel_flags);
 			mw_putc(mw, 0x7E);
 			break;
+		case CMD_IP_CURRENT: {
+			//LSD header
+			mw_putc(mw, 0x7E);
+			mw_putc(mw, 0);
+			mw_putc(mw, 28);
+			//cmd
+			mw_putc(mw, 0);
+			mw_putc(mw, CMD_OK);
+			//length
+			mw_putc(mw, 0);
+			mw_putc(mw, 24);
+			
+			iface_info i;
+			get_host_address(&i);
+			//config number and reserved bytes
+			mw_putc(mw, 0);
+			mw_putc(mw, 0);
+			mw_putc(mw, 0);
+			mw_putc(mw, 0);
+			//ip
+			mw_putc(mw, i.ip[0]);
+			mw_putc(mw, i.ip[1]);
+			mw_putc(mw, i.ip[2]);
+			mw_putc(mw, i.ip[3]);
+			//net mask
+			mw_putc(mw, i.net_mask[0]);
+			mw_putc(mw, i.net_mask[1]);
+			mw_putc(mw, i.net_mask[2]);
+			mw_putc(mw, i.net_mask[3]);
+			//gateway guess
+			mw_putc(mw, i.ip[0] & i.net_mask[0]);
+			mw_putc(mw, i.ip[1] & i.net_mask[1]);
+			mw_putc(mw, i.ip[2] & i.net_mask[2]);
+			mw_putc(mw, (i.ip[3] & i.net_mask[3]) + 1);
+			//dns
+			mw_putc(mw, 127);
+			mw_putc(mw, 0);
+			mw_putc(mw, 0);
+			mw_putc(mw, 1);
+			mw_putc(mw, 127);
+			mw_putc(mw, 0);
+			mw_putc(mw, 0);
+			mw_putc(mw, 1);
+			mw_putc(mw, 0x7E);
+			break;
+		}
 		default:
 			printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size);
 			break;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net.c	Tue Jan 16 09:31:00 2018 -0800
@@ -0,0 +1,49 @@
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <netinet/in.h>
+#include "net.h"
+
+static uint8_t is_loopback(struct sockaddr_in *addr)
+{
+	return (addr->sin_addr.s_addr & 0xFF) == 127;
+}
+
+static void format_address(uint8_t *dst, struct sockaddr_in *addr)
+{
+	long ip = addr->sin_addr.s_addr;
+	dst[0] = ip;
+	dst[1] = ip >> 8;
+	dst[2] = ip >> 16;
+	dst[3] = ip >> 24;
+}
+
+uint8_t get_host_address(iface_info *out)
+{
+	struct ifaddrs *entries, *current, *localhost;
+	if (getifaddrs(&entries)) {
+		return 0;
+	}
+	
+	for (current = entries; current; current = current->ifa_next)
+	{
+		if (current->ifa_addr && current->ifa_addr->sa_family == AF_INET) {
+			struct sockaddr_in *addr = (struct sockaddr_in *)current->ifa_addr;
+			if (is_loopback(addr)) {
+				localhost = current;
+			} else {
+				break;
+			}
+		}
+	}
+	if (!current && localhost) {
+		current = localhost;
+	}
+	uint8_t ret = 0;
+	if (current) {
+		ret = 1;
+		format_address(out->ip, (struct sockaddr_in *)current->ifa_addr);
+		format_address(out->net_mask, (struct sockaddr_in *)current->ifa_netmask);
+	}
+	freeifaddrs(entries);
+	return ret;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net.h	Tue Jan 16 09:31:00 2018 -0800
@@ -0,0 +1,12 @@
+#ifndef NET_H_
+#define NET_H_
+#include <stdint.h>
+
+typedef struct {
+	uint8_t ip[4];
+	uint8_t net_mask[4];
+} iface_info;
+
+uint8_t get_host_address(iface_info *out);
+
+#endif //NET_H_