diff --git a/RUN.sh b/RUN.sh index 26e7022..386d79d 100755 --- a/RUN.sh +++ b/RUN.sh @@ -1,14 +1,19 @@ # Run container cis08583_ots_a end expose all ports with --netword=host container_name=ciso8583_ots_a -if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ] | [ "$(docker ps -aq -f status=created -f name=$container_name)" ]; then +if [ "$(docker ps -aq -f status=exited -f name=$container_name)" ] || [ "$(docker ps -aq -f status=created -f name=$container_name)" ]; then echo "Container $container_name exists but is not running" echo "Removing container $container_name to relaunch it" # cleanup ./STOP.sh + echo "-------------------\n\n" fi if [ ! "$(docker ps -a -q -f name=$container_name)" ]; then echo "Running $container_name" docker run -d -it --name $container_name --network=host -v ciso8583_ots_a_logs:/opt/Ciso8583/log $container_name + # docker run -d -it --name $container_name -p 9101:9101 -v ciso8583_ots_a_logs:/opt/Ciso8583/log $container_name + + # For testing purposes (Mac) + # docker run --rm -it --name $container_name -p 9101:9101 -v ciso8583_ots_a_logs:/opt/Ciso8583/log $container_name fi docker container exec -it $container_name bash \ No newline at end of file diff --git a/STOP.sh b/STOP.sh index 69fa2a7..ee839e9 100755 --- a/STOP.sh +++ b/STOP.sh @@ -1,4 +1,4 @@ -if [ "$(docker ps -a -q -f name=$container_name)" ]; then +if [ "$(docker ps -a -q -f name=ciso8583_ots_a)" ]; then echo "Stopping container" docker container stop ciso8583_ots_a docker container remove ciso8583_ots_a diff --git a/docker/buildContainer.sh b/docker/buildContainer.sh index b00fbeb..15c0862 100644 --- a/docker/buildContainer.sh +++ b/docker/buildContainer.sh @@ -4,6 +4,9 @@ cd src/ make cp -a ../dist/. ../tmp/ +# Add full access to be remove by anyone +chmod 777 ../tmp +chmod 777 ../tmp/ciso8583 make clean diff --git a/docker/compile.dockerfile b/docker/compile.dockerfile index b5cea8e..5d6b536 100644 --- a/docker/compile.dockerfile +++ b/docker/compile.dockerfile @@ -5,7 +5,7 @@ LABEL version="0.1" LABEL description="ISO8583 Server in C (Compilation layer, runnable with ./BUILD.sh in /)" RUN apt update -RUN apt install -y vim gcc cmake +RUN apt install -y vim gcc make RUN mkdir /opt/Ciso8583 # Set root path @@ -16,7 +16,7 @@ COPY src src COPY docker/buildContainer.sh . # Create environment -RUN mkdir bin build dist +RUN mkdir bin build dist tmp RUN chmod +x ./buildContainer.sh # CMD ["ls", "-l"] diff --git a/docker/runProduction.sh b/docker/runProduction.sh index 97315c5..dbd0292 100644 --- a/docker/runProduction.sh +++ b/docker/runProduction.sh @@ -1,3 +1,5 @@ -echo "eoeoeoe" +echo "Running binary" -./bin/ciso8583 \ No newline at end of file +./bin/ciso8583 & + +tail -f ./log/Ciso8583.log \ No newline at end of file diff --git a/src/c/log.c b/src/c/log.c index 36f6b52..be89608 100644 --- a/src/c/log.c +++ b/src/c/log.c @@ -9,10 +9,15 @@ FILE* LOG_PTR; char* LOG_NAME; -void log_start(char* filename) { - LOG_PTR = fopen(filename, "w"); - strcpy(LOG_NAME, filename); +int log_start(char* filename) { + LOG_PTR = fopen(filename, "a"); + if (LOG_PTR == NULL) + return -1; + + LOG_NAME = filename; log_info("Starting log for %s", filename); + + return 0; } void log_close() { log_info("Closing log for %s", LOG_NAME); @@ -26,6 +31,7 @@ void log_format(const char* tag, const char* message, va_list args) { fprintf(LOG_PTR, "%s [%s] ", date, tag); vfprintf(LOG_PTR, message, args); fprintf(LOG_PTR, "\n"); + fflush(LOG_PTR); } void log_error(const char* message, ...) { @@ -42,6 +48,12 @@ void log_info(const char* message, ...) { va_end(args); } +void log_warning(const char* message, ...) { + va_list args; + va_start(args, message); + log_format("warning", message, args); + va_end(args); +} void log_debug(const char* message, ...) { va_list args; diff --git a/src/c/main.c b/src/c/main.c index 738bef7..b230751 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -19,20 +19,95 @@ // Server socket struct #define SA struct sockaddr -#define DEBUG_MODE 0 // Cambia de 0 a 1 para mostrarte logs de debug (default: 0) -#define WARNING_MODE 1 // Cambia de 0 a 1 para mostrar los warning en caso de error de petición (default: 1) +#define DEBUG_MODE 1 // Change from 0 to 1 to show debug logs (default: 0) +#define WARNING_MODE 1 // Change from 0 to 1 to show warning logs during execution (default: 1) /* -Cambia de 0 a 1 para forzar desconexión con el cliente en ambito de debugging (default: 0) -(El cliente no recibe respuesta, es decir que simula desconexión para ver como el cliente actua.) +Change from 0 to 1 to force disconnection with the client in debugging mode (default: 0) +(The client won't receive any response, it means that it will simulate a disconnection to see how the client acts.) -En casos de producción el cliente puede perder conexión red, -por lo que hay que comprobar como actua ambas partes para prevenir que lance errores o se atasque. +In production mode, the client can lose the connection, so it's necessary to check how both parts act to prevent errors or get stuck. -¡¡¡ Siempre tiene que estar en 0, para producción !!!! +¡¡¡ It always has to be 0, for production !!! */ #define FORCE_DISCONNECTION 0 +// Functions +int start_server(int port, int max_client); +int accept_server(int idSocketServer); +int recv_server(int idSocketClient, char* buffer, int bytes); +int send_server(int idSocketClient, char* text, int bytes); +void conn_exit(int idSocket); + + +int start_server(int port, int max_client) { + int idSocketServer; + struct sockaddr_in address; + + // Creating socket file descriptor + if ((idSocketServer = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + log_error("Error opening socket server in port %d", port); + conn_exit(idSocketServer); + } + + // assign IP, PORT + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_ANY); + address.sin_port = htons(port); + + // Binding newly created socket to given IP and verification + if ((bind(idSocketServer, (SA*) &address, sizeof(address))) != 0) { + log_error("Socket bind failed to socket %d", idSocketServer); + conn_exit(idSocketServer); + } + + // Now server is ready to listen and verification + if (listen(idSocketServer, max_client) == -1) { + log_error("Error listening socket %d", idSocketServer); + conn_exit(idSocketServer); + } + + return idSocketServer; +} +int accept_server(int idSocketServer) { + int idSocketClient; + struct sockaddr_in address; + socklen_t len; + + // Accept the data packet from client and verification + len = sizeof(address); + idSocketClient = accept(idSocketServer, (SA*) &address, &len); + if (idSocketClient < 0) { + log_error("Error accepting socket %d", idSocketClient); + conn_exit(idSocketClient); + } + + return idSocketClient; +} + +int recv_server(int idSocketClient, char* buffer, int bytes) { + int totalBytesReceived = 0; + int bytesReceived; + + while (totalBytesReceived < bytes) { + bytesReceived = recv(idSocketClient, buffer + totalBytesReceived, bytes - totalBytesReceived, 0); + + if (bytesReceived <= 0) { + // Error connection or closed in the otherside + return -1; + } + + totalBytesReceived += bytesReceived; + } + + return totalBytesReceived; +} + +void conn_exit(int idSocket) { + close(idSocket); + log_close(); + exit(EXIT_FAILURE); +} /*! * \brief: Arguments posible @@ -41,12 +116,16 @@ por lo que hay que comprobar como actua ambas partes para prevenir que lance err * [ip] : default: 127.0.0.1 */ int main(int argc, char *argv[]) { - - int iRet; + int iRet, bypass = 0; + pid_t pid; char ipSocket[16]; - int idSocket, portSocket, idClient; - struct sockaddr_in address, client; + int portSocket; + // Server + int idSocketServer, idSocketClient; + // Buffer + ISO8583 iso8583Message; + memset(&iso8583Message, '\0', sizeof(iso8583Message)); // Set ip and port memset(ipSocket, '\0', sizeof(16)); @@ -60,598 +139,85 @@ int main(int argc, char *argv[]) { if (argc >= 3) strcpy(ipSocket, argv[2]); - - log_info("Starting socket server in port %d", portSocket); - - // Creating socket file descriptor - if ((idSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - log_error("Error opening socket server in port %d", portSocket); - log_close(); - exit(EXIT_FAILURE); - } - - // assign IP, PORT - address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_ANY); - address.sin_port = htons(portSocket); + // Start logging + iRet = log_start(LOG_FILENAME); - // Binding newly created socket to given IP and verification - if ((bind(idSocket, (SA*)&address, sizeof(address))) != 0) { - log_error("Socket bind failed to socket %d", idSocket); - log_close(); - exit(EXIT_FAILURE); + if (iRet < 0) { + fprintf(stderr, "Error LOGOPEN\n"); + return CODE_ERROR_OPEN_FILE; } - - log_info("Closing"); + // Wake up message + log_info("Starting socket server in port %d", portSocket); + /* + Starting server with parameters: + - PORT: %d + - MAX_CLIENTS: %d + + Any error will be needed to be tested + The function start_server will control the error and exit the program if it's necessary + The function will return the socket id + */ + idSocketServer = start_server(portSocket, MAX_CLIENTS); + + // conn_exit(idSocketServer); + // Main loop + while (1) { + // Accept the data packet from client and verification + idSocketClient = accept_server(idSocketServer); + + // Check status + if (idSocketClient < 0) { + log_error("Error accepting socket %d", idSocketClient); + conn_exit(idSocketClient); + } + + if ((pid = fork()) < 0) { +#if WARNING_MODE == 1 + log_warning("Error forking, bypassing and forcing parent to execute"); +#endif + bypass = 1; + } + +#if DEBUG_MODE == 1 + log_debug("PID %d , Bypass %d", pid, bypass); +#endif + + // Parent + if (pid > 0 || bypass == 1) { + // Print client info + log_info("New client connected with ID: %d", idSocketClient); + } + + // Child process but can be bypassed + if(pid == 0 || bypass == 1) { + while (1) { + // Ejemplo: enviar un mensaje al cliente + // const char *message = "¡Hola, cliente!\n"; + // send(idSocketClient, message, strlen(message), 0); + + // Start receiving header (Size of message) + recv_server(idSocketClient, iso8583Message.header, ISO8583_HEADER_SIZE); + + // Start receiving MTI + recv_server(idSocketClient, iso8583Message.mti, ISO8583_MTI_SIZE); + log_debug("Value %d", atoi(iso8583Message.header)); + +#if PERSISTANCE_CONN != 1 + // Close connection + log_info("Closing client %d", idSocketClient); + close(idSocketClient); + exit(EXIT_SUCCESS); + break; +#endif + } + } + } // Close socket - close(idSocket); + close(idSocketServer); + log_close(); return 0; -} - - -// /* ======================================================================== */ -// /* Example filename data format */ -// /* -- */ -// /* yyyymmddhhiissAAAAAAPPPCCCCC */ -// /* AAAAAA = Ticket number */ -// /* PPP = Pos number */ -// /* CCCCC = Commerce number */ -// /* -------------------------------------------------------------------------*/ -// /* Example request data format */ -// /* -- */ -// /* BBBBBB|yyyymmddhhiissAAAAAAPPPCCCCC|SIGNATURE_DATA_HEX| */ -// /* -------------------------------------------------------------------------*/ -// /* Must include Control Character | */ -// /* ======================================================================== */ - - -// static int SignatureService_readConfig(); -// static int SignatureService_readConfigDB(); -// static int SignatureService_connectDB(void); -// static int SignatureService_disconnectDB(void); -// static int SignatureMain_closeConnection(int); -// static int SignatureMain_checkMessageEnd(char *); -// static int SignatureMain_checkMessageSize(char *, int); -// static int SignatureMain_checkMessageContent(char *, int, int *); -// static int SignatureMain_writeFile(char *, char *, char *); -// static int SignatureMain_commReceiveSocket(int, char *, int, int, int); -// static int SignatureMain_commSendSocket(int, char *); - - -// /* -// * Private global variables definitions -// */ -// /*! \brief Buffer de datos recividos */ -// char DataReceivedPos[COMM_RECEIVE_SIZE_MAX + 1]; -// /*! \brief Buffer de datos enviados */ -// char DataRespondPos[POS_DATA_HEADER_SIZE + 1]; - -// char PosDataSize[POS_DATA_CHECK_SIZE + 1]; -// // 2023110910300011111100088888 -// char PosDataHeader[POS_DATA_HEADER_SIZE + 1]; -// char PosDataEmptyHeader[POS_DATA_HEADER_SIZE + 1]; -// char PosDataBody[COMM_MAX_SIZE]; - -// /* -// * Functions -// */ -// /*! \brief Read default configuration */ -// static int SignatureService_readConfig(void) { -// base_pathBuilder_struct cfg_path; -// base_pathBuilder_translate(&cfg_path, BASE_PATHDEFINE_PARAM_PATH); -// if (conf_general_load(cfg_path.value)) { -// SPLOG_ERROR("Error loading Global Configuration from \"%s\"", cfg_path.value); -// return 1; -// } -// return 0; -// } - -// /*! \brief Reading configuration from DB */ -// static int SignatureService_readConfigDB(void) { -// if (ConfigContainer::loadFromDB() != 0) { -// SPLOG_EMERG("Error loading configuration from DB"); -// return 1; -// } -// SPLOG_NOTICE("Configuration loaded"); - -// return 0; -// } - -// /*! \brief Connection with DB */ -// static int SignatureService_connectDB(void) { -// SPLOG_NOTICE("Opening the database"); -// SPLOG_NOTICE("Initializing access to the database"); -// if (DBA_st_init_database() == -1) { -// SPLOG_ERROR("ERROR initializing access to the database"); -// return -1; -// } -// SPLOG_NOTICE("Access to database initialized"); - -// return 0; -// } - -// /*! \brief Disconnection with DB */ -// static int SignatureService_disconnectDB(void) { -// SPLOG_NOTICE("Closing the database"); -// DBA_st_close_database(); -// return 0; -// } - -// /* =================================== */ -// /* Main functions */ -// /* =================================== */ -// /*! \brief Close connection with client socket id */ -// static int SignatureMain_closeConnection(int idsocket) { -// struct linger ling; -// size_t val; -// val = sizeof(ling); -// ling.l_onoff = 1; -// ling.l_linger = 10; -// /* fcntl (idsocket,F_SETFL,fcntl(idsocket,F_GETFL) | O_NONBLOCK); */ -// setsockopt(idsocket, SOL_SOCKET, SO_LINGER, &ling, val); -// /*shutdown (idsocket,2); */ -// close(idsocket); -// return 0; -// } -// /*! \brief Comprueba si el mensaje enviado por el cliente esta completo por EOM o ha sido corrompiedo o perdido */ -// static int SignatureMain_checkMessageEnd(char *Buffer) { -// if(Buffer[strlen(Buffer) - 1] == SIGNATURE_DATA_SPLIT_CHAR) { -// return 0; -// } -// return 1; -// } -// /*! \brief Comprueba si el mensaje enviado por el cliente esta completo por TAMAÑO o ha sido corrompiedo o perdido */ -// static int SignatureMain_checkMessageSize(char *Buffer, int size) { -// if(strlen(Buffer) == size) { -// return 0; -// } -// return 1; -// } -// /*! \brief Comprueba si existe caracter no valido o numero impar de bytes */ -// static int SignatureMain_checkMessageContent(char *Buffer, int size, int *output) { -// int i = 0; -// // Remove "new line"or space at end of string if exist -// while (Buffer[size - 1] == '\n' || Buffer[size - 1] == ' ') { -// Buffer[size - 1] = '\0'; -// size--; -// } - -// while (1) { -// // Finish -// if (i >= size) -// break; - -// if ((Buffer[i] < '0' || Buffer[i] > '9') && -// (Buffer[i] < 'A' || Buffer[i] > 'F')) { -// *output = (int) Buffer[i]; -// return 4; -// } - -// i++; -// } - -// if (i % 2 != 0) { -// *output = i; -// return 5; -// } - -// return 0; -// } -// /*! \brief Abre el directorio de firmas y escribe dentro del archivo la firma con sus metadatos en el nombre */ -// static int SignatureMain_writeFile(char *path, char *filename, char *content) { -// char *inter = "/"; - -// // File path -// char *filedir = new char[strlen(path)+strlen(filename)+2]; - -// // Verifica si se asignó memoria correctamente -// if (filedir == NULL) { -// SPLOG_ERROR("Error asignando memoria para %s/%s", path, filename); -// return 1; -// } - -// memset(filedir, '\0', sizeof(filedir)); -// strcpy(filedir, path); -// strcat(filedir, inter); -// strcat(filedir, filename); - -// FILE *fptr; - -// // Open directory + filename -// fptr = fopen(filedir, "w"); - -// if(fptr == NULL) { -// SPLOG_ERROR("Error abriendo fichero para %s", filedir); -// return 1; -// } - -// fprintf(fptr, "%s", content); -// fclose(fptr); - -// return 0; -// } -// /*! \brief Recibe datos del cliente hasta parar */ -// static int SignatureMain_commReceiveSocket(int idsocket, char *Buffer, int Long, int Intentos, int Lap) { -// int Recibidos, iRet; -// int real_iSize, repetitions = 0; -// fd_set listo; -// struct timeval timeout, t0, t1; - -// do { -// if (idsocket < 0) { -// comm_errno = EINVAL; -// SPLOG_WARN("Error receiving from socket:%d (%s)", COMM_ERROR_SOCKET_RECEIVE, -// "Wrong parameter."); - -// return COMM_ERROR_SOCKET_RECEIVE; -// } - -// /* Obtenemos el instante actual */ -// gettimeofday(&t0, (struct timezone *)0); -// repetitions = 0; -// do { -// repetitions++; -// FD_ZERO(&listo); -// FD_SET(idsocket, &listo); -// /* En funcion del numero de iteraciones del bucle calculamos el timeout */ -// if (repetitions == 1) { -// /* Asignamos el timeout inicial */ -// timeout.tv_sec = Lap; -// timeout.tv_usec = 0; -// } -// else { -// /* Obtenemos el insntante de ejecucion del bucle */ -// gettimeofday(&t1, (struct timezone *)0); -// /* Calculamos el timeout en funcion de los instantes */ -// timeout.tv_sec = Lap - (t1.tv_sec - t0.tv_sec); -// timeout.tv_usec = 0; -// } -// real_iSize = 0; -// errno = 0; -// iRet = select(idsocket + 1, &listo, 0, 0, &timeout); -// } while (iRet == -1 && errno == EINTR); - -// if (iRet < 0) { -// comm_errno = errno; -// SPLOG_WARN("Error receiving from socket [select] Return:%d errno:%d (%s)", -// COMM_ERROR_SOCKET_RECEIVE, errno, strerror(errno)); -// return (COMM_ERROR_SOCKET_RECEIVE); -// } -// if (iRet == 0) { /* time out */ -// Intentos--; -// } -// else { -// do { -// Recibidos = -// recv(idsocket, (char *)Buffer + real_iSize, -// Long - real_iSize, 0); - -// real_iSize += Recibidos; - -// if(SignatureMain_checkMessageEnd(Buffer) == 0) { -// return 1; -// } - -// if(real_iSize >= Long) { -// // Size exceded -// return -2; -// } - -// // Control data -// switch (Recibidos) { -// case 0: -// // The server stopped receiving successfully -// return 0; -// break; - -// case COMM_ERROR_SOCKET_RECEIVE: -// return COMM_ERROR_SOCKET_RECEIVE; -// break; -// } - -// if(Recibidos < 0) { -// // If the server stop receiving due -// // close connection or end of message must be checked after -// SPLOG_ALARM("Client closed the connection, try again"); -// return -1; -// } - -// if (real_iSize == Long) { -// comm_errno = 0; -// return 0; -// } -// } while (real_iSize < Long); - -// comm_errno = errno; - -// return (COMM_ERROR_SOCKET_RECEIVE); -// } -// } while (Intentos > 0); -// comm_errno = errno; -// SPLOG_WARN("Error:%d (%s)", COMM_ERROR_SOCKET_RECEIVE, -// "Retries exceeded."); -// return (COMM_ERROR_SOCKET_RECEIVE); -// } -// /*! \brief Enviar respuesta de vuelta al cliente */ -// static int SignatureMain_commSendSocket(int idsocket, char *content) { -// #if DEBUG_MODE -// SPLOG_DEBUG("Response: %s", content); -// #endif -// return comm_sendSocket( -// idsocket, content, POS_DATA_HEADER_SIZE, 1, -// ConfigContainer::SERVER_POS_CONNECTION_TIMEOUT); -// } - - -// /* ======================================================================== */ -// /* Main Proccess */ -// /* ======================================================================== */ -// int main(void) { - -// // Environment to LOG status -// int iRet, res; -// base_pathBuilder_struct path; - -// int idSignatureSocket; -// int idSocketAccepted; -// string ipSocket; -// int portSocket; - -// int signatureReceive; -// int signatureRespond; - -// int processInitialization; - -// base_pathBuilder_translate(&path, LOG_SIGNATURE_SERVER); -// iRet = SPLOG_OPEN(path.value); -// if (iRet < 0) { -// fprintf(stderr, "Error LOGOPEN\n"); -// return BASE_PROCESSMANAGER_ERROR_OPEN_LOG; -// } - -// // Wake up message -// SPLOG_NOTICE("-------------------------------------------------------"); -// SPLOG_NOTICE("Starting solverpay-signature-server process"); - -// if (SignatureService_readConfig()) { -// return BASE_PROCESSMANAGER_ERROR_LOAD_CFG; -// } - -// if (SignatureService_connectDB()) { -// return BASE_PROCESSMANAGER_ERROR_CHECK_DB; -// } - -// if (SignatureService_readConfigDB()) { -// SignatureService_disconnectDB(); -// return BASE_PROCESSMANAGER_ERROR_OPEN_DB; -// } - -// // base_process_type process; - -// // base_process_init(&process); -// // process.processTitle = "solverpay-signature-server"; -// // process.logPath = LOG_SIGNATURE_SERVER; - -// // processInitialization = base_process_start(&process); -// // if (processInitialization) { -// // return processInitialization; -// // } - -// ipSocket = ConfigContainer::SIGNATURE_IP_LISTEN; -// portSocket = ConfigContainer::SIGNATURE_PORT_LISTEN; - - -// // Init socket listener -// SPLOG_NOTICE("Server initializing..."); -// idSignatureSocket = -// comm_openSocket(ipSocket.c_str(), -// portSocket); - -// if (idSignatureSocket == COMM_ERROR_OPEN_SOCKET || idSignatureSocket == COMM_ERROR_BIND || idSignatureSocket == COMM_ERROR_LISTEN) { -// SPLOG_EMERG("Error while opening server socket."); -// SignatureService_disconnectDB(); -// log_Transactions_close(); -// SPLOG_CLOSE(); -// return BASE_PROCESSMANAGER_ERROR_SERVER_SOCKET; -// } - -// SPLOG_NOTICE("Server listening in PORT: %d with ID: %d", portSocket, idSignatureSocket); - -// // Set error answer -// memset(PosDataEmptyHeader, '\0', POS_DATA_HEADER_SIZE+1); // Full NULL -// memset(PosDataEmptyHeader, '0', POS_DATA_HEADER_SIZE); - -// /* ============================== */ -// /* Request listener code */ -// /* */ -// /* ============================== */ - -// do { -// // Check status -// idSocketAccepted = comm_AcceptRequest(idSignatureSocket); - -// #if DEBUG_MODE -// SPLOG_DEBUG("Socket ID: %d", idSocketAccepted); -// #endif - -// // Skip error -// if (idSocketAccepted == COMM_ERROR_ACCEPT_PROCESS) { -// continue; -// } - -// if (idSocketAccepted < 0) { -// SPLOG_ERROR("Error accepting socket in PORT: %d with ID: %d", portSocket, idSignatureSocket); -// sleep(1); -// } else { -// // Reset -// memset(DataReceivedPos, '\0', COMM_MAX_SIZE); -// memset(DataRespondPos, '\0', POS_DATA_HEADER_SIZE); -// // Receive -// // Using own function (If the client don't send nothing stop receiving) -// signatureReceive = SignatureMain_commReceiveSocket( -// idSocketAccepted, DataReceivedPos, COMM_MAX_SIZE, 1, -// ConfigContainer::SERVER_POS_CONNECTION_TIMEOUT); - -// #if DEBUG_MODE -// SPLOG_DEBUG("Content %s", DataReceivedPos); -// #endif - -// // Check status -// // 0000000000000000000000000000 = Error Server -// // 0000000000000000000000000001 = Error EOM -// // 0000000000000000000000000002 = Error SIZE -// // 0000000000000000000000000003 = Error SIZE EXCEDED -// // 0000000000000000000000000004 = Error INVALID CHAR -// // 0000000000000000000000000005 = Error ODD SIZE -// switch (signatureReceive) { -// case COMM_ERROR_SOCKET_RECEIVE: -// SPLOG_ERROR("Error reading request in PORT: %d with ID: %d", portSocket, idSignatureSocket); -// SignatureMain_closeConnection(idSocketAccepted); -// SPLOG_CLOSE(); - -// // Server error = 0 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '0'; -// SignatureMain_commSendSocket(idSocketAccepted, PosDataEmptyHeader); - -// return BASE_PROCESSMANAGER_ERROR_SERVER_SOCKET; -// break; - -// case -2: -// // Response size exceded -// #if WARNING_MODE -// SPLOG_DEBUG("Error SIZE EXCEDED: size_limit %dKb", COMM_MAX_K); -// #endif -// // Size limit error = 3 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '3'; -// SignatureMain_commSendSocket(idSocketAccepted, PosDataEmptyHeader); - -// continue; -// break; - -// case -1: -// // Ommit this proccess -// // Client closed connextion -// SignatureMain_closeConnection(idSocketAccepted); - -// continue; -// break; -// case 0: -// // Check if the message is completed after stopped receiving message data -// // Server stopped receiving data -// if (SignatureMain_checkMessageEnd(DataReceivedPos)) { -// #if WARNING_MODE -// SPLOG_DEBUG("Error EOM: char %c", DataReceivedPos[strlen(DataReceivedPos) - 1]); -// #endif -// // EOM error = 1 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '1'; -// SignatureMain_commSendSocket(idSocketAccepted, PosDataEmptyHeader); - -// // Ommit this proccess -// SignatureMain_closeConnection(idSocketAccepted); - -// continue; -// } -// break; -// } - -// memset(PosDataSize, '\0', POS_DATA_CHECK_SIZE); -// memset(PosDataHeader, '\0', POS_DATA_HEADER_SIZE); -// memset(PosDataBody, '\0', COMM_MAX_SIZE); -// // Split body data - -// // TODO: Memcopy -// memcpy(PosDataSize, DataReceivedPos, POS_DATA_CHECK_SIZE); -// memcpy(PosDataHeader, DataReceivedPos + POS_DATA_CHECK_SIZE + 1, POS_DATA_HEADER_SIZE); -// memcpy(PosDataBody, DataReceivedPos + POS_DATA_CHECK_SIZE + POS_DATA_HEADER_SIZE + 2, sizeof(DataReceivedPos) - POS_DATA_CHECK_SIZE - POS_DATA_HEADER_SIZE); -// // Remove control character -// PosDataBody[strlen(PosDataBody) - 1] = '\0'; - -// // Size to int -// int PosContentSize = atoi(PosDataSize); - -// #if DEBUG_MODE -// SPLOG_DEBUG("Size %s", PosContentSize); -// SPLOG_DEBUG("Header: %s", PosDataHeader); -// SPLOG_DEBUG("Body: %s", PosDataBody); -// #endif - -// // Check content body length -// if (SignatureMain_checkMessageSize(PosDataBody, PosContentSize)) { -// #if WARNING_MODE -// SPLOG_DEBUG("Error Size: Defined %d bytes; Received %d bytes", atoi(PosDataSize), strlen(PosDataBody)); -// #endif -// // Size error = 2 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '2'; -// SignatureMain_commSendSocket(idSocketAccepted, PosDataEmptyHeader); - -// // Ommit this proccess -// SignatureMain_closeConnection(idSocketAccepted); - -// continue; -// } - -// // Check content type -// // Los valores aceptados en el body son hexadecimales sin espacios ni nuevas lineas -// res = 0; -// if(signatureReceive = SignatureMain_checkMessageContent(PosDataBody, PosContentSize, &res)) { -// switch (signatureReceive) { -// case 4: -// #if WARNING_MODE -// SPLOG_DEBUG("Error Char: Invalid character \"%c\"", (char)res); -// #endif -// // Char error = 4 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '4'; -// break; - -// case 5: -// #if WARNING_MODE -// SPLOG_DEBUG("Error Odd: Body with odd size number (%d bytes)", res); -// #endif -// // Odd error = 5 -// PosDataEmptyHeader[POS_DATA_HEADER_SIZE-1] = '5'; -// break; -// } - -// SignatureMain_commSendSocket(idSocketAccepted, PosDataEmptyHeader); - -// // Ommit this proccess -// SignatureMain_closeConnection(idSocketAccepted); - -// continue; -// } - -// // Save file -// if(SignatureMain_writeFile(INPUT_PATH, PosDataHeader, PosDataBody)) { -// SPLOG_ERROR("Error writing on file"); -// SignatureMain_closeConnection(idSocketAccepted); -// SPLOG_CLOSE(); -// return BASE_PROCESSMANAGER_ERROR_CANNOT_CONTINUE; -// } - -// // Response -// memcpy(DataRespondPos, PosDataHeader, POS_DATA_HEADER_SIZE); - -// if(!FORCE_DISCONNECTION) { -// iRet = SignatureMain_commSendSocket(idSocketAccepted, PosDataHeader); - -// if(iRet == COMM_ERROR_SOCKET_SEND) { -// SPLOG_ERROR("Error sending answer to ID: %s with PORT: %s", idSignatureSocket, portSocket); -// SignatureMain_closeConnection(idSocketAccepted); -// SPLOG_CLOSE(); -// return BASE_PROCESSMANAGER_ERROR_SERVER_SOCKET; -// } -// } - -// SPLOG_NOTICE("New signature: %s", PosDataHeader); - - -// // Close connection (May don't need it) -// SignatureMain_closeConnection(idSocketAccepted); -// } - -// } while(1); //!base_process_isStopFlag()); -// } +} \ No newline at end of file diff --git a/src/h/env.h b/src/h/env.h index 35e6cff..41aef4f 100644 --- a/src/h/env.h +++ b/src/h/env.h @@ -4,11 +4,12 @@ // ISO8583 Server message data size /*! \brief Header size */ #define ISO8583_HEADER_SIZE 2 +#define ISO8583_MTI_SIZE 2 #define ISO8583_P_BITMAP_SIZE 16 // ================================ // System environment defines /*! \brief Main name */ -#define PROJECT_NAME "Cisco8583" +#define PROJECT_NAME "Ciso8583" /*! \brief Workdir path */ #define WORKDIR_PATH "/opt/" PROJECT_NAME /*! \brief Log file path */ @@ -25,4 +26,20 @@ // ==== #define DEFAULT_PORT 9101 #define DEFAULT_IP "127.0.0.1" +#define MAX_CLIENTS 100 +#define PERSISTANCE_CONN 1 + +// Error codes +#define CODE_OK 0 +#define CODE_ERROR_SERVER -1 +#define CODE_ERROR_OPEN_FILE -2 +#define CODE_ERROR_WRITE_FILE -3 + +// Message +struct ISO8583_MESSAGE { + char header[ISO8583_HEADER_SIZE + 1]; + char mti[ISO8583_MTI_SIZE + 1]; + char buffer[COMM_RECEIVE_SIZE_MAX + 1]; +}; +#define ISO8583 struct ISO8583_MESSAGE #endif \ No newline at end of file diff --git a/src/h/log.h b/src/h/log.h index 03bcd59..449ed52 100644 --- a/src/h/log.h +++ b/src/h/log.h @@ -4,11 +4,12 @@ #define LOG_H -void log_start(char* filename); +int log_start(char* filename); void log_close(void); void log_format(const char* tag, const char* message, va_list args); void log_error(const char* message, ...); void log_info(const char* message, ...); +void log_warning(const char* message, ...); void log_debug(const char* message, ...);