Created
December 12, 2012 04:20
-
-
Save sharat/4264828 to your computer and use it in GitHub Desktop.
Check the given IP address is in IPv6 or v4 format (windows version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Change the headefile for Windows | |
#include <memory> | |
#include <winsock2.h> | |
#include <ws2tcpip.h> | |
#pragma comment(lib,"Ws2_32.lib"); | |
#include <string> | |
using namespace std; | |
// This is compatible with old version of Windows. | |
bool IsIPAddress(const string& value) | |
{ | |
addrinfo hint, *res = NULL; | |
memset(&hint, '\0', sizeof(hint)); | |
hint.ai_family = PF_UNSPEC; | |
hint.ai_flags = AI_NUMERICHOST; | |
if (getaddrinfo(value.c_str(), NULL, &hint, &res)) | |
return false; | |
int ai_family = res->ai_family; | |
freeaddrinfo(res); | |
return ((ai_family == AF_INET) || (ai_family == AF_INET6)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment