Skip to content

Instantly share code, notes, and snippets.

@sharat
Created December 12, 2012 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharat/4264828 to your computer and use it in GitHub Desktop.
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)
// 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