Skip to content

Commit e0c2137

Browse files
authored
Merge pull request #218 from bondagit/issue-216
Issue 216
2 parents dc80697 + ddc7a0d commit e0c2137

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

daemon/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Example
186186

187187
{
188188
"interface_name": "lo",
189+
"http_addr": "127.0.0.1",
189190
"http_port": 8080,
190191
"rtsp_port": 8854,
191192
"log_severity": 2,
@@ -285,7 +286,12 @@ where:
285286
286287
> **ip\_addr**
287288
> JSON string specifying the IP address of the specified network device.
288-
> **_NOTE:_** This parameter is read-only and cannot be set. The server will determine the IP address of the network device at startup time and will monitor it periodically.
289+
> This parameter can be set to specify the prefferred IP address to use.
290+
> In case such address is not valid, the server will determine the IP address of the network device at startup time and will monitor it periodically.
291+
292+
> **http\_addr**
293+
> JSON string specifying the alternate IP address used for the daemon HTTP interface.
294+
> If this address is specified the HTTP interface will bind to this IP instead of the one specified by the *ip_addr* parameter.
289295
290296
> **mdns\_enabled**
291297
> JSON boolean specifying whether the mDNS discovery is enabled or disabled.

daemon/config.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ std::shared_ptr<Config> Config::parse(const std::string& filename,
115115
config.interface_idx_ = interface_idx;
116116
}
117117

118-
auto [ip_addr, ip_str] = get_interface_ip(config.interface_name_);
118+
auto [ip_addr, ip_str, is_new] =
119+
get_new_interface_ip(config.interface_name_, config.ip_str_);
119120
if (ip_str.empty()) {
120121
std::cerr << "Cannot retrieve IPv4 address for interface "
121122
<< config.interface_name_ << std::endl;

daemon/interface.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// interface.cpp
33
//
4-
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
4+
// Copyright (c) 2019 2025 Andrea Bondavalli. All rights reserved.
55
//
66
// This program is free software: you can redistribute it and/or modify
77
// it under the terms of the GNU General Public License as published by
@@ -25,11 +25,33 @@
2525
#include <fstream>
2626
#include <utility>
2727

28+
#include <ifaddrs.h>
2829
#include "log.hpp"
2930

3031
using namespace boost::asio;
3132
using namespace boost::asio::ip;
3233

34+
bool is_interface_ip(const std::string& interface_name,
35+
const std::string& addr) {
36+
struct ifaddrs *ifap, *ifa;
37+
struct sockaddr_in* sa;
38+
char* saddr;
39+
bool found(false);
40+
41+
getifaddrs(&ifap);
42+
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
43+
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
44+
sa = (struct sockaddr_in*)ifa->ifa_addr;
45+
saddr = inet_ntoa(sa->sin_addr);
46+
if (interface_name == ifa->ifa_name && addr == saddr)
47+
found = true;
48+
}
49+
}
50+
51+
freeifaddrs(ifap);
52+
return found;
53+
}
54+
3355
std::pair<uint32_t, std::string> get_interface_ip(
3456
const std::string& interface_name) {
3557
int fd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -59,6 +81,22 @@ std::pair<uint32_t, std::string> get_interface_ip(
5981
return {addr, str_addr};
6082
}
6183

84+
std::tuple<uint32_t, std::string, bool> get_new_interface_ip(
85+
const std::string& interface_name,
86+
const std::string& curr_addr) {
87+
if (is_interface_ip(interface_name, curr_addr))
88+
{
89+
uint32_t ip_addr;
90+
inet_pton(AF_INET, curr_addr.c_str(), &ip_addr);
91+
return { ntohl(ip_addr), curr_addr, false };
92+
}
93+
94+
auto [ip_addr, ip_str] = get_interface_ip(interface_name);
95+
BOOST_LOG_TRIVIAL(info) << "interface " << interface_name
96+
<< " new IP address " << ip_str;
97+
return { ip_addr, ip_str, true };
98+
}
99+
62100
std::pair<std::array<uint8_t, 6>, std::string> get_interface_mac(
63101
const std::string& interface_name) {
64102
std::array<uint8_t, 6> mac{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

daemon/interface.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#ifndef _INTERFACE_HPP_
2121
#define _INTERFACE_HPP_
2222

23+
std::tuple<uint32_t, std::string, bool> get_new_interface_ip(
24+
const std::string& interface_name,
25+
const std::string& curr_addr);
26+
bool is_interface_ip(const std::string& interface_name,
27+
const std::string& addr);
2328
std::pair<uint32_t, std::string> get_interface_ip(
2429
const std::string& interface_name);
2530
std::pair<std::array<uint8_t, 6>, std::string> get_interface_mac(

daemon/json.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ Config json_to_config_(std::istream& js, Config& config) {
379379
remove_undesired_chars(val.get_value<std::string>()));
380380
} else if (key == "auto_sinks_update") {
381381
config.set_auto_sinks_update(val.get_value<bool>());
382-
} else if (key == "mac_addr" || key == "ip_addr" || key == "node_id") {
382+
} else if (key == "ip_addr") {
383+
config.set_ip_addr_str(val.get_value<std::string>());
384+
} else if (key == "mac_addr" || key == "node_id") {
383385
/* ignored */
384386
} else {
385387
std::cerr << "Warning: unkown configuration option " << key

daemon/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace po = boost::program_options;
4343
namespace postyle = boost::program_options::command_line_style;
4444
namespace logging = boost::log;
4545

46-
static const std::string version("bondagit-2.0.5");
46+
static const std::string version("bondagit-2.1.0");
4747
static std::atomic<bool> terminate = false;
4848

4949
void termination_handler(int signum) {
@@ -220,8 +220,9 @@ int main(int argc, char* argv[]) {
220220
sd_notify(0, "WATCHDOG=1");
221221
#endif
222222

223-
auto [ip_addr, ip_str] = get_interface_ip(config->get_interface_name());
224-
if (config->get_ip_addr_str() != ip_str) {
223+
auto [ip_addr, ip_str, is_new] = get_new_interface_ip(
224+
config->get_interface_name(), config->get_ip_addr_str());
225+
if (is_new) {
225226
BOOST_LOG_TRIVIAL(warning)
226227
<< "main:: IP address changed, restarting ...";
227228
break;

0 commit comments

Comments
 (0)