Unverified Commit 6b223391 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

fix(netbios): Return interface address for NetBIOS name lookup (#9885)

* 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: default avatarpre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
parent 76b6ff65
#include "NetBIOS.h" #include "NetBIOS.h"
#include <functional> #include <functional>
extern "C" {
#include <lwip/netif.h>
}; // extern "C"
#define NBNS_PORT 137 #define NBNS_PORT 137
#define NBNS_MAX_HOSTNAME_LEN 32 #define NBNS_MAX_HOSTNAME_LEN 32
...@@ -91,7 +94,16 @@ void NetBIOS::_onPacket(AsyncUDPPacket &packet) { ...@@ -91,7 +94,16 @@ void NetBIOS::_onPacket(AsyncUDPPacket &packet) {
append_32((void *)&nbnsa.ttl, 300000); append_32((void *)&nbnsa.ttl, 300000);
append_16((void *)&nbnsa.data_len, 6); append_16((void *)&nbnsa.data_len, 6);
append_16((void *)&nbnsa.flags, 0); 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); _udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment