From 9d8b43beb664f7c8131e330b5d2c0200b08ff328 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Mon, 17 Jun 2024 17:32:48 -0700 Subject: [PATCH] fix(netbios): Return interface address for NetBIOS When NetBIOS returns a match, it should return the IP address of the device. Presently, however, it returns the address of multicast IP for the subnet since the incoming NBNS packet's UDP target will be multicast (i.e. 192.168.1.255). Iterate over the active network interfaces and check netmasks to determine where the packet came from, and return the appropriate IP interface address instead. --- libraries/NetBIOS/src/NetBIOS.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libraries/NetBIOS/src/NetBIOS.cpp b/libraries/NetBIOS/src/NetBIOS.cpp index 6d3f274a137..23078b35c5d 100644 --- a/libraries/NetBIOS/src/NetBIOS.cpp +++ b/libraries/NetBIOS/src/NetBIOS.cpp @@ -1,5 +1,8 @@ #include "NetBIOS.h" #include +extern "C" { +#include +}; // extern "C" #define NBNS_PORT 137 #define NBNS_MAX_HOSTNAME_LEN 32 @@ -91,7 +94,16 @@ void NetBIOS::_onPacket(AsyncUDPPacket &packet) { append_32((void *)&nbnsa.ttl, 300000); append_16((void *)&nbnsa.data_len, 6); append_16((void *)&nbnsa.flags, 0); - nbnsa.addr = packet.localIP(); + nbnsa.addr = packet.localIP(); // By default, should be overridden below + // Iterate over all netifs, see if the incoming address matches one of the netmaskes networks + for (auto netif = netif_list; netif; netif = netif->next) { + auto maskedip = ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)); + auto maskedin = ((uint32_t)packet.localIP()) & ip4_addr_get_u32(netif_ip4_netmask(netif)); + if (maskedip == maskedin) { + nbnsa.addr = ip4_addr_get_u32(netif_ip4_addr(netif)); + break; + } + } _udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT); } }