1 | //**************************************************************************************************************************************************** |
---|
2 | // Aplicación OpenGNSys |
---|
3 | // Autor: José Manuel Alonso. |
---|
4 | // Licencia: Open Source |
---|
5 | // Fichero: ogAdmServer.cpp |
---|
6 | // Descripción: |
---|
7 | // Este módulo de la aplicación OpenGNSys implementa las comunicaciones con el Repositorio. |
---|
8 | // **************************************************************************************************************************************************** |
---|
9 | #include "ogAdmRepo.h" |
---|
10 | #include "encriptacion.c" |
---|
11 | // ________________________________________________________________________________________________________ |
---|
12 | // Funcin�:RegistraLog |
---|
13 | // |
---|
14 | // Descripcin�: |
---|
15 | // Esta funcin� registra los evento de errores en un fichero log |
---|
16 | // Parametros: |
---|
17 | // - msg : Mensage de error |
---|
18 | // - swerrno: Switch que indica que recupere literal de error del sistema |
---|
19 | // ________________________________________________________________________________________________________ |
---|
20 | void RegistraLog(const char *msg,int swerrno) |
---|
21 | { |
---|
22 | time_t rawtime; |
---|
23 | struct tm * timeinfo; |
---|
24 | |
---|
25 | time ( &rawtime ); |
---|
26 | timeinfo = gmtime(&rawtime); |
---|
27 | |
---|
28 | FLog=fopen(szPathFileLog,"at"); |
---|
29 | if(swerrno) |
---|
30 | fprintf (FLog,"%02d/%02d/%d %02d:%02d ***%s:%s\n",timeinfo->tm_mday,timeinfo->tm_mon+1,timeinfo->tm_year+1900,timeinfo->tm_hour,timeinfo->tm_min,msg,strerror(errno)); |
---|
31 | else |
---|
32 | fprintf (FLog,"%02d/%02d/%d %02d:%02d ***%s\n",timeinfo->tm_mday,timeinfo->tm_mon+1,timeinfo->tm_year+1900,timeinfo->tm_hour,timeinfo->tm_min,msg); |
---|
33 | fclose(FLog); |
---|
34 | } |
---|
35 | |
---|
36 | //________________________________________________________________________________________________________ |
---|
37 | // Funcinn: TomaConfiguracion |
---|
38 | // |
---|
39 | // Descripcinn: |
---|
40 | // Esta funcinn lee el fichero de configuracinn del programa |
---|
41 | // Parametros: |
---|
42 | // - pathfilecfg : Ruta al fichero de configuracinn |
---|
43 | //________________________________________________________________________________________________________ |
---|
44 | int TomaConfiguracion(char* pathfilecfg) |
---|
45 | { |
---|
46 | long lSize; |
---|
47 | char * buffer,*lineas[100],*dualparametro[2]; |
---|
48 | char ch[2]; |
---|
49 | int i,numlin,resul; |
---|
50 | |
---|
51 | if(pathfilecfg==NULL) exit(EXIT_FAILURE);; // Nombre del fichero en blanco |
---|
52 | |
---|
53 | Fconfig = fopen ( pathfilecfg , "rb" ); |
---|
54 | if (Fconfig==NULL) return(FALSE); |
---|
55 | fseek (Fconfig , 0 , SEEK_END); // Obtiene tamaño del fichero. |
---|
56 | lSize = ftell (Fconfig); |
---|
57 | rewind (Fconfig); |
---|
58 | buffer = (char*) malloc (lSize); // Toma memoria para el buffer de lectura. |
---|
59 | if (buffer == NULL) exit(EXIT_FAILURE);; |
---|
60 | fread (buffer,1,lSize,Fconfig); // Lee contenido del fichero |
---|
61 | fclose(Fconfig); |
---|
62 | |
---|
63 | //inicializar |
---|
64 | IPlocal[0]=(char)NULL; |
---|
65 | servidorhidra[0]=(char)NULL; |
---|
66 | Puerto[0]=(char)NULL; |
---|
67 | |
---|
68 | strcpy(ch,"\n");// caracter delimitador ( salto de linea) |
---|
69 | numlin=split_parametros(lineas,buffer,ch); |
---|
70 | for (i=0;i<numlin;i++){ |
---|
71 | strcpy(ch,"=");// caracter delimitador |
---|
72 | split_parametros(dualparametro,lineas[i],ch); // Toma primer nombre del parametros |
---|
73 | |
---|
74 | resul=strcmp(dualparametro[0],"IPlocal"); |
---|
75 | if(resul==0) strcpy(IPlocal,dualparametro[1]); |
---|
76 | |
---|
77 | resul=strcmp(dualparametro[0],"IPhidra"); |
---|
78 | if(resul==0) strcpy(servidorhidra,dualparametro[1]); |
---|
79 | |
---|
80 | resul=strcmp(dualparametro[0],"Puerto"); |
---|
81 | if(resul==0) strcpy(Puerto,dualparametro[1]); |
---|
82 | |
---|
83 | } |
---|
84 | if(IPlocal[0]==(char)NULL){ |
---|
85 | RegistraLog("IPlocal, NO se ha definido este parámetro",false); |
---|
86 | exit(EXIT_FAILURE);; |
---|
87 | } |
---|
88 | if(servidorhidra[0]==(char)NULL){ |
---|
89 | RegistraLog("IPhidra, NO se ha definido este parámetro",false); |
---|
90 | exit(EXIT_FAILURE);; |
---|
91 | } |
---|
92 | if(Puerto[0]==(char)NULL){ |
---|
93 | RegistraLog("Puerto, NO se ha definido este parámetro",false); |
---|
94 | exit(EXIT_FAILURE);; |
---|
95 | } |
---|
96 | puerto=atoi(Puerto); |
---|
97 | |
---|
98 | return(TRUE); |
---|
99 | } |
---|
100 | // ________________________________________________________________________________________________________ |
---|
101 | // Funcinn: INTROaFINCAD |
---|
102 | // |
---|
103 | // Descripcinn?: |
---|
104 | // Cambia INTROS por caracteres fin de cadena ('\0') en una cadena |
---|
105 | // Parametros: |
---|
106 | // - parametros : La cadena a explorar |
---|
107 | // ________________________________________________________________________________________________________ |
---|
108 | void INTROaFINCAD(char* parametros) |
---|
109 | { |
---|
110 | int lon,i; |
---|
111 | lon=strlen(parametros); |
---|
112 | for(i=0;i<lon;i++){ // Cambia los INTROS por NULOS |
---|
113 | if(parametros[i]=='\r') parametros[i]='\0'; |
---|
114 | } |
---|
115 | } |
---|
116 | // ________________________________________________________________________________________________________ |
---|
117 | // Funcinn: INTROaFINCAD |
---|
118 | // |
---|
119 | // Descripcinn?: |
---|
120 | // Cambia INTROS por caracteres fin de cadena ('\0') en una cadena |
---|
121 | // Parametros: |
---|
122 | // - parametros : La cadena a explorar |
---|
123 | // ________________________________________________________________________________________________________ |
---|
124 | void FINCADaINTRO(char* a,char *b) |
---|
125 | { |
---|
126 | char *i; |
---|
127 | for(i=a;i<b;i++){ // Cambia los NULOS por INTROS |
---|
128 | if(*i=='\0') *i='\r'; |
---|
129 | } |
---|
130 | } |
---|
131 | // ________________________________________________________________________________________________________ |
---|
132 | // Funcinn: toma_parametro |
---|
133 | // |
---|
134 | // Descripcinn?: |
---|
135 | // Esta funci? devuelve el valor de un parametro incluido en la trmInfo. |
---|
136 | // El formato del protocolo es: "nombre_parametro=valor_parametro" |
---|
137 | // Par?etros: |
---|
138 | // - nombre_parametro: Es el nombre del par?etro a recuperar |
---|
139 | // - parametros: Es la matriz que contiene todos los par?etros |
---|
140 | // ________________________________________________________________________________________________________ |
---|
141 | char * toma_parametro(const char* nombre_parametro,char *parametros) |
---|
142 | { |
---|
143 | int i=0; |
---|
144 | char* pos; |
---|
145 | |
---|
146 | for(i=0;i<LONGITUD_PARAMETROS-4;i++){ |
---|
147 | if(parametros[i]==nombre_parametro[0]){ |
---|
148 | if(parametros[i+1]==nombre_parametro[1]){ |
---|
149 | if(parametros[i+2]==nombre_parametro[2]){ |
---|
150 | if(parametros[i+3]=='='){ |
---|
151 | pos=¶metros[i+4]; |
---|
152 | return(pos); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | return(NULL); |
---|
159 | } |
---|
160 | // ________________________________________________________________________________________________________ |
---|
161 | // Funci�: split_parametros |
---|
162 | // |
---|
163 | // Descripción: |
---|
164 | // Esta funci� trocea una cadena segn un car�ter delimitador, Devuelve el nmero de trozos |
---|
165 | // Par�etros: |
---|
166 | // - trozos: Array de punteros a cadenas |
---|
167 | // - cadena: Cadena a trocear |
---|
168 | // - ch: Car�ter delimitador |
---|
169 | // ________________________________________________________________________________________________________ |
---|
170 | int split_parametros(char **trozos,char *cadena, char * ch){ |
---|
171 | int i=0; |
---|
172 | char* token; |
---|
173 | |
---|
174 | token= strtok(cadena,ch); // Trocea segn delimitador |
---|
175 | while( token != NULL ){ |
---|
176 | trozos[i++]=token; |
---|
177 | token=strtok(NULL,ch); // Siguiente token |
---|
178 | } |
---|
179 | trozos[i++]=token; |
---|
180 | return(i-1); // Devuelve el numero de trozos |
---|
181 | } |
---|
182 | //_______________________________________________________________________________________________________________ |
---|
183 | // |
---|
184 | // Comprueba si la IP del cliente est?a en la base de datos de Hidra |
---|
185 | // parámetros: |
---|
186 | // trmInfo: Puntero a la estructura de control de la conversacin DHCP |
---|
187 | // ipmac: IP o MAC del cliente que ha abierto la hebra |
---|
188 | // sw: Si vale 1 o 2 o 3 el parmetro anterior ser una IP en caso contrario ser una MAC |
---|
189 | // |
---|
190 | // Devuelve: |
---|
191 | // true: Si el cliente est en la base de datos |
---|
192 | // false: En caso contrario |
---|
193 | // |
---|
194 | // Comentarios: |
---|
195 | // Slo se procesarn mensages dhcp de clientes hidra. |
---|
196 | //_______________________________________________________________________________________________________________ |
---|
197 | int ClienteExistente(TramaRepos *trmInfo) |
---|
198 | { |
---|
199 | char sqlstr[1000],ErrStr[200]; |
---|
200 | Database db; |
---|
201 | Table tbl; |
---|
202 | |
---|
203 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
204 | // ACCESO atnico A TRAVEZ DE OBJETO MUTEX a este trozo de cnigo |
---|
205 | pthread_mutex_lock(&guardia); |
---|
206 | |
---|
207 | if(strcmp(servidorhidra,inet_ntoa(trmInfo->cliente.sin_addr))==0){ // Se trata del servidor hidra |
---|
208 | pthread_mutex_unlock(&guardia); |
---|
209 | return(true); |
---|
210 | } |
---|
211 | |
---|
212 | // Abre conexion con base de datos |
---|
213 | if(!db.Open(usuario,pasguor,datasource,catalog)){ // error de conexion |
---|
214 | db.GetErrorErrStr(ErrStr); |
---|
215 | pthread_mutex_unlock(&guardia); |
---|
216 | return(false); |
---|
217 | } |
---|
218 | |
---|
219 | sprintf(sqlstr,"SELECT ip FROM ordenadores WHERE ip='%s' ",inet_ntoa(trmInfo->cliente.sin_addr)); |
---|
220 | if(!db.Execute(sqlstr,tbl)){ // Error al leer |
---|
221 | db.GetErrorErrStr(ErrStr); |
---|
222 | pthread_mutex_unlock(&guardia); |
---|
223 | db.Close(); |
---|
224 | return(false); |
---|
225 | } |
---|
226 | |
---|
227 | if(tbl.ISEOF()){ // No existe el cliente |
---|
228 | db.Close(); |
---|
229 | pthread_mutex_unlock(&guardia); |
---|
230 | return(false); |
---|
231 | } |
---|
232 | db.Close(); |
---|
233 | pthread_mutex_unlock(&guardia); |
---|
234 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
235 | return(true); |
---|
236 | } |
---|
237 | //___________________________________________________________________________________________________ |
---|
238 | // Funcin: inclusion_REPO |
---|
239 | // |
---|
240 | // Descripcin: |
---|
241 | // Abre una sesin en el servidor Hidra |
---|
242 | //___________________________________________________________________________________________________ |
---|
243 | int inclusion_REPO() |
---|
244 | { |
---|
245 | TRAMA *trama; |
---|
246 | SOCKET sock; |
---|
247 | // Compone la trama |
---|
248 | int lon; |
---|
249 | |
---|
250 | trama=(TRAMA*)malloc(LONGITUD_TRAMA); |
---|
251 | if(!trama) |
---|
252 | return(false); |
---|
253 | lon=sprintf(trama->parametros,"nfn=inclusion_REPO\r"); // Nombre de la funcin a ejecutar en el servidor HIDRA |
---|
254 | lon+=sprintf(trama->parametros+lon,"iph=%s\r",IPlocal); // Ip del ordenador |
---|
255 | |
---|
256 | sock=Abre_conexion(servidorhidra,puerto); |
---|
257 | if(sock==INVALID_SOCKET) { |
---|
258 | sprintf(msglog,"Error al crear socket del Repositorio"); |
---|
259 | RegistraLog(msglog,false); |
---|
260 | return(false); |
---|
261 | } |
---|
262 | envia_tramas(sock,trama); |
---|
263 | recibe_tramas(sock,trama); |
---|
264 | close(sock); |
---|
265 | if(!RESPUESTA_inclusionREPO(trama)){ |
---|
266 | return(false); |
---|
267 | } |
---|
268 | return(true); |
---|
269 | } |
---|
270 | // ________________________________________________________________________________________________________ |
---|
271 | // Funcin: Abre_conexion |
---|
272 | // |
---|
273 | // Descripcin: |
---|
274 | // Crea un socket y lo conecta a un servidor |
---|
275 | // parámetros: |
---|
276 | // - ips : La direccin IP del servidor |
---|
277 | // - port : Puerto para la comunicacin |
---|
278 | // Devuelve: |
---|
279 | // - El socket o nulo dependiendo de si se ha establecido la comunicacin |
---|
280 | // ________________________________________________________________________________________________________ |
---|
281 | SOCKET Abre_conexion(char *ips,int wpuerto) |
---|
282 | { |
---|
283 | struct sockaddr_in server; |
---|
284 | SOCKET s; |
---|
285 | // Crea el socket y se intenta conectar |
---|
286 | s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
---|
287 | if (s == INVALID_SOCKET){ |
---|
288 | return (INVALID_SOCKET); |
---|
289 | } |
---|
290 | server.sin_family = AF_INET; |
---|
291 | server.sin_port = htons((short)wpuerto); |
---|
292 | server.sin_addr.s_addr = inet_addr(ips); |
---|
293 | if (connect(s, (struct sockaddr *)&server, sizeof(server)) == INVALID_SOCKET) |
---|
294 | return (INVALID_SOCKET); |
---|
295 | return(s); // Conectado |
---|
296 | } |
---|
297 | //___________________________________________________________________________________________________ |
---|
298 | // |
---|
299 | // Enva tramas al servidor HIDRA |
---|
300 | //___________________________________________________________________________________________________ |
---|
301 | int envia_tramas(SOCKET s,TRAMA *trama) |
---|
302 | { |
---|
303 | trama->arroba='@'; // cabecera de la trama |
---|
304 | strcpy(trama->identificador,"JMMLCAMDJ"); // identificador de la trama |
---|
305 | trama->ejecutor='1'; // ejecutor de la trama 1=el servidor hidra 2=el cliente hidra |
---|
306 | |
---|
307 | int nLeft,idx,ret; |
---|
308 | Encriptar((char*)trama); |
---|
309 | nLeft = strlen((char*)trama); |
---|
310 | idx = 0; |
---|
311 | while(nLeft > 0){ |
---|
312 | ret = send(s,(char*)&trama[idx], nLeft, 0); |
---|
313 | if (ret == 0) |
---|
314 | break; |
---|
315 | else |
---|
316 | if (ret == SOCKET_ERROR){ |
---|
317 | return(false); |
---|
318 | } |
---|
319 | nLeft -= ret; |
---|
320 | idx += ret; |
---|
321 | } |
---|
322 | return(true); |
---|
323 | } |
---|
324 | //___________________________________________________________________________________________________ |
---|
325 | // |
---|
326 | // Recibe tramas desde el servidor HIDRA |
---|
327 | //___________________________________________________________________________________________________ |
---|
328 | int recibe_tramas(SOCKET s,TRAMA *trama) |
---|
329 | { |
---|
330 | int ret; |
---|
331 | |
---|
332 | ret = recv(s,(char*)trama,LONGITUD_TRAMA,0); |
---|
333 | if (ret == 0) // Conexin cerrada por parte del cliente (Graceful close) |
---|
334 | return (false); |
---|
335 | else{ |
---|
336 | if (ret == SOCKET_ERROR){ |
---|
337 | return (false); |
---|
338 | } |
---|
339 | else{ // Datos recibidos |
---|
340 | Desencriptar((char*)trama); |
---|
341 | trama->parametros[ret-11]=(char)NULL; // Coloca caracter fin de cadena en trama |
---|
342 | return(true); |
---|
343 | } |
---|
344 | } |
---|
345 | } |
---|
346 | //_______________________________________________________________________________________________________________ |
---|
347 | // |
---|
348 | // Gestiona la conexion con un cliente que sea Hidra para el servicio de repositorio |
---|
349 | //_______________________________________________________________________________________________________________ |
---|
350 | LPVOID GestionaServicioRepositorio(LPVOID lpParam) |
---|
351 | { |
---|
352 | TramaRepos *trmInfo=(TramaRepos *)lpParam; |
---|
353 | |
---|
354 | Desencriptar((char*)&trmInfo->trama); |
---|
355 | if (strncmp(trmInfo->trama.identificador,"JMMLCAMDJ",9)==0){ // Es una trmInfo hidra |
---|
356 | //if(ClienteExistente(trmInfo)) // Comprueba que se trata de un cliente Hidra |
---|
357 | gestiona_comando(trmInfo); |
---|
358 | } |
---|
359 | free(trmInfo); |
---|
360 | return(trmInfo); |
---|
361 | } |
---|
362 | //_______________________________________________________________________________________________________________ |
---|
363 | // |
---|
364 | // Gestiona la conexion con un cliente que sea Hidra para el servicio de repositorio |
---|
365 | //_______________________________________________________________________________________________________________ |
---|
366 | void NwGestionaServicioRepositorio(TramaRepos * trmInfo) |
---|
367 | { |
---|
368 | Desencriptar((char*)&trmInfo->trama); |
---|
369 | if (strncmp(trmInfo->trama.identificador,"JMMLCAMDJ",9)==0){ // Es una trmInfo hidra |
---|
370 | //if(ClienteExistente(trmInfo)) // Comprueba que se trata de un cliente Hidra |
---|
371 | gestiona_comando(trmInfo); |
---|
372 | } |
---|
373 | free(trmInfo); |
---|
374 | } |
---|
375 | //_______________________________________________________________________________________________________________ |
---|
376 | // |
---|
377 | // Gestiona la conexion con un cliente que sea Hidra para el servicio de repositorio |
---|
378 | //_______________________________________________________________________________________________________________ |
---|
379 | int gestiona_comando(TramaRepos *trmInfo) |
---|
380 | { |
---|
381 | char* nombrefuncion; |
---|
382 | int resul; |
---|
383 | |
---|
384 | INTROaFINCAD(trmInfo->trama.parametros); |
---|
385 | nombrefuncion=toma_parametro("nfn=",trmInfo->trama.parametros); // Toma nombre funcin |
---|
386 | |
---|
387 | |
---|
388 | resul=strcmp(nombrefuncion,"Arrancar"); |
---|
389 | if(resul==0) |
---|
390 | return(Arrancar(trmInfo)); |
---|
391 | |
---|
392 | resul=strcmp(nombrefuncion,"Apagar"); |
---|
393 | if(resul==0) |
---|
394 | return(RegistraComando(trmInfo)); |
---|
395 | |
---|
396 | resul=strcmp(nombrefuncion,"Reiniciar"); |
---|
397 | if(resul==0) |
---|
398 | return(RegistraComando(trmInfo)); |
---|
399 | |
---|
400 | resul=strcmp(nombrefuncion,"IniciarSesion"); |
---|
401 | if(resul==0) |
---|
402 | return(RegistraComando(trmInfo)); |
---|
403 | |
---|
404 | resul=strcmp(nombrefuncion,"FicheroOperador"); |
---|
405 | if(resul==0) |
---|
406 | return(FicheroOperador(trmInfo)); |
---|
407 | |
---|
408 | resul=strcmp(nombrefuncion,"Actualizar"); |
---|
409 | if(resul==0){ |
---|
410 | return(RegistraComando(trmInfo)); |
---|
411 | } |
---|
412 | |
---|
413 | resul=strcmp(nombrefuncion,"IconoItem"); |
---|
414 | if(resul==0) |
---|
415 | return(IconoItem(trmInfo)); |
---|
416 | |
---|
417 | resul=strcmp(nombrefuncion,"ExisteFichero"); |
---|
418 | if(resul==0) |
---|
419 | return(ExisteFichero(trmInfo)); |
---|
420 | |
---|
421 | resul=strcmp(nombrefuncion,"EliminaFichero"); |
---|
422 | if(resul==0) |
---|
423 | return(EliminaFichero(trmInfo)); |
---|
424 | |
---|
425 | resul=strcmp(nombrefuncion,"LeeFicheroTexto"); |
---|
426 | if(resul==0) |
---|
427 | return(LeeFicheroTexto(trmInfo)); |
---|
428 | |
---|
429 | resul=strcmp(nombrefuncion,"ExecShell"); |
---|
430 | if(resul==0) |
---|
431 | return(RegistraComando(trmInfo)); |
---|
432 | |
---|
433 | resul=strcmp(nombrefuncion,"TomaConfiguracion"); |
---|
434 | if(resul==0) |
---|
435 | return(RegistraComando(trmInfo)); |
---|
436 | |
---|
437 | resul=strcmp(nombrefuncion,"InventarioHardware"); |
---|
438 | if(resul==0) |
---|
439 | return(RegistraComando(trmInfo)); |
---|
440 | |
---|
441 | resul=strcmp(nombrefuncion,"InventarioSoftware"); |
---|
442 | if(resul==0) |
---|
443 | return(RegistraComando(trmInfo)); |
---|
444 | |
---|
445 | resul=strcmp(nombrefuncion,"RestaurarImagen"); |
---|
446 | if(resul==0) |
---|
447 | return(RegistraComando(trmInfo)); |
---|
448 | |
---|
449 | resul=strcmp(nombrefuncion,"CrearPerfilSoftware"); |
---|
450 | if(resul==0) |
---|
451 | return(RegistraComando(trmInfo)); |
---|
452 | |
---|
453 | resul=strcmp(nombrefuncion,"ParticionaryFormatear"); |
---|
454 | if(resul==0) |
---|
455 | return(RegistraComando(trmInfo)); |
---|
456 | |
---|
457 | return(false); |
---|
458 | } |
---|
459 | //_____________________________________________________________________________________________________________ |
---|
460 | // Funcinn: RegistraComando |
---|
461 | // |
---|
462 | // Descripcinn: |
---|
463 | // Crea un fichero de comando para cada cliente hidra |
---|
464 | //_____________________________________________________________________________________________________________ |
---|
465 | int RegistraComando(TramaRepos *trmInfo) |
---|
466 | { |
---|
467 | char* ipes[MAXIMOS_CLIENTES]; |
---|
468 | char ch[2]; |
---|
469 | int i,numipes,lon; |
---|
470 | char nomfilecmd[1024]; |
---|
471 | FILE *Fcomandos; |
---|
472 | |
---|
473 | char *iph=toma_parametro("iph",trmInfo->trama.parametros); // Toma nombre funcin |
---|
474 | if(!iph) return(false); |
---|
475 | strcpy(ch,";");// caracter delimitador |
---|
476 | numipes=split_parametros(ipes,iph,ch); |
---|
477 | |
---|
478 | FINCADaINTRO(trmInfo->trama.parametros,iph); |
---|
479 | *(iph-4)=(char)NULL; |
---|
480 | lon=strlen((char*)&trmInfo->trama); |
---|
481 | |
---|
482 | //sprintf(msglog,"Registra comandos %s",(char*)&trmInfo->trama); |
---|
483 | RegistraLog(msglog,false); |
---|
484 | |
---|
485 | for(i=0;i<numipes;i++){ |
---|
486 | strcpy(nomfilecmd,PathComandos); |
---|
487 | strcat(nomfilecmd,"/CMD_"); |
---|
488 | strcat(nomfilecmd,ipes[i]); |
---|
489 | //sprintf(msglog,"Crea fichero de comandos %s",nomfilecmd); |
---|
490 | RegistraLog(msglog,false); |
---|
491 | |
---|
492 | Fcomandos=fopen( nomfilecmd,"w"); |
---|
493 | if(!Fcomandos) return(false); |
---|
494 | //sprintf(msglog,"Fichero creado %s",nomfilecmd); |
---|
495 | RegistraLog(msglog,false); |
---|
496 | |
---|
497 | fwrite((char*)&trmInfo->trama,lon,1,Fcomandos); |
---|
498 | fclose(Fcomandos); |
---|
499 | } |
---|
500 | return(true); |
---|
501 | } |
---|
502 | //_____________________________________________________________________________________________________________ |
---|
503 | // Funcin: Arrancar |
---|
504 | // |
---|
505 | // Descripcinn: |
---|
506 | // Esta funcinn enciende un ordenadores |
---|
507 | // Parámetros de entrada: |
---|
508 | // - parametros: Cadena con las mac de los ordenadores que se van a arrancar separadas por punto y coma |
---|
509 | //_____________________________________________________________________________________________________________ |
---|
510 | int Arrancar(TramaRepos *trmInfo) |
---|
511 | { |
---|
512 | int i,nummacs; |
---|
513 | char* macs[MAXIMOS_CLIENTES]; |
---|
514 | char ch[2]; // Caracter delimitador |
---|
515 | |
---|
516 | char *mac=toma_parametro("mac",trmInfo->trama.parametros); // Toma Mac |
---|
517 | strcpy(ch,";");// caracter delimitador |
---|
518 | nummacs=split_parametros(macs,mac,ch); |
---|
519 | for(i=0;i<nummacs;i++){ |
---|
520 | levanta(macs[i]); |
---|
521 | } |
---|
522 | return(RegistraComando(trmInfo)); |
---|
523 | } |
---|
524 | //_____________________________________________________________________________________________________________ |
---|
525 | // Funcinn: levanta |
---|
526 | // |
---|
527 | // Descripcion: |
---|
528 | // Enciende el ordenador cuya MAC se pasa como parámetro |
---|
529 | // Parámetros de entrada: |
---|
530 | // - mac: La mac del ordenador |
---|
531 | //_____________________________________________________________________________________________________________ |
---|
532 | int levanta(char * mac) |
---|
533 | { |
---|
534 | BOOL bOpt; |
---|
535 | SOCKET s; |
---|
536 | sockaddr_in local; |
---|
537 | int ret; |
---|
538 | |
---|
539 | int puertowakeup=PUERTO_WAKEUP; |
---|
540 | s = socket(AF_INET, SOCK_DGRAM, 0); // Crea socket |
---|
541 | if (s == INVALID_SOCKET) { |
---|
542 | RegistraLog("Fallo en creacin de socket, mndulo levanta",true); |
---|
543 | return(FALSE); |
---|
544 | } |
---|
545 | bOpt = TRUE; // Pone el socket en modo Broadcast |
---|
546 | ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bOpt,sizeof(bOpt)); |
---|
547 | if (ret == SOCKET_ERROR){ |
---|
548 | RegistraLog("Fallo en funcinn setsockopt(SO_BROADCAST), mndulo levanta",true); |
---|
549 | return(FALSE); |
---|
550 | } |
---|
551 | local.sin_family = AF_INET; |
---|
552 | local.sin_port = htons((short)puertowakeup); |
---|
553 | local.sin_addr.s_addr = htonl(INADDR_ANY); // cualquier interface |
---|
554 | if (bind(s, (sockaddr *)&local, sizeof(local)) == SOCKET_ERROR){ |
---|
555 | RegistraLog("Fallo en funcinn bind(), mndulo levanta",true); |
---|
556 | return(FALSE); |
---|
557 | } |
---|
558 | Wake_Up(s,mac); |
---|
559 | close(s); |
---|
560 | return(TRUE); |
---|
561 | } |
---|
562 | //_____________________________________________________________________________________________________________ |
---|
563 | // Funcinn: Wake_Up |
---|
564 | // |
---|
565 | // Descripcion: |
---|
566 | // Enciende el ordenador cuya MAC se pasa como parámetro |
---|
567 | // Parámetros: |
---|
568 | // - s : Socket para enviar trama en modo broadcast o a la ip del ordenador en cuestin |
---|
569 | // - mac : Cadena con el contenido de la mac |
---|
570 | //_____________________________________________________________________________________________________________ |
---|
571 | int Wake_Up(SOCKET s,char * mac) |
---|
572 | { |
---|
573 | int i,ret; |
---|
574 | char HDaddress_bin[6]; |
---|
575 | struct { |
---|
576 | BYTE secuencia_FF[6]; |
---|
577 | char macbin[16][6]; |
---|
578 | }Trama_WakeUp; |
---|
579 | sockaddr_in WakeUpCliente; |
---|
580 | |
---|
581 | int puertowakeup=PUERTO_WAKEUP; |
---|
582 | for (i=0;i<6;i++) // Primera secuencia de la trama Wake Up (0xFFFFFFFFFFFF) |
---|
583 | Trama_WakeUp.secuencia_FF[i] = 0xFF; |
---|
584 | PasaHexBin( mac,HDaddress_bin); // Pasa a binario la MAC |
---|
585 | for (i=0;i<16;i++) // Segunda secuencia de la trama Wake Up , repetir 16 veces su la MAC |
---|
586 | memcpy( &Trama_WakeUp.macbin[i][0], &HDaddress_bin, 6 ); |
---|
587 | WakeUpCliente.sin_family = AF_INET; |
---|
588 | WakeUpCliente.sin_port = htons((short)puertowakeup); |
---|
589 | WakeUpCliente.sin_addr.s_addr = htonl(INADDR_BROADCAST); // Para hacerlo con broadcast |
---|
590 | ret = sendto(s,(char *)&Trama_WakeUp, sizeof(Trama_WakeUp), 0,(sockaddr *)&WakeUpCliente, sizeof(WakeUpCliente)); |
---|
591 | if (ret == SOCKET_ERROR){ |
---|
592 | RegistraLog("Fallo en funcinn send(), mndulo Wake_Up",true); |
---|
593 | return(FALSE); |
---|
594 | } |
---|
595 | return 0; |
---|
596 | } |
---|
597 | //_____________________________________________________________________________________________________________ |
---|
598 | // Funcinn: PasaHexBin |
---|
599 | // |
---|
600 | // Descripcion: |
---|
601 | // Convierte a binario una direccinn mac desde una cadena de longitud 12 |
---|
602 | // |
---|
603 | // Parámetros de entrada: |
---|
604 | // - cadena : Cadena con el contenido de la mac |
---|
605 | // Parámetros de salida: |
---|
606 | // - numero : la direccinn mac convertida a binario (6 bytes) |
---|
607 | //_____________________________________________________________________________________________________________ |
---|
608 | void PasaHexBin( char *cadena,char *numero) |
---|
609 | { |
---|
610 | int i,j,p; |
---|
611 | char matrizHex[]="0123456789ABCDEF"; |
---|
612 | char Ucadena[12], aux; |
---|
613 | |
---|
614 | for (i=0;i<12;i++) |
---|
615 | Ucadena[i]=toupper(cadena[i]); |
---|
616 | p=0; |
---|
617 | for (i=0;i<12;i++){ |
---|
618 | for (j=0;j<16;j++){ |
---|
619 | if (Ucadena[i]==matrizHex[j]){ |
---|
620 | if (i%2){ |
---|
621 | aux=numero[p]; |
---|
622 | aux=(aux << 4); |
---|
623 | numero[p]=j; |
---|
624 | numero[p]=numero[p] | aux; |
---|
625 | p++; |
---|
626 | } |
---|
627 | else |
---|
628 | numero[p]=j; |
---|
629 | break; |
---|
630 | } |
---|
631 | } |
---|
632 | } |
---|
633 | } |
---|
634 | //_____________________________________________________________________________________________________________ |
---|
635 | // Funcinn: FicheroOperador |
---|
636 | // |
---|
637 | // Descripcinn: |
---|
638 | // Crea un fichero para que un operador de aula o administrador de centro pueda entrar en el menú privado de los clientes rembo |
---|
639 | // Parámetros de entrada: |
---|
640 | // - parametros: Parámetros del comando |
---|
641 | //_____________________________________________________________________________________________________________ |
---|
642 | int FicheroOperador(TramaRepos *trmInfo) |
---|
643 | { |
---|
644 | FILE *FLogin; |
---|
645 | char *amb,*usu,*psw,*ida; |
---|
646 | char nomfilelogin[250]; |
---|
647 | char nomcmd[512]; |
---|
648 | int op,resul,ext; |
---|
649 | |
---|
650 | amb=toma_parametro("amb",trmInfo->trama.parametros); // Toma operacion: Alta,o Baja |
---|
651 | usu=toma_parametro("usu",trmInfo->trama.parametros); // Toma nombre del fichero de login de operador |
---|
652 | psw=toma_parametro("psw",trmInfo->trama.parametros); // Toma login del fichero de login de operador |
---|
653 | ida=toma_parametro("ida",trmInfo->trama.parametros); // Toma identificador del aula |
---|
654 | strcpy(nomfilelogin,PathUsuarios); |
---|
655 | strcat(nomfilelogin,usu); |
---|
656 | ext=atoi(ida); |
---|
657 | if(ext>0){ |
---|
658 | strcat(nomfilelogin,"-"); |
---|
659 | strcat(nomfilelogin,ida); |
---|
660 | } |
---|
661 | op=atoi(amb); |
---|
662 | switch(op){ |
---|
663 | case 1: |
---|
664 | FLogin=fopen( nomfilelogin,"w"); |
---|
665 | if(FLogin==NULL) |
---|
666 | RegistraLog("PathComandos, NO existe el Path para el fichero de login de operador ",false); |
---|
667 | Encriptar(psw); |
---|
668 | fprintf (FLogin,"%s",psw); |
---|
669 | fclose(FLogin); |
---|
670 | break; |
---|
671 | case 3: |
---|
672 | strcpy(nomcmd,"rm -f "); |
---|
673 | strcat(nomcmd,nomfilelogin); |
---|
674 | resul=system(nomcmd); |
---|
675 | break; |
---|
676 | } |
---|
677 | return(true); |
---|
678 | } |
---|
679 | //_____________________________________________________________________________________________________________ |
---|
680 | // Funcinn: FicheroOperador |
---|
681 | // |
---|
682 | // Descripcinn: |
---|
683 | // Crea un fichero para que un operador de aula o administrador de centro pueda entrar en el menú privado de los clientes rembo |
---|
684 | // Parámetros de entrada: |
---|
685 | // - parametros: Parámetros del comando |
---|
686 | //_____________________________________________________________________________________________________________ |
---|
687 | int IconoItem(TramaRepos *trmInfo) |
---|
688 | { |
---|
689 | FILE *FIcono; |
---|
690 | char *nii,*amb,*lii,*iit; |
---|
691 | int lon,op,resul; |
---|
692 | char nomfileicono[250]; |
---|
693 | char nomcmd[260]; |
---|
694 | |
---|
695 | nii=toma_parametro("nii",trmInfo->trama.parametros); // Toma el nombre del fichero |
---|
696 | amb=toma_parametro("amb",trmInfo->trama.parametros); // Toma operacion: Alta,o Baja |
---|
697 | lii=toma_parametro("lii",trmInfo->trama.parametros); // Toma longitud del fichero de icono |
---|
698 | iit=toma_parametro("iit",trmInfo->trama.parametros); // Toma contenido del fichero de icono |
---|
699 | lon=atoi(lii); |
---|
700 | op=atoi(amb); |
---|
701 | strcpy(nomfileicono,PathIconos); |
---|
702 | strcat(nomfileicono,nii); |
---|
703 | switch(op){ |
---|
704 | case 1: |
---|
705 | FIcono=fopen( nomfileicono,"w"); |
---|
706 | fwrite (iit,lon,1,FIcono); |
---|
707 | fclose(FIcono); |
---|
708 | break; |
---|
709 | case 3: |
---|
710 | strcpy(nomcmd,"rm -f "); |
---|
711 | strcat(nomcmd,nomfileicono); |
---|
712 | resul=system(nomcmd); |
---|
713 | break; |
---|
714 | } |
---|
715 | return(true); |
---|
716 | } |
---|
717 | //_______________________________________________________________________________________________________________ |
---|
718 | // |
---|
719 | // Comprueba si existe un fichero |
---|
720 | //_______________________________________________________________________________________________________________ |
---|
721 | bool ExisteFichero(TramaRepos *trmInfo) |
---|
722 | { |
---|
723 | FILE *f; |
---|
724 | char swf[2]; |
---|
725 | char pathfile[250]; |
---|
726 | |
---|
727 | char *nomfile=toma_parametro("nfl",trmInfo->trama.parametros); // Toma nombre funcin |
---|
728 | sprintf(pathfile,"%s%s",PathHidra,nomfile); |
---|
729 | |
---|
730 | f = fopen(pathfile,"rt"); |
---|
731 | if(f==NULL) |
---|
732 | strcpy(swf,"0"); |
---|
733 | else |
---|
734 | strcpy(swf,"1"); |
---|
735 | if(f) fclose(f); |
---|
736 | return(respuesta_peticion(trmInfo,"Respuesta_ExisteFichero",swf,nomfile)); |
---|
737 | } |
---|
738 | //_______________________________________________________________________________________________________________ |
---|
739 | // |
---|
740 | // Envia respuesta a peticin de comando |
---|
741 | //_______________________________________________________________________________________________________________ |
---|
742 | bool respuesta_clienteHidra(TramaRepos *trmInfo) |
---|
743 | { |
---|
744 | int ret; |
---|
745 | //MandaRespuesta |
---|
746 | Encriptar((char*)&trmInfo->trama); |
---|
747 | ret=sendto(trmInfo->sck,(char*)&trmInfo->trama,strlen(trmInfo->trama.parametros)+11,0,(struct sockaddr*)&trmInfo->cliente,trmInfo->sockaddrsize); |
---|
748 | if (ret == SOCKET_ERROR){ |
---|
749 | RegistraLog("***sendto() fallo al enviar respuesta modulo respuesta_clienteHidra() :",true); |
---|
750 | return(false); |
---|
751 | } |
---|
752 | return(true); |
---|
753 | } |
---|
754 | //_______________________________________________________________________________________________________________ |
---|
755 | // |
---|
756 | // Envia respuesta a peticin de comando |
---|
757 | //_______________________________________________________________________________________________________________ |
---|
758 | bool respuesta_peticion(TramaRepos *trmInfo,const char *LitRes,char* swf,char*txt) |
---|
759 | { |
---|
760 | int lon,ret; |
---|
761 | TRAMA *trama=(TRAMA*)malloc(LONGITUD_TRAMA); |
---|
762 | if(!trama){ |
---|
763 | RegistraLog("No hay memoria suficiente para enviar la respuesta al comando",false); |
---|
764 | return(false); |
---|
765 | } |
---|
766 | trama->arroba='@'; |
---|
767 | strncpy(trama->identificador,"JMMLCAMDJ",9); |
---|
768 | trama->ejecutor='1'; |
---|
769 | lon=sprintf(trama->parametros,"nfn=%s\r",LitRes); |
---|
770 | lon+=sprintf(trama->parametros+lon,"res=%s\r",swf); |
---|
771 | lon+=sprintf(trama->parametros+lon,"txt=%s\r",txt); |
---|
772 | //MandaRespuesta |
---|
773 | Encriptar((char*)trama); |
---|
774 | ret=sendto(trmInfo->sck,(char*)trama,lon+11,0,(struct sockaddr*)&trmInfo->cliente,trmInfo->sockaddrsize); |
---|
775 | if (ret == SOCKET_ERROR){ |
---|
776 | RegistraLog("***sendto() fallo al enviar respuesta a peticin de comando:",true); |
---|
777 | return(false); |
---|
778 | } |
---|
779 | return(true); |
---|
780 | } |
---|
781 | //_______________________________________________________________________________________________________________ |
---|
782 | // |
---|
783 | // Comprueba si existe un fichero |
---|
784 | //_______________________________________________________________________________________________________________ |
---|
785 | bool EliminaFichero(TramaRepos *trmInfo) |
---|
786 | { |
---|
787 | char swf[2]; |
---|
788 | char cmdshell[512]; |
---|
789 | int res; |
---|
790 | char pathfile[250]; |
---|
791 | |
---|
792 | char *nomfile=toma_parametro("nfl",trmInfo->trama.parametros); // Toma nombre funcin |
---|
793 | sprintf(pathfile,"%s%s",PathHidra,nomfile); |
---|
794 | sprintf(cmdshell,"rm -f %s",pathfile); |
---|
795 | res=system(cmdshell); |
---|
796 | if(res==0) |
---|
797 | strcpy(swf,"1"); |
---|
798 | else |
---|
799 | strcpy(swf,"0"); |
---|
800 | return(respuesta_peticion(trmInfo,"Respuesta_EliminaFichero",swf,nomfile)); |
---|
801 | } |
---|
802 | //_______________________________________________________________________________________________________________ |
---|
803 | // |
---|
804 | // Comprueba si existe un fichero |
---|
805 | //_______________________________________________________________________________________________________________ |
---|
806 | bool LeeFicheroTexto(TramaRepos *trmInfo) |
---|
807 | { |
---|
808 | char *texto; |
---|
809 | long lSize; |
---|
810 | FILE *f; |
---|
811 | char pathfile[250]; |
---|
812 | char swf[2]; |
---|
813 | |
---|
814 | char *nomfile=toma_parametro("nfl",trmInfo->trama.parametros); // Toma nombre funcin |
---|
815 | sprintf(pathfile,"%s%s",PathHidra,nomfile); |
---|
816 | |
---|
817 | f = fopen(pathfile,"rt"); |
---|
818 | if(!f){ // El fichero no existe |
---|
819 | texto=(char*)malloc(2); |
---|
820 | strcpy(texto," "); |
---|
821 | strcpy(swf,"0"); |
---|
822 | } |
---|
823 | else{ |
---|
824 | fseek(f,0,SEEK_END); |
---|
825 | lSize=ftell(f); |
---|
826 | texto=(char*)malloc(lSize); |
---|
827 | if(!texto){ |
---|
828 | texto=(char*)malloc(2); |
---|
829 | strcpy(texto," "); |
---|
830 | strcpy(swf,"0"); |
---|
831 | } |
---|
832 | else{ |
---|
833 | rewind (f); // Coloca al principio el puntero de lectura |
---|
834 | fread (texto,1,lSize,f); // Lee el contenido del fichero |
---|
835 | strcpy(swf,"1"); |
---|
836 | fclose(f); |
---|
837 | } |
---|
838 | } |
---|
839 | return(respuesta_peticion(trmInfo,"Respuesta_LeeFicheroTexto",swf,texto)); |
---|
840 | } |
---|
841 | |
---|
842 | //_________________________________________________________________________________________________ |
---|
843 | // Funcin: Buffer |
---|
844 | // |
---|
845 | // Descripcin: |
---|
846 | // Reserva memoria |
---|
847 | // parámetros: |
---|
848 | // - l: Longitud en bytes de la reserva |
---|
849 | // Devuelve: |
---|
850 | // Un puntero a la memoria reservada |
---|
851 | //___________________________________________________________________________________________________ |
---|
852 | char * Buffer(int l) |
---|
853 | { |
---|
854 | char *buf; |
---|
855 | buf=(char*)malloc(l); |
---|
856 | if(buf==NULL){ |
---|
857 | RegistraLog("*** fallo de reserva de memoria en modulo Buffer()",true); |
---|
858 | return(false); |
---|
859 | } |
---|
860 | memset(buf,0,l); |
---|
861 | return(buf); |
---|
862 | } |
---|
863 | //_______________________________________________________________________________________________________________ |
---|
864 | // |
---|
865 | // Crea un socket en un puerto determinado para la conversacin UDP con el repositorio |
---|
866 | // |
---|
867 | //_______________________________________________________________________________________________________________ |
---|
868 | int TomaPuertoLibre(int * puerto) |
---|
869 | { |
---|
870 | SOCKET socket_c; // Socket para hebras (UDP) |
---|
871 | struct sockaddr_in cliente; |
---|
872 | int puertolibre; |
---|
873 | |
---|
874 | socket_c = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Crea socket para UDP |
---|
875 | |
---|
876 | if (socket_c == SOCKET_ERROR) |
---|
877 | return (false); |
---|
878 | |
---|
879 | cliente.sin_addr.s_addr = inet_addr(IPlocal); // selecciona interface |
---|
880 | cliente.sin_family = AF_INET; |
---|
881 | puertolibre=PUERTOMINUSER; |
---|
882 | while(puertolibre<PUERTOMAXUSER){ // Busca puerto libre |
---|
883 | cliente.sin_port = htons(puertolibre); // Puerto asignado |
---|
884 | if (bind(socket_c,(struct sockaddr *)&cliente,sizeof(cliente)) == SOCKET_ERROR) |
---|
885 | puertolibre++; |
---|
886 | else |
---|
887 | break; |
---|
888 | } |
---|
889 | if(puertolibre>=PUERTOMAXUSER){ // No hay puertos libres |
---|
890 | return(INVALID_SOCKET); |
---|
891 | } |
---|
892 | *puerto=puertolibre; |
---|
893 | return(true); |
---|
894 | } |
---|
895 | //________________________________________________________________________________________________________ |
---|
896 | // Funcinn: TomaRestoConfiguracion; |
---|
897 | // |
---|
898 | // Descripcinn: |
---|
899 | // Esta funcinn lee la trama respuesta de inclusin del repositorio hidra |
---|
900 | |
---|
901 | //________________________________________________________________________________________________________ |
---|
902 | int RESPUESTA_inclusionREPO(TRAMA *trama) |
---|
903 | { |
---|
904 | |
---|
905 | INTROaFINCAD(trama->parametros); |
---|
906 | char* prm; |
---|
907 | prm=toma_parametro("prp",trama->parametros); // Puero de comunicaciones |
---|
908 | |
---|
909 | if (prm == NULL){ |
---|
910 | RegistraLog("ATENCIÓN.- Este repositorio no está dado de alta en el sistema. Utilice la consola de administración para hacer esto.",false); |
---|
911 | return(false); |
---|
912 | } |
---|
913 | |
---|
914 | puertorepo=atoi(prm); |
---|
915 | prm=toma_parametro("pth",trama->parametros); // Path al directorio base de Hidra |
---|
916 | strcpy(PathHidra,prm); |
---|
917 | prm=toma_parametro("ptx",trama->parametros); // Path al directorio PXE |
---|
918 | strcpy(PathPXE,prm); |
---|
919 | |
---|
920 | strcpy(PathUsuarios,PathHidra); |
---|
921 | strcpy(PathIconos,PathHidra); |
---|
922 | strcpy(PathComandos,PathHidra); |
---|
923 | strcat(PathComandos,"/comandos"); |
---|
924 | strcat(PathUsuarios,"/usuarios/"); |
---|
925 | strcat(PathIconos,"/iconos/"); |
---|
926 | |
---|
927 | prm=toma_parametro("usu",trama->parametros); // usuario acceso B.D. |
---|
928 | strcpy(usuario,prm); |
---|
929 | prm=toma_parametro("pwd",trama->parametros); // Pasword |
---|
930 | strcpy(pasguor,prm); |
---|
931 | prm=toma_parametro("dat",trama->parametros); // Ip gestor de datos |
---|
932 | strcpy(datasource,prm); |
---|
933 | prm=toma_parametro("cat",trama->parametros); // Nombre B.D. |
---|
934 | strcpy(catalog,prm); |
---|
935 | |
---|
936 | return(true); |
---|
937 | } |
---|
938 | //*************************************************************************************************************** |
---|
939 | // PROGRAMA PRINCIPAL |
---|
940 | //*************************************************************************************************************** |
---|
941 | int main(int argc, char **argv) |
---|
942 | { |
---|
943 | SOCKET socket_s; // Socket donde escucha el repositorio |
---|
944 | TramaRepos *trmInfo; |
---|
945 | struct sockaddr_in local; |
---|
946 | int i,ret; |
---|
947 | |
---|
948 | strcpy(szPathFileCfg,"ogAdmRepo.cfg"); |
---|
949 | strcpy(szPathFileLog,"ogAdmRepo.log"); |
---|
950 | for(i = 1; i < argc; i++){ |
---|
951 | if (argv[i][0] == '-'){ |
---|
952 | switch (tolower(argv[i][1])){ |
---|
953 | case 'f': |
---|
954 | if (argv[i+1]!=NULL) |
---|
955 | strcpy(szPathFileCfg, argv[i+1]); |
---|
956 | else{ |
---|
957 | RegistraLog("Fallo en los parámetros: Debe especificar el fichero de configuracin del servicio",false); |
---|
958 | exit(EXIT_FAILURE); |
---|
959 | } |
---|
960 | break; |
---|
961 | case 'l': |
---|
962 | if (argv[i+1]!=NULL) |
---|
963 | strcpy(szPathFileLog, argv[i+1]); |
---|
964 | else{ |
---|
965 | RegistraLog("Fallo en los parámetros: Debe especificar el fichero de log para el servicio",false); |
---|
966 | exit(EXIT_FAILURE); |
---|
967 | } |
---|
968 | break; |
---|
969 | default: |
---|
970 | RegistraLog("Fallo de sintaxis en los parámetros: Debe especificar -f nombre_del_fichero_de_configuracin_del_servicio -l nombre_del_fichero_de_log_del_servicio",false); |
---|
971 | exit(EXIT_FAILURE); |
---|
972 | break; |
---|
973 | } |
---|
974 | } |
---|
975 | } |
---|
976 | if(!TomaConfiguracion(szPathFileCfg)){ // Toma parametros de configuracion |
---|
977 | RegistraLog("NO existe fichero de configuración o contiene un error de sintaxis",false); |
---|
978 | exit(EXIT_FAILURE); |
---|
979 | } |
---|
980 | if(!inclusion_REPO()){ |
---|
981 | RegistraLog("Ha habido algún problema al abrir sesión con el servidor de administración",false); |
---|
982 | exit(EXIT_FAILURE); |
---|
983 | } |
---|
984 | |
---|
985 | RegistraLog("***Inicio de sesion***",false); |
---|
986 | |
---|
987 | socket_s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Crea socket para UDP |
---|
988 | if (socket_s == SOCKET_ERROR){ |
---|
989 | RegistraLog("***Error al crear socket para servicio del Repositorio:",true); |
---|
990 | exit(EXIT_FAILURE); |
---|
991 | } |
---|
992 | RegistraLog("***Creando Socket para comunicaciones***",false); |
---|
993 | |
---|
994 | local.sin_addr.s_addr = inet_addr(IPlocal);// selecciona interface |
---|
995 | local.sin_family = AF_INET; |
---|
996 | local.sin_port = htons(puertorepo); // Puerto |
---|
997 | |
---|
998 | // Enlaza socket |
---|
999 | if (bind(socket_s,(struct sockaddr *)&local,sizeof(local)) == SOCKET_ERROR){ |
---|
1000 | RegistraLog("***Error al enlazar socket con interface para servicio de Repositorio Hidra",true); |
---|
1001 | exit(EXIT_FAILURE);; |
---|
1002 | } |
---|
1003 | RegistraLog("***Enlazado Socket para comunicaciones***",false); |
---|
1004 | while(true){ |
---|
1005 | trmInfo = (TramaRepos*)malloc(sizeof(TramaRepos)); // Crea estructura de control para hebra |
---|
1006 | if (trmInfo == NULL){ |
---|
1007 | RegistraLog("***Fallo al crear estructura de control para protocolo REPO",false); |
---|
1008 | exit(EXIT_FAILURE);; |
---|
1009 | } |
---|
1010 | // Inicializa trmInfo |
---|
1011 | memset(trmInfo,0,sizeof(struct TramaRepos)); |
---|
1012 | trmInfo->sockaddrsize = sizeof(trmInfo->cliente); |
---|
1013 | trmInfo->sck=socket_s; |
---|
1014 | // Espera trmInfos Repositorio |
---|
1015 | ret = recvfrom(trmInfo->sck,(char *)&trmInfo->trama, sizeof(trmInfo->trama),0,(struct sockaddr *)&trmInfo->cliente, &trmInfo->sockaddrsize); |
---|
1016 | if (ret == SOCKET_ERROR){ |
---|
1017 | RegistraLog("***Error al recibir mensaje de cliente hidra. Se para el servicio de repositorio",true); |
---|
1018 | exit(EXIT_FAILURE); |
---|
1019 | } |
---|
1020 | else{ |
---|
1021 | if (ret>0){ |
---|
1022 | /* |
---|
1023 | resul=pthread_create(&hThread,NULL,GestionaServicioRepositorio,(LPVOID)trmInfo); |
---|
1024 | if(resul!=0){ |
---|
1025 | RegistraLog("***Fallo al crear la hebra cliente de repositorio Hidra",false); |
---|
1026 | exit(EXIT_FAILURE); |
---|
1027 | } |
---|
1028 | pthread_detach(hThread); |
---|
1029 | */ |
---|
1030 | NwGestionaServicioRepositorio(trmInfo); |
---|
1031 | } |
---|
1032 | } |
---|
1033 | } |
---|
1034 | close(socket_s); |
---|
1035 | exit(EXIT_SUCCESS); |
---|
1036 | } |
---|