Discussion:
inet_ntop in windows
(too old to reply)
Rayne
2009-12-08 03:56:20 UTC
Permalink
Hi all,

I'm trying to implement the inet_ntop function in Windows, and I found
the code from http://www.mail-archive.com/***@ipv6.org/msg02107.html

I'm using Visual Studio .NET 2003, and according to the msdn article
for getnameinfo(), I need to include the Ws2tcpip.h file and also
include the Wspiapi.h file. So in my header file, I have

#include <Ws2tcpip.h>
#include <Wspiapi.h>
#include <string.h>

and the function definition is in a .c file. Ws232.lib is also
specified in my Project properties -> Configuration Properties ->
Linker -> Input -> Additional Dependencies.

However, I have the following errors (several instances of each error
shown below, and several other similar ones) and they all point to the
Ws2tcpip.h file.

error C2039: 'Byte' : is not a member of 'in6_addr::__unnamed'
error C2039: 'sin6_flowinfo' : is not a member of 'sockaddr_in6'
error C2039: 'sin6_port' : is not a member of 'sockaddr_in6'
error C2039: 'sin6_scope_id' : is not a member of 'sockaddr_in6'
error C2039: 'Word' : is not a member of 'in6_addr::__unnamed'
error C2065: 'AF_INET6' : undeclared identifier
error C2079: 'ip_mreq::imr_interface' uses undefined struct 'in_addr'
error C2079: 'ip_msfilter::imr_interface' uses undefined struct
'in_addr'
error C2079: 'sockaddr_gen::Address' uses undefined struct 'sockaddr'
error C2079: 'sockaddr_gen::AddressIn' uses undefined struct
'sockaddr_in'
error C2371: 'FAR' : redefinition; different basic types
error C2501: 'in6_addr::__unnamed::Byte' : missing storage-class or
type specifiers
error C3861: 'AF_INET6': identifier not found, even with argument-
dependent lookup
error C3861: 'memcmp': identifier not found, even with argument-
dependent lookup
error C3861: 'memset': identifier not found, even with argument-
dependent lookup

I've tried including <Windows.h> and switching the order of
<Ws2tcpip.h> and <Wspiapi.h>, but I still get the errors. Why is this
so?

Thank you.

Regards,
Rayne
Giovanni Dicanio
2009-12-08 09:24:03 UTC
Permalink
Post by Rayne
I'm trying to implement the inet_ntop function in Windows, and I found
[...]
Post by Rayne
Thank you.
I tried to compile the code you pointed in your message in VS2008 SP1
(unfortunately I don't have VS.NET 2003 available):

<code>

#include <WS2tcpip.h>


//////////////////////////////////////////////////////////////////////////
//
// Code from:
// http://www.mail-archive.com/***@ipv6.org/msg02107.html
//
#ifdef _WIN32
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}

int inet_pton(int af, const char *src, void *dst)
{
struct addrinfo hints, *res, *ressave;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af;

if (getaddrinfo(src, NULL, &hints, &res) != 0)
{
// Commented out because dolog and LOG_ERR are not defined...
// dolog(LOG_ERR, "Couldn't resolve host %s\n", src);
return -1;
}

ressave = res;

while (res)
{
memcpy(dst, res->ai_addr, res->ai_addrlen);
res = res->ai_next;
}

freeaddrinfo(ressave);
return 0;
}

#endif

//
//////////////////////////////////////////////////////////////////////////


int main()
{
return 0;
}

</code>

After trying to compile it, I got the following errors:

error C2373: 'inet_pton' : redefinition; different type modifiers
see declaration of 'inet_pton'
error C2491: 'inet_pton' : definition of dllimport function not allowed

They disappear if the custom inet_pton function is renamed (in fact,
inet_pton is already declared in <w2tcpip.h>).


