Thread: getnameinfo() example problem

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    40

    getnameinfo() example problem

    Hi,
    i have seen this thread:
    getnameinfo()

    and i have also tried it

    so, my code is the following:
    Code:
     
    int main(int argc, char * argv[])
    {
    
    
      struct sockaddr_in sa;
       char node[NI_MAXHOST];
       sa.sin_family = AF_INET;
      
      inet_pton(AF_INET, "8.8.178.110", &sa.sin_addr);  /* Host: www.freebsd.org ; official host name: wfe0.ysv.freebsd.org*/
     
     
      int res = getnameinfo((struct sockaddr*)&sa, sizeof(sa), node, sizeof(node), NULL, 0, NI_NUMERICSERV | NI_NUMERICHOST );
      if (res)
      {
        printf("error: %d\n", res);
        printf("%s\n", gai_strerror(res));
      }
      
      printf("node=%s\n", node);
      
      printf("error: %d\n", res);
      printf("%s\n", gai_strerror(res));
      
      return 0;
    }

    my compile options are the follwing:
    Code:
    tt: tt.c
        reset 
        #gcc -Wall -ansi -pedantic tt.c -o tt
        gcc tt.c -o tt
    everything works and compiles great, but instead of the hostname i get again the IP address and i dont know why
    smb is able to help me?

    i thank u in advance for the help

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    It works.
    If it can't resolve the name it defaults to the number unless you use the NI_NAMEREQD flag, which makes it an error.
    The NI_NUMERICHOST flag is asking it to return the numeric host, so don't use that.

    Code:
    #define _GNU_SOURCE
    #include <stdio.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    
    int main(void) {
        struct sockaddr_in sa;
        char node[NI_MAXHOST];
    
        memset(&sa, 0, sizeof sa);
        sa.sin_family = AF_INET;
        
        inet_pton(AF_INET, "8.8.8.8", &sa.sin_addr);
        /* google-public-dns-a.google.com */
    
        int res = getnameinfo((struct sockaddr*)&sa, sizeof(sa),
                              node, sizeof(node),
                              NULL, 0, NI_NAMEREQD);
        
        if (res) {
            printf("error: %d\n", res);
            printf("%s\n", gai_strerror(res));
        }
        else
            printf("node=%s\n", node);
        
        return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    40
    Quote Originally Posted by algorism View Post
    thank u for the answering, but why it was not able to resolve the name? is there a way to find out?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    40
    for example the following piece of code works,

    Code:
    int main(void)
    {
        struct addrinfo *result;
        struct addrinfo *res;
        int error;
    
    
        /* resolve the domain name into a list of addresses */
        error = getaddrinfo("www.freebsd.org", NULL, NULL, &result);
        if (error != 0)
        {   
            fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
            return EXIT_FAILURE;
        }   
    
    
        /* loop over all returned results and do inverse lookup */
        for (res = result; res != NULL; res = res->ai_next)
        {   
            char hostname[NI_MAXHOST] = "";
    
    
            error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); 
            if (error != 0)
            {
                fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
                continue;
            }
            if (*hostname != '\0')
                printf("hostname: %s\n", hostname);
        }   
    
    
        freeaddrinfo(result);
        return EXIT_SUCCESS;
    }
    but why the code getnameinfo() example problem (that is, the code from the first comment)does not work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. getnameinfo()
    By pulio in forum C Programming
    Replies: 0
    Last Post: 10-31-2009, 05:39 AM
  5. getnameinfo()
    By karas in forum Linux Programming
    Replies: 2
    Last Post: 10-04-2007, 12:50 AM

Tags for this Thread