31 #if defined(YARP_HAS_ACE)
32 # include <ace/INET_Addr.h>
33 # include <ace/Sock_Connect.h>
38 #elif defined(__unix__)
39 # include <arpa/inet.h>
41 # include <sys/socket.h>
48 #define CONF_FILENAME YARP_CONFIG_FILENAME
54 bool NameConfig::fromString(
const std::string& txt)
63 if (ss[0].c_str()[0] ==
'[') {
70 yCError(NAMECONFIG,
"Cannot find yarp group in config file");
75 mode = b.
check(
"mode",
Value(
"yarp")).asString();
76 return (address.getPort() != 0);
80 address =
Contact(ss[0], yarp::conf::numeric::from_string<int>(ss[1]));
87 address.setCarrier(
"xmlrpc");
105 yCDebug(NAMECONFIG,
"Configuration file: %s", conf.c_str());
111 std::string result = txt;
112 for (
char& i : result) {
114 if (!((ch >=
'0' && ch <=
'9') || (ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z'))) {
123 std::string fname = (stem !=
nullptr) ? stem :
CONF_FILENAME;
124 if (stem ==
nullptr) {
129 space = getNamespace();
131 if (space !=
"/root") {
133 std::string base = getSafeString(space);
138 return expandFilename(fname.c_str());
144 FILE* fin = fopen(fileName.c_str(),
"r");
145 if (fin ==
nullptr) {
149 while (fgets(buf,
sizeof(buf) - 1, fin) !=
nullptr) {
160 std::string fname = getConfigFileName(
nullptr, ns);
161 if (!fname.empty()) {
162 std::string txt = readConfig(fname);
164 return fromString(txt);
173 std::string fname = getConfigFileName();
174 if (!fname.empty()) {
177 std::string m = (!mode.empty()) ? mode :
"yarp";
180 return writeConfig(fname, txt);
197 FILE* fout = fopen(fileName.c_str(),
"w");
198 if (fout ==
nullptr) {
201 fprintf(fout,
"%s", text.c_str());
212 std::string result =
"127.0.0.1";
213 bool loopback =
true;
220 ACE_INET_Addr* ips =
nullptr;
222 char hostAddress[256];
223 if (ACE::get_ip_interfaces(count, ips) >= 0) {
224 for (
size_t i = 0; i < count; i++) {
225 std::string ip = ips[i].get_host_addr(hostAddress, 256);
229 char hostname[NI_MAXHOST];
231 struct ifaddrs *ifaddr;
233 if (yarp::os::impl::getifaddrs(&ifaddr) == -1) {
234 yCError(NAMECONFIG,
"getifaddrs in getIps: %d, %s", errno, strerror(errno));
235 std::exit(EXIT_FAILURE);
237 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
238 if (ifa->ifa_addr ==
nullptr) {
241 family = ifa->ifa_addr->sa_family;
242 if (family == AF_INET || family == AF_INET6) {
243 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
244 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
251 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
252 std::exit(EXIT_FAILURE);
254 ip = std::string(hostname);
257 yCDebug(NAMECONFIG,
"scanning network interface %s", ip.c_str());
259 if (ip.find(
':') != std::string::npos) {
263 #if defined YARP_HAS_ACE
264 bool would_be_loopback = ips[i].is_loopback();
266 bool would_be_loopback = (ip ==
"127.0.0.1" || ip ==
"127.1.0.1" || ip ==
"127.0.1.1");
272 loopback = would_be_loopback;
280 if (would_be_loopback != prefer_loopback) {
287 if (prefer_loopback != loopback) {
289 loopback = would_be_loopback;
296 loopback = would_be_loopback;
301 if (ip.length() < result.length() && result != seed) {
303 loopback = would_be_loopback;
322 #if defined(YARP_HAS_ACE)
323 ACE_INET_Addr* ips =
nullptr;
325 if (ACE::get_ip_interfaces(count, ips) >= 0) {
326 for (
size_t i = 0; i < count; i++) {
327 std::string ip = ips[i].get_host_addr();
335 #elif defined(__unix__)
340 char hostname[HOST_NAME_MAX];
342 if (strcmp(hostname, name.c_str()) == 0) {
346 Bottle lst = getIpsAsBottle();
347 for (
size_t i = 0; i < lst.
size(); i++) {
357 if (name ==
"localhost" || name ==
"127.0.0.1") {
368 #if defined(YARP_HAS_ACE)
369 ACE_INET_Addr* ips =
nullptr;
371 if (ACE::get_ip_interfaces(count, ips) >= 0) {
372 for (
size_t i = 0; i < count; i++) {
373 std::string ip = ips[i].get_host_addr();
381 char host[NI_MAXHOST];
382 struct ifaddrs *ifaddr;
384 if (getifaddrs(&ifaddr) == -1) {
385 yCError(NAMECONFIG,
"getifaddrs in getIpsAsBottle: %d, %s", errno, strerror(errno));
386 std::exit(EXIT_FAILURE);
388 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
389 if (ifa->ifa_addr ==
nullptr) {
392 family = ifa->ifa_addr->sa_family;
393 if (family == AF_INET || family == AF_INET6) {
394 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
395 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
402 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
403 std::exit(EXIT_FAILURE);
419 for (
size_t i = 0; i < bot.
size(); i++) {
432 this->address = address;
443 if (space.empty() || refresh) {
446 spaces.fromString(senv);
449 spaces.fromString(readConfig(fname));
451 space = spaces.get(0).asString();
455 if (spaces.size() == 0) {
456 spaces.addString(
"/root");
464 getNamespace(refresh);
#define YARP_CONFIG_NAMESPACE_FILENAME
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool isNull() const override
Checks if the object is invalid.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
A class for storing options and configuration information.
void fromConfig(const char *txt, bool wipe=true)
Parses text in the configuration format described in fromConfigFile().
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
A single value (typically within a Bottle).
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
std::string getSafeString(const std::string &txt)
std::string getNamespace(bool refresh=false)
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
std::string readConfig(const std::string &fileName)
static bool isLocalName(const std::string &name)
static std::string expandFilename(const char *fname)
bool fromFile(const char *ns=nullptr)
void setNamespace(const std::string &ns)
bool writeConfig(const std::string &fileName, const std::string &text)
yarp::os::Bottle getNamespaces(bool refresh=false)
static std::string getIps()
std::string getConfigFileName(const char *stem=nullptr, const char *ns=nullptr)
static yarp::os::Bottle getIpsAsBottle()
void setAddress(const Contact &address)
bool toFile(bool clean=false)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string yarpconfighome()
Returns the directory where user-specific YARP configuration files should be written.
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
static constexpr value_type preferred_separator
std::string to_string(IntegerType x)
ContainerT split(const typename ContainerT::value_type &s, std::basic_regex< typename ContainerT::value_type::value_type > regex)
Utility to split a string by a separator, into a vector of strings.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.