--- deliantra/server/socket/init.C 2006/09/14 22:34:05 1.8 +++ deliantra/server/socket/init.C 2006/12/14 02:37:37 1.15 @@ -34,16 +34,12 @@ #include -#ifndef __CEXTRACT__ -# include -#endif -#ifndef WIN32 /* ---win32 exclude include files */ -# include -# include -# include -# include -# include -#endif /* win32 */ +#include +#include +#include +#include +#include +#include #ifdef HAVE_UNISTD_H # include @@ -55,7 +51,15 @@ #include Socket_Info socket_info; -NewSocket *init_sockets; +client_socket *init_sockets; + +client_socket::client_socket () +{ +} + +client_socket::~client_socket () +{ +} /** * Initializes a connection. Really, it just sets up the data structure, @@ -63,115 +67,97 @@ * client. */ void -InitConnection (NewSocket * ns, const char *from_ip) +client_socket::init (const char *from_ip) { - SockList sl; - char buf[256]; int bufsize = 65535; /*Supposed absolute upper limit */ int oldbufsize; int buflen = sizeof (int); -#ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */ - int temp = 1; - - if (ioctlsocket (ns->fd, FIONBIO, &temp) == -1) - LOG (llevError, "InitConnection: Error on ioctlsocket.\n"); -#else - if (fcntl (ns->fd, F_SETFL, O_NONBLOCK) == -1) - { - LOG (llevError, "InitConnection: Error on fcntl.\n"); - } -#endif /* end win32 */ + if (fcntl (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) + if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) oldbufsize = 0; + if (oldbufsize < bufsize) - { -#ifdef ESRV_DEBUG - LOG (llevDebug, "Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize); -#endif - 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); - } - } + 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 (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); + 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 - 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->comment = NULL; - ns->old_mode = (Old_Mode) 0; - 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; + 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. */ - ns->inbuf.len = 0; - ns->inbuf.buf = (unsigned char *) malloc (sizeof (unsigned char) * MAXSOCKBUF); + inbuf_len = 0; + /* Basic initialization. Needed because we do a check in * HandleClient for oldsocketmode without checking the * length of data. */ - memset (ns->inbuf.buf, 0, sizeof (unsigned char) * MAXSOCKBUF); - 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)); + 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)); /* 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; + 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); + + { + packet sl; + sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); + send_packet (sl); + } - 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); - sprintf ((char *) buf, "version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); - sl.buf = (unsigned char *) buf; - sl.len = strlen ((char *) buf); - Send_With_Handling (ns, &sl); -#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 } @@ -183,40 +169,20 @@ struct protoent *protox; struct linger linger_opt; -#ifdef WIN32 /* ***win32 - we init a windows socket */ - WSADATA w; - - socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ - WSAStartup (0x0101, &w); /* this setup all socket stuff */ - /* ill include no error tests here, winsocket 1.1 should always work */ - /* except some old win95 versions without tcp/ip stack */ -#else /* non windows */ - -# ifdef HAVE_SYSCONF socket_info.max_filedescriptor = sysconf (_SC_OPEN_MAX); -# else -# ifdef HAVE_GETDTABLESIZE - socket_info.max_filedescriptor = getdtablesize (); -# else - "Unable to find usable function to get max filedescriptors"; -# endif -# endif -#endif /* win32 */ 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 = (client_socket *) malloc (sizeof (client_socket)); init_sockets[0].faces_sent = NULL; /* unused */ socket_info.allocated_sockets = 1; @@ -226,12 +192,14 @@ 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); @@ -239,60 +207,29 @@ 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)); - } + LOG (llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror (errno)); -/* Would be nice to have an autoconf check for this. It appears that - * these functions are both using the same calling syntax, just one - * of them needs extra valus passed. - */ -#if defined(__osf__) || defined(hpux) || defined(sgi) || defined(NeXT) || \ - defined(__sun__) || defined(__linux__) || defined(SVR4) || \ - defined(__FreeBSD__) || defined(__OpenBSD__) || \ - defined(WIN32) /* ---win32 add this here */ || \ - defined(__GNU__) /* HURD */ { -# ifdef WIN32 - char tmp = 1; -# else int tmp = 1; -# endif if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp))) - { - LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); - } - } -#else - if (setsockopt (init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, (char *) NULL, 0)) - { LOG (llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror (errno)); - } -#endif + } 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)); -#ifdef WIN32 /* ***win32: close() -> closesocket() */ - shutdown (init_sockets[0].fd, SD_BOTH); - closesocket (init_sockets[0].fd); -#else close (init_sockets[0].fd); -#endif /* win32 */ exit (-1); } + if (listen (init_sockets[0].fd, 5) == (-1)) { LOG (llevError, "Cannot listen on socket: %s\n", strerror (errno)); -#ifdef WIN32 /* ***win32: close() -> closesocket() */ - shutdown (init_sockets[0].fd, SD_BOTH); - closesocket (init_sockets[0].fd); -#else close (init_sockets[0].fd); -#endif /* win32 */ exit (-1); } + init_sockets[0].status = Ns_Add; read_client_images (); } @@ -321,36 +258,25 @@ */ void -free_newsocket (NewSocket * ns) +free_newsocket (client_socket *ns) { -#ifdef WIN32 /* ***win32: closesocket in windows style */ - shutdown (ns->fd, SD_BOTH); - if (closesocket (ns->fd)) - { -#else - if (close (ns->fd)) - { -#endif /* win32 */ + close (ns->fd); -#ifdef ESRV_DEBUG - LOG (llevDebug, "Error closing socket %d\n", ns->fd); -#endif - } if (ns->stats.range) free (ns->stats.range); + if (ns->stats.title) free (ns->stats.title); - if (ns->comment) - free (ns->comment); + free (ns->host); - free (ns->inbuf.buf); } /** Sends the 'goodbye' command to the player, and closes connection. */ void final_free_player (player *pl) { - cs_write_string (&pl->socket, "goodbye", 8); + pl->socket.send_packet ("goodbye"); free_newsocket (&pl->socket); free_player (pl); } +