Skip to content

Commit

Permalink
fix(netbios): Return interface address for NetBIOS name lookup (#9885)
Browse files Browse the repository at this point in the history
* 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.

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
earlephilhower and pre-commit-ci-lite[bot] authored Jun 18, 2024
1 parent 76b6ff6 commit 6b22339
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion libraries/NetBIOS/src/NetBIOS.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "NetBIOS.h"
#include <functional>
extern "C" {
#include <lwip/netif.h>
}; // extern "C"

#define NBNS_PORT 137
#define NBNS_MAX_HOSTNAME_LEN 32
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 6b22339

Please sign in to comment.