Skip to content

Commit

Permalink
chore: preview2_tcp_connect passing (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko authored Dec 28, 2023
1 parent 5da0581 commit 0790981
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
11 changes: 8 additions & 3 deletions packages/preview2-shim/lib/io/worker-socket-tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ export function createTcpSocket() {
}

export function socketTcpBind(id, payload) {
const { localAddress, localPort, family } = payload;
const { localAddress, localPort, family, isIpV6Only } = payload;
const socket = getSocketOrThrow(id);

let bind = "bind"; // ipv4
if (family.toLocaleLowerCase() === "ipv6") {
bind = "bind6";
bind = "bind6"; // ipv6
}

return socket[bind](localAddress, localPort);
let flags = 0;
if (isIpV6Only) {
flags |= TCPConstants.UV_TCP_IPV6ONLY;
}

return socket[bind](localAddress, localPort, flags);
}

export function socketTcpConnect(id, payload) {
Expand Down
11 changes: 9 additions & 2 deletions packages/preview2-shim/lib/nodejs/sockets/socket-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ export function deserializeIpAddress(addr, family) {
return address;
}

export function findUnsuedLocalAddress(family) {
export function findUnusedLocalAddress(
family,
{ iPv4MappedAddress = false } = {}
) {
let address = [127, 0, 0, 1];
if (family.toLocaleLowerCase() === "ipv6") {
address = [0, 0, 0, 0, 0, 0, 0, 1];
if (iPv4MappedAddress) {
address = [0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x0001];
} else {
address = [0, 0, 0, 0, 0, 0, 0, 1];
}
}
return {
tag: family,
Expand Down
40 changes: 22 additions & 18 deletions packages/preview2-shim/lib/nodejs/sockets/tcp-socket-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ import {
} from "../../io/worker-io.js";
import {
deserializeIpAddress,
findUnsuedLocalAddress,
findUnusedLocalAddress,
isIPv4MappedAddress,
isMulticastIpAddress,
isUnicastIpAddress,
isWildcardAddress,
serializeIpAddress,
} from "./socket-common.js";

Expand Down Expand Up @@ -138,14 +139,17 @@ export class TcpSocket {
return socket;
}

#autoBind(network, ipFamily) {
const unsusedLocalAddress = findUnsuedLocalAddress(ipFamily);
#autoBind(network, ipFamily, { iPv4MappedAddress = false } = {}) {
const localAddress = findUnusedLocalAddress(ipFamily, {
iPv4MappedAddress,
});
this.#socketOptions.localAddress = serializeIpAddress(
unsusedLocalAddress,
localAddress,
this.#socketOptions.family
);
this.#socketOptions.localPort = unsusedLocalAddress.val.port;
this.startBind(network, unsusedLocalAddress);
this.#socketOptions.localPort = localAddress.val.port;
this.#socketOptions.localIpSocketAddress = localAddress;
this.startBind(network, localAddress);
this.finishBind();
}

Expand Down Expand Up @@ -233,6 +237,8 @@ export class TcpSocket {
localAddress,
localPort,
family,
// Note: don't call getter method here, it will throw because of the assertion
isIpV6Only: this[symbolSocketState].ipv6Only,
});

if (err) {
Expand Down Expand Up @@ -290,8 +296,9 @@ export class TcpSocket {
);

assert(
host === "0.0.0.0" || host === "0:0:0:0:0:0:0:0",
"invalid-argument"
isWildcardAddress(remoteAddress),
"invalid-argument",
"The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`)"
);
assert(
this.#socketOptions.family.toLocaleLowerCase() !==
Expand All @@ -300,14 +307,14 @@ export class TcpSocket {
);
assert(isUnicastIpAddress(remoteAddress) === false, "invalid-argument");
assert(isMulticastIpAddress(remoteAddress), "invalid-argument");
assert(
isIPv4MappedAddress(remoteAddress) && this.ipv6Only(),
"invalid-argument"
);
const iPv4MappedAddress = isIPv4MappedAddress(remoteAddress);
assert(iPv4MappedAddress && this.ipv6Only(), "invalid-argument");
assert(remoteAddress.val.port === 0, "invalid-argument");

if (this[symbolSocketState].isBound === false) {
this.#autoBind(network, ipFamily);
this.#autoBind(network, ipFamily, {
iPv4MappedAddress,
});
}

assert(network !== this.network, "invalid-argument");
Expand Down Expand Up @@ -369,7 +376,7 @@ export class TcpSocket {
assert(err === -89, "-89"); // on macos

assert(err === -99, "ephemeral-ports-exhausted");
assert(err === -101, "remote-unreachable");
assert(err === -101, "remote-unreachable"); // wsl ubuntu
assert(err === -104, "connection-reset");
assert(err === -110, "timeout");
assert(err === -111, "connection-refused");
Expand Down Expand Up @@ -441,10 +448,7 @@ export class TcpSocket {
backlogSize: this[symbolSocketState].backlogSize,
});
if (err) {
console.error(`[tcp] listen error on socket: ${err}`);

// TODO: handle errors
throw new Error(err);
assert(true, "unknown", err);
}

this[symbolSocketState].connectionState = SocketConnectionState.Listening;
Expand Down
4 changes: 2 additions & 2 deletions packages/preview2-shim/lib/nodejs/sockets/udp-socket-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { ioCall, pollableCreate } from "../../io/worker-io.js";
import {
deserializeIpAddress,
findUnsuedLocalAddress,
findUnusedLocalAddress,
isIPv4MappedAddress,
isWildcardAddress,
serializeIpAddress,
Expand Down Expand Up @@ -270,7 +270,7 @@ export class UdpSocket {
}

#autoBind(network, ipFamily) {
const localAddress = findUnsuedLocalAddress(ipFamily);
const localAddress = findUnusedLocalAddress(ipFamily);
this.#socketOptions.localAddress = serializeIpAddress(
localAddress,
this.#socketOptions.family
Expand Down

0 comments on commit 0790981

Please sign in to comment.