1 | // ********************************************************************************************************
|
---|
2 | // Cliente: ogAdmWinClient
|
---|
3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla
|
---|
4 | // Fecha Creación: Febrero-2012
|
---|
5 | // Fecha Última modificación: Febrero-2012
|
---|
6 | // Nombre del fichero: ogAdmWinClient.cpp
|
---|
7 | // Descripción :Este fichero implementa el cliente windows del sistema
|
---|
8 | // ********************************************************************************************************
|
---|
9 | #include "ogAdmWinClient.h"
|
---|
10 | #include "ogAdmLib.c"
|
---|
11 | //________________________________________________________________________________________________________
|
---|
12 | // Función: tomaConfiguracion
|
---|
13 | //
|
---|
14 | // Descripción:
|
---|
15 | // Lee el fichero de configuración del servicio
|
---|
16 | // Parámetros:
|
---|
17 | // filecfg : Ruta completa al fichero de configuración
|
---|
18 | // Devuelve:
|
---|
19 | // TRUE: Si el proceso es correcto
|
---|
20 | // FALSE: En caso de ocurrir algún error
|
---|
21 | //________________________________________________________________________________________________________
|
---|
22 | BOOLEAN tomaConfiguracion(char* filecfg)
|
---|
23 | {
|
---|
24 | char modulo[] = "tomaConfiguracion()";
|
---|
25 |
|
---|
26 | if (filecfg == NULL || strlen(filecfg) == 0) {
|
---|
27 | errorLog(modulo, 1, FALSE); // Fichero de configuración del cliente vacío
|
---|
28 | return (FALSE);
|
---|
29 | }
|
---|
30 | FILE *fcfg;
|
---|
31 | int lSize;
|
---|
32 | char * buffer, *lineas[MAXPRM], *dualparametro[2];
|
---|
33 | int i, numlin, resul;
|
---|
34 |
|
---|
35 | fcfg = fopen(filecfg, "rt");
|
---|
36 | if (fcfg == NULL) {
|
---|
37 | errorLog(modulo, 2, FALSE); // No existe fichero de configuración del cliente
|
---|
38 | return (FALSE);
|
---|
39 | }
|
---|
40 |
|
---|
41 | fseek(fcfg, 0, SEEK_END);
|
---|
42 | lSize = ftell(fcfg); // Obtiene tamaño del fichero.
|
---|
43 | rewind(fcfg);
|
---|
44 | buffer = (char*) reservaMemoria(lSize+1); // Toma memoria para el buffer de lectura.
|
---|
45 | if (buffer == NULL) { // No hay memoria suficiente para el buffer
|
---|
46 | errorLog(modulo, 3, FALSE);
|
---|
47 | return (FALSE);
|
---|
48 | }
|
---|
49 | lSize=fread(buffer, 1, lSize, fcfg); // Lee contenido del fichero
|
---|
50 | buffer[lSize]=CHARNULL;
|
---|
51 | fclose(fcfg);
|
---|
52 |
|
---|
53 | /* Inicializar variables globales */
|
---|
54 | servidoradm[0]=CHARNULL;
|
---|
55 | puerto[0] = CHARNULL;
|
---|
56 | IPlocal[0]=CHARNULL;
|
---|
57 |
|
---|
58 | numlin = splitCadena(lineas, buffer, '\n');
|
---|
59 | for (i = 0; i < numlin; i++){
|
---|
60 | splitCadena(dualparametro, lineas[i], '=');
|
---|
61 |
|
---|
62 | resul = strcmp(StrToUpper(dualparametro[0]), "SERVIDORADM");
|
---|
63 | if (resul == 0)
|
---|
64 | strcpy(servidoradm, dualparametro[1]);
|
---|
65 |
|
---|
66 | resul = strcmp(StrToUpper(dualparametro[0]), "PUERTO");
|
---|
67 | if (resul == 0)
|
---|
68 | strcpy(puerto, dualparametro[1]);
|
---|
69 |
|
---|
70 | resul = strcmp(StrToUpper(dualparametro[0]), "IPLOCAL");
|
---|
71 | if (resul == 0)
|
---|
72 | strcpy(IPlocal, dualparametro[1]);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (servidoradm[0] == CHARNULL) {
|
---|
76 | errorLog(modulo,4, FALSE); // Falta parámetro SERVIDORADM
|
---|
77 | return (FALSE);
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (puerto[0] == CHARNULL) {
|
---|
81 | errorLog(modulo,5, FALSE); // Falta parámetro PUERTO
|
---|
82 | return (FALSE);
|
---|
83 | }
|
---|
84 | if (IPlocal[0] == CHARNULL) {
|
---|
85 | errorLog(modulo, 92, FALSE); // Falta parámetro IPLOCAL
|
---|
86 | return (FALSE);
|
---|
87 | }
|
---|
88 | return (TRUE);
|
---|
89 | }
|
---|
90 | //______________________________________________________________________________________________________
|
---|
91 | // Función: InclusionClienteWinLnx
|
---|
92 | // Descripción:
|
---|
93 | // Abre una sesión en el servidor de administración y registra al cliente en el sistema
|
---|
94 | // Parámetros:
|
---|
95 | // Ninguno
|
---|
96 | // Devuelve:
|
---|
97 | // TRUE: Si el proceso es correcto
|
---|
98 | // FALSE: En caso de ocurrir algún error
|
---|
99 | //______________________________________________________________________________________________________
|
---|
100 | BOOLEAN InclusionClienteWinLnx(TRAMA* ptrTrama)
|
---|
101 | {
|
---|
102 | int lon;
|
---|
103 | SOCKET socket_c;
|
---|
104 | char modulo[] = "InclusionClienteWinLnx()";
|
---|
105 |
|
---|
106 | initParametros(ptrTrama,0);
|
---|
107 | lon=sprintf(ptrTrama->parametros,"nfn=InclusionClienteWinLnx\r"); // Nombre de la función a ejecutar en el servidor
|
---|
108 |
|
---|
109 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_PETICION)){
|
---|
110 | errorLog(modulo,37,FALSE);
|
---|
111 | return(FALSE);
|
---|
112 | }
|
---|
113 | ptrTrama=recibeMensaje(&socket_c);
|
---|
114 | if(!ptrTrama){
|
---|
115 | errorLog(modulo,22,FALSE);
|
---|
116 | return(FALSE);
|
---|
117 | }
|
---|
118 | closesocket(socket_c);
|
---|
119 |
|
---|
120 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama
|
---|
121 | errorLog(modulo,39,FALSE);
|
---|
122 | return(FALSE);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return(TRUE);
|
---|
126 | }
|
---|
127 | //______________________________________________________________________________________________________
|
---|
128 | // Función: RESPUESTA_InclusionClienteWinLnx
|
---|
129 | //
|
---|
130 | // Descripción:
|
---|
131 | // Respuesta del servidor de administración a la petición de inicio
|
---|
132 | // enviando los datos identificativos del cliente
|
---|
133 | // Parámetros:
|
---|
134 | // - ptrTrama: Trama recibida por el servidor con el contenido y los parámetros
|
---|
135 | // Devuelve:
|
---|
136 | // TRUE: Si el proceso es correcto
|
---|
137 | // FALSE: En caso de ocurrir algún error
|
---|
138 | //______________________________________________________________________________________________________
|
---|
139 | BOOLEAN RESPUESTA_InclusionClienteWinLnx(TRAMA* ptrTrama)
|
---|
140 | {
|
---|
141 | char* res;
|
---|
142 | char modulo[] = "RESPUESTA_InclusionClienteWinLnx()";
|
---|
143 | int err;
|
---|
144 |
|
---|
145 | res=copiaParametro("res",ptrTrama); // Resultado del proceso de inclusión
|
---|
146 | err=(int)atoi(res); // Código de error devuelto por el servidor
|
---|
147 | if(err>0){ // Error en el proceso de inclusión
|
---|
148 | errorLog(modulo,41,FALSE);
|
---|
149 | errorLog(modulo,err,FALSE);
|
---|
150 | return (FALSE);
|
---|
151 | }
|
---|
152 | strcpy(idordenador,copiaParametro("ido",ptrTrama)); // Identificador del ordenador
|
---|
153 | strcpy(nombreordenador,copiaParametro("npc",ptrTrama)); // Nombre del ordenador
|
---|
154 |
|
---|
155 | if(idordenador==NULL || nombreordenador==NULL){
|
---|
156 | errorLog(modulo,40,FALSE);
|
---|
157 | return (FALSE);
|
---|
158 | }
|
---|
159 | return(TRUE);
|
---|
160 | }
|
---|
161 | //______________________________________________________________________________________________________
|
---|
162 | // Función: ProcesaComandos
|
---|
163 | //
|
---|
164 | // Descripción:
|
---|
165 | // Espera comando desde el Servidor de Administración para ejecutarlos
|
---|
166 | // Parámetros:
|
---|
167 | // Ninguno
|
---|
168 | // Devuelve:
|
---|
169 | // TRUE: Si el proceso es correcto
|
---|
170 | // FALSE: En caso de ocurrir algún error
|
---|
171 | // ________________________________________________________________________________________________________
|
---|
172 | void procesaComandos(TRAMA* ptrTrama)
|
---|
173 | {
|
---|
174 | int lon;
|
---|
175 | SOCKET socket_c;
|
---|
176 | char modulo[] = "procesaComandos()";
|
---|
177 |
|
---|
178 | initParametros(ptrTrama,0);
|
---|
179 | while(TRUE){
|
---|
180 | lon=sprintf(ptrTrama->parametros,"nfn=DisponibilidadComandos\r");
|
---|
181 | lon+=sprintf(ptrTrama->parametros+lon,"tpc=%s\r",CLIENTE_WIN); // Activar disponibilidad
|
---|
182 | if(!enviaMensajeServidor(&socket_c,ptrTrama,MSG_INFORMACION)){
|
---|
183 | errorLog(modulo,43,FALSE);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | infoLog(19); // Disponibilidad de cliente activada
|
---|
187 | ptrTrama=recibeMensaje(&socket_c);
|
---|
188 | if(!ptrTrama){
|
---|
189 | errorLog(modulo,46,FALSE);
|
---|
190 | return;
|
---|
191 | }
|
---|
192 |
|
---|
193 | closesocket(socket_c);
|
---|
194 |
|
---|
195 | if(!gestionaTrama(ptrTrama)){ // Análisis de la trama
|
---|
196 | errorLog(modulo,39,FALSE);
|
---|
197 | return;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | //_____________________________________________________________________________________________________
|
---|
202 | // Función: Apagar
|
---|
203 | //
|
---|
204 | // Descripción:
|
---|
205 | // Apaga el cliente
|
---|
206 | // Parámetros:
|
---|
207 | // ptrTrama: contenido del mensaje
|
---|
208 | // Devuelve:
|
---|
209 | // TRUE: Si el proceso es correcto
|
---|
210 | // FALSE: En caso de ocurrir algún error
|
---|
211 | //_____________________________________________________________________________________________________
|
---|
212 | BOOLEAN Apagar(TRAMA* ptrTrama)
|
---|
213 | {
|
---|
214 | int lon;
|
---|
215 | char *ids,msglog[LONSTD];
|
---|
216 | char modulo[] = "Apagar()";
|
---|
217 |
|
---|
218 | if (ndebug>=DEBUG_MAXIMO) {
|
---|
219 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo);
|
---|
220 | infoDebug(msglog);
|
---|
221 | }
|
---|
222 | ids=copiaParametro("ids",ptrTrama);
|
---|
223 |
|
---|
224 | initParametros(ptrTrama,0);
|
---|
225 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Apagar");
|
---|
226 | respuestaEjecucionComando(ptrTrama,0,ids);
|
---|
227 |
|
---|
228 | if(versionWin<5)
|
---|
229 | ApagarReiniciar(EWX_POWEROFF | EWX_FORCE,ids,0);
|
---|
230 | else
|
---|
231 | ApagarReiniciar(EWX_SHUTDOWN | EWX_FORCE,ids,0);
|
---|
232 | return(TRUE);
|
---|
233 | }
|
---|
234 | //_____________________________________________________________________________________________________
|
---|
235 | // Función: Reiniciar
|
---|
236 | //
|
---|
237 | // Descripción:
|
---|
238 | // Apaga el cliente
|
---|
239 | // Parámetros:
|
---|
240 | // ptrTrama: contenido del mensaje
|
---|
241 | // Devuelve:
|
---|
242 | // TRUE: Si el proceso es correcto
|
---|
243 | // FALSE: En caso de ocurrir algún errorservidoradm
|
---|
244 | //_____________________________________________________________________________________________________
|
---|
245 | BOOLEAN Reiniciar(TRAMA* ptrTrama)
|
---|
246 | {
|
---|
247 | int lon;
|
---|
248 | char *ids,msglog[LONSTD];
|
---|
249 | char modulo[] = "Reiniciar()";
|
---|
250 |
|
---|
251 | if (ndebug>=DEBUG_MAXIMO) {
|
---|
252 | sprintf(msglog, "%s:%s",tbMensajes[21],modulo);
|
---|
253 | infoDebug(msglog);
|
---|
254 | }
|
---|
255 | ids=copiaParametro("ids",ptrTrama);
|
---|
256 |
|
---|
257 | initParametros(ptrTrama,0);
|
---|
258 | lon=sprintf(ptrTrama->parametros,"nfn=%s\r","RESPUESTA_Reiniciar");
|
---|
259 | respuestaEjecucionComando(ptrTrama,0,ids);
|
---|
260 |
|
---|
261 | if(versionWin<5)
|
---|
262 | ApagarReiniciar(EWX_REBOOT | EWX_FORCE,ids,1);
|
---|
263 | else
|
---|
264 | ApagarReiniciar(EWX_REBOOT | EWX_FORCE,ids,1);
|
---|
265 |
|
---|
266 | return(TRUE);
|
---|
267 | }
|
---|
268 | // _____________________________________________________________________________________________________________
|
---|
269 | //
|
---|
270 | // Función: ApagarReiniciar
|
---|
271 | //
|
---|
272 | // Descripción:
|
---|
273 | // Apaga o reinicia el ordenador o bien hace logout del usuario
|
---|
274 | //
|
---|
275 | // Parámetros:
|
---|
276 | // - uFlags : Especifica el tipo de shutdown
|
---|
277 | // _____________________________________________________________________________________________________________
|
---|
278 | BOOLEAN ApagarReiniciar(UINT uFlags,char *ids,int sw)
|
---|
279 | {
|
---|
280 | HANDLE hToken;
|
---|
281 | TOKEN_PRIVILEGES tkp;
|
---|
282 | char modulo[] = "Reiniciar()";
|
---|
283 |
|
---|
284 | if (versionWin>4){
|
---|
285 | if (!ExitWindowsEx(uFlags, 0)) {
|
---|
286 | errorLog(modulo,86,FALSE);
|
---|
287 | return(FALSE);
|
---|
288 | }
|
---|
289 | return TRUE;
|
---|
290 | }
|
---|
291 | // Get a token for this process.
|
---|
292 | if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
|
---|
293 | errorLog(modulo,86,FALSE);
|
---|
294 | return(FALSE);
|
---|
295 | }
|
---|
296 |
|
---|
297 | // Get the LUID for the shutdown privilege.
|
---|
298 | LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
|
---|
299 |
|
---|
300 | tkp.PrivilegeCount = 1; // one privilege to set
|
---|
301 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
302 |
|
---|
303 | // Get the shutdown privilege for this process.
|
---|
304 | AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
|
---|
305 |
|
---|
306 | if (GetLastError() != ERROR_SUCCESS) {
|
---|
307 | errorLog(modulo,86,FALSE);
|
---|
308 | return(FALSE);
|
---|
309 | }
|
---|
310 |
|
---|
311 | // Shut down the system and force all applications to close.
|
---|
312 | if (!ExitWindowsEx(uFlags, 0)) {
|
---|
313 | errorLog(modulo,86,FALSE);
|
---|
314 | return(FALSE);
|
---|
315 | }
|
---|
316 |
|
---|
317 | return TRUE;
|
---|
318 | }
|
---|
319 | // _____________________________________________________________________________________________________________
|
---|
320 | //
|
---|
321 | // Función: TomaVersionWindows
|
---|
322 | //
|
---|
323 | // Descripción:
|
---|
324 | // Toma la versión del sistema operativo
|
---|
325 | //
|
---|
326 | // Valores de retorno:
|
---|
327 | // 1.- Microsoft Windows Server 2003
|
---|
328 | // 2.- Microsoft Windows XP
|
---|
329 | // 3.- Microsoft Windows 2000
|
---|
330 | // 4.- Microsoft Windows NT
|
---|
331 | // 5.- Microsoft Windows 95
|
---|
332 | // 6.- Microsoft Windows 98
|
---|
333 | // 7.- Microsoft Windows Millennium Edition
|
---|
334 | // _____________________________________________________________________________________________________________
|
---|
335 | int TomaVersionWindows()
|
---|
336 | {
|
---|
337 | OSVERSIONINFOEX osvi;
|
---|
338 | BOOL bOsVersionInfoEx;
|
---|
339 |
|
---|
340 | // Intenta tomar la version usando la estructura OSVERSIONINFOEX
|
---|
341 | // Si falla lo intentausando la estructura OSVERSIONINFO.
|
---|
342 |
|
---|
343 | ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
|
---|
344 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
---|
345 |
|
---|
346 | if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ){
|
---|
347 | osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
|
---|
348 | if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
|
---|
349 | return -1;
|
---|
350 | }
|
---|
351 | switch (osvi.dwPlatformId){
|
---|
352 | // Test for the Windows NT product family.
|
---|
353 | case VER_PLATFORM_WIN32_NT:
|
---|
354 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
|
---|
355 | return(1); // Microsoft Windows Server 2003
|
---|
356 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
|
---|
357 | return(2); // Microsoft Windows XP
|
---|
358 | if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
|
---|
359 | return(3); // Microsoft Windows 2000
|
---|
360 | if ( osvi.dwMajorVersion <= 4 )
|
---|
361 | return(4); // Microsoft Windows NT
|
---|
362 | break;
|
---|
363 |
|
---|
364 | // Test for the Windows Me/98/95.
|
---|
365 | case VER_PLATFORM_WIN32_WINDOWS:
|
---|
366 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
|
---|
367 | return(5); // Microsoft Windows 95
|
---|
368 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
|
---|
369 | return(6); // Microsoft Windows 98
|
---|
370 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
|
---|
371 | return(7); // Microsoft Windows Millennium Edition
|
---|
372 | break;
|
---|
373 | }
|
---|
374 | return -1;
|
---|
375 | }
|
---|
376 | //______________________________________________________________________________________________________
|
---|
377 | // Función: Sondeo
|
---|
378 | //
|
---|
379 | // Descripción:
|
---|
380 | // Envía al servidor una confirmación de que está dentro del sistema
|
---|
381 | // Parámetros:
|
---|
382 | // ptrTrama: contenido del mensajede
|
---|
383 | // Devuelve:
|
---|
384 | // TRUE: Si el proceso es correcto
|
---|
385 | // FALSE: En caso de ocurrir algún error
|
---|
386 | //______________________________________________________________________________________________________
|
---|
387 | BOOLEAN Sondeo(TRAMA* ptrTrama)
|
---|
388 | {
|
---|
389 | return(TRUE);
|
---|
390 | }
|
---|
391 | //______________________________________________________________________________________________________
|
---|
392 | // Función: Actualizar
|
---|
393 | //
|
---|
394 | // Descripción:
|
---|
395 | // Envía al servidor una confirmación de que está dentro del sistema
|
---|
396 | // Parámetros:
|
---|
397 | // ptrTrama: contenido del mensajede
|
---|
398 | // Devuelve:
|
---|
399 | // TRUE: Si el proceso es correcto
|
---|
400 | // FALSE: En caso de ocurrir algún error
|
---|
401 | //______________________________________________________________________________________________________
|
---|
402 | BOOLEAN Actualizar(TRAMA* ptrTrama)
|
---|
403 | {
|
---|
404 | return(TRUE);
|
---|
405 | }
|
---|
406 | //______________________________________________________________________________________________________
|
---|
407 | // Función: respuestaEjecucionComando
|
---|
408 | //
|
---|
409 | // Descripción:
|
---|
410 | // Envia una respuesta a una ejecucion de comando al servidor de Administración
|
---|
411 | // Parámetros:
|
---|
412 | // - ptrTrama: contenido del mensaje
|
---|
413 | // - res: Resultado de la ejecución (Código de error devuelto por el script ejecutado)
|
---|
414 | // - ids: Identificador de la sesion (En caso de no haber seguimiento es NULO)
|
---|
415 | // Devuelve:
|
---|
416 | // TRUE: Si el proceso es correcto
|
---|
417 | // FALSE: En caso de ocurrir algún error
|
---|
418 | // ________________________________________________________________________________________________________
|
---|
419 | BOOLEAN respuestaEjecucionComando(TRAMA* ptrTrama,int res,char *ids)
|
---|
420 | {
|
---|
421 | int lon;
|
---|
422 | SOCKET socket_c;
|
---|
423 | char modulo[] = "respuestaEjecucionComando()";
|
---|
424 |
|
---|
425 | lon=strlen(ptrTrama->parametros);
|
---|
426 | if(ids){ // Existe seguimiento
|
---|
427 | lon+=sprintf(ptrTrama->parametros+lon,"ids=%s\r",ids); // Añade identificador de la sesión
|
---|
428 | }
|
---|
429 | if (res==0){ // Resultado satisfactorio
|
---|
430 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","1");
|
---|
431 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r","");
|
---|
432 | }
|
---|
433 | else{ // Algún error
|
---|
434 | lon+=sprintf(ptrTrama->parametros+lon,"res=%s\r","2");
|
---|
435 | lon+=sprintf(ptrTrama->parametros+lon,"der=%s\r",tbErrores[res]);// Descripción del error
|
---|
436 | }
|
---|
437 | if(!(enviaMensajeServidor(&socket_c,ptrTrama,MSG_NOTIFICACION))){
|
---|
438 | errorLog(modulo,44,FALSE);
|
---|
439 | return(FALSE);
|
---|
440 | }
|
---|
441 | closesocket(socket_c);
|
---|
442 | return(TRUE);
|
---|
443 | }
|
---|
444 | // ________________________________________________________________________________________________________
|
---|
445 | // Función: gestionaTrama
|
---|
446 | //
|
---|
447 | // Descripción:
|
---|
448 | // Procesa las tramas recibidas.servidoradm
|
---|
449 | // Parametros:
|
---|
450 | // ptrTrama: contenido del mensaje
|
---|
451 | // Devuelve:
|
---|
452 | // TRUE: Si el proceso es correcto
|
---|
453 | // FALSE: En caso de ocurrir algún error
|
---|
454 | // ________________________________________________________________________________________________________
|
---|
455 | BOOLEAN gestionaTrama(TRAMA *ptrTrama)
|
---|
456 | {
|
---|
457 | int i, res;
|
---|
458 | char *nfn;
|
---|
459 | char modulo[] = "gestionaTrama()";
|
---|
460 |
|
---|
461 | INTROaFINCAD(ptrTrama);
|
---|
462 | nfn = copiaParametro("nfn", ptrTrama); // Toma nombre de función
|
---|
463 | for (i = 0; i < MAXIMAS_FUNCIONES; i++) { // Recorre funciones que procesan las tramas
|
---|
464 | res = strcmp(tbfuncionesClient[i].nf, nfn);
|
---|
465 | if (res == 0) { // Encontrada la función que procesa el mensaje
|
---|
466 | return(tbfuncionesClient[i].fptr(ptrTrama)); // Invoca la función
|
---|
467 | }
|
---|
468 | }
|
---|
469 | errorLog(modulo, 18, FALSE);
|
---|
470 | return (FALSE);
|
---|
471 | }
|
---|
472 | //______________________________________________________________________________________________________
|
---|
473 | // Función: enviaMensajeServidor
|
---|
474 | //
|
---|
475 | // Descripción:
|
---|
476 | // Envia un mensaje al servidor de Administración
|
---|
477 | // Parámetros:
|
---|
478 | // - socket_c: (Salida) Socket utilizado para el envío
|
---|
479 | // - ptrTrama: contenido del mensaje
|
---|
480 | // - tipo: Tipo de mensaje
|
---|
481 | // C=Comando, N=Respuesta a un comando, P=Peticion,R=Respuesta a una petición, I=Informacion
|
---|
482 | // Devuelve:
|
---|
483 | // TRUE: Si el proceso es correcto
|
---|
484 | // FALSE: En caso de ocurrir algún error
|
---|
485 | // ________________________________________________________________________________________________________
|
---|
486 | BOOLEAN enviaMensajeServidor(SOCKET *socket_c,TRAMA *ptrTrama,char tipo)
|
---|
487 | {
|
---|
488 | int lon;
|
---|
489 | char modulo[] = "enviaMensajeServidor()";
|
---|
490 |
|
---|
491 | *socket_c=abreConexion();
|
---|
492 | if(*socket_c==INVALID_SOCKET){
|
---|
493 | errorLog(modulo,38,FALSE); // Error de conexión con el servidor
|
---|
494 | return(FALSE);
|
---|
495 | }
|
---|
496 | ptrTrama->arroba='@'; // Cabecera de la trama
|
---|
497 | strncpy(ptrTrama->identificador,"JMMLCAMDJ_MCDJ",14); // identificador de la trama
|
---|
498 | ptrTrama->tipo=tipo; // Tipo de mensaje
|
---|
499 | lon=strlen(ptrTrama->parametros); // Compone la trama
|
---|
500 | lon+=sprintf(ptrTrama->parametros+lon,"iph=%s\r",IPlocal); // Ip del ordenador
|
---|
501 | lon+=sprintf(ptrTrama->parametros+lon,"ido=%s\r",idordenador); // Identificador del ordenador
|
---|
502 | lon+=sprintf(ptrTrama->parametros+lon,"npc=%s\r",nombreordenador); // Nombre del ordenador
|
---|
503 |
|
---|
504 | if (!mandaTrama(socket_c,ptrTrama)) {
|
---|
505 | errorLog(modulo,26,FALSE);
|
---|
506 | return (FALSE);
|
---|
507 | }
|
---|
508 | return(TRUE);
|
---|
509 | }
|
---|
510 | // ********************************************************************************************************
|
---|
511 | // PROGRAMA PRINCIPAL (CLIENTE)
|
---|
512 | // ********************************************************************************************************
|
---|
513 | int main(int argc, char* argv[])
|
---|
514 | {
|
---|
515 | TRAMA *ptrTrama;
|
---|
516 | char modulo[] = "main()";
|
---|
517 |
|
---|
518 | #ifdef __WINDOWS__
|
---|
519 | WSADATA wsd;
|
---|
520 | if (WSAStartup(MAKEWORD(2,2),&wsd)!=0){ // Carga librería Winsock
|
---|
521 | errorLog(modulo, 93, FALSE);
|
---|
522 | exit(EXIT_FAILURE);
|
---|
523 | }
|
---|
524 | #endif
|
---|
525 |
|
---|
526 | ptrTrama=(TRAMA *)reservaMemoria(sizeof(TRAMA));
|
---|
527 | if (ptrTrama == NULL) { // No hay memoria suficiente para el bufer de las tramas
|
---|
528 | errorLog(modulo, 3, FALSE);
|
---|
529 | exit(EXIT_FAILURE);
|
---|
530 | }
|
---|
531 | /*--------------------------------------------------------------------------------------------------------
|
---|
532 | Validación de parámetros de ejecución y fichero de configuración
|
---|
533 | ---------------------------------------------------------------------------------------------------------*/
|
---|
534 | if (!validacionParametros(argc, argv,6)) // Valida parámetros de ejecución
|
---|
535 | exit(EXIT_FAILURE);
|
---|
536 |
|
---|
537 | if (!tomaConfiguracion(szPathFileCfg)) // Toma parametros de configuración
|
---|
538 | exit(EXIT_FAILURE);
|
---|
539 |
|
---|
540 | versionWin=TomaVersionWindows(); // Toma versión de windows
|
---|
541 |
|
---|
542 |
|
---|
543 | /*--------------------------------------------------------------------------------------------------------
|
---|
544 | Carga catálogo de funciones que procesan las tramas
|
---|
545 | ---------------------------------------------------------------------------------------------------------*/
|
---|
546 | int cf = 0;
|
---|
547 |
|
---|
548 | strcpy(tbfuncionesClient[cf].nf, "RESPUESTA_InclusionClienteWinLnx");
|
---|
549 | tbfuncionesClient[cf++].fptr = &RESPUESTA_InclusionClienteWinLnx;
|
---|
550 |
|
---|
551 | strcpy(tbfuncionesClient[cf].nf, "Apagar");
|
---|
552 | tbfuncionesClient[cf++].fptr = &Apagar;
|
---|
553 |
|
---|
554 | strcpy(tbfuncionesClient[cf].nf, "Reiniciar");
|
---|
555 | tbfuncionesClient[cf++].fptr = &Reiniciar;
|
---|
556 |
|
---|
557 | strcpy(tbfuncionesClient[cf].nf, "Sondeo");
|
---|
558 | tbfuncionesClient[cf++].fptr = &Sondeo;
|
---|
559 |
|
---|
560 | strcpy(tbfuncionesClient[cf].nf, "Actualizar");
|
---|
561 | tbfuncionesClient[cf++].fptr = &Actualizar;
|
---|
562 |
|
---|
563 | /*--------------------------------------------------------------------------------------------------------
|
---|
564 | Inicio de sesión
|
---|
565 | ---------------------------------------------------------------------------------------------------------*/
|
---|
566 | infoLog(1); // Inicio de sesión
|
---|
567 | infoLog(3); // Abriendo sesión en el servidor de Administración;
|
---|
568 | /*--------------------------------------------------------------------------------------------------------
|
---|
569 | Inclusión del cliente en el sistema
|
---|
570 | ---------------------------------------------------------------------------------------------------------*/
|
---|
571 | if(!InclusionClienteWinLnx(ptrTrama)){ // Ha habido algún problema al abrir sesión
|
---|
572 | errorLog(modulo,0,FALSE);
|
---|
573 | exit(EXIT_FAILURE);
|
---|
574 | }
|
---|
575 | infoLog(4); // Cliente iniciado
|
---|
576 | procesaComandos(ptrTrama); // Bucle para procesar comandos interactivos
|
---|
577 | return(EXIT_SUCCESS);
|
---|
578 | }
|
---|