diff --git a/daemon/config.cpp b/daemon/config.cpp index 586b368..9889958 100644 --- a/daemon/config.cpp +++ b/daemon/config.cpp @@ -86,21 +86,23 @@ std::shared_ptr Config::parse(const std::string& filename) { config.mac_addr_ = mac_addr; 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_); if (interface_idx < 0) { std::cerr << "Cannot retrieve index for interface " << 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.need_restart_ = false; diff --git a/daemon/interface.cpp b/daemon/interface.cpp index 119af68..998ac9f 100644 --- a/daemon/interface.cpp +++ b/daemon/interface.cpp @@ -30,7 +30,7 @@ std::pair get_interface_ip( const std::string& interface_name) { int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { - BOOST_LOG_TRIVIAL(error) + BOOST_LOG_TRIVIAL(warning) << "Cannot retrieve IP address for interface " << interface_name; return {0, ""}; } @@ -39,7 +39,7 @@ std::pair get_interface_ip( strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { close(fd); - BOOST_LOG_TRIVIAL(error) + BOOST_LOG_TRIVIAL(warning) << "Cannot retrieve IP address for interface " << interface_name; return {0, ""}; } @@ -91,7 +91,7 @@ std::pair, std::string> get_interface_mac( int get_interface_index(const std::string& interface_name) { int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { - BOOST_LOG_TRIVIAL(error) + BOOST_LOG_TRIVIAL(warning) << "Cannot retrieve index for interface " << interface_name; 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); if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { close(fd); - BOOST_LOG_TRIVIAL(error) + BOOST_LOG_TRIVIAL(warning) << "Cannot retrieve index for interface " << interface_name; return -1; } diff --git a/daemon/main.cpp b/daemon/main.cpp index 1693d64..56d0f2a 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -52,9 +52,8 @@ int main(int argc, char* argv[]) { po::options_description desc("Options"); desc.add_options()( "config,c", po::value()->default_value("/etc/daemon.conf"), - "daemon configuration file")("interface_name,i", po::value(), - "Network interface name")( - "http_port,p", po::value(), "HTTP server port")( + "daemon configuration file")("http_port,p", po::value(), + "HTTP server port")( "help,h", "Print this help message"); int unix_style = postyle::unix_style | postyle::short_allow_next; @@ -94,15 +93,18 @@ int main(int argc, char* argv[]) { return EXIT_FAILURE; } /* override configuration according to command line args */ - if (vm.count("interface_name")) { - config->set_interface_name(vm["interface_name"].as()); - } if (vm.count("http_port")) { config->set_http_port(vm["http_port"].as()); } /* init logging */ 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"; try { auto driver = DriverManager::create(); @@ -147,11 +149,9 @@ int main(int argc, char* argv[]) { BOOST_LOG_TRIVIAL(debug) << "main:: init done, entering loop..."; while (!is_terminated()) { 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) << "main:: IP address changed, restarting ..."; - config->set_ip_addr_str(ip_str); - config->set_ip_addr(ip_addr); break; } diff --git a/daemon/sap.cpp b/daemon/sap.cpp index 579d81e..51d9ccb 100644 --- a/daemon/sap.cpp +++ b/daemon/sap.cpp @@ -149,8 +149,8 @@ bool SAP::send(bool is_announce, try { socket_.send_to(boost::asio::buffer(buffer, sap_header_len + sdp.length()), remote_endpoint_); - } catch (boost::system::error_code& ec) { - BOOST_LOG_TRIVIAL(error) << "sap::send_to " << ec.message(); + } catch (...) { + BOOST_LOG_TRIVIAL(error) << "sap::send_to failed"; return false; } return true;