summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--megawifi.c48
-rw-r--r--net.c49
-rw-r--r--net.h12
4 files changed, 110 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index dc2cde1..ad2ce4f 100644
--- a/Makefile
+++ b/Makefile
@@ -127,7 +127,7 @@ Z80OBJS=z80inst.o z80_to_x86.o
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
diff --git a/megawifi.c b/megawifi.c
index 17270c2..898bc8a 100644
--- a/megawifi.c
+++ b/megawifi.c
@@ -2,6 +2,7 @@
#include <stdint.h>
#include <string.h>
#include "genesis.h"
+#include "net.h"
enum {
TX_IDLE,
@@ -88,6 +89,7 @@ static void process_packet(megawifi *mw)
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 @@ static void process_packet(megawifi *mw)
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;
diff --git a/net.c b/net.c
new file mode 100644
index 0000000..eefc9cd
--- /dev/null
+++ b/net.c
@@ -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
diff --git a/net.h b/net.h
new file mode 100644
index 0000000..bcd5ca6
--- /dev/null
+++ b/net.h
@@ -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_