HTH,
Giovanni
Rayne
2009-12-08 09:44:20 UTC
Permalink
On Dec 8, 5:24 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
Post by Rayne
I'm trying to implement the inet_ntop function in Windows, and I found
[...]
Post by Rayne
Thank you.
I tried to compile the code you pointed in your message in VS2008 SP1
<code>
#include <WS2tcpip.h>
//////////////////////////////////////////////////////////////////////////
//
//
#ifdef _WIN32
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}
int inet_pton(int af, const char *src, void *dst)
{
struct addrinfo hints, *res, *ressave;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af;
if (getaddrinfo(src, NULL, &hints, &res) != 0)
{
// Commented out because dolog and LOG_ERR are not defined...
// dolog(LOG_ERR, "Couldn't resolve host %s\n", src);
return -1;
}
ressave = res;
while (res)
{
memcpy(dst, res->ai_addr, res->ai_addrlen);
res = res->ai_next;
}
freeaddrinfo(ressave);
return 0;
}
#endif
//
//////////////////////////////////////////////////////////////////////////
int main()
{
return 0;
}
</code>
error C2373: 'inet_pton' : redefinition; different type modifiers
see declaration of 'inet_pton'
error C2491: 'inet_pton' : definition of dllimport function not allowed
They disappear if the custom inet_pton function is renamed (in fact,
inet_pton is already declared in <w2tcpip.h>).
HTH,
Giovanni
I actually left out the inet_pton function, because I only want
inet_ntop. Did you only #include <Ws2tcpip.h>? I took out <Wspiapi.h>
and still have the errors. In fact, I now have several new errors,
which point to the WSPiApi.h file:

error C3861: 'gethostbyaddr': identifier not found, even with argument-
dependent lookup
error C3861: 'getservbyname': identifier not found, even with argument-
dependent lookup
error C3861: 'htonl': identifier not found, even with argument-
dependent lookup
error C3861: 'htons': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_ntoa': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_addr': identifier not found, even with argument-
dependent lookup
error C3861: 'ntohs': identifier not found, even with argument-
dependent lookup
error C3861: 'WSAGetLastError': identifier not found, even with
argument-dependent lookup

What is strange is that they (errors that point to WSPiApi.h) still
appear when I took out the .h and .c files that declared/defined the
inet_ntop function, and commented out the one header file that
#included it.
Giovanni Dicanio
2009-12-08 09:59:01 UTC
Permalink
Post by Rayne
I actually left out the inet_pton function, because I only want
inet_ntop. Did you only #include <Ws2tcpip.h>? I took out <Wspiapi.h>
and still have the errors. In fact, I now have several new errors,
I posted a compilable code in my previous message (defined between <code>
and </code>).
And the first (and only) #include statement is:

#include <WS2tcpip.h>

You may want to try to paste that code in your VS.NET2003 editor, try to
compile and see what happens.

(I tried that on VS2008 SP1 with Windows 7 SDK).

Giovanni
Rayne
2009-12-08 10:24:54 UTC
Permalink
On Dec 8, 5:59 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
Post by Rayne
I actually left out the inet_pton function, because I only want
inet_ntop. Did you only #include <Ws2tcpip.h>? I took out <Wspiapi.h>
and still have the errors. In fact, I now have several new errors,
I posted a compilable code in my previous message (defined between <code>
and </code>).
#include <WS2tcpip.h>
You may want to try to paste that code in your VS.NET2003 editor, try to
compile and see what happens.
(I tried that on VS2008 SP1 with Windows 7 SDK).
Giovanni
I tried the code on a completely new project, and still get the errors
(all pointing to Ws2tcpip.h), with and without #include <WSPiApi.h>.

But the errors are different:

error C2027: use of undefined type 'in6_addr'
error C2037: left of 'sin6_addr' specifies undefined struct/union
'sockaddr_in6'
error C2037: left of 'u' specifies undefined struct/union 'in6_addr'
error C2061: syntax error : identifier 'u_char'
error C2061: syntax error : identifier 'u_long'
error C2061: syntax error : identifier 'u_short'
error C2061: syntax error : identifier 'Word'
error C2065: 'AF_INET6' : undeclared identifier
error C2079: 'imr_interface' uses undefined struct 'in_addr'
error C2168: 'memset': too few actual parameters for intrinsic
function
error C2198: 'IN6_IS_ADDR_UNSPECIFIED' : too few arguments for call
through pointer-to-function
warning C4033: 'IN6_IS_ADDR_LINKLOCAL' must return a value
Giovanni Dicanio
2009-12-08 11:20:36 UTC
Permalink
Post by Rayne
I tried the code on a completely new project, and still get the errors
(all pointing to Ws2tcpip.h), with and without #include <WSPiApi.h>.
Did you create an empty project?
Do you use precompiled headers? If so, is #include "stdafx.h" the first
non-comment line in your source code?

