#include "ExternalIp.h"
/* ExternalIp.cpp
Si veda il file ExternalIp.h per l'utilizzo delle funzioni
presenti nel modulo */
char *GetExternalIp(){
//Inizializza il socket
SOCKET s = InitClientSocket(WEBSERVER,WEBPORT);
if (s == 0) return "0.0.0.0\0";
//Inizializza la richiesta
char *pRequest = new char[0xFF];
wsprintf(pRequest,"GET / HTTP/1.1\nHost: %s:%d\n\n\0",WEBSERVER,WEBPORT);
//Invia la richiesta
SendTo(s,pRequest);
//Riceve la risposta...
char *pBuffer = new char[0xFFF];
int *iIpDigits = new int[3];
int res;
do{
res = RecvFrom(s,pBuffer);
//Stiamo leggendo la riga che ci serve?
if (strstr(pBuffer,"</TITLE>") != NULL){
//Etraiamo l'ip...
pBuffer = ExtractIpFromResponse(pBuffer);
//Usciamo dal ciclo
break;
}
}while(res == SERVER_SOCKET_OK);
closesocket(s);
return pBuffer;
}
char *ExtractIpFromResponse(char *pResponse){
char *pIp = new char[0x10];
memset(pIp,'\0',0x10);
int iDotsCount = 0;
bool bDigitFound = false;
for (register int c = 0; c<lstrlen(pResponse) && iDotsCount < 4; c++){
//Trovato una cifra dell'ip?
if (isdigit(pResponse[c])){
wsprintf(pIp,"%s%c",pIp,pResponse[c]);
bDigitFound = true;
//Trovato un punto?
}else if(pResponse[c] == '.' && bDigitFound){
wsprintf(pIp,"%s.",pIp);
iDotsCount++;
}
}
return pIp;
}