static void lookupAddrForHostname()

in gloo/transport/tcp/device.cc [88:140]


static void lookupAddrForHostname(struct attr& attr) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = attr.ai_family;
  hints.ai_socktype = SOCK_STREAM;
  struct addrinfo* result;
  int bind_rv = 0;
  int bind_errno = 0;
  std::string bind_addr;
  auto rv = getaddrinfo(attr.hostname.data(), nullptr, &hints, &result);
  GLOO_ENFORCE_NE(rv, -1);
  struct addrinfo* rp;
  for (rp = result; rp != nullptr; rp = rp->ai_next) {
    auto fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (fd == -1) {
      continue;
    }

    bind_rv = bind(fd, rp->ai_addr, rp->ai_addrlen);
    if (bind_rv == -1) {
      bind_errno = errno;
      bind_addr = Address(rp->ai_addr, rp->ai_addrlen).str();
      close(fd);
      continue;
    }

    attr.ai_family = rp->ai_family;
    attr.ai_socktype = rp->ai_socktype;
    attr.ai_protocol = rp->ai_protocol;
    memcpy(&attr.ai_addr, rp->ai_addr, rp->ai_addrlen);
    attr.ai_addrlen = rp->ai_addrlen;
    close(fd);
    break;
  }

  // If the final call to bind(2) failed, raise error saying so.
  GLOO_ENFORCE(
    bind_rv == 0,
    "Unable to find address for ",
    attr.hostname,
    "; bind(2) for ",
    bind_addr,
    " failed with: ",
    strerror(bind_errno));

  // Verify that we were able to find an address in the first place.
  GLOO_ENFORCE(
    rp != nullptr,
    "Unable to find address for: ",
    attr.hostname);
  freeaddrinfo(result);
  return;
}