The value is a struct sockaddr, which is commonly used by IP networking code to 
hold an address.  The definition of a "struct sockaddr" (see <sys/sockaddr.h>) 
is mostly a storage container.  It specifies what kind of address is in the 
container (the "address family"), and then just has a buffer that holds the 
value of the address.  

In many cases, there's no need to interpret what's stored in a sockaddr at all. 
 Most networking APIs use sockaddr structures.  So, for example, you can call 
getaddrinfo() to obtain one or more addresses (as sockaddrs) for some service 
that you want to contact, then just try making a socket connection with the 
connect() call.  You get your socket, but you don't really need to know what 
kind of address (the address family) was used.   This means that the same code 
will work with any kind of networking that's supported by your operating 
system.  Your code really doesn't need to know if the system used IPv4, IPv6, 
or perhaps some other networking protocol to make the connection, since all you 
really want is a socket.

If you need to dig inside the sockaddr, your code will need to first look at 
the sa_family variable.  Then it will need to handle each type of address 
family that you expect (or need to support).  Typically, you can simply cast 
the "struct sockaddr" type to another structure that breaks down the raw data 
buffer part of the sockaddr into meaningful components.  For example, if the 
address family is IPv4 (AF_INET - see <sys/socket.h>) then you can cast the 
struct sockaddr to a "struct sockaddr_in" (see <netinet/in.h>).  That structure 
breaks down the sockaddr address buffer into a port (TCP or UDP port number) 
and an IPv4 address.  If the family is AF_INET6, then you can cast to a "struct 
sockaddr_in6" (see <netinet6/in6.h>) where the sockaddr address is broken down 
into a port number, IPv6 flow information, an IPv6 address, and a scope ID.


--
Marc Majka

On 13 Jan, 2012, at 08:59, Martin McCormick wrote:

> I am experimenting with getaddrinfo and getnameinfo and have
> gotten a little confused as to the best way to extract the
> IP address recovered after the function runs. The element in the
> structure 
> res->ai_addr is a socket address which, if I am reading the
> documentation correctly, will give me the binary form of the
> internet address of the host I just looked up.
> 
>       Is that correct? Also, what is the easiest way to
> extract that value in to an unsigned long?
> 
>       Wikipedia has an excellent sample program that uses
> getaddrinfo to resolve a name to an IP address and then
> getnameinfo to reverse the lookup back to a name, but
> getnameinfo just takes res->ai_addr as an argument and works
> great, but one doesn't see how it extracted the IP address.
> 
>       Thanks for all good suggestions.
> 
> Martin McCormick WB5AGZ  Stillwater, OK 
> Systems Engineer
> OSU Information Technology Department Telecommunications Services Group
> _______________________________________________
> Please visit https://lists.isc.org/mailman/listinfo/bind-users to unsubscribe 
> from this list
> 
> bind-users mailing list
> bind-users@lists.isc.org
> https://lists.isc.org/mailman/listinfo/bind-users

_______________________________________________
Please visit https://lists.isc.org/mailman/listinfo/bind-users to unsubscribe 
from this list

bind-users mailing list
bind-users@lists.isc.org
https://lists.isc.org/mailman/listinfo/bind-users

Reply via email to