wangshuaijie
wangshuaijie

Reputation: 1931

Does struct hostent have a field "h_addr"?

I encountered the following code snapshot:

struct hostent *hp;
 hp = my_gethostbyname(localhost);
    if (hp == NULL) {
       ls_syslog(LOG_ERR, I18N_FUNC_FAIL, fname, "my_gethostbyname()");
       return -1;
    }
    strcpy(localhost, hp->h_name);

    memcpy(&addr, hp->h_addr, hp->h_length);

I am rather confused by the last statement, the declaration of struct hostent is like this:

struct hostent {
   char *h_name;       /* official name of host */
   char **h_aliases;   /* alias list */
   int h_addrtype;     /* host address type */
   int h_length;       /* length of address */
   char **h_addr_list; /* list of addresses */
};

It doesn't have a field named "h_addr", but the code did can compile, can anyone tell me why? thanks.

Upvotes: 24

Views: 29920

Answers (4)

Ratul Sharker
Ratul Sharker

Reputation: 8011

In the GNU libc manual (or see here for the entire libc manual all on one page) they say:

Recall that the host might be connected to multiple networks and have different addresses on each one

They also provide the h_addr variable which is just the first element of the vector h_addr_list.

Upvotes: 5

mortmann
mortmann

Reputation: 101

h_addr is not POSIX. See POSIX netdb.h. Using h_addr could result in error: ‘struct hostent’ has no member named ‘h_addr’. Portable code should use h_addr_list instead.

Upvotes: 2

kralyk
kralyk

Reputation: 4387

Note that the h_addr macro is on some systems only visible if you define _BSD_SOURCE and/or _DEFAULT_SOURCE before including header files.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

You missed this bit right under it:

#define h_addr h_addr_list[0] /* for backward compatibility */

So no, there is no problem.

Upvotes: 31

Related Questions