/* 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 */ /** * \file * Socket general functions * * \date 2003-12-02 * * Mainly deals with initialization and higher level socket * maintenance (checking for lost connections and if data has arrived.) * The reading of data is handled in ericserver.c */ #include #include #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_ARPA_INET_H # include #endif #include Socket_Info socket_info; NewSocket *init_sockets; /** * 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 InitConnection (NewSocket *ns, const char *from_ip) { int bufsize = 65535; /*Supposed absolute upper limit */ int oldbufsize; int buflen = sizeof (int); if (fcntl (ns->fd, F_SETFL, O_NONBLOCK) == -1) LOG (llevError, "InitConnection: Error on fcntl.\n"); if (getsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) oldbufsize = 0; if (oldbufsize < bufsize) if (setsockopt (ns->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 (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); #ifdef ESRV_DEBUG LOG (llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); #endif ns->faceset = 0; ns->facecache = 0; ns->image2 = 0; ns->sound = 0; ns->exp64 = 0; ns->monitor_spells = 0; ns->mapmode = Map0Cmd; ns->darkness = 1; ns->status = Ns_Add; ns->mapx = 11; ns->mapy = 11; ns->newmapcmd = 0; ns->itemcmd = 1; /* Default is version item1 command */ ns->ext_mapinfos = 0; /*extendedmapinfo datas */ ns->EMI_smooth = 0; ns->look_position = 0; ns->update_look = 0; ns->has_readable_type = 0; ns->supported_readables = 0; ns->monitor_spells = 0; ns->mapinfocmd = 0; ns->extcmd = 0; ns->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. */ ns->inbuf_len = 0; /* Basic initialization. Needed because we do a check in * HandleClient for oldsocketmode without checking the * length of data. */ memset (&ns->lastmap, 0, sizeof (struct Map)); memset (ns->faces_sent, 0, ns->faces_sent_len * sizeof (*ns->faces_sent)); memset (&ns->anims_sent, 0, sizeof (ns->anims_sent)); memset (&ns->stats, 0, sizeof (struct statsinfo)); /* 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. */ ns->faces_sent[0] = NS_FACESENT_FACE; ns->outputbuffer.start = 0; ns->outputbuffer.len = 0; ns->can_write = 1; ns->password_fails = 0; ns->current_map = 0; ns->current_x = 0; ns->current_y = 0; ns->client[0] = 0; ns->buggy_mapscroll = 0; ns->sent_scroll = 0; ns->host = strdup_local (from_ip); { SockList sl (MAXSOCKBUF); sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); Send_With_Handling (ns, &sl); sl.free (); } #ifdef CS_LOGSTATS 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; #endif } /** This sets up the socket and reads all the image information into memory. */ void init_ericserver (void) { 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; #ifdef CS_LOGSTATS 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); #endif LOG (llevDebug, "Initialize new client/server data\n"); socket_info.nconns = 1; init_sockets = (NewSocket *) malloc (sizeof (NewSocket)); init_sockets[0].faces_sent = NULL; /* unused */ socket_info.allocated_sockets = 1; protox = getprotobyname ("tcp"); if (protox == NULL) { LOG (llevError, "init_ericserver: Error getting protox\n"); return; } 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); } 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)); { int tmp = 1; if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp))) LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); } 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); } 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); } init_sockets[0].status = Ns_Add; read_client_images (); } /******************************************************************************* * * Start of functions dealing with freeing of the data. * ******************************************************************************/ /** Free's all the memory that ericserver allocates. */ void free_all_newserver (void) { 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 (NewSocket *ns) { close (ns->fd); if (ns->stats.range) free (ns->stats.range); if (ns->stats.title) free (ns->stats.title); free (ns->host); } /** 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); }