From 34fdcff6d54aaa4dfea301fb8bf70401f8e43a0c Mon Sep 17 00:00:00 2001 From: slaff Date: Wed, 2 May 2018 16:02:38 +0200 Subject: [PATCH] Exposed the remote port and ip for the connections. (#1370) Closes: #1367 --- Sming/SmingCore/Network/Http/HttpConnection.h | 3 +++ Sming/SmingCore/Network/Http/HttpServerConnection.h | 3 +++ Sming/Wiring/IPAddress.h | 2 ++ samples/Basic_WebClient/app/application.cpp | 1 + samples/HttpServer_ConfigNetwork/app/application.cpp | 10 +++++++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index 10c950aa6d..21c1061ccc 100644 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -55,6 +55,9 @@ class HttpConnection : protected TcpClient { */ HttpResponse* getResponse(); + using TcpConnection::getRemoteIp; + using TcpConnection::getRemotePort; + using TcpClient::close; #ifdef ENABLE_SSL diff --git a/Sming/SmingCore/Network/Http/HttpServerConnection.h b/Sming/SmingCore/Network/Http/HttpServerConnection.h index 534df786d5..26701bc826 100644 --- a/Sming/SmingCore/Network/Http/HttpServerConnection.h +++ b/Sming/SmingCore/Network/Http/HttpServerConnection.h @@ -47,6 +47,9 @@ class HttpServerConnection: public TcpClient using TcpClient::send; + using TcpConnection::getRemoteIp; + using TcpConnection::getRemotePort; + protected: virtual err_t onReceive(pbuf *buf); virtual void onReadyToSendData(TcpConnectionEvent sourceEvent); diff --git a/Sming/Wiring/IPAddress.h b/Sming/Wiring/IPAddress.h index bc47350ac9..617bbc8be0 100644 --- a/Sming/Wiring/IPAddress.h +++ b/Sming/Wiring/IPAddress.h @@ -66,6 +66,8 @@ class IPAddress : public Printable operator uint32_t() { return *((uint32_t*)_address); }; operator ip_addr() { ip_addr ret; ret.addr = *((uint32_t*)_address); return ret; }; operator ip_addr*() { return (ip_addr*)_address; }; + + operator char*() { return (char *)toString().c_str(); } #if LWIP_VERSION_MAJOR == 2 operator ip_addr_t*() { return (ip_addr_t*)_address; }; #endif diff --git a/samples/Basic_WebClient/app/application.cpp b/samples/Basic_WebClient/app/application.cpp index 3421b55eca..3109ed2b13 100644 --- a/samples/Basic_WebClient/app/application.cpp +++ b/samples/Basic_WebClient/app/application.cpp @@ -74,6 +74,7 @@ void displayCipher(SSL *ssl) int onDownload(HttpConnection& connection, bool success) { + debugf("RemoteIP: %s", (char *)connection.getRemoteIp()); debugf("Got response code: %d", connection.getResponseCode()); debugf("Success: %d", success); if(connection.getRequest()->method != HTTP_HEAD) { diff --git a/samples/HttpServer_ConfigNetwork/app/application.cpp b/samples/HttpServer_ConfigNetwork/app/application.cpp index faf2234b5d..bfa0846ca6 100644 --- a/samples/HttpServer_ConfigNetwork/app/application.cpp +++ b/samples/HttpServer_ConfigNetwork/app/application.cpp @@ -18,10 +18,16 @@ void onIndex(HttpRequest &request, HttpResponse &response) response.sendTemplate(tmpl); // will be automatically deleted } -void onIpConfig(HttpRequest &request, HttpResponse &response) +int onIpConfig(HttpServerConnection& connection, HttpRequest &request, HttpResponse &response) { if (request.method == HTTP_POST) { + debugf("Request coming from IP: %s", (char *)connection.getRemoteIp()); + // If desired you can also limit the access based on remote IP. Example below: +// if(!(IPAddress("192.168.4.23") == connection.getRemoteIp())) { +// return 1; // error +// } + AppSettings.dhcp = request.getPostParameter("dhcp") == "1"; AppSettings.ip = request.getPostParameter("ip"); AppSettings.netmask = request.getPostParameter("netmask"); @@ -51,6 +57,8 @@ void onIpConfig(HttpRequest &request, HttpResponse &response) } response.sendTemplate(tmpl); // will be automatically deleted + + return 0; } void onFile(HttpRequest &request, HttpResponse &response)