1 | /* |
1 | /* |
2 | * static char *rcsid_init_c = |
2 | * static char *rcsid_init_c = |
3 | * "$Id: init.C,v 1.3 2006/08/18 02:06:58 root Exp $"; |
3 | * "$Id: init.C,v 1.6 2006/08/30 16:30:37 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); |
… | |
… | |
156 | ns->password_fails = 0; |
156 | ns->password_fails = 0; |
157 | |
157 | |
158 | ns->current_map = 0; |
158 | ns->current_map = 0; |
159 | ns->current_x = 0; |
159 | ns->current_x = 0; |
160 | ns->current_y = 0; |
160 | ns->current_y = 0; |
|
|
161 | ns->client[0] = 0; |
|
|
162 | ns->buggy_mapscroll = 0; |
161 | |
163 | |
162 | ns->sent_scroll=0; |
164 | ns->sent_scroll=0; |
163 | ns->host=strdup_local(from_ip); |
165 | ns->host=strdup_local(from_ip); |
164 | 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); |
165 | sl.buf=(unsigned char*)buf; |
167 | sl.buf=(unsigned char*)buf; |
166 | sl.len=strlen((char*)buf); |
168 | sl.len=strlen((char*)buf); |
167 | Send_With_Handling(ns, &sl); |
169 | Send_With_Handling(ns, &sl); |
168 | #ifdef CS_LOGSTATS |
170 | #ifdef CS_LOGSTATS |
169 | if (socket_info.nconns>cst_tot.max_conn) |
171 | if (socket_info.nconns>cst_tot.max_conn) |
170 | cst_tot.max_conn = socket_info.nconns; |
172 | cst_tot.max_conn = socket_info.nconns; |
171 | if (socket_info.nconns>cst_lst.max_conn) |
173 | if (socket_info.nconns>cst_lst.max_conn) |
172 | cst_lst.max_conn = socket_info.nconns; |
174 | cst_lst.max_conn = socket_info.nconns; |
173 | #endif |
175 | #endif |
174 | } |
176 | } |
175 | |
177 | |
176 | |
178 | |
177 | /** 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. */ |
… | |
… | |
180 | struct sockaddr_in insock; |
182 | struct sockaddr_in insock; |
181 | struct protoent *protox; |
183 | struct protoent *protox; |
182 | struct linger linger_opt; |
184 | struct linger linger_opt; |
183 | |
185 | |
184 | #ifdef WIN32 /* ***win32 - we init a windows socket */ |
186 | #ifdef WIN32 /* ***win32 - we init a windows socket */ |
185 | WSADATA w; |
187 | WSADATA w; |
186 | |
188 | |
187 | socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ |
189 | socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ |
188 | WSAStartup (0x0101,&w); /* this setup all socket stuff */ |
190 | WSAStartup (0x0101,&w); /* this setup all socket stuff */ |
189 | /* 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 */ |
190 | /* except some old win95 versions without tcp/ip stack */ |
192 | /* except some old win95 versions without tcp/ip stack */ |
191 | #else /* non windows */ |
193 | #else /* non windows */ |
192 | |
194 | |
193 | #ifdef HAVE_SYSCONF |
195 | #ifdef HAVE_SYSCONF |
194 | socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX); |
196 | socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX); |
195 | #else |
197 | #else |
… | |
… | |
218 | init_sockets[0].faces_sent = NULL; /* unused */ |
220 | init_sockets[0].faces_sent = NULL; /* unused */ |
219 | socket_info.allocated_sockets=1; |
221 | socket_info.allocated_sockets=1; |
220 | |
222 | |
221 | protox = getprotobyname("tcp"); |
223 | protox = getprotobyname("tcp"); |
222 | if (protox==NULL) { |
224 | if (protox==NULL) { |
223 | LOG(llevError,"init_ericserver: Error getting protox\n"); |
225 | LOG(llevError,"init_ericserver: Error getting protox\n"); |
224 | return; |
226 | return; |
225 | } |
227 | } |
226 | 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); |
227 | if (init_sockets[0].fd == -1) { |
229 | if (init_sockets[0].fd == -1) { |
228 | LOG(llevError, "Cannot create socket: %s\n", strerror_local(errno)); |
230 | LOG(llevError, "Cannot create socket: %s\n", strerror(errno)); |
229 | exit(-1); |
231 | exit(-1); |
230 | } |
232 | } |
231 | insock.sin_family = AF_INET; |
233 | insock.sin_family = AF_INET; |
232 | insock.sin_port = htons(settings.csport); |
234 | insock.sin_port = htons(settings.csport); |
233 | insock.sin_addr.s_addr = htonl(INADDR_ANY); |
235 | insock.sin_addr.s_addr = htonl(INADDR_ANY); |
234 | |
236 | |
235 | linger_opt.l_onoff = 0; |
237 | linger_opt.l_onoff = 0; |
236 | linger_opt.l_linger = 0; |
238 | linger_opt.l_linger = 0; |
237 | 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, |
238 | sizeof(struct linger))) { |
240 | sizeof(struct linger))) { |
239 | LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror_local(errno)); |
241 | LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror(errno)); |
240 | } |
242 | } |
241 | /* 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 |
242 | * these functions are both using the same calling syntax, just one |
244 | * these functions are both using the same calling syntax, just one |
243 | * of them needs extra valus passed. |
245 | * of them needs extra valus passed. |
244 | */ |
246 | */ |
… | |
… | |
249 | defined(__GNU__) /* HURD */ |
251 | defined(__GNU__) /* HURD */ |
250 | { |
252 | { |
251 | #ifdef WIN32 |
253 | #ifdef WIN32 |
252 | char tmp = 1; |
254 | char tmp = 1; |
253 | #else |
255 | #else |
254 | int tmp =1; |
256 | int tmp =1; |
255 | #endif |
257 | #endif |
256 | |
258 | |
257 | 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))) { |
258 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
260 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); |
259 | } |
261 | } |
260 | } |
262 | } |
261 | #else |
263 | #else |
262 | 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)) { |
263 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror_local(errno)); |
265 | LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); |
264 | } |
266 | } |
265 | #endif |
267 | #endif |
266 | |
268 | |
267 | 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)) { |
268 | 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(errno)); |
269 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
271 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
270 | shutdown(init_sockets[0].fd,SD_BOTH); |
272 | shutdown(init_sockets[0].fd,SD_BOTH); |
271 | closesocket(init_sockets[0].fd); |
273 | closesocket(init_sockets[0].fd); |
272 | #else |
274 | #else |
273 | close(init_sockets[0].fd); |
275 | close(init_sockets[0].fd); |
274 | #endif /* win32 */ |
276 | #endif /* win32 */ |
275 | exit(-1); |
277 | exit(-1); |
276 | } |
278 | } |
277 | if (listen(init_sockets[0].fd,5) == (-1)) { |
279 | if (listen(init_sockets[0].fd,5) == (-1)) { |
278 | LOG(llevError, "Cannot listen on socket: %s\n", strerror_local(errno)); |
280 | LOG(llevError, "Cannot listen on socket: %s\n", strerror(errno)); |
279 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
281 | #ifdef WIN32 /* ***win32: close() -> closesocket() */ |
280 | shutdown(init_sockets[0].fd,SD_BOTH); |
282 | shutdown(init_sockets[0].fd,SD_BOTH); |
281 | closesocket(init_sockets[0].fd); |
283 | closesocket(init_sockets[0].fd); |
282 | #else |
284 | #else |
283 | close(init_sockets[0].fd); |
285 | close(init_sockets[0].fd); |
284 | #endif /* win32 */ |
286 | #endif /* win32 */ |
285 | exit(-1); |
287 | exit(-1); |
286 | } |
288 | } |
287 | init_sockets[0].status=Ns_Add; |
289 | init_sockets[0].status=Ns_Add; |
288 | read_client_images(); |
290 | read_client_images(); |
289 | } |
291 | } |
290 | |
292 | |
… | |
… | |
311 | */ |
313 | */ |
312 | |
314 | |
313 | void free_newsocket(NewSocket *ns) |
315 | void free_newsocket(NewSocket *ns) |
314 | { |
316 | { |
315 | #ifdef WIN32 /* ***win32: closesocket in windows style */ |
317 | #ifdef WIN32 /* ***win32: closesocket in windows style */ |
316 | shutdown(ns->fd,SD_BOTH); |
318 | shutdown(ns->fd,SD_BOTH); |
317 | if (closesocket(ns->fd)) { |
319 | if (closesocket(ns->fd)) { |
318 | #else |
320 | #else |
319 | if (close(ns->fd)) { |
321 | if (close(ns->fd)) { |
320 | #endif /* win32 */ |
322 | #endif /* win32 */ |
321 | |
323 | |
322 | #ifdef ESRV_DEBUG |
324 | #ifdef ESRV_DEBUG |
323 | LOG(llevDebug,"Error closing socket %d\n", ns->fd); |
325 | LOG(llevDebug,"Error closing socket %d\n", ns->fd); |
324 | #endif |
326 | #endif |
325 | } |
327 | } |
326 | if (ns->stats.range) |
328 | if (ns->stats.range) |
327 | free(ns->stats.range); |
329 | free(ns->stats.range); |
328 | if (ns->stats.title) |
330 | if (ns->stats.title) |
329 | free(ns->stats.title); |
331 | free(ns->stats.title); |
330 | if (ns->comment) |
332 | if (ns->comment) |
331 | free(ns->comment); |
333 | free(ns->comment); |
332 | free(ns->host); |
334 | free(ns->host); |
333 | free(ns->inbuf.buf); |
335 | free(ns->inbuf.buf); |
334 | } |
336 | } |
335 | |
337 | |
336 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
338 | /** Sends the 'goodbye' command to the player, and closes connection. */ |