24

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.

wangshuaijie
  • 1,821
  • 3
  • 21
  • 37
  • 1
    I'm getting an error on VSCode that says, "struct "hostent" has no field "h_addr" but it compiles and runs fine. The code was copy pasted from here: https://www.cs.cmu.edu/afs/cs/academic/class/15213-f99/www/class26/tcpclient.c – mLstudent33 Dec 31 '19 at 04:42

4 Answers4

29

You missed this bit right under it:

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

So no, there is no problem.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

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.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • One might wonder: "how long is the `h_addr_list` array?" Answer: Iterate down it until you find a `NULL` ptr. An address string inside the `h_addr_list` which consists of nothing but a `NULL` ptr indicates the end of the list. The [GNU libc documentation link (see here for it all one 1 pg)](https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-struct-hostent) states that too: `The vector is terminated by a null pointer.` – Gabriel Staples Mar 29 '22 at 02:08
2

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.

mortmann
  • 71
  • 3
1

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.

kralyk
  • 4,249
  • 1
  • 32
  • 34