Commit 4e43f8f7 authored by jan.koester's avatar jan.koester
Browse files

httpd: several bind addresses per listener

The address may list addresses separated by commas, semicolons or
whitespace ("10.1.2.20, fd10:12:2::20"); each gets its own TLS + QUIC
(or plain TCP) socket. An empty address stays one wildcard listener,
which libnetplus binds dual-stack on "::". With "::" in the list,
0.0.0.0 is dropped (it would fail with EADDRINUSE), except on OpenBSD,
which has no dual-stack sockets.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 2a6ea295
Loading
Loading
Loading
Loading
+53 −16
Changes for src/httpd.cpp: 53 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -3246,6 +3246,39 @@ void libhttppp::HttpEvent::DisconnectEvent(netplus::con &curcon,const int tid,UL
    DisconnectEvent(static_cast<HttpRequest&>(curcon),tid,args);
}

// A bind setting may list several addresses ("10.1.2.20, fd10:12:2::20",
// separated by commas, semicolons or whitespace); each gets its own listener.
// An empty setting yields one wildcard entry, which libnetplus binds as a
// dual-stack "::" socket (IPv4 and IPv6).
static std::vector<std::string> splitBindAddresses(const std::string &spec) {
    std::vector<std::string> out;
    std::string cur;
    auto flush = [&]() {
        if (!cur.empty()) {
            if (std::find(out.begin(), out.end(), cur) == out.end())
                out.push_back(cur);
            cur.clear();
        }
    };
    for (char c : spec) {
        if (c == ',' || c == ';' || c == ' ' || c == '\t' || c == '\r' || c == '\n')
            flush();
        else
            cur += c;
    }
    flush();
#ifndef __OpenBSD__
    // "::" is bound dual-stack (IPV6_V6ONLY=0) and already takes IPv4, so a
    // separate 0.0.0.0 on the same port would fail with EADDRINUSE. OpenBSD
    // has no dual-stack sockets and needs both entries.
    if (std::find(out.begin(), out.end(), "::") != out.end())
        out.erase(std::remove(out.begin(), out.end(), "0.0.0.0"), out.end());
#endif
    if (out.empty())
        out.push_back("");
    return out;
}

libhttppp::HttpD::HttpD(int argc, char** argv) {
    _fileServer = false;
    /*Register Parameters*/
@@ -3316,19 +3349,21 @@ libhttppp::HttpD::HttpD(int argc, char** argv) {
                }

                _certBundle[httpaddr] = bundle;
                auto tss = std::make_unique<netplus::ssl>(_certBundle, httpaddr, port, maxconnections, -1);
                auto qss = std::make_unique<Http3QuicSocket>(_certBundle, httpaddr, port, maxconnections, -1);
                _ServerSockets.push_back(std::move(tss));
                _ServerSockets.push_back(std::move(qss));
                for (const std::string &addr : splitBindAddresses(httpaddr)) {
                    _ServerSockets.push_back(std::make_unique<netplus::ssl>(_certBundle, addr, port, maxconnections, -1));
                    _ServerSockets.push_back(std::make_unique<Http3QuicSocket>(_certBundle, addr, port, maxconnections, -1));
                }
        }else{

            #ifndef Windows
            if (port > 0)
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, port, maxconnections,-1));
                for (const std::string &addr : splitBindAddresses(httpaddr))
                    _ServerSockets.push_back(std::make_unique<netplus::tcp>(addr, port, maxconnections, -1));
            else
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, maxconnections,-1));
            #else
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, port, maxconnections, -1));
                for (const std::string &addr : splitBindAddresses(httpaddr))
                    _ServerSockets.push_back(std::make_unique<netplus::tcp>(addr, port, maxconnections, -1));
            #endif 
        }

@@ -3358,19 +3393,21 @@ libhttppp::HttpD::HttpD(const std::string& httpaddr, int port,int maxconnections
                }

                _certBundle[httpaddr] = bundle;
                auto tss = std::make_unique<netplus::ssl>(_certBundle, httpaddr, port, maxconnections, -1);
                auto qss = std::make_unique<Http3QuicSocket>(_certBundle, httpaddr, port, maxconnections, -1);
                for (const std::string &addr : splitBindAddresses(httpaddr)) {
                    _ServerSockets.push_back(std::make_unique<netplus::ssl>(_certBundle, addr, port, maxconnections, -1));
                    _ServerSockets.push_back(std::make_unique<Http3QuicSocket>(_certBundle, addr, port, maxconnections, -1));
                }

                _ServerSockets.push_back(std::move(tss));
                _ServerSockets.push_back(std::move(qss));
        }else{
            #ifndef Windows
            if (port != -1)
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, port, maxconnections,-1));
                for (const std::string &addr : splitBindAddresses(httpaddr))
                    _ServerSockets.push_back(std::make_unique<netplus::tcp>(addr, port, maxconnections, -1));
            else
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, maxconnections,-1));
            #else
                _ServerSockets.push_back(std::make_unique<netplus::tcp>(httpaddr, port, maxconnections,-1));
                for (const std::string &addr : splitBindAddresses(httpaddr))
                    _ServerSockets.push_back(std::make_unique<netplus::tcp>(addr, port, maxconnections, -1));
            #endif
        }
    }catch (netplus::NetException &e) {
@@ -3397,10 +3434,10 @@ libhttppp::HttpD::HttpD(const std::string &httpaddr, int port, int maxconnection
                throw _httpexception;
            }
        }
        auto tss = std::make_unique<netplus::ssl>(_certBundle, httpaddr, port, maxconnections, -1);
        auto qss = std::make_unique<Http3QuicSocket>(_certBundle, httpaddr, port, maxconnections, -1);
        _ServerSockets.push_back(std::move(tss));
        _ServerSockets.push_back(std::move(qss));
        for (const std::string &addr : splitBindAddresses(httpaddr)) {
            _ServerSockets.push_back(std::make_unique<netplus::ssl>(_certBundle, addr, port, maxconnections, -1));
            _ServerSockets.push_back(std::make_unique<Http3QuicSocket>(_certBundle, addr, port, maxconnections, -1));
        }
    } catch (netplus::NetException &e) {
        HTTPException ee;
        ee[HTTPException::Critical] << e.what();