campo-sirio/setup/setupkey.cpp

266 lines
6.6 KiB
C++
Raw Normal View History

#include "wxinc.h"
#include <wx/socket.h>
#include "setupkey.h"
extern "C"
{
#include "../xvaga/hlapi_c.h"
#include "../xvaga/skeylink.h"
}
///////////////////////////////
// Utilities //
///////////////////////////////
bool ErrorBox(const wxString str)
{
wxMessageBox(str, APPNAME, wxOK | wxICON_ERROR);
return false;
}
bool WarningBox(const wxString str)
{
wxMessageBox(str, APPNAME, wxOK | wxICON_EXCLAMATION);
return false;
}
static int ThisYear()
{
int anno = 2006;
time_t lt;
if (time(&lt) == 0)
{
struct tm* timeloc = localtime(&lt) ;
if (timeloc != NULL)
anno = timeloc->tm_year + 1900;
}
return anno;
}
///////////////////////////////
// Gestione chiave Hardlock //
///////////////////////////////
bool HardlockGarble(unsigned short* data)
{
HL_CODE(data, 1);
return true;
}
unsigned short HardlockLogin(int& year)
{
unsigned short serno = 0xFFFF;
unsigned char REFKEY[16] = "CAMPOKEY";
unsigned char VERKEY[16] = "<EFBFBD><EFBFBD>c<EFBFBD><";
if (HL_LOGIN(26952, LOCAL_DEVICE, REFKEY, VERKEY) == STATUS_OK)
{
unsigned short eprom[64]; memset(eprom, 0, sizeof(eprom));
HL_READBL((unsigned char*)eprom);
unsigned short data[4];
memcpy(data, eprom, sizeof(data));
HardlockGarble(data);
if (data[0] == 0xFAE8)
serno = data[1];
else
serno = 0;
memcpy(data, &eprom[60], sizeof(data));
if (HardlockGarble(data))
year = (int)data[0];
HL_LOGOUT();
}
return serno;
}
///////////////////////////////
// Gestione chiave Eutron //
///////////////////////////////
void EncodeEutronPassword(char* str)
{
const char* const key = "QSECOFR-";
char tmp[16];
int i;
for (i = 0; str[i]; i++)
tmp[i] = str[i] + (i < 8 ? key[i] : str[i - 8]);
tmp[i] = '\0';
strcpy(str, tmp);
}
unsigned short EutronLogin(int& year)
{
unsigned short serno = 0xFFFF;
const char* const login[2] = { "AGA.CAMPO", "25EBAI" };
for (int i = 0; i < 2; i++)
{
KEY_NET eutron_key;
memset(&eutron_key, 0, sizeof(KEY_NET));
eutron_key.net_command = NET_KEY_OPEN;
eutron_key.command = LOCATING_MODE;
const char* const chiaro = login[i];
char cifrato[16];
strcpy(cifrato, chiaro);
EncodeEutronPassword(cifrato);
memset(eutron_key.label, 0, LABEL_LENGTH);
strcpy((char*)eutron_key.label, chiaro);
memset(eutron_key.password, 0, PASSWORD_LENGTH);
strcpy((char*)eutron_key.password, cifrato);
smartlink(&eutron_key);
if (eutron_key.status == ST_OK)
{
eutron_key.net_command = NET_KEY_ACCESS;
eutron_key.command = BLOCK_READING_MODE;
short* pointer = (short*)(&eutron_key.data[0]);
short* number = (short*)(&eutron_key.data[2]);
*pointer = 0; // Posizione in cui leggere
*number = 8; // Words da leggere
eutron_key.status = -1;
smartlink(&eutron_key);
if (eutron_key.status == ST_OK)
{
serno = (unsigned short)atol((const char*)eutron_key.data+4);
if (i == 0)
{
const unsigned short y = *(unsigned short*)(eutron_key.data+12);
if (y > 2000 && y < 3000)
year = y;
}
else
year = ThisYear();
eutron_key.net_command = NET_KEY_CLOSE;
smartlink(&eutron_key);
break;
}
}
}
return serno;
}
//////////////////////////////////
// Gestione Server di chiavi //
//////////////////////////////////
static unsigned short InquireSocketNumber(wxSocketClient& sc, const wxString& strCommand)
{
sc.Write(strCommand, (wxUint32)strCommand.Len());
long number[2] = { 0L, 0L };
sc.Read(number, sizeof(number));
return (unsigned short)number[1];
}
static unsigned short InquireServer(const wxString& strSrvName, int& year, const bool bMsg)
{
unsigned short serno = 0xFFFF;
year = 0;
wxIPV4address ip;
if (!ip.Hostname(strSrvName) || !ip.Service("1883")) //indirizzo NON valido o porta scazzata!
{
if (bMsg)
ErrorBox("Indirizzo IP errato!");
return serno;
}
wxSocketClient* sc = new wxSocketClient;
sc->SetTimeout(2);
if (sc->Connect(ip)) //il computer remoto risponde ma il server no (non c'e' o e' spento)
{
serno = InquireSocketNumber(*sc, "DongleNumber()");
year = InquireSocketNumber(*sc, "DongleYear()");
sc->Discard();
}
else
{
if (bMsg)
{
wxString strMsg;
strMsg << "Il server " << strSrvName << " non e' raggiungibile!";
ErrorBox(strMsg);
}
}
sc->Destroy();
return serno;
}
unsigned short ServerLogin(int& year, wxString& strSrvName)
{
unsigned short serno = 0xFFFF;
//se non specificato un nome/indirizzo di server, lo cerca tra i miei "cpu fratelli" in rete
if (strSrvName.IsEmpty())
{
//intanto deve trovare il suo IP di rete
wxIPV4address ipLocal;
ipLocal.AnyAddress();
wxString strMyName = ipLocal.Hostname();
ipLocal.Hostname(strMyName);
const wxString strMyIP = ipLocal.IPAddress();
if (strMyIP.Len() > 7)
{
const unsigned short MaxSck = 256;
// for (int j = 3; j < 256 && serno == 0xFFFF; j += MaxSck)
{
wxSocketClient* sc[MaxSck];
memset(sc, 0, sizeof(sc));
wxIPV4address ipRemote[MaxSck];
for (int i = 0; i < MaxSck; i++)
{
const int n = i; //+j;
if (n < 256)
{
//deve sostituire l'ultima cifra dell'IP con tutti i numeri che vanno da 0 a 255 alla ricerca del server
wxString strSrvIP = strMyIP.BeforeLast('.');
strSrvIP << "." << n; //ip del computer remoto
if (strSrvIP == strMyIP)
continue;
if (ipRemote[i].Hostname(strSrvIP) && ipRemote[i].Service("1883"))
{
sc[i] = new wxSocketClient;
sc[i]->SetTimeout(2);
sc[i]->Connect(ipRemote[i], false);
}
}
}
wxMilliSleep(100);
for (int i = 0; i < MaxSck; i++) if (sc[i])
{
sc[i]->WaitOnConnect(0, 1);
if (sc[i]->IsConnected())
{
sc[i]->GetPeer(ipRemote[i]);
strSrvName = ipRemote[i].Hostname();
serno = InquireSocketNumber(*sc[i], "DongleNumber()");
year = InquireSocketNumber(*sc[i], "DongleYear()");
sc[i]->Discard();
if (year > 0)
break;
}
sc[i]->Close();
} //for (inti =0...
for (int i = 0; i < MaxSck; i++) if (sc[i])
sc[i]->Destroy();
} //for(int j=0...
} //if(strMyIP.Len()...
} //if(strSrvName.Is...
else
serno = InquireServer(strSrvName, year, true);
return serno;
}
bool DictionaryLogin(const wxString& strSrvName)
{
return false;
}