[1a9dea4] | 1 | // ************************************************************************* |
---|
| 2 | // Aplicación: OPENGNSYS |
---|
| 3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla |
---|
| 4 | // Fecha Creación: Año 2003-2004 |
---|
| 5 | // Fecha Última modificación: Marzo-2006 |
---|
| 6 | // Nombre del fichero: ogAdmServer.php |
---|
| 7 | // Descripción : |
---|
| 8 | // |
---|
| 9 | // **************************************************************************** |
---|
[34fc87f] | 10 | #include "ogAdmServer.h" |
---|
[9fd5e97] | 11 | #include "ogAdmLib.c" |
---|
[277d0cd] | 12 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 13 | // Función: TomaEnvio |
---|
| 14 | // |
---|
| 15 | // Descripción: |
---|
| 16 | // Toma la hora actual del sistema para identificar envios |
---|
[9fd5e97] | 17 | // Parámetros: |
---|
| 18 | // Ninguno |
---|
[90f061f6] | 19 | // ________________________________________________________________________________________________________ |
---|
| 20 | unsigned int TomaEnvio() { |
---|
[277d0cd] | 21 | time_t rawtime; |
---|
[90f061f6] | 22 | time(&rawtime); |
---|
| 23 | return (rawtime); |
---|
[277d0cd] | 24 | } |
---|
[34fc87f] | 25 | //________________________________________________________________________________________________________ |
---|
| 26 | // |
---|
| 27 | // Función: TomaConfiguracion |
---|
| 28 | // |
---|
| 29 | // Descripción: |
---|
[9fd5e97] | 30 | // Esta función lee el fichero de configuración del programa hidralinuxcli y toma los parámetros |
---|
[34fc87f] | 31 | // Parametros: |
---|
| 32 | // - pathfilecfg : Ruta al fichero de configuración |
---|
| 33 | //________________________________________________________________________________________________________ |
---|
[90f061f6] | 34 | int TomaConfiguracion(char* pathfilecfg) { |
---|
[34fc87f] | 35 | long lSize; |
---|
[90f061f6] | 36 | char * buffer, *lineas[100], *dualparametro[2]; |
---|
[34fc87f] | 37 | char ch[2]; |
---|
[90f061f6] | 38 | int i, numlin, resul; |
---|
| 39 | |
---|
| 40 | if (pathfilecfg == NULL) |
---|
| 41 | return (FALSE); // Nombre del fichero en blanco |
---|
| 42 | |
---|
| 43 | Fconfig = fopen(pathfilecfg, "rb"); |
---|
| 44 | if (Fconfig == NULL) |
---|
| 45 | return (FALSE); |
---|
| 46 | fseek(Fconfig, 0, SEEK_END); // Obtiene tamaño del fichero. |
---|
| 47 | lSize = ftell(Fconfig); |
---|
| 48 | rewind(Fconfig); |
---|
| 49 | buffer = (char*) malloc(lSize); // Toma memoria para el buffer de lectura. |
---|
| 50 | if (buffer == NULL) |
---|
| 51 | return (FALSE); |
---|
| 52 | fread(buffer, 1, lSize, Fconfig); // Lee contenido del fichero |
---|
[34fc87f] | 53 | fclose(Fconfig); |
---|
| 54 | |
---|
| 55 | //inicializar |
---|
[90f061f6] | 56 | IPlocal[0] = (char) NULL; |
---|
| 57 | servidorhidra[0] = (char) NULL; |
---|
| 58 | Puerto[0] = (char) NULL; |
---|
[9fd5e97] | 59 | |
---|
[90f061f6] | 60 | usuario[0] = (char) NULL; |
---|
| 61 | pasguor[0] = (char) NULL; |
---|
| 62 | datasource[0] = (char) NULL; |
---|
| 63 | catalog[0] = (char) NULL; |
---|
| 64 | |
---|
| 65 | strcpy(ch, "\n");// caracter delimitador ( salto de linea) |
---|
| 66 | numlin = split_parametros(lineas, buffer, ch); |
---|
| 67 | for (i = 0; i < numlin; i++) { |
---|
| 68 | strcpy(ch, "=");// caracter delimitador |
---|
| 69 | split_parametros(dualparametro, lineas[i], ch); // Toma primer nombre del parametros |
---|
| 70 | |
---|
| 71 | resul = strcmp(dualparametro[0], "IPhidra"); |
---|
| 72 | if (resul == 0) |
---|
| 73 | strcpy(IPlocal, dualparametro[1]); |
---|
| 74 | |
---|
| 75 | resul = strcmp(dualparametro[0], "IPhidra"); |
---|
| 76 | if (resul == 0) |
---|
| 77 | strcpy(servidorhidra, dualparametro[1]); |
---|
| 78 | |
---|
| 79 | resul = strcmp(dualparametro[0], "Puerto"); |
---|
| 80 | if (resul == 0) |
---|
| 81 | strcpy(Puerto, dualparametro[1]); |
---|
| 82 | |
---|
| 83 | resul = strcmp(dualparametro[0], "AulaUp"); |
---|
| 84 | if (resul == 0) |
---|
| 85 | strcpy(AulaUp, dualparametro[1]); |
---|
| 86 | |
---|
| 87 | resul = strcmp(dualparametro[0], "Usuario"); |
---|
| 88 | if (resul == 0) |
---|
| 89 | strcpy(usuario, dualparametro[1]); |
---|
| 90 | |
---|
| 91 | resul = strcmp(dualparametro[0], "PassWord"); |
---|
| 92 | if (resul == 0) |
---|
| 93 | strcpy(pasguor, dualparametro[1]); |
---|
| 94 | |
---|
| 95 | resul = strcmp(dualparametro[0], "DataSource"); |
---|
| 96 | if (resul == 0) |
---|
| 97 | strcpy(datasource, dualparametro[1]); |
---|
| 98 | |
---|
| 99 | resul = strcmp(dualparametro[0], "Catalog"); |
---|
| 100 | if (resul == 0) |
---|
| 101 | strcpy(catalog, dualparametro[1]); |
---|
| 102 | } |
---|
| 103 | if (IPlocal[0] == (char) NULL) { |
---|
| 104 | RegistraLog("IPlocal, NO se ha definido este parámetro", false); |
---|
| 105 | return (FALSE); |
---|
| 106 | } |
---|
| 107 | if (servidorhidra[0] == (char) NULL) { |
---|
| 108 | RegistraLog("IPhidra, NO se ha definido este parámetro", false); |
---|
| 109 | return (FALSE); |
---|
| 110 | } |
---|
| 111 | if (Puerto[0] == (char) NULL) { |
---|
| 112 | RegistraLog("Puerto, NO se ha definido este parámetro", false); |
---|
| 113 | return (FALSE); |
---|
| 114 | } |
---|
| 115 | puerto = atoi(Puerto); |
---|
| 116 | |
---|
| 117 | if (AulaUp[0] == (char) NULL) { |
---|
| 118 | RegistraLog("AulaUp, NO se ha definido este parámetro", false); |
---|
| 119 | return (FALSE); |
---|
| 120 | } |
---|
| 121 | aulaup = atoi(AulaUp); |
---|
| 122 | |
---|
| 123 | if (usuario[0] == (char) NULL) { |
---|
| 124 | RegistraLog("Usuario, NO se ha definido este parámetro", false); |
---|
| 125 | return (FALSE); |
---|
| 126 | } |
---|
| 127 | if (pasguor[0] == (char) NULL) { |
---|
| 128 | RegistraLog("PassWord, NO se ha definido este parámetro", false); |
---|
| 129 | return (FALSE); |
---|
| 130 | } |
---|
| 131 | if (datasource[0] == (char) NULL) { |
---|
| 132 | RegistraLog("DataSource, NO se ha definido este parámetro", false); |
---|
| 133 | return (FALSE); |
---|
| 134 | } |
---|
| 135 | if (catalog[0] == (char) NULL) { |
---|
| 136 | RegistraLog("Catalog, NO se ha definido este parámetro", false); |
---|
| 137 | return (FALSE); |
---|
| 138 | } |
---|
| 139 | return (TRUE); |
---|
[34fc87f] | 140 | } |
---|
| 141 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 142 | // Función: GestionaConexion |
---|
[34fc87f] | 143 | // |
---|
[b0bb14f] | 144 | // Descripción: |
---|
[9fd5e97] | 145 | // Esta hebra es la encargada de comunicarse con los clientes a traves del socket enviado como parámetro. |
---|
[34fc87f] | 146 | // Parametros: |
---|
[9fd5e97] | 147 | // - s : Socket usado |
---|
[34fc87f] | 148 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 149 | void * GestionaConexion(void* s) { |
---|
| 150 | SOCKET socket_c = *(SOCKET*) s; |
---|
| 151 | TRAMA trama; |
---|
| 152 | |
---|
| 153 | if (recibe_trama(socket_c, &trama)) { |
---|
| 154 | if (strncmp(trama.identificador, "JMMLCAMDJ", 9) == 0) // Es una trama hidra |
---|
| 155 | gestiona_comando(socket_c, trama); |
---|
| 156 | } |
---|
| 157 | return (s); |
---|
[34fc87f] | 158 | } |
---|
| 159 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 160 | // Función: gestiona_comando |
---|
[34fc87f] | 161 | // |
---|
[b0bb14f] | 162 | // Descripción: |
---|
[9fd5e97] | 163 | // Analiza el comando enviado por el servidor web y lo reenvía al cliente rembo o lo ejecuta |
---|
[34fc87f] | 164 | // Parametros: |
---|
| 165 | // - s : Socket usado |
---|
[9fd5e97] | 166 | // - trama : La trama con los parámetros del comando |
---|
[34fc87f] | 167 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 168 | void gestiona_comando(SOCKET s, TRAMA trama) { |
---|
| 169 | int i, resul, idaccion, numipes, cont, estado_cliente, contOG; |
---|
| 170 | char *parametros, *nombrefuncion; |
---|
| 171 | char *iph, *ids, *coletilla; |
---|
| 172 | char pids[20], ipes[MAXLON_PARAMETROSIPH]; |
---|
| 173 | |
---|
| 174 | parametros = &trama.parametros[0]; |
---|
| 175 | |
---|
| 176 | if (trama.ejecutor == '1') { // Debe ejecutar el servidor |
---|
[34fc87f] | 177 | INTROaFINCAD(parametros); |
---|
[90f061f6] | 178 | nombrefuncion = toma_parametro("nfn", parametros); |
---|
| 179 | resul = strcmp(nombrefuncion, "InclusionCliente"); |
---|
| 180 | if (resul == 0) { |
---|
| 181 | if (!InclusionCliente(s, parametros)) |
---|
[34fc87f] | 182 | respuesta_cortesia(s); |
---|
| 183 | return; |
---|
| 184 | } |
---|
| 185 | |
---|
[90f061f6] | 186 | resul = strcmp(nombrefuncion, "inclusion_cliWINLNX"); |
---|
| 187 | if (resul == 0) { |
---|
| 188 | inclusion_cliWINLNX(s, parametros); |
---|
[34fc87f] | 189 | return; |
---|
| 190 | } |
---|
[90f061f6] | 191 | |
---|
| 192 | resul = strcmp(nombrefuncion, "inclusion_REPO"); |
---|
| 193 | if (resul == 0) { |
---|
| 194 | inclusion_REPO(s, parametros); |
---|
[34fc87f] | 195 | return; |
---|
[90f061f6] | 196 | } |
---|
[34fc87f] | 197 | |
---|
[90f061f6] | 198 | resul = strcmp(nombrefuncion, "ComandosPendientes"); |
---|
| 199 | if (resul == 0) { |
---|
| 200 | if (!ComandosPendientes(s, parametros)) |
---|
[34fc87f] | 201 | respuesta_cortesia(s); |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | |
---|
[90f061f6] | 205 | resul = strcmp(nombrefuncion, "RecuperaItem"); |
---|
| 206 | if (resul == 0) { |
---|
| 207 | if (!RecuperaItem(s, parametros)) |
---|
[34fc87f] | 208 | respuesta_cortesia(s); |
---|
| 209 | return; |
---|
| 210 | } |
---|
| 211 | |
---|
[90f061f6] | 212 | resul = strcmp(nombrefuncion, "EjecutarItem"); |
---|
| 213 | if (resul == 0) { |
---|
| 214 | if (!EjecutarItem(s, parametros)) |
---|
[34fc87f] | 215 | respuesta_cortesia(s); |
---|
| 216 | return; |
---|
| 217 | } |
---|
[90f061f6] | 218 | resul = strcmp(nombrefuncion, "DisponibilidadComandos"); |
---|
| 219 | if (resul == 0) { |
---|
| 220 | DisponibilidadComandos(s, parametros); |
---|
[34fc87f] | 221 | respuesta_cortesia(s); |
---|
| 222 | return; |
---|
| 223 | } |
---|
[90f061f6] | 224 | resul = strcmp(nombrefuncion, "EcoConsola"); |
---|
| 225 | if (resul == 0) { |
---|
| 226 | EcoConsola(s, parametros); |
---|
[2f4f9ff] | 227 | return; |
---|
| 228 | } |
---|
| 229 | |
---|
[90f061f6] | 230 | resul = strcmp(nombrefuncion, "Sondeo"); |
---|
| 231 | if (resul == 0) { |
---|
| 232 | Sondeo(s, parametros); |
---|
[34fc87f] | 233 | return; |
---|
| 234 | } |
---|
[2f4f9ff] | 235 | |
---|
[90f061f6] | 236 | resul = strcmp(nombrefuncion, "Arrancar"); |
---|
| 237 | if (resul == 0) { |
---|
[34fc87f] | 238 | Arrancar(parametros); |
---|
| 239 | return; |
---|
| 240 | } |
---|
| 241 | |
---|
[90f061f6] | 242 | resul = strcmp(nombrefuncion, "Actualizar"); |
---|
| 243 | if (resul == 0) { |
---|
[34fc87f] | 244 | Actualizar(parametros); |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | |
---|
[90f061f6] | 248 | resul = strcmp(nombrefuncion, "Conmutar"); |
---|
| 249 | if (resul == 0) { |
---|
[34fc87f] | 250 | Conmutar(parametros); |
---|
| 251 | return; |
---|
| 252 | } |
---|
[90f061f6] | 253 | resul = strcmp(nombrefuncion, "Purgar"); |
---|
| 254 | if (resul == 0) { |
---|
[34fc87f] | 255 | PurgarTablaSockets(parametros); |
---|
| 256 | return; |
---|
| 257 | } |
---|
[90f061f6] | 258 | |
---|
| 259 | resul = strcmp(nombrefuncion, "ConsolaRemota"); |
---|
| 260 | if (resul == 0) { |
---|
[2f4f9ff] | 261 | ConsolaRemota(parametros); |
---|
| 262 | return; |
---|
| 263 | } |
---|
| 264 | |
---|
[90f061f6] | 265 | resul = strcmp(nombrefuncion, "RESPUESTA_Arrancar"); |
---|
| 266 | if (resul == 0) { |
---|
| 267 | RESPUESTA_Arrancar(s, parametros); |
---|
[34fc87f] | 268 | respuesta_cortesia(s); |
---|
| 269 | return; |
---|
| 270 | } |
---|
| 271 | |
---|
[90f061f6] | 272 | resul = strcmp(nombrefuncion, "RESPUESTA_Apagar"); |
---|
| 273 | if (resul == 0) { |
---|
| 274 | RESPUESTA_Apagar(s, parametros); |
---|
[34fc87f] | 275 | respuesta_cortesia(s); |
---|
| 276 | return; |
---|
| 277 | } |
---|
| 278 | |
---|
[90f061f6] | 279 | resul = strcmp(nombrefuncion, "RESPUESTA_IniciarSesion"); |
---|
| 280 | if (resul == 0) { |
---|
| 281 | RESPUESTA_IniciarSesion(s, parametros); |
---|
[34fc87f] | 282 | respuesta_cortesia(s); |
---|
| 283 | return; |
---|
| 284 | } |
---|
| 285 | |
---|
[90f061f6] | 286 | resul = strcmp(nombrefuncion, "RESPUESTA_Reiniciar"); |
---|
| 287 | if (resul == 0) { |
---|
| 288 | RESPUESTA_Reiniciar(s, parametros); |
---|
[34fc87f] | 289 | respuesta_cortesia(s); |
---|
| 290 | return; |
---|
| 291 | } |
---|
[90f061f6] | 292 | |
---|
| 293 | resul = strcmp(nombrefuncion, "RESPUESTA_IniciarSesion"); |
---|
| 294 | if (resul == 0) { |
---|
| 295 | RESPUESTA_Reiniciar(s, parametros); |
---|
[dc00c0f] | 296 | respuesta_cortesia(s); |
---|
| 297 | return; |
---|
[90f061f6] | 298 | } |
---|
| 299 | resul = strcmp(nombrefuncion, "RESPUESTA_ExecShell"); |
---|
| 300 | if (resul == 0) { |
---|
| 301 | RESPUESTA_ExecShell(s, parametros); |
---|
[34fc87f] | 302 | respuesta_cortesia(s); |
---|
| 303 | return; |
---|
| 304 | } |
---|
[90f061f6] | 305 | resul = strcmp(nombrefuncion, "RESPUESTA_CrearPerfilSoftware"); |
---|
| 306 | if (resul == 0) { |
---|
| 307 | RESPUESTA_CrearPerfilSoftware(s, parametros); |
---|
[34fc87f] | 308 | respuesta_cortesia(s); |
---|
| 309 | return; |
---|
| 310 | } |
---|
| 311 | |
---|
[90f061f6] | 312 | resul = strcmp(nombrefuncion, "RESPUESTA_CrearSoftwareIncremental"); |
---|
| 313 | if (resul == 0) { |
---|
| 314 | RESPUESTA_CrearSoftwareIncremental(s, parametros); |
---|
[34fc87f] | 315 | respuesta_cortesia(s); |
---|
| 316 | return; |
---|
| 317 | } |
---|
[90f061f6] | 318 | resul = strcmp(nombrefuncion, "RESPUESTA_RestaurarImagen"); |
---|
| 319 | if (resul == 0) { |
---|
| 320 | RESPUESTA_RestaurarImagen(s, parametros); |
---|
[34fc87f] | 321 | respuesta_cortesia(s); |
---|
| 322 | return; |
---|
| 323 | } |
---|
[90f061f6] | 324 | resul = strcmp(nombrefuncion, "RESPUESTA_ParticionaryFormatear"); |
---|
| 325 | if (resul == 0) { |
---|
| 326 | RESPUESTA_ParticionaryFormatear(s, parametros); |
---|
[34fc87f] | 327 | respuesta_cortesia(s); |
---|
| 328 | return; |
---|
| 329 | } |
---|
[90f061f6] | 330 | resul = strcmp(nombrefuncion, "RESPUESTA_Configurar"); |
---|
| 331 | if (resul == 0) { |
---|
| 332 | RESPUESTA_Configurar(s, parametros); |
---|
[34fc87f] | 333 | respuesta_cortesia(s); |
---|
| 334 | return; |
---|
| 335 | } |
---|
[90f061f6] | 336 | resul = strcmp(nombrefuncion, "RESPUESTA_TomaConfiguracion"); |
---|
| 337 | if (resul == 0) { |
---|
| 338 | RESPUESTA_TomaConfiguracion(s, parametros); |
---|
[34fc87f] | 339 | respuesta_cortesia(s); |
---|
| 340 | return; |
---|
| 341 | } |
---|
[90f061f6] | 342 | resul = strcmp(nombrefuncion, "RESPUESTA_TomaHardware"); |
---|
| 343 | if (resul == 0) { |
---|
| 344 | RESPUESTA_TomaHardware(s, parametros); |
---|
[34fc87f] | 345 | respuesta_cortesia(s); |
---|
| 346 | return; |
---|
[90f061f6] | 347 | } |
---|
| 348 | resul = strcmp(nombrefuncion, "RESPUESTA_TomaSoftware"); |
---|
| 349 | if (resul == 0) { |
---|
| 350 | RESPUESTA_TomaSoftware(s, parametros); |
---|
[ec01697] | 351 | respuesta_cortesia(s); |
---|
| 352 | return; |
---|
[90f061f6] | 353 | } |
---|
[9fd5e97] | 354 | } else { // Debe ejecutar el cliente rembo |
---|
[90f061f6] | 355 | coletilla = corte_iph(parametros); // toma el puntero al comienzo del parametros iph |
---|
[34fc87f] | 356 | INTROaFINCAD(coletilla); |
---|
[90f061f6] | 357 | iph = toma_parametro("iph", coletilla); // Toma ipes |
---|
[9fd5e97] | 358 | ids = toma_parametro("ids", coletilla); // Toma identificador de la acción |
---|
[90f061f6] | 359 | coletilla[0] = '\0';// Corta la trama en la ip |
---|
| 360 | strcpy(ipes, iph); // Copia la cadena de ipes |
---|
| 361 | if (ids != NULL) { |
---|
| 362 | idaccion = atoi(ids); |
---|
| 363 | sprintf(pids, "ids=%d\r", idaccion); |
---|
[9fd5e97] | 364 | strcat(parametros, pids); // Le añade el identificador de la acción |
---|
[90f061f6] | 365 | } |
---|
[9fd5e97] | 366 | numipes = cuenta_ipes(ipes); // Número de ipes a los que enviar las tramas |
---|
[90f061f6] | 367 | cont = 0; |
---|
[9fd5e97] | 368 | contOG = 0; //Contador para saber al numero de clientes opengnsys a los que se envía el comando |
---|
[34fc87f] | 369 | DesmarcaServidoresRembo(); |
---|
[90f061f6] | 370 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 371 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 372 | if (IgualIP(ipes, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
| 373 | estado_cliente = strcmp(tbsockets[i].estado, CLIENTE_REMBO); |
---|
| 374 | if (estado_cliente == 0) { // Cliente Rembo ... |
---|
| 375 | strcpy(tbsockets[i].estado, CLIENTE_OCUPADO); |
---|
| 376 | contOG++; |
---|
| 377 | MarcaServidoresRembo(tbsockets[i].ipsrvrmb, |
---|
| 378 | tbsockets[i].ip); |
---|
| 379 | } else { |
---|
| 380 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 381 | CLIENTE_OCUPADO); |
---|
| 382 | if (estado_cliente != 0) { // Cliente Windows(Windows98,Windows2000,windows XP...) y Linux |
---|
| 383 | strcpy(tbsockets[i].estado, CLIENTE_OCUPADO); |
---|
| 384 | manda_comando(tbsockets[i].sock, parametros); |
---|
[34fc87f] | 385 | } |
---|
| 386 | } |
---|
[9fd5e97] | 387 | cont++; // Contador de envíos de tramas a ordenadores |
---|
[90f061f6] | 388 | if (cont == numipes) |
---|
| 389 | break; |
---|
[34fc87f] | 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
[90f061f6] | 393 | EnviaServidoresRembo(parametros, contOG); |
---|
[34fc87f] | 394 | } |
---|
| 395 | } |
---|
| 396 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 397 | // Función: manda_comando |
---|
[34fc87f] | 398 | // |
---|
[b0bb14f] | 399 | // Descripción: |
---|
[9fd5e97] | 400 | // Esta función envía un comando por la red (TCP) desde el servidor hidra al servidor rembo que controla al cliente que lo ejecuta |
---|
[34fc87f] | 401 | // Parametros: |
---|
| 402 | // - sock : El socket del cliente |
---|
| 403 | // - parametros: El contenido del comando |
---|
| 404 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 405 | int manda_comando(SOCKET sock, char* parametros) { |
---|
| 406 | TRAMA trama; |
---|
[34fc87f] | 407 | int resul; |
---|
| 408 | |
---|
[90f061f6] | 409 | trama.arroba = '@'; |
---|
| 410 | strncpy(trama.identificador, "JMMLCAMDJ", 9); |
---|
| 411 | trama.ejecutor = '0'; |
---|
| 412 | strcpy(trama.parametros, parametros); |
---|
| 413 | resul = manda_trama(sock, &trama); |
---|
| 414 | return (resul); |
---|
[34fc87f] | 415 | } |
---|
| 416 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 417 | // Función: manda_trama |
---|
[34fc87f] | 418 | // |
---|
[b0bb14f] | 419 | // Descripción: |
---|
[9fd5e97] | 420 | // Esta función envía una trama por la red (TCP) |
---|
[34fc87f] | 421 | // Parametros: |
---|
| 422 | // - sock : El socket del host al que se dirige la trama |
---|
| 423 | // - trama: El contenido de la trama |
---|
| 424 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 425 | int manda_trama(SOCKET sock, TRAMA* trama) { |
---|
| 426 | int nLeft, idx, ret; |
---|
| 427 | Encriptar((char*) trama); |
---|
| 428 | nLeft = strlen((char*) trama); |
---|
[34fc87f] | 429 | idx = 0; |
---|
[90f061f6] | 430 | while (nLeft > 0) { |
---|
| 431 | ret = send(sock, (char*) &trama[idx], nLeft, 0); |
---|
| 432 | |
---|
| 433 | if (ret == 0) { |
---|
[34fc87f] | 434 | break; |
---|
[90f061f6] | 435 | } else if (ret == SOCKET_ERROR) { |
---|
| 436 | RegistraLog("***send() fallo en hebra cliente", true); |
---|
| 437 | return (FALSE); |
---|
[34fc87f] | 438 | } |
---|
| 439 | nLeft -= ret; |
---|
| 440 | idx += ret; |
---|
| 441 | } |
---|
[90f061f6] | 442 | return (TRUE); |
---|
[34fc87f] | 443 | } |
---|
| 444 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 445 | // Función: recibe_trama |
---|
[34fc87f] | 446 | // |
---|
[b0bb14f] | 447 | // Descripción: |
---|
[9fd5e97] | 448 | // Esta función recibe una trama por la red (TCP) |
---|
[34fc87f] | 449 | // Parametros: |
---|
| 450 | // - sock : El socket del cliente |
---|
| 451 | // - trama: El buffer para recibir la trama |
---|
| 452 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 453 | int recibe_trama(SOCKET sock, TRAMA* trama) { |
---|
[34fc87f] | 454 | int ret; |
---|
| 455 | |
---|
[90f061f6] | 456 | while (1) { // Bucle para recibir datos del cliente |
---|
| 457 | ret = recv(sock, (char*) trama, LONGITUD_TRAMA, 0); |
---|
[9fd5e97] | 458 | if (ret == 0) // Conexión cerrada por parte del cliente (Graceful close) |
---|
[34fc87f] | 459 | break; |
---|
[90f061f6] | 460 | else { |
---|
| 461 | if (ret == SOCKET_ERROR) { |
---|
| 462 | RegistraLog("***recv() fallo en recepcion trama", true); |
---|
[34fc87f] | 463 | return (FALSE); |
---|
[90f061f6] | 464 | } else |
---|
| 465 | // Datos recibidos |
---|
[34fc87f] | 466 | break; |
---|
| 467 | } |
---|
| 468 | } |
---|
[90f061f6] | 469 | Desencriptar((char*) trama); |
---|
| 470 | trama->parametros[ret - 11] = (char) NULL; // Coloca caracter fin de cadena en trama |
---|
| 471 | return (TRUE); |
---|
[34fc87f] | 472 | } |
---|
| 473 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 474 | // Función: hay_hueco |
---|
[34fc87f] | 475 | // |
---|
[b0bb14f] | 476 | // Descripción: |
---|
[9fd5e97] | 477 | // Esta función devuelve true o false dependiendo de que haya hueco en la tabla de sockets para un nuevo cliente. |
---|
[34fc87f] | 478 | // Parametros: |
---|
[9fd5e97] | 479 | // - idx: Primer indice libre que se podrá utilizar |
---|
[34fc87f] | 480 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 481 | int hay_hueco(int *idx) { |
---|
[34fc87f] | 482 | int i; |
---|
| 483 | |
---|
[90f061f6] | 484 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 485 | if (strncmp(tbsockets[i].ip, "\0", 1) == 0) { // Hay un hueco |
---|
| 486 | *idx = i; |
---|
| 487 | return (TRUE); |
---|
[34fc87f] | 488 | } |
---|
| 489 | } |
---|
[90f061f6] | 490 | return (FALSE); |
---|
[34fc87f] | 491 | } |
---|
| 492 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 493 | // Función: cliente_existente |
---|
[34fc87f] | 494 | // |
---|
[9fd5e97] | 495 | // Descripción: |
---|
| 496 | // Esta función devuelve true o false dependiendo de si el cliente está registrado en la tabla de sockets |
---|
| 497 | // Parámetros: |
---|
[34fc87f] | 498 | // - ip : La ip del cliente a buscar |
---|
[9fd5e97] | 499 | // - idx: Indice que ocupará el cliente, en el caso de estar ya registrado |
---|
[34fc87f] | 500 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 501 | BOOLEAN cliente_existente(char *ip, int* idx) { |
---|
[34fc87f] | 502 | int i; |
---|
[90f061f6] | 503 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 504 | if (strcmp(ip, tbsockets[i].ip) == 0) { // Si existe la IP ... |
---|
| 505 | *idx = i; |
---|
| 506 | return (TRUE); |
---|
[34fc87f] | 507 | } |
---|
| 508 | } |
---|
[90f061f6] | 509 | return (FALSE); |
---|
[34fc87f] | 510 | } |
---|
| 511 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 512 | // Función: hay_huecoservidorrembo |
---|
[34fc87f] | 513 | // |
---|
[b0bb14f] | 514 | // Descripción: |
---|
[9fd5e97] | 515 | // Esta función devuelve true o false dependiendo de que haya hueco en la tabla de sockets para un nuevo servidor rembo. |
---|
| 516 | // Parámetros: |
---|
| 517 | // - idx: Primer índice libre que se podrá utilizar |
---|
[34fc87f] | 518 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 519 | int hay_huecoservidorrembo(int *idx) { |
---|
[34fc87f] | 520 | int i; |
---|
[90f061f6] | 521 | for (i = 0; i < MAXIMOS_SRVRMB; i++) { |
---|
| 522 | if (strncmp(tbsocketsSRVRMB[i].ip, "\0", 1) == 0) { // Hay un hueco |
---|
| 523 | *idx = i; |
---|
| 524 | return (TRUE); |
---|
[34fc87f] | 525 | } |
---|
| 526 | } |
---|
[90f061f6] | 527 | return (FALSE); |
---|
[34fc87f] | 528 | } |
---|
| 529 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 530 | // Función: servidorrembo_existente |
---|
[34fc87f] | 531 | // |
---|
[b0bb14f] | 532 | // Descripción: |
---|
[9fd5e97] | 533 | // Esta función devuelve true o false dependiendo de si el servidor está registrado en la tabla de sockets |
---|
[34fc87f] | 534 | // Parametros: |
---|
[9fd5e97] | 535 | // - ip : La ip del cliente a buscar |
---|
| 536 | // - idx Indice que ocupará el servidor, de existir |
---|
[34fc87f] | 537 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 538 | BOOLEAN servidorrembo_existente(char *ip, int* idx) { |
---|
[34fc87f] | 539 | int i; |
---|
[90f061f6] | 540 | for (i = 0; i < MAXIMOS_SRVRMB; i++) { |
---|
| 541 | if (strcmp(ip, tbsocketsSRVRMB[i].ip) == 0) { // Si existe la IP ... |
---|
| 542 | *idx = i; |
---|
| 543 | return (TRUE); |
---|
[34fc87f] | 544 | } |
---|
| 545 | } |
---|
[90f061f6] | 546 | return (FALSE); |
---|
[34fc87f] | 547 | } |
---|
[277d0cd] | 548 | |
---|
| 549 | |
---|
| 550 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 551 | // Función: corte_iph |
---|
[34fc87f] | 552 | // |
---|
[9fd5e97] | 553 | // Descripción: |
---|
| 554 | // Esta función devuelve el valor del parametro iph incluido en la trama que debe ser el último parámetro de la trama. |
---|
[b0bb14f] | 555 | // Parámetros: |
---|
| 556 | // - parametros: Parámetros de la trama |
---|
[34fc87f] | 557 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 558 | char* corte_iph(char *parametros) { |
---|
| 559 | int i = 0; |
---|
[34fc87f] | 560 | char nombre_parametro[5]; |
---|
| 561 | |
---|
[90f061f6] | 562 | strcpy(nombre_parametro, "iph="); |
---|
| 563 | for (i = 0; i < LONGITUD_PARAMETROS - 4; i++) { |
---|
| 564 | if (parametros[i] == nombre_parametro[0]) { |
---|
| 565 | if (parametros[i + 1] == nombre_parametro[1]) { |
---|
| 566 | if (parametros[i + 2] == nombre_parametro[2]) { |
---|
| 567 | if (parametros[i + 3] == '=') { |
---|
| 568 | return (¶metros[i]); //Devuelve la posicion de comienzo de la iph |
---|
[34fc87f] | 569 | } |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | } |
---|
| 573 | } |
---|
[90f061f6] | 574 | return (NULL); |
---|
[34fc87f] | 575 | } |
---|
[b0bb14f] | 576 | |
---|
| 577 | // ________________________________________________________________________________________________________ |
---|
| 578 | // Función: escaparComillas |
---|
| 579 | // |
---|
| 580 | // Descripción: |
---|
| 581 | // Escapa las comillas simples de una cadena |
---|
[9fd5e97] | 582 | // Parámetros: |
---|
| 583 | // - cadena: Cadena de caracteres |
---|
| 584 | // Devuelve: |
---|
[b0bb14f] | 585 | // La cadena con las comillas escapadas "\'" |
---|
| 586 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 587 | char* escaparComillas(char *cadena) { |
---|
[b0bb14f] | 588 | |
---|
[90f061f6] | 589 | int lon, i, con = 0; |
---|
[b0bb14f] | 590 | char *cadenaescapada; |
---|
[90f061f6] | 591 | |
---|
| 592 | lon = strlen(cadena); |
---|
| 593 | for (i = 0; i < lon; i++) { // Cuenta las comillas |
---|
| 594 | if (cadena[i] == COMILLAS_SIMPES) |
---|
| 595 | con++; |
---|
| 596 | } |
---|
| 597 | if (con > 0) { // Existen comillas |
---|
| 598 | cadenaescapada = (char*) malloc(lon + con); // Toma memoria para la cadena escapada. |
---|
| 599 | if (cadenaescapada == NULL) |
---|
| 600 | return (NULL); |
---|
| 601 | int ptr = 0; |
---|
| 602 | for (i = 0; i < lon; i++) { |
---|
| 603 | if (cadena[i] == COMILLAS_SIMPES) |
---|
| 604 | cadenaescapada[ptr++] = BARRA_INVERTIDA; |
---|
| 605 | cadenaescapada[ptr++] = cadena[i]; |
---|
[b0bb14f] | 606 | } |
---|
[90f061f6] | 607 | } else |
---|
| 608 | cadenaescapada = cadena; |
---|
[b0bb14f] | 609 | |
---|
[90f061f6] | 610 | return (cadenaescapada); |
---|
[b0bb14f] | 611 | } |
---|
[34fc87f] | 612 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 613 | // Función: respuesta_cortesia |
---|
[34fc87f] | 614 | // |
---|
[b0bb14f] | 615 | // Descripción: |
---|
[9fd5e97] | 616 | // Envía respuesta de cortesía al cliente rembo |
---|
[b0bb14f] | 617 | // Parámetros: |
---|
[34fc87f] | 618 | // - s: Socket usado por el cliente para comunicarse con el servidor HIDRA |
---|
| 619 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 620 | int respuesta_cortesia(SOCKET s) { |
---|
[34fc87f] | 621 | char nwparametros[100]; |
---|
| 622 | |
---|
[90f061f6] | 623 | nwparametros[0] = '\0'; |
---|
| 624 | strcat(nwparametros, "nfn=Cortesia"); |
---|
| 625 | strcat(nwparametros, "\r"); |
---|
| 626 | return (manda_comando(s, nwparametros)); |
---|
[34fc87f] | 627 | } |
---|
| 628 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 629 | // Función: NoComandosPendientes |
---|
[34fc87f] | 630 | // |
---|
[b0bb14f] | 631 | // Descripción: |
---|
[9fd5e97] | 632 | // Envía respuesta de cortesía al cliente rembo |
---|
[b0bb14f] | 633 | // Parámetros: |
---|
[34fc87f] | 634 | // - s: Socket usado por el cliente para comunicarse con el servidor HIDRA |
---|
| 635 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 636 | int NoComandosPendientes(SOCKET s) { |
---|
[34fc87f] | 637 | char nwparametros[100]; |
---|
| 638 | |
---|
[90f061f6] | 639 | nwparametros[0] = '\0'; |
---|
| 640 | strcat(nwparametros, "nfn=NoComandosPtes"); |
---|
| 641 | strcat(nwparametros, "\r"); |
---|
| 642 | return (manda_comando(s, nwparametros)); |
---|
[34fc87f] | 643 | } |
---|
| 644 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 645 | // Función: InclusionCliente |
---|
[34fc87f] | 646 | // |
---|
[b0bb14f] | 647 | // Descripción: |
---|
[9fd5e97] | 648 | // Esta función incorpora el socket de un nuevo cliente a la tabla de sockets y le devuelve alguna de sus propiedades: nombre, |
---|
| 649 | // identificador, perfil hardware , mens... |
---|
[b0bb14f] | 650 | // Parámetros: |
---|
[34fc87f] | 651 | // - s: Socket del cliente |
---|
[b0bb14f] | 652 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 653 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 654 | int InclusionCliente(SOCKET s, char *parametros) { |
---|
| 655 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 656 | Database db; |
---|
| 657 | Table tbl; |
---|
| 658 | |
---|
[90f061f6] | 659 | char *iph, *cfg, *mac, *nau, *nor, *ipr, *ipd; |
---|
| 660 | int i, lon, glon, idx, resul, puertorepo; |
---|
[34fc87f] | 661 | char nwparametros[LONGITUD_PARAMETROS]; |
---|
[90f061f6] | 662 | char ipservidordhcp[16], ipservidorrembo[16], nombreordenador[100]; |
---|
| 663 | int idordenador, idaula, idconfiguracion, idparticion, idperfilhard, |
---|
| 664 | idmenu, cache; |
---|
[34fc87f] | 665 | |
---|
[9fd5e97] | 666 | // Toma parámetros |
---|
[90f061f6] | 667 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 668 | mac = toma_parametro("mac", parametros); // Toma mac |
---|
| 669 | cfg = toma_parametro("cfg", parametros); // Toma configuracion |
---|
| 670 | nau = toma_parametro("nau", parametros); // Toma nombre del grupo em el fichero config de rembo |
---|
| 671 | nor = toma_parametro("nor", parametros); // Toma nombre del ordenador en el fichero config de rembo |
---|
| 672 | ipd = toma_parametro("ipd", parametros); // Toma ip del servidor dhcpd |
---|
| 673 | ipr = toma_parametro("ipr", parametros); // Toma ip del servidor rembo |
---|
[34fc87f] | 674 | |
---|
| 675 | // Toma las propiedades del ordenador |
---|
[90f061f6] | 676 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
| 677 | RegistraLog("Error de conexión con la base de datos", false); |
---|
[34fc87f] | 678 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 679 | return (false); |
---|
[34fc87f] | 680 | } |
---|
| 681 | // Recupera los datos del ordenador |
---|
[90f061f6] | 682 | sprintf( |
---|
| 683 | sqlstr, |
---|
| 684 | "SELECT ordenadores.idordenador,ordenadores.idaula,ordenadores.nombreordenador, ordenadores.idperfilhard, ordenadores.idconfiguracion,ordenadores.idparticion,servidoresrembo.ip AS ipservidorrembo,servidoresrembo.puertorepo, ordenadores.idmenu,ordenadores.cache FROM ordenadores INNER JOIN servidoresrembo ON ordenadores.idservidorrembo = servidoresrembo.idservidorrembo WHERE ordenadores.ip = '%s'", |
---|
| 685 | iph); |
---|
[34fc87f] | 686 | |
---|
[90f061f6] | 687 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
| 688 | RegistraLog("Error al ejecutar la consulta", false); |
---|
[34fc87f] | 689 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 690 | return (false); |
---|
| 691 | } |
---|
| 692 | if (tbl.ISEOF()) { // Si No existe registro |
---|
| 693 | RegistraLog("Cliente No encontrado, se rechaza la petición", false); |
---|
[9fd5e97] | 694 | if (aulaup == AUTOINCORPORACION_OFF) // No está activada la incorporación automática |
---|
[90f061f6] | 695 | return (false); |
---|
| 696 | if (!cuestion_nuevoordenador(db, tbl, &idordenador, nau, nor, iph, mac, |
---|
[9fd5e97] | 697 | cfg, ipd, ipr)) // Ha habido algún error en la incorporación automónica |
---|
[90f061f6] | 698 | return (false); |
---|
[34fc87f] | 699 | // Valores por defecto del nuevo ordenador |
---|
[90f061f6] | 700 | strcpy(nombreordenador, nor); |
---|
| 701 | idperfilhard = 0; |
---|
| 702 | strcpy(ipservidordhcp, ipd); |
---|
| 703 | strcpy(ipservidorrembo, ipr); |
---|
| 704 | idmenu = 0; |
---|
| 705 | } else { |
---|
| 706 | // sprintf(msglog,"Petición de Inclusión del CLiente:%s",iph); |
---|
| 707 | // RegistraLog(msglog,false); |
---|
| 708 | |
---|
| 709 | if (!tbl.Get("idordenador", idordenador)) { // Toma dato |
---|
| 710 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 711 | return (false); |
---|
[34fc87f] | 712 | } |
---|
[90f061f6] | 713 | if (!tbl.Get("nombreordenador", nombreordenador)) { // Toma dato |
---|
| 714 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 715 | return (false); |
---|
| 716 | } |
---|
| 717 | if (!tbl.Get("idaula", idaula)) { // Toma dato |
---|
| 718 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 719 | return (false); |
---|
[34fc87f] | 720 | } |
---|
| 721 | |
---|
[90f061f6] | 722 | if (!tbl.Get("idconfiguracion", idconfiguracion)) { // Toma dato |
---|
| 723 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 724 | return (false); |
---|
[34fc87f] | 725 | } |
---|
[90f061f6] | 726 | if (!tbl.Get("idparticion", idparticion)) { // Toma dato |
---|
| 727 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 728 | return (false); |
---|
[34fc87f] | 729 | } |
---|
[90f061f6] | 730 | if (!tbl.Get("idperfilhard", idperfilhard)) { // Toma dato |
---|
| 731 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 732 | return (false); |
---|
[34fc87f] | 733 | } |
---|
[51dcdc7] | 734 | /* |
---|
[90f061f6] | 735 | if(!tbl.Get("ipservidordhcp",ipservidordhcp)){ // Toma dato |
---|
| 736 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 737 | return(false); |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | lon=strlen(ipservidordhcp); |
---|
| 741 | for (i=0;i<lon;i++){ |
---|
| 742 | if(ipservidordhcp[i]==' ') { |
---|
| 743 | ipservidordhcp[i]='\0'; |
---|
| 744 | break; |
---|
| 745 | } |
---|
| 746 | } |
---|
| 747 | */ |
---|
| 748 | if (!tbl.Get("ipservidorrembo", ipservidorrembo)) { // Toma dato |
---|
| 749 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 750 | return (false); |
---|
[34fc87f] | 751 | } |
---|
[90f061f6] | 752 | lon = strlen(ipservidorrembo); |
---|
| 753 | for (i = 0; i < lon; i++) { |
---|
| 754 | if (ipservidorrembo[i] == ' ') { |
---|
| 755 | ipservidorrembo[i] = '\0'; |
---|
[34fc87f] | 756 | break; |
---|
| 757 | } |
---|
| 758 | } |
---|
[90f061f6] | 759 | if (!tbl.Get("puertorepo", puertorepo)) { // Toma dato |
---|
| 760 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 761 | return (false); |
---|
[34fc87f] | 762 | } |
---|
[90f061f6] | 763 | |
---|
| 764 | if (!tbl.Get("idmenu", idmenu)) { // Toma dato |
---|
| 765 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 766 | return (false); |
---|
[34fc87f] | 767 | } |
---|
[90f061f6] | 768 | if (!tbl.Get("cache", cache)) { // Toma dato |
---|
| 769 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 770 | return (false); |
---|
[34fc87f] | 771 | } |
---|
[90f061f6] | 772 | resul = actualiza_configuracion(db, tbl, cfg, idconfiguracion, |
---|
[9fd5e97] | 773 | idparticion, iph); // Actualiza la configuración del ordenador |
---|
[90f061f6] | 774 | if (!resul) { |
---|
| 775 | pthread_mutex_unlock(&guardia); |
---|
| 776 | return (false); |
---|
[34fc87f] | 777 | } |
---|
| 778 | } |
---|
[9fd5e97] | 779 | // Incluyendo al cliente en la tabla de sockets |
---|
[90f061f6] | 780 | if (cliente_existente(iph, &i)) { // Si ya existe la IP ... |
---|
| 781 | idx = i; |
---|
[34fc87f] | 782 | //close(tbsockets[idx].sock); |
---|
[90f061f6] | 783 | } else { |
---|
| 784 | if (hay_hueco(&i)) { // Busca hueco para el nuevo cliente |
---|
| 785 | idx = i; |
---|
| 786 | strcpy(tbsockets[idx].ip, iph);// Copia IP |
---|
| 787 | } else |
---|
| 788 | return (false); // No hay huecos |
---|
| 789 | } |
---|
| 790 | strcpy(tbsockets[idx].estado, CLIENTE_INICIANDO); // Actualiza el estado del cliente |
---|
| 791 | tbsockets[idx].sock = s; // Guarda el socket |
---|
[51dcdc7] | 792 | //strcpy(tbsockets[idx].ipsrvdhcp,ipservidordhcp);// Guarda IP servidor dhcp |
---|
[90f061f6] | 793 | strcpy(tbsockets[idx].ipsrvrmb, ipservidorrembo);// Guarda IP servidor rembo |
---|
[34fc87f] | 794 | |
---|
[90f061f6] | 795 | inclusion_srvRMB(ipservidorrembo, puertorepo); // Actualiza tabla de servidores rembo |
---|
[34fc87f] | 796 | |
---|
| 797 | // Prepara la trama |
---|
[90f061f6] | 798 | lon = sprintf(nwparametros, "nfn=RESPUESTA_InclusionCliente\r"); |
---|
| 799 | lon += sprintf(nwparametros + lon, "ido=%d\r", idordenador); |
---|
| 800 | lon += sprintf(nwparametros + lon, "npc=%s\r", nombreordenador); |
---|
| 801 | lon += sprintf(nwparametros + lon, "ida=%d\r", idaula); |
---|
| 802 | lon += sprintf(nwparametros + lon, "hrd=%s\r", servidorhidra); |
---|
| 803 | lon += sprintf(nwparametros + lon, "prt=%d\r", puerto); |
---|
| 804 | lon += sprintf(nwparametros + lon, "ifh=%d\r", idperfilhard); |
---|
| 805 | lon += sprintf(nwparametros + lon, "che=%d\r", cache); |
---|
| 806 | lon += sprintf(nwparametros + lon, "ipr=%s\r", ipservidorrembo); |
---|
| 807 | lon += sprintf(nwparametros + lon, "rep=%d\r", puertorepo); |
---|
| 808 | glon = lon; |
---|
| 809 | if (!Toma_menu(db, tbl, nwparametros, idmenu, lon)) |
---|
| 810 | nwparametros[glon] = (char) NULL; |
---|
[34fc87f] | 811 | db.Close(); |
---|
[90f061f6] | 812 | return (manda_comando(s, nwparametros)); |
---|
[34fc87f] | 813 | } |
---|
| 814 | // ________________________________________________________________________________________________________ |
---|
| 815 | // Función: Toma menu |
---|
| 816 | // |
---|
[b0bb14f] | 817 | // Descripción: |
---|
[9fd5e97] | 818 | // Esta función toma los parametros del menu inicial del cliente rembo y se los envía en el proceso de inclusión |
---|
[b0bb14f] | 819 | // Parámetros: |
---|
[9fd5e97] | 820 | // - db: Base de datos |
---|
| 821 | // - tbl: Objeto tabla |
---|
| 822 | // - nwparametros: Cadena con los parámetros a enviar al cliente |
---|
| 823 | // - idmenu: Identificador del menú |
---|
| 824 | // - lon : Longitud inicial de la cadena de parámetros |
---|
[34fc87f] | 825 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 826 | int Toma_menu(Database db, Table tbl, char* nwparametros, int idmenu, int lon) { |
---|
[34fc87f] | 827 | Table littbl; |
---|
| 828 | |
---|
[90f061f6] | 829 | char sqlstr[1000], ErrStr[200], titulo[250], descripitem[250], urlimg[250]; |
---|
| 830 | int idaccionmenu, idtipoaccion, coorx, coory, idurlimg; |
---|
| 831 | int modalidad, resolucion, tipoaccion, tipoitem; |
---|
| 832 | char htmlmenupub[250], htmlmenupri[250]; |
---|
[34fc87f] | 833 | |
---|
[90f061f6] | 834 | sprintf( |
---|
| 835 | sqlstr, |
---|
| 836 | "SELECT menus.resolucion,menus.titulo,menus.coorx,menus.coory,menus.modalidad,menus.scoorx,menus.scoory,menus.smodalidad,menus.htmlmenupub,menus.htmlmenupri,acciones_menus.tipoaccion,acciones_menus.idaccionmenu,acciones_menus.idtipoaccion,acciones_menus.tipoitem,acciones_menus.descripitem,acciones_menus.idurlimg FROM acciones_menus INNER JOIN menus ON acciones_menus.idmenu = menus.idmenu WHERE menus.idmenu=%d order by acciones_menus.orden", |
---|
| 837 | idmenu); |
---|
| 838 | |
---|
| 839 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 840 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 841 | return (false); |
---|
[34fc87f] | 842 | } |
---|
[90f061f6] | 843 | if (tbl.ISEOF()) |
---|
| 844 | return (true); |
---|
| 845 | |
---|
| 846 | if (!tbl.Get("titulo", titulo)) { // Toma dato |
---|
[34fc87f] | 847 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 848 | return (false); |
---|
[34fc87f] | 849 | } |
---|
[90f061f6] | 850 | if (!tbl.Get("coorx", coorx)) { // Toma dato |
---|
[34fc87f] | 851 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 852 | return (false); |
---|
[34fc87f] | 853 | } |
---|
[90f061f6] | 854 | if (!tbl.Get("coory", coory)) { // Toma dato |
---|
[34fc87f] | 855 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 856 | return (false); |
---|
[34fc87f] | 857 | } |
---|
[90f061f6] | 858 | if (!tbl.Get("modalidad", modalidad)) { // Toma dato |
---|
[34fc87f] | 859 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 860 | return (false); |
---|
[34fc87f] | 861 | } |
---|
[90f061f6] | 862 | lon += sprintf(nwparametros + lon, "cmn=%s&%d&%d&%d&", titulo, coorx, |
---|
| 863 | coory, modalidad); // Cabecera de menu |
---|
[34fc87f] | 864 | |
---|
[90f061f6] | 865 | if (!tbl.Get("scoorx", coorx)) { // Toma dato |
---|
[34fc87f] | 866 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 867 | return (false); |
---|
[34fc87f] | 868 | } |
---|
[90f061f6] | 869 | if (!tbl.Get("scoory", coory)) { // Toma dato |
---|
[34fc87f] | 870 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 871 | return (false); |
---|
[34fc87f] | 872 | } |
---|
[90f061f6] | 873 | if (!tbl.Get("smodalidad", modalidad)) { // Toma dato |
---|
[34fc87f] | 874 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 875 | return (false); |
---|
[34fc87f] | 876 | } |
---|
[90f061f6] | 877 | lon += sprintf(nwparametros + lon, "%d&%d&%d", coorx, coory, modalidad); // Cabecera de menu |
---|
[34fc87f] | 878 | |
---|
[90f061f6] | 879 | if (!tbl.Get("resolucion", resolucion)) { // Toma dato |
---|
[34fc87f] | 880 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 881 | return (false); |
---|
[34fc87f] | 882 | } |
---|
[90f061f6] | 883 | lon += sprintf(nwparametros + lon, "&%d\r", resolucion); // Resolucion de la pantalla |
---|
[34fc87f] | 884 | |
---|
[90f061f6] | 885 | if (!tbl.Get("htmlmenupub", htmlmenupub)) { // Toma dato |
---|
[34fc87f] | 886 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 887 | return (false); |
---|
[34fc87f] | 888 | } |
---|
[90f061f6] | 889 | if (!tbl.Get("htmlmenupri", htmlmenupri)) { // Toma dato |
---|
[34fc87f] | 890 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 891 | return (false); |
---|
[34fc87f] | 892 | } |
---|
[90f061f6] | 893 | lon += sprintf(nwparametros + lon, "htm=%s;%s\r", htmlmenupub, htmlmenupri); // Html de menu |
---|
[34fc87f] | 894 | |
---|
[90f061f6] | 895 | lon += sprintf(nwparametros + lon, "mnu="); |
---|
| 896 | while (!tbl.ISEOF()) { // Recorre acciones del menu |
---|
| 897 | if (!tbl.Get("tipoaccion", tipoaccion)) { // Toma dato |
---|
[34fc87f] | 898 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 899 | return (false); |
---|
[34fc87f] | 900 | } |
---|
[90f061f6] | 901 | if (!tbl.Get("tipoitem", tipoitem)) { // Toma dato |
---|
[34fc87f] | 902 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 903 | return (false); |
---|
[34fc87f] | 904 | } |
---|
[90f061f6] | 905 | if (!tbl.Get("idtipoaccion", idtipoaccion)) { // Toma dato |
---|
[34fc87f] | 906 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 907 | return (false); |
---|
[34fc87f] | 908 | } |
---|
[90f061f6] | 909 | if (!tbl.Get("idaccionmenu", idaccionmenu)) { // Toma dato |
---|
[34fc87f] | 910 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 911 | return (false); |
---|
[34fc87f] | 912 | } |
---|
[90f061f6] | 913 | if (!tbl.Get("descripitem", descripitem)) { // Toma dato |
---|
[34fc87f] | 914 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 915 | return (false); |
---|
[34fc87f] | 916 | } |
---|
[90f061f6] | 917 | if (!tbl.Get("idurlimg", idurlimg)) { // Toma dato |
---|
[34fc87f] | 918 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 919 | return (false); |
---|
[34fc87f] | 920 | } |
---|
[90f061f6] | 921 | |
---|
| 922 | sprintf(sqlstr, "SELECT urlicono FROM iconos WHERE idicono=%d", |
---|
| 923 | idurlimg); |
---|
| 924 | if (!db.Execute(sqlstr, littbl)) { // Error al leer |
---|
[34fc87f] | 925 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 926 | return (false); |
---|
[34fc87f] | 927 | } |
---|
[90f061f6] | 928 | if (!littbl.ISEOF()) { |
---|
| 929 | if (!littbl.Get("urlicono", urlimg)) { // Toma dato |
---|
[34fc87f] | 930 | littbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 931 | return (false); |
---|
[34fc87f] | 932 | } |
---|
[90f061f6] | 933 | } else |
---|
| 934 | sprintf(urlimg, "itemdefault.pcx"); |
---|
[34fc87f] | 935 | |
---|
[90f061f6] | 936 | lon += sprintf(nwparametros + lon, "%d&%s&%s&%d&%d\?", idaccionmenu, |
---|
| 937 | urlimg, descripitem, tipoitem, tipoaccion); |
---|
[34fc87f] | 938 | tbl.MoveNext(); |
---|
| 939 | } |
---|
[90f061f6] | 940 | nwparametros[lon - 1] = '\r'; |
---|
| 941 | nwparametros[lon] = (char) NULL; |
---|
| 942 | return (true); |
---|
[34fc87f] | 943 | } |
---|
| 944 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 945 | // Función:RecuperaItem |
---|
[34fc87f] | 946 | // |
---|
[b0bb14f] | 947 | // Descripción: |
---|
[9fd5e97] | 948 | // Esta función busca en la base de datos, los parametros de un items de un menu |
---|
[b0bb14f] | 949 | // Parámetros: |
---|
[34fc87f] | 950 | // - s: Socket del cliente |
---|
[b0bb14f] | 951 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 952 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 953 | int RecuperaItem(SOCKET s, char *parametros) { |
---|
| 954 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 955 | Database db; |
---|
| 956 | Table tbl; |
---|
| 957 | char *ida; |
---|
[90f061f6] | 958 | int idtipoaccion, tipoaccion; |
---|
[34fc87f] | 959 | |
---|
| 960 | // Toma parnetros |
---|
[90f061f6] | 961 | ida = toma_parametro("ida", parametros); // Toma identificador de la acción |
---|
[34fc87f] | 962 | |
---|
| 963 | // Abre conexin con la base de datos |
---|
[90f061f6] | 964 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 965 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 966 | return (false); |
---|
[34fc87f] | 967 | } |
---|
[90f061f6] | 968 | sprintf( |
---|
| 969 | sqlstr, |
---|
| 970 | "SELECT tipoaccion,idtipoaccion FROM acciones_menus WHERE idaccionmenu=%s", |
---|
| 971 | ida); |
---|
| 972 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 973 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 974 | return (false); |
---|
[34fc87f] | 975 | } |
---|
[90f061f6] | 976 | if (tbl.ISEOF()) |
---|
| 977 | return (false); |
---|
| 978 | |
---|
| 979 | if (!tbl.Get("tipoaccion", tipoaccion)) { // Toma tipo de acción |
---|
[34fc87f] | 980 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 981 | return (false); |
---|
[34fc87f] | 982 | } |
---|
[90f061f6] | 983 | if (!tbl.Get("idtipoaccion", idtipoaccion)) { // Toma identificador del tipo de acción |
---|
[34fc87f] | 984 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 985 | return (false); |
---|
| 986 | } |
---|
| 987 | switch (tipoaccion) { |
---|
| 988 | case EJECUCION_PROCEDIMIENTO: |
---|
| 989 | sprintf( |
---|
| 990 | sqlstr, |
---|
| 991 | "SELECT procedimientos_comandos.parametros FROM procedimientos_comandos WHERE procedimientos_comandos.idprocedimiento=%d", |
---|
| 992 | idtipoaccion); |
---|
| 993 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 994 | db.GetErrorErrStr(ErrStr); |
---|
| 995 | return (false); |
---|
| 996 | } |
---|
| 997 | if (tbl.ISEOF()) // No existe procedimiento |
---|
| 998 | return (false); |
---|
[34fc87f] | 999 | |
---|
[90f061f6] | 1000 | while (!tbl.ISEOF()) { |
---|
| 1001 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
| 1002 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1003 | return (false); |
---|
[34fc87f] | 1004 | } |
---|
[90f061f6] | 1005 | tbl.MoveNext(); |
---|
| 1006 | } |
---|
| 1007 | break; |
---|
| 1008 | case EJECUCION_TAREA: |
---|
| 1009 | //Las tareas no se recuperan como fichero de items; |
---|
| 1010 | break; |
---|
| 1011 | case EJECUCION_TRABAJO: |
---|
| 1012 | //Los t rabajos no se recuperan como fichero de items; |
---|
| 1013 | break; |
---|
[34fc87f] | 1014 | } |
---|
| 1015 | db.Close(); |
---|
[90f061f6] | 1016 | return (manda_comando(s, parametros)); |
---|
[34fc87f] | 1017 | } |
---|
| 1018 | |
---|
| 1019 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 1020 | // Función: actualiza_hardware |
---|
[34fc87f] | 1021 | // |
---|
[b0bb14f] | 1022 | // Descripción: |
---|
[9fd5e97] | 1023 | // Esta función actualiza la base de datos con la configuracion de sistemas operativos y particiones de un ordenador |
---|
[b0bb14f] | 1024 | // Parámetros: |
---|
[34fc87f] | 1025 | // - db: Objeto base de datos (ya operativo) |
---|
| 1026 | // - tbl: Objeto tabla |
---|
[9fd5e97] | 1027 | // - hrd: El path del archivo de inventario |
---|
| 1028 | // - ip: Ip del cliente |
---|
| 1029 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
[34fc87f] | 1030 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1031 | int actualiza_hardware(Database db, Table tbl, char* hrd, char* ip, char*ido) { |
---|
[a6b881e] | 1032 | int idtipohardware; |
---|
[90f061f6] | 1033 | int i, lon = 0, idcentro, widcentro; |
---|
| 1034 | char *tbHardware[MAXHARDWARE]; |
---|
| 1035 | int tbidhardware[MAXHARDWARE]; |
---|
| 1036 | char *dualHardware[2]; |
---|
[9fd5e97] | 1037 | char ch[2]; // Carácter delimitador |
---|
[90f061f6] | 1038 | char sqlstr[1000], ErrStr[200], descripcion[250], nombreordenador[250]; |
---|
[ae04987] | 1039 | |
---|
[34fc87f] | 1040 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[9fd5e97] | 1041 | // ACCESO único A TRAVES DE OBJETO MUTEX a este trozo de código |
---|
[90f061f6] | 1042 | pthread_mutex_lock(&guardia); |
---|
| 1043 | |
---|
[34fc87f] | 1044 | // Toma Centro |
---|
[90f061f6] | 1045 | sprintf( |
---|
| 1046 | sqlstr, |
---|
| 1047 | "SELECT aulas.idcentro,ordenadores.nombreordenador FROM aulas INNER JOIN ordenadores ON aulas.idaula=ordenadores.idaula WHERE ordenadores.idordenador=%s", |
---|
| 1048 | ido); |
---|
| 1049 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1050 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1051 | pthread_mutex_unlock(&guardia); |
---|
| 1052 | return (false); |
---|
| 1053 | } |
---|
| 1054 | if (!tbl.Get("idcentro", widcentro)) { // Toma dato |
---|
[34fc87f] | 1055 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1056 | pthread_mutex_unlock(&guardia); |
---|
| 1057 | return (false); |
---|
| 1058 | } |
---|
| 1059 | idcentro = widcentro + 0; // Bug Mysql |
---|
[ae04987] | 1060 | |
---|
[90f061f6] | 1061 | if (!tbl.Get("nombreordenador", nombreordenador)) { // Toma dato |
---|
[34fc87f] | 1062 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1063 | pthread_mutex_unlock(&guardia); |
---|
| 1064 | return (false); |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | if (lon > MAXHARDWARE) |
---|
| 1068 | lon = MAXHARDWARE; |
---|
[b0bb14f] | 1069 | |
---|
| 1070 | // Lee archivo de inventario hardware |
---|
| 1071 | FILE *Finv; |
---|
| 1072 | char *buffer; |
---|
| 1073 | long lSize; |
---|
[9fd5e97] | 1074 | Finv = fopen(hrd, "rb"); // EL parámetro sft contiene el path del archivo de inventario |
---|
[90f061f6] | 1075 | if (Finv == NULL) |
---|
| 1076 | return (false); |
---|
| 1077 | fseek(Finv, 0, SEEK_END); // Obtiene tamaño del fichero. |
---|
| 1078 | lSize = ftell(Finv); |
---|
| 1079 | if (lSize == 0) |
---|
| 1080 | return (false); |
---|
| 1081 | rewind(Finv); |
---|
| 1082 | buffer = (char*) malloc(lSize); // Toma memoria para el buffer de lectura. |
---|
| 1083 | if (buffer == NULL) |
---|
| 1084 | return (false); |
---|
| 1085 | fread(buffer, 1, lSize, Finv); // Lee contenido del fichero |
---|
[b0bb14f] | 1086 | fclose(Finv); |
---|
[90f061f6] | 1087 | buffer = escaparComillas(buffer); |
---|
| 1088 | |
---|
[b0bb14f] | 1089 | // Trocea la cadena de configuración |
---|
[90f061f6] | 1090 | strcpy(ch, "\n");// caracter delimitador |
---|
| 1091 | lon = split_parametros(tbHardware, buffer, ch); |
---|
| 1092 | |
---|
[2f4f9ff] | 1093 | /* |
---|
[90f061f6] | 1094 | for (i=0;i<lon;i++){ |
---|
| 1095 | sprintf(msglog,"Linea de inventario: %s",tbHardware[i]); |
---|
| 1096 | RegistraLog(msglog,false); |
---|
| 1097 | } |
---|
| 1098 | */ |
---|
| 1099 | |
---|
[9fd5e97] | 1100 | // Trocea las cadenas de parametros de partición |
---|
[90f061f6] | 1101 | for (i = 0; i < lon; i++) { |
---|
| 1102 | strcpy(ch, "=");// caracter delimitador "=" |
---|
| 1103 | split_parametros(dualHardware, tbHardware[i], ch); |
---|
| 1104 | |
---|
[2f4f9ff] | 1105 | //sprintf(msglog,"nemonico: %s",dualHardware[0]); |
---|
| 1106 | //RegistraLog(msglog,false); |
---|
| 1107 | //sprintf(msglog,"valor: %s",dualHardware[1]); |
---|
| 1108 | //RegistraLog(msglog,false); |
---|
[90f061f6] | 1109 | |
---|
| 1110 | |
---|
| 1111 | sprintf( |
---|
| 1112 | sqlstr, |
---|
| 1113 | "SELECT idtipohardware,descripcion FROM tipohardwares WHERE nemonico='%s'", |
---|
| 1114 | dualHardware[0]); |
---|
| 1115 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1116 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1117 | pthread_mutex_unlock(&guardia); |
---|
| 1118 | return (false); |
---|
| 1119 | } |
---|
| 1120 | if (tbl.ISEOF()) { // Tipo de Hardware NO existente |
---|
| 1121 | sprintf( |
---|
| 1122 | msglog, |
---|
| 1123 | "Existe un tipo de hardware que no está registrado (nemónico:%s). Se rechaza proceso de inventario", |
---|
| 1124 | dualHardware[0]); |
---|
| 1125 | RegistraLog(msglog, false); |
---|
| 1126 | pthread_mutex_unlock(&guardia); |
---|
| 1127 | return (false); |
---|
| 1128 | } else { // Tipo de Hardware Existe |
---|
| 1129 | if (!tbl.Get("idtipohardware", idtipohardware)) { // Toma dato |
---|
[34fc87f] | 1130 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1131 | pthread_mutex_unlock(&guardia); |
---|
| 1132 | return (false); |
---|
[34fc87f] | 1133 | } |
---|
[90f061f6] | 1134 | if (!tbl.Get("descripcion", descripcion)) { // Toma dato |
---|
[34fc87f] | 1135 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1136 | pthread_mutex_unlock(&guardia); |
---|
| 1137 | return (false); |
---|
[ae04987] | 1138 | } |
---|
| 1139 | |
---|
[90f061f6] | 1140 | sprintf( |
---|
| 1141 | sqlstr, |
---|
| 1142 | "SELECT idhardware FROM hardwares WHERE idtipohardware=%d AND descripcion='%s'", |
---|
| 1143 | idtipohardware, dualHardware[1]); |
---|
| 1144 | |
---|
[34fc87f] | 1145 | // EJecuta consulta |
---|
[90f061f6] | 1146 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1147 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1148 | pthread_mutex_unlock(&guardia); |
---|
| 1149 | return (false); |
---|
| 1150 | } |
---|
[ae04987] | 1151 | |
---|
[90f061f6] | 1152 | if (tbl.ISEOF()) { // Hardware NO existente |
---|
| 1153 | sprintf( |
---|
| 1154 | sqlstr, |
---|
| 1155 | "INSERT hardwares (idtipohardware,descripcion,idcentro,grupoid) VALUES(%d,'%s',%d,0)", |
---|
| 1156 | idtipohardware, dualHardware[1], idcentro); |
---|
| 1157 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 1158 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1159 | pthread_mutex_unlock(&guardia); |
---|
| 1160 | return (false); |
---|
| 1161 | } |
---|
[34fc87f] | 1162 | // Recupera el identificador del hardware |
---|
[90f061f6] | 1163 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1164 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1165 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1166 | pthread_mutex_unlock(&guardia); |
---|
| 1167 | return (false); |
---|
[34fc87f] | 1168 | } |
---|
[90f061f6] | 1169 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1170 | if (!tbl.Get("identificador", tbidhardware[i])) { |
---|
| 1171 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1172 | pthread_mutex_unlock(&guardia); |
---|
| 1173 | return (false); |
---|
[34fc87f] | 1174 | } |
---|
[90f061f6] | 1175 | } |
---|
| 1176 | } else { |
---|
| 1177 | if (!tbl.Get("idhardware", tbidhardware[i])) { // Toma dato |
---|
[34fc87f] | 1178 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1179 | pthread_mutex_unlock(&guardia); |
---|
| 1180 | return (false); |
---|
| 1181 | } |
---|
[34fc87f] | 1182 | } |
---|
[90f061f6] | 1183 | } // Fin for |
---|
[34fc87f] | 1184 | } |
---|
[90f061f6] | 1185 | // Comprueba existencia de perfil hardware y actualización de éste para el ordenador |
---|
| 1186 | if (!CuestionPerfilHardware(db, tbl, idcentro, ido, tbidhardware, i, |
---|
| 1187 | nombreordenador)) { |
---|
[34fc87f] | 1188 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1189 | pthread_mutex_unlock(&guardia); |
---|
| 1190 | return (false); |
---|
| 1191 | } |
---|
| 1192 | pthread_mutex_unlock(&guardia); |
---|
[34fc87f] | 1193 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[90f061f6] | 1194 | return (true); |
---|
[34fc87f] | 1195 | } |
---|
| 1196 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1197 | // Función: CuestionPerfilHardware |
---|
[9fd5e97] | 1198 | // |
---|
| 1199 | // Parámetros: |
---|
| 1200 | // - db: Objeto base de datos (ya operativo) |
---|
| 1201 | // - tbl: Objeto tabla |
---|
| 1202 | // - idcentro: Identificador del centro |
---|
| 1203 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
| 1204 | // - tbidhardware: Identificador hardware |
---|
| 1205 | // - nombreordenador: Nombre del ordenador del cliente |
---|
[34fc87f] | 1206 | //________________________________________________________________________________________________________/ |
---|
[90f061f6] | 1207 | int CuestionPerfilHardware(Database db, Table tbl, int idcentro, char* ido, |
---|
| 1208 | int *tbidhardware, int i, char *nombreordenador) { |
---|
| 1209 | char sqlstr[1000], ErrStr[200]; |
---|
| 1210 | int tbidhardwareperfil[MAXHARDWARE]; |
---|
| 1211 | int j = 0; |
---|
[34fc87f] | 1212 | int idperfilhard; |
---|
| 1213 | // Busca perfil hard del ordenador |
---|
[90f061f6] | 1214 | sprintf( |
---|
| 1215 | sqlstr, |
---|
| 1216 | "SELECT perfileshard_hardwares.idhardware FROM ordenadores INNER JOIN perfileshard ON ordenadores.idperfilhard = perfileshard.idperfilhard INNER JOIN perfileshard_hardwares ON perfileshard_hardwares.idperfilhard = perfileshard.idperfilhard WHERE ordenadores.idordenador =%s", |
---|
| 1217 | ido); |
---|
[34fc87f] | 1218 | // EJecuta consulta |
---|
[90f061f6] | 1219 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1220 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1221 | return (false); |
---|
| 1222 | } |
---|
| 1223 | while (!tbl.ISEOF()) { // Recorre acciones del menu |
---|
| 1224 | if (!tbl.Get("idhardware", tbidhardwareperfil[j++])) { // Toma dato |
---|
[34fc87f] | 1225 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1226 | return (false); |
---|
| 1227 | } |
---|
[34fc87f] | 1228 | tbl.MoveNext(); |
---|
| 1229 | } |
---|
| 1230 | // Comprueba si el perfil del ordenador contiene todo el hardware enviado |
---|
[90f061f6] | 1231 | int k, q, sw = false; |
---|
| 1232 | for (k = 0; k < i; k++) { // Elemento hardware |
---|
| 1233 | for (q = 0; q < j; q++) { |
---|
| 1234 | if (tbidhardware[k] == tbidhardwareperfil[q]) { |
---|
| 1235 | sw = true; |
---|
[34fc87f] | 1236 | break; |
---|
| 1237 | } |
---|
| 1238 | } |
---|
[90f061f6] | 1239 | if (!sw) |
---|
| 1240 | break; |
---|
[34fc87f] | 1241 | } |
---|
| 1242 | // La variable sw contiene false si se ha encontrado algún hardware que no está en el perfil hardware del ordenador |
---|
[90f061f6] | 1243 | if (sw) |
---|
| 1244 | return (true); // Todo el hardware está en el perfil actual |
---|
| 1245 | |
---|
[34fc87f] | 1246 | // Crea perfil nuevo con todo el hardware inventariado |
---|
[90f061f6] | 1247 | sprintf( |
---|
| 1248 | sqlstr, |
---|
| 1249 | "INSERT perfileshard (descripcion,idcentro,grupoid) VALUES('Perfil Hardware (%s)',%d,0)", |
---|
| 1250 | nombreordenador, idcentro); |
---|
| 1251 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 1252 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1253 | return (false); |
---|
| 1254 | } |
---|
[34fc87f] | 1255 | // Recupera el identificador del hardware |
---|
[90f061f6] | 1256 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1257 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1258 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1259 | return (false); |
---|
[34fc87f] | 1260 | } |
---|
[90f061f6] | 1261 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1262 | if (!tbl.Get("identificador", idperfilhard)) { |
---|
[34fc87f] | 1263 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1264 | return (false); |
---|
[34fc87f] | 1265 | } |
---|
[90f061f6] | 1266 | } |
---|
| 1267 | for (k = 0; k < i; k++) { // relaciona elementos hardwares con el nuevo perfil hardware |
---|
| 1268 | sprintf( |
---|
| 1269 | sqlstr, |
---|
| 1270 | "INSERT perfileshard_hardwares (idperfilhard,idhardware) VALUES(%d,%d)", |
---|
| 1271 | idperfilhard, tbidhardware[k]); |
---|
| 1272 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 1273 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1274 | return (false); |
---|
| 1275 | } |
---|
| 1276 | } |
---|
| 1277 | sprintf(sqlstr, |
---|
| 1278 | "UPDATE ordenadores SET idperfilhard=%d WHERE idordenador=%s", |
---|
| 1279 | idperfilhard, ido); |
---|
| 1280 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 1281 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1282 | return (false); |
---|
| 1283 | } |
---|
| 1284 | return (true); |
---|
[34fc87f] | 1285 | } |
---|
| 1286 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 1287 | // Función: actualiza_software |
---|
[34fc87f] | 1288 | // |
---|
[b0bb14f] | 1289 | // Descripción: |
---|
[9fd5e97] | 1290 | // Esta función actualiza la base de datos con la configuración de sistemas operativos y particiones de un ordenador |
---|
[b0bb14f] | 1291 | // Parámetros: |
---|
[34fc87f] | 1292 | // - db: Objeto base de datos (ya operativo) |
---|
| 1293 | // - tbl: Objeto tabla |
---|
[9fd5e97] | 1294 | // - sft: Software |
---|
| 1295 | // - par: Partición |
---|
| 1296 | // - tfs: Tipo de partición |
---|
[34fc87f] | 1297 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1298 | int actualiza_software(Database db, Table tbl, char* sft, char* par, char* tfs, |
---|
| 1299 | char* ip, char*ido) { |
---|
| 1300 | int i, lon = 0, idcentro, auxint, idtiposo; |
---|
| 1301 | char *tbSoftware[MAXSOFTWARE]; |
---|
[b1f0d31] | 1302 | int tbidsoftware[MAXSOFTWARE]; |
---|
[90f061f6] | 1303 | char ch[2], descripso[50]; // Caracter delimitador y nombre del estandar sistema operativo |
---|
| 1304 | char sqlstr[1000], ErrStr[200], nombreordenador[250]; |
---|
[5eae07e] | 1305 | |
---|
| 1306 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[9fd5e97] | 1307 | // ACCESO único A TRAVES DE OBJETO MUTEX a este trozo de código |
---|
[90f061f6] | 1308 | pthread_mutex_lock(&guardia); |
---|
| 1309 | |
---|
[5eae07e] | 1310 | // Toma Centro |
---|
[90f061f6] | 1311 | sprintf( |
---|
| 1312 | sqlstr, |
---|
| 1313 | "SELECT aulas.idcentro,ordenadores.nombreordenador FROM aulas INNER JOIN ordenadores ON aulas.idaula=ordenadores.idaula WHERE ordenadores.idordenador=%s", |
---|
| 1314 | ido); |
---|
| 1315 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[5eae07e] | 1316 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1317 | pthread_mutex_unlock(&guardia); |
---|
| 1318 | return (false); |
---|
| 1319 | } |
---|
| 1320 | if (!tbl.Get("idcentro", auxint)) { // Toma dato |
---|
[5eae07e] | 1321 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1322 | pthread_mutex_unlock(&guardia); |
---|
| 1323 | return (false); |
---|
| 1324 | } |
---|
| 1325 | idcentro = auxint + 0; // Bug Mysql |
---|
[5eae07e] | 1326 | |
---|
[90f061f6] | 1327 | if (!tbl.Get("nombreordenador", nombreordenador)) { // Toma dato |
---|
[5eae07e] | 1328 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1329 | pthread_mutex_unlock(&guardia); |
---|
| 1330 | return (false); |
---|
| 1331 | } |
---|
| 1332 | |
---|
| 1333 | if (lon > MAXSOFTWARE) |
---|
| 1334 | lon = MAXSOFTWARE; |
---|
[9fd5e97] | 1335 | // Trocea la cadena de configuración |
---|
[90f061f6] | 1336 | strcpy(ch, "\n");// caracter delimitador |
---|
[00ad799] | 1337 | |
---|
| 1338 | |
---|
[90f061f6] | 1339 | // Lee archivo de inventario software |
---|
[00ad799] | 1340 | FILE *Finv; |
---|
| 1341 | char *buffer; |
---|
| 1342 | long lSize; |
---|
[90f061f6] | 1343 | Finv = fopen(sft, "rb"); // EL parametro sft contiene el path del archivo de inventario |
---|
| 1344 | if (Finv == NULL) |
---|
| 1345 | return (false); |
---|
| 1346 | fseek(Finv, 0, SEEK_END); // Obtiene tamaño del fichero. |
---|
| 1347 | lSize = ftell(Finv); |
---|
| 1348 | rewind(Finv); |
---|
| 1349 | buffer = (char*) malloc(lSize); // Toma memoria para el buffer de lectura. |
---|
| 1350 | if (buffer == NULL) |
---|
| 1351 | return (false); |
---|
| 1352 | fread(buffer, 1, lSize, Finv); // Lee contenido del fichero |
---|
[00ad799] | 1353 | fclose(Finv); |
---|
[90f061f6] | 1354 | buffer = escaparComillas(buffer); |
---|
[9fd5e97] | 1355 | // trocea las líneas |
---|
[90f061f6] | 1356 | lon = split_parametros(tbSoftware, buffer, ch); |
---|
[00ad799] | 1357 | |
---|
[b1f0d31] | 1358 | // Incorpora el sistema Operativo de la partición |
---|
[90f061f6] | 1359 | sprintf(sqlstr, |
---|
| 1360 | "SELECT idtiposo,descripcion FROM tiposos WHERE tipopar ='%s'", tfs); |
---|
[b1f0d31] | 1361 | // Ejecuta consulta |
---|
[90f061f6] | 1362 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[b1f0d31] | 1363 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1364 | pthread_mutex_unlock(&guardia); |
---|
| 1365 | return (false); |
---|
| 1366 | } |
---|
| 1367 | if (tbl.ISEOF()) { // Software NO existente |
---|
| 1368 | pthread_mutex_unlock(&guardia); |
---|
| 1369 | return (false); |
---|
| 1370 | } else { |
---|
| 1371 | if (!tbl.Get("idtiposo", auxint)) { |
---|
[b1f0d31] | 1372 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1373 | pthread_mutex_unlock(&guardia); |
---|
| 1374 | return (false); |
---|
[b1f0d31] | 1375 | } |
---|
[90f061f6] | 1376 | idtiposo = auxint + 0; // Bug Mysql |
---|
| 1377 | if (!tbl.Get("descripcion", descripso)) { |
---|
[b1f0d31] | 1378 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1379 | pthread_mutex_unlock(&guardia); |
---|
| 1380 | return (false); |
---|
| 1381 | } |
---|
| 1382 | tbSoftware[lon++] = descripso; |
---|
| 1383 | } |
---|
[9fd5e97] | 1384 | // Trocea las cadenas de parametros de partición |
---|
[90f061f6] | 1385 | for (i = 0; i < lon; i++) { |
---|
| 1386 | sprintf(sqlstr, |
---|
| 1387 | "SELECT idsoftware FROM softwares WHERE descripcion ='%s'", |
---|
| 1388 | tbSoftware[i]); |
---|
| 1389 | |
---|
| 1390 | // EJecuta consulta |
---|
| 1391 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 1392 | db.GetErrorErrStr(ErrStr); |
---|
| 1393 | pthread_mutex_unlock(&guardia); |
---|
| 1394 | return (false); |
---|
| 1395 | } |
---|
| 1396 | if (tbl.ISEOF()) { // Software NO existente |
---|
| 1397 | if ((lon - i) > 1) // No es el último elemento que es el S.O. el idtiposoftware es 2 (Aplicaciones) |
---|
| 1398 | sprintf( |
---|
| 1399 | sqlstr, |
---|
| 1400 | "INSERT softwares (idtiposoftware,descripcion,idcentro,grupoid) VALUES(2,'%s',%d,0)", |
---|
| 1401 | tbSoftware[i], idcentro); |
---|
| 1402 | else |
---|
| 1403 | // Es el último elemento que es el S.O. el idtiposoftware es 1 (Sistemas operativos) |
---|
| 1404 | sprintf( |
---|
| 1405 | sqlstr, |
---|
| 1406 | "INSERT softwares (idtiposoftware,idtiposo,descripcion,idcentro,grupoid) VALUES(1,%d,'%s',%d,0)", |
---|
| 1407 | idtiposo, tbSoftware[i], idcentro); |
---|
| 1408 | |
---|
| 1409 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[5eae07e] | 1410 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1411 | pthread_mutex_unlock(&guardia); |
---|
| 1412 | return (false); |
---|
| 1413 | } |
---|
| 1414 | // Recupera el identificador del software |
---|
| 1415 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1416 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 1417 | db.GetErrorErrStr(ErrStr); |
---|
| 1418 | pthread_mutex_unlock(&guardia); |
---|
| 1419 | return (false); |
---|
[5eae07e] | 1420 | } |
---|
[90f061f6] | 1421 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1422 | if (!tbl.Get("identificador", tbidsoftware[i])) { |
---|
[5eae07e] | 1423 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1424 | pthread_mutex_unlock(&guardia); |
---|
| 1425 | return (false); |
---|
| 1426 | } |
---|
| 1427 | } |
---|
| 1428 | } else { |
---|
| 1429 | if (!tbl.Get("idsoftware", tbidsoftware[i])) { // Toma dato |
---|
| 1430 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1431 | pthread_mutex_unlock(&guardia); |
---|
| 1432 | return (false); |
---|
| 1433 | } |
---|
| 1434 | } // Fin for |
---|
[5eae07e] | 1435 | } |
---|
[90f061f6] | 1436 | // Comprueba existencia de perfil software y actualización de éste para el ordenador |
---|
| 1437 | if (!CuestionPerfilSoftware(db, tbl, idcentro, ido, tbidsoftware, i, |
---|
| 1438 | nombreordenador, par)) { |
---|
[5eae07e] | 1439 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1440 | pthread_mutex_unlock(&guardia); |
---|
| 1441 | return (false); |
---|
| 1442 | } |
---|
| 1443 | pthread_mutex_unlock(&guardia); |
---|
[5eae07e] | 1444 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[90f061f6] | 1445 | return (true); |
---|
[5eae07e] | 1446 | } |
---|
| 1447 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1448 | // Función: CuestionPerfilSoftware |
---|
[9fd5e97] | 1449 | // |
---|
| 1450 | // Parámetros: |
---|
| 1451 | // - db: Objeto base de datos (ya operativo) |
---|
| 1452 | // - tbl: Objeto tabla |
---|
| 1453 | // - idcentro: Identificador del centro en la tabla |
---|
| 1454 | // - ido: Identificador del ordenador del cliente en la tabla |
---|
| 1455 | // - tbidsoftware: Tipo de partición |
---|
| 1456 | // - i: Número de particiones |
---|
| 1457 | // - nombreordenador: Nombre del ordenador del cliente |
---|
| 1458 | // - particion: Tipo de la partición |
---|
[5eae07e] | 1459 | //________________________________________________________________________________________________________/ |
---|
[90f061f6] | 1460 | int CuestionPerfilSoftware(Database db, Table tbl, int idcentro, char* ido, |
---|
| 1461 | int *tbidsoftware, int i, char *nombreordenador, char *particion) { |
---|
| 1462 | char sqlstr[1000], ErrStr[200]; |
---|
| 1463 | int tbidsoftwareperfil[MAXSOFTWARE]; |
---|
| 1464 | int j = 0; |
---|
[5eae07e] | 1465 | int idperfilsoft; |
---|
| 1466 | // Busca perfil soft del ordenador |
---|
[90f061f6] | 1467 | sprintf( |
---|
| 1468 | sqlstr, |
---|
| 1469 | "SELECT perfilessoft_softwares.idsoftware FROM ordenador_perfilsoft INNER JOIN perfilessoft ON ordenador_perfilsoft.idperfilsoft = perfilessoft.idperfilsoft INNER JOIN perfilessoft_softwares ON perfilessoft_softwares.idperfilsoft=perfilessoft.idperfilsoft WHERE ordenador_perfilsoft.idordenador =%s", |
---|
| 1470 | ido); |
---|
[5eae07e] | 1471 | // EJecuta consulta |
---|
[90f061f6] | 1472 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[5eae07e] | 1473 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1474 | return (false); |
---|
| 1475 | } |
---|
| 1476 | while (!tbl.ISEOF()) { // Recorre software del perfils |
---|
| 1477 | if (!tbl.Get("idsoftware", tbidsoftwareperfil[j++])) { // Toma dato |
---|
[5eae07e] | 1478 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1479 | return (false); |
---|
| 1480 | } |
---|
[5eae07e] | 1481 | tbl.MoveNext(); |
---|
| 1482 | } |
---|
| 1483 | // Comprueba si el perfil del ordenador contiene todo el software enviado |
---|
[90f061f6] | 1484 | int k, q, sw = false; |
---|
| 1485 | if (i == j) { // Si son el mismo número de componenetes software ... |
---|
| 1486 | for (k = 0; k < i; k++) { // Elemento software |
---|
| 1487 | for (q = 0; q < j; q++) { |
---|
| 1488 | if (tbidsoftware[k] == tbidsoftwareperfil[q]) { |
---|
| 1489 | sw = true; |
---|
[ec01697] | 1490 | break; |
---|
| 1491 | } |
---|
[5eae07e] | 1492 | } |
---|
[90f061f6] | 1493 | if (!sw) |
---|
| 1494 | break; |
---|
[5eae07e] | 1495 | } |
---|
| 1496 | } |
---|
[90f061f6] | 1497 | |
---|
[5eae07e] | 1498 | // La variable sw contiene false si se ha encontrado algún software que no está en el perfil software del ordenador |
---|
[90f061f6] | 1499 | if (sw) |
---|
| 1500 | return (true); // Todo el software está en el perfil actual |
---|
| 1501 | |
---|
[5eae07e] | 1502 | // Crea perfil nuevo con todo el software inventariado |
---|
[90f061f6] | 1503 | sprintf( |
---|
| 1504 | sqlstr, |
---|
| 1505 | "INSERT perfilessoft (descripcion,idcentro,grupoid) VALUES('Perfil Software (%s, Part:%s) ',%d,0)", |
---|
| 1506 | nombreordenador, particion, idcentro); |
---|
| 1507 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[5eae07e] | 1508 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1509 | return (false); |
---|
| 1510 | } |
---|
[5eae07e] | 1511 | // Recupera el identificador del software |
---|
[90f061f6] | 1512 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1513 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[5eae07e] | 1514 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1515 | return (false); |
---|
[5eae07e] | 1516 | } |
---|
[90f061f6] | 1517 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1518 | if (!tbl.Get("identificador", idperfilsoft)) { |
---|
[5eae07e] | 1519 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1520 | return (false); |
---|
[5eae07e] | 1521 | } |
---|
[90f061f6] | 1522 | } |
---|
| 1523 | for (k = 0; k < i; k++) { // relaciona elementos softwares con el nuevo perfil software |
---|
| 1524 | sprintf( |
---|
| 1525 | sqlstr, |
---|
| 1526 | "INSERT perfilessoft_softwares (idperfilsoft,idsoftware) VALUES(%d,%d)", |
---|
| 1527 | idperfilsoft, tbidsoftware[k]); |
---|
| 1528 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[5eae07e] | 1529 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1530 | return (false); |
---|
| 1531 | } |
---|
[ec01697] | 1532 | } |
---|
| 1533 | // Busca si existe un perfil software para ese ordenador y esa partición |
---|
[90f061f6] | 1534 | sprintf( |
---|
| 1535 | sqlstr, |
---|
| 1536 | "SELECT idperfilsoft FROM ordenador_perfilsoft WHERE idordenador =%s AND particion=%s", |
---|
| 1537 | ido, particion); |
---|
[ec01697] | 1538 | // Ejecuta consulta |
---|
[90f061f6] | 1539 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[5eae07e] | 1540 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1541 | return (false); |
---|
| 1542 | } |
---|
| 1543 | if (!tbl.ISEOF()) { // existe un perfilsoft que se cambia al nuevo |
---|
| 1544 | sprintf( |
---|
| 1545 | sqlstr, |
---|
| 1546 | "UPDATE ordenador_perfilsoft SET idperfilsoft=%d WHERE idordenador=%s AND particion=%s", |
---|
| 1547 | idperfilsoft, ido, particion); |
---|
| 1548 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[ec01697] | 1549 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1550 | return (false); |
---|
| 1551 | } |
---|
| 1552 | } else { |
---|
| 1553 | sprintf( |
---|
| 1554 | sqlstr, |
---|
| 1555 | "INSERT INTO ordenador_perfilsoft (idordenador,particion,idperfilsoft) VALUE (%s,%s,%d)", |
---|
| 1556 | ido, particion, idperfilsoft); |
---|
| 1557 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[ec01697] | 1558 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1559 | return (false); |
---|
| 1560 | } |
---|
| 1561 | |
---|
[ec01697] | 1562 | } |
---|
[90f061f6] | 1563 | return (true); |
---|
[5eae07e] | 1564 | } |
---|
| 1565 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1566 | // Función: actualiza_configuracion |
---|
[5eae07e] | 1567 | // |
---|
[b0bb14f] | 1568 | // Descripción: |
---|
[9fd5e97] | 1569 | // Esta función actualiza la base de datos con la configuración de sistemas operativos y particiones de un ordenador |
---|
[b0bb14f] | 1570 | // Parámetros: |
---|
[5eae07e] | 1571 | // - db: Objeto base de datos (ya operativo) |
---|
| 1572 | // - tbl: Objeto tabla |
---|
[9fd5e97] | 1573 | // - cfg: cadena con una configuración |
---|
| 1574 | // - idcfgo: Identificador de la configuración actual del ordenador |
---|
| 1575 | // - idprto: Identificador de la configuración actual de las particiones del ordenador |
---|
| 1576 | // - ipho: Ip del ordenador |
---|
[5eae07e] | 1577 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1578 | int actualiza_configuracion(Database db, Table tbl, char* cfg, int idcfgo, |
---|
| 1579 | int idprto, char* ipho) { |
---|
| 1580 | char sqlstr[1000], ErrStr[200]; |
---|
| 1581 | int idconfiguracion, idparticion, lon; |
---|
[34fc87f] | 1582 | char * part; |
---|
| 1583 | |
---|
| 1584 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[9fd5e97] | 1585 | // ACCESO único A TRAVES DE OBJETO MUTEX a este trozo de código |
---|
[90f061f6] | 1586 | pthread_mutex_lock(&guardia); |
---|
| 1587 | sprintf( |
---|
| 1588 | sqlstr, |
---|
| 1589 | "SELECT idconfiguracion FROM configuraciones WHERE configuracion LIKE '%s'", |
---|
| 1590 | cfg); |
---|
| 1591 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1592 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1593 | pthread_mutex_unlock(&guardia); |
---|
| 1594 | return (false); |
---|
[34fc87f] | 1595 | } |
---|
[9fd5e97] | 1596 | if (!tbl.ISEOF()) { // Configuración ya existente |
---|
[90f061f6] | 1597 | if (!tbl.Get("idconfiguracion", idconfiguracion)) { // Toma dato |
---|
| 1598 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1599 | pthread_mutex_unlock(&guardia); |
---|
| 1600 | return (false); |
---|
[34fc87f] | 1601 | } |
---|
[9fd5e97] | 1602 | } else { // Nueva configuración |
---|
[90f061f6] | 1603 | sprintf(sqlstr, "INSERT configuraciones (configuracion) VALUES('%s')", |
---|
| 1604 | cfg); |
---|
| 1605 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
| 1606 | db.GetErrorErrStr(ErrStr); |
---|
| 1607 | pthread_mutex_unlock(&guardia); |
---|
| 1608 | return (false); |
---|
| 1609 | } |
---|
| 1610 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1611 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 1612 | db.GetErrorErrStr(ErrStr); |
---|
| 1613 | pthread_mutex_unlock(&guardia); |
---|
| 1614 | return (false); |
---|
| 1615 | } |
---|
| 1616 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1617 | if (!tbl.Get("identificador", idconfiguracion)) { |
---|
| 1618 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1619 | pthread_mutex_unlock(&guardia); |
---|
| 1620 | return (false); |
---|
[34fc87f] | 1621 | } |
---|
[90f061f6] | 1622 | } |
---|
[34fc87f] | 1623 | } |
---|
| 1624 | // Genera cadena de particiones |
---|
[90f061f6] | 1625 | lon = strlen(cfg); |
---|
| 1626 | part = (char*) malloc(lon); |
---|
| 1627 | TomaParticiones(cfg, part, lon); |
---|
| 1628 | sprintf(sqlstr, |
---|
| 1629 | "SELECT idparticion FROM particiones WHERE particion LIKE '%s'", |
---|
| 1630 | part); |
---|
| 1631 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1632 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1633 | pthread_mutex_unlock(&guardia); |
---|
| 1634 | return (false); |
---|
[34fc87f] | 1635 | } |
---|
[9fd5e97] | 1636 | if (!tbl.ISEOF()) { // Configuración ya existente |
---|
[90f061f6] | 1637 | if (!tbl.Get("idparticion", idparticion)) { // Toma dato |
---|
| 1638 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1639 | pthread_mutex_unlock(&guardia); |
---|
| 1640 | return (false); |
---|
| 1641 | } |
---|
[9fd5e97] | 1642 | } else { // Nueva partición |
---|
[90f061f6] | 1643 | sprintf(sqlstr, "INSERT particiones (particion) VALUES('%s')", part); |
---|
| 1644 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
| 1645 | db.GetErrorErrStr(ErrStr); |
---|
| 1646 | pthread_mutex_unlock(&guardia); |
---|
| 1647 | return (false); |
---|
| 1648 | } |
---|
| 1649 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 1650 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 1651 | db.GetErrorErrStr(ErrStr); |
---|
| 1652 | pthread_mutex_unlock(&guardia); |
---|
| 1653 | return (false); |
---|
| 1654 | } |
---|
| 1655 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 1656 | if (!tbl.Get("identificador", idparticion)) { |
---|
[34fc87f] | 1657 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1658 | pthread_mutex_unlock(&guardia); |
---|
| 1659 | return (false); |
---|
| 1660 | } |
---|
[34fc87f] | 1661 | } |
---|
| 1662 | } |
---|
[9fd5e97] | 1663 | if (idconfiguracion != idcfgo || idparticion != idprto) { // Si el odenador tiene una configuración distinta ... |
---|
[90f061f6] | 1664 | sprintf( |
---|
| 1665 | sqlstr, |
---|
| 1666 | "Update ordenadores set idconfiguracion=%d, idparticion=%d WHERE ip='%s'", |
---|
| 1667 | idconfiguracion, idparticion, ipho); |
---|
| 1668 | if (!db.Execute(sqlstr, tbl)) { // Error al actualizar |
---|
[34fc87f] | 1669 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1670 | pthread_mutex_unlock(&guardia); |
---|
| 1671 | return (false); |
---|
[34fc87f] | 1672 | } |
---|
| 1673 | } |
---|
[90f061f6] | 1674 | pthread_mutex_unlock(&guardia); |
---|
[34fc87f] | 1675 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[90f061f6] | 1676 | return (true); |
---|
[34fc87f] | 1677 | } |
---|
| 1678 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1679 | // Función: TomaParticiones |
---|
[34fc87f] | 1680 | // |
---|
[b0bb14f] | 1681 | // Descripción: |
---|
[9fd5e97] | 1682 | // Esta función compone basándose en la cadena de configuración que devuelve el ordenador, una cadena de particiones con |
---|
| 1683 | // los valores "n0=PPPP;n1=PPPP..." con las duplas:el número de partición y el tipo, separados por coma |
---|
[b0bb14f] | 1684 | // Parámetros: |
---|
[9fd5e97] | 1685 | // - cfg: Cadena de configuración |
---|
[34fc87f] | 1686 | // - parts: Cadena devuelta con el formato anterior descrito |
---|
[9fd5e97] | 1687 | // - lonprt: Longitud mínima para las cadenas |
---|
[34fc87f] | 1688 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1689 | void TomaParticiones(char* cfg, char* parts, int lonprt) { |
---|
[34fc87f] | 1690 | int i; |
---|
[90f061f6] | 1691 | int lon = 0; |
---|
[9fd5e97] | 1692 | char *tbParticiones[10]; // Para albergar hasta 10 particiones ( Normalmente Mínimo 8); |
---|
| 1693 | char *tbParticion[8]; // Para albergar hasta 8 parámetros de partición; |
---|
| 1694 | char *tbIgualdad[2]; // Para albergar hasta 8 parámetros de partición; |
---|
| 1695 | char ch[2]; // Carácter delimitador |
---|
[34fc87f] | 1696 | char *apun; |
---|
| 1697 | int p; |
---|
[9fd5e97] | 1698 | // Toma memoria para cada elemento de partición |
---|
[90f061f6] | 1699 | for (i = 0; i < 10; i++) |
---|
| 1700 | tbParticiones[i] = (char*) malloc(lonprt); |
---|
[34fc87f] | 1701 | |
---|
[9fd5e97] | 1702 | // Toma memoria para cada parámetro de partición |
---|
[90f061f6] | 1703 | for (i = 0; i < 8; i++) |
---|
| 1704 | tbParticion[i] = (char*) malloc(lonprt); |
---|
[34fc87f] | 1705 | |
---|
| 1706 | // Toma memoria para cada igualdad |
---|
[90f061f6] | 1707 | for (i = 0; i < 2; i++) |
---|
| 1708 | tbIgualdad[i] = (char*) malloc(20); |
---|
[34fc87f] | 1709 | |
---|
[9fd5e97] | 1710 | // Trocea la cadena de configuración |
---|
| 1711 | strcpy(ch, "\t");// carácter delimitador (tabulador) |
---|
[90f061f6] | 1712 | lonprt = split_parametros(tbParticiones, cfg, ch); |
---|
[34fc87f] | 1713 | // Trocea las cadenas de parametros de particin |
---|
[90f061f6] | 1714 | for (p = 0; p < lonprt; p++) { |
---|
[9fd5e97] | 1715 | strcpy(ch, "\n");// carácter delimitador (salto de linea) |
---|
[90f061f6] | 1716 | split_parametros(tbParticion, tbParticiones[p], ch); |
---|
[9fd5e97] | 1717 | strcpy(ch, "=");// carácter delimitador "=" |
---|
[90f061f6] | 1718 | split_parametros(tbIgualdad, tbParticion[4], ch); // Nmero de particin |
---|
| 1719 | lon += sprintf(parts + lon, "%s=", tbIgualdad[1]); |
---|
| 1720 | split_parametros(tbIgualdad, tbParticion[2], ch); // Tipo de particion |
---|
| 1721 | apun = tbIgualdad[1]; |
---|
[34fc87f] | 1722 | //if(apun[0]=='H') apun++; // Si es oculta ... |
---|
[90f061f6] | 1723 | lon += sprintf(parts + lon, "%s;", apun); |
---|
[34fc87f] | 1724 | } |
---|
[90f061f6] | 1725 | lon += sprintf(parts + lon, "@prt"); |
---|
[34fc87f] | 1726 | } |
---|
| 1727 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1728 | // Función: ComandosPendientes |
---|
[34fc87f] | 1729 | // |
---|
[b0bb14f] | 1730 | // Descripción: |
---|
[9fd5e97] | 1731 | // Esta función busca en la base de datos,comandos pendientes de ejecutar por un ordenador concreto |
---|
[b0bb14f] | 1732 | // Parámetros: |
---|
[34fc87f] | 1733 | // - s: Socket del cliente |
---|
[b0bb14f] | 1734 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 1735 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1736 | int ComandosPendientes(SOCKET s, char *parametros) { |
---|
| 1737 | char *iph, *ido, *coletilla; |
---|
[34fc87f] | 1738 | int ids; |
---|
[90f061f6] | 1739 | char pids[20], ipe[16], idord[16]; |
---|
[34fc87f] | 1740 | |
---|
[90f061f6] | 1741 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 1742 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 1743 | strcpy(ipe, iph); |
---|
| 1744 | strcpy(idord, ido); |
---|
[34fc87f] | 1745 | |
---|
[90f061f6] | 1746 | if (busca_comandos(ipe, idord, parametros, &ids)) { |
---|
| 1747 | Coloca_estado(ipe, CLIENTE_OCUPADO, s); |
---|
[34fc87f] | 1748 | //Manda el comando pendiente |
---|
[90f061f6] | 1749 | coletilla = corte_iph(parametros); |
---|
| 1750 | coletilla[0] = '\0';// Corta la trama en la ip |
---|
| 1751 | sprintf(pids, "ids=%d\r", ids); |
---|
[9fd5e97] | 1752 | strcat(parametros, pids); // Le añade el identificador de la acción |
---|
[90f061f6] | 1753 | return (manda_comando(s, parametros)); |
---|
| 1754 | } |
---|
[9fd5e97] | 1755 | NoComandosPendientes(s); // Indica al cliente rembo que ya no hay más comandos pendientes |
---|
[90f061f6] | 1756 | return (true); |
---|
[34fc87f] | 1757 | } |
---|
| 1758 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1759 | // Función: EjecutarItem |
---|
[34fc87f] | 1760 | // |
---|
[b0bb14f] | 1761 | // Descripción: |
---|
[9fd5e97] | 1762 | // Esta función ejecuta un item de un men concreto solicitado por algn cliente rembo |
---|
[b0bb14f] | 1763 | // Parámetros: |
---|
[34fc87f] | 1764 | // - s: Socket del cliente |
---|
[b0bb14f] | 1765 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 1766 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1767 | int EjecutarItem(SOCKET s, char *parametros) { |
---|
| 1768 | char sqlstr[1000], ErrStr[200]; |
---|
[34fc87f] | 1769 | Database db; |
---|
[90f061f6] | 1770 | Table tbl, tbln; |
---|
| 1771 | int idtipoaccion, lon, cont_comandos = 0, i, puertorepo; |
---|
| 1772 | char tipoaccion, *iph, *idt, ipe[16]; |
---|
[34fc87f] | 1773 | char *tbComandosparametros[100]; |
---|
| 1774 | |
---|
[90f061f6] | 1775 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 1776 | idt = toma_parametro("idt", parametros); // Toma idemtificador del item |
---|
| 1777 | strcpy(ipe, iph); |
---|
[34fc87f] | 1778 | |
---|
[90f061f6] | 1779 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 1780 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1781 | return (false); |
---|
[34fc87f] | 1782 | } |
---|
[90f061f6] | 1783 | sprintf( |
---|
| 1784 | sqlstr, |
---|
| 1785 | "SELECT acciones_menus.tipoaccion, acciones_menus.idtipoaccion FROM acciones_menus WHERE acciones_menus.idaccionmenu=%s", |
---|
| 1786 | idt); |
---|
| 1787 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 1788 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1789 | return (false); |
---|
[34fc87f] | 1790 | } |
---|
[90f061f6] | 1791 | if (tbl.ISEOF()) { |
---|
| 1792 | return (false); // No hay comandos pendientes |
---|
[34fc87f] | 1793 | } |
---|
| 1794 | |
---|
[90f061f6] | 1795 | if (!tbl.Get("tipoaccion", tipoaccion)) { // Toma dato |
---|
[34fc87f] | 1796 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1797 | return (false); |
---|
[34fc87f] | 1798 | } |
---|
| 1799 | |
---|
[90f061f6] | 1800 | if (!tbl.Get("idtipoaccion", idtipoaccion)) { // Toma dato |
---|
[34fc87f] | 1801 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 1802 | return (false); |
---|
[34fc87f] | 1803 | } |
---|
| 1804 | |
---|
[90f061f6] | 1805 | switch (tipoaccion) { |
---|
| 1806 | case EJECUCION_PROCEDIMIENTO: |
---|
| 1807 | sprintf( |
---|
| 1808 | sqlstr, |
---|
| 1809 | "SELECT procedimientos_comandos.parametros FROM procedimientos_comandos WHERE procedimientos_comandos.idprocedimiento=%d", |
---|
| 1810 | idtipoaccion); |
---|
| 1811 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
| 1812 | db.GetErrorErrStr(ErrStr); |
---|
| 1813 | return (false); |
---|
| 1814 | } |
---|
| 1815 | if (tbl.ISEOF()) // No existe procedimiento |
---|
| 1816 | return (false); |
---|
| 1817 | |
---|
| 1818 | while (!tbl.ISEOF()) { |
---|
| 1819 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
| 1820 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 1821 | return (false); |
---|
[34fc87f] | 1822 | } |
---|
[90f061f6] | 1823 | lon = strlen(parametros); |
---|
| 1824 | tbComandosparametros[cont_comandos] = (char*) malloc(lon); |
---|
| 1825 | if (tbComandosparametros[cont_comandos] == NULL) |
---|
| 1826 | return (false); // No hay memoria bastante |
---|
| 1827 | strcpy(tbComandosparametros[cont_comandos++], parametros); |
---|
| 1828 | tbl.MoveNext(); |
---|
| 1829 | } |
---|
| 1830 | strcpy(parametros, tbComandosparametros[0]); |
---|
| 1831 | strcat(parametros, "iph="); |
---|
| 1832 | strcat(parametros, ipe); |
---|
| 1833 | strcat(parametros, "\r"); |
---|
| 1834 | for (i = 1; i < cont_comandos; i++) { |
---|
| 1835 | strcat(parametros, "\n"); |
---|
| 1836 | strcat(parametros, tbComandosparametros[i]); |
---|
| 1837 | strcat(parametros, "iph="); |
---|
| 1838 | strcat(parametros, ipe); |
---|
| 1839 | strcat(parametros, "\r"); |
---|
| 1840 | } |
---|
| 1841 | if (TomaIPServidorRembo(ipe, &puertorepo)) |
---|
| 1842 | return (manda_trama_servidorrembo(ipe, parametros, puertorepo)); |
---|
| 1843 | break; |
---|
| 1844 | case EJECUCION_TAREA: |
---|
| 1845 | EjecutarTarea(idtipoaccion, 0, 0, 0, db, parametros); |
---|
| 1846 | break; |
---|
| 1847 | case EJECUCION_TRABAJO: |
---|
[9fd5e97] | 1848 | EjecutarTrabajo(idtipoaccion, db, parametros); // Es una programación de un trabajo |
---|
[90f061f6] | 1849 | break; |
---|
[34fc87f] | 1850 | } |
---|
| 1851 | db.Close(); |
---|
[90f061f6] | 1852 | return (true); |
---|
[34fc87f] | 1853 | } |
---|
| 1854 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1855 | // Función: DisponibilidadComandos |
---|
[34fc87f] | 1856 | // |
---|
[b0bb14f] | 1857 | // Descripción: |
---|
[9fd5e97] | 1858 | // Esta función habilita a un clinte rembo para recibir o no, comandos iteractivos |
---|
[b0bb14f] | 1859 | // Parámetros: |
---|
[34fc87f] | 1860 | // - s: Socket del cliente |
---|
[9fd5e97] | 1861 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 1862 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1863 | int DisponibilidadComandos(SOCKET s, char *parametros) { |
---|
| 1864 | char *iph, *swd; |
---|
| 1865 | int resul = 0, i; |
---|
[34fc87f] | 1866 | |
---|
[90f061f6] | 1867 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 1868 | swd = toma_parametro("swd", parametros); // Toma switch de diponibilidad |
---|
[34fc87f] | 1869 | |
---|
[90f061f6] | 1870 | if (strcmp(swd, "1") == 0) // Cliente disponible |
---|
| 1871 | resul = Coloca_estado(iph, CLIENTE_REMBO, s); |
---|
| 1872 | else { |
---|
| 1873 | if (cliente_existente(iph, &i)) // Si ya existe la IP ... |
---|
| 1874 | resul = borra_entrada(i); // Cliente apagado |
---|
[34fc87f] | 1875 | } |
---|
[90f061f6] | 1876 | return (resul); |
---|
[34fc87f] | 1877 | } |
---|
| 1878 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1879 | // Función: Coloca_estado |
---|
[34fc87f] | 1880 | // |
---|
[b0bb14f] | 1881 | // Descripción: |
---|
[9fd5e97] | 1882 | // Esta función coloca el estado de un ordenador en la tabla de sockets |
---|
[b0bb14f] | 1883 | // Parámetros: |
---|
[34fc87f] | 1884 | // - iph: Ip del ordenador |
---|
| 1885 | // - e: Nuevo estado |
---|
| 1886 | // - s: Socket usado por el cliente para comunicarse con el servidor HIDRA |
---|
| 1887 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1888 | int Coloca_estado(char *iph, const char *e, SOCKET s) { |
---|
[34fc87f] | 1889 | int i; |
---|
[90f061f6] | 1890 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 1891 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 1892 | if (IgualIP(iph, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
| 1893 | strcpy(tbsockets[i].estado, e); // Cambia el estado |
---|
| 1894 | tbsockets[i].sock = s; // Guarda el socket |
---|
| 1895 | return (true); |
---|
[34fc87f] | 1896 | } |
---|
| 1897 | } |
---|
| 1898 | } |
---|
[90f061f6] | 1899 | return (false); |
---|
[34fc87f] | 1900 | } |
---|
[90f061f6] | 1901 | |
---|
[34fc87f] | 1902 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1903 | // Función: inclusion_srvRMB |
---|
[34fc87f] | 1904 | // |
---|
[b0bb14f] | 1905 | // Descripción: |
---|
[277d0cd] | 1906 | // Esta funcin incorpora el socket de un nuevo servidor rembo a la tabla de sockets |
---|
[b0bb14f] | 1907 | // Parámetros: |
---|
[9fd5e97] | 1908 | // - iphsrvrmb: Ip del servidor |
---|
| 1909 | // - puertorepo: Puerto del repositorio |
---|
[34fc87f] | 1910 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1911 | int inclusion_srvRMB(char *iphsrvrmb, int puertorepo) { |
---|
| 1912 | int i, idx; |
---|
| 1913 | |
---|
[34fc87f] | 1914 | // Incluyendo al cliente en la tabla de sokets |
---|
[90f061f6] | 1915 | if (servidorrembo_existente(iphsrvrmb, &i)) { // Si ya existe la IP ... |
---|
| 1916 | idx = i; |
---|
| 1917 | } else { |
---|
| 1918 | if (hay_huecoservidorrembo(&i)) { // Busca hueco para el nuevo cliente |
---|
| 1919 | idx = i; |
---|
| 1920 | strcpy(tbsocketsSRVRMB[idx].ip, iphsrvrmb);// Copia IP |
---|
| 1921 | tbsocketsSRVRMB[idx].puertorepo = puertorepo; |
---|
| 1922 | } else |
---|
| 1923 | return (false); // No hay huecos |
---|
| 1924 | } |
---|
| 1925 | return (true); |
---|
[34fc87f] | 1926 | } |
---|
| 1927 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1928 | // Función: inclusion_cliWINLNX |
---|
[34fc87f] | 1929 | // |
---|
[b0bb14f] | 1930 | // Descripción: |
---|
[9fd5e97] | 1931 | // Esta función incorpora el socket de un nuevo cliente rembo a la tabla de sockets |
---|
[b0bb14f] | 1932 | // Parámetros: |
---|
[34fc87f] | 1933 | // - s: Socket del servidor rembo |
---|
[b0bb14f] | 1934 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 1935 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1936 | int inclusion_cliWINLNX(SOCKET s, char *parametros) { |
---|
| 1937 | char *iph, *tso; |
---|
| 1938 | int i, idx; |
---|
[34fc87f] | 1939 | |
---|
[9fd5e97] | 1940 | // Toma parámetros |
---|
[90f061f6] | 1941 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 1942 | tso = toma_parametro("tso", parametros); // Toma ip |
---|
[34fc87f] | 1943 | // Incluyendo al cliente en la tabla de sokets |
---|
[90f061f6] | 1944 | if (cliente_existente(iph, &i)) { // Si ya existe la IP ... |
---|
| 1945 | idx = i; |
---|
[34fc87f] | 1946 | close(tbsockets[idx].sock); |
---|
[90f061f6] | 1947 | } else { |
---|
| 1948 | if (hay_hueco(&i)) { // Busca hueco para el nuevo cliente |
---|
| 1949 | idx = i; |
---|
| 1950 | strcpy(tbsockets[idx].ip, iph);// Copia IP |
---|
| 1951 | } else |
---|
| 1952 | return (false); // No hay huecos |
---|
| 1953 | } |
---|
| 1954 | tbsockets[idx].sock = s; // Guarda el socket |
---|
| 1955 | strcpy(tbsockets[idx].estado, tso); |
---|
| 1956 | return (true); |
---|
[34fc87f] | 1957 | } |
---|
| 1958 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 1959 | // Función: inclusion_REPO |
---|
[34fc87f] | 1960 | // |
---|
[b0bb14f] | 1961 | // Descripción: |
---|
[9fd5e97] | 1962 | // Esta función incorpora el socket de un nuevo repositorio hidra |
---|
| 1963 | // Parámetros: |
---|
| 1964 | // - s: Socket del servidor rembo |
---|
| 1965 | // - parametros: Parámetros de la trama recibida |
---|
[34fc87f] | 1966 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 1967 | int inclusion_REPO(SOCKET s, char *parametros) { |
---|
| 1968 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 1969 | Database db; |
---|
| 1970 | Table tbl; |
---|
[90f061f6] | 1971 | |
---|
[34fc87f] | 1972 | char *iph; |
---|
[90f061f6] | 1973 | char PathHidra[250], PathPXE[250]; // path al directorio base de Hidra |
---|
| 1974 | int puertorepo, lon; |
---|
[73ed2a1] | 1975 | |
---|
[34fc87f] | 1976 | // Toma parnetros |
---|
[90f061f6] | 1977 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 1978 | |
---|
[34fc87f] | 1979 | // Toma las propiedades del ordenador |
---|
[90f061f6] | 1980 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 1981 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1982 | return (false); |
---|
[34fc87f] | 1983 | } |
---|
| 1984 | // Recupera los datos del ordenador |
---|
[90f061f6] | 1985 | sprintf( |
---|
| 1986 | sqlstr, |
---|
| 1987 | "SELECT puertorepo,pathrembod,pathpxe FROM servidoresrembo WHERE ip = '%s'", |
---|
| 1988 | iph); |
---|
| 1989 | |
---|
| 1990 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 1991 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 1992 | return (false); |
---|
[34fc87f] | 1993 | } |
---|
[90f061f6] | 1994 | if (tbl.ISEOF()) { // Si No existe registro |
---|
| 1995 | RegistraLog("No existe el Repositorio, se rechaza la petición", false); |
---|
| 1996 | return (false); |
---|
[73ed2a1] | 1997 | } |
---|
[90f061f6] | 1998 | if (!tbl.Get("puertorepo", puertorepo)) { // Toma dato |
---|
[34fc87f] | 1999 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 2000 | return (false); |
---|
[34fc87f] | 2001 | } |
---|
[90f061f6] | 2002 | if (!tbl.Get("pathrembod", PathHidra)) { // Toma dato |
---|
[34fc87f] | 2003 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 2004 | return (false); |
---|
[34fc87f] | 2005 | } |
---|
[90f061f6] | 2006 | if (!tbl.Get("pathpxe", PathPXE)) { // Toma dato |
---|
[df5bd24] | 2007 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 2008 | return (false); |
---|
| 2009 | } |
---|
| 2010 | inclusion_srvRMB(iph, puertorepo); // Actualiza tabla de servidores rembo |
---|
| 2011 | TRAMA *trama = (TRAMA*) malloc(LONGITUD_TRAMA); |
---|
| 2012 | if (!trama) |
---|
| 2013 | return (false); |
---|
[9fd5e97] | 2014 | // envíala trama |
---|
[90f061f6] | 2015 | |
---|
| 2016 | trama->arroba = '@'; |
---|
| 2017 | strncpy(trama->identificador, "JMMLCAMDJ", 9); |
---|
| 2018 | trama->ejecutor = '1'; |
---|
| 2019 | lon = sprintf(trama->parametros, "nfn=RESPUESTA_inclusionREPO\r"); |
---|
| 2020 | lon += sprintf(trama->parametros + lon, "prp=%d\r", puertorepo); |
---|
| 2021 | lon += sprintf(trama->parametros + lon, "pth=%s\r", PathHidra); |
---|
| 2022 | lon += sprintf(trama->parametros + lon, "ptx=%s\r", PathPXE); |
---|
| 2023 | lon += sprintf(trama->parametros + lon, "usu=%s\r", usuario); |
---|
| 2024 | lon += sprintf(trama->parametros + lon, "pwd=%s\r", pasguor); |
---|
| 2025 | lon += sprintf(trama->parametros + lon, "dat=%s\r", datasource); |
---|
| 2026 | lon += sprintf(trama->parametros + lon, "cat=%s\r", catalog); |
---|
| 2027 | return (manda_trama(s, trama)); |
---|
[34fc87f] | 2028 | } |
---|
| 2029 | // ________________________________________________________________________________________________________ |
---|
[2f4f9ff] | 2030 | // Función: EcoConsola |
---|
| 2031 | // |
---|
| 2032 | // Descripción: |
---|
| 2033 | // Esta función devuelve el eco de una consola remota |
---|
| 2034 | // Parámetros: |
---|
| 2035 | // - s: Socket del servidor web que envía el comando |
---|
| 2036 | // - parametros: Parámetros de la trama enviada |
---|
| 2037 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2038 | int EcoConsola(SOCKET s, char *parametros) { |
---|
| 2039 | |
---|
| 2040 | char *iph, *pfe, ipr[16]; |
---|
| 2041 | char nomfilesrc[512], nomfiledst[512], rep[16]; |
---|
[2f4f9ff] | 2042 | |
---|
[90f061f6] | 2043 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2044 | pfe = toma_parametro("pfe", parametros); // Toma path al archivo de eco |
---|
[2f4f9ff] | 2045 | |
---|
[90f061f6] | 2046 | if (!tomaIpRepoPort(iph, ipr, rep)) { |
---|
| 2047 | close(s); //NO se ha podido recuperar el repositorio del ordenador |
---|
| 2048 | return (false); |
---|
| 2049 | } |
---|
| 2050 | sprintf(nomfilesrc, "%s/eco-%s", pfe, iph); // Nombre del fichero destino |
---|
| 2051 | sprintf(nomfiledst, "/tmp/eco-%s", iph); // Nombre del fichero destino |
---|
| 2052 | if (!recibeFichero(ipr, rep, nomfilesrc, nomfiledst)) { |
---|
| 2053 | return(enviaEcoConsola(s,"Sin eco o error de sintaxis")); // NO se ha podido recuperar el fichero de eco |
---|
| 2054 | } |
---|
| 2055 | // Lee archivo de eco |
---|
[2f4f9ff] | 2056 | FILE *Finv; |
---|
| 2057 | char *buffer; |
---|
| 2058 | long lSize; |
---|
[90f061f6] | 2059 | Finv = fopen(nomfiledst, "rt"); |
---|
| 2060 | fseek(Finv, 0, SEEK_END); // Obtiene tamaño del fichero. |
---|
| 2061 | lSize = ftell(Finv); |
---|
| 2062 | rewind(Finv); |
---|
| 2063 | if(lSize>0){ |
---|
| 2064 | buffer = (char*) malloc(lSize + 1); // Toma memoria para el buffer de lectura. |
---|
| 2065 | if (buffer == NULL) { |
---|
| 2066 | sprintf(msglog, |
---|
| 2067 | "NO se ha podido reservar memoria para leer archivo de eco %s", |
---|
| 2068 | nomfilesrc); |
---|
| 2069 | RegistraLog(msglog, false); |
---|
| 2070 | return(enviaEcoConsola(s,msglog)); |
---|
| 2071 | } |
---|
| 2072 | fread(buffer, 1, lSize, Finv); // Lee contenido del fichero |
---|
| 2073 | fclose(Finv); |
---|
| 2074 | buffer[lSize] = '\0'; |
---|
| 2075 | buffer = escaparComillas(buffer); |
---|
| 2076 | return(enviaEcoConsola(s,buffer)); |
---|
| 2077 | } |
---|
| 2078 | return(enviaEcoConsola(s,"Sin eco o error de sintaxis")); |
---|
| 2079 | return(true); |
---|
| 2080 | } |
---|
| 2081 | // ________________________________________________________________________________________________________ |
---|
| 2082 | // Función: enviaEcoConsola |
---|
| 2083 | // |
---|
| 2084 | // Descripción: |
---|
[9fd5e97] | 2085 | // Envía eco a la consola |
---|
[90f061f6] | 2086 | // Parámetros: |
---|
| 2087 | // - s: Socket del servidor web que envía el comando |
---|
| 2088 | // - eco: Salida de consola |
---|
| 2089 | // ________________________________________________________________________________________________________ |
---|
| 2090 | int enviaEcoConsola(SOCKET s,const char *eco) |
---|
| 2091 | { |
---|
| 2092 | char nwparametros[LONGITUD_PARAMETROS]; |
---|
| 2093 | int res; |
---|
| 2094 | |
---|
| 2095 | nwparametros[0] = '\0'; |
---|
| 2096 | strcat(nwparametros, "eco="); // Compone retorno eco (Pantalla de consola remota) |
---|
| 2097 | strcat(nwparametros, eco); |
---|
| 2098 | res=manda_comando(s, nwparametros); |
---|
| 2099 | close(s); |
---|
| 2100 | return(res); |
---|
[2f4f9ff] | 2101 | } |
---|
| 2102 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2103 | // Función: Sondeo |
---|
[34fc87f] | 2104 | // |
---|
[b0bb14f] | 2105 | // Descripción: |
---|
[9fd5e97] | 2106 | // Esta función recupera el estado de los ordenadores solicitados |
---|
[b0bb14f] | 2107 | // Parámetros: |
---|
[34fc87f] | 2108 | // - s: Socket del servidor web que envn el comando |
---|
[9fd5e97] | 2109 | // - parametros: Parámetros de la trama enviada |
---|
[34fc87f] | 2110 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2111 | int Sondeo(SOCKET s, char *parametros) { |
---|
[34fc87f] | 2112 | char *iph; |
---|
| 2113 | char nwparametros[LONGITUD_PARAMETROS]; |
---|
| 2114 | int j; |
---|
| 2115 | |
---|
[90f061f6] | 2116 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2117 | nwparametros[0] = '\0'; |
---|
| 2118 | strcat(nwparametros, "tso="); // Compone retorno tso ( sistemas operativos de los clientes ) |
---|
| 2119 | for (j = 0; j < MAXIMOS_SOCKETS; j++) { |
---|
| 2120 | if (strncmp(tbsockets[j].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 2121 | if (IgualIP(iph, tbsockets[j].ip)) { // Si existe la IP en la cadena |
---|
| 2122 | strcat(nwparametros, tbsockets[j].ip); // Compone retorno |
---|
| 2123 | strcat(nwparametros, "/"); // "ip=sistemaoperatico;" |
---|
| 2124 | strcat(nwparametros, tbsockets[j].estado); |
---|
| 2125 | strcat(nwparametros, ";"); |
---|
[34fc87f] | 2126 | } |
---|
| 2127 | } |
---|
| 2128 | } |
---|
[90f061f6] | 2129 | return (manda_comando(s, nwparametros)); |
---|
[34fc87f] | 2130 | } |
---|
| 2131 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2132 | // Función: Actualizar |
---|
[34fc87f] | 2133 | // |
---|
[b0bb14f] | 2134 | // Descripción: |
---|
[9fd5e97] | 2135 | // Esta función actualiza la vista de ordenadores |
---|
[b0bb14f] | 2136 | // Parámetros: |
---|
[9fd5e97] | 2137 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2138 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2139 | int Actualizar(char *parametros) { |
---|
| 2140 | TRAMA *trama = (TRAMA*) malloc(LONGITUD_TRAMA); |
---|
| 2141 | if (!trama) |
---|
| 2142 | return (false); |
---|
| 2143 | int i, estado_cliente, lon; |
---|
| 2144 | char *iph, *rmb; |
---|
| 2145 | |
---|
| 2146 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2147 | rmb = toma_parametro("rmb", parametros); // Toma ipe del servidor rembo |
---|
| 2148 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 2149 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 2150 | if (IgualIP(iph, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
| 2151 | estado_cliente = strcmp(tbsockets[i].estado, CLIENTE_OCUPADO); |
---|
| 2152 | if (estado_cliente != 0) { // Cliente NO OCUPADO ... |
---|
| 2153 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2154 | CLIENTE_INICIANDO); |
---|
| 2155 | if (estado_cliente != 0) { // Cliente NO INICIANDO ... |
---|
| 2156 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2157 | CLIENTE_REMBO); |
---|
| 2158 | if (estado_cliente != 0) { // Cliente windows o linux ... |
---|
| 2159 | lon |
---|
| 2160 | = sprintf(trama->parametros, |
---|
| 2161 | "nfn=Actualizar\r"); |
---|
| 2162 | manda_comando(tbsockets[i].sock, |
---|
| 2163 | (char*) trama->parametros); |
---|
[34fc87f] | 2164 | } |
---|
| 2165 | borra_entrada(i); |
---|
| 2166 | } |
---|
| 2167 | } |
---|
| 2168 | } |
---|
| 2169 | } |
---|
| 2170 | } |
---|
| 2171 | int j; |
---|
[90f061f6] | 2172 | for (j = 0; j < MAXIMOS_SRVRMB; j++) { |
---|
| 2173 | if (strcmp(rmb, tbsocketsSRVRMB[j].ip) == 0) { // Si existe la IP ... |
---|
| 2174 | FINCADaINTRO(parametros, iph); |
---|
| 2175 | return (manda_trama_servidorrembo(rmb, parametros, |
---|
| 2176 | tbsocketsSRVRMB[j].puertorepo)); |
---|
[34fc87f] | 2177 | } |
---|
| 2178 | } |
---|
[90f061f6] | 2179 | return (false); |
---|
[34fc87f] | 2180 | } |
---|
| 2181 | // ________________________________________________________________________________________________________ |
---|
[2f4f9ff] | 2182 | // Función: ConsolaRemota |
---|
| 2183 | // |
---|
| 2184 | // Descripción: |
---|
| 2185 | // Esta función implementa la consola remota |
---|
| 2186 | // Parámetros: |
---|
[9fd5e97] | 2187 | // - parametros: parámetros del comando |
---|
[2f4f9ff] | 2188 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2189 | int ConsolaRemota(char *parametros) { |
---|
| 2190 | TRAMA *trama = (TRAMA*) malloc(LONGITUD_TRAMA); |
---|
| 2191 | if (!trama) |
---|
| 2192 | return (false); |
---|
| 2193 | int i, estado_cliente, lon; |
---|
| 2194 | char *iph, *rmb; |
---|
| 2195 | |
---|
| 2196 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2197 | rmb = toma_parametro("rmb", parametros); // Toma ipe del servidor rembo |
---|
| 2198 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 2199 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 2200 | if (IgualIP(iph, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
| 2201 | estado_cliente = strcmp(tbsockets[i].estado, CLIENTE_OCUPADO); |
---|
| 2202 | if (estado_cliente != 0) { // Cliente NO OCUPADO ... |
---|
| 2203 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2204 | CLIENTE_INICIANDO); |
---|
| 2205 | if (estado_cliente != 0) { // Cliente NO INICIANDO ... |
---|
| 2206 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2207 | CLIENTE_REMBO); |
---|
| 2208 | if (estado_cliente != 0) { // Cliente windows o linux ... |
---|
| 2209 | lon = sprintf(trama->parametros, |
---|
| 2210 | "nfn=ConsolaRemota\r"); |
---|
| 2211 | manda_comando(tbsockets[i].sock, |
---|
| 2212 | (char*) trama->parametros); |
---|
[2f4f9ff] | 2213 | } |
---|
| 2214 | } |
---|
| 2215 | } |
---|
| 2216 | } |
---|
| 2217 | } |
---|
| 2218 | } |
---|
| 2219 | int j; |
---|
[90f061f6] | 2220 | for (j = 0; j < MAXIMOS_SRVRMB; j++) { |
---|
| 2221 | if (strcmp(rmb, tbsocketsSRVRMB[j].ip) == 0) { // Si existe la IP ... |
---|
| 2222 | FINCADaINTRO(parametros, iph); |
---|
| 2223 | return (manda_trama_servidorrembo(rmb, parametros, |
---|
| 2224 | tbsocketsSRVRMB[j].puertorepo)); |
---|
[2f4f9ff] | 2225 | } |
---|
| 2226 | } |
---|
[90f061f6] | 2227 | return (false); |
---|
[2f4f9ff] | 2228 | } |
---|
| 2229 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2230 | // Función: FicheroOperador |
---|
[34fc87f] | 2231 | // |
---|
[b0bb14f] | 2232 | // Descripción: |
---|
[9fd5e97] | 2233 | // Esta función envía al servidor datos de un operador para crear fichero de login |
---|
[b0bb14f] | 2234 | // Parámetros: |
---|
[9fd5e97] | 2235 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2236 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2237 | int FicheroOperador(char *parametros) { |
---|
| 2238 | TRAMA trama; |
---|
[34fc87f] | 2239 | SOCKET s; |
---|
[90f061f6] | 2240 | char *rmb, *amb, *usu, *psw, *ida; |
---|
| 2241 | int resul, lon; |
---|
[34fc87f] | 2242 | |
---|
[90f061f6] | 2243 | rmb = toma_parametro("rmb", parametros); // Toma ipe del servidor rembo |
---|
[34fc87f] | 2244 | |
---|
[9fd5e97] | 2245 | // Abre conexion con el servidor rembo y envíatrama |
---|
[90f061f6] | 2246 | s = AbreConexion(rmb, puerto + 1); |
---|
| 2247 | if (!s) { |
---|
| 2248 | RegistraLog( |
---|
| 2249 | "Fallo al conectar con el servidor rembo para envio de tramas", |
---|
| 2250 | true); |
---|
| 2251 | return (FALSE); |
---|
[34fc87f] | 2252 | } |
---|
| 2253 | |
---|
[90f061f6] | 2254 | amb = toma_parametro("amb", parametros); // Toma tipo de operacion |
---|
| 2255 | usu = toma_parametro("usu", parametros); // Toma usuario |
---|
| 2256 | psw = toma_parametro("psw", parametros); // Toma passwrod |
---|
| 2257 | ida = toma_parametro("ida", parametros); // Toma identificador del aula |
---|
[34fc87f] | 2258 | |
---|
[9fd5e97] | 2259 | // envíala trama |
---|
[90f061f6] | 2260 | trama.arroba = '@'; |
---|
| 2261 | strncpy(trama.identificador, "JMMLCAMDJ", 9); |
---|
| 2262 | trama.ejecutor = '1'; |
---|
| 2263 | lon = sprintf(trama.parametros, "nfn=FicheroOperador\r"); |
---|
| 2264 | lon += sprintf(trama.parametros + lon, "amb=%s\r", amb); |
---|
| 2265 | lon += sprintf(trama.parametros + lon, "usu=%s\r", usu); |
---|
| 2266 | lon += sprintf(trama.parametros + lon, "psw=%s\r", psw); |
---|
| 2267 | lon += sprintf(trama.parametros + lon, "ida=%s\r", ida); |
---|
| 2268 | resul = (manda_trama(s, &trama)); |
---|
| 2269 | if (!resul) |
---|
| 2270 | RegistraLog("Fallo en el envio de trama al servidor rembo", true); |
---|
| 2271 | return (resul); |
---|
[34fc87f] | 2272 | } |
---|
| 2273 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2274 | // Función: Conmutar |
---|
[34fc87f] | 2275 | // |
---|
[b0bb14f] | 2276 | // Descripción: |
---|
[9fd5e97] | 2277 | // Esta función conmuta un cliente rembo del modo NO administrado al modo admnistrado |
---|
[b0bb14f] | 2278 | // Parámetros: |
---|
[9fd5e97] | 2279 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2280 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2281 | int Conmutar(char *parametros) { |
---|
| 2282 | TRAMA trama; |
---|
[34fc87f] | 2283 | SOCKET s; |
---|
[90f061f6] | 2284 | int i, estado_cliente, lon, resul; |
---|
| 2285 | char *iph, *rmb; |
---|
| 2286 | |
---|
| 2287 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2288 | rmb = toma_parametro("rmb", parametros); // Toma ipe del servidor rembo |
---|
| 2289 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 2290 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 2291 | if (IgualIP(iph, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
| 2292 | estado_cliente = strcmp(tbsockets[i].estado, CLIENTE_OCUPADO); |
---|
| 2293 | if (estado_cliente != 0) { // Cliente NO OCUPADO ... |
---|
| 2294 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2295 | CLIENTE_INICIANDO); |
---|
| 2296 | if (estado_cliente != 0) { // Cliente NO INICIANDO ... |
---|
| 2297 | estado_cliente = strcmp(tbsockets[i].estado, |
---|
| 2298 | CLIENTE_REMBO); |
---|
| 2299 | if (estado_cliente != 0) { // Cliente windows o linux ... |
---|
| 2300 | lon = sprintf(trama.parametros, "nfn=Conmutar\r"); |
---|
| 2301 | manda_comando(tbsockets[i].sock, trama.parametros); |
---|
[34fc87f] | 2302 | } |
---|
| 2303 | } |
---|
| 2304 | } |
---|
| 2305 | } |
---|
| 2306 | } |
---|
| 2307 | } |
---|
| 2308 | |
---|
[9fd5e97] | 2309 | // Abre conexión con el servidor rembo y envíatrama |
---|
[90f061f6] | 2310 | s = AbreConexion(rmb, puerto + 1); |
---|
| 2311 | if (!s) { |
---|
| 2312 | RegistraLog( |
---|
| 2313 | "Fallo al conectar con el servidor rembo para envio de tramas", |
---|
| 2314 | true); |
---|
| 2315 | resul = FALSE; |
---|
| 2316 | } else { |
---|
[9fd5e97] | 2317 | // Envía la trama |
---|
[90f061f6] | 2318 | trama.arroba = '@'; |
---|
| 2319 | strncpy(trama.identificador, "JMMLCAMDJ", 9); |
---|
| 2320 | trama.ejecutor = '2'; |
---|
| 2321 | lon = sprintf(trama.parametros, "nfn=Conmutar\r"); |
---|
| 2322 | lon += sprintf(trama.parametros + lon, "iph=%s\r", iph); |
---|
| 2323 | resul = (manda_trama(s, &trama)); |
---|
| 2324 | if (!resul) { |
---|
| 2325 | RegistraLog("Fallo en el envio de trama al servidor rembo", true); |
---|
[34fc87f] | 2326 | } |
---|
| 2327 | } |
---|
[90f061f6] | 2328 | return (resul); |
---|
[34fc87f] | 2329 | } |
---|
| 2330 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2331 | // Función: PurgarTablaSockets |
---|
[34fc87f] | 2332 | // |
---|
[b0bb14f] | 2333 | // Descripción: |
---|
[34fc87f] | 2334 | // Borra ordenadores de la tabla de sockets |
---|
[b0bb14f] | 2335 | // Parámetros: |
---|
[9fd5e97] | 2336 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2337 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2338 | void PurgarTablaSockets(char *parametros) { |
---|
[34fc87f] | 2339 | int i; |
---|
| 2340 | char *iph; |
---|
| 2341 | |
---|
[90f061f6] | 2342 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2343 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 2344 | if (strncmp(tbsockets[i].ip, "\0", 1) != 0) { // Si es un cliente activo |
---|
| 2345 | if (IgualIP(iph, tbsockets[i].ip)) { // Si existe la IP en la cadena |
---|
[34fc87f] | 2346 | borra_entrada(i); |
---|
| 2347 | } |
---|
| 2348 | } |
---|
| 2349 | } |
---|
| 2350 | } |
---|
| 2351 | // _____________________________________________________________________________________________________________ |
---|
| 2352 | // Función: Arrancar |
---|
| 2353 | // |
---|
| 2354 | // Descripción: |
---|
| 2355 | // Esta función arranca los ordenadores solicitados. PAra ello le envía el comando arrancar al servidor rembo que lo controla y |
---|
| 2356 | // es éste el que le envía la trama de wake-up |
---|
| 2357 | // Parámetros: |
---|
[9fd5e97] | 2358 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2359 | // _____________________________________________________________________________________________________________ |
---|
[90f061f6] | 2360 | int Arrancar(char *parametros) { |
---|
| 2361 | TRAMA *trama = (TRAMA*) malloc(LONGITUD_TRAMA); |
---|
| 2362 | if (!trama) |
---|
| 2363 | return (false); |
---|
| 2364 | char *iph, *rmb, *mac; |
---|
[34fc87f] | 2365 | int j; |
---|
| 2366 | |
---|
[90f061f6] | 2367 | rmb = toma_parametro("rmb", parametros); |
---|
| 2368 | mac = toma_parametro("mac", parametros); |
---|
| 2369 | iph = toma_parametro("iph", parametros); |
---|
[34fc87f] | 2370 | |
---|
[90f061f6] | 2371 | for (j = 0; j < MAXIMOS_SRVRMB; j++) { |
---|
| 2372 | if (strcmp(rmb, tbsocketsSRVRMB[j].ip) == 0) { // Si existe la IP ... |
---|
| 2373 | FINCADaINTRO(parametros, iph); |
---|
| 2374 | return (manda_trama_servidorrembo(rmb, parametros, |
---|
| 2375 | tbsocketsSRVRMB[j].puertorepo)); |
---|
[34fc87f] | 2376 | } |
---|
| 2377 | } |
---|
[90f061f6] | 2378 | return (false); |
---|
[34fc87f] | 2379 | } |
---|
| 2380 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2381 | // Función: RESPUESTA_Arrancar |
---|
[34fc87f] | 2382 | // |
---|
[b0bb14f] | 2383 | // Descripción: |
---|
[34fc87f] | 2384 | // Responde al comando Apagar |
---|
[b0bb14f] | 2385 | // Parámetros: |
---|
[34fc87f] | 2386 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
[9fd5e97] | 2387 | // - parametros: parámetros del comando |
---|
[34fc87f] | 2388 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2389 | int RESPUESTA_Arrancar(SOCKET s, char *parametros) { |
---|
[34fc87f] | 2390 | char ErrStr[200]; |
---|
| 2391 | Database db; |
---|
| 2392 | Table tbl; |
---|
| 2393 | |
---|
[90f061f6] | 2394 | char *res, *der, *iph, *ido, *ids; |
---|
| 2395 | |
---|
| 2396 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2397 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2398 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2399 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2400 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
[34fc87f] | 2401 | |
---|
[90f061f6] | 2402 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2403 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2404 | return (false); |
---|
[34fc87f] | 2405 | } |
---|
[90f061f6] | 2406 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2407 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2408 | } |
---|
| 2409 | db.Close(); |
---|
[90f061f6] | 2410 | return (true); |
---|
[34fc87f] | 2411 | } |
---|
| 2412 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2413 | // Función: RESPUESTA_Apagar |
---|
[34fc87f] | 2414 | // |
---|
[b0bb14f] | 2415 | // Descripción: |
---|
[34fc87f] | 2416 | // Responde al comando Apagar |
---|
[b0bb14f] | 2417 | // Parámetros: |
---|
[34fc87f] | 2418 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2419 | // - parametros: parametros del comando |
---|
| 2420 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2421 | int RESPUESTA_Apagar(SOCKET s, char *parametros) { |
---|
[34fc87f] | 2422 | char ErrStr[200]; |
---|
| 2423 | Database db; |
---|
| 2424 | Table tbl; |
---|
| 2425 | int i; |
---|
[90f061f6] | 2426 | char *res, *der, *iph, *ido, *ids; |
---|
[34fc87f] | 2427 | |
---|
[90f061f6] | 2428 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2429 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2430 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2431 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2432 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
[34fc87f] | 2433 | |
---|
[90f061f6] | 2434 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2435 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2436 | return (false); |
---|
[34fc87f] | 2437 | } |
---|
[90f061f6] | 2438 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2439 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2440 | } |
---|
| 2441 | |
---|
[90f061f6] | 2442 | if (strcmp(res, ACCION_FALLIDA) == 0) |
---|
[9fd5e97] | 2443 | return (TRUE); // Error en la ejecución de la acción en el cliente rembo |
---|
[34fc87f] | 2444 | |
---|
[90f061f6] | 2445 | if (cliente_existente(iph, &i)) // Si ya existe la IP ... |
---|
[34fc87f] | 2446 | borra_entrada(i); |
---|
| 2447 | db.Close(); |
---|
[90f061f6] | 2448 | return (true); |
---|
[34fc87f] | 2449 | } |
---|
| 2450 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2451 | // Función: RESPUESTA_Reiniciar |
---|
[34fc87f] | 2452 | // |
---|
[b0bb14f] | 2453 | // Descripción: |
---|
| 2454 | // Responde al comando Reiniciar |
---|
| 2455 | // Parámetros: |
---|
[34fc87f] | 2456 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2457 | // - parametros: parametros del comando |
---|
| 2458 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2459 | int RESPUESTA_Reiniciar(SOCKET s, char *parametros) { |
---|
[b0bb14f] | 2460 | int i; |
---|
[34fc87f] | 2461 | char ErrStr[200]; |
---|
| 2462 | Database db; |
---|
| 2463 | Table tbl; |
---|
| 2464 | |
---|
[90f061f6] | 2465 | char *res, *der, *iph, *ido, *ids; |
---|
[34fc87f] | 2466 | |
---|
[90f061f6] | 2467 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2468 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2469 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2470 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2471 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
| 2472 | |
---|
| 2473 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2474 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2475 | return (false); |
---|
[34fc87f] | 2476 | } |
---|
[90f061f6] | 2477 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2478 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2479 | } |
---|
[90f061f6] | 2480 | if (strcmp(res, ACCION_FALLIDA) == 0) |
---|
| 2481 | return (TRUE); // Error en la ejecucin de la acción en el cliente rembo |
---|
[34fc87f] | 2482 | |
---|
[90f061f6] | 2483 | if (cliente_existente(iph, &i)) // Si ya existe la IP ... |
---|
[34fc87f] | 2484 | borra_entrada(i); |
---|
| 2485 | db.Close(); |
---|
[90f061f6] | 2486 | return (true); |
---|
[34fc87f] | 2487 | } |
---|
| 2488 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 2489 | // Función: RESPUESTA_IniciarSesion |
---|
[34fc87f] | 2490 | // |
---|
[b0bb14f] | 2491 | // Descripción: |
---|
[9fd5e97] | 2492 | // Responde al comando Iniciar sesión |
---|
[b0bb14f] | 2493 | // Parámetros: |
---|
[34fc87f] | 2494 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2495 | // - parametros: parametros del comando |
---|
| 2496 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2497 | int RESPUESTA_IniciarSesion(SOCKET s, char *parametros) { |
---|
[34fc87f] | 2498 | char ErrStr[200]; |
---|
| 2499 | Database db; |
---|
| 2500 | Table tbl; |
---|
[b0bb14f] | 2501 | int i; |
---|
[90f061f6] | 2502 | char *res, *der, *iph, *ido, *ids; |
---|
[34fc87f] | 2503 | |
---|
[90f061f6] | 2504 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2505 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2506 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2507 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2508 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
[34fc87f] | 2509 | |
---|
[90f061f6] | 2510 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2511 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2512 | return (false); |
---|
[34fc87f] | 2513 | } |
---|
[90f061f6] | 2514 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2515 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2516 | } |
---|
[b0bb14f] | 2517 | |
---|
[90f061f6] | 2518 | if (strcmp(res, ACCION_FALLIDA) == 0) |
---|
| 2519 | return (TRUE); // Error en la ejecucin de la acción en el cliente rembo |
---|
[34fc87f] | 2520 | |
---|
[90f061f6] | 2521 | if (cliente_existente(iph, &i)) // Si ya existe la IP ... |
---|
[34fc87f] | 2522 | borra_entrada(i); |
---|
| 2523 | db.Close(); |
---|
[90f061f6] | 2524 | return (true); |
---|
[34fc87f] | 2525 | } |
---|
| 2526 | // ________________________________________________________________________________________________________ |
---|
| 2527 | // |
---|
[b0bb14f] | 2528 | // Función: borra_entrada |
---|
[34fc87f] | 2529 | // |
---|
[b0bb14f] | 2530 | // Descripción: |
---|
[34fc87f] | 2531 | // Borra la entrada de un ordenador en la tabla de socket |
---|
[b0bb14f] | 2532 | // Parámetros: |
---|
[34fc87f] | 2533 | // - i: Indice dentro de la tabla |
---|
| 2534 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2535 | int borra_entrada(int i) { |
---|
| 2536 | tbsockets[i].ip[0] = (char) NULL; |
---|
| 2537 | tbsockets[i].estado[0] = (char) NULL; |
---|
| 2538 | if (!tbsockets[i].sock) |
---|
| 2539 | close(tbsockets[i].sock); |
---|
| 2540 | tbsockets[i].sock = INVALID_SOCKET; |
---|
[51dcdc7] | 2541 | //tbsockets[i].ipsrvdhcp[0]=(char)NULL; |
---|
[90f061f6] | 2542 | tbsockets[i].ipsrvrmb[0] = (char) NULL; |
---|
[34fc87f] | 2543 | |
---|
[90f061f6] | 2544 | return (true); |
---|
[34fc87f] | 2545 | } |
---|
| 2546 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2547 | // Función: RESPUESTA_ExecShell |
---|
[34fc87f] | 2548 | // |
---|
[b0bb14f] | 2549 | // Descripción: |
---|
[34fc87f] | 2550 | // Responde al comando Ejecutar script |
---|
[b0bb14f] | 2551 | // Parámetros: |
---|
[34fc87f] | 2552 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2553 | // - parametros: parametros del comando |
---|
| 2554 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2555 | int RESPUESTA_ExecShell(SOCKET s, char *parametros) { |
---|
[34fc87f] | 2556 | char ErrStr[200]; |
---|
| 2557 | Database db; |
---|
| 2558 | Table tbl; |
---|
[90f061f6] | 2559 | |
---|
| 2560 | char *res, *der, *ids, *iph, *ido, *cfg; |
---|
| 2561 | |
---|
| 2562 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2563 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2564 | ids = toma_parametro("ids", parametros); // Toma idperfilsoft |
---|
| 2565 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2566 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2567 | cfg = toma_parametro("cfg", parametros); // Toma configuracin |
---|
| 2568 | |
---|
| 2569 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2570 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2571 | return (false); |
---|
[34fc87f] | 2572 | } |
---|
[90f061f6] | 2573 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2574 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2575 | } |
---|
| 2576 | |
---|
[9fd5e97] | 2577 | if (strcmp(res, ACCION_FALLIDA) != 0) { // Ha habido algún error en la ejecución de la acción del cliente rembo |
---|
| 2578 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) // El ordenador ha cambiado de configuración |
---|
[90f061f6] | 2579 | return (false); |
---|
[34fc87f] | 2580 | } |
---|
| 2581 | db.Close(); |
---|
[90f061f6] | 2582 | return (true); |
---|
[34fc87f] | 2583 | } |
---|
| 2584 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2585 | // Función: RespuestaEstandar |
---|
[34fc87f] | 2586 | // |
---|
[b0bb14f] | 2587 | // Descripción: |
---|
[9fd5e97] | 2588 | // Esta función actualiza la base de datos con el resultado de la ejecución de un comando con seguimiento |
---|
[b0bb14f] | 2589 | // Parámetros: |
---|
[34fc87f] | 2590 | // - res: resultado de la ejecucin del comando |
---|
[b0bb14f] | 2591 | // - der: Descripción del error si hubiese habido |
---|
| 2592 | // - ids: identificador de la acción notificada |
---|
[34fc87f] | 2593 | // - ido: Identificador del ordenador que notifica |
---|
| 2594 | // - db: Objeto base de datos (operativo) |
---|
| 2595 | // - tbl: Objeto tabla |
---|
| 2596 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2597 | int RespuestaEstandar(char *res, char *der, char *ids, char* ido, Database db, |
---|
| 2598 | Table tbl) { |
---|
| 2599 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 2600 | char parametros[LONGITUD_PARAMETROS]; |
---|
| 2601 | char fechareg[100]; |
---|
[90f061f6] | 2602 | int i, resul; |
---|
| 2603 | int idaccion, accionid, idnotificador; |
---|
[34fc87f] | 2604 | char *iph; |
---|
[90f061f6] | 2605 | struct tm* st; |
---|
[34fc87f] | 2606 | |
---|
| 2607 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[9fd5e97] | 2608 | // ACCESO único A TRAVES DE OBJETO MUTEX a este trozo de código |
---|
[90f061f6] | 2609 | pthread_mutex_lock(&guardia); |
---|
[34fc87f] | 2610 | |
---|
[90f061f6] | 2611 | sprintf(sqlstr, "Select * from acciones WHERE idaccion=%s", ids); |
---|
| 2612 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 2613 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2614 | pthread_mutex_unlock(&guardia); |
---|
| 2615 | return (false); |
---|
[34fc87f] | 2616 | } |
---|
[90f061f6] | 2617 | if (tbl.ISEOF()) { // No existe registro de acciones |
---|
| 2618 | pthread_mutex_unlock(&guardia); |
---|
| 2619 | return (true); |
---|
[34fc87f] | 2620 | } |
---|
[90f061f6] | 2621 | if (!tbl.Get("parametros", parametros)) { // Toma parametros de la acción |
---|
| 2622 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 2623 | pthread_mutex_unlock(&guardia); |
---|
| 2624 | return (false); |
---|
[34fc87f] | 2625 | } |
---|
[b0bb14f] | 2626 | char resultado[2]; // comprueba si ya ha fallado la acción |
---|
[90f061f6] | 2627 | if (!tbl.Get("resultado", resultado)) { // Toma resultado actual de la acción |
---|
| 2628 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 2629 | pthread_mutex_unlock(&guardia); |
---|
| 2630 | return (false); |
---|
| 2631 | } |
---|
| 2632 | if (!tbl.Get("idaccion", idaccion)) { // Toma el identificador de la acción para tener el dato en formato "int" |
---|
| 2633 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 2634 | pthread_mutex_unlock(&guardia); |
---|
| 2635 | return (false); |
---|
| 2636 | } |
---|
| 2637 | if (!tbl.Get("accionid", accionid)) { // Toma la accion padre |
---|
| 2638 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 2639 | pthread_mutex_unlock(&guardia); |
---|
| 2640 | return (false); |
---|
| 2641 | } |
---|
| 2642 | if (!tbl.Get("idnotificador", idnotificador)) { // Toma el identificador del notificador |
---|
| 2643 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 2644 | pthread_mutex_unlock(&guardia); |
---|
| 2645 | return (false); |
---|
| 2646 | } |
---|
| 2647 | |
---|
| 2648 | st = TomaHora(); |
---|
| 2649 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1, |
---|
| 2650 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
[34fc87f] | 2651 | |
---|
[9fd5e97] | 2652 | // Graba notificación |
---|
[90f061f6] | 2653 | sprintf( |
---|
| 2654 | sqlstr, |
---|
| 2655 | "INSERT INTO notificaciones (accionid,idnotificador,fechahorareg,resultado,descrinotificacion) VALUES (%s,%s,'%s','%s','%s')", |
---|
| 2656 | ids, ido, fechareg, res, der); |
---|
| 2657 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 2658 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2659 | pthread_mutex_unlock(&guardia); |
---|
| 2660 | return (false); |
---|
| 2661 | } |
---|
| 2662 | |
---|
| 2663 | if (strcmp(res, ACCION_FALLIDA) == 0 |
---|
| 2664 | && strcmp(resultado, ACCION_SINERRORES) == 0) { // Accion fallida en el cliente rembo |
---|
| 2665 | sprintf(sqlstr, "Update acciones set resultado='%s' WHERE idaccion=%s", |
---|
| 2666 | ACCION_CONERRORES, ids); |
---|
| 2667 | strcpy(resultado, ACCION_CONERRORES); |
---|
| 2668 | if (!db.Execute(sqlstr)) { // Error al actualizar |
---|
[34fc87f] | 2669 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2670 | pthread_mutex_unlock(&guardia); |
---|
| 2671 | return (false); |
---|
[34fc87f] | 2672 | } |
---|
| 2673 | } |
---|
[b0bb14f] | 2674 | // Comprueba si la acción se ejecutncorrectamente para el ambito sumando notificaciones |
---|
[34fc87f] | 2675 | INTROaFINCAD(parametros); |
---|
[90f061f6] | 2676 | iph = toma_parametro("iph", parametros); // Toma cadenaip |
---|
| 2677 | int tbnumipes = 0, totalipes = 1, lon; |
---|
[34fc87f] | 2678 | |
---|
[90f061f6] | 2679 | lon = strlen(iph); |
---|
| 2680 | for (i = 0; i < lon; i++) { |
---|
| 2681 | if (iph[i] == ';') |
---|
[34fc87f] | 2682 | totalipes++; // ip detectada |
---|
| 2683 | } |
---|
[90f061f6] | 2684 | |
---|
| 2685 | sprintf( |
---|
| 2686 | sqlstr, |
---|
| 2687 | "SELECT COUNT(*) AS tbnumipes FROM notificaciones WHERE accionid=%s", |
---|
| 2688 | ids); |
---|
| 2689 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
| 2690 | pthread_mutex_unlock(&guardia); |
---|
[34fc87f] | 2691 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2692 | pthread_mutex_unlock(&guardia); |
---|
| 2693 | return (false); |
---|
[34fc87f] | 2694 | } |
---|
[90f061f6] | 2695 | |
---|
| 2696 | if (!tbl.Get("tbnumipes", tbnumipes)) { // Recupera el numero de ordenadores que ya han notificado |
---|
| 2697 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo |
---|
| 2698 | pthread_mutex_unlock(&guardia); |
---|
| 2699 | return (false); |
---|
[34fc87f] | 2700 | } |
---|
[90f061f6] | 2701 | if (tbnumipes != totalipes) { |
---|
| 2702 | pthread_mutex_unlock(&guardia); |
---|
| 2703 | return (true); // No es el ultimo ordenador en notificar |
---|
[34fc87f] | 2704 | } |
---|
| 2705 | |
---|
[90f061f6] | 2706 | st = TomaHora(); |
---|
| 2707 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1, |
---|
| 2708 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
[34fc87f] | 2709 | |
---|
[9fd5e97] | 2710 | // Actualización después de que todos los ordenadores han notificado |
---|
| 2711 | if (strcmp(resultado, ACCION_SINERRORES) == 0) { // Acción finalizada con éxito |
---|
[90f061f6] | 2712 | sprintf( |
---|
| 2713 | sqlstr, |
---|
| 2714 | "Update acciones set estado='%s',resultado='%s',fechahorafin='%s' WHERE idaccion=%s", |
---|
| 2715 | ACCION_FINALIZADA, ACCION_EXITOSA, fechareg, ids); |
---|
| 2716 | if (!db.Execute(sqlstr, tbl)) { // Error al actualizar |
---|
[34fc87f] | 2717 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2718 | pthread_mutex_unlock(&guardia); |
---|
| 2719 | return (false); |
---|
[34fc87f] | 2720 | } |
---|
| 2721 | } |
---|
[9fd5e97] | 2722 | if (strcmp(resultado, ACCION_CONERRORES) == 0) { // Acción finalizada con errores |
---|
[90f061f6] | 2723 | sprintf( |
---|
| 2724 | sqlstr, |
---|
| 2725 | "Update acciones set estado='%s',resultado='%s',fechahorafin='%s' WHERE idaccion=%s", |
---|
| 2726 | ACCION_FINALIZADA, ACCION_FALLIDA, fechareg, ids); |
---|
| 2727 | if (!db.Execute(sqlstr, tbl)) { // Error al actualizar |
---|
[34fc87f] | 2728 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2729 | pthread_mutex_unlock(&guardia); |
---|
| 2730 | return (false); |
---|
[34fc87f] | 2731 | } |
---|
| 2732 | } |
---|
[90f061f6] | 2733 | resul = true; |
---|
[9fd5e97] | 2734 | if (accionid > 0) { // Existe acción padre que hay que actualizar |
---|
[90f061f6] | 2735 | resul = InsertaNotificaciones(idaccion, idnotificador, accionid, |
---|
| 2736 | resultado, db); |
---|
| 2737 | if (resul) |
---|
| 2738 | resul = comprueba_resultados(accionid, db); |
---|
[34fc87f] | 2739 | } |
---|
[90f061f6] | 2740 | pthread_mutex_unlock(&guardia); |
---|
[34fc87f] | 2741 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[90f061f6] | 2742 | return (resul); |
---|
[34fc87f] | 2743 | } |
---|
| 2744 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2745 | // Función: RESPUESTA_CrearPerfilSoftware |
---|
[34fc87f] | 2746 | // |
---|
[b0bb14f] | 2747 | // Descripción: |
---|
[34fc87f] | 2748 | // Responde al comando Crear Perfil Software |
---|
[b0bb14f] | 2749 | // Parámetros: |
---|
[34fc87f] | 2750 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2751 | // - parametros: parametros del comando |
---|
| 2752 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2753 | int RESPUESTA_CrearPerfilSoftware(SOCKET s, char *parametros) { |
---|
| 2754 | char ErrStr[200], sqlstr[1000]; |
---|
| 2755 | char *res, *der, *ids, *ifh, *ifs, *iph, *ido; |
---|
[34fc87f] | 2756 | Database db; |
---|
| 2757 | Table tbl; |
---|
[90f061f6] | 2758 | |
---|
| 2759 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2760 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2761 | return (false); |
---|
[34fc87f] | 2762 | } |
---|
| 2763 | |
---|
[90f061f6] | 2764 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2765 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2766 | ids = toma_parametro("ids", parametros); // Toma idperfilsoft |
---|
| 2767 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2768 | ido = toma_parametro("ido", parametros); // Toma dentificador del ordenador |
---|
| 2769 | ifh = toma_parametro("ifh", parametros); // Toma idperfilhard |
---|
| 2770 | ifs = toma_parametro("ifs", parametros); // Toma idperfilsoft |
---|
[34fc87f] | 2771 | |
---|
[90f061f6] | 2772 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2773 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2774 | } |
---|
| 2775 | |
---|
[9fd5e97] | 2776 | if (strcmp(res, ACCION_FALLIDA) == 0) { // Ha habido algún error en la ejecución de la acción en el cliente rembo |
---|
[34fc87f] | 2777 | db.Close(); |
---|
[90f061f6] | 2778 | return (false); |
---|
[34fc87f] | 2779 | } |
---|
| 2780 | |
---|
[90f061f6] | 2781 | sprintf( |
---|
| 2782 | sqlstr, |
---|
| 2783 | "Select * from perfileshard_perfilessoft WHERE idperfilhard=%s AND idperfilsoft=%s", |
---|
| 2784 | ifh, ifs); |
---|
| 2785 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 2786 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2787 | return (false); |
---|
[34fc87f] | 2788 | } |
---|
[90f061f6] | 2789 | if (!tbl.ISEOF()) { // Si ya existe el registro ... no hace falta insertarlo |
---|
[34fc87f] | 2790 | db.Close(); |
---|
[90f061f6] | 2791 | return (false); |
---|
[34fc87f] | 2792 | } |
---|
[90f061f6] | 2793 | sprintf( |
---|
| 2794 | sqlstr, |
---|
| 2795 | "INSERT INTO perfileshard_perfilessoft (idperfilhard,idperfilsoft) VALUES(%s,%s)", |
---|
| 2796 | ifh, ifs); |
---|
| 2797 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 2798 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2799 | return (false); |
---|
[34fc87f] | 2800 | } |
---|
| 2801 | db.Close(); |
---|
[90f061f6] | 2802 | return (true); |
---|
[34fc87f] | 2803 | } |
---|
| 2804 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2805 | // Función: RESPUESTA_CrearSoftwareIncremental |
---|
[34fc87f] | 2806 | // |
---|
[b0bb14f] | 2807 | // Descripción: |
---|
[9fd5e97] | 2808 | // Esta función responde a un comando de creación de un software incremental. Además actualiza la base de datos insertando |
---|
| 2809 | // en su caso la nueva combinación de perfil software con incremental. |
---|
[b0bb14f] | 2810 | // Parámetros: |
---|
[34fc87f] | 2811 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2812 | // - parametros: parametros del comando |
---|
| 2813 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2814 | int RESPUESTA_CrearSoftwareIncremental(SOCKET s, char *parametros) { |
---|
| 2815 | char ErrStr[200], sqlstr[1000]; |
---|
| 2816 | char *res, *der, *ids, *ifh, *ifs, *icr, *iph, *ido; |
---|
[34fc87f] | 2817 | int idphardidpsoft; |
---|
| 2818 | Database db; |
---|
| 2819 | Table tbl; |
---|
[90f061f6] | 2820 | |
---|
[9fd5e97] | 2821 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexión |
---|
[34fc87f] | 2822 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2823 | return (false); |
---|
[34fc87f] | 2824 | } |
---|
| 2825 | |
---|
[90f061f6] | 2826 | res = toma_parametro("res", parametros); // Toma resultado |
---|
[9fd5e97] | 2827 | der = toma_parametro("der", parametros); // Toma descripción del error ( si hubiera habido) |
---|
[90f061f6] | 2828 | ids = toma_parametro("ids", parametros); // Toma idperfilsoft |
---|
| 2829 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2830 | ido = toma_parametro("ido", parametros); // Toma dentificador del ordenador |
---|
| 2831 | ifh = toma_parametro("ifh", parametros); // Toma idperfilhard |
---|
| 2832 | ifs = toma_parametro("ifs", parametros); // Toma idperfilsoft |
---|
| 2833 | icr = toma_parametro("icr", parametros); // Toma idsoftincremental |
---|
[34fc87f] | 2834 | |
---|
[90f061f6] | 2835 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 2836 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 2837 | } |
---|
| 2838 | |
---|
[9fd5e97] | 2839 | if (strcmp(res, ACCION_FALLIDA) == 0) { // Ha habido algn error en la ejecución de la acción en el cliente rembo |
---|
[34fc87f] | 2840 | db.Close(); |
---|
[90f061f6] | 2841 | return (false); |
---|
[34fc87f] | 2842 | } |
---|
| 2843 | |
---|
[90f061f6] | 2844 | sprintf( |
---|
| 2845 | sqlstr, |
---|
| 2846 | "Select idphardidpsoft from perfileshard_perfilessoft WHERE idperfilhard=%s AND idperfilsoft=%s", |
---|
| 2847 | ifh, ifs); |
---|
| 2848 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 2849 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2850 | return (false); |
---|
[34fc87f] | 2851 | } |
---|
| 2852 | |
---|
[90f061f6] | 2853 | if (tbl.ISEOF()) { // Si no existe el registro ... |
---|
[34fc87f] | 2854 | db.Close(); |
---|
[90f061f6] | 2855 | return (false); |
---|
[34fc87f] | 2856 | } |
---|
| 2857 | |
---|
[9fd5e97] | 2858 | if (!tbl.Get("idphardidpsoft", idphardidpsoft)) { // Recupera el identificador de la combinación de perfiles |
---|
[34fc87f] | 2859 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo |
---|
[90f061f6] | 2860 | return (false); |
---|
[34fc87f] | 2861 | } |
---|
| 2862 | |
---|
[90f061f6] | 2863 | sprintf( |
---|
| 2864 | sqlstr, |
---|
| 2865 | "Select * from phard_psoft_softincremental WHERE idphardidpsoft=%d AND idsoftincremental=%s", |
---|
| 2866 | idphardidpsoft, icr); |
---|
| 2867 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 2868 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2869 | return (false); |
---|
[34fc87f] | 2870 | } |
---|
| 2871 | |
---|
[90f061f6] | 2872 | if (!tbl.ISEOF()) { // Si ya existe el registro ... |
---|
[34fc87f] | 2873 | db.Close(); |
---|
[90f061f6] | 2874 | return (false); |
---|
[34fc87f] | 2875 | } |
---|
| 2876 | |
---|
[90f061f6] | 2877 | sprintf( |
---|
| 2878 | sqlstr, |
---|
| 2879 | "INSERT INTO phard_psoft_softincremental (idphardidpsoft,idsoftincremental) VALUES(%d,%s)", |
---|
| 2880 | idphardidpsoft, icr); |
---|
| 2881 | if (!db.Execute(sqlstr, tbl)) { // Error al insertar |
---|
[34fc87f] | 2882 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2883 | return (false); |
---|
[34fc87f] | 2884 | } |
---|
[90f061f6] | 2885 | db.Close(); |
---|
| 2886 | return (true); |
---|
[34fc87f] | 2887 | } |
---|
| 2888 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2889 | // Función: RESPUESTA_RestaurarImagen |
---|
[34fc87f] | 2890 | // |
---|
[b0bb14f] | 2891 | // Descripción: |
---|
[9fd5e97] | 2892 | // Esta función responde a un comando de restauracin de una imagen. Además actualiza la base de datos. |
---|
[b0bb14f] | 2893 | // Parámetros: |
---|
[34fc87f] | 2894 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 2895 | // - parametros: parametros del comando |
---|
| 2896 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2897 | int RESPUESTA_RestaurarImagen(SOCKET s, char *parametros) { |
---|
| 2898 | char ErrStr[200], gido[20]; |
---|
| 2899 | char *res, *der, *ids, *iph, *ido, *idi, *par, *cfg; |
---|
[34fc87f] | 2900 | Database db; |
---|
| 2901 | Table tbl; |
---|
| 2902 | |
---|
[90f061f6] | 2903 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 2904 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2905 | return (false); |
---|
[34fc87f] | 2906 | } |
---|
| 2907 | |
---|
| 2908 | INTROaFINCAD(parametros); |
---|
| 2909 | |
---|
[90f061f6] | 2910 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 2911 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 2912 | ids = toma_parametro("ids", parametros); // Toma identificador de la accion |
---|
| 2913 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 2914 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 2915 | cfg = toma_parametro("cfg", parametros); // Toma configuracin |
---|
| 2916 | par = toma_parametro("par", parametros); // particion |
---|
| 2917 | idi = toma_parametro("idi", parametros); // identificador de la imagen |
---|
[34fc87f] | 2918 | |
---|
[90f061f6] | 2919 | strcpy(gido, ido); // Guarda el identificador del ordenador |
---|
[34fc87f] | 2920 | |
---|
[90f061f6] | 2921 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
| 2922 | return (false); // Error al registrar notificacion |
---|
[34fc87f] | 2923 | } |
---|
[90f061f6] | 2924 | if (strcmp(res, ACCION_FALLIDA) == 0) { // Ha habido algn error en la ejecucin de la acción del cliente rembo |
---|
[34fc87f] | 2925 | db.Close(); |
---|
[90f061f6] | 2926 | return (false); |
---|
[34fc87f] | 2927 | } |
---|
| 2928 | |
---|
[90f061f6] | 2929 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) |
---|
| 2930 | return (false); // Erro al actualiza la configuracin |
---|
| 2931 | if (!Actualiza_ordenador_imagen(par, idi, gido, db)) |
---|
| 2932 | return (false); |
---|
[34fc87f] | 2933 | db.Close(); |
---|
[90f061f6] | 2934 | return (true); |
---|
[34fc87f] | 2935 | } |
---|
| 2936 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 2937 | // Función: Actualiza_ordenador_imagen |
---|
[34fc87f] | 2938 | // |
---|
[b0bb14f] | 2939 | // Descripción: |
---|
[9fd5e97] | 2940 | // Esta función actualiza la tabla ordenador_imagen |
---|
[b0bb14f] | 2941 | // Parámetros: |
---|
[9fd5e97] | 2942 | // - par: partición |
---|
[34fc87f] | 2943 | // - idi: identificador de la imagen ( 0 ninguna ) |
---|
| 2944 | // - ido: identificador del ordenador |
---|
[9fd5e97] | 2945 | // - db: Conexión ADO operativa |
---|
[34fc87f] | 2946 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 2947 | int Actualiza_ordenador_imagen(char *par, const char *idi, char *ido, |
---|
| 2948 | Database db) { |
---|
| 2949 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 2950 | Table tbl; |
---|
[90f061f6] | 2951 | int idimagen, idimagenres; |
---|
| 2952 | |
---|
| 2953 | idimagenres = atoi(idi); |
---|
[9fd5e97] | 2954 | if (idimagenres == 0) { // Se ha formateado la partición y se ha borrado la imagen por tanto |
---|
[90f061f6] | 2955 | sprintf( |
---|
| 2956 | sqlstr, |
---|
| 2957 | "DELETE FROM ordenador_imagen WHERE idordenador=%s AND particion=%s", |
---|
| 2958 | ido, par); |
---|
| 2959 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 2960 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2961 | return (false); |
---|
[34fc87f] | 2962 | } |
---|
[90f061f6] | 2963 | return (true); |
---|
[34fc87f] | 2964 | } |
---|
| 2965 | |
---|
[90f061f6] | 2966 | sprintf( |
---|
| 2967 | sqlstr, |
---|
| 2968 | "SELECT idimagen FROM ordenador_imagen INNER JOIN ordenadores ON ordenador_imagen.idordenador = ordenadores.idordenador WHERE ordenadores.idordenador = %s AND ordenador_imagen.particion = %s", |
---|
| 2969 | ido, par); |
---|
| 2970 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 2971 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2972 | return (false); |
---|
[34fc87f] | 2973 | } |
---|
[90f061f6] | 2974 | if (!tbl.ISEOF()) { // Existe registro |
---|
| 2975 | if (!tbl.Get("idimagen", idimagen)) { |
---|
[34fc87f] | 2976 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 2977 | return (false); |
---|
| 2978 | } else { |
---|
| 2979 | if (idimagenres != idimagen) { |
---|
| 2980 | sprintf( |
---|
| 2981 | sqlstr, |
---|
| 2982 | "Update ordenador_imagen set idimagen=%s WHERE idordenador=%s AND particion=%s", |
---|
| 2983 | idi, ido, par); |
---|
| 2984 | if (!db.Execute(sqlstr)) { // Error al actualizar |
---|
[34fc87f] | 2985 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 2986 | return (false); |
---|
[34fc87f] | 2987 | } |
---|
| 2988 | } |
---|
| 2989 | } |
---|
[90f061f6] | 2990 | } else { // No existe el registro |
---|
| 2991 | sprintf( |
---|
| 2992 | sqlstr, |
---|
| 2993 | "INSERT INTO ordenador_imagen (idordenador,particion,idimagen) VALUES(%s,%s,%s)", |
---|
| 2994 | ido, par, idi); |
---|
| 2995 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
| 2996 | db.GetErrorErrStr(ErrStr); |
---|
| 2997 | return (false); |
---|
| 2998 | } |
---|
[34fc87f] | 2999 | } |
---|
[90f061f6] | 3000 | return (true); |
---|
[34fc87f] | 3001 | } |
---|
| 3002 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3003 | // Función: RESPUESTA_ParticionaryFormatear |
---|
[34fc87f] | 3004 | // |
---|
[b0bb14f] | 3005 | // Descripción: |
---|
[9fd5e97] | 3006 | // Esta función responde a un comando de particionar y formatear. Además actualiza la base de datos. |
---|
[b0bb14f] | 3007 | // Parámetros: |
---|
[34fc87f] | 3008 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 3009 | // - parametros: parametros del comando |
---|
| 3010 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3011 | int RESPUESTA_ParticionaryFormatear(SOCKET s, char *parametros) { |
---|
| 3012 | char sqlstr[1000], ErrStr[200], gido[20]; |
---|
[34fc87f] | 3013 | Database db; |
---|
| 3014 | Table tbl; |
---|
[90f061f6] | 3015 | char *res, *der, *ids, *iph, *ido, *cfg; |
---|
| 3016 | |
---|
| 3017 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 3018 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 3019 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
| 3020 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 3021 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 3022 | cfg = toma_parametro("cfg", parametros); // Toma configuracin |
---|
| 3023 | |
---|
| 3024 | strcpy(gido, ido); // Guarda el identificador del ordenador |
---|
| 3025 | |
---|
| 3026 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 3027 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3028 | return (false); |
---|
[34fc87f] | 3029 | } |
---|
[90f061f6] | 3030 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
| 3031 | return (false); // Error al registrar notificacion |
---|
[34fc87f] | 3032 | } |
---|
[90f061f6] | 3033 | if (strcmp(res, ACCION_FALLIDA) == 0) { |
---|
| 3034 | db.Close(); |
---|
| 3035 | return (true); // Ha habido algn error en la ejecucin de la acción del cliente rembo |
---|
[34fc87f] | 3036 | } |
---|
[90f061f6] | 3037 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) |
---|
[9fd5e97] | 3038 | return (false); // Error al actualiza la configuración |
---|
[34fc87f] | 3039 | |
---|
[9fd5e97] | 3040 | // Elimina información sobre imagenes en este ordenador, al haber sido formateado |
---|
[90f061f6] | 3041 | sprintf(sqlstr, "DELETE FROM ordenador_imagen WHERE idordenador=%s", gido); |
---|
| 3042 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 3043 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3044 | return (false); |
---|
[34fc87f] | 3045 | } |
---|
[90f061f6] | 3046 | db.Close(); |
---|
| 3047 | return (true); |
---|
[34fc87f] | 3048 | } |
---|
| 3049 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3050 | // Función: RESPUESTA_Configurar |
---|
[34fc87f] | 3051 | // |
---|
[b0bb14f] | 3052 | // Descripción: |
---|
[9fd5e97] | 3053 | // Esta función responde a un comando de Configurar. Además actualiza la base de datos. |
---|
[b0bb14f] | 3054 | // Parámetros: |
---|
[34fc87f] | 3055 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
[9fd5e97] | 3056 | // - parametros: parámetros del comando |
---|
[34fc87f] | 3057 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3058 | int RESPUESTA_Configurar(SOCKET s, char *parametros) { |
---|
| 3059 | char ErrStr[200], gids[20], gido[20]; |
---|
[34fc87f] | 3060 | Database db; |
---|
| 3061 | Table tbl; |
---|
[90f061f6] | 3062 | int lon, resul, i; |
---|
| 3063 | char *res, *der, *ids, *iph, *ido, *cfg, *hdc; |
---|
[34fc87f] | 3064 | |
---|
[90f061f6] | 3065 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 3066 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 3067 | ids = toma_parametro("ids", parametros); // Toma idperfilsoft |
---|
| 3068 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 3069 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 3070 | cfg = toma_parametro("cfg", parametros); // Toma configuracin |
---|
| 3071 | hdc = toma_parametro("hdc", parametros); // Toma participaciones a formatear |
---|
[34fc87f] | 3072 | |
---|
[90f061f6] | 3073 | strcpy(gids, ids); // Guarda el identificador de la acción |
---|
| 3074 | strcpy(gido, ido); // Guarda el identificador del ordenador |
---|
[34fc87f] | 3075 | |
---|
[90f061f6] | 3076 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 3077 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3078 | return (false); |
---|
[34fc87f] | 3079 | } |
---|
[90f061f6] | 3080 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
| 3081 | return (false); // Error al registrar notificacion |
---|
[34fc87f] | 3082 | } |
---|
| 3083 | |
---|
[90f061f6] | 3084 | if (strcmp(res, ACCION_FALLIDA) == 0) { |
---|
[34fc87f] | 3085 | db.Close(); |
---|
[90f061f6] | 3086 | return (true); // Ha habido algn error en la ejecucin de la acción del cliente rembo |
---|
| 3087 | } |
---|
| 3088 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) |
---|
[9fd5e97] | 3089 | return (false); // Error al actualiza la configuracin |
---|
[90f061f6] | 3090 | |
---|
| 3091 | lon = strlen(hdc); |
---|
| 3092 | for (i = 0; i < lon; i++) { |
---|
| 3093 | if (hdc[i] == ';') |
---|
| 3094 | hdc[i] = '\0'; |
---|
| 3095 | } |
---|
| 3096 | for (i = 0; i < lon; i++) { |
---|
| 3097 | if (*hdc != '\0') { |
---|
| 3098 | resul = Actualiza_ordenador_imagen(hdc, "0", gido, db); |
---|
| 3099 | if (!resul) { |
---|
[34fc87f] | 3100 | db.Close(); |
---|
[90f061f6] | 3101 | return (false); |
---|
[34fc87f] | 3102 | } |
---|
| 3103 | } |
---|
| 3104 | hdc++; |
---|
| 3105 | } |
---|
| 3106 | db.Close(); |
---|
[90f061f6] | 3107 | return (true); |
---|
[34fc87f] | 3108 | } |
---|
| 3109 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3110 | // Función: RESPUESTA_TomaConfiguracion |
---|
[34fc87f] | 3111 | // |
---|
[b0bb14f] | 3112 | // Descripción: |
---|
[9fd5e97] | 3113 | // Esta función responde a un comando de Toma Comfiguracin. Además actualiza la base de datos. |
---|
[b0bb14f] | 3114 | // Parámetros: |
---|
[34fc87f] | 3115 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
[9fd5e97] | 3116 | // - parametros: parámetros del comando |
---|
[34fc87f] | 3117 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3118 | int RESPUESTA_TomaConfiguracion(SOCKET s, char *parametros) { |
---|
[34fc87f] | 3119 | char ErrStr[200]; |
---|
| 3120 | Database db; |
---|
| 3121 | Table tbl; |
---|
[90f061f6] | 3122 | |
---|
| 3123 | char *res, *der, *ids, *iph, *ido, *cfg; |
---|
| 3124 | |
---|
| 3125 | res = toma_parametro("res", parametros); // Toma resultado |
---|
[9fd5e97] | 3126 | der = toma_parametro("der", parametros); // Toma descripción del error ( si hubiera habido) |
---|
[90f061f6] | 3127 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
| 3128 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 3129 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
[9fd5e97] | 3130 | cfg = toma_parametro("cfg", parametros); // Toma configuración |
---|
[90f061f6] | 3131 | |
---|
[9fd5e97] | 3132 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexión |
---|
[34fc87f] | 3133 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3134 | return (false); |
---|
[34fc87f] | 3135 | } |
---|
[90f061f6] | 3136 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 3137 | return (false); // Error al registrar notificación |
---|
[34fc87f] | 3138 | } |
---|
[9fd5e97] | 3139 | if (strcmp(res, ACCION_FALLIDA) != 0) { // Ha habido algn error en la ejecución de la acción del cliente rembo |
---|
| 3140 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) // El ordenador ha cambiado de configuración |
---|
[90f061f6] | 3141 | return (false); |
---|
[34fc87f] | 3142 | } |
---|
| 3143 | db.Close(); |
---|
[90f061f6] | 3144 | return (true); |
---|
[34fc87f] | 3145 | } |
---|
| 3146 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3147 | // Función: RESPUESTA_TomaHardware |
---|
[34fc87f] | 3148 | // |
---|
[b0bb14f] | 3149 | // Descripción: |
---|
[9fd5e97] | 3150 | // Esta función responde a un comando de Toma HArdware. Además actualiza la base de datos. |
---|
[b0bb14f] | 3151 | // Parámetros: |
---|
[34fc87f] | 3152 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
| 3153 | // - parametros: parametros del comando |
---|
| 3154 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3155 | int RESPUESTA_TomaHardware(SOCKET s, char *parametros) { |
---|
[34fc87f] | 3156 | char ErrStr[200]; |
---|
| 3157 | Database db; |
---|
| 3158 | Table tbl; |
---|
[90f061f6] | 3159 | char nomfiledst[512]; |
---|
| 3160 | char *res, *der, *ids, *iph, *ido, *hrd, *ipr, *rep; |
---|
| 3161 | |
---|
| 3162 | res = toma_parametro("res", parametros); // Toma resultado |
---|
| 3163 | der = toma_parametro("der", parametros); // Toma descripcin del error ( si hubiera habido) |
---|
| 3164 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
| 3165 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 3166 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 3167 | |
---|
| 3168 | hrd = toma_parametro("hrd", parametros); // Toma nombre del archivo de inventario |
---|
| 3169 | ipr = toma_parametro("ipr", parametros); // Dirección IP repositorio |
---|
| 3170 | rep = toma_parametro("rep", parametros); // puerto comunicaciones |
---|
| 3171 | |
---|
[9fd5e97] | 3172 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexión |
---|
[34fc87f] | 3173 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3174 | return (false); |
---|
[34fc87f] | 3175 | } |
---|
[90f061f6] | 3176 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
| 3177 | return (false); // Error al registrar notificacion |
---|
[34fc87f] | 3178 | } |
---|
[90f061f6] | 3179 | if (strcmp(res, ACCION_FALLIDA) != 0) { |
---|
| 3180 | sprintf(nomfiledst, "/tmp/hard-%s", iph); // Nombre del fichero destino |
---|
| 3181 | if (recibeFichero(ipr, rep, hrd, nomfiledst)) { |
---|
| 3182 | if (!actualiza_hardware(db, tbl, nomfiledst, iph, ido)) |
---|
| 3183 | return (false); |
---|
| 3184 | } else |
---|
| 3185 | return (false); |
---|
[34fc87f] | 3186 | } |
---|
| 3187 | db.Close(); |
---|
[90f061f6] | 3188 | return (true); |
---|
| 3189 | } |
---|
| 3190 | //______________________________________________________________________________________________________ |
---|
| 3191 | // Función: recibeFichero |
---|
| 3192 | // |
---|
| 3193 | // Descripción: |
---|
| 3194 | // Se trae un fichero del repositorio y lo coloca en el diretorio /tmp |
---|
| 3195 | // Parámetros: |
---|
| 3196 | // - nomfile : Nombre del fichero |
---|
| 3197 | // Devuelve: |
---|
| 3198 | // true si el proceso es correcto y false en caso contrario |
---|
| 3199 | // Especificaciones: |
---|
| 3200 | // En los parametros de la trama se copian el contenido del del archivo de comandos |
---|
| 3201 | // ________________________________________________________________________________________________________ |
---|
| 3202 | int recibeFichero(char *ipr, char *rep, char *nomfilesrc, char *nomfiledst) { |
---|
| 3203 | SOCKET udpsock; |
---|
| 3204 | int blk, lsize; |
---|
| 3205 | char *b, *l; |
---|
| 3206 | FILE *f; |
---|
| 3207 | int ret; |
---|
| 3208 | struct sockaddr_in addrRepo; |
---|
| 3209 | socklen_t iAddrSize = sizeof(addrRepo); |
---|
| 3210 | TRAMA trama; |
---|
| 3211 | |
---|
| 3212 | udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
| 3213 | if (udpsock == SOCKET_ERROR) { |
---|
| 3214 | RegistraLog( |
---|
| 3215 | "*** No se ha podido crear socket para comunicación con el repositorio en módulo recibeFichero", |
---|
| 3216 | true); |
---|
| 3217 | return (false); |
---|
| 3218 | } |
---|
| 3219 | |
---|
| 3220 | f = fopen(nomfiledst, "wb"); |
---|
| 3221 | if (!f) { |
---|
| 3222 | RegistraLog("*** No se ha podido crear archivo", false); |
---|
| 3223 | close(udpsock); |
---|
| 3224 | return (false); |
---|
| 3225 | } |
---|
| 3226 | |
---|
| 3227 | sprintf(trama.parametros, "nfn=mandaFichero\rnfl=%s\r", nomfilesrc); // Nombre de la función a ejecutar en el servidor de administración |
---|
| 3228 | if (envia_comandos(udpsock, &trama, ipr, atoi(rep))) { |
---|
| 3229 | b = &trama.arroba; // Puntero al comienzo de la trama para colocar el bloque leido |
---|
| 3230 | l = b + sizeof(blk); // Puntero después del dato bloque para colocar los bytes leidos |
---|
| 3231 | do { |
---|
| 3232 | ret = recvfrom(udpsock, (char *) &trama, LONGITUD_TRAMA, 0, |
---|
| 3233 | (struct sockaddr *) &addrRepo, &iAddrSize); |
---|
| 3234 | if (ret) { |
---|
| 3235 | memcpy(&blk, b, sizeof(blk)); |
---|
| 3236 | memcpy(&lsize, l, sizeof(lsize)); |
---|
| 3237 | if (lsize > 0) |
---|
| 3238 | lsize = fwrite(trama.parametros, 1, lsize, f); // Escribe contenido en el fichero |
---|
| 3239 | else { |
---|
| 3240 | fclose(f); |
---|
| 3241 | close(udpsock); |
---|
| 3242 | return (true); |
---|
| 3243 | } |
---|
| 3244 | } else { |
---|
| 3245 | RegistraLog("*** Error de recepción de archivo", false); |
---|
| 3246 | break; |
---|
| 3247 | } |
---|
| 3248 | envia_comandos(udpsock, &trama, ipr, atoi(rep)); |
---|
| 3249 | } while (lsize > 0); |
---|
| 3250 | fclose(f); |
---|
| 3251 | } else { |
---|
| 3252 | RegistraLog( |
---|
| 3253 | "*** Error de envío de trama al repositorio en módulo recibeFichero", |
---|
| 3254 | false); |
---|
| 3255 | close(udpsock); |
---|
| 3256 | fclose(f); |
---|
| 3257 | return (false); |
---|
| 3258 | } |
---|
| 3259 | close(udpsock); |
---|
| 3260 | return (true); |
---|
[34fc87f] | 3261 | } |
---|
| 3262 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3263 | // Función: RESPUESTA_TomaSoftware |
---|
[5eae07e] | 3264 | // |
---|
[b0bb14f] | 3265 | // Descripción: |
---|
[9fd5e97] | 3266 | // Esta función responde a un comando de Inventario Software. Además actualiza la base de datos. |
---|
[b0bb14f] | 3267 | // Parámetros: |
---|
[5eae07e] | 3268 | // - s: Socket que el cliente rembo usa para comunicarse con el servidor HIDRA |
---|
[9fd5e97] | 3269 | // - parametros: parámetros del comando |
---|
[5eae07e] | 3270 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3271 | int RESPUESTA_TomaSoftware(SOCKET s, char *parametros) { |
---|
[5eae07e] | 3272 | char ErrStr[200]; |
---|
| 3273 | Database db; |
---|
| 3274 | Table tbl; |
---|
[90f061f6] | 3275 | char nomfiledst[512]; |
---|
| 3276 | char *res, *der, *ids, *iph, *ido, *sft, *par, *tfs, *ipr, *rep; |
---|
| 3277 | |
---|
| 3278 | res = toma_parametro("res", parametros); // Toma resultado |
---|
[9fd5e97] | 3279 | der = toma_parametro("der", parametros); // Toma descripción del error ( si hubiera habido) |
---|
[90f061f6] | 3280 | ids = toma_parametro("ids", parametros); // Toma identificador de la acción |
---|
| 3281 | iph = toma_parametro("iph", parametros); // Toma ip |
---|
| 3282 | ido = toma_parametro("ido", parametros); // Toma identificador del ordenador |
---|
| 3283 | |
---|
| 3284 | sft = toma_parametro("sft", parametros); // Toma software |
---|
| 3285 | par = toma_parametro("par", parametros); // Toma partición |
---|
| 3286 | tfs = toma_parametro("tfs", parametros); // Toma tipo partición |
---|
| 3287 | |
---|
| 3288 | ipr = toma_parametro("ipr", parametros); // Dirección IP repositorio |
---|
| 3289 | rep = toma_parametro("rep", parametros); // puerto comunicaciones |
---|
| 3290 | |
---|
[9fd5e97] | 3291 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexión |
---|
[5eae07e] | 3292 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3293 | return (false); |
---|
[5eae07e] | 3294 | } |
---|
[90f061f6] | 3295 | if (!RespuestaEstandar(res, der, ids, ido, db, tbl)) { |
---|
[9fd5e97] | 3296 | return (false); // Error al registrar notificación |
---|
[5eae07e] | 3297 | } |
---|
[9fd5e97] | 3298 | if (strcmp(res, ACCION_FALLIDA) != 0) { // Ha habido algn error en la ejecución de la acción del cliente rembo |
---|
[90f061f6] | 3299 | sprintf(nomfiledst, "/tmp/soft-%s-%s", iph, par); // Nombre del fichero destino |
---|
| 3300 | if (recibeFichero(ipr, rep, sft, nomfiledst)) { |
---|
[9fd5e97] | 3301 | if (!actualiza_software(db, tbl, nomfiledst, par, tfs, iph, ido)) // El ordenador ha cambiado de configuración |
---|
[90f061f6] | 3302 | return (false); |
---|
| 3303 | } |
---|
| 3304 | } else |
---|
| 3305 | return (false); |
---|
| 3306 | |
---|
[5eae07e] | 3307 | db.Close(); |
---|
[90f061f6] | 3308 | return (true); |
---|
[5eae07e] | 3309 | } |
---|
| 3310 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3311 | // Función: busca_comandos |
---|
[34fc87f] | 3312 | // |
---|
[b0bb14f] | 3313 | // Descripción: |
---|
[9fd5e97] | 3314 | // Esta función busca en la base de datos,comandos pendientes de ejecutar para el ordenador cocreto |
---|
[b0bb14f] | 3315 | // Parámetros: |
---|
[9fd5e97] | 3316 | // - iph: Dirección IP del ordenador |
---|
[34fc87f] | 3317 | // - ido: Identificador del ordenador |
---|
[b0bb14f] | 3318 | // - parametros: parametros de la acción buscada |
---|
| 3319 | // - ids: Identificador de la acción |
---|
[34fc87f] | 3320 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3321 | int busca_comandos(char* iph, char *ido, char *parametros, int *ids) { |
---|
| 3322 | char sqlstr[1000], ErrStr[200]; |
---|
[34fc87f] | 3323 | Database db; |
---|
[90f061f6] | 3324 | Table tbl, tbln; |
---|
[34fc87f] | 3325 | |
---|
[90f061f6] | 3326 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion |
---|
[34fc87f] | 3327 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3328 | return (false); |
---|
| 3329 | } |
---|
| 3330 | sprintf( |
---|
| 3331 | sqlstr, |
---|
| 3332 | "SELECT idaccion,resultado,estado,parametros FROM acciones WHERE tipoaccion=%d AND estado = '%s' AND (resultado='%s' OR resultado='%s') AND parametros LIKE '%c%s%c' ORDER BY idaccion", |
---|
| 3333 | EJECUCION_COMANDO, ACCION_INICIADA, ACCION_SINERRORES, |
---|
| 3334 | ACCION_CONERRORES, 37, iph, 37); |
---|
| 3335 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3336 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3337 | return (false); |
---|
[34fc87f] | 3338 | } |
---|
[90f061f6] | 3339 | if (tbl.ISEOF()) { |
---|
[34fc87f] | 3340 | db.Close(); |
---|
[90f061f6] | 3341 | return (false); // No hay comandos pendientes |
---|
[34fc87f] | 3342 | } |
---|
| 3343 | |
---|
[90f061f6] | 3344 | while (!tbl.ISEOF()) { // Busca entre todas las acciones de diversos ambitos |
---|
[34fc87f] | 3345 | |
---|
[9fd5e97] | 3346 | if (!tbl.Get("parametros", parametros)) { // Toma parámetros |
---|
[90f061f6] | 3347 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo parametros |
---|
| 3348 | return (false); |
---|
[34fc87f] | 3349 | } |
---|
| 3350 | |
---|
[90f061f6] | 3351 | if (IgualIP(parametros, iph)) { // Si existe la IP en la cadena |
---|
| 3352 | if (!tbl.Get("idaccion", *ids)) { // Toma identificador de la acción |
---|
[34fc87f] | 3353 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3354 | return (false); |
---|
[34fc87f] | 3355 | } |
---|
| 3356 | |
---|
[9fd5e97] | 3357 | // Comprueba que aunque el resultado es ACCION_INICIADA, este ordenador aún no ha notificado |
---|
[90f061f6] | 3358 | sprintf( |
---|
| 3359 | sqlstr, |
---|
| 3360 | "SELECT idnotificador FROM notificaciones WHERE accionid=%d AND idnotificador=%s", |
---|
| 3361 | *ids, ido); |
---|
| 3362 | if (!db.Execute(sqlstr, tbln)) { // Error al leer |
---|
[34fc87f] | 3363 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3364 | return (false); |
---|
[34fc87f] | 3365 | } |
---|
[90f061f6] | 3366 | if (tbln.ISEOF()) { |
---|
| 3367 | db.Close(); |
---|
| 3368 | return (true); // No ha notificado este ordenador |
---|
[34fc87f] | 3369 | } |
---|
| 3370 | } |
---|
| 3371 | tbl.MoveNext(); |
---|
| 3372 | } |
---|
| 3373 | db.Close(); |
---|
[90f061f6] | 3374 | return (false); // No hay mn acciones |
---|
[34fc87f] | 3375 | } |
---|
| 3376 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3377 | // Función: InsertaNotificaciones |
---|
| 3378 | // |
---|
| 3379 | // |
---|
| 3380 | // Parámetros: |
---|
| 3381 | // - idaccion: Identificador en la base de datos de la acción |
---|
| 3382 | // - idnotificador: Identificador en la base de datos de la notificación |
---|
| 3383 | // - accionid: Identificador de la acción padre |
---|
| 3384 | // - resultado: Resultado de la acción |
---|
| 3385 | // - db: Objeto conexión con la base de datos |
---|
| 3386 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3387 | int InsertaNotificaciones(int idaccion, int idnotificador, int accionid, |
---|
| 3388 | char *resultado, Database db) { |
---|
| 3389 | |
---|
| 3390 | struct tm* st; |
---|
| 3391 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 3392 | char fechahorareg[100]; |
---|
| 3393 | char descrinotificacion[100]; |
---|
| 3394 | |
---|
[90f061f6] | 3395 | st = TomaHora(); |
---|
| 3396 | sprintf(fechahorareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon |
---|
| 3397 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
[34fc87f] | 3398 | |
---|
[90f061f6] | 3399 | strcpy(descrinotificacion, " "); |
---|
[34fc87f] | 3400 | |
---|
[90f061f6] | 3401 | if (strcmp(resultado, ACCION_CONERRORES) == 0) { |
---|
| 3402 | strcpy(descrinotificacion, |
---|
[9fd5e97] | 3403 | "Ha ocurrido algn error en la ejecución de esta tarea."); |
---|
[90f061f6] | 3404 | strcpy(resultado, ACCION_FALLIDA); |
---|
[34fc87f] | 3405 | } |
---|
[90f061f6] | 3406 | if (strcmp(resultado, ACCION_SINERRORES) == 0) |
---|
| 3407 | strcpy(resultado, ACCION_EXITOSA); |
---|
| 3408 | |
---|
| 3409 | sprintf( |
---|
| 3410 | sqlstr, |
---|
| 3411 | "INSERT INTO notificaciones (accionid,idnotificador,fechahorareg,resultado,descrinotificacion,idaccion) VALUES (%d,%d,'%s','%s','%s',%d)", |
---|
| 3412 | accionid, idnotificador, fechahorareg, resultado, |
---|
| 3413 | descrinotificacion, idaccion); |
---|
| 3414 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 3415 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3416 | return (false); |
---|
[34fc87f] | 3417 | } |
---|
[90f061f6] | 3418 | return (true); |
---|
[34fc87f] | 3419 | } |
---|
| 3420 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3421 | // Función: comprueba_resultados |
---|
| 3422 | // |
---|
| 3423 | // |
---|
| 3424 | // Parámetros: |
---|
| 3425 | // - idaccion: Identificador en la base de datos de la acción |
---|
| 3426 | // - db: Objeto de la base de datos |
---|
| 3427 | // |
---|
| 3428 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3429 | int comprueba_resultados(int idaccion, Database db) { |
---|
[34fc87f] | 3430 | |
---|
[90f061f6] | 3431 | char ErrStr[200], sqlstr[1000]; |
---|
[34fc87f] | 3432 | int numfallidas; |
---|
| 3433 | char finalaccion[2]; |
---|
| 3434 | Table tbl; |
---|
| 3435 | |
---|
[90f061f6] | 3436 | sprintf( |
---|
| 3437 | sqlstr, |
---|
| 3438 | "SELECT COUNT(*) as numfallidas FROM notificaciones WHERE resultado='%s' AND accionid=%d", |
---|
| 3439 | ACCION_FALLIDA, idaccion); |
---|
| 3440 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 3441 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3442 | return (false); |
---|
[34fc87f] | 3443 | } |
---|
[90f061f6] | 3444 | if (tbl.ISEOF()) |
---|
| 3445 | return (false); // No existe registro de acciones |
---|
| 3446 | |
---|
| 3447 | if (!tbl.Get("numfallidas", numfallidas)) { // Toma dato |
---|
| 3448 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 3449 | return (false); |
---|
[34fc87f] | 3450 | } |
---|
| 3451 | |
---|
[90f061f6] | 3452 | if (numfallidas > 0) |
---|
| 3453 | strcpy(finalaccion, ACCION_CONERRORES); |
---|
[34fc87f] | 3454 | else |
---|
[90f061f6] | 3455 | strcpy(finalaccion, ACCION_SINERRORES); |
---|
[34fc87f] | 3456 | |
---|
[90f061f6] | 3457 | sprintf(sqlstr, "UPDATE acciones SET resultado='%s' WHERE idaccion=%d", |
---|
| 3458 | finalaccion, idaccion); |
---|
| 3459 | if (!db.Execute(sqlstr, tbl)) { // Error al actualizar |
---|
[34fc87f] | 3460 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3461 | return (false); |
---|
[34fc87f] | 3462 | } |
---|
[b0bb14f] | 3463 | // Comprueba si ha finalizado esta acción e inserta su notificador correspondiente |
---|
[90f061f6] | 3464 | return (comprueba_finalizada(idaccion, finalaccion, db)); |
---|
[34fc87f] | 3465 | } |
---|
| 3466 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3467 | // Función: comprueba_finalizada |
---|
| 3468 | // |
---|
| 3469 | // |
---|
| 3470 | // Parámetros: |
---|
| 3471 | // - idaccion: Identificar en la base de datos de la acción |
---|
| 3472 | // - resultado: Resultado de la acción |
---|
| 3473 | // - db: Objeto conxión con la base de datos |
---|
| 3474 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3475 | int comprueba_finalizada(int idaccion, char *resultado, Database db) { |
---|
[34fc87f] | 3476 | |
---|
[90f061f6] | 3477 | char ErrStr[200], sqlstr[1000]; |
---|
| 3478 | int numnotificaciones, tipoaccion, idnotificador; |
---|
| 3479 | char parametros[LONGITUD_PARAMETROS], *cadenanot; |
---|
[34fc87f] | 3480 | char fechareg[100]; |
---|
[90f061f6] | 3481 | int accionid, cont, i, resul, lon; |
---|
[34fc87f] | 3482 | Table tbl; |
---|
[90f061f6] | 3483 | struct tm* st; |
---|
[34fc87f] | 3484 | |
---|
[90f061f6] | 3485 | sprintf( |
---|
| 3486 | sqlstr, |
---|
| 3487 | "SELECT COUNT(*) as numnotificaciones FROM notificaciones WHERE accionid=%d", |
---|
| 3488 | idaccion); |
---|
| 3489 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 3490 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3491 | return (false); |
---|
[34fc87f] | 3492 | } |
---|
[90f061f6] | 3493 | if (tbl.ISEOF()) |
---|
| 3494 | return (false); // No existe registro de acciones |
---|
[34fc87f] | 3495 | |
---|
[90f061f6] | 3496 | if (!tbl.Get("numnotificaciones", numnotificaciones)) { // Toma dato |
---|
| 3497 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 3498 | return (false); |
---|
[34fc87f] | 3499 | } |
---|
| 3500 | |
---|
[90f061f6] | 3501 | sprintf( |
---|
| 3502 | sqlstr, |
---|
| 3503 | "SELECT tipoaccion,parametros,idnotificador,accionid FROM acciones WHERE idaccion=%d", |
---|
| 3504 | idaccion); |
---|
| 3505 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 3506 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3507 | return (false); |
---|
[34fc87f] | 3508 | } |
---|
[90f061f6] | 3509 | if (tbl.ISEOF()) |
---|
| 3510 | return (true); // No existe registro de acciones |
---|
| 3511 | |
---|
| 3512 | if (!tbl.Get("tipoaccion", tipoaccion)) { // Toma dato |
---|
| 3513 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 3514 | return (false); |
---|
[34fc87f] | 3515 | } |
---|
[90f061f6] | 3516 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
| 3517 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 3518 | return (false); |
---|
[34fc87f] | 3519 | } |
---|
[90f061f6] | 3520 | if (!tbl.Get("idnotificador", idnotificador)) { // Toma dato |
---|
| 3521 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 3522 | return (false); |
---|
[34fc87f] | 3523 | } |
---|
[90f061f6] | 3524 | if (!tbl.Get("accionid", accionid)) { // Toma dato |
---|
| 3525 | tbl.GetErrorErrStr(ErrStr); // error al recuperar el campo resultado |
---|
| 3526 | return (false); |
---|
[34fc87f] | 3527 | } |
---|
| 3528 | |
---|
| 3529 | INTROaFINCAD(parametros); |
---|
[90f061f6] | 3530 | switch (tipoaccion) { |
---|
| 3531 | case EJECUCION_COMANDO: |
---|
| 3532 | cadenanot = toma_parametro("iph", parametros); // Toma cadenaip |
---|
| 3533 | break; |
---|
| 3534 | case EJECUCION_TAREA: |
---|
| 3535 | cadenanot = toma_parametro("cmd", parametros); // Toma comandos |
---|
| 3536 | break; |
---|
| 3537 | case EJECUCION_TRABAJO: |
---|
| 3538 | cadenanot = toma_parametro("tsk", parametros); // Toma tareas |
---|
| 3539 | break; |
---|
| 3540 | default: |
---|
| 3541 | return (false); |
---|
| 3542 | } |
---|
| 3543 | cont = 1; |
---|
| 3544 | lon = strlen(cadenanot); |
---|
| 3545 | for (i = 0; i < lon; i++) { |
---|
| 3546 | if (cadenanot[i] == ';') |
---|
| 3547 | cont++; |
---|
| 3548 | } |
---|
| 3549 | resul = true; |
---|
| 3550 | if (numnotificaciones == cont) { |
---|
| 3551 | st = TomaHora(); |
---|
| 3552 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon |
---|
| 3553 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
| 3554 | |
---|
| 3555 | if (strcmp(resultado, ACCION_CONERRORES) == 0) |
---|
| 3556 | sprintf( |
---|
| 3557 | sqlstr, |
---|
| 3558 | "UPDATE acciones SET resultado='%s',estado='%s',fechahorafin='%s' WHERE idaccion=%d", |
---|
| 3559 | ACCION_FALLIDA, ACCION_FINALIZADA, fechareg, idaccion); |
---|
[34fc87f] | 3560 | else |
---|
[90f061f6] | 3561 | sprintf( |
---|
| 3562 | sqlstr, |
---|
| 3563 | "UPDATE acciones SET resultado='%s',estado='%s',fechahorafin='%s' WHERE idaccion=%d", |
---|
| 3564 | ACCION_EXITOSA, ACCION_FINALIZADA, fechareg, idaccion); |
---|
[34fc87f] | 3565 | |
---|
[90f061f6] | 3566 | if (!db.Execute(sqlstr)) { // Error al actualizar |
---|
[34fc87f] | 3567 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3568 | return (false); |
---|
[34fc87f] | 3569 | } |
---|
| 3570 | |
---|
[9fd5e97] | 3571 | if (accionid > 0) { // Esto no se ejecutar si la tarea tiene un trabajo padre |
---|
[90f061f6] | 3572 | resul = InsertaNotificaciones(idaccion, idnotificador, accionid, |
---|
| 3573 | resultado, db); |
---|
| 3574 | if (resul) |
---|
| 3575 | return (comprueba_resultados(accionid, db)); |
---|
[34fc87f] | 3576 | } |
---|
| 3577 | } |
---|
[90f061f6] | 3578 | return (resul); |
---|
[34fc87f] | 3579 | } |
---|
| 3580 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3581 | // Función: EnviaServidoresRembo |
---|
[34fc87f] | 3582 | // |
---|
[b0bb14f] | 3583 | // Descripción: |
---|
[9fd5e97] | 3584 | // Esta función envía una trama a un servidor rembo para que sus clientes ejecuten un comando |
---|
[b0bb14f] | 3585 | // Parámetros: |
---|
[34fc87f] | 3586 | // - parametros: parametros del comando |
---|
[9fd5e97] | 3587 | // - cont: contador de clientes |
---|
[34fc87f] | 3588 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3589 | void EnviaServidoresRembo(char * parametros, int cont) { |
---|
| 3590 | int i, lon; |
---|
| 3591 | char paux[20]; |
---|
| 3592 | |
---|
| 3593 | sprintf(paux, "ide=%u\r", TomaEnvio()); |
---|
| 3594 | strcat(parametros, paux); // identificador de envio |
---|
| 3595 | |
---|
| 3596 | sprintf(paux, "nip=%d\r", cont); |
---|
[9fd5e97] | 3597 | strcat(parametros, paux); // Contador de clientes a los que se envía la trama |
---|
[90f061f6] | 3598 | |
---|
| 3599 | for (i = 0; i < MAXIMOS_SRVRMB; i++) { |
---|
[9fd5e97] | 3600 | if (tbsocketsSRVRMB[i].swenv == 1) { // El switch de envío está a uno, hay que enviar al servidor trama ... |
---|
[90f061f6] | 3601 | strcat(parametros, "iph="); |
---|
| 3602 | strcat(parametros, tbsocketsSRVRMB[i].ipes); |
---|
| 3603 | lon = strlen(parametros); |
---|
| 3604 | parametros[lon - 1] = '\r'; // Quita la coma final |
---|
| 3605 | manda_trama_servidorrembo(tbsocketsSRVRMB[i].ip, parametros, |
---|
| 3606 | tbsocketsSRVRMB[i].puertorepo); |
---|
[34fc87f] | 3607 | } |
---|
| 3608 | } |
---|
| 3609 | } |
---|
| 3610 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3611 | // Función: manda_trama_servidorrembo |
---|
[34fc87f] | 3612 | // |
---|
[b0bb14f] | 3613 | // Descripción: |
---|
[9fd5e97] | 3614 | // Esta función envía una trama a un servidor rembo para que sus clientes ejecuten un comando |
---|
[b0bb14f] | 3615 | // Parámetros: |
---|
[9fd5e97] | 3616 | // - ip_srvrbm: Dirección IP del servidor REMBO |
---|
[34fc87f] | 3617 | // - parametros: parametros del comando |
---|
[9fd5e97] | 3618 | // - puertorepo: puerto del repositorio |
---|
[34fc87f] | 3619 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3620 | int manda_trama_servidorrembo(char* ip_srvrbm, char *parametros, int puertorepo) { |
---|
| 3621 | int ret; |
---|
| 3622 | TRAMA *trama = (TRAMA*) malloc(LONGITUD_TRAMA); |
---|
| 3623 | if (!trama) |
---|
| 3624 | return (false); |
---|
| 3625 | strcpy(trama->parametros, parametros); |
---|
[34fc87f] | 3626 | SOCKET udpsock; |
---|
[90f061f6] | 3627 | udpsock = UDPConnect(IPlocal); |
---|
| 3628 | if (udpsock == INVALID_SOCKET) |
---|
| 3629 | return (false); |
---|
| 3630 | ret = envia_comandos(udpsock, trama, ip_srvrbm, puertorepo); |
---|
[34fc87f] | 3631 | close(udpsock); |
---|
[90f061f6] | 3632 | return (ret); |
---|
[34fc87f] | 3633 | } |
---|
| 3634 | //_______________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3635 | // Función: UDPConnect |
---|
[34fc87f] | 3636 | // |
---|
[9fd5e97] | 3637 | // Descripción: |
---|
| 3638 | // Crea un socket en un puerto determinado para la conversación UDP con el repositorio |
---|
| 3639 | // Parámetros: |
---|
| 3640 | // - ips: Ip local |
---|
[34fc87f] | 3641 | //_______________________________________________________________________________________________________________ |
---|
[90f061f6] | 3642 | SOCKET UDPConnect(char *ips) { |
---|
[34fc87f] | 3643 | SOCKET socket_c; // Socket para hebras (UDP) |
---|
[90f061f6] | 3644 | struct sockaddr_in cliente; |
---|
[34fc87f] | 3645 | int puerto; |
---|
| 3646 | |
---|
| 3647 | socket_c = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Crea socket para UDP |
---|
| 3648 | |
---|
| 3649 | if (socket_c == SOCKET_ERROR) |
---|
| 3650 | return (INVALID_SOCKET); |
---|
| 3651 | |
---|
| 3652 | cliente.sin_addr.s_addr = inet_addr(ips); // selecciona interface |
---|
| 3653 | cliente.sin_family = AF_INET; |
---|
[90f061f6] | 3654 | puerto = PUERTOMINUSER; |
---|
| 3655 | while (puerto < PUERTOMAXUSER) { // Busca puerto libre |
---|
[34fc87f] | 3656 | cliente.sin_port = htons(puerto); // Puerto asignado |
---|
[90f061f6] | 3657 | if (bind(socket_c, (struct sockaddr *) &cliente, sizeof(cliente)) |
---|
| 3658 | == SOCKET_ERROR) |
---|
[34fc87f] | 3659 | puerto++; |
---|
| 3660 | else |
---|
| 3661 | break; |
---|
| 3662 | } |
---|
[90f061f6] | 3663 | if (puerto >= PUERTOMAXUSER) { // No hay puertos libres |
---|
| 3664 | return (INVALID_SOCKET); |
---|
[34fc87f] | 3665 | } |
---|
[90f061f6] | 3666 | return (socket_c); |
---|
[34fc87f] | 3667 | } |
---|
| 3668 | //________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3669 | // Función: envia_comandos |
---|
[34fc87f] | 3670 | // |
---|
[b0bb14f] | 3671 | // Descripción: |
---|
[34fc87f] | 3672 | // Enva trama UDP |
---|
[9fd5e97] | 3673 | // Parámetros: |
---|
| 3674 | // - s: socket |
---|
| 3675 | // - trama: El contenido de la trama |
---|
| 3676 | // - ipsrv: Ip del repositorio |
---|
| 3677 | // - puerto: puerto de conexión |
---|
[34fc87f] | 3678 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3679 | int envia_comandos(SOCKET s, TRAMA* trama, char* ipsrv, int puerto) { |
---|
| 3680 | int ret, lon; |
---|
[34fc87f] | 3681 | struct sockaddr_in addrRepo; |
---|
[90f061f6] | 3682 | |
---|
| 3683 | trama->arroba = '@'; // cabecera de la trama |
---|
| 3684 | strcpy(trama->identificador, "JMMLCAMDJ"); // identificador de la trama |
---|
| 3685 | trama->ejecutor = '1'; // ejecutor de la trama 1=el servidor hidra 2=el cliente hidra |
---|
| 3686 | |
---|
[34fc87f] | 3687 | addrRepo.sin_family = AF_INET; |
---|
[90f061f6] | 3688 | addrRepo.sin_port = htons((short) puerto); |
---|
[9fd5e97] | 3689 | addrRepo.sin_addr.s_addr = inet_addr(ipsrv); // Dirección IP repositorio |
---|
[90f061f6] | 3690 | Encriptar((char*) trama); |
---|
| 3691 | lon = strlen((char*) trama); |
---|
| 3692 | ret = sendto(s, (char *) trama, lon, 0, (struct sockaddr *) &addrRepo, |
---|
| 3693 | sizeof(addrRepo)); |
---|
| 3694 | if (ret == SOCKET_ERROR) { |
---|
| 3695 | RegistraLog("***send() fallo en envío al repositorio", true); |
---|
| 3696 | return (FALSE); |
---|
| 3697 | } |
---|
[34fc87f] | 3698 | return true; |
---|
| 3699 | } |
---|
| 3700 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3701 | // Función: DesmarcaServidoresRembo |
---|
[34fc87f] | 3702 | // |
---|
[b0bb14f] | 3703 | // Descripción: |
---|
[9fd5e97] | 3704 | // Esta función desmarca la tabla completa de servidores rembo para iniciar la cuestion de envío |
---|
| 3705 | // Parámetros: |
---|
| 3706 | // Ninguno |
---|
[34fc87f] | 3707 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3708 | void DesmarcaServidoresRembo(void) { |
---|
[34fc87f] | 3709 | int i; |
---|
[90f061f6] | 3710 | for (i = 0; i < MAXIMOS_SRVRMB; i++) { |
---|
| 3711 | tbsocketsSRVRMB[i].swenv = 0; |
---|
| 3712 | tbsocketsSRVRMB[i].ipes[0] = (char) NULL; |
---|
[34fc87f] | 3713 | } |
---|
| 3714 | } |
---|
| 3715 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3716 | // Función: MarcaServidoresRembo |
---|
[34fc87f] | 3717 | // |
---|
[b0bb14f] | 3718 | // Descripción: |
---|
[9fd5e97] | 3719 | // Esta función marca la tabla de servidores Rembo y coloca la ip del cliente en el buffer |
---|
[b0bb14f] | 3720 | // Parámetros: |
---|
[34fc87f] | 3721 | // - ipsrvrmb: ip del servidor rembo |
---|
| 3722 | // - ipclrmb: ip del cliente rembo |
---|
| 3723 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3724 | void MarcaServidoresRembo(char* ipsrvrmb, char*ipclrmb) { |
---|
| 3725 | int i, resul; |
---|
| 3726 | for (i = 0; i < MAXIMOS_SRVRMB; i++) { |
---|
| 3727 | resul = strcmp(tbsocketsSRVRMB[i].ip, ipsrvrmb); |
---|
| 3728 | if (resul == 0) {// servidor rembo encontrado |
---|
| 3729 | strcat(tbsocketsSRVRMB[i].ipes, ipclrmb); |
---|
| 3730 | strcat(tbsocketsSRVRMB[i].ipes, ";"); |
---|
| 3731 | tbsocketsSRVRMB[i].swenv = 1; |
---|
[34fc87f] | 3732 | return; |
---|
| 3733 | } |
---|
| 3734 | } |
---|
| 3735 | } |
---|
| 3736 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3737 | // Función: TomaIPServidorRembo |
---|
[34fc87f] | 3738 | // |
---|
[b0bb14f] | 3739 | // Descripción: |
---|
[9fd5e97] | 3740 | // Esta función devuelve true o false dependiendo si el Servidor REMBO está en la tabla de servidores. |
---|
[b0bb14f] | 3741 | // Parámetros: |
---|
[34fc87f] | 3742 | // - ip : La ip del servidor a buscar |
---|
[9fd5e97] | 3743 | // - p: parámetro de salida. Si encuentra la ip trae el puerto en la variable |
---|
[34fc87f] | 3744 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3745 | BOOLEAN TomaIPServidorRembo(char *ip, int *p) { |
---|
| 3746 | int i, j; |
---|
| 3747 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 3748 | if (strcmp(ip, tbsockets[i].ip) == 0) { // Si existe la IP ... |
---|
| 3749 | strcpy(ip, tbsockets[i].ipsrvrmb); |
---|
| 3750 | for (j = 0; j < MAXIMOS_SRVRMB; j++) { |
---|
| 3751 | if (strcmp(ip, tbsocketsSRVRMB[j].ip) == 0) { // Si existe la IP ... |
---|
| 3752 | *p = tbsocketsSRVRMB[j].puertorepo; |
---|
| 3753 | return (TRUE); |
---|
[34fc87f] | 3754 | } |
---|
| 3755 | } |
---|
| 3756 | } |
---|
[90f061f6] | 3757 | } |
---|
| 3758 | return (FALSE); |
---|
[34fc87f] | 3759 | } |
---|
| 3760 | |
---|
| 3761 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3762 | // Función: EjecutarTarea |
---|
[34fc87f] | 3763 | // |
---|
[b0bb14f] | 3764 | // Descripción: |
---|
[9fd5e97] | 3765 | // Registra una acción (Tarea) y la envía para su ejecución |
---|
[b0bb14f] | 3766 | // Parámetros: |
---|
[34fc87f] | 3767 | // - idtarea : Identificador de la tarea |
---|
| 3768 | // - accionid: identificador del trabajo padre (si existe) |
---|
[9fd5e97] | 3769 | // - idnotificador: identificador del trabajo_tarea incluido en el trabajo padre (si existe) |
---|
[34fc87f] | 3770 | // - idcentro: Centro propietario del trabjo padre (si existe este trabajo) |
---|
[9fd5e97] | 3771 | // - db: Objeto de la base de datos |
---|
| 3772 | // - parametros: parámetros de la acción |
---|
[34fc87f] | 3773 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3774 | int EjecutarTarea(int idtarea, int accionid, int idnotificador, int idcentro, |
---|
| 3775 | Database db, char* parametros) { |
---|
| 3776 | char sqlstr[1000], ErrStr[200], ambito; |
---|
[34fc87f] | 3777 | Table tbl; |
---|
[90f061f6] | 3778 | int cont_comandos = 0, lon; |
---|
| 3779 | int idcomando, idambito, idtareacomando, accionidcmd; |
---|
| 3780 | char wambitarea[20], ambitarea[4000]; |
---|
| 3781 | char wparamtarea[20], paramtarea[1000], pids[20]; |
---|
| 3782 | int tblon[100], tbComandosidcomando[100], tbComandosambito[100], |
---|
| 3783 | tbComandosidnotificador[100], tbComandosidambito[100]; |
---|
[34fc87f] | 3784 | char *tbComandosparametros[100]; |
---|
| 3785 | |
---|
[9fd5e97] | 3786 | ambitarea[0] = (char) NULL; // Inicialización |
---|
| 3787 | strcpy(paramtarea, "cmd="); // Inicialización |
---|
[90f061f6] | 3788 | if (idcentro == 0) { |
---|
[34fc87f] | 3789 | // recupera el identificador del Centro propietario de la tarea |
---|
[90f061f6] | 3790 | sprintf(sqlstr, "SELECT idcentro FROM tareas WHERE idtarea=%d", idtarea); |
---|
| 3791 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3792 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3793 | return (false); |
---|
[34fc87f] | 3794 | } |
---|
[90f061f6] | 3795 | if (tbl.ISEOF()) |
---|
| 3796 | return (true); |
---|
| 3797 | if (!tbl.Get("idcentro", idcentro)) { // Toma dato |
---|
[34fc87f] | 3798 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3799 | return (false); |
---|
[34fc87f] | 3800 | } |
---|
| 3801 | } |
---|
| 3802 | // Recupera los comandos que forman parte de la tarea |
---|
[90f061f6] | 3803 | sprintf(sqlstr, |
---|
| 3804 | "SELECT * FROM tareas_comandos WHERE idtarea=%d ORDER by orden", |
---|
| 3805 | idtarea); |
---|
| 3806 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3807 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3808 | return (false); |
---|
[34fc87f] | 3809 | } |
---|
[90f061f6] | 3810 | if (tbl.ISEOF()) |
---|
| 3811 | return (true); |
---|
| 3812 | |
---|
[34fc87f] | 3813 | // Recorre tareas-comandos |
---|
[90f061f6] | 3814 | while (!tbl.ISEOF()) { |
---|
| 3815 | if (!tbl.Get("idcomando", idcomando)) { // Toma dato |
---|
[34fc87f] | 3816 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3817 | return (false); |
---|
[34fc87f] | 3818 | } |
---|
[90f061f6] | 3819 | tbComandosidcomando[cont_comandos] = idcomando; |
---|
[34fc87f] | 3820 | |
---|
[90f061f6] | 3821 | if (!tbl.Get("ambito", ambito)) { // Toma dato |
---|
[34fc87f] | 3822 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3823 | return (false); |
---|
[34fc87f] | 3824 | } |
---|
[90f061f6] | 3825 | tbComandosambito[cont_comandos] = ambito; |
---|
[34fc87f] | 3826 | |
---|
[90f061f6] | 3827 | if (!tbl.Get("idambito", idambito)) { // Toma dato |
---|
[34fc87f] | 3828 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3829 | return (false); |
---|
[34fc87f] | 3830 | } |
---|
[90f061f6] | 3831 | tbComandosidambito[cont_comandos] = idambito; |
---|
[34fc87f] | 3832 | |
---|
[90f061f6] | 3833 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
[34fc87f] | 3834 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3835 | return (false); |
---|
[34fc87f] | 3836 | } |
---|
| 3837 | |
---|
[90f061f6] | 3838 | lon = strlen(parametros); |
---|
| 3839 | tblon[cont_comandos] = lon; |
---|
| 3840 | tbComandosparametros[cont_comandos] = (char*) malloc(lon + 20); |
---|
| 3841 | if (tbComandosparametros[cont_comandos] == NULL) |
---|
[9fd5e97] | 3842 | return (false); // No hay memoria suficiente |
---|
[34fc87f] | 3843 | |
---|
[90f061f6] | 3844 | strcpy(tbComandosparametros[cont_comandos], parametros); |
---|
| 3845 | |
---|
| 3846 | if (!tbl.Get("idtareacomando", idtareacomando)) { // Toma dato |
---|
[34fc87f] | 3847 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3848 | return (false); |
---|
[34fc87f] | 3849 | } |
---|
[90f061f6] | 3850 | tbComandosidnotificador[cont_comandos] = idtareacomando; |
---|
[34fc87f] | 3851 | |
---|
[90f061f6] | 3852 | sprintf(wambitarea, "%d:%d;", ambito, idambito); |
---|
| 3853 | strcat(ambitarea, wambitarea); |
---|
[34fc87f] | 3854 | |
---|
[90f061f6] | 3855 | sprintf(wparamtarea, "%d;", idtareacomando); |
---|
| 3856 | strcat(paramtarea, wparamtarea); |
---|
[34fc87f] | 3857 | |
---|
| 3858 | cont_comandos++; |
---|
| 3859 | tbl.MoveNext(); |
---|
| 3860 | } |
---|
[90f061f6] | 3861 | lon = strlen(ambitarea); |
---|
| 3862 | ambitarea[lon - 1] = (char) NULL; // Quita la coma final |
---|
[34fc87f] | 3863 | |
---|
[90f061f6] | 3864 | lon = strlen(paramtarea); |
---|
| 3865 | paramtarea[lon - 1] = (char) NULL; // Quita la coma final |
---|
[34fc87f] | 3866 | |
---|
| 3867 | char _fechahorareg[100]; |
---|
[90f061f6] | 3868 | struct tm* st; |
---|
| 3869 | st = TomaHora(); |
---|
| 3870 | sprintf(_fechahorareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon |
---|
| 3871 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
| 3872 | |
---|
| 3873 | sprintf( |
---|
| 3874 | sqlstr, |
---|
| 3875 | "INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,ambitskwrk,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,0,0,'%s','%s','%s','%s',%d,'%s',%d,%d)", |
---|
| 3876 | EJECUCION_TAREA, idtarea, PROCESOS, ambitarea, _fechahorareg, |
---|
| 3877 | ACCION_INICIADA, ACCION_SINERRORES, idcentro, paramtarea, accionid, |
---|
| 3878 | idnotificador); |
---|
| 3879 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 3880 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3881 | return (false); |
---|
[34fc87f] | 3882 | } |
---|
[90f061f6] | 3883 | accionid = 0; |
---|
[b0bb14f] | 3884 | // Toma identificador dela acción |
---|
[90f061f6] | 3885 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 3886 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3887 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3888 | return (false); |
---|
[34fc87f] | 3889 | } |
---|
[90f061f6] | 3890 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 3891 | if (!tbl.Get("identificador", accionid)) { |
---|
[34fc87f] | 3892 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3893 | return (false); |
---|
[34fc87f] | 3894 | } |
---|
| 3895 | } |
---|
| 3896 | int i; |
---|
| 3897 | // Insertar acciones:comandos |
---|
[90f061f6] | 3898 | for (i = 0; i < cont_comandos; i++) { |
---|
| 3899 | st = TomaHora(); |
---|
| 3900 | sprintf(_fechahorareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, |
---|
| 3901 | st->tm_mon + 1, st->tm_mday, st->tm_hour, st->tm_min, |
---|
| 3902 | st->tm_sec); |
---|
| 3903 | sprintf( |
---|
| 3904 | sqlstr, |
---|
| 3905 | "INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,%d,%d,'%s','%s','%s',%d,'%s',%d,%d)", |
---|
| 3906 | EJECUCION_COMANDO, tbComandosidcomando[i], PROCESOS, |
---|
| 3907 | tbComandosambito[i], tbComandosidambito[i], _fechahorareg, |
---|
| 3908 | ACCION_EXITOSA, ACCION_SINERRORES, idcentro, |
---|
| 3909 | tbComandosparametros[i], accionid, tbComandosidnotificador[i]); |
---|
| 3910 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 3911 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3912 | free(tbComandosparametros[i]); |
---|
| 3913 | return (false); |
---|
[34fc87f] | 3914 | } |
---|
[b0bb14f] | 3915 | // Toma identificador dela acción |
---|
[90f061f6] | 3916 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 3917 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3918 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3919 | return (false); |
---|
[34fc87f] | 3920 | } |
---|
[90f061f6] | 3921 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 3922 | if (!tbl.Get("identificador", accionidcmd)) { |
---|
| 3923 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 3924 | return (false); |
---|
[34fc87f] | 3925 | } |
---|
| 3926 | } |
---|
[90f061f6] | 3927 | sprintf(pids, "ids=%d\r", accionidcmd); |
---|
[9fd5e97] | 3928 | strcat((char*) tbComandosparametros[i], pids); // Le ande el identificador de la acción |
---|
[34fc87f] | 3929 | envia_tarea(tbComandosparametros[i]); |
---|
| 3930 | free(tbComandosparametros[i]); |
---|
| 3931 | } |
---|
[90f061f6] | 3932 | return (true); |
---|
[34fc87f] | 3933 | } |
---|
| 3934 | // ________________________________________________________________________________________________________ |
---|
[9fd5e97] | 3935 | // Función: envia_tarea |
---|
[34fc87f] | 3936 | // |
---|
[b0bb14f] | 3937 | // Descripción: |
---|
[9fd5e97] | 3938 | // Esta función envía una tarea por la red. |
---|
[b0bb14f] | 3939 | // Parámetros: |
---|
[34fc87f] | 3940 | // - parametros: El contenido de la tarea |
---|
| 3941 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3942 | void envia_tarea(char* parametros) { |
---|
| 3943 | TRAMA trama; |
---|
[34fc87f] | 3944 | |
---|
[90f061f6] | 3945 | trama.arroba = '@'; |
---|
| 3946 | strncpy(trama.identificador, "JMMLCAMDJ", 9); |
---|
| 3947 | trama.ejecutor = parametros[0]; |
---|
| 3948 | strcpy(trama.parametros, (char*) ¶metros[1]); |
---|
| 3949 | gestiona_comando(INVALID_SOCKET, trama); |
---|
[34fc87f] | 3950 | } |
---|
| 3951 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 3952 | // Función: EjecutarTrabajo |
---|
[34fc87f] | 3953 | // |
---|
[b0bb14f] | 3954 | // Descripción: |
---|
[9fd5e97] | 3955 | // Registra una acción (Trabajo) y la envía para su ejecución |
---|
[b0bb14f] | 3956 | // Parámetros: |
---|
[34fc87f] | 3957 | // - idtrabajo : Identificador del trabajo |
---|
[9fd5e97] | 3958 | // - db: Objeto de la base de datos |
---|
| 3959 | // - parametros: parámetros de la acción |
---|
[34fc87f] | 3960 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 3961 | int EjecutarTrabajo(int idtrabajo, Database db, char*parametros) { |
---|
| 3962 | char sqlstr[1000], ErrStr[200]; |
---|
[34fc87f] | 3963 | Table tbl; |
---|
[90f061f6] | 3964 | int cont_tareas = 0, lon; |
---|
| 3965 | int idtarea, idtrabajotarea, idcentro; |
---|
| 3966 | char wambitrabajo[500], ambitrabajo[4000]; |
---|
| 3967 | char wparamtrabajo[20], paramtrabajo[1000]; |
---|
| 3968 | int tbTareasidtarea[100], tbTareasidnotificador[100]; |
---|
[34fc87f] | 3969 | char ambitskwrk[500]; |
---|
| 3970 | |
---|
[9fd5e97] | 3971 | ambitrabajo[0] = (char) NULL; // Inicialización |
---|
| 3972 | strcpy(paramtrabajo, "tsk="); // Inicialización |
---|
[34fc87f] | 3973 | |
---|
| 3974 | // recupera el identificador del Centro propietario de la tarea |
---|
[90f061f6] | 3975 | sprintf(sqlstr, "SELECT idcentro FROM trabajos WHERE idtrabajo=%d", |
---|
| 3976 | idtrabajo); |
---|
| 3977 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3978 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3979 | return (false); |
---|
[34fc87f] | 3980 | } |
---|
[90f061f6] | 3981 | if (tbl.ISEOF()) |
---|
| 3982 | return (true); |
---|
| 3983 | if (!tbl.Get("idcentro", idcentro)) { // Toma dato |
---|
[34fc87f] | 3984 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 3985 | return (false); |
---|
[34fc87f] | 3986 | } |
---|
| 3987 | // Recupera las tareas que forman parte del trabajo |
---|
[90f061f6] | 3988 | sprintf(sqlstr, |
---|
| 3989 | "SELECT * FROM trabajos_tareas WHERE idtrabajo=%d ORDER by orden", |
---|
| 3990 | idtrabajo); |
---|
| 3991 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 3992 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 3993 | return (false); |
---|
[34fc87f] | 3994 | } |
---|
[90f061f6] | 3995 | if (tbl.ISEOF()) |
---|
| 3996 | return (true); |
---|
[34fc87f] | 3997 | // Recorre trabajos-tareas |
---|
[90f061f6] | 3998 | while (!tbl.ISEOF()) { |
---|
| 3999 | if (!tbl.Get("idtrabajotarea", idtrabajotarea)) { // Toma dato |
---|
[34fc87f] | 4000 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4001 | return (false); |
---|
[34fc87f] | 4002 | } |
---|
[90f061f6] | 4003 | tbTareasidnotificador[cont_tareas] = idtrabajotarea; |
---|
[34fc87f] | 4004 | |
---|
[90f061f6] | 4005 | if (!tbl.Get("idtarea", idtarea)) { // Toma dato |
---|
[34fc87f] | 4006 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4007 | return (false); |
---|
[34fc87f] | 4008 | } |
---|
[90f061f6] | 4009 | tbTareasidtarea[cont_tareas] = idtarea; |
---|
[34fc87f] | 4010 | |
---|
[90f061f6] | 4011 | if (!tbl.Get("parametros", parametros)) { // Toma dato |
---|
[34fc87f] | 4012 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4013 | return (false); |
---|
[34fc87f] | 4014 | } |
---|
[90f061f6] | 4015 | |
---|
| 4016 | if (!tbl.Get("ambitskwrk", ambitskwrk)) { // Toma dato |
---|
[34fc87f] | 4017 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4018 | return (false); |
---|
[34fc87f] | 4019 | } |
---|
[90f061f6] | 4020 | sprintf(wambitrabajo, "%s;", ambitskwrk); |
---|
| 4021 | strcat(ambitrabajo, wambitrabajo); |
---|
[34fc87f] | 4022 | |
---|
[90f061f6] | 4023 | sprintf(wparamtrabajo, "%d;", idtrabajotarea); |
---|
| 4024 | strcat(paramtrabajo, wparamtrabajo); |
---|
[34fc87f] | 4025 | |
---|
| 4026 | cont_tareas++; |
---|
| 4027 | tbl.MoveNext(); |
---|
| 4028 | } |
---|
[90f061f6] | 4029 | lon = strlen(ambitrabajo); |
---|
| 4030 | ambitrabajo[lon - 1] = (char) NULL; // Quita la coma final |
---|
[34fc87f] | 4031 | |
---|
[90f061f6] | 4032 | lon = strlen(paramtrabajo); |
---|
| 4033 | paramtrabajo[lon - 1] = (char) NULL; // Quita la coma final |
---|
[34fc87f] | 4034 | |
---|
| 4035 | char _fechahorareg[100]; |
---|
[90f061f6] | 4036 | struct tm* st; |
---|
| 4037 | st = TomaHora(); |
---|
| 4038 | sprintf(_fechahorareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon |
---|
| 4039 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec); |
---|
| 4040 | |
---|
| 4041 | sprintf( |
---|
| 4042 | sqlstr, |
---|
| 4043 | "INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,ambitskwrk,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,0,0,'%s','%s','%s','%s',%d,'%s',0,0)", |
---|
| 4044 | EJECUCION_TRABAJO, idtrabajo, PROCESOS, ambitrabajo, _fechahorareg, |
---|
| 4045 | ACCION_INICIADA, ACCION_SINERRORES, idcentro, paramtrabajo); |
---|
| 4046 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 4047 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4048 | return (false); |
---|
[34fc87f] | 4049 | } |
---|
[90f061f6] | 4050 | int accionid = 0; |
---|
[b0bb14f] | 4051 | // Toma identificador dela acción |
---|
[90f061f6] | 4052 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 4053 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 4054 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4055 | return (false); |
---|
[34fc87f] | 4056 | } |
---|
[90f061f6] | 4057 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 4058 | if (!tbl.Get("identificador", accionid)) { |
---|
[34fc87f] | 4059 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4060 | return (false); |
---|
[34fc87f] | 4061 | } |
---|
| 4062 | } |
---|
| 4063 | int i; |
---|
| 4064 | // Insertar acciones:tareas |
---|
[90f061f6] | 4065 | for (i = 0; i < cont_tareas; i++) { |
---|
| 4066 | if (!EjecutarTarea(tbTareasidtarea[i], accionid, |
---|
| 4067 | tbTareasidnotificador[i], idcentro, db, parametros)) { |
---|
| 4068 | return (false); |
---|
[34fc87f] | 4069 | } |
---|
| 4070 | } |
---|
[90f061f6] | 4071 | return (true); |
---|
[34fc87f] | 4072 | } |
---|
| 4073 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 4074 | // Función: cuestion_nuevoordenador |
---|
[34fc87f] | 4075 | // |
---|
[9fd5e97] | 4076 | // Descripción: |
---|
| 4077 | // Esta función da de alta un ordenador y un aula si el sistema está configurado para ello |
---|
[b0bb14f] | 4078 | // Parámetros: |
---|
[34fc87f] | 4079 | // - db: Objeto base de datos (ya operativo) |
---|
| 4080 | // - tbl: Objeto tabla |
---|
[9fd5e97] | 4081 | // - ido: identificador del ordenador que se dará de alta automáticamente (se devuelve) |
---|
[34fc87f] | 4082 | // - nau: Nombre del grupo donde estnel ordenador( rembo.conf) |
---|
| 4083 | // - nor: Nombre del ordenador dado por rembo(dhcpd) |
---|
| 4084 | // - iph: IP del ordenador |
---|
| 4085 | // - mac: MAC del ordenador |
---|
[9fd5e97] | 4086 | // - cfg: configuración |
---|
[34fc87f] | 4087 | // - ipd: ip del servidor dhcp |
---|
| 4088 | // - ipr: ip del servidor rembo |
---|
| 4089 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 4090 | int cuestion_nuevoordenador(Database db, Table tbl, int*ido, char *nau, |
---|
| 4091 | char *nor, char *iph, char *mac, char*cfg, char*ipd, char*ipr) { |
---|
| 4092 | char sqlstr[1000], ErrStr[200]; |
---|
| 4093 | int ida, isd, isr; |
---|
[34fc87f] | 4094 | |
---|
| 4095 | // Recupera los datos del aula |
---|
[90f061f6] | 4096 | sprintf(sqlstr, "SELECT idaula FROM aulas WHERE nombreaula= '%s'", nau); |
---|
[34fc87f] | 4097 | |
---|
[90f061f6] | 4098 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[34fc87f] | 4099 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4100 | return (false); |
---|
[34fc87f] | 4101 | } |
---|
[90f061f6] | 4102 | if (tbl.ISEOF()) { // Si NO existe el aula |
---|
| 4103 | sprintf(sqlstr, "SELECT idaula FROM aulas WHERE nombreaula= '%s'", |
---|
| 4104 | "Default"); |
---|
| 4105 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
[0d997c6] | 4106 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4107 | return (false); |
---|
[0d997c6] | 4108 | } |
---|
[90f061f6] | 4109 | if (tbl.ISEOF()) { // Inserta el aula por defecto |
---|
| 4110 | sprintf(sqlstr, "INSERT INTO aulas (nombreaula) VALUES ('Default')"); |
---|
| 4111 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[0d997c6] | 4112 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4113 | return (false); |
---|
[0d997c6] | 4114 | } |
---|
[90f061f6] | 4115 | ida = 0; |
---|
| 4116 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 4117 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[0d997c6] | 4118 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4119 | return (false); |
---|
[0d997c6] | 4120 | } |
---|
[90f061f6] | 4121 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 4122 | if (!tbl.Get("identificador", ida)) { |
---|
[0d997c6] | 4123 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4124 | return (false); |
---|
[0d997c6] | 4125 | } |
---|
[90f061f6] | 4126 | } |
---|
[0d997c6] | 4127 | } |
---|
[90f061f6] | 4128 | } else { |
---|
| 4129 | if (!tbl.Get("idaula", ida)) { // Toma dato |
---|
[34fc87f] | 4130 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4131 | return (false); |
---|
[34fc87f] | 4132 | } |
---|
| 4133 | } |
---|
[90f061f6] | 4134 | if (!Toma_idservidorres(db, tbl, ipd, ipr, &isd, &isr)) |
---|
| 4135 | return (false); |
---|
| 4136 | if (!alta_ordenador(db, tbl, ido, nor, iph, mac, ida, isd, isr)) |
---|
| 4137 | return (false); // Alta del ordenador |
---|
[9fd5e97] | 4138 | if (!actualiza_configuracion(db, tbl, cfg, 0, 0, iph)) { // Actualiza la configuración del ordenador |
---|
[90f061f6] | 4139 | return (false); |
---|
[34fc87f] | 4140 | } |
---|
[90f061f6] | 4141 | return (true); |
---|
[34fc87f] | 4142 | } |
---|
| 4143 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 4144 | // Función: alta_ordenador |
---|
[34fc87f] | 4145 | // |
---|
[b0bb14f] | 4146 | // Descripción: |
---|
[277d0cd] | 4147 | // Esta funcin da de alta un ordenador |
---|
[b0bb14f] | 4148 | // Parámetros: |
---|
[34fc87f] | 4149 | // - db: Objeto base de datos (ya operativo) |
---|
| 4150 | // - tbl: Objeto tabla |
---|
| 4151 | // - mac: MAC del ordenador |
---|
| 4152 | // - ida: Identificador del aula |
---|
| 4153 | // - isd: Identificador del servidor dhcp |
---|
| 4154 | // - isr: Identificador del servidor rembo |
---|
| 4155 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 4156 | int alta_ordenador(Database db, Table tbl, int* ido, char *nor, char *iph, |
---|
| 4157 | char*mac, int ida, int isd, int isr) { |
---|
| 4158 | char sqlstr[1000], ErrStr[200], strmac[20]; |
---|
| 4159 | int idordenador, lon, i, p; |
---|
[34fc87f] | 4160 | |
---|
| 4161 | // Prepara mac |
---|
[90f061f6] | 4162 | lon = strlen(mac); |
---|
| 4163 | p = 0; |
---|
| 4164 | for (i = 0; i < lon; i++) { |
---|
| 4165 | if (mac[i] != ' ') // Si no es espacio |
---|
| 4166 | strmac[p++] = mac[i]; |
---|
| 4167 | } |
---|
| 4168 | strmac[p] = (char) NULL; |
---|
| 4169 | |
---|
| 4170 | sprintf( |
---|
| 4171 | sqlstr, |
---|
| 4172 | "INSERT INTO ordenadores(nombreordenador,ip,mac,idperfilhard,idservidordhcp,idservidorrembo,idmenu,idaula,grupoid,idconfiguracion) VALUES ('%s','%s','%s',0,%d,%d,0,%d,0,0)", |
---|
| 4173 | nor, iph, strmac, isd, isr, ida); |
---|
| 4174 | if (!db.Execute(sqlstr)) { // Error al insertar |
---|
[34fc87f] | 4175 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4176 | return (false); |
---|
[34fc87f] | 4177 | } |
---|
[90f061f6] | 4178 | idordenador = 0; |
---|
[b0bb14f] | 4179 | // Toma identificador dela acción |
---|
[90f061f6] | 4180 | sprintf(sqlstr, "SELECT LAST_INSERT_ID() as identificador"); |
---|
| 4181 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 4182 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4183 | return (false); |
---|
[34fc87f] | 4184 | } |
---|
[90f061f6] | 4185 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 4186 | if (!tbl.Get("identificador", idordenador)) { |
---|
[34fc87f] | 4187 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4188 | return (false); |
---|
[34fc87f] | 4189 | } |
---|
| 4190 | } |
---|
[90f061f6] | 4191 | *ido = idordenador; |
---|
| 4192 | return (true); |
---|
[34fc87f] | 4193 | } |
---|
| 4194 | // ________________________________________________________________________________________________________ |
---|
[b0bb14f] | 4195 | // Función: Toma_idservidorres |
---|
[34fc87f] | 4196 | // |
---|
[b0bb14f] | 4197 | // Descripción: |
---|
[277d0cd] | 4198 | // Esta funcin devuelve los identificadores de los servidores rembo y dhcp de un determinado ordenador |
---|
[b0bb14f] | 4199 | // Parámetros: |
---|
[34fc87f] | 4200 | // db: Objeto base de datos (ya operativo) |
---|
| 4201 | // tbl: Objeto tabla |
---|
| 4202 | // ipd: ip del servidor dhcp |
---|
| 4203 | // ipr: ip del servidor rembo |
---|
| 4204 | // isd: identificador del servidor dhcp |
---|
| 4205 | // isr: identificador del servidor rembo |
---|
| 4206 | // ________________________________________________________________________________________________________ |
---|
[90f061f6] | 4207 | int Toma_idservidorres(Database db, Table tbl, char*ipd, char*ipr, int*isd, |
---|
| 4208 | int*isr) { |
---|
| 4209 | char sqlstr[1000], ErrStr[200]; |
---|
| 4210 | int identificador_dhcp = 0; |
---|
| 4211 | int identificador_rembo, puertorepo_rembo; |
---|
| 4212 | |
---|
[51dcdc7] | 4213 | /* Servidor dhcp |
---|
[90f061f6] | 4214 | sprintf(sqlstr,"SELECT idservidordhcp FROM servidoresdhcp where ip='%s'",ipd); |
---|
| 4215 | if(!db.Execute(sqlstr,tbl)){ // Error al leer |
---|
| 4216 | db.GetErrorErrStr(ErrStr); |
---|
| 4217 | return(false); |
---|
| 4218 | } |
---|
| 4219 | if(!tbl.ISEOF()){ // Si existe registro |
---|
| 4220 | if(!tbl.Get("idservidordhcp",identificador_dhcp)){ |
---|
| 4221 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 4222 | return(false); |
---|
| 4223 | } |
---|
| 4224 | } |
---|
| 4225 | */ |
---|
| 4226 | // Servidor rembo |
---|
| 4227 | sprintf( |
---|
| 4228 | sqlstr, |
---|
| 4229 | "SELECT idservidorrembo,puertorepo FROM servidoresrembo where ip='%s'", |
---|
| 4230 | ipr); |
---|
| 4231 | if (!db.Execute(sqlstr, tbl)) { // Error al leer |
---|
[34fc87f] | 4232 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4233 | return (false); |
---|
[34fc87f] | 4234 | } |
---|
[90f061f6] | 4235 | if (!tbl.ISEOF()) { // Si existe registro |
---|
| 4236 | if (!tbl.Get("idservidorrembo", identificador_rembo)) { |
---|
[34fc87f] | 4237 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4238 | return (false); |
---|
| 4239 | } |
---|
| 4240 | if (!tbl.Get("puertorepo", puertorepo_rembo)) { |
---|
| 4241 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 4242 | return (false); |
---|
[34fc87f] | 4243 | } |
---|
| 4244 | } |
---|
[90f061f6] | 4245 | *isd = identificador_dhcp; |
---|
| 4246 | *isr = identificador_rembo; |
---|
| 4247 | |
---|
| 4248 | return (true); |
---|
| 4249 | } |
---|
| 4250 | // ________________________________________________________________________________________________________ |
---|
| 4251 | // Función: tomaIpRepoPort |
---|
| 4252 | // |
---|
| 4253 | // Descripción: |
---|
[9fd5e97] | 4254 | // Devuelve la ip y el puerto de un repositorio |
---|
[90f061f6] | 4255 | // Parámetros: |
---|
| 4256 | // iph: ip del cliente |
---|
| 4257 | // ipr: ip del servidor rembo |
---|
| 4258 | // rep: puerto del repositorio |
---|
| 4259 | // ________________________________________________________________________________________________________ |
---|
| 4260 | int tomaIpRepoPort(char *iph, char *ipr, char *rep) { |
---|
| 4261 | char ErrStr[200], sqlstr[1000]; |
---|
| 4262 | Database db; |
---|
| 4263 | Table tbl; |
---|
| 4264 | char iprepositorio[16]; |
---|
| 4265 | int puertorepo; |
---|
| 4266 | |
---|
| 4267 | // Toma las propiedades del ordenador |
---|
[9fd5e97] | 4268 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexión |
---|
[90f061f6] | 4269 | RegistraLog("Error de conexión con la base de datos", false); |
---|
| 4270 | db.GetErrorErrStr(ErrStr); |
---|
| 4271 | return (false); |
---|
| 4272 | } |
---|
| 4273 | // Recupera los datos del ordenador |
---|
| 4274 | sprintf( |
---|
| 4275 | sqlstr, |
---|
| 4276 | "SELECT servidoresrembo.ip,servidoresrembo.puertorepo" |
---|
| 4277 | " FROM ordenadores " |
---|
| 4278 | " INNER JOIN servidoresrembo ON ordenadores.idservidorrembo = servidoresrembo.idservidorrembo" |
---|
| 4279 | " WHERE ordenadores.ip = '%s'", iph); |
---|
| 4280 | |
---|
| 4281 | if (!db.Execute(sqlstr, tbl)) { // Error al consultar |
---|
| 4282 | RegistraLog("Error al ejecutar la consulta", false); |
---|
[34fc87f] | 4283 | db.GetErrorErrStr(ErrStr); |
---|
[90f061f6] | 4284 | return (false); |
---|
[34fc87f] | 4285 | } |
---|
[90f061f6] | 4286 | if (tbl.ISEOF()) { // Si No existe registro |
---|
| 4287 | RegistraLog("Repositorio NO encontrado", false); |
---|
| 4288 | return (false); |
---|
| 4289 | |
---|
| 4290 | } else { |
---|
| 4291 | if (!tbl.Get("ip", iprepositorio)) { // Toma dato |
---|
[34fc87f] | 4292 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
[90f061f6] | 4293 | return (false); |
---|
| 4294 | } |
---|
| 4295 | if (!tbl.Get("puertorepo", puertorepo)) { // Toma dato |
---|
| 4296 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro |
---|
| 4297 | return (false); |
---|
[34fc87f] | 4298 | } |
---|
| 4299 | } |
---|
[90f061f6] | 4300 | sprintf(ipr, "%s", iprepositorio); |
---|
| 4301 | sprintf(rep, "%d", puertorepo); |
---|
| 4302 | db.Close(); |
---|
| 4303 | return (true); |
---|
[34fc87f] | 4304 | } |
---|
| 4305 | //************************************************************************************************************************************************ |
---|
| 4306 | // PROGRAMA PRINCIPAL ( SERVICIO) |
---|
| 4307 | //*************************************************************************************************************************************************** |
---|
[90f061f6] | 4308 | int main(int argc, char *argv[]) { |
---|
| 4309 | SOCKET socket_s; // Socket donde escucha el servidor |
---|
| 4310 | SOCKET socket_c; // Socket de los clientes que se conectan |
---|
[9fd5e97] | 4311 | int i;// Tamaño de la estructura de direccionamiento IP del cliente |
---|
[90f061f6] | 4312 | socklen_t iAddrSize; |
---|
| 4313 | struct sockaddr_in local, cliente; |
---|
[34fc87f] | 4314 | //pthread_t hThread; |
---|
| 4315 | //void *resul |
---|
| 4316 | // Validación de parámetros |
---|
[90f061f6] | 4317 | |
---|
| 4318 | strcpy(szPathFileCfg, "ogAdmServer.cfg"); |
---|
| 4319 | strcpy(szPathFileLog, "ogAdmServer.log"); |
---|
| 4320 | |
---|
| 4321 | for (i = 1; (i + 1) < argc; i += 2) { |
---|
| 4322 | if (argv[i][0] == '-') { |
---|
| 4323 | switch (tolower(argv[i][1])) { |
---|
| 4324 | case 'f': |
---|
| 4325 | if (argv[i + 1] != NULL) |
---|
| 4326 | strcpy(szPathFileCfg, argv[i + 1]); |
---|
| 4327 | else { |
---|
| 4328 | RegistraLog( |
---|
| 4329 | "Fallo en los parámetros: Debe especificar el fichero de configuración del servicio", |
---|
| 4330 | false); |
---|
| 4331 | exit(EXIT_FAILURE); |
---|
| 4332 | } |
---|
| 4333 | break; |
---|
| 4334 | case 'l': |
---|
| 4335 | if (argv[i + 1] != NULL) |
---|
| 4336 | strcpy(szPathFileLog, argv[i + 1]); |
---|
| 4337 | else { |
---|
| 4338 | RegistraLog( |
---|
| 4339 | "Fallo en los parámetros: Debe especificar el fichero de log para el servicio", |
---|
| 4340 | false); |
---|
| 4341 | exit(EXIT_FAILURE); |
---|
| 4342 | } |
---|
| 4343 | break; |
---|
| 4344 | default: |
---|
| 4345 | RegistraLog( |
---|
| 4346 | "Fallo de sintaxis en los parámetros: Debe especificar -f nombre_del_fichero_de_configuración_del_servicio", |
---|
| 4347 | false); |
---|
| 4348 | exit(EXIT_FAILURE); |
---|
| 4349 | break; |
---|
| 4350 | } |
---|
| 4351 | } |
---|
| 4352 | } |
---|
| 4353 | if (szPathFileCfg == NULL) { |
---|
| 4354 | printf("***Error. No se ha especificado fichero de configuración\n"); |
---|
[b25881b] | 4355 | exit(EXIT_FAILURE); |
---|
| 4356 | } |
---|
[9fd5e97] | 4357 | if (!TomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuración |
---|
[90f061f6] | 4358 | RegistraLog( |
---|
| 4359 | "El fichero de configuración contiene un error de sintaxis", |
---|
| 4360 | false); |
---|
[b25881b] | 4361 | exit(EXIT_FAILURE); |
---|
[34fc87f] | 4362 | } |
---|
[90f061f6] | 4363 | pthread_mutex_init(&guardia, NULL); // Creación del mutex para control de hebras |
---|
[34fc87f] | 4364 | |
---|
[90f061f6] | 4365 | for (i = 0; i < MAXIMOS_SOCKETS; i++) { |
---|
| 4366 | tbsockets[i].ip[0] = '\0'; // Inicializa IP |
---|
| 4367 | tbsockets[i].sock = INVALID_SOCKET; // Inicializa Socket |
---|
[34fc87f] | 4368 | } |
---|
[90f061f6] | 4369 | RegistraLog("***Inicio de sesion***", false); |
---|
[34fc87f] | 4370 | |
---|
| 4371 | socket_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Crea socket |
---|
[90f061f6] | 4372 | if (socket_s == SOCKET_ERROR) { |
---|
| 4373 | RegistraLog("***socket() fallo:", true); |
---|
[34fc87f] | 4374 | } |
---|
| 4375 | local.sin_addr.s_addr = htonl(INADDR_ANY); // selecciona interface |
---|
[90f061f6] | 4376 | local.sin_family = AF_INET; |
---|
[34fc87f] | 4377 | local.sin_port = htons(puerto); // Puerto |
---|
| 4378 | |
---|
[90f061f6] | 4379 | if (bind(socket_s, (struct sockaddr *) &local, // Enlaza socket |
---|
| 4380 | sizeof(local)) == SOCKET_ERROR) { |
---|
| 4381 | RegistraLog("***bind() fallo:", true); |
---|
| 4382 | exit(EXIT_FAILURE); |
---|
[34fc87f] | 4383 | } |
---|
| 4384 | |
---|
[90f061f6] | 4385 | listen(socket_s, 250); // Pone a escuchar al socket |
---|
[34fc87f] | 4386 | iAddrSize = sizeof(cliente); |
---|
| 4387 | |
---|
[90f061f6] | 4388 | while (true) { // Bucle para escuchar peticiones de clientes |
---|
| 4389 | socket_c = accept(socket_s, (struct sockaddr *) &cliente, &iAddrSize); |
---|
| 4390 | if (socket_c == INVALID_SOCKET) { |
---|
| 4391 | RegistraLog("***accept() fallo:", true); |
---|
| 4392 | break; |
---|
[34fc87f] | 4393 | } |
---|
| 4394 | //resul=pthread_create(&hThread,NULL,GestionaConexion,(void*)&socket_c); |
---|
| 4395 | GestionaConexion(&socket_c); |
---|
| 4396 | /*if(resul!=0){2 |
---|
[90f061f6] | 4397 | RegistraLog("***Fallo al crear la hebra cliente",false); |
---|
| 4398 | break; |
---|
| 4399 | } |
---|
| 4400 | */ |
---|
[34fc87f] | 4401 | //pthread_detach(hThread); |
---|
[9fd5e97] | 4402 | close(socket_c); // Cierra la conexión con el servidor hidra |
---|
[34fc87f] | 4403 | } |
---|
| 4404 | close(socket_s); |
---|
[90f061f6] | 4405 | exit(EXIT_SUCCESS); |
---|
[34fc87f] | 4406 | } |
---|