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