Can anybody explain me this statement!
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
Can anybody explain me this statement!
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
It's casting the struct member h_addr as a struct in_addr* then referencing the s_addr member of the newly casted in_addr
This treats hp->h_addr
as though it points to a struct in_addr
, and then tries to access the s_addr
member of that struct in_addr
set the s_addr field of sin_addr field of structure containing variable pin with value placed in field s_addr which is located at address which pointed by h_addr field of structure contained in variable hp
in other words take addres from value of h_addr of hp and assume it points to (in_addr) and copy field s_addr to approriate field of pin.sin_addr
From the inside ot:
hp->h_haddr
gets the h_addr
member of the structure pointed to by hp
.(struct in_addr *)
casts this h_addr
value to a pointer to an in_addr
structure.->s_addr
gets the s_addr
member of the in_addr
structure that it pointed to.Therefore, it gets the s_addr
member of the structure pointed to by the h_addr
member of the structure pointed to by hp
. (hp
and h_addr
are both pointers)
It then assigns this value to pin.sin_addr.s_addr
.
(hp->h_addr) : get the h_addr field from pointer hp
((struct in_addr *)(hp->h_addr)) : cast the result from above statement to a pointer to in_addr struct
->s_addr : get the s_addr field from the resulting pointer of the above statement
= : assignment.
Takes hp->h_addr
, and casts it to struct in_addr*
. The cast may have different reasons. Perhaps hp->h_addr
is void*
and the cast is needed to tell the compiler what type it should use for finding s_addr
. It may also be another struct that has an in_addr
as its first member
struct in_addr {
struct saddr_t s_addr;
};
struct socket {
struct in_addr addr;
};
struct server {
struct socket *h_addr;
};
server *hp;
Then, casting socket*
to in_addr*
would be valid because on its address there is a in_addr
at the beginning. After the cast, the member s_addr
is accessed. These types are common in network programming under POSIX, and don't look like in my above example, but the above could have been the situation at hand.
hp->a_addr is casted to a pointer to struct of type in_addr, in_addr has fields specified such as s_addr etc.
Unix sockets can handle different types of network addressing, internet addressing is just one of them, therefore one must cast the socket address descriptor to the correct one when accessing various fields.
For example sockaddr_in6 handles IPv6 addresses which is different from sockaddr_in.
hp is a struct hostent type pointer which is most likely to be returned by a "gethostbyname()" call. It has a member named h_addr which is actually an alias for the h_addr_list[0] (this holds the first IP address retrieved for your name query). In this case; you are passing this IP address to your struct sockaddr_in type variable "pin". It has a member named sin_addr with type struct in_addr.
struct in_addr { unsigned long int s_addr; }
To complete the assignment, you should typecast h_addr to struct in_addr ptr (normally it is char*), then access to its s_addr member.
I hope this explains what is going on with you line.