Like Tree1Likes
  • 1 Post By Syscal

FreeBSD - getaddrinfo() returning duplicate records...

This is a discussion on FreeBSD - getaddrinfo() returning duplicate records... within the Networking/Device Communication forums, part of the General Programming Boards category; Here is the code to query for addresses in their numeric form: Code: #include <netdb.h> #include <sys/socket.h> #include <stdio.h> #include ...

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265

    FreeBSD - getaddrinfo() returning duplicate records...

    Here is the code to query for addresses in their numeric form:

    Code:
    #include <netdb.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <string.h>
    
    int ga_hints( const int, char ** , struct addrinfo * );
    
    int main(int argc, char ** argv){
    
      struct addrinfo * res, * result, hints;
      int error = 0, host_arg = 0; 
      char host[NI_MAXHOST];
      
      host_arg = ga_hints( argc, argv, &hints );
    
      if( ( error = getaddrinfo( argv[ host_arg ], NULL, &hints, &result ) ) != 0 ){
        fputs( gai_strerror( error ), stderr );
        exit( -1 ); 
      }
    
      for( res = result; res != NULL; res = res->ai_next ){
    
        if( ( error = getnameinfo( res->ai_addr, res->ai_addrlen,  host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST ) ) != 0){
          fputs( gai_strerror( error ), stderr );
          exit( -1 );
        }
    
        printf(" - %s\n", host);
    
      }
      
    
      free(result);
    
      return 0;
    
    }
    
    // Usage: ./galookup -f [inet4 | inet6 | inet4-6] -h [hostname] 
    
    int ga_hints( const int argc, char ** argv, struct addrinfo * hints ){
    
      int arg, host;
    
      bzero( hints, sizeof( *hints) );
    
      for( arg = 1; arg < argc; arg++ ){
        
        if( !strcmp( "-f", argv[ arg ] ) ){
          
          switch( atoi( argv[ ++arg ] ) ){
    	
          case 4:
    	hints->ai_family = AF_INET;
    	break;
    
          case 6:
    	hints->ai_family = AF_INET6;
    	break;
    
          case 0:
    	hints->ai_family = AF_UNSPEC;
    	break;
          default:
    	printf( "%s is not a valid address family\nDefaulting to AF_UNSPEC...\n", argv[ arg ] );
    	hints->ai_family = AF_UNSPEC;
    	break;
    
          } // Switch
    
        } //if( !strcmp())
        else if( !strcmp( "-h", argv[ arg ] ) )
          host = arg + 1;
    
      } //for(...)
    
      return host;
    
    }

    and look what happens when I run it:


    Code:
    $ ./galookup -f 0 -h localhost
     - 127.0.0.1
     - 127.0.0.1
     - 127.0.0.1
     - ::1
     - ::1
     - ::1
    $
    $
    $
    $ ./galookup -f 0 -h www.google.com
     - 74.125.227.146
     - 74.125.227.145
     - 74.125.227.144
     - 74.125.227.147
     - 74.125.227.148
     - 74.125.227.144
     - 74.125.227.145
     - 74.125.227.148
     - 74.125.227.147
     - 74.125.227.148
     - 74.125.227.144
     - 74.125.227.145
     - 74.125.227.146
     - 74.125.227.147
     - 74.125.227.146
     - 2001:4860:4002:800::1010
     - 2001:4860:4002:800::1010
     - 2001:4860:4002:800::1010
    $ 
    $
    $ ./galookup -f 0 -h FreeBSD-01v
     - 127.0.0.1
     - 127.0.0.1
     - 127.0.0.1
     - 192.168.0.4
     - 192.168.0.4
     - 192.168.0.4
    $
    Here's what's in /etc/hosts

    Code:
    ::1			localhost localhost.my.domain
    127.0.0.1		localhost FreeBSD-01v localhost.my.domain
    192.168.0.4 		FreeBSD-01v
    Also, when I do an nslookup, the addresses are totally different for websites but they all seem to work... What gives with that also?
    Last edited by Syscal; 07-18-2012 at 08:05 PM.

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    ... Figured it out.

    Code:
    hints->ai_socktype = SOCK_STREAM;
    Lookup is performed for both SOCK_STREAM and SOCK_DGRAM if you don't specify the hint.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ip address using getaddrinfo()
    By ueg1990 in forum C Programming
    Replies: 4
    Last Post: 07-07-2012, 06:54 PM
  2. is getaddrinfo really necessary?
    By sunjayc99 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-28-2008, 03:14 PM
  3. getaddrinfo()
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2006, 02:41 AM
  4. Database entry - avoiding duplicate records
    By ChadJohnson in forum Tech Board
    Replies: 2
    Last Post: 02-03-2006, 11:43 PM
  5. Files&Records(Entering records)
    By Beginnerinc in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21