Skip to content

Commit e026902

Browse files
Return interface address for NetBIOS name lookup
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.
1 parent 86b3163 commit e026902

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Diff for: libraries/NetBIOS/src/NetBIOS.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "NetBIOS.h"
22
#include <functional>
3+
extern "C" {
4+
#include <lwip/netif.h>
5+
}; // extern "C"
36

47
#define NBNS_PORT 137
58
#define NBNS_MAX_HOSTNAME_LEN 32
@@ -91,7 +94,16 @@ void NetBIOS::_onPacket(AsyncUDPPacket &packet) {
9194
append_32((void *)&nbnsa.ttl, 300000);
9295
append_16((void *)&nbnsa.data_len, 6);
9396
append_16((void *)&nbnsa.flags, 0);
94-
nbnsa.addr = packet.localIP();
97+
nbnsa.addr = packet.localIP(); // By default, should be overridden below
98+
// Iterate over all netifs, see if the incoming address matches one of the netmaskes networks
99+
for (auto netif = netif_list; netif; netif = netif->next) {
100+
auto maskedip = ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif));
101+
auto maskedin = ((uint32_t)packet.localIP()) & ip4_addr_get_u32(netif_ip4_netmask(netif));
102+
if (maskedip == maskedin) {
103+
nbnsa.addr = ip4_addr_get_u32(netif_ip4_addr(netif));
104+
break;
105+
}
106+
}
95107
_udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT);
96108
}
97109
}

0 commit comments

Comments
 (0)