Skip to content

Commit 26677a4

Browse files
bbx10me-no-dev
authored andcommitted
Add WiFiClient localIP and localPort (espressif#428)
Implement methods from ESP8266WiFiClient but use sockets API.
1 parent 73cd8d7 commit 26677a4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

libraries/WiFi/src/WiFiClient.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,34 @@ uint16_t WiFiClient::remotePort() const
346346
return remotePort(fd());
347347
}
348348

349+
IPAddress WiFiClient::localIP(int fd) const
350+
{
351+
struct sockaddr_storage addr;
352+
socklen_t len = sizeof addr;
353+
getsockname(fd, (struct sockaddr*)&addr, &len);
354+
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
355+
return IPAddress((uint32_t)(s->sin_addr.s_addr));
356+
}
357+
358+
uint16_t WiFiClient::localPort(int fd) const
359+
{
360+
struct sockaddr_storage addr;
361+
socklen_t len = sizeof addr;
362+
getsockname(fd, (struct sockaddr*)&addr, &len);
363+
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
364+
return ntohs(s->sin_port);
365+
}
366+
367+
IPAddress WiFiClient::localIP() const
368+
{
369+
return localIP(fd());
370+
}
371+
372+
uint16_t WiFiClient::localPort() const
373+
{
374+
return localPort(fd());
375+
}
376+
349377
bool WiFiClient::operator==(const WiFiClient& rhs)
350378
{
351379
return clientSocketHandle == rhs.clientSocketHandle && remotePort() == rhs.remotePort() && remoteIP() == rhs.remoteIP();

libraries/WiFi/src/WiFiClient.h

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class WiFiClient : public Client
8585
uint16_t remotePort() const;
8686
uint16_t remotePort(int fd) const;
8787

88+
IPAddress localIP() const;
89+
IPAddress localIP(int fd) const;
90+
uint16_t localPort() const;
91+
uint16_t localPort(int fd) const;
92+
8893
//friend class WiFiServer;
8994
using Print::write;
9095
};

0 commit comments

Comments
 (0)