--- deliantra/server/socket/init.C 2006/12/14 02:37:37 1.15 +++ deliantra/server/socket/init.C 2007/01/23 01:05:18 1.37 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2001 Mark Wedel - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The author can be reached via e-mail to -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2001 Mark Wedel + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The author can be reached via e-mail to + */ /** * \file @@ -32,205 +33,202 @@ * The reading of data is handled in ericserver.c */ - #include #include #include #include #include #include +#include +#include #include -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef HAVE_ARPA_INET_H -# include -#endif -#include +#include +#include -Socket_Info socket_info; -client_socket *init_sockets; - -client_socket::client_socket () -{ -} +#include -client_socket::~client_socket () -{ -} +sockvec clients; /** * Initializes a connection. Really, it just sets up the data structure, * socket setup is handled elsewhere. We do send a version to the * client. */ -void -client_socket::init (const char *from_ip) +client::client (int fd, const char *peername) +: fd (fd), host (strdup (peername)), + socket_ev (this, &client::socket_cb), + cmd_ev (this, &client::cmd_cb), + cc_inv (this), cc_other (this) { - int bufsize = 65535; /*Supposed absolute upper limit */ - int oldbufsize; - int buflen = sizeof (int); - - if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1) - LOG (llevError, "InitConnection: Error on fcntl.\n"); - - if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) - oldbufsize = 0; - - if (oldbufsize < bufsize) - if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof (&bufsize))) - LOG (llevError, "InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); - - buflen = sizeof (oldbufsize); - getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); -#ifdef ESRV_DEBUG - LOG (llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); + refcnt_inc (); // the socket is an external reference + + { + struct linger linger_opt; + + linger_opt.l_onoff = 0; + linger_opt.l_linger = 0; + + if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) + LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); + } + + { + int val = IPTOS_LOWDELAY; + + if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) + LOG (llevError, "IP_TOS: %s\n", strerror (errno)); + } + + { + int val = 1; + + if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) + LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); + } + +#ifdef __linux__ + { + int val; + + // set some very aggressive keepalive parameters + val = 10; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); + + val = 5; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); + + val = 1; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPINTVL: %s\n", strerror (errno)); + } #endif - faceset = 0; - facecache = 0; - image2 = 0; - sound = 0; - exp64 = 0; - monitor_spells = 0; + { + int bufsize = 65535; /*Supposed absolute upper limit */ + int oldbufsize; + int buflen = sizeof (int); + + if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1) + LOG (llevError, "InitConnection: Error on fcntl.\n"); + + if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) + oldbufsize = 0; + + if (oldbufsize < bufsize) + if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof (&bufsize))) + LOG (llevError, "InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); + + buflen = sizeof (oldbufsize); + getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); + } + + state = ST_SETUP; mapmode = Map0Cmd; darkness = 1; - status = Ns_Add; mapx = 11; mapy = 11; - newmapcmd = 0; itemcmd = 1; /* Default is version item1 command */ - ext_mapinfos = 0; /*extendedmapinfo datas */ - EMI_smooth = 0; - look_position = 0; - update_look = 0; - has_readable_type = 0; - supported_readables = 0; - monitor_spells = 0; - mapinfocmd = 0; - extcmd = 0; - extmap = 0; - - /* we should really do some checking here - if total clients overflows - * we need to do something more intelligent, because client id's will start - * duplicating (not likely in normal cases, but malicous attacks that - * just open and close connections could get this total up. - */ - inbuf_len = 0; - /* Basic initialization. Needed because we do a check in - * HandleClient for oldsocketmode without checking the - * length of data. - */ - memset (&lastmap, 0, sizeof (struct Map)); - memset (faces_sent, 0, faces_sent_len * sizeof (*faces_sent)); - memset (&anims_sent, 0, sizeof (anims_sent)); - memset (&stats, 0, sizeof (struct statsinfo)); + faces_sent = salloc0 (nrofpixmaps); + /* Do this so we don't send a face command for the client for * this face. Face 0 is sent to the client to say clear * face information. */ faces_sent[0] = NS_FACESENT_FACE; - outputbuffer.start = 0; - outputbuffer.len = 0; - can_write = 1; - password_fails = 0; - - current_map = 0; - current_x = 0; - current_y = 0; - client[0] = 0; - buggy_mapscroll = 0; + socket_ev.prio (PE_PRIO_NORMAL); + socket_ev.fd (fd); + socket_ev.poll (PE_R); - sent_scroll = 0; - host = strdup_local (from_ip); + cmd_ev.prio (PE_PRIO_NORMAL); - { - packet sl; - sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); - send_packet (sl); - } + // initialisation done, kick it! + send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); + flush (); + + reset_stats (); - if (socket_info.nconns > cst_tot.max_conn) - cst_tot.max_conn = socket_info.nconns; - if (socket_info.nconns > cst_lst.max_conn) - cst_lst.max_conn = socket_info.nconns; + clients.insert (this); } +client::~client () +{ + clients.erase (this); + + sfree (faces_sent, nrofpixmaps); + free (stats.range); + free (stats.title); + free (host); +} -/** This sets up the socket and reads all the image information into memory. */ void -init_ericserver (void) +client::do_destroy () { - struct sockaddr_in insock; - struct protoent *protox; - struct linger linger_opt; - - socket_info.max_filedescriptor = sysconf (_SC_OPEN_MAX); - - socket_info.timeout.tv_sec = 0; - socket_info.timeout.tv_usec = 0; - socket_info.nconns = 0; + attachable::do_destroy (); - memset (&cst_tot, 0, sizeof (CS_Stats)); - memset (&cst_lst, 0, sizeof (CS_Stats)); - cst_tot.time_start = time (NULL); - cst_lst.time_start = time (NULL); + if (pl) + pl->disconnect (); - LOG (llevDebug, "Initialize new client/server data\n"); - socket_info.nconns = 1; - init_sockets = (client_socket *) malloc (sizeof (client_socket)); - init_sockets[0].faces_sent = NULL; /* unused */ - socket_info.allocated_sockets = 1; - - protox = getprotobyname ("tcp"); - if (protox == NULL) + if (fd >= 0) { - LOG (llevError, "init_ericserver: Error getting protox\n"); - return; - } + send_packet ("goodbye"); + flush (); - init_sockets[0].fd = socket (PF_INET, SOCK_STREAM, protox->p_proto); - if (init_sockets[0].fd == -1) - { - LOG (llevError, "Cannot create socket: %s\n", strerror (errno)); - exit (-1); + close (fd); } - insock.sin_family = AF_INET; - insock.sin_port = htons (settings.csport); - insock.sin_addr.s_addr = htonl (INADDR_ANY); - - linger_opt.l_onoff = 0; - linger_opt.l_linger = 0; - if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_LINGER, (char *) &linger_opt, sizeof (struct linger))) - LOG (llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror (errno)); + state = ST_DEAD; - { - int tmp = 1; + socket_ev.suspend (); + cmd_ev.suspend (); - if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp))) - LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); - } + refcnt_dec (); // socket no longer open +} - if (bind (init_sockets[0].fd, (struct sockaddr *) &insock, sizeof (insock)) == (-1)) - { - LOG (llevError, "Cannot bind socket to port %d: %s\n", ntohs (insock.sin_port), strerror (errno)); - close (init_sockets[0].fd); - exit (-1); - } +void +client::reset_stats () +{ + /* we need to clear these to -1 and not zero - otherwise, + * if a player quits and starts a new character, we wont + * send new values to the client, as things like exp start + * at zero. + */ + for (int i = 0; i < NUM_SKILLS; i++) + last_skill_exp[i] = -1; - if (listen (init_sockets[0].fd, 5) == (-1)) - { - LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno)); - close (init_sockets[0].fd); - exit (-1); - } + for (int i = 0; i < NROFATTACKS; i++) + last_resist[i] = -1; + + last_weapon_sp = -1; + last_level = -1; + last_stats.exp = -1; + last_weight = (uint32) - 1; + last_flags = 0; + last_weight = 0; + last_weight_limit = 0; + last_path_attuned = 0; + last_path_repelled = 0; + last_path_denied = 0; + last_speed = 0; + flags = 0; + + static living zero_living; + last_stats = zero_living; +} + +/** This sets up the socket and reads all the image information into memory. */ +void +init_ericserver (void) +{ + cst_tot.time_start = time (NULL); + cst_lst.time_start = time (NULL); + + LOG (llevDebug, "Initialize new client/server data\n"); - init_sockets[0].status = Ns_Add; read_client_images (); } @@ -247,36 +245,25 @@ { LOG (llevDebug, "Freeing all new client/server information.\n"); free_socket_images (); - free (init_sockets); } -/** - * Frees a socket. - * Basically, all we need to do here is free all data structures that - * might be associated with the socket. It is up to the caller to - * update the list - */ - -void -free_newsocket (client_socket *ns) +client * +client::create (int fd, const char *peername) { - close (ns->fd); - - if (ns->stats.range) - free (ns->stats.range); + if (checkbanned (NULL, peername)) + { + LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); + return 0; + } + else + { + client *ns = new client (dup (fd), peername); - if (ns->stats.title) - free (ns->stats.title); + ns->instantiate (); // effectively a nop right now - free (ns->host); -} + INVOKE_CLIENT (CONNECT, ns); -/** Sends the 'goodbye' command to the player, and closes connection. */ -void -final_free_player (player *pl) -{ - pl->socket.send_packet ("goodbye"); - free_newsocket (&pl->socket); - free_player (pl); + return ns; + } }