1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

feat(ext/net): enable sending to broadcast address (#12860)

You can now send UDP datagrams to the broadcast address.
This commit is contained in:
Michael Busby 2021-11-29 04:14:46 -06:00 committed by GitHub
parent 3881e2e7bf
commit 6c09e02304
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View file

@ -306,6 +306,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() {

View file

@ -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 {