/** * @file server.c * @author Tobias Eidelpes * @date 2018-03-02 * * @brief Server for OSUE exercise 1B `Battleship'. */ #include "common.h" /** * @details Constant which holds the port number to listen on. */ static const char *port = DEFAULT_PORT; /** * @details Struct which holds info for the socket. */ static struct addrinfo *ai = NULL; /** * @details Global variable for the socket file descriptor (easy cleanup). */ static int sockfd = -1; /** * @details Global variable for the connection file descriptor (easy cleanup). */ static int connfd = -1; /** * @details Global variable for the program name for error messages. */ static char* pname; /** * @details Struct for storing ships. */ struct Ship { int shipsize; int hitcount; int isDestroyed; }; /** * @details Struct which points either to nil or a ship. */ struct Tile { int isHit; struct Ship *here; }; /** * @details Global array stores tiles which point to ships. */ static struct Tile field[MAP_SIZE][MAP_SIZE]; /** * @details Separate list of ships for easy traversing and cleanup. */ static struct Ship *ships[6]; /** * @details Variable which gets set on signal received. */ 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; } /** * @details Prints the program usage. * @param void Function takes no arguments. * @return Always non-zero. */ 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); } /** * @details Function for convenient cleanup of resources. * @param void Function takes no arguments. * @return 0 on success. Non-zero on failure. */ static int cleanup(void) { for (int i = 0; i < (int) sizeof(ships); i++) { free(ships[i]); } if (close(sockfd) < 0) { perror(pname); return 1; } free(ai); return 0; } /** * @details Tests and adds a ship from the given coordinates to the ship array. * @param coords The coordinates for the new ship. * @return Returns the ship that has been added on success or exits the program. */ 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; } /** * @details Parses the commandline arguments. * @param argc The count of commandline arguments supplied. * @param argv The array holding the commandline arguments. * @return Calls the usage() function on error. */ 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]); } // check if ships have correct length int countLength2 = 0; int countLength3 = 0; int countLength4 = 0; for (int i = 0; i < 6; i++) { if (ships[i]->shipsize == 2) { countLength2++; } else if (ships[i]->shipsize == 3) { countLength3++; } else if (ships[i]->shipsize == 4) { countLength4++; } else { fprintf(stderr, "[%s]: Ship must be 2, 3 or 4 tiles big!\n", pname); cleanup(); usage(); } } if ((countLength2 != SHIP_CNT_LEN2) || (countLength3 != SHIP_CNT_LEN3) || (countLength4 != SHIP_CNT_LEN4)) { fprintf(stderr, "[%s]: There must be exactly 2 ships of length 2, 3 ships of length 3 and 1 ship of length 4!\n", pname); cleanup(); usage(); } } /** * @details Check whether the player has won the game. * @param void Function takes no parameters. * @return 0 on not won and 1 when game won. */ 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; } /** * @details Registers a hit on the internal game board. * @param msg The encoded x and y coordinates of the hit. * @return 0 on missed shot. 1 on ship hit but not destroyed. 2 on ship destroyed * but game still running. 3 on ship destroyed and all sunk. */ 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 } /** * @details Calculates even parity for the received encoded coordinates. * @param msg The received coordinates encoded in 2 bytes. * @return 0 if parity OK, otherwise 1. */ 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; } } /** * @details Checks if the coordinates are valid. * @param msg The received coordinates encoded in 2 bytes. * @return 0 on success. 1 on failure. */ 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; } /** * The main entry point of the program. * @details Signal handler is set up. The commandline arguments are parsed. * A socket gets created and listens for connections. Starts the game * and finishes it appropriately. * @param argc The count of commandline arguments. * @param argv The array of commandline arguments. * @return 0 on success. Non-zero on failure. */ 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 == MAX_ROUNDS) { 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); }