-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPGetter.cpp
43 lines (39 loc) · 1009 Bytes
/
IPGetter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "IPGetter.h"
size_t IP::downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer)
{
std::string* psResponse = (std::string*) writer;
size_t size = sz * nmemb;
psResponse->append((char*) buffer, size);
return sz * nmemb;
}
std::string IP::GetWebString(std::string strUrl)
{
std::string strTmpStr;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::string strRsp;
if (res != CURLE_OK)
{
strRsp = "error";
}
else
{
strRsp = strTmpStr;
}
return strRsp;
}
std::string IP::IPv4()
{
return GetWebString("http://api.ipify.org");
}
std::string IP::IPv6()
{
return GetWebString("http://v6.ident.me");
}