Allow listening on multiple interfaces

This commit is contained in:
2025-09-03 16:09:16 -04:00
parent b8eb00e313
commit 46edb7cd26
6 changed files with 172 additions and 106 deletions

View File

@@ -79,9 +79,31 @@ void ConfigParser::parse_section(const auto &toml_data,
void ConfigParser::parse_server_config(const auto &toml_data,
ServerConfig &config) {
parse_section(toml_data, "server", [&](const auto &srv) {
parse_field(srv, "bind_address", config.bind_address);
parse_field(srv, "port", config.port);
parse_field(srv, "unix_socket_path", config.unix_socket_path);
// Parse interfaces array
if (srv.contains("interfaces")) {
auto interfaces = srv.at("interfaces");
if (interfaces.is_array()) {
for (const auto &iface : interfaces.as_array()) {
if (iface.contains("type")) {
std::string type = iface.at("type").as_string();
if (type == "tcp") {
std::string address = iface.at("address").as_string();
int port = iface.at("port").as_integer();
config.interfaces.push_back(ListenInterface::tcp(address, port));
} else if (type == "unix") {
std::string path = iface.at("path").as_string();
config.interfaces.push_back(ListenInterface::unix_socket(path));
}
}
}
}
}
// If no interfaces configured, use default TCP interface
if (config.interfaces.empty()) {
config.interfaces.push_back(ListenInterface::tcp("127.0.0.1", 8080));
}
parse_field(srv, "max_request_size_bytes", config.max_request_size_bytes);
parse_field(srv, "io_threads", config.io_threads);
@@ -127,24 +149,37 @@ void ConfigParser::parse_subscription_config(const auto &toml_data,
bool ConfigParser::validate_config(const Config &config) {
bool valid = true;
// Validate server configuration
if (config.server.unix_socket_path.empty()) {
// TCP mode validation
if (config.server.port <= 0 || config.server.port > 65535) {
std::cerr << "Configuration error: server.port must be between 1 and "
"65535, got "
<< config.server.port << std::endl;
valid = false;
}
} else {
// Unix socket mode validation
if (config.server.unix_socket_path.length() >
107) { // UNIX_PATH_MAX is typically 108
std::cerr << "Configuration error: unix_socket_path too long (max 107 "
"chars), got "
<< config.server.unix_socket_path.length() << " chars"
<< std::endl;
valid = false;
// Validate server interfaces
if (config.server.interfaces.empty()) {
std::cerr << "Configuration error: no interfaces configured" << std::endl;
valid = false;
}
for (const auto &iface : config.server.interfaces) {
if (iface.type == ListenInterface::Type::TCP) {
if (iface.port <= 0 || iface.port > 65535) {
std::cerr << "Configuration error: TCP port must be between 1 and "
"65535, got "
<< iface.port << std::endl;
valid = false;
}
if (iface.address.empty()) {
std::cerr << "Configuration error: TCP address cannot be empty"
<< std::endl;
valid = false;
}
} else { // Unix socket
if (iface.path.empty()) {
std::cerr << "Configuration error: Unix socket path cannot be empty"
<< std::endl;
valid = false;
}
if (iface.path.length() > 107) { // UNIX_PATH_MAX is typically 108
std::cerr << "Configuration error: Unix socket path too long (max 107 "
"chars), got "
<< iface.path.length() << " chars" << std::endl;
valid = false;
}
}
}