Skip to content

Commit

Permalink
simplified control flow in handle_fake_networking() and WispNetworkAd…
Browse files Browse the repository at this point in the history
…apter.send()

- in both functions, all the scattered "if" statements are mutually exclusive
- changed control flow to mutual exclusive cases
- removed boolean return values in handle_fake_networking(), they were never checked by the caller
  • Loading branch information
chschnell committed Nov 24, 2024
1 parent 5ec28a2 commit 38e01d8
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 139 deletions.
50 changes: 20 additions & 30 deletions src/browser/fake_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,41 +404,31 @@ function handle_fake_dhcp(packet, adapter) {
function handle_fake_networking(data, adapter) {
let packet = {};
parse_eth(data, packet);
if(packet.tcp) {
if(handle_fake_tcp(packet, adapter)) {
return true;
}
}

if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
arp_whohas(packet, adapter);
}

if(packet.dns) {
if(handle_fake_dns(packet, adapter)) {
return;
if(packet.ipv4) {
if(packet.tcp) {
handle_fake_tcp(packet, adapter);
}
}

if(packet.ntp) {
if(handle_fake_ntp(packet, adapter)) {
return;
else if(packet.udp) {
if(packet.dns) {
handle_fake_dns(packet, adapter);
}
else if(packet.dhcp) {
handle_fake_dhcp(packet, adapter);
}
else if(packet.ntp) {
handle_fake_ntp(packet, adapter);
}
else if(packet.udp.dport === 8) {
handle_udp_echo(packet, adapter);
}
}
}

// ICMP Ping
if(packet.icmp && packet.icmp.type === 8) {
handle_fake_ping(packet, adapter);
}

if(packet.dhcp) {
if(handle_fake_dhcp(packet, adapter)) {
return;
else if(packet.icmp && packet.icmp.type === 8) {
handle_fake_ping(packet, adapter);
}
}

if(packet.udp && packet.udp.dport === 8) {
handle_udp_echo(packet, adapter);
else if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
arp_whohas(packet, adapter);
}
}

Expand Down
216 changes: 107 additions & 109 deletions src/browser/wisp_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,133 +183,131 @@ WispNetworkAdapter.prototype.send = function(data)
let packet = {};
parse_eth(data, packet);

if(packet.tcp) {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_TCP,
src: packet.ipv4.dest,
dest: packet.ipv4.src
};

let tuple = [
packet.ipv4.src.join("."),
packet.tcp.sport,
packet.ipv4.dest.join("."),
packet.tcp.dport
].join(":");
if(packet.ipv4) {
if(packet.tcp) {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_TCP,
src: packet.ipv4.dest,
dest: packet.ipv4.src
};

if(packet.tcp.syn) {
if(this.tcp_conn[tuple]) {
dbg_log("SYN to already opened port", LOG_FETCH);
}
const tcp_conn = new TCPConnection();
let tuple = [
packet.ipv4.src.join("."),
packet.tcp.sport,
packet.ipv4.dest.join("."),
packet.tcp.dport
].join(":");

tcp_conn.state = TCP_STATE_SYN_RECEIVED;
tcp_conn.net = this;
tcp_conn.tuple = tuple;
tcp_conn.stream_id = this.last_stream++;
this.tcp_conn[tuple] = tcp_conn;
if(packet.tcp.syn) {
if(this.tcp_conn[tuple]) {
dbg_log("SYN to already opened port", LOG_FETCH);
}
const tcp_conn = new TCPConnection();

tcp_conn.state = TCP_STATE_SYN_RECEIVED;
tcp_conn.net = this;
tcp_conn.tuple = tuple;
tcp_conn.stream_id = this.last_stream++;
this.tcp_conn[tuple] = tcp_conn;

tcp_conn.on_data = (data) => {
if(data.length !== 0) {
this.send_wisp_frame({
type: "DATA",
stream_id: tcp_conn.stream_id,
data: data
});
}
};

tcp_conn.on_shutdown = () => {
this.send_wisp_frame({
type: "CLOSE",
stream_id: tcp_conn.stream_id,
reason: 0x02 // 0x02: Voluntary stream closure
});
};

tcp_conn.on_data = (data) => {
if(data.length !== 0) {
tcp_conn.on_close = () => {
this.send_wisp_frame({
type: "DATA",
type: "CLOSE",
stream_id: tcp_conn.stream_id,
data: data
reason: 0x02 // 0x02: Voluntary stream closure
});
}
};
};

tcp_conn.on_shutdown = () => {
this.send_wisp_frame({
type: "CLOSE",
type: "CONNECT",
stream_id: tcp_conn.stream_id,
reason: 0x02 // 0x02: Voluntary stream closure
hostname: packet.ipv4.dest.join("."),
port: packet.tcp.dport,
data_callback: (data) => {
tcp_conn.write(data);
},
close_callback: (data) => {
tcp_conn.close();
}
});
};

tcp_conn.on_close = () => {
this.send_wisp_frame({
type: "CLOSE",
stream_id: tcp_conn.stream_id,
reason: 0x02 // 0x02: Voluntary stream closure
});
};
tcp_conn.accept(packet);
return;
}

this.send_wisp_frame({
type: "CONNECT",
stream_id: tcp_conn.stream_id,
hostname: packet.ipv4.dest.join("."),
port: packet.tcp.dport,
data_callback: (data) => {
tcp_conn.write(data);
},
close_callback: (data) => {
tcp_conn.close();
}
});
if(!this.tcp_conn[tuple]) {
dbg_log(`I dont know about ${tuple}, so restting`, LOG_FETCH);
let bop = packet.tcp.ackn;
if(packet.tcp.fin || packet.tcp.syn) bop += 1;
reply.tcp = {
sport: packet.tcp.dport,
dport: packet.tcp.sport,
seq: bop,
ackn: packet.tcp.seq + (packet.tcp.syn ? 1: 0),
winsize: packet.tcp.winsize,
rst: true,
ack: packet.tcp.syn
};
this.receive(make_packet(this.eth_encoder_buf, reply));
return;
}

tcp_conn.accept(packet);
return;
this.tcp_conn[tuple].process(packet);
}

if(!this.tcp_conn[tuple]) {
dbg_log(`I dont know about ${tuple}, so restting`, LOG_FETCH);
let bop = packet.tcp.ackn;
if(packet.tcp.fin || packet.tcp.syn) bop += 1;
reply.tcp = {
sport: packet.tcp.dport,
dport: packet.tcp.sport,
seq: bop,
ackn: packet.tcp.seq + (packet.tcp.syn ? 1: 0),
winsize: packet.tcp.winsize,
rst: true,
ack: packet.tcp.syn
};
this.receive(make_packet(this.eth_encoder_buf, reply));
return;
else if(packet.udp) {
// TODO: remove when this wisp client supports udp
if(packet.dns) {
(async () => {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_UDP,
src: this.router_ip,
dest: packet.ipv4.src,
};
reply.udp = { sport: 53, dport: packet.udp.sport };
const result = await ((await fetch(`https://${this.doh_server}/dns-query`, {method: "POST", headers: [["content-type", "application/dns-message"]], body: packet.udp.data})).arrayBuffer());
reply.udp.data = new Uint8Array(result);
this.receive(make_packet(this.eth_encoder_buf, reply));
})();
}
else if(packet.dhcp) {
handle_fake_dhcp(packet, this);
}
else if(packet.ntp) {
handle_fake_ntp(packet, this);
}
else if(packet.udp.dport === 8) {
handle_udp_echo(packet, this);
}
}
else if(packet.icmp && packet.icmp.type === 8) {
handle_fake_ping(packet, this);
}

this.tcp_conn[tuple].process(packet);
}

if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
else if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {
arp_whohas(packet, this);
}

if(packet.dns) {
// TODO: remove when this wisp client supports udp
(async () => {
let reply = {};
reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.router_mac, dest: packet.eth.src };
reply.ipv4 = {
proto: IPV4_PROTO_UDP,
src: this.router_ip,
dest: packet.ipv4.src,
};
reply.udp = { sport: 53, dport: packet.udp.sport };
const result = await ((await fetch(`https://${this.doh_server}/dns-query`, {method: "POST", headers: [["content-type", "application/dns-message"]], body: packet.udp.data})).arrayBuffer());
reply.udp.data = new Uint8Array(result);
this.receive(make_packet(this.eth_encoder_buf, reply));
})();
}

if(packet.ntp) {
// TODO: remove when this wisp client supports udp
handle_fake_ntp(packet, this);
return;
}

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

if(packet.udp && packet.udp.dport === 8) {
// TODO: remove when this wisp client supports udp
handle_udp_echo(packet, this);
}
};

/**
Expand Down

0 comments on commit 38e01d8

Please sign in to comment.