--- deliantra/server/socket/init.C 2006/12/14 02:37:37 1.15 +++ deliantra/server/socket/init.C 2006/12/14 04:30:33 1.16 @@ -50,103 +50,66 @@ #endif #include -Socket_Info socket_info; -client_socket *init_sockets; - -client_socket::client_socket () -{ -} - -client_socket::~client_socket () -{ -} +sockvec client_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 -client_socket::init (const char *from_ip) +client_socket::client_socket (int fd, const char *peername) +: fd (fd), host (strdup (peername)) { - 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); -#endif + { + 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, "Cannot setsockopt(SO_LINGER): %s\n", strerror (errno)); + } + + { + 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); + } - faceset = 0; - facecache = 0; - image2 = 0; - sound = 0; - exp64 = 0; - monitor_spells = 0; 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_len = nrofpixmaps; + faces_sent = (uint8 *)calloc (nrofpixmaps, sizeof (*faces_sent)); + if (!faces_sent) + fatal (OUT_OF_MEMORY); + /* 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; - - sent_scroll = 0; - host = strdup_local (from_ip); + // initialisation done, kick it! { packet sl; @@ -154,83 +117,32 @@ send_packet (sl); } - 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; +#if 0//TODO + socket_info.nconns++; + 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 } +client_socket::~client_socket () +{ + status = Ns_Dead; +} /** 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; - 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); 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) - { - 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 (); } @@ -247,7 +159,6 @@ { LOG (llevDebug, "Freeing all new client/server information.\n"); free_socket_images (); - free (init_sockets); } /** @@ -260,7 +171,8 @@ void free_newsocket (client_socket *ns) { - close (ns->fd); + if (ns->fd >= 0) + close (ns->fd); if (ns->stats.range) free (ns->stats.range); @@ -269,14 +181,35 @@ free (ns->stats.title); free (ns->host); + + ns->fd = -1; + ns->stats.range = 0; + ns->stats.title = 0; + ns->host = 0; } /** 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); + pl->socket->send_packet ("goodbye"); + free_newsocket (pl->socket); free_player (pl); } +bool +add_client_socket (int fd, const char *peername) +{ + if (checkbanned (NULL, peername)) + { + LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); + return false; + } + else + { + client_socket *ns = new client_socket (dup (fd), peername); + client_sockets.push_back (ns); + return true; + } +} +