util.cpp (8060c0dab000f50c2a69f4103e6c41344ed35c82) | util.cpp (e94c9ffcc639f6280972bb28b26bb357b830491f) |
---|---|
1#include "config.h" 2 3#include "util.hpp" 4 5#include "config_parser.hpp" 6#include "types.hpp" 7 8#include <arpa/inet.h> --- 352 unchanged lines hidden (view full) --- 361 } 362 if (idx == 0) 363 { 364 return "ethaddr"; 365 } 366 return "eth" + std::to_string(idx) + "addr"; 367} 368 | 1#include "config.h" 2 3#include "util.hpp" 4 5#include "config_parser.hpp" 6#include "types.hpp" 7 8#include <arpa/inet.h> --- 352 unchanged lines hidden (view full) --- 361 } 362 if (idx == 0) 363 { 364 return "ethaddr"; 365 } 366 return "eth" + std::to_string(idx) + "addr"; 367} 368 |
369bool getIPv6AcceptRA(const config::Parser& config) | 369static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) |
370{ | 370{ |
371#ifdef ENABLE_IPV6_ACCEPT_RA 372 constexpr bool def = true; 373#else 374 constexpr bool def = false; 375#endif 376 377 auto value = config.map.getLastValueString("Network", "IPv6AcceptRA"); 378 if (value == nullptr) | 371 if (config::icaseeq(str, "ipv4")) |
379 { | 372 { |
380 auto msg = fmt::format( 381 "Unable to get the value for Network[IPv6AcceptRA] from {}", 382 config.getFilename().native()); 383 log<level::NOTICE>(msg.c_str(), 384 entry("FILE=%s", config.getFilename().c_str())); 385 return def; | 373 return DHCPVal{.v4 = true, .v6 = false}; |
386 } | 374 } |
387 auto ret = config::parseBool(*value); 388 if (!ret.has_value()) | 375 if (config::icaseeq(str, "ipv6")) |
389 { | 376 { |
390 auto msg = fmt::format( 391 "Failed to parse section Network[IPv6AcceptRA] from {}: `{}`", 392 config.getFilename().native(), *value); 393 log<level::NOTICE>(msg.c_str(), 394 entry("FILE=%s", config.getFilename().c_str()), 395 entry("VALUE=%s", value->c_str())); | 377 return DHCPVal{.v4 = false, .v6 = true}; |
396 } | 378 } |
397 return ret.value_or(def); | 379 if (auto b = config::parseBool(str); b) 380 { 381 return DHCPVal{.v4 = *b, .v6 = *b}; 382 } 383 return std::nullopt; |
398} 399 | 384} 385 |
400DHCPVal getDHCPValue(const config::Parser& config) | 386inline auto systemdParseLast(const config::Parser& config, 387 std::string_view section, std::string_view key, 388 auto&& fun) |
401{ | 389{ |
402 constexpr auto def = DHCPVal{.v4 = true, .v6 = true}; 403 404 const auto value = config.map.getLastValueString("Network", "DHCP"); 405 if (value == nullptr) | 390 if (auto str = config.map.getLastValueString(section, key); str == nullptr) |
406 { | 391 { |
407 auto msg = 408 fmt::format("Unable to get the value for Network[DHCP] from {}", 409 config.getFilename().native()); 410 log<level::NOTICE>(msg.c_str(), | 392 auto err = fmt::format("Unable to get the value of {}[{}] from {}", 393 section, key, config.getFilename().native()); 394 log<level::NOTICE>(err.c_str(), |
411 entry("FILE=%s", config.getFilename().c_str())); | 395 entry("FILE=%s", config.getFilename().c_str())); |
412 return def; | |
413 } | 396 } |
414 if (config::icaseeq(*value, "ipv4")) | 397 else if (auto val = fun(*str); !val) |
415 { | 398 { |
416 return DHCPVal{.v4 = true, .v6 = false}; | 399 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section, 400 key, config.getFilename().native(), *str); 401 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()), 402 entry("FILE=%s", config.getFilename().c_str())); |
417 } | 403 } |
418 if (config::icaseeq(*value, "ipv6")) | 404 else |
419 { | 405 { |
420 return DHCPVal{.v4 = false, .v6 = true}; | 406 return val; |
421 } | 407 } |
422 auto ret = config::parseBool(*value); 423 if (!ret.has_value()) 424 { 425 auto str = fmt::format("Unable to parse Network[DHCP] from {}: `{}`", 426 config.getFilename().native(), *value); 427 log<level::NOTICE>(str.c_str(), 428 entry("FILE=%s", config.getFilename().c_str()), 429 entry("VALUE=%s", value->c_str())); 430 return def; 431 } 432 return *ret ? DHCPVal{.v4 = true, .v6 = true} 433 : DHCPVal{.v4 = false, .v6 = false}; | 408 return decltype(fun(std::string_view{}))(std::nullopt); |
434} 435 | 409} 410 |
411bool getIPv6AcceptRA(const config::Parser& config) 412{ 413#ifdef ENABLE_IPV6_ACCEPT_RA 414 constexpr bool def = true; 415#else 416 constexpr bool def = false; 417#endif 418 return systemdParseLast(config, "Network", "IPv6AcceptRA", 419 config::parseBool) 420 .value_or(def); 421} 422 423DHCPVal getDHCPValue(const config::Parser& config) 424{ 425 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) 426 .value_or(DHCPVal{.v4 = true, .v6 = true}); 427} 428 429bool getDHCPProp(const config::Parser& config, std::string_view key) 430{ 431 return systemdParseLast(config, "DHCP", key, config::parseBool) 432 .value_or(true); 433} 434 |
|
436namespace mac_address 437{ 438 439constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; 440constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; 441constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; 442constexpr auto propIntf = "org.freedesktop.DBus.Properties"; 443constexpr auto methodGet = "Get"; --- 157 unchanged lines hidden --- | 435namespace mac_address 436{ 437 438constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; 439constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; 440constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; 441constexpr auto propIntf = "org.freedesktop.DBus.Properties"; 442constexpr auto methodGet = "Get"; --- 157 unchanged lines hidden --- |