Skip to content

Commit 08f9f8c

Browse files
committed
Fake networking socket handling cleanup.
1 parent db22e06 commit 08f9f8c

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

src/browser/fake_network.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,31 @@ function handle_fake_tcp(packet, adapter)
243243
{
244244
const tuple = `${packet.ipv4.src.join(".")}:${packet.tcp.sport}:${packet.ipv4.dest.join(".")}:${packet.tcp.dport}`;
245245

246-
if(packet.tcp.syn) {
246+
if(packet.tcp.syn && !packet.tcp.ack) {
247247
if(adapter.tcp_conn[tuple]) {
248248
dbg_log("SYN to already opened port", LOG_FETCH);
249+
delete adapter.tcp_conn[tuple];
249250
}
250-
if(adapter.on_tcp_connection(packet, tuple)) {
251-
return;
251+
252+
let conn = new TCPConnection();
253+
conn.state = TCP_STATE_SYN_RECEIVED;
254+
conn.net = adapter;
255+
conn.tuple = tuple;
256+
conn.last = packet;
257+
258+
conn.hsrc = packet.eth.dest;
259+
conn.psrc = packet.ipv4.dest;
260+
conn.sport = packet.tcp.dport;
261+
conn.hdest = packet.eth.src;
262+
conn.dport = packet.tcp.sport;
263+
conn.pdest = packet.ipv4.src;
264+
265+
adapter.bus.pair.send("tcp-connection", conn);
266+
267+
if(adapter.on_tcp_connection) {
268+
adapter.on_tcp_connection(conn, packet);
252269
}
270+
if(adapter.tcp_conn[tuple]) return;
253271
}
254272

255273
if(!adapter.tcp_conn[tuple]) {
@@ -1083,7 +1101,9 @@ TCPConnection.prototype.connect = function() {
10831101
this.ack = 1;
10841102
this.start_seq = 0;
10851103
this.winsize = 64240;
1086-
this.state = TCP_STATE_SYN_SENT;
1104+
if(this.state !== TCP_STATE_SYN_PROBE) {
1105+
this.state = TCP_STATE_SYN_SENT;
1106+
}
10871107

10881108
let reply = this.ipv4_reply();
10891109
reply.ipv4.id = 2345;
@@ -1099,16 +1119,12 @@ TCPConnection.prototype.connect = function() {
10991119
};
11001120

11011121

1102-
TCPConnection.prototype.accept = function(packet) {
1122+
TCPConnection.prototype.accept = function(packet=undefined) {
1123+
packet = packet || this.last;
1124+
this.net.tcp_conn[this.tuple] = this;
11031125
this.seq = 1338;
11041126
this.ack = packet.tcp.seq + 1;
11051127
this.start_seq = packet.tcp.seq;
1106-
this.hsrc = this.net.router_mac;
1107-
this.psrc = packet.ipv4.dest;
1108-
this.sport = packet.tcp.dport;
1109-
this.hdest = packet.eth.src;
1110-
this.dport = packet.tcp.sport;
1111-
this.pdest = packet.ipv4.src;
11121128
this.winsize = packet.tcp.winsize;
11131129

11141130
let reply = this.ipv4_reply();
@@ -1127,6 +1143,7 @@ TCPConnection.prototype.accept = function(packet) {
11271143
};
11281144

11291145
TCPConnection.prototype.process = function(packet) {
1146+
this.last = packet;
11301147
if(this.state === TCP_STATE_CLOSED) {
11311148
// dbg_log(`TCP[${this.tuple}]: WARNING: connection already closed, packet dropped`, LOG_FETCH);
11321149
const reply = this.packet_reply(packet, {rst: true});

src/browser/fetch_network.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { LOG_FETCH } from "../const.js";
2-
import { h } from "../lib.js";
32
import { dbg_log } from "../log.js";
43

54
import {
@@ -46,27 +45,18 @@ export function FetchNetworkAdapter(bus, config)
4645
{
4746
this.send(data);
4847
}, this);
48+
this.bus.register("tcp-connection", (conn) => {
49+
if(conn.sport === 80) {
50+
conn.on("data", on_data_http);
51+
conn.accept();
52+
}
53+
}, this);
4954
}
5055

5156
FetchNetworkAdapter.prototype.destroy = function()
5257
{
5358
};
5459

55-
FetchNetworkAdapter.prototype.on_tcp_connection = function(packet, tuple)
56-
{
57-
if(packet.tcp.dport === 80) {
58-
let conn = new TCPConnection();
59-
conn.state = TCP_STATE_SYN_RECEIVED;
60-
conn.net = this;
61-
conn.on("data", on_data_http);
62-
conn.tuple = tuple;
63-
conn.accept(packet);
64-
this.tcp_conn[tuple] = conn;
65-
return true;
66-
}
67-
return false;
68-
};
69-
7060
FetchNetworkAdapter.prototype.connect = function(port)
7161
{
7262
return fake_tcp_connect(port, this);
@@ -144,7 +134,7 @@ async function on_data_http(data)
144134
const fetch_url = this.net.cors_proxy ? this.net.cors_proxy + encodeURIComponent(target.href) : target.href;
145135
const encoder = new TextEncoder();
146136
let response_started = false;
147-
this.net.fetch(fetch_url, opts).then((resp) => {
137+
let handler = (resp) => {
148138
let resp_headers = new Headers(resp.headers);
149139
resp_headers.delete("content-encoding");
150140
resp_headers.delete("keep-alive");
@@ -177,7 +167,9 @@ async function on_data_http(data)
177167
this.close();
178168
});
179169
}
180-
})
170+
};
171+
172+
this.net.fetch(fetch_url, opts).then(handler)
181173
.catch((e) => {
182174
console.warn("Fetch Failed: " + fetch_url + "\n" + e);
183175
if(!response_started) {

src/browser/wisp_network.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,12 @@ WispNetworkAdapter.prototype.destroy = function()
185185
};
186186

187187
/**
188+
* @param {TCPConnection} conn
188189
* @param {Uint8Array} packet
189-
* @param {String} tuple
190190
*/
191-
WispNetworkAdapter.prototype.on_tcp_connection = function(packet, tuple)
191+
WispNetworkAdapter.prototype.on_tcp_connection = function(conn, packet)
192192
{
193-
let conn = new TCPConnection();
194-
conn.state = TCP_STATE_SYN_RECEIVED;
195-
conn.net = this;
196-
conn.tuple = tuple;
197193
conn.stream_id = this.last_stream++;
198-
this.tcp_conn[tuple] = conn;
199194

200195
conn.on("data", data => {
201196
if(data.length !== 0) {
@@ -222,7 +217,7 @@ WispNetworkAdapter.prototype.on_tcp_connection = function(packet, tuple)
222217
type: "CONNECT",
223218
stream_id: conn.stream_id,
224219
hostname: packet.ipv4.dest.join("."),
225-
port: packet.tcp.dport,
220+
port: conn.sport,
226221
data_callback: (data) => {
227222
conn.write(data);
228223
},
@@ -231,7 +226,7 @@ WispNetworkAdapter.prototype.on_tcp_connection = function(packet, tuple)
231226
}
232227
});
233228

234-
conn.accept(packet);
229+
conn.accept();
235230
return true;
236231
};
237232

src/virtio_console.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,9 @@ export function VirtioConsole(cpu, bus)
107107
{
108108
dbg_assert(false, "VirtioConsole Notified for wrong queue: " + queue_id +
109109
" (expected queue_id of 2)");
110-
return;
110+
111111
}
112-
const queue = this.virtio.queues[queue_id];
113-
// Full buffer looks like an empty buffer so prevent it from filling
114-
while(queue.count_requests() > queue.size - 2) queue.pop_request();
112+
115113
},
116114
(queue_id) =>
117115
{

0 commit comments

Comments
 (0)