Add server with challenges

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-11-06 00:33:34 -03:00
parent a1250cb334
commit 08365d9ff3
12 changed files with 183 additions and 20 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
.bash_history
.bashrc
.config/
.gdb_history
.gdbinit
.idea/
.inputrc
.viminfo

View File

@ -2,6 +2,8 @@
"files.associations": {
"error.h": "c",
"client.h": "c",
"errors.h": "c"
"errors.h": "c",
"server.h": "c",
"challenges.h": "c"
}
}

View File

@ -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

47
challenges.c Normal file
View File

@ -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);
}

View File

@ -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);

BIN
client.o

Binary file not shown.

BIN
errors.o

Binary file not shown.

22
include/challenges.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef CHALLENGES_H
#define CHALLENGES_H
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#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

View File

@ -9,7 +9,6 @@
#include <stdlib.h>
#include "errors.h"
#define MAX_ARGS 3
#define MAX_LEN 100
#define PORT 8080
#define ADDRESS "127.0.0.1"

23
include/server.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef SERVER_H
#define SERVER_H
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "errors.h"
#include <netinet/in.h>
#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

View File

@ -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;
}

BIN
server.o

Binary file not shown.