34 #if defined(YARP_HAS_ACE)
35 # include <ace/INET_Addr.h>
36 # include <ace/Sock_Connect.h>
41 #elif defined(__unix__)
42 # include <arpa/inet.h>
44 # include <sys/socket.h>
51 #define CONF_FILENAME YARP_CONFIG_FILENAME
57 bool NameConfig::fromString(
const std::string& txt)
62 if (ss.get(0)[0] ==
'[') {
69 yCError(NAMECONFIG,
"Cannot find yarp group in config file");
74 mode = b.
check(
"mode",
Value(
"yarp")).asString();
75 return (address.getPort() != 0);
87 address.setCarrier(
"xmlrpc");
104 yCDebug(NAMECONFIG,
"Configuration file: %s", conf.c_str());
110 std::string result = txt;
111 for (
char& i : result) {
113 if (!((ch >=
'0' && ch <=
'9') || (ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z'))) {
122 std::string fname = (stem !=
nullptr) ? stem :
CONF_FILENAME;
123 if (stem ==
nullptr) {
128 space = getNamespace();
130 if (space !=
"/root") {
132 std::string base = getSafeString(space);
137 return expandFilename(fname.c_str());
143 FILE* fin = fopen(fileName.c_str(),
"r");
144 if (fin ==
nullptr) {
148 while (fgets(buf,
sizeof(buf) - 1, fin) !=
nullptr) {
159 std::string fname = getConfigFileName(
nullptr, ns);
160 if (!fname.empty()) {
161 std::string txt = readConfig(fname);
163 return fromString(txt);
172 std::string fname = getConfigFileName();
173 if (!fname.empty()) {
176 std::string m = (!mode.empty()) ? mode :
"yarp";
177 txt += address.getHost() +
" " +
NetType::toString(address.getPort()) +
" " + m +
"\n";
179 return writeConfig(fname, txt);
196 FILE* fout = fopen(fileName.c_str(),
"w");
197 if (fout ==
nullptr) {
200 fprintf(fout,
"%s", text.c_str());
211 std::string result =
"127.0.0.1";
212 bool loopback =
true;
219 ACE_INET_Addr* ips =
nullptr;
221 char hostAddress[256];
222 if (ACE::get_ip_interfaces(count, ips) >= 0) {
223 for (
size_t i = 0; i < count; i++) {
224 std::string ip = ips[i].get_host_addr(hostAddress, 256);
228 char hostname[NI_MAXHOST];
230 struct ifaddrs *ifaddr;
232 if (yarp::os::impl::getifaddrs(&ifaddr) == -1) {
233 yCError(NAMECONFIG,
"getifaddrs in getIps: %d, %s", errno, strerror(errno));
234 std::exit(EXIT_FAILURE);
236 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
237 if (ifa->ifa_addr ==
nullptr) {
240 family = ifa->ifa_addr->sa_family;
241 if (family == AF_INET || family == AF_INET6) {
242 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
243 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
250 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
251 std::exit(EXIT_FAILURE);
253 ip = std::string(hostname);
256 yCDebug(NAMECONFIG,
"scanning network interface %s", ip.c_str());
258 if (ip.find(
':') != std::string::npos) {
262 #if defined YARP_HAS_ACE
263 bool would_be_loopback = ips[i].is_loopback();
265 bool would_be_loopback = (ip ==
"127.0.0.1" || ip ==
"127.1.0.1" || ip ==
"127.0.1.1");
271 loopback = would_be_loopback;
279 if (would_be_loopback != prefer_loopback) {
286 if (prefer_loopback != loopback) {
288 loopback = would_be_loopback;
295 loopback = would_be_loopback;
300 if (ip.length() < result.length() && result != seed) {
302 loopback = would_be_loopback;
321 #if defined(YARP_HAS_ACE)
322 ACE_INET_Addr* ips =
nullptr;
324 if (ACE::get_ip_interfaces(count, ips) >= 0) {
325 for (
size_t i = 0; i < count; i++) {
326 std::string ip = ips[i].get_host_addr();
334 #elif defined(__unix__)
339 char hostname[HOST_NAME_MAX];
341 if (strcmp(hostname, name.c_str()) == 0) {
345 Bottle lst = getIpsAsBottle();
346 for (
size_t i = 0; i < lst.
size(); i++) {
356 if (name ==
"localhost" || name ==
"127.0.0.1") {
367 #if defined(YARP_HAS_ACE)
368 ACE_INET_Addr* ips =
nullptr;
370 if (ACE::get_ip_interfaces(count, ips) >= 0) {
371 for (
size_t i = 0; i < count; i++) {
372 std::string ip = ips[i].get_host_addr();
380 char host[NI_MAXHOST];
381 struct ifaddrs *ifaddr;
383 if (getifaddrs(&ifaddr) == -1) {
384 yCError(NAMECONFIG,
"getifaddrs in getIpsAsBottle: %d, %s", errno, strerror(errno));
385 std::exit(EXIT_FAILURE);
387 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
388 if (ifa->ifa_addr ==
nullptr) {
391 family = ifa->ifa_addr->sa_family;
392 if (family == AF_INET || family == AF_INET6) {
393 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
394 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
401 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
402 std::exit(EXIT_FAILURE);
418 for (
size_t i = 0; i < bot.
size(); i++) {
431 this->address = address;
442 if (space.empty() || refresh) {
445 spaces.fromString(senv);
448 spaces.fromString(readConfig(fname));
450 space = spaces.get(0).asString();
454 if (spaces.size() == 0) {
455 spaces.addString(
"/root");
463 getNamespace(refresh);