From 26677a4b055e572320de4e61ff90679081f75e52 Mon Sep 17 00:00:00 2001 From: bbx10 Date: Wed, 14 Jun 2017 01:17:14 -0700 Subject: [PATCH] Add WiFiClient localIP and localPort (#428) Implement methods from ESP8266WiFiClient but use sockets API. --- libraries/WiFi/src/WiFiClient.cpp | 28 ++++++++++++++++++++++++++++ libraries/WiFi/src/WiFiClient.h | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index d6f27354..bcea2bb8 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -346,6 +346,34 @@ uint16_t WiFiClient::remotePort() const return remotePort(fd()); } +IPAddress WiFiClient::localIP(int fd) const +{ + struct sockaddr_storage addr; + socklen_t len = sizeof addr; + getsockname(fd, (struct sockaddr*)&addr, &len); + struct sockaddr_in *s = (struct sockaddr_in *)&addr; + return IPAddress((uint32_t)(s->sin_addr.s_addr)); +} + +uint16_t WiFiClient::localPort(int fd) const +{ + struct sockaddr_storage addr; + socklen_t len = sizeof addr; + getsockname(fd, (struct sockaddr*)&addr, &len); + struct sockaddr_in *s = (struct sockaddr_in *)&addr; + return ntohs(s->sin_port); +} + +IPAddress WiFiClient::localIP() const +{ + return localIP(fd()); +} + +uint16_t WiFiClient::localPort() const +{ + return localPort(fd()); +} + bool WiFiClient::operator==(const WiFiClient& rhs) { return clientSocketHandle == rhs.clientSocketHandle && remotePort() == rhs.remotePort() && remoteIP() == rhs.remoteIP(); diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index 5472c74d..6d27c3e4 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -85,6 +85,11 @@ public: uint16_t remotePort() const; uint16_t remotePort(int fd) const; + IPAddress localIP() const; + IPAddress localIP(int fd) const; + uint16_t localPort() const; + uint16_t localPort(int fd) const; + //friend class WiFiServer; using Print::write; };