public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* GCCs implementation of gethostbyname()
@ 2006-02-20 19:19 Jim Stapleton
  2006-02-20 19:46 ` Jim Stapleton
  0 siblings, 1 reply; 2+ messages in thread
From: Jim Stapleton @ 2006-02-20 19:19 UTC (permalink / raw)
  To: gcc-help

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;
  }


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: GCCs implementation of gethostbyname()
  2006-02-20 19:19 GCCs implementation of gethostbyname() Jim Stapleton
@ 2006-02-20 19:46 ` Jim Stapleton
  0 siblings, 0 replies; 2+ messages in thread
From: Jim Stapleton @ 2006-02-20 19:46 UTC (permalink / raw)
  To: gcc-help

ok, it wasn't a GCC problem, sorry for the bother.

I needed to change something on /etc/host.conf

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-02-20 19:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-20 19:19 GCCs implementation of gethostbyname() Jim Stapleton
2006-02-20 19:46 ` Jim Stapleton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).