1 | /* |
1 | /* |
2 | * static char *rcsid_init_c = |
2 | * static char *rcsid_init_c = |
3 | * "$Id: init.C,v 1.5 2006/08/29 08:01:38 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 | |
… | |
… | |
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(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(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 | */ |
… | |
… | |
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(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(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(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(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); |