From dd745f29731e1a33ce9cb92e322a479ac2d63233 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Mon, 23 Sep 2024 22:14:43 -0600 Subject: [PATCH] os: expose broadcast address in os.networkInterfaces() Not using the correct broadcast address in applications can cause disasterous results. Rather than letting the programmer guess at the broadcast address and try to derive it correctly, allow him to query the system instead for the correctly configured state. Fixes: https://github.com/nodejs/node/issues/23437 Depends-on: https://github.com/libuv/libuv/pull/2033 --- doc/api/os.md | 2 ++ doc/api/report.md | 1 + src/env_properties.h | 1 + src/node_os.cc | 2 ++ 4 files changed, 6 insertions(+) diff --git a/doc/api/os.md b/doc/api/os.md index a367a8c4aa0234..d627360904f96f 100644 --- a/doc/api/os.md +++ b/doc/api/os.md @@ -289,6 +289,7 @@ The properties available on the assigned network address object include: * `address` {string} The assigned IPv4 or IPv6 address * `netmask` {string} The IPv4 or IPv6 network mask +* `broadcast` {string} The IPv4 or IPv6 broadcast address (not available on all interface types) * `family` {string} Either `IPv4` or `IPv6` * `mac` {string} The MAC address of the network interface * `internal` {boolean} `true` if the network interface is a loopback or @@ -326,6 +327,7 @@ The properties available on the assigned network address object include: { address: '192.168.1.108', netmask: '255.255.255.0', + broadcast: '192.168.1.255', family: 'IPv4', mac: '01:02:03:0a:0b:0c', internal: false, diff --git a/doc/api/report.md b/doc/api/report.md index 19a39400ff9bee..869243031353d1 100644 --- a/doc/api/report.md +++ b/doc/api/report.md @@ -98,6 +98,7 @@ is provided below for reference. "mac": "13:10:de:ad:be:ef", "address": "10.0.0.37", "netmask": "255.255.255.0", + "broadcast": "10.0.0.255", "family": "IPv4" } ], diff --git a/src/env_properties.h b/src/env_properties.h index 4265b87c516375..fe462799947cd0 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -80,6 +80,7 @@ V(base_string, "base") \ V(bits_string, "bits") \ V(block_list_string, "blockList") \ + V(broadcast_string, "broadcast") \ V(buffer_string, "buffer") \ V(bytes_parsed_string, "bytesParsed") \ V(bytes_read_string, "bytesRead") \ diff --git a/src/node_os.cc b/src/node_os.cc index 7318c1a368d871..f4e49d26e0feff 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -177,6 +177,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { int count, i; char ip[INET6_ADDRSTRLEN]; char netmask[INET6_ADDRSTRLEN]; + char broadcast[INET6_ADDRSTRLEN]; std::array mac; Local name, family; @@ -218,6 +219,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { if (interfaces[i].address.address4.sin_family == AF_INET) { uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip)); uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask)); + uv_ip4_name(&interfaces[i].broadcast.broadcast4, broadcast, sizeof(broadcast)); family = env->ipv4_string(); } else if (interfaces[i].address.address4.sin_family == AF_INET6) { uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));