363 lines
9.5 KiB
C
363 lines
9.5 KiB
C
/**
|
|
* @file server.c
|
|
* @author Tobias Eidelpes <e01527193@student.tuwien.ac.at>
|
|
* @date 2018-03-02
|
|
*
|
|
* @brief Server for OSUE exercise 1B `Battleship'.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
#include <fcntl.h>
|
|
|
|
#include "common.h"
|
|
|
|
static const char *port = DEFAULT_PORT;
|
|
|
|
static struct addrinfo *ai = NULL;
|
|
static int sockfd = -1;
|
|
static int connfd = -1;
|
|
static char* pname;
|
|
|
|
struct Ship {
|
|
int shipsize;
|
|
int hitcount;
|
|
int isDestroyed;
|
|
};
|
|
|
|
struct Tile {
|
|
int isHit;
|
|
struct Ship *here;
|
|
};
|
|
|
|
static struct Tile field[MAP_SIZE][MAP_SIZE];
|
|
static struct Ship *ships[6];
|
|
|
|
volatile sig_atomic_t quit = 0;
|
|
|
|
/**
|
|
* @details Signal handler.
|
|
* @param signal The number of the received signal.
|
|
* @return This function has no return value.
|
|
*/
|
|
static void handle_signal(int signal)
|
|
{
|
|
quit = 1;
|
|
}
|
|
|
|
static void usage(void)
|
|
{
|
|
fprintf(stderr, "[%s] Usage:\n\tSYNOPSIS\n\t\tserver [-p PORT] SHIP1...\n\tEXAMPLE\n\tserver -p 1280 C2E2 F0H0 B6A6 E8E6 I2I5 H8I8\n", pname);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
static void cleanup(void)
|
|
{
|
|
for (int i = 0; i < (int) sizeof(ships); i++) {
|
|
free(ships[i]);
|
|
}
|
|
|
|
close(sockfd);
|
|
free(ai);
|
|
}
|
|
|
|
static struct Ship * addShip(char *coords)
|
|
{
|
|
if (strlen(coords) != 4) {
|
|
fprintf(stderr, "[%s] ERROR: wrong syntax for ship coordinates: %s\n", pname, coords);
|
|
cleanup();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int x1 = coords[0] - 65;
|
|
int x2 = coords[1] - 48;
|
|
int y1 = coords[2] - 65;
|
|
int y2 = coords[3] - 48;
|
|
if ((x1 < 0 || x1 > 9) || (x2 < 0 || x2 > 9)
|
|
|| (y1 < 0 || y1 > 9) || (y2 < 0 || y2 > 9)) {
|
|
fprintf(stderr, "[%s] ERROR: coordinates outside of map: %s.\n", pname, coords);
|
|
cleanup();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int shipsize = 0;
|
|
if (x1 == y1) {
|
|
shipsize = (MAX(x2, y2)) - (MIN(x2, y2)) + 1;
|
|
} else if (x2 == y2) {
|
|
shipsize = (MAX(x1, y1)) - (MIN(x1, y1)) + 1;
|
|
} else {
|
|
fprintf(stderr, "[%s] ERROR: ships must be aligned either horizontally or vertically: %s\n", pname, coords);
|
|
cleanup();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
struct Ship *newShip = malloc(sizeof(struct Ship));
|
|
newShip->shipsize = shipsize;
|
|
newShip->hitcount = 0;
|
|
newShip->isDestroyed = 0;
|
|
struct Tile newShipTile = {0, newShip};
|
|
|
|
for (int x = (MIN(x1, y1)); x <= (MAX(x1, y1)); x++) {
|
|
for (int y = (MIN(x2, y2)); y <= (MAX(x2, y2)); y++) {
|
|
field[x][y] = newShipTile;
|
|
}
|
|
}
|
|
|
|
return newShip;
|
|
}
|
|
|
|
static void parse(int argc, char *argv[])
|
|
{
|
|
if (argc == 1)
|
|
usage();
|
|
|
|
int opt_ind = 0;
|
|
int pflag = 0;
|
|
|
|
while ((opt_ind = getopt(argc, argv, "p:")) != -1) {
|
|
switch (opt_ind) {
|
|
case 'p':
|
|
if (pflag != 0) {
|
|
usage();
|
|
} else {
|
|
port = optarg;
|
|
pflag = 1;
|
|
}
|
|
break;
|
|
|
|
case '?':
|
|
usage();
|
|
|
|
default:
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
if ((argc - optind) != 6)
|
|
usage();
|
|
|
|
for (int i = optind, c = 0; i < argc; i++, c++) {
|
|
ships[c] = addShip(argv[i]);
|
|
}
|
|
}
|
|
|
|
static int checkWin(void)
|
|
{
|
|
int counter = 0;
|
|
|
|
for (int i = 0; i < (int) (sizeof(ships) / sizeof(ships[0])); i++) {
|
|
if (ships[i]->isDestroyed)
|
|
counter++;
|
|
}
|
|
|
|
if (counter == (int) (sizeof(ships) / sizeof(ships[0])))
|
|
return 1; // all ships sunk
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int makeHit(uint16_t msg)
|
|
{
|
|
uint8_t y = msg & 0x003F;
|
|
uint8_t x = msg >> 6;
|
|
x &= 0x003F;
|
|
|
|
if (field[x][y].isHit) {
|
|
if (field[x][y].here == NULL)
|
|
return 0; // no ship at position
|
|
if (field[x][y].here->isDestroyed == 1)
|
|
return 0; // ship already destroyed
|
|
return 1; // tile with ship hit again
|
|
}
|
|
|
|
field[x][y].isHit = 1;
|
|
|
|
if (field[x][y].here == NULL) {
|
|
return 0; // missed shot
|
|
}
|
|
|
|
field[x][y].here->hitcount += 1;
|
|
|
|
if (field[x][y].here->hitcount == field[x][y].here->shipsize) {
|
|
field[x][y].here->isDestroyed = 1;
|
|
if (checkWin()) // return == 1 (game won)
|
|
return 3;
|
|
return 2; // checkWin() == 0 (ship destroyed but still running)
|
|
}
|
|
|
|
return 1; // ship hit but not destroyed
|
|
}
|
|
|
|
static int checkParity(uint16_t msg)
|
|
{
|
|
uint16_t v = msg;
|
|
v &= 0x7FF;
|
|
uint8_t parity = 0;
|
|
|
|
while (v) {
|
|
parity = !parity;
|
|
v = v & (v - 1);
|
|
}
|
|
uint16_t msb = msg;
|
|
msb &= 0x8000;
|
|
msb >>= 15;
|
|
if (parity == msb) {
|
|
return 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
static int checkCoords(uint16_t msg)
|
|
{
|
|
uint8_t y = msg & 0x003F;
|
|
uint8_t x = msg >> 6;
|
|
x &= 0x003F;
|
|
|
|
if ((x > 9) || (y > 9))
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void printMap(void)
|
|
{
|
|
for (int y = 0; y < MAP_SIZE; y++) {
|
|
for (int x = 0; x < MAP_SIZE; x++) {
|
|
fprintf(stdout, "%d\t", field[x][y].isHit);
|
|
}
|
|
fprintf(stdout, "\n");
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = handle_signal;
|
|
sigaction(SIGINT, &sa, NULL);
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
pname = argv[0];
|
|
|
|
parse(argc, argv);
|
|
|
|
struct addrinfo hints;
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_INET;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
int res = getaddrinfo(NULL, port, &hints, &ai);
|
|
if (res < 0) {
|
|
fprintf(stderr, "[%s] ERROR: Failed to get address info: %s\n", pname, gai_strerror(res));
|
|
cleanup();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
if (sockfd < 0) {
|
|
perror(pname);
|
|
cleanup();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int val = 1;
|
|
res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
|
if (res < 0) {
|
|
cleanup();
|
|
perror(pname);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
res = bind(sockfd, ai->ai_addr, ai->ai_addrlen);
|
|
if (res < 0) {
|
|
cleanup();
|
|
perror(pname);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
res = listen(sockfd, 1);
|
|
if (res < 0) {
|
|
cleanup();
|
|
perror(pname);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
connfd = accept(sockfd, NULL, NULL);
|
|
if (connfd < 0) {
|
|
cleanup();
|
|
perror(pname);
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
int round = 0;
|
|
|
|
while (quit == 0) {
|
|
uint8_t buffer[1];
|
|
if (round == 80) {
|
|
fprintf(stdout, "[%s] Game lost\n", pname);
|
|
buffer[0] = 0x04;
|
|
send(connfd, buffer, 1, 0);
|
|
cleanup();
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
round++;
|
|
|
|
recv(connfd, buffer, 1, 0);
|
|
uint16_t msg = buffer[0];
|
|
recv(connfd, buffer, 1, 0);
|
|
msg += 256U*buffer[0];
|
|
if (checkParity(msg) == 1) {
|
|
fprintf(stderr, "[%s] Parity error\n", pname);
|
|
buffer[0] = 0x08;
|
|
send(connfd, buffer, 1, 0);
|
|
cleanup();
|
|
exit(2);
|
|
}
|
|
if (checkCoords(msg) == 1) {
|
|
fprintf(stderr, "[%s] Invalid coordinate\n", pname);
|
|
buffer[0] = 0x0C;
|
|
send(connfd, buffer, 1, 0);
|
|
cleanup();
|
|
exit(3);
|
|
}
|
|
|
|
int hit = makeHit(msg);
|
|
if (hit == 0) {
|
|
buffer[0] = 0x00;
|
|
send(connfd, buffer, 1, 0);
|
|
} else if (hit == 1) {
|
|
buffer[0] = 0x01;
|
|
send(connfd, buffer, 1, 0);
|
|
} else if (hit == 2) {
|
|
buffer[0] = 0x02;
|
|
send(connfd, buffer, 1, 0);
|
|
} else {
|
|
fprintf(stdout, "[%s] Client wins in %d rounds\n", pname, round);
|
|
buffer[0] = 0x07;
|
|
send(connfd, buffer, 1, 0);
|
|
cleanup();
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
}
|
|
|
|
cleanup();
|
|
exit(EXIT_SUCCESS);
|
|
}
|