X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-server.c;h=5d951fd40d5843430d89937687285c431ff90223;hp=d10d442e621f4b024c9e5cc31f6e8ed4f10d77ce;hb=5e8532a5e248965f2ff3cfc01e9dcc307fe5825a;hpb=49f7f9e3dc198c3593021bff5514f19090a4834c diff --git a/src/ttt-server.c b/src/ttt-server.c index d10d442..5d951fd 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -1,4 +1,4 @@ -/* ttt.c - client-server tic-tac-toe game +/* ttt-server.c - tic-tac-toe game server * * Copyright © 2005 Carl Worth * @@ -19,157 +19,309 @@ * Author: Carl Worth */ -#include "ttt.h" +#include "ttt-server.h" -static ttt_status_t -_sockaddr_init (struct sockaddr_in *addr, - const char *host, - const char *port) +#include "ttt-args.h" +#include "ttt-client.h" +#include "ttt-error.h" +#include "ttt-socket.h" + +struct _ttt_server { + pthread_mutex_t mutex; + + const char *host; + const char *port; + + ttt_client_t **clients; + int clients_size; + int num_clients; +}; + +static void +ttt_server_init (ttt_server_t *server, const char *host, const char *port) { - struct hostent *hostent; - struct servent *servent; - char *endptr; - long port_long; - uint16_t port_short; - - addr->sin_family = AF_INET; - - hostent = gethostbyname (host); - if (hostent == NULL) - { - fprintf (stderr, "Error: Lookup failed for host %s: %s\n", - host, hstrerror (h_errno)); - return TTT_STATUS_FAILURE; - } - memcpy (&addr->sin_addr.s_addr, hostent->h_addr_list[0], - sizeof (addr->sin_addr.s_addr)); - - assert (*port != '\0'); - port_long = strtol (port, &endptr, 10); - if (*endptr == '\0') - { - /* Numeric port*/ - if (port_long <= 0 || port_long >= (1 << 16)) { - fprintf (stderr, "Error: Port %ld out of range.\n", port_long); - return TTT_STATUS_FAILURE; - } - port_short = port_long; - addr->sin_port = htons (port_short); - } else { - /* Named port */ - servent = getservbyname (port, "tcp"); - if (port == NULL) { - fprintf (stderr, "Error: Lookup failed for port %s: %s\n", - port, hstrerror (h_errno)); - return TTT_STATUS_FAILURE; + pthread_mutex_init (&server->mutex, NULL); + + server->host = host; + server->port = port; + + server->clients = NULL; + server->clients_size = 0; + server->num_clients = 0; +} + +/* Exported: See ttt-server.h for documentation. */ +ttt_error_t +ttt_server_register_client (ttt_server_t *server, ttt_client_t *client) +{ + int i; + ttt_error_t error = TTT_ERROR_NONE; + const char *username; + + pthread_mutex_lock (&server->mutex); + + username = ttt_client_get_username (client); + + assert (username != NULL); + + if (username[0] == '\0') + return TTT_ERROR_INVALID_NAME; + + for (i = 0; i < server->num_clients; i++) { + if (strcmp (ttt_client_get_username (server->clients[i]), username) == 0) { + error = TTT_ERROR_INVALID_NAME; + goto CLEANUP_LOCK; } - addr->sin_port = servent->s_port; } - return TTT_STATUS_SUCCESS; + printf ("Client %s has joined.\r\n", username); + + server->num_clients++; + + if (server->num_clients > server->clients_size) { + if (server->clients_size == 0) + server->clients_size = 1; + else + server->clients_size *= 2; + + server->clients = xrealloc (server->clients, + server->clients_size * sizeof (ttt_client_t *)); + } + + server->clients [server->num_clients - 1] = client; + + CLEANUP_LOCK: + pthread_mutex_unlock (&server->mutex); + + return error; +} + +/* Exported: See ttt-server.h for documentation. */ +void +ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client) +{ + int i; + + pthread_mutex_lock (&server->mutex); + + for (i = 0; i < server->num_clients; i++) + if (server->clients[i] == client) + break; + + assert (i < server->num_clients); + + printf ("Client %s has left.\r\n", ttt_client_get_username (client)); + + memmove (&server->clients[i], &server->clients[i+1], + (server->num_clients - i - 1) * sizeof (ttt_client_t *)); + + server->num_clients--; + + pthread_mutex_unlock (&server->mutex); } -static int -_wait_for_connection (int listen_socket) +/* Exported: See ttt-server.h for documentation. */ +void +ttt_server_broadcast (ttt_server_t *server, const char *message) { - fd_set read_set; - int num_selected; - int flags, err; - int connected_socket; + int i; + + pthread_mutex_lock (&server->mutex); - FD_ZERO (&read_set); - FD_SET (listen_socket, &read_set); + for (i = 0; i < server->num_clients; i++) + ttt_client_send (server->clients[i], message); - while (1) { - num_selected = select (listen_socket + 1, - &read_set, NULL, NULL, - NULL); + pthread_mutex_unlock (&server->mutex); +} + +/* Exported: See ttt-server.h for documentation. */ +const char* +ttt_server_who (ttt_server_t *server) +{ + int i; + char *response; - flags = fcntl (listen_socket, F_GETFL); - flags |= O_NONBLOCK; - xfcntl (listen_socket, F_SETFL, flags); + pthread_mutex_lock (&server->mutex); - connected_socket = accept (listen_socket, 0, 0); - err = errno; + xasprintf (&response, "WHO"); - flags &= ~O_NONBLOCK; - xfcntl (listen_socket, F_SETFL, flags); + for (i = 0; i < server->num_clients; i++) + xasprintf (&response, "%s %s", + response, + ttt_client_get_username (server->clients[i])); - if (connected_socket != -1) - return connected_socket; + xasprintf (&response, "%s\r\n", response); - if (err == EWOULDBLOCK || err == EAGAIN) - continue; + pthread_mutex_unlock (&server->mutex); + + return response; +} - fprintf (stderr, "Error: accept failed: %s. Aborting.\n", - strerror (err)); - exit (1); +/* Exported: See ttt-server.h for documentation. */ +ttt_error_t +ttt_server_statistics (ttt_server_t *server, const char *username, char **response) +{ + ttt_bool_t usernamefound = FALSE; + const char *client_username; + int client_num_wins; + int i; + + pthread_mutex_lock (&server->mutex); + + for (i = 0; i < server->num_clients; i++) { + client_username = ttt_client_get_username (server->clients[i]); + if (strcasecmp (username, client_username) == 0) { + usernamefound = TRUE; + client_num_wins = ttt_client_get_num_wins (server->clients[i]); + xasprintf (response, "STATISTICS %s \"\r\n" + "TICTACTOE WINS %d\r\n\"\r\n", + client_username, + client_num_wins); + } + } + + pthread_mutex_unlock (&server->mutex); + + if (!usernamefound) + return TTT_ERROR_NO_USER; + + return TTT_ERROR_NONE; +} + +/* Exported: See ttt-server.h for documentation. */ +ttt_error_t +ttt_server_verify_username (ttt_server_t *server, const char *username) +{ + ttt_bool_t usernamefound = FALSE; + const char *client_username; + int i; + + pthread_mutex_lock (&server->mutex); + + for (i = 0; i < server->num_clients; i++) { + client_username = ttt_client_get_username (server->clients[i]); + if (strcasecmp (username, client_username) == 0) + usernamefound = TRUE; } + + pthread_mutex_unlock (&server->mutex); + + if (!usernamefound) + return TTT_ERROR_NO_USER; + + return TTT_ERROR_NONE; +} + +/* Exported: See ttt-server.h for documentation. */ +const char* +ttt_server_get_host (ttt_server_t *server) +{ + return server->host; +} + +/* Exported: See ttt-server.h for documentation. */ +const char* +ttt_server_get_port (ttt_server_t *server) +{ + return server->port; } static const char *WELCOME_MESSAGE = -"Welcome to ttt-server. So far, this program is simply a demonstration\n" -"of a one-shot TCP/IP server. The server is currently listening on:\n" -"\n %s:%s\n" -"\nTo test this program, simply connect a client to that host and port.\n" -"For example:\n" -"\n telnet %s %s\n" -"\nOnce you have connected a client, the server will echo any characters\n" -"it receives back to the client as well as to stdout.\n" -"\nNote that to terminate the telnet client you type Control-], then\n" -", then \"close\" (and ) at the \"telnet> \" prompt.\n" -"\nHave fun!\n" -"-Carl\n" -"\nPS. By one-shot, I mean that the server will only accept a connection\n" -"from a single client, and the server will exit when that client disconnects\n" -"To test the server again, you will need to manually restart it again.\n" -"Extending the server to start listening again, or to handle multiple\n" -"clients simultaneously would both be fun projects for a motivated\n" -"student (as would writing a custom client program).\n\n"; +"Welcome to ttt-server. The server is currently listening on:\r\n" +"\r\n" +" %s:%s\r\n" +"\r\n" +"To test this, simply connect one or more clients to that host and port.\r\n" +"For example:\r\n" +"\r\n" +" telnet %s %s\r\n" +"\r\n" +"The TTTP (tic-tac-toe protocol) has been partially implemented.\r\n" +"The following commands should work: HELO, HELP, MESSAGE, VERSION, QUIT, WHO.\r\n" +"\r\n"; + +static void +_ttt_server_accept (void *closure, int client_socket) +{ + ttt_server_t *server = closure; + + ttt_client_new (server, client_socket); +} + +static void +_detach_and_write_child_pid_to (const char *filename) +{ + pid_t pid; + + /* Use the Unix double-fork trick to detach completely. See + * setsid(2) for some details as to why two forks are + * needed. */ + pid = xfork (); + if (pid) { + /* First parent just exits */ + exit (0); + } + + chdir ("/"); + setsid (); + + pid = xfork (); + if (pid) { + /* Second parent exits after writing pid */ + FILE *file = xfopen (filename, "w"); + fprintf (file, "%d\n", pid); + fclose (file); + exit (0); + } + + /* Final, detached child returns. */ +} int main (int argc, char **argv) { ttt_args_t args; - int listen_socket, connected_socket; - struct sockaddr_in addr; + ttt_server_t server; + int socket; ttt_args_parse (&args, argc, argv); - if (args.log_file) - stderr = xfreopen (args.log_file, "a", stderr); - - listen_socket = xsocket (PF_INET, SOCK_STREAM, 0); - - _sockaddr_init (&addr, args.host, args.port); - -#ifdef SO_REUSEADDR - { - int one = 1; - setsockopt (listen_socket, SOL_SOCKET, - SO_REUSEADDR, (char *) &one, sizeof (int)); + if (args.log_file || args.detach) { + FILE *log_file; + /* In the detach case, we force redirection to a log file. */ + if (args.log_file == NULL) + args.log_file = "/var/log/ttt-server.log"; + log_file = fopen (args.log_file, "a"); + if (log_file == NULL) { + printf ("Warning: Failed to open log file %s: %s.\n", + args.log_file, strerror (errno)); + printf ("Logging will be disabled.\n"); + xdup2 (1, 2); + } else { + xdup2 (fileno (log_file), 2); + } } -#endif - xbind (listen_socket, (struct sockaddr *) &addr, sizeof (addr)); + if (args.detach) + _detach_and_write_child_pid_to (args.pid_file); - xlisten (listen_socket, SOMAXCONN); + socket = ttt_socket_create_server (args.host, args.port); - printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port); + if (args.detach) { + xdup2 (2, 1); + printf ("Server started listening on %s:%s\n", args.host, args.port); + } else { + printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port); + } - connected_socket = _wait_for_connection (listen_socket); + fclose (stdout); + fclose (stdin); -#define BUF_SIZE 1024 + ttt_server_init (&server, args.host, args.port); - while (1) { - char buf[BUF_SIZE]; - int cnt; - cnt = read (connected_socket, buf, BUF_SIZE); - if (cnt == 0) - break; - write (0, buf, cnt); - write (connected_socket, buf, cnt); - } + while (1) + ttt_socket_accept (socket, _ttt_server_accept, &server); - return 0; + /* We only reach here if something bad happened. */ + return 1; }