X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=config.cpp;h=e7c9c844d1ab4ffd5f24cd9f7fcf493b2f8ecf8e;hp=cb16cbc33e06f7d1032d2b8d13f20379b20c262a;hb=f2530dbb8415f9e7cc0f2d4b45741120c95e8a05;hpb=bd694fdd3dd1417399aecead2c8b91fc4fe95ce8 diff --git a/config.cpp b/config.cpp index cb16cbc..e7c9c84 100644 --- a/config.cpp +++ b/config.cpp @@ -11,6 +11,7 @@ #include #include +#include "acceptor.h" #include "config.h" #include "log.h" #include "parse.h" @@ -119,13 +120,81 @@ bool parse_port(const ConfigLine &line, Config *config) return false; } + int port = atoi(line.arguments[0].c_str()); + if (port < 1 || port >= 65536) { + log(ERROR, "port %d is out of range (must be [1,65536>).", port); + return false; + } + AcceptorConfig acceptor; - acceptor.port = atoi(line.arguments[0].c_str()); - if (acceptor.port < 1 || acceptor.port >= 65536) { - log(ERROR, "port %d is out of range (must be [1,65536>).", acceptor.port); + acceptor.addr = CreateAnyAddress(port); + + config->acceptors.push_back(acceptor); + return true; +} + +bool parse_listen(const ConfigLine &line, Config *config) +{ + if (line.arguments.size() != 1) { + log(ERROR, "'listen' takes exactly one argument"); + return false; + } + + string addr_string = line.arguments[0]; + if (addr_string.empty()) { + // Actually, this should never happen. + log(ERROR, "'listen' argument cannot be empty"); return false; } + string port_string; + + AcceptorConfig acceptor; + memset(&acceptor.addr, 0, sizeof(acceptor.addr)); + acceptor.addr.sin6_family = AF_INET6; + if (addr_string[0] == '[') { + // IPv6 address: [addr]:port. + size_t addr_end = addr_string.find("]:"); + if (addr_end == string::npos) { + log(ERROR, "IPv6 address '%s' should be on form [address]:port", addr_string.c_str()); + return false; + } + + string addr_only = addr_string.substr(1, addr_end - 1); + if (inet_pton(AF_INET6, addr_only.c_str(), &acceptor.addr.sin6_addr) != 1) { + log(ERROR, "Invalid IPv6 address '%s'", addr_only.c_str()); + return false; + } + + port_string = addr_string.substr(addr_end + 2); + } else { + // IPv4 address: addr:port. + size_t addr_end = addr_string.find(":"); + if (addr_end == string::npos) { + log(ERROR, "IPv4 address '%s' should be on form address:port", addr_string.c_str()); + return false; + } + + in_addr addr4; + string addr_only = addr_string.substr(0, addr_end); + if (inet_pton(AF_INET, addr_only.c_str(), &addr4) != 1) { + log(ERROR, "Invalid IPv4 address '%s'", addr_only.c_str()); + return false; + } + + // Convert to a v4-mapped address. + acceptor.addr.sin6_addr.s6_addr32[2] = htonl(0xffff); + acceptor.addr.sin6_addr.s6_addr32[3] = addr4.s_addr; + port_string = addr_string.substr(addr_end + 1); + } + + int port = atoi(port_string.c_str()); + if (port < 1 || port >= 65536) { + log(ERROR, "port %d is out of range (must be [1,65536>).", port); + return false; + } + acceptor.addr.sin6_port = ntohs(port); + config->acceptors.push_back(acceptor); return true; } @@ -234,6 +303,14 @@ bool parse_stream(const ConfigLine &line, Config *config) stream.mark_pool = allocate_mark_pool(from, to, config); } + // Parse the pacing rate, converting from kilobits to bytes as needed. + map::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit"); + if (pacing_rate_it == line.parameters.end()) { + stream.pacing_rate = ~0U; + } else { + stream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8; + } + config->streams.push_back(stream); return true; } @@ -316,9 +393,18 @@ bool parse_udpstream(const ConfigLine &line, Config *config) udpstream.mark_pool = allocate_mark_pool(from, to, config); } + // Parse the pacing rate, converting from kilobits to bytes as needed. + map::const_iterator pacing_rate_it = line.parameters.find("pacing_rate_kbit"); + if (pacing_rate_it == line.parameters.end()) { + udpstream.pacing_rate = ~0U; + } else { + udpstream.pacing_rate = atoi(pacing_rate_it->second.c_str()) * 1024 / 8; + } + config->udpstreams.push_back(udpstream); return true; } + bool parse_error_log(const ConfigLine &line, Config *config) { if (line.arguments.size() != 0) { @@ -406,6 +492,10 @@ bool parse_config(const string &filename, Config *config) if (!parse_port(line, config)) { return false; } + } else if (line.keyword == "listen") { + if (!parse_listen(line, config)) { + return false; + } } else if (line.keyword == "stream") { if (!parse_stream(line, config)) { return false;