Skip to content

Commit 9ba205e

Browse files
authored
Merge pull request fhessel#71 from fhessel/feature/get-client-ip
Add getClientIP() to HTTPRequest
2 parents 7d1f6f2 + f814dd5 commit 9ba205e

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

src/ConnectionContext.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SRC_CONNECTIONCONTEXT_HPP_
33

44
#include <Arduino.h>
5+
#include <IPAddress.h>
56

67
// Required for SSL
78
#include "openssl/ssl.h"
@@ -30,6 +31,7 @@ class ConnectionContext {
3031

3132
virtual bool isSecure() = 0;
3233
virtual void setWebsocketHandler(WebsocketHandler *wsHandler);
34+
virtual IPAddress getClientIP() = 0;
3335

3436
WebsocketHandler * _wsHandler;
3537
};

src/HTTPConnection.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ HTTPConnection::~HTTPConnection() {
3333
int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders) {
3434
if (_connectionState == STATE_UNDEFINED) {
3535
_defaultHeaders = defaultHeaders;
36+
_addrLen = sizeof(_sockAddr);
3637
_socket = accept(serverSocketID, (struct sockaddr * )&_sockAddr, &_addrLen);
3738

3839
// Build up SSL Connection context if the socket has been created successfully
@@ -42,11 +43,11 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
4243
_httpHeaders = new HTTPHeaders();
4344
refreshTimeout();
4445
return _socket;
45-
4646
}
4747

4848
HTTPS_LOGE("Could not accept() new connection");
4949

50+
_addrLen = 0;
5051
_connectionState = STATE_ERROR;
5152
_clientState = CSTATE_ACTIVE;
5253

@@ -58,6 +59,16 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
5859
return -1;
5960
}
6061

62+
/**
63+
* Returns the client's IPv4
64+
*/
65+
IPAddress HTTPConnection::getClientIP() {
66+
if (_addrLen > 0 && _sockAddr.sa_family == AF_INET) {
67+
struct sockaddr_in *sockAddrIn = (struct sockaddr_in *)(&_sockAddr);
68+
return IPAddress(sockAddrIn->sin_addr.s_addr);
69+
}
70+
return IPAddress(0, 0, 0, 0);
71+
}
6172

6273
/**
6374
* True if the connection is timed out.

src/HTTPConnection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SRC_HTTPCONNECTION_HPP_
33

44
#include <Arduino.h>
5+
#include <IPAddress.h>
56

67
#include <string>
78
#include <mbedtls/base64.h>
@@ -42,6 +43,7 @@ class HTTPConnection : private ConnectionContext {
4243
virtual int initialize(int serverSocketID, HTTPHeaders *defaultHeaders);
4344
virtual void closeConnection();
4445
virtual bool isSecure();
46+
virtual IPAddress getClientIP();
4547

4648
void loop();
4749
bool isClosed();

src/HTTPRequest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ HTTPNode * HTTPRequest::getResolvedNode() {
5353
return _resolvedNode;
5454
}
5555

56+
IPAddress HTTPRequest::getClientIP() {
57+
return _con->getClientIP();
58+
}
59+
5660
size_t HTTPRequest::readBytes(byte * buffer, size_t length) {
5761

5862
// Limit reading to content length

src/HTTPRequest.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SRC_HTTPREQUEST_HPP_
33

44
#include <Arduino.h>
5+
#include <IPAddress.h>
56
#include <string>
67

78
#include <mbedtls/base64.h>
@@ -29,6 +30,7 @@ class HTTPRequest {
2930
std::string getRequestString();
3031
std::string getMethod();
3132
std::string getTag();
33+
IPAddress getClientIP();
3234

3335
size_t readChars(char * buffer, size_t length);
3436
size_t readBytes(byte * buffer, size_t length);

0 commit comments

Comments
 (0)