diff --git a/libraries/NetBIOS/src/NetBIOS.cpp b/libraries/NetBIOS/src/NetBIOS.cpp index 6d3f274a137..ca2c29a4c4d 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); } }