… | |
… | |
65 | * before player joins, and those happen after the player has joined. |
65 | * before player joins, and those happen after the player has joined. |
66 | * As such, we have function types that might be called, so |
66 | * As such, we have function types that might be called, so |
67 | * we end up having 2 tables. |
67 | * we end up having 2 tables. |
68 | */ |
68 | */ |
69 | |
69 | |
70 | typedef void (*func_uint8_int_ns) (char *, int, NewSocket *); |
70 | typedef void (*func_uint8_int_ns) (char *, int, client_socket *); |
71 | |
71 | |
72 | struct NsCmdMapping |
72 | struct NsCmdMapping |
73 | { |
73 | { |
74 | const char *cmdname; |
74 | const char *cmdname; |
75 | func_uint8_int_ns cmdproc; |
75 | func_uint8_int_ns cmdproc; |
… | |
… | |
132 | * RequestInfo is sort of a meta command. There is some specific |
132 | * RequestInfo is sort of a meta command. There is some specific |
133 | * request of information, but we call other functions to provide |
133 | * request of information, but we call other functions to provide |
134 | * that information. |
134 | * that information. |
135 | */ |
135 | */ |
136 | void |
136 | void |
137 | RequestInfo (char *buf, int len, NewSocket * ns) |
137 | RequestInfo (char *buf, int len, client_socket * ns) |
138 | { |
138 | { |
139 | char *params = NULL, *cp; |
139 | char *params = NULL, *cp; |
140 | |
140 | |
141 | /* No match */ |
141 | /* No match */ |
142 | char bigbuf[MAX_BUF]; |
142 | char bigbuf[MAX_BUF]; |
… | |
… | |
181 | * with this socket, null if no player (one of the init_sockets for just |
181 | * with this socket, null if no player (one of the init_sockets for just |
182 | * starting a connection) |
182 | * starting a connection) |
183 | */ |
183 | */ |
184 | |
184 | |
185 | void |
185 | void |
186 | HandleClient (NewSocket *ns, player *pl) |
186 | HandleClient (client_socket *ns, player *pl) |
187 | { |
187 | { |
188 | /* Loop through this - maybe we have several complete packets here. */ |
188 | /* Loop through this - maybe we have several complete packets here. */ |
189 | // limit to a few commands only, though, as to not monopolise the server |
189 | // limit to a few commands only, though, as to not monopolise the server |
190 | for (int repeat = 16; repeat--;) |
190 | for (int repeat = 16; repeat--;) |
191 | { |
191 | { |
… | |
… | |
264 | |
264 | |
265 | return; |
265 | return; |
266 | } |
266 | } |
267 | } |
267 | } |
268 | |
268 | |
269 | printf ("BP34n");//D |
|
|
270 | /* If we get here, we didn't find a valid command. Logging |
269 | /* If we get here, we didn't find a valid command. Logging |
271 | * this might be questionable, because a broken client/malicious |
270 | * this might be questionable, because a broken client/malicious |
272 | * user could certainly send a whole bunch of invalid commands. |
271 | * user could certainly send a whole bunch of invalid commands. |
273 | */ |
272 | */ |
274 | LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); |
273 | LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); |
… | |
… | |
276 | next_packet: |
275 | next_packet: |
277 | ; |
276 | ; |
278 | } |
277 | } |
279 | } |
278 | } |
280 | |
279 | |
281 | |
|
|
282 | /***************************************************************************** |
|
|
283 | * |
|
|
284 | * Low level socket looping - select calls and watchdog udp packet |
|
|
285 | * sending. |
|
|
286 | * |
|
|
287 | ******************************************************************************/ |
|
|
288 | |
|
|
289 | #ifdef WATCHDOG |
|
|
290 | |
|
|
291 | /** |
|
|
292 | * Tell watchdog that we are still alive |
|
|
293 | * |
|
|
294 | * I put the function here since we should hopefully already be getting |
|
|
295 | * all the needed include files for socket support |
|
|
296 | */ |
|
|
297 | |
|
|
298 | void |
|
|
299 | watchdog (void) |
|
|
300 | { |
|
|
301 | static int fd = -1; |
|
|
302 | static struct sockaddr_in insock; |
|
|
303 | |
|
|
304 | if (fd == -1) |
|
|
305 | { |
|
|
306 | struct protoent *protoent; |
|
|
307 | |
|
|
308 | if ((protoent = getprotobyname ("udp")) == NULL || (fd = socket (PF_INET, SOCK_DGRAM, protoent->p_proto)) == -1) |
|
|
309 | { |
|
|
310 | return; |
|
|
311 | } |
|
|
312 | insock.sin_family = AF_INET; |
|
|
313 | insock.sin_port = htons ((unsigned short) 13325); |
|
|
314 | insock.sin_addr.s_addr = inet_addr ("127.0.0.1"); |
|
|
315 | } |
|
|
316 | sendto (fd, (void *) &fd, 1, 0, (struct sockaddr *) &insock, sizeof (insock)); |
|
|
317 | } |
|
|
318 | #endif |
|
|
319 | |
|
|
320 | void |
280 | void |
321 | flush_sockets (void) |
281 | flush_sockets (void) |
322 | { |
282 | { |
323 | player *pl; |
283 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
324 | |
|
|
325 | for (pl = first_player; pl != NULL; pl = pl->next) |
|
|
326 | if (pl->socket.status != Ns_Dead) |
284 | if ((*i)->status != Ns_Dead) |
327 | Socket_Flush (&pl->socket); |
285 | (*i)->flush (); |
328 | } |
286 | } |
329 | |
287 | |
330 | /** |
288 | /** |
331 | * This checks the sockets for input, does the right thing. |
289 | * This checks the sockets for input, does the right thing. |
332 | * |
290 | * |
… | |
… | |
336 | */ |
294 | */ |
337 | void |
295 | void |
338 | doeric_server (void) |
296 | doeric_server (void) |
339 | { |
297 | { |
340 | int i, pollret; |
298 | int i, pollret; |
341 | fd_set tmp_read, tmp_write; |
299 | fd_set tmp_read; |
342 | struct sockaddr_in addr; |
300 | struct sockaddr_in addr; |
343 | socklen_t addrlen = sizeof (struct sockaddr); |
301 | socklen_t addrlen = sizeof (struct sockaddr); |
344 | player *pl, *next; |
302 | player *pl, *next; |
|
|
303 | int maxfd = 0; |
345 | |
304 | |
346 | #ifdef CS_LOGSTATS |
305 | #ifdef CS_LOGSTATS |
347 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
306 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
348 | write_cs_stats (); |
307 | write_cs_stats (); |
349 | #endif |
308 | #endif |
350 | |
309 | |
351 | FD_ZERO (&tmp_read); |
|
|
352 | FD_ZERO (&tmp_write); |
|
|
353 | |
|
|
354 | for (i = 0; i < socket_info.allocated_sockets; i++) |
|
|
355 | { |
|
|
356 | if (init_sockets[i].status == Ns_Dead) |
|
|
357 | { |
|
|
358 | free_newsocket (&init_sockets[i]); |
|
|
359 | init_sockets[i].status = Ns_Avail; |
|
|
360 | socket_info.nconns--; |
|
|
361 | } |
|
|
362 | else if (init_sockets[i].status != Ns_Avail) |
|
|
363 | { |
|
|
364 | FD_SET (init_sockets[i].fd, &tmp_read); |
|
|
365 | FD_SET (init_sockets[i].fd, &tmp_write); |
|
|
366 | } |
|
|
367 | } |
|
|
368 | |
|
|
369 | /* Go through the players. Let the loop set the next pl value, |
310 | /* Go through the players. Let the loop set the next pl value, |
370 | * since we may remove some |
311 | * since we may remove some |
371 | */ |
312 | */ |
|
|
313 | //TODO: must be handled cleanly elsewhere |
372 | for (pl = first_player; pl != NULL;) |
314 | for (pl = first_player; pl; ) |
373 | { |
315 | { |
|
|
316 | player *npl = pl->next; |
|
|
317 | |
|
|
318 | //TODO: must be handled cleanly elsewhere |
374 | if (pl->socket.status == Ns_Dead) |
319 | if (pl->socket->status == Ns_Dead) |
375 | { |
320 | { |
376 | player *npl = pl->next; |
|
|
377 | |
|
|
378 | save_player (pl->ob, 0); |
321 | save_player (pl->ob, 0); |
379 | |
322 | |
380 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
323 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
381 | { |
324 | { |
382 | terminate_all_pets (pl->ob); |
325 | terminate_all_pets (pl->ob); |
383 | pl->ob->remove (); |
326 | pl->ob->remove (); |
384 | } |
327 | } |
385 | |
328 | |
386 | leave (pl, 1); |
329 | leave (pl, 1); |
387 | final_free_player (pl); |
330 | final_free_player (pl); |
|
|
331 | } |
|
|
332 | |
388 | pl = npl; |
333 | pl = npl; |
|
|
334 | } |
|
|
335 | |
|
|
336 | FD_ZERO (&tmp_read); |
|
|
337 | |
|
|
338 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) |
|
|
339 | { |
|
|
340 | client_socket *s = *i; |
|
|
341 | |
|
|
342 | if (s->status == Ns_Dead) |
|
|
343 | { |
|
|
344 | client_sockets.erase (i); |
|
|
345 | delete s; |
389 | } |
346 | } |
390 | else |
347 | else |
391 | { |
348 | { |
|
|
349 | if (s->fd > maxfd) maxfd = s->fd; |
|
|
350 | |
392 | FD_SET ((uint32) pl->socket.fd, &tmp_read); |
351 | FD_SET (s->fd, &tmp_read); |
393 | FD_SET ((uint32) pl->socket.fd, &tmp_write); |
352 | |
394 | pl = pl->next; |
353 | ++i; |
395 | } |
354 | } |
396 | } |
355 | } |
397 | |
356 | |
398 | /* Reset timeout each time, since some OS's will change the values on |
357 | struct timeval timeout; |
399 | * the return from select. |
358 | |
400 | */ |
|
|
401 | socket_info.timeout.tv_sec = 0; |
359 | timeout.tv_sec = 0; |
402 | socket_info.timeout.tv_usec = 0; |
360 | timeout.tv_usec = 0; |
403 | |
361 | |
404 | pollret = select (socket_info.max_filedescriptor, |
362 | pollret = select (maxfd + 1, |
405 | &tmp_read, &tmp_write, 0, |
363 | &tmp_read, 0, 0, |
406 | &socket_info.timeout); |
364 | &timeout); |
407 | |
365 | |
408 | if (pollret == -1) |
366 | if (pollret == -1) |
409 | { |
367 | { |
410 | LOG (llevError, "select failed: %s\n", strerror (errno)); |
368 | LOG (llevError, "select failed: %s\n", strerror (errno)); |
411 | return; |
369 | return; |
412 | } |
370 | } |
413 | |
371 | |
414 | /* We need to do some of the processing below regardless */ |
372 | /* We need to do some of the processing below regardless */ |
415 | |
373 | |
416 | /* if (!pollret) return;*/ |
374 | /* Check for any input on the sockets */ |
417 | |
375 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
418 | /* Following adds a new connection */ |
|
|
419 | if (pollret && FD_ISSET (init_sockets[0].fd, &tmp_read)) |
|
|
420 | { |
376 | { |
421 | int newsocknum = 0; |
377 | client_socket *s = *i; |
|
|
378 | player *pl = s->pl; |
422 | |
379 | |
423 | #ifdef ESRV_DEBUG |
|
|
424 | LOG (llevDebug, "doeric_server: New Connection\n"); |
|
|
425 | #endif |
|
|
426 | /* If this is the case, all sockets currently in used */ |
|
|
427 | if (socket_info.allocated_sockets <= socket_info.nconns) |
|
|
428 | { |
|
|
429 | init_sockets = (NewSocket *) realloc (init_sockets, sizeof (NewSocket) * (socket_info.nconns + 1)); |
|
|
430 | if (!init_sockets) |
|
|
431 | fatal (OUT_OF_MEMORY); |
|
|
432 | newsocknum = socket_info.allocated_sockets; |
|
|
433 | socket_info.allocated_sockets++; |
|
|
434 | init_sockets[newsocknum].faces_sent_len = nrofpixmaps; |
|
|
435 | init_sockets[newsocknum].faces_sent = (uint8 *) malloc (nrofpixmaps * sizeof (*init_sockets[newsocknum].faces_sent)); |
|
|
436 | if (!init_sockets[newsocknum].faces_sent) |
|
|
437 | fatal (OUT_OF_MEMORY); |
|
|
438 | init_sockets[newsocknum].status = Ns_Avail; |
|
|
439 | } |
|
|
440 | else |
|
|
441 | { |
|
|
442 | int j; |
|
|
443 | |
|
|
444 | for (j = 1; j < socket_info.allocated_sockets; j++) |
|
|
445 | if (init_sockets[j].status == Ns_Avail) |
|
|
446 | { |
|
|
447 | newsocknum = j; |
|
|
448 | break; |
|
|
449 | } |
|
|
450 | } |
|
|
451 | |
|
|
452 | init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen); |
|
|
453 | |
|
|
454 | if (init_sockets[newsocknum].fd == -1) |
|
|
455 | LOG (llevError, "accept failed: %s\n", strerror (errno)); |
|
|
456 | else |
|
|
457 | { |
|
|
458 | char buf[MAX_BUF]; |
|
|
459 | long ip; |
|
|
460 | NewSocket *ns; |
|
|
461 | |
|
|
462 | ns = &init_sockets[newsocknum]; |
|
|
463 | |
|
|
464 | ip = ntohl (addr.sin_addr.s_addr); |
|
|
465 | sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255); |
|
|
466 | |
|
|
467 | if (checkbanned (NULL, buf)) |
|
|
468 | { |
|
|
469 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", buf); |
|
|
470 | close (init_sockets[newsocknum].fd); |
|
|
471 | init_sockets[newsocknum].fd = -1; |
|
|
472 | } |
|
|
473 | else |
|
|
474 | { |
|
|
475 | InitConnection (ns, buf); |
|
|
476 | socket_info.nconns++; |
|
|
477 | } |
|
|
478 | } |
|
|
479 | } |
|
|
480 | |
|
|
481 | /* Check for any input on the sockets */ |
|
|
482 | if (pollret) |
|
|
483 | for (i = 1; i < socket_info.allocated_sockets; i++) |
|
|
484 | { |
|
|
485 | if (init_sockets[i].status == Ns_Avail) |
|
|
486 | continue; |
|
|
487 | |
|
|
488 | //TODO: disassociate handleclient from socket readin |
380 | //TODO: disassociate handleclient from socket readin |
489 | if (init_sockets[i].inbuf_len || FD_ISSET (init_sockets[i].fd, &tmp_read)) |
381 | if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) |
490 | HandleClient (&init_sockets[i], NULL); |
|
|
491 | |
|
|
492 | if (FD_ISSET (init_sockets[i].fd, &tmp_write)) |
|
|
493 | init_sockets[i].can_write = 1; |
|
|
494 | } |
|
|
495 | |
|
|
496 | /* This does roughly the same thing, but for the players now */ |
|
|
497 | for (pl = first_player; pl != NULL; pl = next) |
|
|
498 | { |
|
|
499 | |
|
|
500 | next = pl->next; |
|
|
501 | if (pl->socket.status == Ns_Dead) |
|
|
502 | continue; |
|
|
503 | |
|
|
504 | if (FD_ISSET (pl->socket.fd, &tmp_write)) |
|
|
505 | { |
|
|
506 | if (!pl->socket.can_write) |
|
|
507 | { |
|
|
508 | #if 0 |
|
|
509 | LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name); |
|
|
510 | #endif |
|
|
511 | pl->socket.can_write = 1; |
|
|
512 | write_socket_buffer (&pl->socket); |
|
|
513 | } |
|
|
514 | |
|
|
515 | /* if we get an error on the write_socket buffer, no reason to |
|
|
516 | * continue on this socket. |
|
|
517 | */ |
|
|
518 | if (pl->socket.status == Ns_Dead) |
|
|
519 | continue; |
|
|
520 | } |
|
|
521 | else |
|
|
522 | pl->socket.can_write = 0; |
|
|
523 | |
|
|
524 | HandleClient (&pl->socket, pl); |
382 | HandleClient (s, pl); |
525 | /* If the player has left the game, then the socket status |
383 | |
526 | * will be set to this be the leave function. We don't |
384 | //TODO: should not be done here, either |
527 | * need to call leave again, as it has already been called |
|
|
528 | * once. |
|
|
529 | */ |
|
|
530 | if (pl->socket.status == Ns_Dead) |
385 | if (s->status != Ns_Dead && pl) |
531 | { |
|
|
532 | save_player (pl->ob, 0); |
|
|
533 | |
|
|
534 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
|
|
535 | { |
|
|
536 | terminate_all_pets (pl->ob); |
|
|
537 | pl->ob->remove (); |
|
|
538 | } |
|
|
539 | |
|
|
540 | leave (pl, 1); |
|
|
541 | final_free_player (pl); |
|
|
542 | } |
|
|
543 | else |
|
|
544 | { |
386 | { |
545 | /* Update the players stats once per tick. More efficient than |
387 | /* Update the players stats once per tick. More efficient than |
546 | * sending them whenever they change, and probably just as useful |
388 | * sending them whenever they change, and probably just as useful |
547 | */ |
389 | */ |
548 | esrv_update_stats (pl); |
390 | esrv_update_stats (pl); |
… | |
… | |
556 | |
398 | |
557 | /* draw_client_map does sanity checking that map is |
399 | /* draw_client_map does sanity checking that map is |
558 | * valid, so don't do it here. |
400 | * valid, so don't do it here. |
559 | */ |
401 | */ |
560 | draw_client_map (pl->ob); |
402 | draw_client_map (pl->ob); |
561 | if (pl->socket.update_look) |
403 | if (s->update_look) |
562 | esrv_draw_look (pl->ob); |
404 | esrv_draw_look (pl->ob); |
563 | } |
405 | } |
564 | } |
406 | } |
565 | } |
407 | } |
566 | |
408 | |