Skip to content

Commit

Permalink
Merge branch 'master' into virtio-net
Browse files Browse the repository at this point in the history
  • Loading branch information
basicer authored Aug 29, 2024
2 parents 8d34247 + 67bfed3 commit 046fb69
Show file tree
Hide file tree
Showing 5 changed files with 687 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ CORE_FILES=const.js config.js io.js main.js lib.js buffer.js ide.js pci.js flopp
LIB_FILES=9p.js filesystem.js jor1k.js marshall.js utf8.js
BROWSER_FILES=screen.js keyboard.js mouse.js speaker.js serial.js \
network.js starter.js worker_bus.js dummy_screen.js \
fake_network.js fetch_network.js print_stats.js filestorage.js
fake_network.js wisp_network.js fetch_network.js print_stats.js filestorage.js

RUST_FILES=$(shell find src/rust/ -name '*.rs') \
src/rust/gen/interpreter.rs src/rust/gen/interpreter0f.rs \
Expand Down Expand Up @@ -307,6 +307,7 @@ devices-test: all-debug
./tests/devices/virtio_console.js
./tests/devices/fetch_network.js
USE_VIRTIO=1 ./tests/devices/fetch_network.js
./tests/devices/wisp_network.js

rust-test: $(RUST_FILES)
env RUSTFLAGS="-D warnings" RUST_BACKTRACE=full RUST_TEST_THREADS=1 cargo test -- --nocapture
Expand Down
123 changes: 68 additions & 55 deletions src/browser/fake_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,33 +225,7 @@ function handle_fake_networking(data, adapter) {
}

if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
let packet_subnet = iptolong(packet.arp.tpa) & 0xFFFFFF00;
let router_subnet = iptolong(adapter.router_ip) & 0xFFFFFF00;

if(!adapter.masquerade) {
if(packet_subnet !== router_subnet) {
return;
}
}

if(packet_subnet === router_subnet) {
// Ignore the DHCP client area
if(packet.arp.tpa[3] > 99) return;
}

// Reply to ARP Whohas
let reply = {};
reply.eth = { ethertype: ETHERTYPE_ARP, src: adapter.router_mac, dest: packet.eth.src };
reply.arp = {
htype: 1,
ptype: ETHERTYPE_IPV4,
oper: 2,
sha: adapter.router_mac,
spa: packet.arp.tpa,
tha: packet.eth.src,
tpa: packet.arp.spa
};
adapter.receive(make_packet(reply));
arp_whohas(packet, adapter);
}

if(packet.dns) {
Expand All @@ -264,41 +238,15 @@ function handle_fake_networking(data, adapter) {

// ICMP Ping
if(packet.icmp && packet.icmp.type === 8) {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_ICMP,
src: adapter.router_ip,
dest: packet.ipv4.src,
};
reply.icmp = {
type: 0,
code: packet.icmp.code,
data: packet.icmp.data
};
adapter.receive(make_packet(reply));
return;
handle_fake_ping(packet, adapter);
}

if(packet.dhcp) {
if(handle_fake_dhcp(packet, adapter)) return;
}

if(packet.udp && packet.udp.dport === 8) {
// UDP Echo Server
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_UDP,
src: packet.ipv4.dest,
dest: packet.ipv4.src,
};
reply.udp = {
sport: packet.udp.dport,
dport: packet.udp.sport,
data: new TextEncoder().encode(packet.udp.data_s)
};
adapter.receive(make_packet(reply));
handle_udp_echo(packet, adapter);
}
}

Expand Down Expand Up @@ -528,6 +476,7 @@ function parse_udp(data, o) {
dport: view.getUint16(2),
len: view.getUint16(4),
checksum: view.getUint16(6),
data: data.subarray(8),
data_s: new TextDecoder().decode(data.subarray(8))
};

Expand Down Expand Up @@ -1133,3 +1082,67 @@ TCPConnection.prototype.pump = function() {
this.net.receive(make_packet(reply));
}
};


function arp_whohas(packet, adapter) {
let packet_subnet = iptolong(packet.arp.tpa) & 0xFFFFFF00;
let router_subnet = iptolong(adapter.router_ip) & 0xFFFFFF00;

if(!adapter.masquerade) {
if(packet_subnet !== router_subnet) {
return;
}
}

if(packet_subnet === router_subnet) {
// Ignore the DHCP client area
if(packet.arp.tpa[3] > 99) return;
}

// Reply to ARP Whohas
let reply = {};
reply.eth = { ethertype: ETHERTYPE_ARP, src: adapter.router_mac, dest: packet.eth.src };
reply.arp = {
htype: 1,
ptype: ETHERTYPE_IPV4,
oper: 2,
sha: adapter.router_mac,
spa: packet.arp.tpa,
tha: packet.eth.src,
tpa: packet.arp.spa
};
adapter.receive(make_packet(reply));
}

function handle_fake_ping(packet, adapter) {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_ICMP,
src: adapter.router_ip,
dest: packet.ipv4.src,
};
reply.icmp = {
type: 0,
code: packet.icmp.code,
data: packet.icmp.data
};
adapter.receive(make_packet(reply));
}

function handle_udp_echo(packet, adapter) {
// UDP Echo Server
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_UDP,
src: packet.ipv4.dest,
dest: packet.ipv4.src,
};
reply.udp = {
sport: packet.udp.dport,
dport: packet.udp.sport,
data: new TextEncoder().encode(packet.udp.data_s)
};
adapter.receive(make_packet(reply));
}
3 changes: 3 additions & 0 deletions src/browser/starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ V86.prototype.continue_init = async function(emulator, options)
{
this.network_adapter = new FetchNetworkAdapter(this.bus);
}
else if(options.network_relay_url.startsWith("wisp://") || options.network_relay_url.startsWith("wisps://")) {
this.network_adapter = new WispNetworkAdapter(options.network_relay_url, this.bus, options);
}
else
{
this.network_adapter = new NetworkAdapter(options.network_relay_url, this.bus);
Expand Down
Loading

0 comments on commit 046fb69

Please sign in to comment.