214 lines
5.3 KiB
C++
214 lines
5.3 KiB
C++
#include "StdAfx.h"
|
|
#include "LicCliente.h"
|
|
#include "b_file.h"
|
|
#include "Csock_cl.h"
|
|
//*********************************************************************************************************************
|
|
LicCliente::LicCliente(void)
|
|
{
|
|
lic.clear();
|
|
pathf[0]=0;
|
|
}
|
|
//*********************************************************************************************************************
|
|
LicCliente::~LicCliente(void)
|
|
{
|
|
}
|
|
//*********************************************************************************************************************
|
|
bool LicCliente::cargaLicencia( LicClientEscucha* escucha, char* exePath )
|
|
{
|
|
this->escucha = escucha;
|
|
lic.clear();
|
|
pathf[0]=0;
|
|
if(!lic.fillLic(exePath))
|
|
{
|
|
escucha->muestaError("Error, no se puede inicializar licencia", lic.nombre_producto);
|
|
return false;
|
|
}
|
|
//revisa archivo--------------
|
|
if(!cargaArchivo())
|
|
{
|
|
if(!escucha->getClave(lic.clave, lic.nombre_producto))
|
|
return false;
|
|
}
|
|
|
|
if(!lic.clave[0])
|
|
{
|
|
lic.id = -1;
|
|
goto fin;
|
|
}
|
|
lic.instaVersion[0]=0;
|
|
//conecta---------------------
|
|
conectaServer();
|
|
fin:
|
|
if(lic.id<0)
|
|
{
|
|
lic.permisos =0;
|
|
escucha->muestaError("Sin licencia para este producto", lic.nombre_producto);
|
|
borraFileLic();
|
|
return false;
|
|
}
|
|
grabaFile();
|
|
if(lic.instaVersion[0] && strcmp(lic.instaVersion, lic.version_producto))
|
|
{
|
|
if(!escucha->descargaInstalador(&lic))
|
|
return false;
|
|
}
|
|
|
|
return lic.id >=0 ;
|
|
}
|
|
//*********************************************************************************************************************
|
|
bool LicCliente::cargaArchivo()
|
|
{
|
|
Cb_file f;
|
|
char str[128];
|
|
if(lic.nombre_producto[0])
|
|
sprintf(str,"%s.lic",lic.nombre_producto);
|
|
else
|
|
strcpy(str,LU_DEFAULT_FILE);
|
|
if(!f.abre(str,1))
|
|
{
|
|
pathf[0]=0;
|
|
if(!escucha->getPathLic(pathf, lic.nombre_producto)|| !pathf[0] || !f.abre(pathf,1))
|
|
{
|
|
if(!pathf[0])
|
|
strcpy(pathf, str);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
strcpy(pathf, str);
|
|
Licencia l;
|
|
if(!l.leeLic(&f))
|
|
return false;
|
|
if(l.tipo == 2)//licencia propietario
|
|
{
|
|
l.fillidmYuser();
|
|
}
|
|
if(strcmp(l.idm, lic.idm)|| strcmp(l.nombre_producto, lic.nombre_producto))
|
|
return false;
|
|
strcpy(l.version_producto, lic.version_producto);
|
|
lic = l;
|
|
lic.id;
|
|
//comprueba fechas----------------
|
|
if(lic.tipo == 1)
|
|
{
|
|
//CTime tiempo = CTime::GetCurrentTime();
|
|
__int64 tact = CTime::GetCurrentTime().GetTime();
|
|
if(lic.caducidad<=tact || (lic.ultimo_acceso+lic.renovacion)<=tact)
|
|
lic.id = -1;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//*********************************************************************************************************************
|
|
void LicCliente::grabaFile()
|
|
{
|
|
Cb_file f;
|
|
if(!f.abre(pathf,2) || !lic.grabaLic(&f))
|
|
escucha->muestaError("Error al grabar licencia", lic.nombre_producto);
|
|
}
|
|
//*********************************************************************************************************************
|
|
void LicCliente::borraFileLic()
|
|
{
|
|
DeleteFile(pathf);
|
|
}
|
|
//*********************************************************************************************************************
|
|
void LicCliente::conectaServer()
|
|
{
|
|
Csock_cl* sc = new Csock_cl();
|
|
BYTE bufout[sizeof(int)+sizeof(Licencia)];
|
|
int fin =PETICION_GENERAL_NO;
|
|
|
|
int idp;
|
|
int nb ;
|
|
int ii=0;
|
|
char LicServerUrl[128];
|
|
int puerto = lic.puertoServerLic;
|
|
strcpy(LicServerUrl, lic.LicServerUrl);
|
|
for(int ii =0; ii<2; ii++)
|
|
{
|
|
sc->conectar(LicServerUrl, puerto);
|
|
if(envia_ok(sc))
|
|
break;
|
|
if(!escucha->getServer(LicServerUrl, &puerto, lic.nombre_producto))
|
|
return;
|
|
|
|
}
|
|
|
|
if(ii>=2)
|
|
{
|
|
delete sc;
|
|
return;
|
|
}
|
|
|
|
lic.puertoServerLic =puerto;
|
|
strcpy(lic.LicServerUrl, LicServerUrl);
|
|
(*(int*)bufout) = PETICION_GENERAL_PRESENTA;
|
|
lic.getLic(&bufout[sizeof(int)]);
|
|
if(!sc->envia_package(bufout,sizeof(int)+sizeof(Licencia)))
|
|
{
|
|
delete sc;
|
|
return;
|
|
}
|
|
if(!sc->recibe_package(0))
|
|
{
|
|
delete sc;
|
|
return;
|
|
}
|
|
idp = *(int*)sc->buf;
|
|
nb =sc->nb-(sizeof(int));
|
|
|
|
void* buf = &((int*)sc->buf)[1];
|
|
sc->envia_package((BYTE*)&fin, sizeof(fin));
|
|
|
|
if(nb<sizeof(Licencia) || idp != PETICION_GENERAL_OK)
|
|
{
|
|
if( idp != PETICION_GENERAL_OK)
|
|
lic.id = -1;
|
|
delete sc;
|
|
return;
|
|
}
|
|
|
|
lic.setLic(buf);
|
|
delete sc;
|
|
}
|
|
//*********************************************************************************************************************
|
|
bool LicCliente::envia_ok(Csock_cl* sc)
|
|
{
|
|
int pok=PETICION_GENERAL_OK;
|
|
if(!sc->envia_package((BYTE*)&pok, sizeof(int)))
|
|
{
|
|
return false;
|
|
}
|
|
if(!sc->recibe_package(0))
|
|
{
|
|
return false;
|
|
}
|
|
return (sc->nb == sizeof(int)) && (*(int*)sc->buf == pok);
|
|
}
|
|
//*********************************************************************************************************************
|
|
bool LicCliente::isLicencia()
|
|
{
|
|
return lic.id >=0;
|
|
}
|
|
//*********************************************************************************************************************
|
|
int LicCliente::getPermisos()
|
|
{
|
|
return lic.permisos;
|
|
}
|
|
//*********************************************************************************************************************
|
|
bool LicCliente::creaLicPropietaria( char *key, int tipo, char *path )
|
|
{
|
|
lic.clear();
|
|
lic.fillLic(path);
|
|
|
|
strcpy(lic.clave, key);
|
|
lic.tipo = tipo;
|
|
lic.idm[0]=0;
|
|
lic.id = 0;
|
|
lic.nombre_user[0]=0;
|
|
sprintf(pathf,"%s.lic", lic.nombre_producto);
|
|
grabaFile();
|
|
return true;
|
|
}
|
|
//*********************************************************************************************************************
|