21#if defined(YARP_HAS_ACE)
22# include <ace/INET_Addr.h>
28# include <arpa/inet.h>
29# include <sys/socket.h>
30# include <sys/types.h>
41#if !defined(YARP_HAS_ACE)
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
52 Private(std::string regName,
56 regName(
std::move(regName)),
57 carrier(
std::move(carrier)),
58 hostname(
std::move(hostname)),
76Contact::Contact(
const std::string& hostname,
78 mPriv(new Private(
std::string(),
std::string(), hostname, port))
83 const std::string& hostname,
85 mPriv(new Private(
std::string(), carrier, hostname, port))
90 const std::string& carrier,
91 const std::string& hostname,
93 mPriv(new Private(name, carrier, hostname, port))
98 mPriv(new Private(*(rhs.mPriv)))
116 *mPriv = *(rhs.mPriv);
124 std::swap(mPriv, rhs.mPriv);
132 result.mPriv->port = config.
check(
"port_number",
Value(-1)).asInt32();
133 result.mPriv->hostname = config.
check(
"ip",
Value(
"")).asString();
134 result.mPriv->regName = config.
check(
"name",
Value(
"")).asString();
135 result.mPriv->carrier = config.
check(
"carrier",
Value(
"tcp")).asString();
141 std::string str(txt);
143 std::string::size_type start = 0;
144 std::string::size_type base = str.find(
"://");
145 std::string::size_type offset = 2;
146 if (base == std::string::npos) {
147 base = str.find(
":/");
150 if (base == std::string::npos) {
151 if (str.length() > 0 && str[0] ==
'/') {
156 if (base != std::string::npos) {
157 c.mPriv->carrier = str.substr(0, base);
158 start = base + offset;
160 std::string::size_type colon = std::string::npos;
163 std::string::size_type i;
164 for (i = start + 1; i < str.length(); i++) {
179 if (ch >=
'0' && ch <=
'9') {
187 if (mode == 1 && nums >= 1) {
189 if (c.mPriv->carrier.empty()) {
190 c.mPriv->carrier =
"tcp";
192 c.mPriv->hostname = str.substr(start + 1, colon - start - 1);
193 c.mPriv->port = atoi(str.substr(colon + 1, nums).c_str());
197 std::string rname = str.substr(start);
199 c.mPriv->regName = rname;
207 if (!mPriv->regName.empty()) {
208 return mPriv->regName;
210 if (!mPriv->hostname.empty() && mPriv->port >= 0) {
219 return mPriv->regName;
224 mPriv->regName = name;
230 return mPriv->hostname;
235 this->mPriv->hostname = hostname;
252 return mPriv->carrier;
257 mPriv->carrier = carrier;
263 return mPriv->nestedContact;
268 this->mPriv->nestedContact = nestedContact;
274 return mPriv->timeout >= 0;
279 return mPriv->timeout;
284 this->mPriv->timeout = timeout;
289 const std::string& hostname,
292 mPriv->carrier = carrier;
293 mPriv->hostname = hostname;
300 return mPriv->port >= 0;
306 if (!mPriv->carrier.empty()) {
307 return mPriv->carrier +
":/" + name;
316 if (includeCarrier && !mPriv->carrier.empty()) {
317 result += mPriv->carrier;
320 if (!mPriv->hostname.empty() && mPriv->port >= 0) {
322 result += mPriv->hostname;
333#if defined(YARP_HAS_ACE)
334 ACE_INET_Addr addr((u_short)0, name);
336 addr.get_host_addr(ipstr,
sizeof(ipstr));
339 char ipstr[INET6_ADDRSTRLEN];
341 struct addrinfo hints, *res, *p;
343 memset(&hints, 0,
sizeof hints);
344 hints.ai_family = AF_UNSPEC;
345 hints.ai_socktype = SOCK_STREAM;
346 hints.ai_flags = AI_PASSIVE;
348 if ((status = yarp::os::impl::getaddrinfo(name,
"http", &hints, &res)) != 0) {
349 yCError(CONTACT,
"getaddrinfo error: %s\n", yarp::os::impl::gai_strerror(status));
353 for (p = res; p !=
nullptr; p = p->ai_next) {
356 if (p->ai_family == AF_INET) {
357 auto* ipv4 =
reinterpret_cast<struct sockaddr_in*
>(p->ai_addr);
358 addr = &(ipv4->sin_addr);
360 auto* ipv6 =
reinterpret_cast<struct sockaddr_in6*
>(p->ai_addr);
361 addr = &(ipv6->sin6_addr);
365 inet_ntop(p->ai_family, addr, ipstr,
sizeof ipstr);
367 yarp::os::impl::freeaddrinfo(res);
370 if (NameConfig::isLocalName(ipstr)) {
371 return NameConfig::getHostName();
Various utilities related to types and formats.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Small helper class to help deal with legacy YARP configuration files.
#define yCError(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string to_string(IntegerType x)