summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit/net_test.ts38
-rw-r--r--ext/net/ops.rs2
2 files changed, 40 insertions, 0 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 7ea1b8bcf..7efc7e689 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -308,6 +308,44 @@ Deno.test(
Deno.test(
{ permissions: { net: true } },
+ async function netUdpSendReceiveBroadcast() {
+ // Must bind sender to an address that can send to the broadcast address on MacOS.
+ // Macos will give us error 49 when sending the broadcast packet if we omit hostname here.
+ const alice = Deno.listenDatagram({
+ port: 3500,
+ transport: "udp",
+ hostname: "0.0.0.0",
+ });
+
+ const bob = Deno.listenDatagram({
+ port: 4501,
+ transport: "udp",
+ hostname: "0.0.0.0",
+ });
+ assert(bob.addr.transport === "udp");
+ assertEquals(bob.addr.port, 4501);
+ assertEquals(bob.addr.hostname, "0.0.0.0");
+
+ const broadcastAddr = { ...bob.addr, hostname: "255.255.255.255" };
+
+ const sent = new Uint8Array([1, 2, 3]);
+ const byteLength = await alice.send(sent, broadcastAddr);
+
+ assertEquals(byteLength, 3);
+ const [recvd, remote] = await bob.receive();
+ assert(remote.transport === "udp");
+ assertEquals(remote.port, 3500);
+ assertEquals(recvd.length, 3);
+ assertEquals(1, recvd[0]);
+ assertEquals(2, recvd[1]);
+ assertEquals(3, recvd[2]);
+ alice.close();
+ bob.close();
+ },
+);
+
+Deno.test(
+ { permissions: { net: true } },
async function netUdpConcurrentSendReceive() {
const socket = Deno.listenDatagram({ port: 3500, transport: "udp" });
assert(socket.addr.transport === "udp");
diff --git a/ext/net/ops.rs b/ext/net/ops.rs
index 1f7005247..739807123 100644
--- a/ext/net/ops.rs
+++ b/ext/net/ops.rs
@@ -440,6 +440,8 @@ fn listen_udp(
) -> Result<(u32, SocketAddr), AnyError> {
let std_socket = std::net::UdpSocket::bind(&addr)?;
std_socket.set_nonblocking(true)?;
+ // Enable messages to be sent to the broadcast address (255.255.255.255) by default
+ std_socket.set_broadcast(true)?;
let socket = UdpSocket::from_std(std_socket)?;
let local_addr = socket.local_addr()?;
let socket_resource = UdpSocketResource {