1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (©) 1992,2007 Frank Tore Johansen |
5 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * |
6 | * |
7 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
7 | * Crossfire TRT is free software: you can redistribute it and/or modify |
8 | * under the terms of the GNU General Public License as published by the Free |
8 | * it under the terms of the GNU General Public License as published by |
9 | * Software Foundation; either version 2 of the License, or (at your option) |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * any later version. |
10 | * (at your option) any later version. |
11 | * |
11 | * |
12 | * This program is distributed in the hope that it will be useful, but |
12 | * This program is distributed in the hope that it will be useful, |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * for more details. |
15 | * GNU General Public License for more details. |
16 | * |
16 | * |
17 | * You should have received a copy of the GNU General Public License along |
17 | * You should have received a copy of the GNU General Public License |
18 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
20 | * |
19 | * |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
22 | */ |
21 | */ |
23 | |
22 | |
24 | using namespace std; |
23 | using namespace std; |
… | |
… | |
32 | # include <sys/socket.h> |
31 | # include <sys/socket.h> |
33 | # include <netinet/in.h> |
32 | # include <netinet/in.h> |
34 | # include <netinet/tcp.h> |
33 | # include <netinet/tcp.h> |
35 | #endif |
34 | #endif |
36 | |
35 | |
37 | // disconnect a socket after this many seconds without an ack |
|
|
38 | #define SOCKET_TIMEOUT 8. |
|
|
39 | |
|
|
40 | // force a packet when idle for more than this many seconds, |
36 | // force a packet when idle for more than this many seconds, |
41 | // forcing an ack regularly. |
37 | // forcing an ack regularly. |
42 | #define IDLE_PING 2. |
38 | #define IDLE_PING 2. |
43 | |
39 | |
44 | void |
40 | void |
45 | client::flush () |
41 | client::flush () |
46 | { |
42 | { |
47 | if (destroyed ()) |
43 | if (destroyed ()) |
48 | return; |
44 | return; |
49 | |
|
|
50 | #if HAVE_TCP_INFO |
|
|
51 | // check about once per second, spread evenly over all clients |
|
|
52 | // do this only when player is active |
|
|
53 | if (!((pticks + fd) & 7) && pl && pl->active) |
|
|
54 | { |
|
|
55 | // check time of last ack, and, if too old, kill connection |
|
|
56 | struct tcp_info tcpi; |
|
|
57 | socklen_t len = sizeof (tcpi); |
|
|
58 | |
|
|
59 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
|
|
60 | { |
|
|
61 | if (tcpi.tcpi_snd_mss) |
|
|
62 | mss = tcpi.tcpi_snd_mss; |
|
|
63 | |
|
|
64 | rtt = tcpi.tcpi_rtt; |
|
|
65 | rttvar = tcpi.tcpi_rttvar; |
|
|
66 | |
|
|
67 | if (tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
|
|
68 | { |
|
|
69 | send_msg (NDI_RED, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
|
|
70 | write_outputbuffer (); |
|
|
71 | |
|
|
72 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
|
|
73 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
|
|
74 | destroy (); |
|
|
75 | } |
|
|
76 | } |
|
|
77 | } |
|
|
78 | #endif |
|
|
79 | |
45 | |
80 | /** |
46 | /** |
81 | * Writes data to socket. |
47 | * Writes data to socket. |
82 | * |
48 | * |
83 | * When the socket is clear to write, and we have backlogged data, this |
49 | * When the socket is clear to write, and we have backlogged data, this |
… | |
… | |
166 | if (inbuf_len >= 2 + pkt_len) |
132 | if (inbuf_len >= 2 + pkt_len) |
167 | return 2 + pkt_len; |
133 | return 2 + pkt_len; |
168 | |
134 | |
169 | if (inbuf_len == sizeof (inbuf)) |
135 | if (inbuf_len == sizeof (inbuf)) |
170 | { |
136 | { |
171 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); |
137 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED | NDI_REPLY); |
172 | destroy (); |
138 | destroy (); |
173 | return -1; |
139 | return -1; |
174 | } |
140 | } |
175 | } |
141 | } |
176 | |
142 | |
… | |
… | |
270 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
236 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
271 | else |
237 | else |
272 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
238 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
273 | } |
239 | } |
274 | else |
240 | else |
275 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); |
241 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name); |
276 | } |
242 | } |
277 | |
243 | |
278 | bool |
244 | bool |
279 | client::handle_packet () |
245 | client::handle_packet () |
280 | { |
246 | { |
… | |
… | |
321 | |
287 | |
322 | goto next_packet; |
288 | goto next_packet; |
323 | } |
289 | } |
324 | |
290 | |
325 | // If we get here, we didn't find a valid command. |
291 | // If we get here, we didn't find a valid command. |
326 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
292 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
327 | next_packet: |
293 | next_packet: |
328 | skip_packet (pkt_len); |
294 | skip_packet (pkt_len); |
329 | |
295 | |
330 | // input buffer has space again |
296 | // input buffer has space again |
331 | socket_ev.poll (socket_ev.poll () | PE_R); |
297 | socket_ev.poll (socket_ev.poll () | PE_R); |
… | |
… | |
385 | } |
351 | } |
386 | else |
352 | else |
387 | { |
353 | { |
388 | inbuf_len += amount; |
354 | inbuf_len += amount; |
389 | |
355 | |
390 | cmd_ev.start (); |
|
|
391 | } |
|
|
392 | } |
|
|
393 | } |
|
|
394 | |
|
|
395 | // called whenever we have additional commands to process |
|
|
396 | void |
|
|
397 | client::cmd_cb (iw &w) |
|
|
398 | { |
|
|
399 | if (handle_packet ()) |
356 | if (handle_packet ()) |
400 | w.start (); |
357 | { |
401 | else |
358 | while (handle_packet ()) |
402 | flush (); |
359 | ; |
|
|
360 | |
|
|
361 | flush (); |
|
|
362 | } |
|
|
363 | } |
|
|
364 | } |
403 | } |
365 | } |
404 | |
366 | |
405 | /******************************************************************************* |
367 | /******************************************************************************* |
406 | * |
368 | * |
407 | * Start of write related routines. |
369 | * Start of write related routines. |
… | |
… | |
463 | client::send_packet (packet &sl) |
425 | client::send_packet (packet &sl) |
464 | { |
426 | { |
465 | if (destroyed ()) |
427 | if (destroyed ()) |
466 | return; |
428 | return; |
467 | |
429 | |
468 | if (sl.length () >= MAXSOCKBUF) |
430 | if (sl.length () > MAXSOCKBUF) |
469 | { |
431 | { |
470 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
432 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
471 | /* Almost certainly we've overflowed a buffer, so quit now to make |
433 | /* Almost certainly we've overflowed a buffer, so quit now to make |
472 | * it easier to debug. |
434 | * it easier to debug. |
473 | */ |
435 | */ |
… | |
… | |
521 | } |
483 | } |
522 | |
484 | |
523 | void |
485 | void |
524 | client::send_msg (int color, const char *type, const char *msg) |
486 | client::send_msg (int color, const char *type, const char *msg) |
525 | { |
487 | { |
526 | if (msg_is_special (msg)) |
488 | if (!msg || !type) // it can happen (for example, missing attack messages cause this) |
|
|
489 | { |
|
|
490 | LOG (logBacktrace | llevError, "send_msg(%d,%p,%p) called with NULL msg or type.\n", color, type, msg); |
|
|
491 | return; |
|
|
492 | } |
|
|
493 | |
|
|
494 | int len = strlen (msg); |
|
|
495 | |
|
|
496 | if (msg_is_special (msg) || (type [0] == 'c' && type [1] == '/') || len > (MAXSOCKBUF - 128)) |
527 | cfperl_send_msg (this, color, type, msg); |
497 | cfperl_send_msg (this, color, type, msg); |
528 | else if (can_msg) |
498 | else if (can_msg) |
529 | send_packet_printf ("msg %d %s %s", color, type, msg); |
499 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
530 | else if (color < 0) |
500 | else if (color < 0) |
531 | return; // client cannot handle this |
501 | return; // client cannot handle this |
532 | else |
502 | else |
533 | send_packet_printf ("drawinfo %d %s", color, msg); |
503 | send_packet_printf ("drawinfo %d %s", color & NDI_COLOR_MASK, msg); |
534 | } |
504 | } |
535 | |
505 | |
536 | void |
506 | void |
537 | client::send_drawinfo (const char *msg, int flags) |
507 | client::send_drawinfo (const char *msg, int flags) |
538 | { |
508 | { |