From: "Jim Stapleton" <stapleton.41@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: GCCs implementation of gethostbyname()
Date: Mon, 20 Feb 2006 19:19:00 -0000 [thread overview]
Message-ID: <80f4f2b20602201119u7be1ee3an1f42bc9fa0c63d24@mail.gmail.com> (raw)
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;
}
========================================
next reply other threads:[~2006-02-20 19:19 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-02-20 19:19 Jim Stapleton [this message]
2006-02-20 19:46 ` Jim Stapleton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=80f4f2b20602201119u7be1ee3an1f42bc9fa0c63d24@mail.gmail.com \
--to=stapleton.41@gmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).