Question

I'm trying to understand what the getaddrinfo function returns :

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>

int main (int argc, char *argv[])
{


struct addrinfo *res = 0 ;

  getaddrinfo("localhost", NULL ,NULL,&res);
  printf("ai_flags -> %i\n", res->ai_flags) ;
  printf("ai_family -> %i\n", res->ai_family) ;
  printf("ai_socktype -> %i\n", res->ai_socktype) ;
  printf("ai_protocol -> %i\n", res->ai_protocol) ;
  printf("ai_addrlen -> %i\n", res->ai_addrlen) ;
  struct sockaddr_in* saddr = (struct sockaddr_in*)res->ai_addr;
  printf("ai_addr hostname ->  %s\n", inet_ntoa(saddr->sin_addr));

  freeaddrinfo(res);

  return 0 ;
}

results :

ai_flags -> 40
ai_family -> 2
ai_socktype -> 1
ai_protocol -> 6
ai_addrlen -> 16
ai_addr hostname ->  127.0.0.1

In /etc/hosts, I 've got :

127.0.0.1 localhost    
::1     localhost

Getaddrinfo returns only 127.0.0.1 and not ::1 ? I don't understand why ?

The second question is where can I find the meaning of those ints (40,2,1,6 etc) ? I've read the man but there is nothing about that.

I also wanted to know if it's possible to give a IPv6 adress (for example ::1) and the function returns the name : localhost ?

Thanks a lot !!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top