Skip to content

Add ipv6 support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/HTTPConnection.cpp
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
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
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