Refactor code

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-07 22:15:19 -03:00
parent c150cb1e4e
commit 7fc6221c5f
7 changed files with 56 additions and 66 deletions

View File

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

View File

@ -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)
@ -30,30 +28,3 @@ int main(int argc, char *argv[]) {
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");
}
}
}

View File

@ -10,9 +10,8 @@
#include <string.h>
#include <stdlib.h>
#include "errors.h"
#include "sockets.h"
#define MAX_LEN 100
#define PORT 8080
#define ADDRESS "127.0.0.1"
#endif

View File

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

19
include/sockets.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef SOCKETS_H
#define SOCKETS_H
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "errors.h"
#define PORT 8080
#define ADDRESS "127.0.0.1"
void setSockAddress(int argc, char *argv[], struct sockaddr_in * address);
#endif

View File

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

30
sockets.c Normal file
View File

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