Crossfire Server, Branches 1.12  R18729
init.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_init_c =
3  * "$Id: init.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2006 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The author can be reached via e-mail to crossfire-devel@real-time.com
27 */
28 
40 #include <global.h>
41 #ifndef __CEXTRACT__
42 #include <sproto.h>
43 #endif
44 #ifndef WIN32 /* ---win32 exclude include files */
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netdb.h>
50 #endif /* win32 */
51 
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59 #include <newserver.h>
60 #include <loader.h>
61 
69 
75 void init_connection(socket_struct *ns, const char *from_ip) {
76  SockList sl;
77  int bufsize = 65535; /*Supposed absolute upper limit */
78  int oldbufsize;
79  socklen_t buflen = sizeof(int);
80 
81 #ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */
82  int temp = 1;
83 
84  if (ioctlsocket(ns->fd, FIONBIO , &temp) == -1)
85  LOG(llevError, "init_connection: Error on ioctlsocket.\n");
86 #else
87  if (fcntl(ns->fd, F_SETFL, O_NONBLOCK) == -1) {
88  LOG(llevError, "init_connection: Error on fcntl.\n");
89  }
90 #endif /* end win32 */
91 
92  if (getsockopt(ns->fd, SOL_SOCKET, SO_SNDBUF, (char *)&oldbufsize, &buflen) == -1)
93  oldbufsize = 0;
94  if (oldbufsize < bufsize) {
95 #ifdef ESRV_DEBUG
96  LOG(llevDebug, "Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize);
97 #endif
98  if (setsockopt(ns->fd, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, sizeof(bufsize))) {
99  LOG(llevError, "init_connection: setsockopt unable to set output buf size to %d\n", bufsize);
100  }
101  }
102  buflen = sizeof(oldbufsize);
103  getsockopt(ns->fd, SOL_SOCKET, SO_SNDBUF, (char *)&oldbufsize, &buflen);
104 #ifdef ESRV_DEBUG
105  LOG(llevDebug, "Socket buffer size now %d bytes\n", oldbufsize);
106 #endif
107 
108  ns->faceset = 0;
109  ns->facecache = 0;
110  ns->sound = 0;
111  ns->monitor_spells = 0;
112  ns->darkness = 1;
113  ns->status = Ns_Add;
114  ns->mapx = 11;
115  ns->mapy = 11;
116  ns->newmapcmd = 0;
117  ns->look_position = 0;
118  ns->update_look = 0;
119  ns->has_readable_type = 0;
120  ns->supported_readables = 0;
121  ns->monitor_spells = 0;
122  ns->tick = 0;
123  ns->is_bot = 0;
125  ns->want_pickup = 0;
126 
127  /* we should really do some checking here - if total clients overflows
128  * we need to do something more intelligent, because client id's will start
129  * duplicating (not likely in normal cases, but malicous attacks that
130  * just open and close connections could get this total up.
131  */
132  SockList_Init(&ns->inbuf);
134  /* Basic initialization. Needed because we do a check in
135  * handle_client for oldsocketmode without checking the
136  * length of data.
137  */
138  memset(ns->inbuf.buf, 0, sizeof(ns->inbuf.buf));
139  memset(&ns->lastmap, 0, sizeof(struct Map));
140  if (!ns->faces_sent)
141  ns->faces_sent = calloc(sizeof(*ns->faces_sent), nrofpixmaps);
143 
144  memset(&ns->anims_sent, 0, sizeof(ns->anims_sent));
145  memset(&ns->stats, 0, sizeof(struct statsinfo));
146  ns->map_scroll_x = 0;
147  ns->map_scroll_y = 0;
148  /* Do this so we don't send a face command for the client for
149  * this face. Face 0 is sent to the client to say clear
150  * face information.
151  */
152  ns->faces_sent[0] = NS_FACESENT_FACE;
153 
154  ns->outputbuffer.start = 0;
155  ns->outputbuffer.len = 0;
156  ns->can_write = 1;
157  ns->password_fails = 0;
158 
159  ns->host = strdup_local(from_ip);
160  SockList_Init(&sl);
161  SockList_AddPrintf(&sl, "version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO);
162  Send_With_Handling(ns, &sl);
163  SockList_Term(&sl);
164 #ifdef CS_LOGSTATS
165  if (socket_info.nconns > cst_tot.max_conn)
166  cst_tot.max_conn = socket_info.nconns;
167  if (socket_info.nconns > cst_lst.max_conn)
168  cst_lst.max_conn = socket_info.nconns;
169 #endif
170 }
171 
173 void init_server(void) {
174  struct sockaddr_in insock;
175  struct protoent *protox;
176  struct linger linger_opt;
177  char err[MAX_BUF];
178 
179 #ifdef WIN32 /* ***win32 - we init a windows socket */
180  WSADATA w;
181 
182  socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */
183  WSAStartup(0x0101, &w); /* this setup all socket stuff */
184  /* ill include no error tests here, winsocket 1.1 should always work */
185  /* except some old win95 versions without tcp/ip stack */
186 #else /* non windows */
187 
188 #ifdef HAVE_SYSCONF
189  socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX);
190 #else
191 # ifdef HAVE_GETDTABLESIZE
192  socket_info.max_filedescriptor = getdtablesize();
193 # else
194  "Unable to find usable function to get max filedescriptors";
195 # endif
196 #endif
197 #endif /* win32 */
198 
199  socket_info.timeout.tv_sec = 0;
200  socket_info.timeout.tv_usec = 0;
201  socket_info.nconns = 0;
202 
203 #ifdef CS_LOGSTATS
204  memset(&cst_tot, 0, sizeof(CS_Stats));
205  memset(&cst_lst, 0, sizeof(CS_Stats));
206  cst_tot.time_start = time(NULL);
207  cst_lst.time_start = time(NULL);
208 #endif
209 
210  LOG(llevDebug, "Initialize new client/server data\n");
211  socket_info.nconns = 1;
212  init_sockets = malloc(sizeof(socket_struct));
213  init_sockets[0].faces_sent = NULL; /* unused */
214  socket_info.allocated_sockets = 1;
215 
216  protox = getprotobyname("tcp");
217  if (protox == NULL) {
218  LOG(llevError, "init_server: Error getting protox\n");
219  return;
220  }
221  init_sockets[0].fd = socket(PF_INET, SOCK_STREAM, protox->p_proto);
222  if (init_sockets[0].fd == -1) {
223  LOG(llevError, "Cannot create socket: %s\n", strerror_local(errno, err, sizeof(err)));
224  exit(-1);
225  }
226  insock.sin_family = AF_INET;
227  insock.sin_port = htons(settings.csport);
228  insock.sin_addr.s_addr = htonl(INADDR_ANY);
229 
230  linger_opt.l_onoff = 0;
231  linger_opt.l_linger = 0;
232  if (setsockopt(init_sockets[0].fd, SOL_SOCKET, SO_LINGER, (char *)&linger_opt, sizeof(struct linger))) {
233  LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror_local(errno, err, sizeof(err)));
234  }
235 /* Would be nice to have an autoconf check for this. It appears that
236  * these functions are both using the same calling syntax, just one
237  * of them needs extra valus passed.
238  */
239 #if defined(__osf__) || defined(hpux) || defined(sgi) || defined(NeXT) || \
240  defined(__sun__) || defined(__linux__) || defined(SVR4) || \
241  defined(__FreeBSD__) || defined(__OpenBSD__) || \
242  defined(WIN32) /* ---win32 add this here */ || \
243  defined(__GNU__) /* HURD */
244  {
245 #ifdef WIN32
246  char tmp = 1;
247 #else
248  int tmp = 1;
249 #endif
250 
251  if (setsockopt(init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp))) {
252  LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno, err, sizeof(err)));
253  }
254  }
255 #else
256  if (setsockopt(init_sockets[0].fd, SOL_SOCKET, SO_REUSEADDR, (char *)NULL, 0)) {
257  LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno, err, sizeof(err)));
258  }
259 #endif
260 
261  if (bind(init_sockets[0].fd, (struct sockaddr *)&insock, sizeof(insock)) == (-1)) {
262  LOG(llevError, "Cannot bind socket to port %d: %s\n", ntohs(insock.sin_port), strerror_local(errno, err, sizeof(err)));
263 #ifdef WIN32 /* ***win32: close() -> closesocket() */
264  shutdown(init_sockets[0].fd, SD_BOTH);
265  closesocket(init_sockets[0].fd);
266 #else
267  close(init_sockets[0].fd);
268 #endif /* win32 */
269  exit(-1);
270  }
271  if (listen(init_sockets[0].fd, 5) == (-1)) {
272  LOG(llevError, "Cannot listen on socket: %s\n", strerror_local(errno, err, sizeof(err)));
273 #ifdef WIN32 /* ***win32: close() -> closesocket() */
274  shutdown(init_sockets[0].fd, SD_BOTH);
275  closesocket(init_sockets[0].fd);
276 #else
277  close(init_sockets[0].fd);
278 #endif /* win32 */
279  exit(-1);
280  }
281  init_sockets[0].status = Ns_Add;
283 }
284 
285 /*******************************************************************************
286  *
287  * Start of functions dealing with freeing of the data.
288  *
289  ******************************************************************************/
290 
292 void free_all_newserver(void) {
293  LOG(llevDebug, "Freeing all new client/server information.\n");
295  free(init_sockets);
296 }
297 
306 #ifdef WIN32 /* ***win32: closesocket in windows style */
307  shutdown(ns->fd, SD_BOTH);
308  if (closesocket(ns->fd)) {
309 #else
310  if (close(ns->fd)) {
311 #endif /* win32 */
312 
313 #ifdef ESRV_DEBUG
314  LOG(llevDebug, "Error closing socket %d\n", ns->fd);
315 #endif
316  }
317  ns->fd = -1;
318  if (ns->stats.range)
320  if (ns->stats.title)
322  if (ns->host)
323  FREE_AND_CLEAR(ns->host);
324  SockList_Term(&ns->inbuf);
325 }
326 
329  SockList sl;
330 
331  SockList_Init(&sl);
332  SockList_AddString(&sl, "goodbye");
333  Send_With_Handling(&pl->socket, &sl);
334  SockList_Term(&sl);
335  free_newsocket(&pl->socket);
336  free_player(pl);
337 }
void SockList_AddPrintf(SockList *sl, const char *format,...)
Definition: lowlevel.c:187
Definition: player.h:146
void free_player(player *pl)
Definition: player.c:68
uint8 num_look_objects
Definition: newserver.h:149
#define NS_FACESENT_FACE
Definition: newserver.h:164
int nrofpixmaps
Definition: image.c:76
uint32 can_write
Definition: newserver.h:132
void SockList_Init(SockList *sl)
Definition: lowlevel.c:67
void SockList_ResetRead(SockList *sl)
Definition: lowlevel.c:94
uint32 tick
Definition: newserver.h:136
struct Map lastmap
Definition: newserver.h:118
socket_struct socket
Definition: player.h:148
enum Sock_Status status
Definition: newserver.h:116
#define socklen_t
Definition: win32.h:148
sint8 map_scroll_y
Definition: newserver.h:119
int max_filedescriptor
Definition: newserver.h:173
void free_newsocket(socket_struct *ns)
Definition: init.c:305
char * title
Definition: newserver.h:86
char * host
Definition: newserver.h:125
#define VERSION_SC
Definition: newserver.h:181
size_t faces_sent_len
Definition: newserver.h:120
void init_server(void)
Definition: init.c:173
unsigned char buf[2+65536UL+1]
Definition: newclient.h:576
uint32 sound
Definition: newserver.h:139
uint8 * faces_sent
Definition: newserver.h:121
buffer_struct outputbuffer
Definition: newserver.h:126
#define DEFAULT_NUM_LOOK_OBJECTS
Definition: newserver.h:50
uint32 has_readable_type
Definition: newserver.h:133
CS_Stats cst_lst
void free_all_newserver(void)
Definition: init.c:292
short max_conn
Definition: newclient.h:583
void SockList_Term(SockList *sl)
Definition: lowlevel.c:77
Definition: newserver.h:77
uint32 monitor_spells
Definition: newserver.h:135
sint8 map_scroll_x
Definition: newserver.h:119
uint32 is_bot
Definition: newserver.h:137
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:154
uint32 facecache
Definition: newserver.h:128
void free_socket_images(void)
Definition: image.c:584
#define VERSION_CS
Definition: newserver.h:180
void read_client_images(void)
Definition: image.c:471
char * range
Definition: newserver.h:86
int allocated_sockets
Definition: newserver.h:175
char * strdup_local(const char *str)
Definition: porting.c:310
#define MAX_BUF
Definition: define.h:81
uint32 supported_readables
Definition: newserver.h:140
uint16 look_position
Definition: newserver.h:142
uint32 darkness
Definition: newserver.h:130
void init_connection(socket_struct *ns, const char *from_ip)
Definition: init.c:75
uint32 update_look
Definition: newserver.h:131
struct timeval timeout
Definition: newserver.h:172
CS_Stats cst_tot
struct statsinfo stats
Definition: newserver.h:123
struct Settings settings
Definition: init.c:48
Socket_Info socket_info
Definition: init.c:63
uint32 newmapcmd
Definition: newserver.h:129
socket_struct * init_sockets
Definition: init.c:68
uint16 csport
Definition: global.h:326
time_t time_start
Definition: newclient.h:584
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
uint8 anims_sent[MAXANIMNUM]
Definition: newserver.h:122
void final_free_player(player *pl)
Definition: init.c:328
char * strerror_local(int errnum, char *buf, size_t size)
Definition: porting.c:525
#define VERSION_INFO
Definition: newserver.h:182
uint8 password_fails
Definition: newserver.h:127
#define FREE_AND_CLEAR(xyz)
Definition: global.h:282
SockList inbuf
Definition: newserver.h:124
uint32 want_pickup
Definition: newserver.h:138
uint8 faceset
Definition: newserver.h:144
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:541