campo-sirio/xvaga/xvt_ssa.cpp

228 lines
5.1 KiB
C++
Raw Normal View History

#include "wxinc.h"
#include "xvt.h"
#include "wx/filename.h"
#include "../ssa/h/ssadll.h"
#include "../ssa/h/ssaerr.h"
#include <errno.h>
///////////////////////////////////////////////////////////
// TSSA_Pinger
///////////////////////////////////////////////////////////
class TSSA_Pinger : public wxTimer
{
int m_nSerNo;
bool m_bRemote;
wxString m_strModule;
protected:
virtual void Notify();
bool IsRemote() const { return m_bRemote; }
bool IsMenu() const;
int AddRef(const wxString& m, int nDelta = +1);
int DecRef(const wxString& m) { return AddRef(m, -1); }
wxString NormalizedModule(const char* m) const;
bool LoginProduct();
bool LogoutProduct();
const char* UserId() const;
public:
int Login(const char* module);
int Serial() const { return m_nSerNo; }
int Logout(const char* module);
TSSA_Pinger();
};
static TSSA_Pinger* _ssa_timer = NULL;
static const char* const _ssa_product = "CAMPO";
const char* TSSA_Pinger::UserId() const
{
static char id[256] = { 0 };
if (!*id)
{
const int sess = xvt_sys_get_session_id();
char user[64], host[64];
xvt_sys_get_user_name(user, sizeof(user));
xvt_sys_get_host_name(host, sizeof(host));
sprintf_s(id, sizeof(id), "%s@%s:%d", user, host, sess);
}
return id;
}
bool TSSA_Pinger::IsMenu() const
{
const wxFileName argv0 = __argv[0];
return argv0.GetName().IsSameAs("ba0", false);
}
void TSSA_Pinger::Notify()
{ SSA_Ping(UserId()); }
bool TSSA_Pinger::LoginProduct()
{
bool bLoggedIn = IsRemote() && !IsMenu();
if (!bLoggedIn)
{
const int err = SSA_Login(UserId(), _ssa_product);
bLoggedIn = err == 0;
}
if (bLoggedIn && m_nSerNo < 0)
m_nSerNo = SSA_NumeroSerie(_ssa_product);
if (bLoggedIn && IsRemote() && IsMenu())
::WritePrivateProfileSection(UserId(), "\0\0", "./ssa.ini"); // Azzera tutti i conteggi dei moduli
return bLoggedIn;
}
bool TSSA_Pinger::LogoutProduct()
{
m_nSerNo = SSA_UTENTE_NON_LOGGATO;
if (IsRemote() && !IsMenu())
return true;
int err = SSA_Logout(UserId(), _ssa_product);
return err == 0;
}
wxString TSSA_Pinger::NormalizedModule(const char* m) const
{
wxString module;
if (m && *m)
{
module = m;
module.Trim();
module.MakeLower();
module.Truncate(2);
}
return module;
}
int TSSA_Pinger::AddRef(const wxString& module, int nDelta)
{
int nCount = xvt_sys_get_profile_int("./ssa.ini", UserId(), module, 0);
nCount += nDelta;
if (nCount < 0) nCount = 0;
xvt_sys_set_profile_int("./ssa.ini", UserId(), module, nCount);
return nCount;
}
int TSSA_Pinger::Login(const char* mod)
{
if (mod == NULL || *mod == '\0')
return LoginProduct() ? Serial() : SSA_UTENTE_NON_LOGGATO;
const wxString module = NormalizedModule(mod);
if (module == "ba" || module == "pd" || module == "ps") // Base o personalizzazione
return 0;
if (IsRemote() && AddRef(module) > 1)
return 0;
const char* uid = UserId();
int err = SSA_ApriModulo(uid, module);
if (err == SSA_UTENTE_NON_LOGGATO) // ritenta!
{
LoginProduct();
err = SSA_ApriModulo(uid, module);
}
if (err != 0 && *module >= 'a')
err = SSA_ApriModulo(uid, module.Upper());
if (IsRemote() && err != 0)
DecRef(module);
if (err == 0)
m_strModule = module;
return err;
}
int TSSA_Pinger::Logout(const char* mod)
{
const wxString module = mod == NULL ? m_strModule : NormalizedModule(mod);
int err= 0;
bool cm = !module.IsEmpty() && module != "ba";
if (cm)
{
if (IsRemote())
cm = DecRef(module) <= 0;
if (cm)
err = SSA_ChiudiModulo(UserId(), module);
}
if (err == 0)
LogoutProduct();
return err;
}
TSSA_Pinger::TSSA_Pinger()
{
char ssaagent[128] = { 0 };
const int len = xvt_sys_get_profile_string("ssa.ini", "", "SSA-PORT", "", ssaagent, sizeof(ssaagent));
m_bRemote = len > 8;
m_nSerNo = -1;
}
///////////////////////////////////////////////////////////
// xvt_dongle_sa_...
///////////////////////////////////////////////////////////
int xvt_dongle_sa_login(const char* module)
{
if (_ssa_timer == NULL)
_ssa_timer = new TSSA_Pinger;
return _ssa_timer->Login(module);
}
int xvt_dongle_sa_crypt(unsigned short* data)
{
if (_ssa_timer == NULL)
return SSA_UTENTE_NON_LOGGATO;
if (data == NULL)
return -EACCES;
data[0] ^= 0xDEAD;
data[1] ^= 0xBEEF;
data[2] ^= 0xDEAD;
data[3] ^= 0xBEEF;
return 0;
}
int xvt_dongle_sa_logout(const char* module)
{
int err = SSA_UTENTE_NON_LOGGATO;
if (_ssa_timer != NULL)
err = _ssa_timer->Logout(module); // logout
return err;
}
int xvt_dongle_sa_test(const char* module)
{
int err = SSA_PROD_NOTFOUND;
if (module && *module && *module != '?')
{
wxString p = _ssa_product;
wxString m = module;
const int dot = m.Find('.');
if (dot > 0)
{
p = m.Left(dot);
m = m.Mid(dot+1);
}
err = SSA_VerificaModulo(p, m);
if (err <= SSA_MOD_NOTFOUND && m[0] >= 'a')
{
m.MakeUpper();
err = SSA_VerificaModulo(p, m);
}
}
return err;
}