Code:
/*
* socket.c - routines to handle all socket matters for Phantasia
*/
#include "include.h"
extern void *Do_start_thread(void *c);
extern server_hook;
extern randData;
/************************************************************************
/
/ FUNCTION NAME: Do_initialize_socket(struct server_t *server)
/
/ FUNCTION: To initialize the program's socket
/
/ AUTHOR: Brian Kelly, 4/12/99
/
/ ARGUMENTS:
/ struct server_t *s - address of the server's main data strcture
/
/ RETURN VALUE:
/
/ MODULES CALLED: wmove(), _filbuf(), clearok(), waddstr(), wrefresh(),
/ wclrtoeol()
/
/ DESCRIPTION:
/ Read a string from the keyboard.
/ This routine is specially designed to:
/
/ - strip non-printing characters (unless Wizard)
/ - echo, if desired
/ - redraw the screen if CH_REDRAW is entered
/ - read in only 'mx - 1' characters or less characters
/ - nul-terminate string, and throw away newline
/
/ 'mx' is assumed to be at least 2.
/
*************************************************************************/
int Do_init_server_socket()
{
struct sockaddr_in bind_address;
char error_msg[SZ_ERROR_MESSAGE];
int the_socket, error, on=1;
/* create a socket */
errno = 0;
if ((the_socket=socket(AF_INET, SOCK_STREAM, 0)) == -1) {
sprintf(error_msg,
"[0.0.0.0:?] Socket creation failed in Do_init_server_socket: %s\n",
strerror(errno));
Do_log_error(error_msg);
exit(SOCKET_CREATE_ERROR);
}
error = setsockopt(the_socket, SOL_SOCKET, SO_REUSEADDR,
(char *) &on, sizeof(on));
if (error != 0) {
sprintf(error_msg, "[0.0.0.0:?] setsockopt failed with error code of %d in Do_init_server_socket.\n", error);
Do_log_error(error_msg);
exit(SOCKET_CREATE_ERROR);
}
/* set up the bind address */
bind_address.sin_family = AF_INET;
bind_address.sin_addr.s_addr = INADDR_ANY;
bind_address.sin_port = PHANTASIA_PORT;
/* bind to that socket */
error = bind(the_socket, (struct sockaddr *) &bind_address,
sizeof(bind_address));
if (error != 0) {
sprintf(error_msg, "[0.0.0.0:?] bind to socket failed with error code of %d in Do_init_server_socket.\n", error);
Do_log_error(error_msg);
exit(SOCKET_BIND_ERROR);
}
/* start listening on the socket */
error = listen(the_socket, SOMAXCONN);
if (error != 0) {
sprintf(error_msg, "[0.0.0.0:?] listen command failed with error code of %d in Do_init_server_socket\n", error);
Do_log_error(error_msg);
exit(SOCKET_LISTEN_ERROR);
}
if (error = fcntl(the_socket, F_SETOWN, getpid()) < 0) {
sprintf(error_msg, "[0.0.0.0:?] fcntl F_SETOWN failed with error code of %d in Do_init_server_socket.\n", error);
Do_log_error(error_msg);
exit(SOCKET_BIND_ERROR);
}
if (error = fcntl(the_socket, F_SETFL, FASYNC) < 0) {
sprintf(error_msg, "[0.0.0.0:?] fcntl F_SETFL failed with error code of %d in Do_init_server_socket.\n", error);
Do_log_error(error_msg);
exit(SOCKET_BIND_ERROR);
}
return the_socket; /* no problems */
}
/************************************************************************
/
/ FUNCTION NAME: Do_accept_connections(struct server_t *s)
/
/ FUNCTION: Create new games for new connections on the socket
/
/ AUTHOR: Brian Kelly, 4/12/99
/
/ ARGUMENTS:
/ struct server_t *s - address of the server's main data strcture
/
/ RETURN VALUE: int error
/
/ MODULES CALLED: wmove(), _filbuf(), clearok(), waddstr(), wrefresh(),
/ wclrtoeol()
/
/ DESCRIPTION:
/ Read a string from the keyboard.
/ This routine is specially designed to:
/
/ - strip non-printing characters (unless Wizard)
/ - echo, if desired
/ - redraw the screen if CH_REDRAW is entered
/ - read in only 'mx - 1' characters or less characters
/ - nul-terminate string, and throw away newline
/
/ 'mx' is assumed to be at least 2.
/
*************************************************************************/
int Do_accept_connections(struct server_t *s)
{
struct game_t *game_ptr;
struct client_t *client_ptr;
pthread_attr_t thread_attr;
size_t addrlen;
char error_msg[SZ_ERROR_MESSAGE];
int theError, on=1, terms, itemp;
char gethost_buffer[16384], *string_ptr, *string_ptr_two;
struct hostent *host_info, host_buffer;
struct in_addr theNetwork;
while (Do_check_socket(s->the_socket)) {
/* on a new connection, seed the random number generator */
srandom_r (time(NULL), (struct random_data *)&randData);