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