I need to determine whether a particular string is a valid IPv4 or IPv6 address literal. If I understand correctly, the correct way to do this on POSIX systems is to use inet_pton
to convert it into a network address structure and see if it succeeds. Windows Vista and later have InetPton
which does essentially the same thing. But as far as I can tell, Windows XP doesn't declare either of those, and I need to be able to do this correctly on XP. So, the question is what system function to use to do this?
Worst case, I can write a function to parse it myself, but I'd prefer a standard, system function which has therefore been thoroughly tested and properly handles all corner cases and whatnot. It's already bad enough that Microsoft couldn't just declare inet_pton
like everyone else and went with InetPton
for their newer OSes.
In windows XP you can use these functions:
That's it. Link with ws2_32 library.
Looking through the SDK header files I find
inet_pton()
declared in "c:\Program Files\Microsoft SDKs\Windows\v7.1\Include\WS2tcpip.h".InetPton
is only a #define to that.So, it seems MS is providing the proper interface after all, but only starting with Vista. But XP has no IPv6 support that is worth mentioning, if I recall correctly.
The other choice is to wrap it yourself with two calls to
WSAAddressToString()
, one withAF_INET
, one withAF_INET6
. I'm guessing here from the docs, haven't tried it.InetPton documentation states the following requirements:
How about relying of a portion of code from the OSS network tool Wireshark?
You can find their reimplementation of inet_pton in their source code repository.
The licence (see below) is very permissive, so that may be a good alternative.