Skip to content

Commit fd67bf6

Browse files
committed
Fix incorrect use of getpeername
1 parent a2bc22a commit fd67bf6

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ size_t arduino::WiFiUDP::write(const uint8_t *buffer, size_t size) {
6262
}
6363

6464
int arduino::WiFiUDP::parsePacket() {
65-
nsapi_size_or_error_t ret = _socket.recvfrom(NULL, _packet_buffer, WIFI_UDP_BUFFER_SIZE);
65+
nsapi_size_or_error_t ret = _socket.recvfrom(&_remoteHost, _packet_buffer, WIFI_UDP_BUFFER_SIZE);
6666

6767
if (ret == NSAPI_ERROR_WOULD_BLOCK) {
6868
// no data
@@ -152,24 +152,12 @@ int arduino::WiFiUDP::read(unsigned char* buffer, size_t len) {
152152
}
153153

154154
IPAddress arduino::WiFiUDP::remoteIP() {
155-
SocketAddress remoteAddress("");
156-
157-
if(_socket.getpeername(&remoteAddress) != NSAPI_ERROR_OK){
158-
return nullptr;
159-
}
160-
161-
nsapi_addr_t address = remoteAddress.get_addr();
155+
nsapi_addr_t address = _remoteHost.get_addr();
162156
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
163157
}
164158

165-
uint16_t arduino::WiFiUDP::remotePort() {
166-
SocketAddress remoteAddress("");
167-
168-
if(_socket.getpeername(&remoteAddress) != NSAPI_ERROR_OK){
169-
return -1;
170-
}
171-
172-
return remoteAddress.get_port();
159+
uint16_t arduino::WiFiUDP::remotePort() {
160+
return _remoteHost.get_port();
173161
}
174162

175163
void arduino::WiFiUDP::flush(){

libraries/WiFi/src/WiFiUdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class WiFiUDP : public UDP {
3434
private:
3535
UDPSocket _socket; // Mbed OS socket
3636
SocketAddress _host; // Host to be used to send data
37+
SocketAddress _remoteHost; // Remote host that sent incoming packets
3738

3839
uint8_t* _packet_buffer; // Raw packet buffer (contains data we got from the UDPSocket)
3940

0 commit comments

Comments
 (0)