- modified daemon code to support startup in case the specified network interface doesn't have an IP address. In this case the daemon waits for an IP address.

- removed not functional daemon option (-i)
- fixed exception handling in SAP::send() method
This commit is contained in:
Andrea Bondavalli 2020-06-18 19:49:24 +02:00
parent 6449524ed5
commit 9062a82033
4 changed files with 27 additions and 25 deletions

View File

@ -86,21 +86,23 @@ std::shared_ptr<Config> Config::parse(const std::string& filename) {
config.mac_addr_ = mac_addr; config.mac_addr_ = mac_addr;
config.mac_str_ = mac_str; config.mac_str_ = mac_str;
auto [ip_addr, ip_str] = get_interface_ip(config.interface_name_);
if (ip_str.empty()) {
std::cerr << "Cannot retrieve IPv4 address for interface "
<< config.interface_name_ << std::endl;
return nullptr;
}
config.ip_addr_ = ip_addr;
config.ip_str_ = ip_str;
auto interface_idx = get_interface_index(config.interface_name_); auto interface_idx = get_interface_index(config.interface_name_);
if (interface_idx < 0) { if (interface_idx < 0) {
std::cerr << "Cannot retrieve index for interface " std::cerr << "Cannot retrieve index for interface "
<< config.interface_name_ << std::endl; << config.interface_name_ << std::endl;
return nullptr; } else {
config.interface_idx_ = interface_idx;
} }
config.interface_idx_ = interface_idx;
auto [ip_addr, ip_str] = get_interface_ip(config.interface_name_);
if (ip_str.empty()) {
std::cerr << "Cannot retrieve IPv4 address for interface "
<< config.interface_name_ << std::endl;
} else {
config.ip_addr_ = ip_addr;
config.ip_str_ = ip_str;
}
config.config_filename_ = filename; config.config_filename_ = filename;
config.need_restart_ = false; config.need_restart_ = false;

View File

@ -30,7 +30,7 @@ std::pair<uint32_t, std::string> get_interface_ip(
const std::string& interface_name) { const std::string& interface_name) {
int fd = socket(AF_INET, SOCK_DGRAM, 0); int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) { if (fd < 0) {
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve IP address for interface " << interface_name; << "Cannot retrieve IP address for interface " << interface_name;
return {0, ""}; return {0, ""};
} }
@ -39,7 +39,7 @@ std::pair<uint32_t, std::string> get_interface_ip(
strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
close(fd); close(fd);
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve IP address for interface " << interface_name; << "Cannot retrieve IP address for interface " << interface_name;
return {0, ""}; return {0, ""};
} }
@ -91,7 +91,7 @@ std::pair<std::array<uint8_t, 6>, std::string> get_interface_mac(
int get_interface_index(const std::string& interface_name) { int get_interface_index(const std::string& interface_name) {
int fd = socket(AF_INET, SOCK_DGRAM, 0); int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) { if (fd < 0) {
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve index for interface " << interface_name; << "Cannot retrieve index for interface " << interface_name;
return -1; return -1;
} }
@ -100,7 +100,7 @@ int get_interface_index(const std::string& interface_name) {
strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
close(fd); close(fd);
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(warning)
<< "Cannot retrieve index for interface " << interface_name; << "Cannot retrieve index for interface " << interface_name;
return -1; return -1;
} }

View File

@ -52,9 +52,8 @@ int main(int argc, char* argv[]) {
po::options_description desc("Options"); po::options_description desc("Options");
desc.add_options()( desc.add_options()(
"config,c", po::value<std::string>()->default_value("/etc/daemon.conf"), "config,c", po::value<std::string>()->default_value("/etc/daemon.conf"),
"daemon configuration file")("interface_name,i", po::value<std::string>(), "daemon configuration file")("http_port,p", po::value<int>(),
"Network interface name")( "HTTP server port")(
"http_port,p", po::value<int>(), "HTTP server port")(
"help,h", "Print this help message"); "help,h", "Print this help message");
int unix_style = postyle::unix_style | postyle::short_allow_next; int unix_style = postyle::unix_style | postyle::short_allow_next;
@ -94,15 +93,18 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* override configuration according to command line args */ /* override configuration according to command line args */
if (vm.count("interface_name")) {
config->set_interface_name(vm["interface_name"].as<std::string>());
}
if (vm.count("http_port")) { if (vm.count("http_port")) {
config->set_http_port(vm["http_port"].as<int>()); config->set_http_port(vm["http_port"].as<int>());
} }
/* init logging */ /* init logging */
log_init(*config); log_init(*config);
if (config->get_ip_addr_str().empty()) {
BOOST_LOG_TRIVIAL(info) << "main:: no IP address, waiting ...";
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
BOOST_LOG_TRIVIAL(debug) << "main:: initializing daemon"; BOOST_LOG_TRIVIAL(debug) << "main:: initializing daemon";
try { try {
auto driver = DriverManager::create(); auto driver = DriverManager::create();
@ -147,11 +149,9 @@ int main(int argc, char* argv[]) {
BOOST_LOG_TRIVIAL(debug) << "main:: init done, entering loop..."; BOOST_LOG_TRIVIAL(debug) << "main:: init done, entering loop...";
while (!is_terminated()) { while (!is_terminated()) {
auto [ip_addr, ip_str] = get_interface_ip(config->get_interface_name()); auto [ip_addr, ip_str] = get_interface_ip(config->get_interface_name());
if (!ip_str.empty() && config->get_ip_addr_str() != ip_str) { if (config->get_ip_addr_str() != ip_str) {
BOOST_LOG_TRIVIAL(warning) BOOST_LOG_TRIVIAL(warning)
<< "main:: IP address changed, restarting ..."; << "main:: IP address changed, restarting ...";
config->set_ip_addr_str(ip_str);
config->set_ip_addr(ip_addr);
break; break;
} }

View File

@ -149,8 +149,8 @@ bool SAP::send(bool is_announce,
try { try {
socket_.send_to(boost::asio::buffer(buffer, sap_header_len + sdp.length()), socket_.send_to(boost::asio::buffer(buffer, sap_header_len + sdp.length()),
remote_endpoint_); remote_endpoint_);
} catch (boost::system::error_code& ec) { } catch (...) {
BOOST_LOG_TRIVIAL(error) << "sap::send_to " << ec.message(); BOOST_LOG_TRIVIAL(error) << "sap::send_to failed";
return false; return false;
} }
return true; return true;