2007-09-13 10:24:51 +00:00
|
|
|
|
#include "wxinc.h"
|
|
|
|
|
#include <wx/socket.h>
|
|
|
|
|
|
2007-09-20 09:53:21 +00:00
|
|
|
|
#include "key.h"
|
|
|
|
|
#include "utils.h"
|
2007-09-13 10:24:51 +00:00
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#include "../xvaga/hlapi_c.h"
|
|
|
|
|
#include "../xvaga/skeylink.h"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
// Utilities //
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
static int ThisYear()
|
|
|
|
|
{
|
2010-03-11 15:28:12 +00:00
|
|
|
|
return wxDateTime::Now().GetYear();
|
2007-09-13 10:24:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
// 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>pو<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];
|
2007-09-14 11:07:04 +00:00
|
|
|
|
|
|
|
|
|
HL_LOGOUT();
|
2007-09-13 10:24:51 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
2007-09-14 11:07:04 +00:00
|
|
|
|
eutron_key.status = -1;
|
2007-09-13 10:24:51 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2007-09-14 11:07:04 +00:00
|
|
|
|
eutron_key.net_command = NET_KEY_CLOSE;
|
|
|
|
|
smartlink(&eutron_key);
|
|
|
|
|
|
2007-09-13 10:24:51 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////
|
2007-09-14 15:43:08 +00:00
|
|
|
|
// Gestione Server di chiavi //
|
2007-09-13 10:24:51 +00:00
|
|
|
|
//////////////////////////////////
|
2007-09-14 15:43:08 +00:00
|
|
|
|
|
|
|
|
|
class TBordello
|
|
|
|
|
{
|
|
|
|
|
WX_DECLARE_STRING_HASH_MAP(wxSocketClient*, TBrothel);
|
|
|
|
|
TBrothel m_map;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
wxSocketClient& Zoccola(wxIPV4address& ip);
|
|
|
|
|
wxSocketClient* QuellaBuona();
|
|
|
|
|
void Chiudi();
|
|
|
|
|
virtual ~TBordello();
|
|
|
|
|
} m_bordello;
|
|
|
|
|
|
|
|
|
|
wxSocketClient& TBordello::Zoccola(wxIPV4address& ip)
|
|
|
|
|
{
|
|
|
|
|
const wxString strIP = ip.IPAddress();
|
|
|
|
|
if (m_map[strIP] == NULL)
|
|
|
|
|
{
|
|
|
|
|
m_map[strIP] = new wxSocketClient;
|
|
|
|
|
m_map[strIP]->SetTimeout(2);
|
|
|
|
|
}
|
|
|
|
|
return *m_map[strIP];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wxSocketClient* TBordello::QuellaBuona()
|
|
|
|
|
{
|
|
|
|
|
wxSocketClient* good = NULL;
|
|
|
|
|
for(TBrothel::iterator it = m_map.begin(); it != m_map.end(); ++it )
|
|
|
|
|
{
|
|
|
|
|
wxSocketClient* sc = (wxSocketClient*)it->second;
|
|
|
|
|
if (sc != NULL)
|
|
|
|
|
{
|
|
|
|
|
sc->WaitOnConnect(0, 1);
|
|
|
|
|
if (sc->IsConnected())
|
|
|
|
|
{
|
|
|
|
|
good = sc;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return good;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TBordello::Chiudi()
|
|
|
|
|
{
|
|
|
|
|
for(TBrothel::iterator it = m_map.begin(); it != m_map.end(); ++it )
|
|
|
|
|
{
|
|
|
|
|
wxSocketClient* sc = (wxSocketClient*)it->second;
|
|
|
|
|
if (sc != NULL)
|
2010-03-11 15:28:12 +00:00
|
|
|
|
sc->Destroy();
|
2007-09-14 15:43:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TBordello::~TBordello()
|
|
|
|
|
{
|
|
|
|
|
/* Non funziona per oggetti statici
|
|
|
|
|
for(TBrothel::iterator it = m_map.begin(); it != m_map.end(); ++it )
|
|
|
|
|
{
|
|
|
|
|
wxSocketClient* sc = (wxSocketClient*)it->second;
|
|
|
|
|
sc->Destroy();
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
m_map.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-13 10:24:51 +00:00
|
|
|
|
static unsigned short InquireSocketNumber(wxSocketClient& sc, const wxString& strCommand)
|
|
|
|
|
{
|
2007-09-14 15:43:08 +00:00
|
|
|
|
sc.Discard();
|
|
|
|
|
sc.Write(strCommand, (wxUint32)strCommand.Len());
|
|
|
|
|
long number[2] = { 0L, 0L };
|
|
|
|
|
sc.Read(number, sizeof(number));
|
|
|
|
|
return (unsigned short)number[1];
|
2007-09-13 10:24:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-21 10:37:43 +00:00
|
|
|
|
//metodo per sapere dal server di chiave quanti sono gli utonti connessi;non funziona finch<63> ai servers non..
|
|
|
|
|
//..viene aggiunto il metodo NumberActiveUsers (<28> qui come segnaposto futuro)
|
2009-12-15 11:55:49 +00:00
|
|
|
|
unsigned short ServerLoggedUsers(const wxString& strSrvName)
|
2007-11-21 10:37:43 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned short nUtonti = 0;
|
|
|
|
|
wxIPV4address ip;
|
|
|
|
|
if (ip.Hostname(strSrvName) && ip.Service("1883")) //riesce a connettersi
|
|
|
|
|
{
|
|
|
|
|
wxSocketClient& sc = m_bordello.Zoccola(ip);
|
|
|
|
|
if (sc.IsConnected() || sc.Connect(ip)) //il computer remoto risponde
|
|
|
|
|
{
|
2009-12-15 11:55:49 +00:00
|
|
|
|
// quante licenze sono loggate al momento (utonti connessi)
|
|
|
|
|
nUtonti = InquireSocketNumber(sc, "DongleLoggedLicenses");
|
2007-11-21 10:37:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nUtonti;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-13 10:24:51 +00:00
|
|
|
|
static unsigned short InquireServer(const wxString& strSrvName, int& year, const bool bMsg)
|
|
|
|
|
{
|
2007-09-14 11:07:04 +00:00
|
|
|
|
unsigned short serno = 0xFFFF;
|
|
|
|
|
year = 0;
|
|
|
|
|
|
2007-09-13 10:24:51 +00:00
|
|
|
|
wxIPV4address ip;
|
|
|
|
|
if (!ip.Hostname(strSrvName) || !ip.Service("1883")) //indirizzo NON valido o porta scazzata!
|
|
|
|
|
{
|
|
|
|
|
if (bMsg)
|
|
|
|
|
ErrorBox("Indirizzo IP errato!");
|
2007-09-14 11:07:04 +00:00
|
|
|
|
return serno;
|
2007-09-13 10:24:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-14 15:43:08 +00:00
|
|
|
|
wxSocketClient& sc = m_bordello.Zoccola(ip);
|
|
|
|
|
if (sc.IsConnected() || sc.Connect(ip)) //il computer remoto risponde
|
2007-09-14 11:07:04 +00:00
|
|
|
|
{
|
2007-09-14 15:43:08 +00:00
|
|
|
|
// ma il server e' davvero acceso o spento ?
|
|
|
|
|
serno = InquireSocketNumber(sc, "DongleNumber()");
|
|
|
|
|
year = InquireSocketNumber(sc, "DongleYear()");
|
2007-09-14 11:07:04 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2007-09-13 10:24:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (bMsg)
|
2007-09-13 15:25:14 +00:00
|
|
|
|
{
|
|
|
|
|
wxString strMsg;
|
|
|
|
|
strMsg << "Il server " << strSrvName << " non e' raggiungibile!";
|
|
|
|
|
ErrorBox(strMsg);
|
|
|
|
|
}
|
2007-09-13 10:24:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned short ServerLogin(int& year, wxString& strSrvName)
|
|
|
|
|
{
|
|
|
|
|
unsigned short serno = 0xFFFF;
|
2007-09-13 15:25:14 +00:00
|
|
|
|
|
2007-09-13 10:24:51 +00:00
|
|
|
|
//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
|
2007-09-13 11:30:52 +00:00
|
|
|
|
wxIPV4address ipLocal;
|
|
|
|
|
ipLocal.AnyAddress();
|
|
|
|
|
wxString strMyName = ipLocal.Hostname();
|
|
|
|
|
ipLocal.Hostname(strMyName);
|
|
|
|
|
const wxString strMyIP = ipLocal.IPAddress();
|
|
|
|
|
|
|
|
|
|
if (strMyIP.Len() > 7)
|
2007-09-13 10:24:51 +00:00
|
|
|
|
{
|
2007-09-14 11:07:04 +00:00
|
|
|
|
const unsigned short MaxSck = 256;
|
2007-09-14 15:43:08 +00:00
|
|
|
|
for (int i = 0; i < MaxSck; i++)
|
2007-09-13 10:24:51 +00:00
|
|
|
|
{
|
2010-03-11 15:28:12 +00:00
|
|
|
|
if ((i+1) % 64 == 0) // ogni 16 zoccole controllo se ce n'e' una bbona.
|
|
|
|
|
{
|
|
|
|
|
wxSleep(1);
|
|
|
|
|
wxSocketClient* sc = m_bordello.QuellaBuona();
|
|
|
|
|
if (sc != NULL)
|
|
|
|
|
{
|
|
|
|
|
wxIPV4address ipRemote;
|
|
|
|
|
sc->GetPeer(ipRemote);
|
|
|
|
|
strSrvName = ipRemote.Hostname();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
m_bordello.Chiudi();
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-14 15:43:08 +00:00
|
|
|
|
//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 << "." << i; //ip del computer remoto
|
|
|
|
|
if (strSrvIP != strMyIP)
|
2007-09-13 11:30:52 +00:00
|
|
|
|
{
|
2007-09-14 15:43:08 +00:00
|
|
|
|
wxIPV4address ipRemote;
|
|
|
|
|
if (ipRemote.Hostname(strSrvIP) && ipRemote.Service("1883"))
|
2007-09-13 15:25:14 +00:00
|
|
|
|
{
|
2007-09-14 15:43:08 +00:00
|
|
|
|
wxSocketClient& sc = m_bordello.Zoccola(ipRemote);
|
|
|
|
|
if (!sc.IsConnected())
|
|
|
|
|
sc.Connect(ipRemote, ipLocal, false);
|
2007-09-13 15:25:14 +00:00
|
|
|
|
}
|
2007-09-13 11:30:52 +00:00
|
|
|
|
}
|
2007-09-14 15:43:08 +00:00
|
|
|
|
}
|
2010-03-11 15:28:12 +00:00
|
|
|
|
m_bordello.Chiudi();
|
2007-09-13 15:25:14 +00:00
|
|
|
|
} //if(strMyIP.Len()...
|
|
|
|
|
} //if(strSrvName.Is...
|
2007-09-14 15:43:08 +00:00
|
|
|
|
|
|
|
|
|
if (!strSrvName.IsEmpty())
|
2008-01-03 11:21:17 +00:00
|
|
|
|
serno = InquireServer(strSrvName, year, false);
|
2007-09-13 10:24:51 +00:00
|
|
|
|
|
|
|
|
|
return serno;
|
|
|
|
|
}
|
2007-09-14 11:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DictionaryLogin(const wxString& strSrvName)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|