FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add ipv6 support by Xavierhorwood · Pull Request #6 · meshtastic/esp32_https_server · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (2) .hpp  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions src/HTTPConnection.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ IPAddress HTTPConnection::getClientIP() {
struct sockaddr_in *sockAddrIn = (struct sockaddr_in *)(&_sockAddr);
return IPAddress(sockAddrIn->sin_addr.s_addr);
}
if (_addrLen > 0 && _sockAddr.sa_family == AF_INET6) {
struct sockaddr_in6 *sockAddrIn = (struct sockaddr_in6 *)(&_sockAddr);
return IPAddress(sockAddrIn->sin6_addr.s6_addr);
}
return IPAddress(0, 0, 0, 0);
}

Expand Down
38 changes: 31 additions & 7 deletions src/HTTPServer.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "HTTPServer.hpp"
#include <WiFi.h>

namespace httpsserver {

Expand Down Expand Up @@ -168,15 +169,38 @@ int HTTPServer::createConnection(int idx) {
* This method prepares the tcp server socket
*/
uint8_t HTTPServer::setupSocket() {
// (AF_INET = IPv4, SOCK_STREAM = TCP)
_socket = socket(AF_INET, SOCK_STREAM, 0);
bool IPv6 = WiFi.localIPv6() == IPv6Address();

if (!IPv6)
{
// (AF_INET = IPv4, SOCK_STREAM = TCP)
_socket = socket(AF_INET, SOCK_STREAM, 0);
} else {
_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6);
int opt = 0;
//Allow to bind to both ipv4 and ipv6
setsockopt(_socket, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
}

if (_socket>=0) {
_sock_addr.sin_family = AF_INET;
// Listen on all interfaces
_sock_addr.sin_addr.s_addr = _bindAddress;
// Set the server port
_sock_addr.sin_port = htons(_port);
if (!IPv6)
{
struct sockaddr_in *dest_addr = (struct sockaddr_in *)&_sock_addr;
dest_addr->sin_family = AF_INET;
// Listen on all interfaces
dest_addr->sin_addr.s_addr = _bindAddress;
// Set the server port
dest_addr->sin_port = htons(_port);
}
else
{
struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&_sock_addr;
bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un));

dest_addr_ip6->sin6_family = AF_INET6;
// Set the server port
dest_addr_ip6->sin6_port = htons(_port);
}

// Now bind the TCP socket we did create above to the socket address we specified
// (The TCP-socket now listens on 0.0.0.0:port)
Expand Down
2 changes: 1 addition & 1 deletion src/HTTPServer.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class HTTPServer : public ResourceResolver {
int _socket;

// The server socket address, that our service is bound to
sockaddr_in _sock_addr;
sockaddr_storage _sock_addr;
// Headers that are included in every response
HTTPHeaders _defaultHeaders;

Expand Down

Back | FazBrowse Home | New Git URL