This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - john_engit

Pages: [1]
1
C - C++ / [C++] Get External/WAN IP (win32)
« on: December 06, 2011, 01:17:36 PM »
IPuR.h
Code: [Select]
//////////////////////////////////////
// Author    : john_engit
//////////////////////////////////////
#include <windows.h>
#include <wininet.h>

#pragma comment(lib,"wininet.lib")
#pragma comment(lib,"ws2_32.lib")

namespace IPuR
{
    //download file from WWW in a buffer
    bool WWWFileBuffer(char *host,char *path,char *outBuffer,int outBufferSize)
    {
        bool retval = false;
        LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};
        DWORD dwSize = outBufferSize-1,dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
        HINTERNET opn= NULL,con = NULL,req = NULL;
        opn = InternetOpen(TEXT("Evilzone.org"),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
        if(!opn)
            return retval;
        con = InternetConnect(opn,host,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
        if(!con)
            return retval;
        req = HttpOpenRequest(con,TEXT("GET"),path,HTTP_VERSION,NULL,(LPCTSTR*)AcceptTypes,dwFlags, 0);
        if(!req)
            return retval;
        if(HttpSendRequest(req, NULL, 0, NULL, 0))
        {
            DWORD statCodeLen = sizeof(DWORD);
            DWORD statCode;
            if(HttpQueryInfo(req,
                HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
                &statCode,&statCodeLen,NULL))
            {
                if(statCode==200 && InternetReadFile(req, (LPVOID)outBuffer, outBufferSize-1, &dwSize))
                {
                    retval = TRUE;
                }
            }
        }
        InternetCloseHandle(req);
        InternetCloseHandle(con);
        InternetCloseHandle(opn);
        return retval;
    }

    //Get your External/WAN IP
    BOOL GetWanIP(char *ip,int size)
    {
        bool retval = false;
        char *tmpip = new char[150];
        memset(ip,0,size);

        //www.whatismyip.org
        if(!retval)
        {
            memset(tmpip,0,150);
            WWWFileBuffer("www.whatismyip.org",NULL,tmpip,150);
            if(strlen(tmpip)>0 && inet_addr(tmpip) != INADDR_NONE)
            {
                retval = true;
            }
        }

        //automation.whatismyip.com/n09230945.asp
        if(!retval)
        {
            memset(tmpip,0,150);
            WWWFileBuffer("automation.whatismyip.com","/n09230945.asp",tmpip,150);
            if(strlen(tmpip)>0 && inet_addr(tmpip) != INADDR_NONE)
            {
                retval = true;
            }
        }

        //www.showmyip.com/simple
        if(!retval)
        {
            memset(tmpip,0,150);
            WWWFileBuffer("www.showmyip.com","/simple",tmpip,150);
            if(strlen(tmpip)>0 && inet_addr(tmpip) != INADDR_NONE)
            {
                retval = true;
            }
        }

        //myip.dnsomatic.com
        if(!retval)
        {
            memset(tmpip,0,150);
            WWWFileBuffer("myip.dnsomatic.com",NULL,tmpip,150);
            if(strlen(tmpip)>0 && inet_addr(tmpip) != INADDR_NONE)
            {
                retval = true;
            }
        }

        //checkip.dyndns.org
        if(!retval)
        {
            memset(tmpip,0,150);
            WWWFileBuffer("checkip.dyndns.org",NULL,tmpip,150);
            if(strlen(tmpip)>0)
            {
                strcpy_s(tmpip,150,(strstr(tmpip,":")+2));
                int res = (int)(strstr(tmpip,"<") - tmpip);
                tmpip[res]='\0';
                if(inet_addr(tmpip) != INADDR_NONE)
                {
                    retval = true;
                }
                else
                    retval = false;
            }
        }

        if(retval)
            strcpy_s(ip,size,tmpip);
        delete[] tmpip;
        return retval;
    }
}

Usage :
Code: [Select]
char ip[16];
IPuR::GetWanIP(ip,16);

Function GetWanIP() takes two parameters :
1st param : pointer to char array
2nd param : length of char array

Example Code :
Code: [Select]
#include <iostream>
#include "IPuR.h"

using namespace std;

int main(int argc,char **argv)
{
    char ip[16];
    if(IPuR::GetWanIP(ip,16))
       cout << "WAN IP : " << ip <<endl;
    else
       cout << "Error Retreiving IP.";
    return EXIT_SUCCESS;
}

2
C - C++ / Re: [C]OpenGLDemo.1
« on: December 01, 2011, 11:04:57 AM »
awesome  :)

Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.