From 7fc6221c5f947b26bf9b0ade05771db3258c9704 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 7 Nov 2021 22:15:19 -0300 Subject: [PATCH] Refactor code Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- Makefile | 9 +++++---- client.c | 29 ----------------------------- include/client.h | 3 +-- include/server.h | 3 +-- include/sockets.h | 19 +++++++++++++++++++ server.c | 29 ----------------------------- sockets.c | 30 ++++++++++++++++++++++++++++++ 7 files changed, 56 insertions(+), 66 deletions(-) create mode 100644 include/sockets.h create mode 100644 sockets.c diff --git a/Makefile b/Makefile index 6bff332..457ee0a 100644 --- a/Makefile +++ b/Makefile @@ -6,25 +6,26 @@ SERVER=server ERROR=errors.o CHALLENGE=challenges.o RANDOM=random.o +SOCKETS=sockets.o TMP := $(shell mktemp) -all: $(ERROR) $(CHALLENGE) $(RANDOM) $(CLIENT) $(SERVER) +all: $(ERROR) $(CHALLENGE) $(RANDOM) $(SOCKETS) $(CLIENT) $(SERVER) $(SERVER): server.c challenges.c errors.c include/errors.h include/challenges.h include/server.h include/challengesLib.h - $(CC) $(CCFLAGS) server.c -o server challenges.o errors.o random.o -lm + $(CC) $(CCFLAGS) server.c -o server challenges.o errors.o random.o sockets.o -lm objcopy --add-section .RUN_ME="$(TMP)" --set-section-flags .mydata=noload,readonly server strip --strip-debug server rm "$(TMP)" $(CLIENT): client.c errors.c include/errors.h include/client.h - $(CC) $(CCFLAGS) client.c -o client errors.o + $(CC) $(CCFLAGS) client.c -o client errors.o sockets.o %.o: %.c $(CC) $(CCFLAGS) -I./include -c $< clean: - rm -rf $(ERROR) $(CHALLENGE) $(CLIENT) $(SERVER) $(RANDOM) + rm -rf $(ERROR) $(CHALLENGE) $(CLIENT) $(SERVER) $(RANDOM) $(SOCKETS) PVS-Studio.log report.tasks strace_out test: pvs-studio-analyzer trace -- make diff --git a/client.c b/client.c index 46cb63f..32d874d 100644 --- a/client.c +++ b/client.c @@ -2,8 +2,6 @@ // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "include/client.h" -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address); - int main(int argc, char *argv[]) { int fd; if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) @@ -29,31 +27,4 @@ int main(int argc, char *argv[]) { close(fd); return EXIT_SUCCESS; -} - -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address) { - address->sin_family = AF_INET; - - if (argc == 1) { - address->sin_addr.s_addr = inet_addr(ADDRESS); - address->sin_port = htons(PORT); - } - - int opt; - while ((opt = getopt(argc, argv, "a:p:")) != -1) { - switch (opt) { - case 'a': - if (optarg[0] == '-') - printError("Usage: server [-a address] [-p port]\n"); - address->sin_addr.s_addr = inet_addr(optarg); - break; - case 'p': - if (optarg[0] == '-') - printError("Usage: server [-a address] [-p port]\n"); - address->sin_port = htons(atoi(optarg)); - break; - default: - printError("Usage: server [-a address] [-p port]\n"); - } - } } \ No newline at end of file diff --git a/include/client.h b/include/client.h index f123fb2..4918f2f 100644 --- a/include/client.h +++ b/include/client.h @@ -10,9 +10,8 @@ #include #include #include "errors.h" +#include "sockets.h" #define MAX_LEN 100 -#define PORT 8080 -#define ADDRESS "127.0.0.1" #endif \ No newline at end of file diff --git a/include/server.h b/include/server.h index 66839f7..4b4059f 100644 --- a/include/server.h +++ b/include/server.h @@ -14,10 +14,9 @@ #include "errors.h" #include "challenges.h" #include "challengesLib.h" +#include "sockets.h" #define MAX_LEN 100 -#define PORT 8080 -#define ADDRESS "127.0.0.1" #define MAX_PEND_REQ 3 #define TIME_SLEEP 2 diff --git a/include/sockets.h b/include/sockets.h new file mode 100644 index 0000000..53f45e1 --- /dev/null +++ b/include/sockets.h @@ -0,0 +1,19 @@ +#ifndef SOCKETS_H +#define SOCKETS_H + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include +#include "errors.h" + +#define PORT 8080 +#define ADDRESS "127.0.0.1" + +void setSockAddress(int argc, char *argv[], struct sockaddr_in * address); + +#endif \ No newline at end of file diff --git a/server.c b/server.c index 44b7c54..976db84 100644 --- a/server.c +++ b/server.c @@ -2,8 +2,6 @@ // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "include/server.h" -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address); - int main(int argc, char *argv[]) { int fd; if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) @@ -66,30 +64,3 @@ void startChallenge(int fd) { return; } - -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address) { - address->sin_family = AF_INET; - - if (argc == 1) { - address->sin_addr.s_addr = inet_addr(ADDRESS); - address->sin_port = htons(PORT); - } - - int opt; - while ((opt = getopt(argc, argv, "a:p:")) != -1) { - switch (opt) { - case 'a': - if (optarg[0] == '-') - printError("Usage: server [-a address] [-p port]\n"); - address->sin_addr.s_addr = inet_addr(optarg); - break; - case 'p': - if (optarg[0] == '-') - printError("Usage: server [-a address] [-p port]\n"); - address->sin_port = htons(atoi(optarg)); - break; - default: - printError("Usage: server [-a address] [-p port]\n"); - } - } -} \ No newline at end of file diff --git a/sockets.c b/sockets.c new file mode 100644 index 0000000..bc501cd --- /dev/null +++ b/sockets.c @@ -0,0 +1,30 @@ +// This is a personal academic project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com +#include "include/sockets.h" + +void setSockAddress(int argc, char *argv[], struct sockaddr_in * address) { + address->sin_family = AF_INET; + + if (argc == 1) { + address->sin_addr.s_addr = inet_addr(ADDRESS); + address->sin_port = htons(PORT); + } + + int opt; + while ((opt = getopt(argc, argv, "a:p:")) != -1) { + switch (opt) { + case 'a': + if (optarg[0] == '-') + printError("Usage: server [-a address] [-p port]\n"); + address->sin_addr.s_addr = inet_addr(optarg); + break; + case 'p': + if (optarg[0] == '-') + printError("Usage: server [-a address] [-p port]\n"); + address->sin_port = htons(atoi(optarg)); + break; + default: + printError("Usage: server [-a address] [-p port]\n"); + } + } +} \ No newline at end of file