/ src / configuration.cpp
configuration.cpp
  1  // SPDX-FileCopyrightText: Copyright (C) 2025 Marek Küthe <m.k@mk16.de>
  2  //
  3  // SPDX-License-Identifier: GPL-3.0-or-later
  4  
  5  #include "configuration.hpp"
  6  
  7  using namespace MPingSender;
  8  
  9  // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
 10  MPingSender::Configuration::Configuration(const std::span<char *> args)
 11  {
 12      boost::program_options::options_description options{"Configuration"};
 13      // clang-format off
 14      options.add_options()
 15        ("help", "Help screen")
 16  #ifndef __clang__
 17        ("log-level", boost::program_options::value<boost::log::trivial::severity_level>(&this->_log_level)->default_value(boost::log::trivial::info), "Log level. Valid options are trace, debug, info, warning, error and fatal. Defaults to info.")
 18  #endif
 19        ("bind-address", boost::program_options::value<std::string>(&this->_bind_address)->required(), "Address to which the UDP socket is bound. Required.")
 20        ("bind-port", boost::program_options::value<uint16_t>(&this->_bind_port)->default_value(4321), "Port to which the UDP socket is bound. Defaults to 4321.")
 21        ("address", boost::program_options::value<std::string>(&this->_address)->default_value("ff2e::42"), "Address to send packets to. Defaults to ff2e::42")
 22        ("port", boost::program_options::value<uint16_t>(&this->_port)->default_value(4321), "Address to which the packages are sent.")
 23        ("ttl", boost::program_options::value<uint8_t>(&this->_ttl)->default_value(64), "TTTL or hop limit with which the packets are sent. Defaults to 64.")
 24        ("interface-name", boost::program_options::value<std::string>(&this->_interface_name)->required(), "Name of the interface from which the packets are sent. Required.")
 25      ;
 26      // clang-format on
 27  
 28      boost::program_options::command_line_parser parser{
 29          static_cast<int>(args.size()), args.data()};
 30      parser.options(options).allow_unregistered().style(
 31          boost::program_options::command_line_style::allow_long |
 32          boost::program_options::command_line_style::allow_sticky |
 33          boost::program_options::command_line_style::long_allow_next);
 34      const boost::program_options::parsed_options parsed_options = parser.run();
 35  
 36      boost::program_options::variables_map vm;
 37      store(parsed_options, vm);
 38  
 39      this->_help = vm.contains("help");
 40      if (this->_help)
 41      {
 42          std::clog << "mping-sender" << std::endl
 43                    << std::endl
 44                    << "Usage: mping-sender [configuration]" << std::endl
 45                    << std::endl
 46                    << options << std::endl;
 47          std::exit(EXIT_SUCCESS); // NOLINT(concurrency-mt-unsafe)
 48      }
 49  
 50      notify(vm);
 51  
 52  #ifdef __clang__
 53      boost::log::core::get()->set_filter(boost::log::trivial::severity >=
 54                                          boost::log::trivial::info);
 55  #else
 56      boost::log::core::get()->set_filter(boost::log::trivial::severity >=
 57                                          this->_log_level);
 58  #endif
 59  }
 60  
 61  // NOLINTEND(cppcoreguidelines-pro-type-member-init)
 62  
 63  bool MPingSender::Configuration::get_help() const noexcept
 64  {
 65      return this->_help;
 66  }
 67  
 68  boost::log::trivial::severity_level
 69      MPingSender::Configuration::get_log_level() const noexcept
 70  {
 71      return this->_log_level;
 72  }
 73  
 74  std::string MPingSender::Configuration::get_bind_address() const noexcept
 75  {
 76      return this->_bind_address;
 77  }
 78  
 79  uint16_t MPingSender::Configuration::get_bind_port() const noexcept
 80  {
 81      return this->_bind_port;
 82  }
 83  
 84  std::string MPingSender::Configuration::get_address() const noexcept
 85  {
 86      return this->_address;
 87  }
 88  
 89  uint16_t MPingSender::Configuration::get_port() const noexcept
 90  {
 91      return this->_port;
 92  }
 93  
 94  uint8_t MPingSender::Configuration::get_ttl() const noexcept
 95  {
 96      return this->_ttl;
 97  }
 98  
 99  std::string MPingSender::Configuration::get_interface_name() const noexcept
100  {
101      return this->_interface_name;
102  }