24 #if defined(YARP_HAS_ACE)
25 # include <ace/INET_Addr.h>
31 # include <arpa/inet.h>
32 # include <sys/socket.h>
33 # include <sys/types.h>
44 #if !defined(YARP_HAS_ACE)
50 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 class Contact::Private
55 Private(std::string regName,
59 regName(std::move(regName)),
60 carrier(std::move(carrier)),
61 hostname(std::move(hostname)),
76 #endif // DOXYGEN_SHOULD_SKIP_THIS
79 Contact::Contact(
const std::string& hostname,
81 mPriv(new Private(std::string(), std::string(), hostname, port))
86 const std::string& hostname,
88 mPriv(new Private(std::string(), carrier, hostname, port))
93 const std::string& carrier,
94 const std::string& hostname,
96 mPriv(new Private(name, carrier, hostname, port))
101 mPriv(new Private(*(rhs.mPriv)))
119 *mPriv = *(rhs.mPriv);
127 std::swap(mPriv, rhs.mPriv);
135 result.mPriv->port = config.
check(
"port_number",
Value(-1)).asInt32();
136 result.mPriv->hostname = config.
check(
"ip",
Value(
"")).asString();
137 result.mPriv->regName = config.
check(
"name",
Value(
"")).asString();
138 result.mPriv->carrier = config.
check(
"carrier",
Value(
"tcp")).asString();
144 std::string str(txt);
146 std::string::size_type start = 0;
147 std::string::size_type base = str.find(
"://");
148 std::string::size_type offset = 2;
149 if (base == std::string::npos) {
150 base = str.find(
":/");
153 if (base == std::string::npos) {
154 if (str.length() > 0 && str[0] ==
'/') {
159 if (base != std::string::npos) {
160 c.mPriv->carrier = str.substr(0, base);
161 start = base + offset;
163 std::string::size_type colon = std::string::npos;
166 std::string::size_type i;
167 for (i = start + 1; i < str.length(); i++) {
182 if (ch >=
'0' && ch <=
'9') {
190 if (mode == 1 && nums >= 1) {
192 if (c.mPriv->carrier.empty()) {
193 c.mPriv->carrier =
"tcp";
195 c.mPriv->hostname = str.substr(start + 1, colon - start - 1);
196 c.mPriv->port = atoi(str.substr(colon + 1, nums).c_str());
200 std::string rname = str.substr(start);
202 c.mPriv->regName = rname;
210 if (!mPriv->regName.empty()) {
211 return mPriv->regName;
213 if (!mPriv->hostname.empty() && mPriv->port >= 0) {
214 std::string name = std::string(
"/") + mPriv->hostname +
":" +
NetType::toString(mPriv->port);
222 return mPriv->regName;
227 mPriv->regName = name;
233 return mPriv->hostname;
238 this->mPriv->hostname = hostname;
255 return mPriv->carrier;
260 mPriv->carrier = carrier;
266 return mPriv->nestedContact;
271 this->mPriv->nestedContact = nestedContact;
277 return mPriv->timeout >= 0;
282 return mPriv->timeout;
287 this->mPriv->timeout = timeout;
292 const std::string& hostname,
295 mPriv->carrier = carrier;
296 mPriv->hostname = hostname;
303 return mPriv->port >= 0;
309 if (!mPriv->carrier.empty()) {
310 return mPriv->carrier +
":/" + name;
319 if (includeCarrier && !mPriv->carrier.empty()) {
320 result += mPriv->carrier;
323 if (!mPriv->hostname.empty() && mPriv->port >= 0) {
325 result += mPriv->hostname;
336 #if defined(YARP_HAS_ACE)
337 ACE_INET_Addr addr((u_short)0, name);
339 addr.get_host_addr(ipstr,
sizeof(ipstr));
342 char ipstr[INET6_ADDRSTRLEN];
344 struct addrinfo hints, *res, *p;
346 memset(&hints, 0,
sizeof hints);
347 hints.ai_family = AF_UNSPEC;
348 hints.ai_socktype = SOCK_STREAM;
349 hints.ai_flags = AI_PASSIVE;
351 if ((status = yarp::os::impl::getaddrinfo(name,
"http", &hints, &res)) != 0) {
352 yCError(CONTACT,
"getaddrinfo error: %s\n", yarp::os::impl::gai_strerror(status));
356 for (p = res; p !=
nullptr; p = p->ai_next) {
359 if (p->ai_family == AF_INET) {
360 auto* ipv4 =
reinterpret_cast<struct sockaddr_in*
>(p->ai_addr);
361 addr = &(ipv4->sin_addr);
363 auto* ipv6 =
reinterpret_cast<struct sockaddr_in6*
>(p->ai_addr);
364 addr = &(ipv6->sin6_addr);
368 inet_ntop(p->ai_family, addr, ipstr,
sizeof ipstr);
370 yarp::os::impl::freeaddrinfo(res);
373 if (NameConfig::isLocalName(ipstr)) {
374 return NameConfig::getHostName();