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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <windows.h>
#include <winsock2.h>
#include <cstdio>
#include <ctime>
FILE* file=fopen("iplog.txt","a");
time_t rawtime;
struct tm* timeinfo;
char buffer[256];
char spf_buffer[256];
void log(const char* msg){
time(&rawtime);
timeinfo=localtime(&rawtime);
strcpy(spf_buffer,asctime(timeinfo));
spf_buffer[strlen(spf_buffer)-1]=0;
fprintf(file,"%s: %s\n",spf_buffer,msg);}
struct cleanup{
~cleanup(){
if(file!=NULL){
log("Program exiting");
fclose(file);}
WSACleanup();}}trash;
bool getip(SOCKET& client){
send(client,"GET / HTTP/1.0\r\n\r\n",18,0);
int x=recv(client,buffer,256,0);
if(!strstr(buffer,"200 OK") || x<=0){
return false;}
for(x=0;recv(client,buffer+x,1,0)>0;++x){}
buffer[x]=0;
return true;}
int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hprev,LPSTR cmdline,int cmdshow){
if(file==NULL){
return 1;}
log("Program starting");
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa);
LPHOSTENT hostentry=gethostbyname("www.whatismyip.org");
if(!hostentry){
log("");
fprintf(file,"ERROR %i: Failed to connect to host www.whatismyip.org\n",WSAGetLastError());
return 2;}
SOCKET client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(client==INVALID_SOCKET){
log("");
fprintf(file,"ERROR %i: Socket initialization failed\n",WSAGetLastError());
return 3;}
SOCKADDR_IN serverinfo;
serverinfo.sin_family=AF_INET;
serverinfo.sin_addr=*((LPIN_ADDR)*hostentry->h_addr_list);
serverinfo.sin_port=htons(80);
if(connect(client,(SOCKADDR*)&serverinfo,sizeof(struct sockaddr_in))==SOCKET_ERROR){
log("");
fprintf(file,"ERROR: %i: Failed to connect to host www.whatismyip.org",WSAGetLastError());
return 4;}
while(1){
if(!getip(client)){
break;}
//for debug mode to make sure the IP is correct
printf("IP: %s",buffer);
log(buffer);
Sleep(1000*60*60*3);}
return 0;}
|