Advertisement
asqapro

Proxy-GetIP

Dec 6th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock2.h>
  3. #include <wininet.h> //note that this requires a linked .lib
  4.  
  5. using namespace std;
  6.  
  7. char* get_local_address(){
  8.     WSADATA wsadata;
  9.  
  10.     int error = WSAStartup(0x0202, &wsadata);
  11.  
  12.  
  13.     if(error){
  14.         return NULL;
  15.     }
  16.     if(wsadata.wVersion != 0x0202)
  17.     {
  18.         WSACleanup(); //Clean up Winsock
  19.         return NULL;
  20.     }
  21.  
  22.     char ac[80];
  23.     gethostname(ac, sizeof(ac));
  24.  
  25.     struct hostent *phe = gethostbyname(ac);
  26.     if (phe == 0) {
  27.         cerr << "Yow! Bad host lookup." << endl;
  28.         return NULL;
  29.     }
  30.  
  31.     struct in_addr addr;
  32.     cout << "All possible localhost IPs:" << endl;
  33.     for(unsigned int iter = 0; iter < sizeof(phe->h_addr_list); iter++){
  34.         memcpy(&addr, phe->h_addr_list[iter], sizeof(struct in_addr));
  35.         cout << "   Possible IP: " << inet_ntoa(addr) << endl;
  36.     }
  37.     return inet_ntoa(addr);
  38. }
  39.  
  40. char *MESSAGE;
  41.  
  42. int main(){
  43.     get_local_address();
  44.  
  45.     HINTERNET hInternet, hFile;
  46.     DWORD rSize;
  47.     char buffer[32];
  48.  
  49.     hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  50.     hFile = InternetOpenUrl(hInternet, "http://ipecho.net/plain", NULL, 0, INTERNET_FLAG_RELOAD, 0);
  51.     InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
  52.     buffer[rSize] = '\0';
  53.  
  54.     InternetCloseHandle(hFile);
  55.     InternetCloseHandle(hInternet);
  56.  
  57.     cout << "Your external IP Address is: " << buffer << "\n";
  58.     system("pause");
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement