1 | /* |
1 | /* |
2 | * static char *rcsid_init_c = |
2 | * static char *rcsid_init_c = |
3 | * "$Id: init.C,v 1.1 2006/08/13 17:16:05 elmex Exp $"; |
3 | * "$Id: init.C,v 1.5 2006/08/29 08:01:38 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
74 | int bufsize=65535; /*Supposed absolute upper limit */ |
74 | int bufsize=65535; /*Supposed absolute upper limit */ |
75 | int oldbufsize; |
75 | int oldbufsize; |
76 | int buflen=sizeof(int); |
76 | int buflen=sizeof(int); |
77 | |
77 | |
78 | #ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */ |
78 | #ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */ |
79 | int temp = 1; |
79 | int temp = 1; |
80 | |
80 | |
81 | if(ioctlsocket(ns->fd, FIONBIO , &temp) == -1) |
81 | if(ioctlsocket(ns->fd, FIONBIO , &temp) == -1) |
82 | LOG(llevError,"InitConnection: Error on ioctlsocket.\n"); |
82 | LOG(llevError,"InitConnection: Error on ioctlsocket.\n"); |
83 | #else |
83 | #else |
84 | if (fcntl(ns->fd, F_SETFL, O_NONBLOCK)==-1) { |
84 | if (fcntl(ns->fd, F_SETFL, O_NONBLOCK)==-1) { |
85 | LOG(llevError,"InitConnection: Error on fcntl.\n"); |
85 | LOG(llevError,"InitConnection: Error on fcntl.\n"); |
86 | } |
86 | } |
87 | #endif /* end win32 */ |
87 | #endif /* end win32 */ |
88 | |
88 | |
89 | if (getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen)==-1) |
89 | if (getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen)==-1) |
90 | oldbufsize=0; |
90 | oldbufsize=0; |
91 | if (oldbufsize<bufsize) { |
91 | if (oldbufsize<bufsize) { |
92 | #ifdef ESRV_DEBUG |
92 | #ifdef ESRV_DEBUG |
93 | LOG(llevDebug, "Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize); |
93 | LOG(llevDebug, "Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize); |
94 | #endif |
94 | #endif |
95 | if(setsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&bufsize, sizeof(&bufsize))) { |
95 | if(setsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&bufsize, sizeof(&bufsize))) { |
96 | LOG(llevError,"InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); |
96 | LOG(llevError,"InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); |
97 | } |
97 | } |
98 | } |
98 | } |
99 | buflen=sizeof(oldbufsize); |
99 | buflen=sizeof(oldbufsize); |
100 | getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen); |
100 | getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen); |
101 | #ifdef ESRV_DEBUG |
101 | #ifdef ESRV_DEBUG |
102 | LOG(llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); |
102 | LOG(llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); |
… | |
… | |
124 | ns->has_readable_type = 0; |
124 | ns->has_readable_type = 0; |
125 | ns->supported_readables = 0; |
125 | ns->supported_readables = 0; |
126 | ns->monitor_spells = 0; |
126 | ns->monitor_spells = 0; |
127 | ns->mapinfocmd = 0; |
127 | ns->mapinfocmd = 0; |
128 | ns->extcmd = 0; |
128 | ns->extcmd = 0; |
|
|
129 | ns->extmap = 0; |
129 | |
130 | |
130 | /* we should really do some checking here - if total clients overflows |
131 | /* we should really do some checking here - if total clients overflows |
131 | * we need to do something more intelligent, because client id's will start |
132 | * we need to do something more intelligent, because client id's will start |
132 | * duplicating (not likely in normal cases, but malicous attacks that |
133 | * duplicating (not likely in normal cases, but malicous attacks that |
133 | * just open and close connections could get this total up. |
134 | * just open and close connections could get this total up. |
… | |
… | |
153 | ns->outputbuffer.len=0; |
154 | ns->outputbuffer.len=0; |
154 | ns->can_write=1; |
155 | ns->can_write=1; |
155 | ns->password_fails = 0; |
156 | ns->password_fails = 0; |
156 | |
157 | |
157 | ns->current_map = 0; |
158 | ns->current_map = 0; |
|
|
159 | ns->current_x = 0; |
|
|
160 | ns->current_y = 0; |
|
|
161 | ns->client[0] = 0; |
|
|
162 | ns->buggy_mapscroll = 0; |
158 | |
163 | |
159 | ns->sent_scroll=0; |
164 | ns->sent_scroll=0; |
160 | ns->host=strdup_local(from_ip); |
165 | ns->host=strdup_local(from_ip); |
161 | sprintf((char*)buf, "version %d %d %s\n", VERSION_CS,VERSION_SC, VERSION_INFO); |
166 | sprintf((char*)buf, "version %d %d %s\n", VERSION_CS,VERSION_SC, VERSION_INFO); |
162 | sl.buf=(unsigned char*)buf; |
167 | sl.buf=(unsigned char*)buf; |
163 | sl.len=strlen((char*)buf); |
168 | sl.len=strlen((char*)buf); |
164 | Send_With_Handling(ns, &sl); |
169 | Send_With_Handling(ns, &sl); |
165 | #ifdef CS_LOGSTATS |
170 | #ifdef CS_LOGSTATS |
166 | if (socket_info.nconns>cst_tot.max_conn) |
171 | if (socket_info.nconns>cst_tot.max_conn) |
167 | cst_tot.max_conn = socket_info.nconns; |
172 | cst_tot.max_conn = socket_info.nconns; |
168 | if (socket_info.nconns>cst_lst.max_conn) |
173 | if (socket_info.nconns>cst_lst.max_conn) |
169 | cst_lst.max_conn = socket_info.nconns; |
174 | cst_lst.max_conn = socket_info.nconns; |
170 | #endif |
175 | #endif |
171 | } |
176 | } |
172 | |
177 | |
173 | |
178 | |
174 | /** This sets up the socket and reads all the image information into memory. */ |
179 | /** This sets up the socket and reads all the image information into memory. */ |
… | |
… | |
177 | struct sockaddr_in insock; |
182 | struct sockaddr_in insock; |
178 | struct protoent *protox; |
183 | struct protoent *protox; |
179 | struct linger linger_opt; |
184 | struct linger linger_opt; |
180 | |
185 | |
181 | #ifdef WIN32 /* ***win32 - we init a windows socket */ |
186 | #ifdef WIN32 /* ***win32 - we init a windows socket */ |
182 | WSADATA w; |
187 | WSADATA w; |
183 | |
188 | |
184 | socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ |
189 | socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ |
185 | WSAStartup (0x0101,&w); /* this setup all socket stuff */ |
190 | WSAStartup (0x0101,&w); /* this setup all socket stuff */ |
186 | /* ill include no error tests here, winsocket 1.1 should always work */ |
191 | /* ill include no error tests here, winsocket 1.1 should always work */ |
187 | /* except some old win95 versions without tcp/ip stack */ |
192 | /* except some old win95 versions without tcp/ip stack */ |
188 | #else /* non windows */ |
193 | #else /* non windows */ |
189 | |
194 | |
190 | #ifdef HAVE_SYSCONF |
195 | #ifdef HAVE_SYSCONF |
191 | socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX); |
196 | socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX); |
192 | #else |
197 | #else |
… | |
… | |
215 | init_sockets[0].faces_sent = NULL; /* unused */ |
220 | init_sockets[0].faces_sent = NULL; /* unused */ |
216 | socket_info.allocated_sockets=1; |
221 | socket_info.allocated_sockets=1; |
217 | |
222 | |
218 | protox = getprotobyname("tcp"); |
223 | protox = getprotobyname("tcp"); |
219 | if (protox==NULL) { |
224 | if (protox==NULL) { |
220 | LOG(llevError,"init_ericserver: Error getting protox\n"); |
225 | LOG(llevError,"init_ericserver: Error getting protox\n"); |
221 | return; |
226 | return; |
222 | } |
227 | } |
223 | init_sockets[0].fd = socket(PF_INET, SOCK_STREAM, protox->p_proto); |
228 | init_sockets[0].fd = socket(PF_INET, SOCK_STREAM, protox->p_proto); |
224 | if (init_sockets[0].fd == -1) { |
229 | if (init_sockets[0].fd == -1) { |
225 | LOG(llevError, "Cannot create socket: %s\n", strerror_local(errno)); |
230 | LOG(llevError, "Cannot create socket: %s\n", strerror_local(errno)); |
226 | exit(-1); |
231 | exit(-1); |
227 | } |
232 | } |
228 | insock.sin_family = AF_INET; |
233 | insock.sin_family = AF_INET; |
229 | insock.sin_port = htons(settings.csport); |
234 | insock.sin_port = htons(settings.csport); |
230 | insock.sin_addr.s_addr = htonl(INADDR_ANY); |
235 | insock.sin_addr.s_addr = htonl(INADDR_ANY); |
231 | |
236 | |
232 | linger_opt.l_onoff = 0; |
237 | linger_opt.l_onoff = 0; |
233 | linger_opt.l_linger = 0; |
238 | linger_opt.l_linger = 0; |
234 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_LINGER,(char *) &linger_opt, |
239 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_LINGER,(char *) &linger_opt, |
235 | sizeof(struct linger))) { |
240 | sizeof(struct linger))) { |
236 | LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror_local(errno)); |
241 | LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror_local(errno)); |
237 | } |
242 | } |
238 | /* Would be nice to have an autoconf check for this. It appears that |
243 | /* Would be nice to have an autoconf check for this. It appears that |
239 | * these functions are both using the same calling syntax, just one |
244 | * these functions are both using the same calling syntax, just one |
240 | * of them needs extra valus passed. |
245 | * of them needs extra valus passed. |
241 | */ |
246 | */ |
… | |
… | |
246 | defined(__GNU__) /* HURD */ |
251 | defined(__GNU__) /* HURD */ |
247 | { |
252 | { |
248 | #ifdef WIN32 |
253 | #ifdef WIN32 |
249 | char tmp = 1; |
254 | char tmp = 1; |
250 | #else |
255 | #else |
251 | int tmp =1; |
256 | int tmp =1; |
252 | #endif |
257 | #endif |
253 | |
258 | |
254 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR, &tmp, sizeof(tmp))) { |
259 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR, &tmp, sizeof(tmp))) { |
255 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
260 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
256 | } |
261 | } |
257 | } |
262 | } |
258 | #else |
263 | #else |
259 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR,(char *)NULL,0)) { |
264 | if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR,(char *)NULL,0)) { |
260 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
265 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
261 | } |
266 | } |
262 | #endif |
267 | #endif |
263 | |
268 | |
264 | if (bind(init_sockets[0].fd,(struct sockaddr *)&insock,sizeof(insock)) == (-1)) { |
269 | if (bind(init_sockets[0].fd,(struct sockaddr *)&insock,sizeof(insock)) == (-1)) { |
265 | LOG(llevError, "Cannot bind socket to port %d: %s\n", ntohs(insock.sin_port), strerror_local(errno)); |
270 | LOG(llevError, "Cannot bind socket to port %d: %s\n", ntohs(insock.sin_port), strerror_local(errno)); |
266 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
271 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
267 | shutdown(init_sockets[0].fd,SD_BOTH); |
272 | shutdown(init_sockets[0].fd,SD_BOTH); |
268 | closesocket(init_sockets[0].fd); |
273 | closesocket(init_sockets[0].fd); |
269 | #else |
274 | #else |
270 | close(init_sockets[0].fd); |
275 | close(init_sockets[0].fd); |
271 | #endif /* win32 */ |
276 | #endif /* win32 */ |
272 | exit(-1); |
277 | exit(-1); |
273 | } |
278 | } |
274 | if (listen(init_sockets[0].fd,5) == (-1)) { |
279 | if (listen(init_sockets[0].fd,5) == (-1)) { |
275 | LOG(llevError, "Cannot listen on socket: %s\n", strerror_local(errno)); |
280 | LOG(llevError, "Cannot listen on socket: %s\n", strerror_local(errno)); |
276 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
281 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
277 | shutdown(init_sockets[0].fd,SD_BOTH); |
282 | shutdown(init_sockets[0].fd,SD_BOTH); |
278 | closesocket(init_sockets[0].fd); |
283 | closesocket(init_sockets[0].fd); |
279 | #else |
284 | #else |
280 | close(init_sockets[0].fd); |
285 | close(init_sockets[0].fd); |
281 | #endif /* win32 */ |
286 | #endif /* win32 */ |
282 | exit(-1); |
287 | exit(-1); |
283 | } |
288 | } |
284 | init_sockets[0].status=Ns_Add; |
289 | init_sockets[0].status=Ns_Add; |
285 | read_client_images(); |
290 | read_client_images(); |
286 | } |
291 | } |
287 | |
292 | |
… | |
… | |
308 | */ |
313 | */ |
309 | |
314 | |
310 | void free_newsocket(NewSocket *ns) |
315 | void free_newsocket(NewSocket *ns) |
311 | { |
316 | { |
312 | #ifdef WIN32 /* ***win32: closesocket in windows style */ |
317 | #ifdef WIN32 /* ***win32: closesocket in windows style */ |
313 | shutdown(ns->fd,SD_BOTH); |
318 | shutdown(ns->fd,SD_BOTH); |
314 | if (closesocket(ns->fd)) { |
319 | if (closesocket(ns->fd)) { |
315 | #else |
320 | #else |
316 | if (close(ns->fd)) { |
321 | if (close(ns->fd)) { |
317 | #endif /* win32 */ |
322 | #endif /* win32 */ |
318 | |
323 | |
319 | #ifdef ESRV_DEBUG |
324 | #ifdef ESRV_DEBUG |
320 | LOG(llevDebug,"Error closing socket %d\n", ns->fd); |
325 | LOG(llevDebug,"Error closing socket %d\n", ns->fd); |
321 | #endif |
326 | #endif |
322 | } |
327 | } |
323 | if (ns->stats.range) |
328 | if (ns->stats.range) |
324 | free(ns->stats.range); |
329 | free(ns->stats.range); |
325 | if (ns->stats.title) |
330 | if (ns->stats.title) |
326 | free(ns->stats.title); |
331 | free(ns->stats.title); |
327 | if (ns->comment) |
332 | if (ns->comment) |
328 | free(ns->comment); |
333 | free(ns->comment); |
329 | free(ns->host); |
334 | free(ns->host); |
330 | free(ns->inbuf.buf); |
335 | free(ns->inbuf.buf); |
331 | } |
336 | } |
332 | |
337 | |
333 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
338 | /** Sends the 'goodbye' command to the player, and closes connection. */ |