#include "wxinc.h" #include "xvt.h" #include "wx/filename.h" #include "../ssa/h/ssadll.h" #include "../ssa/h/ssaerr.h" #include /////////////////////////////////////////////////////////// // TSSA_Pinger /////////////////////////////////////////////////////////// class TSSA_Pinger : public wxTimer { int m_nSerNo; bool m_bRemote; wxString m_strModule; wxString _id, _ini; protected: virtual void Notify(); bool IsRemote() const { return m_bRemote; } bool IsMenu() const; bool IsFreeModule(const wxString& mod) 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(); 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"; bool TSSA_Pinger::IsMenu() const { const wxFileName argv0 = __argv[0]; return argv0.GetName().IsSameAs("ba0", false); } void TSSA_Pinger::Notify() { SSA_Ping(_id); } bool TSSA_Pinger::LoginProduct() { bool bLoggedIn = IsRemote() && !IsMenu(); if (!bLoggedIn) { const int err = SSA_Login(_id, _ssa_product); bLoggedIn = err == 0; } if (bLoggedIn && m_nSerNo < 0) m_nSerNo = SSA_NumeroSerie(_ssa_product); if (bLoggedIn && IsRemote() && IsMenu()) ::WritePrivateProfileSection(_ssa_product, "\0\0", _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(_id, _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(_ini, _ssa_product, module, 0); nCount += nDelta; if (nCount < 0) nCount = 0; xvt_sys_set_profile_int(_ini, _ssa_product, module, nCount); return nCount; } bool TSSA_Pinger::IsFreeModule(const wxString& mod) const { return mod.IsEmpty() || mod=="ba" || mod=="pd" || mod=="ps"; } int TSSA_Pinger::Login(const char* mod) { if (mod == NULL || *mod <= ' ') return LoginProduct() ? Serial() : SSA_UTENTE_NON_LOGGATO; const wxString module = NormalizedModule(mod); if (IsFreeModule(module)) // Base o personalizzazione return 0; if (IsRemote() && AddRef(module) > 1) return 0; int err = SSA_ApriModulo(_id, module); if (err == SSA_UTENTE_NON_LOGGATO) // ritenta! { LoginProduct(); err = SSA_ApriModulo(_id, module); } if (err != 0 && *module >= 'a') err = SSA_ApriModulo(_id, 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 = !IsFreeModule(module); if (cm) { if (IsRemote()) cm = DecRef(module) <= 0; if (cm) err = SSA_ChiudiModulo(_id, module); } if (err == 0) LogoutProduct(); return err; } TSSA_Pinger::TSSA_Pinger() { wxFileName ini = xvt_fsys_get_campo_ini(); ini.SetName("ssacount"); ini.MakeAbsolute(); _ini = ini.GetFullPath(); 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)); _id.Printf("%s@%s:%d", user, host, sess); 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; }