This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GCCs implementation of gethostbyname()


I call this function, and the structure it returns only ever has 1 ip
in it's h_addr_list, no matter how I have things set on my computer,
is this an error in GCC, an intended in GCC, or most likely, an error
in my programming?

I have determined the function checks /etc/hosts first, and then puts
the first entry into the list, dropping all other entries. If there
are no matching entries, it instead checks the DNS, again only taking
one entry.


Thanks again for all of the help everyone,
-Jim




Obviously domains and hostnames have been changed to protect the
innocent, but here are the results from my tests (tildes are placed in
the empty /etc/hosts file columns for visiblity, they are not in the
actual /etc/hosts file):

test 1:
========================================
# /etc/hosts
127.0.0.1   myhost.mydomain.myglobal   myhost  localhost.localdomain  localhost
1.2.3.4   myhost.mydomain.myglobal   myhost  ~  ~
========================================
Checking host IP [0x005049C0]: 127.000.000.001
Checking host IP [0x00000000]
========================================

test 2:
========================================
# /etc/hosts
127.0.0.1   myhost.mydomain.myglobal   myhost  localhost.localdomain  localhost
========================================
Checking host IP [0x005049C0]: 127.000.000.001
Checking host IP [0x00000000]
========================================

test 3:
========================================
# /etc/hosts
1.2.3.4   myhost.mydomain.myglobal   myhost  ~  ~
========================================
Checking host IP [0x005049C0]: 001.002.003.004
Checking host IP [0x00000000]
========================================

test 4:
========================================
# /etc/hosts
1.2.3.4  myhost.mydomain.myglobal  myhost  ~  ~
127.0.0.1  myhost.mydomain.myglobal  myhost  localhost.localdomain  localhost
========================================
Checking host IP [0x005049C0]: 001.002.003.004
Checking host IP [0x00000000]
========================================

test 5:
========================================
# /etc/hosts
127.0.0.1  ~  ~  localhost.localdomain  localhost
========================================
Checking host IP [0x005049C0]: 001.002.003.004
Checking host IP [0x00000000]
========================================




The test code:
========================================
  char strbuff[20]; /*this is more than enough to hold any ip information*/
  char *cptr, *cptr2; /*temp string pointer for copying.*/
  unsigned char *ipptr;
  int ipnum, charnum, bytenum; /*for copying IPs safely*/
  int i;
  int bound; /*socket bind error checking*/

  int i;
  struct sockaddr_in my_socket_data;
  struct hostent *my_host_entry;
  struct in_addr **addr_ptr;
  char *ipptr;

  my_host_entry = gethostbyname("myhost.mydomain.myglobal");
  addr_ptr = (struct in_addr **)(my_host_entry->h_addr_list);
  my_socket_data.sin_addr.s_addr = 0;
  for(i = 0; addr_ptr[i] && my_socket_data.sin_addr.s_addr == 0; i++)
  {
    ipptr = (char*)&addr_ptr[i]->s_addr;

    fprintf(stderr, "Checking host IP [0x%08X]:
%03d.%03d.%03d.%03d\n", addr_ptr[i], ipptr[0], ipptr[1], ipptr[2],
ipptr[3]);

    /*asign the IP, only if not loopback*/
    if(ipptr[0] != 127 || ipptr[1] != 0 || ipptr[2] != 0 || ipptr[3] != 1)
    {
      my_socket_data.sin_addr.s_addr = addr_ptr[i]->s_addr;
    }
  }
  fprintf(stderr, "Checking host IP [0x%08X]\n", addr_ptr[i]);

  if(!addr_ptr[i])
  {
    fprintf(stderr, "falling back on IP.\n");
    addr_ptr = (struct in_addr **)(my_host_entry->h_addr_list);
    mydata->my_socket_data.sin_addr.s_addr = (*addr_ptr)->s_addr;
  }


========================================


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]