Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:38:41+00:00 2026-05-22T01:38:41+00:00

I’m trying to understand what the getaddrinfo function returns : #include <stdlib.h> #include <sys/types.h>

  • 0

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 !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:38:41+00:00Added an answer on May 22, 2026 at 1:38 am

    @jwodder and @onteria_ covered the IPv6 portion well, so I’ll just tackle the numbers portion:

    ai_flags -> 40
    

    Probably this is going to be the sum of the following two in /usr/include/netdb.h:

    # define AI_V4MAPPED    0x0008  /* IPv4 mapped addresses are acceptable.  */
    # define AI_ADDRCONFIG  0x0020  /* Use configuration of this host to choose
    

    This is the protocol family, inet, inet6, apx, unix, etc.:

    ai_family -> 2
    
    bits/socket.h:78:#define    PF_INET     2   /* IP protocol family.  */
    bits/socket.h:119:#define   AF_INET     PF_INET
    

    This is the socket type, stream, dgram, packet, rdm, seqpacket:

    ai_socktype -> 1
    
    bits/socket.h:42:  SOCK_STREAM = 1,     /* Sequenced, reliable, connection-based
    

    The higher-level protocol, TCP, UDP, TCP6, UDP6, UDPlite, ospf, icmp, etc:

    ai_protocol -> 6
    

    Funny enough, in /etc/protocols:

    tcp 6   TCP     # transmission control protocol
    

    The size of the struct sockaddr. (Differs based on the address family! Ugh.)

    ai_addrlen -> 16
    

    This is because you’re getting back a struct sockaddr_in, see linux/in.h:

    #define __SOCK_SIZE__   16      /* sizeof(struct sockaddr)  */
    struct sockaddr_in {
      sa_family_t       sin_family; /* Address family       */
      __be16        sin_port;   /* Port number          */
      struct in_addr    sin_addr;   /* Internet address     */
    
      /* Pad to size of `struct sockaddr'. */
      unsigned char     __pad[__SOCK_SIZE__ - sizeof(short int) -
                sizeof(unsigned short int) - sizeof(struct in_addr)];
    };
    

    And the last one, from /etc/hosts 🙂

    ai_addr hostname ->  127.0.0.1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.