|
|
|
date: Thu, 31 Jul 2008 03:37:47 -0700 (PDT),
group: microsoft.public.platformsdk.networking.ipv6
back
How to get the IP address in network byte order
Hi,
Currently I have the following code to get the IP address in network
byte order,
unsigned int getIP(char *sHostName)
{
PHOSTENT ph;
unsigned int nAddr = inet_addr( sHostName );
if(nAddr == INADDR_NONE) /* hostname is in name format */
{
ph = gethostbyname(sHostName);
nAddr = *(unsigned int*)( ph->h_addr_list[0];
}
else /* hostname is in x.x.x.x format */
{
ph = gethostbyaddr( (const char*)&nAddr, 4, PF_INET );
nAddr = *(unsigned int*)( ph->h_addr_list[0] ) ;
}
return nAddr;
}
Going forward for IPv6, since these structures and functions are
deprecated, I have to replace PHOSTENT, gethostbyname, gethostbyaddr
and inet_addr by newer functions. I have been trying to achieve the
same using getaddrinfo, but the results are not the same.
Can anyone tell me how do we get the ip address in network byte order
by using getaddrinfo function or any other function which is IPv6
compliant
Thanks.
date: Thu, 31 Jul 2008 03:37:47 -0700 (PDT)
author: ipv6
RE: How to get the IP address in network byte order
"ipv6" wrote:
> Hi,
>
> Currently I have the following code to get the IP address in network
> byte order,
>
> unsigned int getIP(char *sHostName)
> {
> PHOSTENT ph;
> unsigned int nAddr = inet_addr( sHostName );
> if(nAddr == INADDR_NONE) /* hostname is in name format */
> {
> ph = gethostbyname(sHostName);
> nAddr = *(unsigned int*)( ph->h_addr_list[0];
> }
> else /* hostname is in x.x.x.x format */
> {
> ph = gethostbyaddr( (const char*)&nAddr, 4, PF_INET );
> nAddr = *(unsigned int*)( ph->h_addr_list[0] ) ;
> }
> return nAddr;
> }
>
> Going forward for IPv6, since these structures and functions are
> deprecated, I have to replace PHOSTENT, gethostbyname, gethostbyaddr
> and inet_addr by newer functions. I have been trying to achieve the
> same using getaddrinfo, but the results are not the same.
>
> Can anyone tell me how do we get the ip address in network byte order
> by using getaddrinfo function or any other function which is IPv6
> compliant
>
> Thanks.
>
date: Tue, 26 Aug 2008 02:05:00 -0700
author: mrhacker
|
|