The following code compiles fine in VS2008 SP1 with Windows 7 SDK:

<code>

#include <WS2tcpip.h>


const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}


int main()
{
return 0;
}

</code>


I don't have VS.NET 2003 available, so I'm sorry if I can't give you better
help.

Giovanni
Rayne
2009-12-09 02:44:24 UTC
Permalink
On Dec 8, 7:20 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
Post by Rayne
I tried the code on a completely new project, and still get the errors
(all pointing to Ws2tcpip.h), with and without #include <WSPiApi.h>.
Did you create an empty project?
Do you use precompiled headers? If so, is #include "stdafx.h" the first
non-comment line in your source code?
<code>
#include <WS2tcpip.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}
int main()
{
return 0;
}
</code>
I don't have VS.NET 2003 available, so I'm sorry if I can't give you better
help.
Giovanni
I compiled using

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>

in my new project and it compiled and ran without any errors.

When I used the same #include files in the header file in my original
project (keeping everything the same), there doesn't seem to be any of
the errors that point to WS2tcpip.h that I posted. But there are still
errors that point to WSPiApi.h file, like I've posted previously:

error C3861: 'gethostbyaddr': identifier not found, even with argument-
dependent lookup
error C3861: 'getservbyname': identifier not found, even with argument-
dependent lookup
error C3861: 'htonl': identifier not found, even with argument-
dependent lookup
error C3861: 'htons': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_ntoa': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_addr': identifier not found, even with argument-
dependent lookup
error C3861: 'ntohs': identifier not found, even with argument-
dependent lookup
error C3861: 'WSAGetLastError': identifier not found, even with
argument-dependent lookup

These errors appear even though I did not explicitly include
WSPiApi.h. And I can't figure out why.

Giovanni, I really appreciate your help! Thanks!
Ismo Salonen
2009-12-09 06:13:16 UTC
Permalink
Post by Rayne
On Dec 8, 7:20 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
Post by Rayne
I tried the code on a completely new project, and still get the errors
(all pointing to Ws2tcpip.h), with and without #include <WSPiApi.h>.
Did you create an empty project?
Do you use precompiled headers? If so, is #include "stdafx.h" the first
non-comment line in your source code?
<code>
#include <WS2tcpip.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}
int main()
{
return 0;
}
</code>
I don't have VS.NET 2003 available, so I'm sorry if I can't give you better
help.
Giovanni
I compiled using
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
in my new project and it compiled and ran without any errors.
When I used the same #include files in the header file in my original
project (keeping everything the same), there doesn't seem to be any of
the errors that point to WS2tcpip.h that I posted. But there are still
error C3861: 'gethostbyaddr': identifier not found, even with argument-
dependent lookup
error C3861: 'getservbyname': identifier not found, even with argument-
dependent lookup
error C3861: 'htonl': identifier not found, even with argument-
dependent lookup
error C3861: 'htons': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_ntoa': identifier not found, even with argument-
dependent lookup
error C3861: 'inet_addr': identifier not found, even with argument-
dependent lookup
error C3861: 'ntohs': identifier not found, even with argument-
dependent lookup
error C3861: 'WSAGetLastError': identifier not found, even with
argument-dependent lookup
These errors appear even though I did not explicitly include
WSPiApi.h. And I can't figure out why.
Giovanni, I really appreciate your help! Thanks!
As Giovanni expected I also would suspect the precompiled headers.
Switch the "use precompiled headers" off for the offending file for
testing purposes and recompile. If it now compiles then you found the error.

ismo

Continue reading on narkive:
Loading...