[3ec149c] | 1 | // ******************************************************************************************************** |
---|
| 2 | // Servicio: ogAdmRepo |
---|
| 3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla |
---|
| 4 | // Fecha Creación: Marzo-2010 |
---|
| 5 | // Fecha Última modificación: Marzo-2010 |
---|
| 6 | // Nombre del fichero: ogAdmRepo.cpp |
---|
| 7 | // Descripción :Este fichero implementa el servicio de administración general del sistema |
---|
| 8 | // ******************************************************************************************************** |
---|
| 9 | #include "ogAdmRepo.h" |
---|
| 10 | #include "ogAdmLib.c" |
---|
| 11 | //________________________________________________________________________________________________________ |
---|
| 12 | // Función: tomaConfiguracion |
---|
| 13 | // |
---|
| 14 | // Descripción: |
---|
| 15 | // Lee el fichero de configuración del servicio |
---|
| 16 | // Parámetros: |
---|
| 17 | // filecfg : Ruta completa al fichero de configuración |
---|
| 18 | // Devuelve: |
---|
| 19 | // TRUE: Si el proceso es correcto |
---|
| 20 | // FALSE: En caso de ocurrir algún error |
---|
| 21 | //________________________________________________________________________________________________________ |
---|
| 22 | BOOLEAN tomaConfiguracion(char* filecfg) { |
---|
| 23 | char modulo[] = "tomaConfiguracion()"; |
---|
| 24 | |
---|
| 25 | if (filecfg == NULL || strlen(filecfg) == 0) { |
---|
| 26 | errorLog(modulo, 1, FALSE); // Fichero de configuración del servicio vacío |
---|
| 27 | return (FALSE); |
---|
| 28 | } |
---|
| 29 | FILE *fcfg; |
---|
| 30 | long lSize; |
---|
| 31 | char * buffer, *lineas[MAXPRM], *dualparametro[2]; |
---|
| 32 | int i, numlin, resul; |
---|
| 33 | |
---|
| 34 | fcfg = fopen(filecfg, "rt"); |
---|
| 35 | if (fcfg == NULL) { |
---|
| 36 | errorLog(modulo, 2, FALSE); // No existe fichero de configuración del servicio |
---|
| 37 | return (FALSE); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | fseek(fcfg, 0, SEEK_END); |
---|
| 41 | lSize = ftell(fcfg); // Obtiene tamaño del fichero. |
---|
| 42 | rewind(fcfg); |
---|
| 43 | buffer = (char*) reservaMemoria(lSize+1); // Toma memoria para el buffer de lectura. |
---|
| 44 | if (buffer == NULL) { // No hay memoria suficiente para el buffer |
---|
| 45 | errorLog(modulo, 3, FALSE); |
---|
| 46 | return (FALSE); |
---|
| 47 | } |
---|
| 48 | fread(buffer, 1, lSize, fcfg); // Lee contenido del fichero |
---|
| 49 | buffer[lSize]=(char) NULL; |
---|
| 50 | fclose(fcfg); |
---|
| 51 | |
---|
| 52 | iplocal[0] = (char) NULL; //inicializar variables globales |
---|
| 53 | puerto[0] = (char) NULL; |
---|
| 54 | |
---|
| 55 | numlin = splitCadena(lineas, buffer, '\n'); |
---|
| 56 | for (i = 0; i < numlin; i++) { |
---|
| 57 | splitCadena(dualparametro, lineas[i], '='); |
---|
| 58 | resul = strcmp(StrToUpper(dualparametro[0]), "IPLOCAL"); |
---|
| 59 | if (resul == 0) |
---|
| 60 | strcpy(iplocal, dualparametro[1]); |
---|
| 61 | resul = strcmp(StrToUpper(dualparametro[0]), "PUERTO"); |
---|
| 62 | if (resul == 0) |
---|
| 63 | strcpy(puerto, dualparametro[1]); |
---|
| 64 | } |
---|
| 65 | if (iplocal[0] == (char) NULL) { |
---|
| 66 | errorLog(modulo, 4, FALSE); // Falta parámetro IPLOCAL |
---|
| 67 | return (FALSE); |
---|
| 68 | } |
---|
| 69 | if (puerto[0] == (char) NULL) { |
---|
| 70 | errorLog(modulo, 5, FALSE); // Falta parámetro PUERTO |
---|
| 71 | return (FALSE); |
---|
| 72 | } |
---|
| 73 | return (TRUE); |
---|
| 74 | } |
---|
| 75 | // ________________________________________________________________________________________________________ |
---|
| 76 | // Función: gestionaTrama |
---|
| 77 | // |
---|
| 78 | // Descripción: |
---|
| 79 | // Procesa las tramas recibidas . |
---|
| 80 | // Parametros: |
---|
| 81 | // - s : Socket usado para comunicaciones |
---|
| 82 | // Devuelve: |
---|
| 83 | // TRUE: Si el proceso es correcto |
---|
| 84 | // FALSE: En caso de ocurrir algún error |
---|
| 85 | // ________________________________________________________________________________________________________ |
---|
| 86 | BOOLEAN gestionaTrama(SOCKET *socket_c) |
---|
| 87 | { |
---|
| 88 | TRAMA* ptrTrama; |
---|
| 89 | int i, res; |
---|
| 90 | char *nfn; |
---|
| 91 | char modulo[] = "gestionaTrama()"; |
---|
| 92 | |
---|
| 93 | ptrTrama=recibeTrama(socket_c); |
---|
| 94 | if (ptrTrama){ |
---|
| 95 | INTROaFINCAD(ptrTrama); |
---|
| 96 | nfn = copiaParametro("nfn",ptrTrama); // Toma dirección/es IP |
---|
| 97 | for (i = 0; i < MAXIMAS_FUNCIONES; i++) { // Recorre funciones que procesan las tramas |
---|
| 98 | res = strcmp(tbfuncionesRepo[i].nf, nfn); |
---|
| 99 | if (res == 0) { // Encontrada la función que procesa el mensaje |
---|
| 100 | return (tbfuncionesRepo[i].fptr(socket_c, ptrTrama)); // Invoca la función |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | errorLog(modulo, 17, FALSE); // Error en la recepción |
---|
| 106 | return (TRUE); |
---|
| 107 | } |
---|
| 108 | // ******************************************************************************************************** |
---|
| 109 | // PROGRAMA PRINCIPAL (SERVICIO) |
---|
| 110 | // ******************************************************************************************************** |
---|
| 111 | int main(int argc, char *argv[]) |
---|
| 112 | { |
---|
| 113 | SOCKET socket_r; // Socket donde escucha el servidor |
---|
| 114 | SOCKET socket_c; // Socket de los clientes que se conectan |
---|
| 115 | socklen_t iAddrSize; |
---|
| 116 | struct sockaddr_in local, cliente; |
---|
| 117 | char modulo[] = "main()"; |
---|
| 118 | |
---|
| 119 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 120 | Validación de parámetros de ejecución y lectura del fichero de configuración del servicio |
---|
| 121 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 122 | if (!validacionParametros(argc, argv,1)) // Valida parámetros de ejecución |
---|
| 123 | exit(EXIT_FAILURE); |
---|
| 124 | |
---|
| 125 | if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion |
---|
| 126 | exit(EXIT_FAILURE); |
---|
| 127 | } |
---|
| 128 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 129 | Carga del catálogo de funciones que procesan las tramas (referencia directa por puntero a función) |
---|
| 130 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 131 | int cf = 0; |
---|
| 132 | |
---|
| 133 | cf++; |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 137 | Creación y configuración del socket del servicio |
---|
| 138 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 139 | socket_r = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Crea socket del servicio |
---|
| 140 | if (socket_r == SOCKET_ERROR) { // Error al crear el socket del servicio |
---|
| 141 | errorLog(modulo, 13, TRUE); |
---|
| 142 | exit(EXIT_FAILURE); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | local.sin_addr.s_addr = htonl(INADDR_ANY); // Configura el socket del servicio |
---|
| 146 | local.sin_family = AF_INET; |
---|
| 147 | local.sin_port = htons(atoi(puerto)); |
---|
| 148 | |
---|
| 149 | if (bind(socket_r, (struct sockaddr *) &local, sizeof(local))== SOCKET_ERROR) { // Enlaza socket |
---|
| 150 | errorLog(modulo, 14, TRUE); |
---|
| 151 | exit(EXIT_FAILURE); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | listen(socket_r, 250); // Pone a escuchar al socket |
---|
| 155 | iAddrSize = sizeof(cliente); |
---|
| 156 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 157 | Bucle para acceptar conexiones |
---|
| 158 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 159 | infoLog(1); // Inicio de sesión |
---|
| 160 | while(TRUE) { |
---|
| 161 | socket_c = accept(socket_r, (struct sockaddr *) &cliente, &iAddrSize); |
---|
| 162 | if (socket_c == INVALID_SOCKET) { |
---|
| 163 | errorLog(modulo, 15, TRUE); |
---|
| 164 | exit(EXIT_FAILURE); |
---|
| 165 | } |
---|
| 166 | if(!gestionaTrama(&socket_c)){ |
---|
| 167 | errorLog(modulo, 39, TRUE); |
---|
| 168 | break; |
---|
| 169 | } |
---|
| 170 | close(socket_c); |
---|
| 171 | } |
---|
| 172 | /*-------------------------------------------------------------------------------------------------------- |
---|
| 173 | Fin del servicio |
---|
| 174 | ---------------------------------------------------------------------------------------------------------*/ |
---|
| 175 | close(socket_r); |
---|
| 176 | exit(EXIT_SUCCESS); |
---|
| 177 | } |
---|