Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does struct hostent have a field "h_addr"?

Tags:

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.

like image 555
wangshuaijie Avatar asked Jul 10 '12 02:07

wangshuaijie


People also ask

What is Hostent structure?

The hostent structure is used by functions to store information about a given host, such as host name, IPv4 address, and so forth. An application should never attempt to modify this structure or to free any of its components.

What is h_ addr?

h_addr is is a char pointer and host is a pointer to a struct of type hostent .


2 Answers

You missed this bit right under it:

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

So no, there is no problem.

like image 163
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 11:09

Ignacio Vazquez-Abrams


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.

like image 21
Ratul Sharker Avatar answered Sep 29 '22 13:09

Ratul Sharker