Ejemplo n.º 1
0
// Returns a Try of the IP for the provided hostname or an error if no IP is
// obtained.
inline Try<IP> getIP(const std::string& hostname, int family)
{
  struct addrinfo hints = createAddrInfo(SOCK_STREAM, family, 0);
  struct addrinfo* result = NULL;

  int error = getaddrinfo(hostname.c_str(), NULL, &hints, &result);

  if (error != 0) {
    return Error(gai_strerror(error));
  }

  if (result->ai_addr == NULL) {
    freeaddrinfo(result);
    return Error("No addresses found");
  }

  Try<IP> ip = IP::create(*result->ai_addr);

  if (ip.isError()) {
    freeaddrinfo(result);
    return Error("Unsupported family type");
  }

  freeaddrinfo(result);
  return ip.get();
}
Ejemplo n.º 2
0
inline Try<std::string> hostname()
{
  char host[512];

  if (gethostname(host, sizeof(host)) < 0) {
    return ErrnoError();
  }

  // TODO(evelinad): Add AF_UNSPEC when we will support IPv6
  struct addrinfo hints = createAddrInfo(SOCK_STREAM, AF_INET, AI_CANONNAME);
  struct addrinfo *result;

  int error = getaddrinfo(host, NULL, &hints, &result);

  if (error != 0 || result == NULL) {
    if (result != NULL) {
      freeaddrinfo(result);
    }
    return Error(gai_strerror(error));
  }

  std::string hostname = result->ai_canonname;
  freeaddrinfo(result);

  return hostname;
}
Ejemplo n.º 3
0
/*--------------------------------------------------------------------------------------
 * Purpose: Start to listen on the configured addr+port.
 * Input:  none
 * Output: socket ID of the listener or -1 if listener create fails
 * Kevin Burnett @ November 18, 2008
 * -------------------------------------------------------------------------------------*/
int startLoginControlListener() {	 
	// socket to listen for incoming connections
	int listenSocket = 0;

	// create addrinfo struct for the listener
	struct addrinfo *res = createAddrInfo(LoginSettings.listenAddr, LoginSettings.listenPort);
	if( res == NULL ) 
	{
		log_err( "login control thread createAddrInfo error!" );
		freeaddrinfo(res);
		listenSocket = -1;
		return listenSocket;
	}

	// open the listen socket
	listenSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if ( listenSocket == -1 )
	{
		log_err( "fail to create login listener socket %s", strerror(errno) );
		freeaddrinfo(res);
		return listenSocket;
	}	

#ifdef DEBUG
	// allow rapid reuse of socket if in debug mode
	int yes=1;
	if(setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR,&yes, sizeof(int)) < 0)
		log_err("setsockopt error");
#endif

	// bind to configured address and port
	if (bind(listenSocket, res->ai_addr, res->ai_addrlen) < 0)
	{
		log_err( "login listener unable to bind %s", strerror(errno) );
		close(listenSocket);
		freeaddrinfo(res);
		listenSocket = -1;
		return listenSocket;
	}	

	//start listening
	if (listen(listenSocket, 0) < 0) {
		log_err( "login listener unable to listen" );
		freeaddrinfo(res);
		close(listenSocket);
		listenSocket = -1;
		return listenSocket;
	}
	freeaddrinfo(res);

	return(listenSocket);	
}
Ejemplo n.º 4
0
// Returns a Try of the IP for the provided hostname or an error if no IP is
// obtained.
inline Try<uint32_t> getIP(const std::string& hostname, sa_family_t family)
{
  struct addrinfo hints, *result;
  hints = createAddrInfo(SOCK_STREAM, family, 0);

  int error = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
  if (error != 0 || result == NULL) {
    if (result != NULL ) {
      freeaddrinfo(result);
    }
    return Error(gai_strerror(error));
  }
  if (result->ai_addr == NULL) {
    freeaddrinfo(result);
    return Error("Got no addresses for '" + hostname + "'");
  }

  uint32_t ip = ((struct sockaddr_in*)(result->ai_addr))->sin_addr.s_addr;
  freeaddrinfo(result);

  return ip;
}