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