diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d529103 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.bash_history +.bashrc +.config/ +.gdb_history +.gdbinit +.idea/ +.inputrc +.viminfo diff --git a/.vscode/settings.json b/.vscode/settings.json index aa45b88..1adbd76 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "files.associations": { "error.h": "c", "client.h": "c", - "errors.h": "c" + "errors.h": "c", + "server.h": "c", + "challenges.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index c26f792..e052720 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,25 @@ CC = gcc CCFLAGS = -Wall -std=c99 -pedantic -g -OBJECTS = server.o errors.o client.o +# OBJECTS = errors.o server.o client.o -all: $(OBJECTS) +CLIENT=client.o +SERVER=server.o +ERROR=errors.o +CHALLENGE=challenges.o -server.o: server.c include/errors.h - $(CC) $(CCFLAGS) -c server.c -errors.o: errors.c include/errors.h - $(CC) $(CCFLAGS) -c errors.c -client.o: client.c errors.o - $(CC) $(CCFLAGS) client.c -o client.o errors.o +all: $(ERROR) $(CHALLENGE) $(CLIENT) $(SERVER) + +$(SERVER): + $(CC) $(CCFLAGS) server.c -o server challenges.o errors.o +$(CLIENT): + $(CC) $(CCFLAGS) client.c -o client errors.o + +%.o: %.c + $(CC) $(CCFLAGS) -I./include -c $< clean: - rm -rf $(OBJECTS) + rm -rf $(ERROR) $(CHALLENGE) $(CLIENT) $(SERVER) server client test: pvs-studio-analyzer trace -- make @@ -21,4 +27,4 @@ test: plog-converter -a '64:1,2,3;GA:1,2,3;OP:1,2,3' -t tasklist -o report.tasks PVS-Studio.log cppcheck --quiet --enable=all --force --inconclusive . -.PHONY: all clean test \ No newline at end of file +.PHONY: all clean test diff --git a/challenges.c b/challenges.c new file mode 100644 index 0000000..93d0b52 --- /dev/null +++ b/challenges.c @@ -0,0 +1,47 @@ +#include "include/challenges.h" + +// char introChallenge(int fd, char * output, char * message, char * question, char * flag) { +// printf("%s\n", message); + +// int readChars; +// while ((readChars = read(fd, output, MAX_LEN - 1)) != 0) { +// if (readChars < 0) +// printSystemError("Challenges: read()"); +// output[readChars] = '\0'; +// } + +// printf("%s\n", question); + +// return strcmp(output, flag) == 0; +// } + +// char genChallenge(int fd, char * output, char * message, char * question, char * flag) { +// printf("%s\n", message); + +// int readChars; +// while ((readChars = read(fd, output, MAX_LEN - 1)) != 0) { +// if (readChars < 0) +// printSystemError("Challenges: read()"); +// output[readChars] = '\0'; +// } + +// printf("%s\n", question); + +// return !strcmp(output, flag); +// } + +char genChallenge(int fd, char * output, challenge_t challenge) { + printf("%s\n", challenge.message); + + int readChars; + // while ((readChars = read(fd, output, MAX_LEN - 1)) != 0) { + readChars = read(fd, output, MAX_LEN - 1); + if (readChars < 0) + printSystemError("Challenges: read()"); + output[readChars] = '\0'; + // } + + printf("%s\n", challenge.question); + + return strcmp(output, challenge.flag); +} \ No newline at end of file diff --git a/client.c b/client.c index 187fb49..f8319a8 100644 --- a/client.c +++ b/client.c @@ -2,19 +2,16 @@ int main(int argc, char *argv[]) { int fd = 0; - struct sockaddr_in serv_addr; - - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) printSystemError("User: socket()"); - } + struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); serv_addr.sin_addr.s_addr = inet_addr(ADDRESS); - if (connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + if (connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) printSystemError("User: connect()"); - } char input[MAX_LEN] = {0}; int readChars; @@ -23,9 +20,8 @@ int main(int argc, char *argv[]) { printSystemError("User: read()"); input[readChars] = '\0'; - if (write(fd, input, readChars) < 0) { + if (write(fd, input, readChars) < 0) printSystemError("User: write()"); - } } close(fd); diff --git a/client.o b/client.o deleted file mode 100755 index 85bf82b..0000000 Binary files a/client.o and /dev/null differ diff --git a/errors.o b/errors.o deleted file mode 100644 index a398c99..0000000 Binary files a/errors.o and /dev/null differ diff --git a/include/challenges.h b/include/challenges.h new file mode 100644 index 0000000..24c3e20 --- /dev/null +++ b/include/challenges.h @@ -0,0 +1,22 @@ +#ifndef CHALLENGES_H +#define CHALLENGES_H + +#include +#include +#include +#include +#include "errors.h" + +#define MAX_LEN 100 + +typedef struct challenge_t { + // char (* challenge) (int fd, char * output, char * message, char * question, char * flag); + char * message; + char * question; + char * flag; +} challenge_t; + +// char genChallenge(int fd, char * output, char * message, char * question, char * flag); +char genChallenge(int fd, char * output, challenge_t challenge); + +#endif \ No newline at end of file diff --git a/include/client.h b/include/client.h index 3b2505f..277e179 100644 --- a/include/client.h +++ b/include/client.h @@ -9,7 +9,6 @@ #include #include "errors.h" -#define MAX_ARGS 3 #define MAX_LEN 100 #define PORT 8080 #define ADDRESS "127.0.0.1" diff --git a/include/server.h b/include/server.h new file mode 100644 index 0000000..000c3e6 --- /dev/null +++ b/include/server.h @@ -0,0 +1,23 @@ +#ifndef SERVER_H +#define SERVER_H + +#include +#include +#include +#include +#include +#include +#include "errors.h" +#include +#include "challenges.h" + +#define MAX_LEN 100 +#define PORT 8080 +#define ADDRESS "127.0.0.1" +#define MAX_PEND_REQ 3 +#define MAX_CHALLENGES 2 +#define TIME_SLEEP 2 + +void startChallenge(); + +#endif \ No newline at end of file diff --git a/server.c b/server.c index e69de29..656f632 100644 --- a/server.c +++ b/server.c @@ -0,0 +1,60 @@ +#include "include/server.h" + +int clientFd; + +challenge_t challenges[] = { + {"A volar bobby", "Cómo es que matan?", "IZPUAN"}, + {"A cagar bobby", "A una niña", "PUANIZ"} +}; + +int main(int argc, char *argv[]) { + int fd; + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) + printSystemError("Server: socket()"); + + int opt = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) + printSystemError("Server: setsockopt()"); + + struct sockaddr_in address; + address.sin_family = AF_INET; + // address.sin_addr.s_addr = INADDR_ANY; + address.sin_addr.s_addr = inet_addr(ADDRESS); + address.sin_port = htons(PORT); + + if (bind(fd, (struct sockaddr *) &address, sizeof(address)) < 0) + printSystemError("Server: bind()"); + + if (listen(fd, MAX_PEND_REQ) < 0) + printSystemError("Server: listen()"); + + int addrlen = sizeof(address); + if ((clientFd = accept(fd, (struct sockaddr *) &address, (socklen_t *) &addrlen)) < 0) + printSystemError("Server: accept()"); + + startChallenge(); + + close(clientFd); + close(fd); + + return EXIT_SUCCESS; +} + +void startChallenge() { + int challengeCount = 0; + char ansChallenge = 1, output[MAX_LEN] = {0}; + + while (challengeCount < MAX_CHALLENGES && ansChallenge) { + challenge_t currentChallenge = challenges[challengeCount]; + printf("\033[1;1H\033[2J"); // Limpiamos shell + if ((ansChallenge = genChallenge(clientFd, output, currentChallenge))) { + challengeCount++; + } + else { + printf("Incorrecta\n"); + sleep(TIME_SLEEP); + } + } + + return; +} \ No newline at end of file diff --git a/server.o b/server.o deleted file mode 100644 index 3282402..0000000 Binary files a/server.o and /dev/null differ