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
54bool 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]));
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) {
131 if (space !=
"/root") {
144 FILE* fin = fopen(fileName.c_str(),
"r");
145 if (fin ==
nullptr) {
149 while (fgets(buf,
sizeof(buf) - 1, fin) !=
nullptr) {
161 if (!fname.empty()) {
164 return fromString(txt);
174 if (!fname.empty()) {
177 std::string m = (!mode.empty()) ? mode :
"yarp";
195 yCError(NAMECONFIG,
"Unable to create dir for file %s, check your permissions",fileName.c_str());
198 FILE* fout = fopen(fileName.c_str(),
"w");
199 if (fout ==
nullptr) {
200 yCError(NAMECONFIG,
"Unable to write file %s, check your permissions",fileName.c_str());
203 fprintf(fout,
"%s", text.c_str());
214 std::string result =
"127.0.0.1";
215 bool loopback =
true;
222 ACE_INET_Addr* ips =
nullptr;
224 char hostAddress[256];
225 if (ACE::get_ip_interfaces(count, ips) >= 0) {
226 for (
size_t i = 0; i < count; i++) {
227 std::string ip = ips[i].get_host_addr(hostAddress, 256);
231 char hostname[NI_MAXHOST];
233 struct ifaddrs *ifaddr;
235 if (yarp::os::impl::getifaddrs(&ifaddr) == -1) {
236 yCError(NAMECONFIG,
"getifaddrs in getIps: %d, %s", errno, strerror(errno));
237 std::exit(EXIT_FAILURE);
239 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
240 if (ifa->ifa_addr ==
nullptr) {
243 family = ifa->ifa_addr->sa_family;
244 if (family == AF_INET || family == AF_INET6) {
245 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
246 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
253 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
254 std::exit(EXIT_FAILURE);
256 ip = std::string(hostname);
259 yCDebug(NAMECONFIG,
"scanning network interface %s", ip.c_str());
261 if (ip.find(
':') != std::string::npos) {
265#if defined YARP_HAS_ACE
266 bool would_be_loopback = ips[i].is_loopback();
268 bool would_be_loopback = (ip ==
"127.0.0.1" || ip ==
"127.1.0.1" || ip ==
"127.0.1.1");
274 loopback = would_be_loopback;
282 if (would_be_loopback != prefer_loopback) {
289 if (prefer_loopback != loopback) {
291 loopback = would_be_loopback;
298 loopback = would_be_loopback;
303 if (ip.length() < result.length() && result != seed) {
305 loopback = would_be_loopback;
324#if defined(YARP_HAS_ACE)
325 ACE_INET_Addr* ips =
nullptr;
327 if (ACE::get_ip_interfaces(count, ips) >= 0) {
328 for (
size_t i = 0; i < count; i++) {
329 std::string ip = ips[i].get_host_addr();
337#elif defined(__unix__)
342 char hostname[HOST_NAME_MAX];
344 if (strcmp(hostname, name.c_str()) == 0) {
349 for (
size_t i = 0; i < lst.
size(); i++) {
359 if (name ==
"localhost" || name ==
"127.0.0.1") {
370#if defined(YARP_HAS_ACE)
371 ACE_INET_Addr* ips =
nullptr;
373 if (ACE::get_ip_interfaces(count, ips) >= 0) {
374 for (
size_t i = 0; i < count; i++) {
375 std::string ip = ips[i].get_host_addr();
383 char host[NI_MAXHOST];
384 struct ifaddrs *ifaddr;
386 if (getifaddrs(&ifaddr) == -1) {
387 yCError(NAMECONFIG,
"getifaddrs in getIpsAsBottle: %d, %s", errno, strerror(errno));
388 std::exit(EXIT_FAILURE);
390 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
391 if (ifa->ifa_addr ==
nullptr) {
394 family = ifa->ifa_addr->sa_family;
395 if (family == AF_INET || family == AF_INET6) {
396 s = yarp::os::impl::getnameinfo(ifa->ifa_addr,
397 (family == AF_INET) ?
sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6),
404 yCError(NAMECONFIG,
"getnameinfo() failed: %s", yarp::os::impl::gai_strerror(s));
405 std::exit(EXIT_FAILURE);
421 for (
size_t i = 0; i < bot.
size(); i++) {
434 this->address = address;
445 if (space.empty() || refresh) {
457 if (spaces.
size() == 0) {
#define YARP_CONFIG_NAMESPACE_FILENAME
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
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.