Skip to content

Commit 5354464

Browse files
committed
Add destinationIP method to WiFiUDP
Might be useful to distinguish between normal and multicast packets arriving at the same port (#105)
1 parent 727c61e commit 5354464

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

libraries/ESP8266WiFi/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ beginPacket KEYWORD2
4747
beginPacketMulticast KEYWORD2
4848
endPacket KEYWORD2
4949
parsePacket KEYWORD2
50+
destinationIP KEYWORD2
5051
remoteIP KEYWORD2
5152
remotePort KEYWORD2
5253
softAP KEYWORD2

libraries/ESP8266WiFi/src/WiFiUdp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ uint16_t WiFiUDP::remotePort()
240240
return _ctx->getRemotePort();
241241
}
242242

243+
IPAddress WiFiUDP::destinationIP()
244+
{
245+
IPAddress addr;
246+
247+
if (!_ctx)
248+
return addr;
249+
250+
addr = _ctx->getDestAddress();
251+
return addr;
252+
}
253+
243254
uint16_t WiFiUDP::localPort()
244255
{
245256
if (!_ctx)

libraries/ESP8266WiFi/src/WiFiUdp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class WiFiUDP : public UDP {
9797
virtual IPAddress remoteIP();
9898
// Return the port of the host who sent the current incoming packet
9999
virtual uint16_t remotePort();
100+
// Return the destination address for incoming packets,
101+
// useful to distinguish multicast and ordinary packets
102+
IPAddress destinationIP();
100103
// Return the local port for outgoing packets
101104
uint16_t localPort();
102105

libraries/ESP8266WiFi/src/include/UdpContext.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class UdpContext;
2626
extern "C" void esp_yield();
2727
extern "C" void esp_schedule();
2828

29-
29+
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
30+
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
3031
class UdpContext
3132
{
3233
public:
@@ -122,7 +123,7 @@ class UdpContext
122123
if (!_rx_buf)
123124
return 0;
124125

125-
struct ip_hdr* iphdr = (struct ip_hdr*) (((uint8_t*)_rx_buf->payload) - UDP_HLEN - IP_HLEN);
126+
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
126127
return iphdr->src.addr;
127128
}
128129

@@ -131,10 +132,16 @@ class UdpContext
131132
if (!_rx_buf)
132133
return 0;
133134

134-
struct udp_hdr* udphdr = (struct udp_hdr*) (((uint8_t*)_rx_buf->payload) - UDP_HLEN);
135+
udp_hdr* udphdr = GET_UDP_HDR(_rx_buf);
135136
return ntohs(udphdr->src);
136137
}
137138

139+
uint32_t getDestAddress()
140+
{
141+
ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
142+
return iphdr->dest.addr;
143+
}
144+
138145
uint16_t getLocalPort()
139146
{
140147
if (!_pcb)

0 commit comments

Comments
 (0)