Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ConnectionContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SRC_CONNECTIONCONTEXT_HPP_

#include <Arduino.h>
#include <IPAddress.h>

// Required for SSL
#include "openssl/ssl.h"
Expand Down Expand Up @@ -30,6 +31,7 @@ class ConnectionContext {

virtual bool isSecure() = 0;
virtual void setWebsocketHandler(WebsocketHandler *wsHandler);
virtual IPAddress getClientIP() = 0;

WebsocketHandler * _wsHandler;
};
Expand Down
13 changes: 12 additions & 1 deletion src/HTTPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ HTTPConnection::~HTTPConnection() {
int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders) {
if (_connectionState == STATE_UNDEFINED) {
_defaultHeaders = defaultHeaders;
_addrLen = sizeof(_sockAddr);
_socket = accept(serverSocketID, (struct sockaddr * )&_sockAddr, &_addrLen);

// Build up SSL Connection context if the socket has been created successfully
Expand All @@ -42,11 +43,11 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
_httpHeaders = new HTTPHeaders();
refreshTimeout();
return _socket;

}

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

_addrLen = 0;
_connectionState = STATE_ERROR;
_clientState = CSTATE_ACTIVE;

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

/**
* Returns the client's IPv4
*/
IPAddress HTTPConnection::getClientIP() {
if (_addrLen > 0 && _sockAddr.sa_family == AF_INET) {
struct sockaddr_in *sockAddrIn = (struct sockaddr_in *)(&_sockAddr);
return IPAddress(sockAddrIn->sin_addr.s_addr);
}
return IPAddress(0, 0, 0, 0);
}

/**
* True if the connection is timed out.
Expand Down
2 changes: 2 additions & 0 deletions src/HTTPConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SRC_HTTPCONNECTION_HPP_

#include <Arduino.h>
#include <IPAddress.h>

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

void loop();
bool isClosed();
Expand Down
4 changes: 4 additions & 0 deletions src/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ HTTPNode * HTTPRequest::getResolvedNode() {
return _resolvedNode;
}

IPAddress HTTPRequest::getClientIP() {
return _con->getClientIP();
}

size_t HTTPRequest::readBytes(byte * buffer, size_t length) {

// Limit reading to content length
Expand Down
2 changes: 2 additions & 0 deletions src/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SRC_HTTPREQUEST_HPP_

#include <Arduino.h>
#include <IPAddress.h>
#include <string>

#include <mbedtls/base64.h>
Expand Down Expand Up @@ -29,6 +30,7 @@ class HTTPRequest {
std::string getRequestString();
std::string getMethod();
std::string getTag();
IPAddress getClientIP();

size_t readChars(char * buffer, size_t length);
size_t readBytes(byte * buffer, size_t length);
Expand Down