1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * it under the terms of the GNU General Public License as published by |
8 | * the terms of the Affero GNU General Public License as published by the |
9 | * the Free Software Foundation, either version 3 of the License, or |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * (at your option) any later version. |
10 | * option) any later version. |
11 | * |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License 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 |
17 | * You should have received a copy of the Affero GNU General Public License |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | using namespace std; |
24 | using namespace std; |
24 | |
25 | |
25 | #include <global.h> |
26 | #include <global.h> |
… | |
… | |
31 | # include <sys/socket.h> |
32 | # include <sys/socket.h> |
32 | # include <netinet/in.h> |
33 | # include <netinet/in.h> |
33 | # include <netinet/tcp.h> |
34 | # include <netinet/tcp.h> |
34 | #endif |
35 | #endif |
35 | |
36 | |
36 | // disconnect a socket after this many seconds without an ack |
|
|
37 | #define SOCKET_TIMEOUT 8. |
|
|
38 | |
|
|
39 | // force a packet when idle for more than this many seconds, |
37 | // force a packet when idle for more than this many seconds, |
40 | // forcing an ack regularly. |
38 | // forcing an ack regularly. |
41 | #define IDLE_PING 2. |
39 | #define IDLE_PING 2. |
42 | |
40 | |
43 | void |
41 | void |
44 | client::flush () |
42 | client::flush () |
45 | { |
43 | { |
46 | if (destroyed ()) |
44 | if (destroyed ()) |
47 | return; |
45 | return; |
48 | |
|
|
49 | #if HAVE_TCP_INFO |
|
|
50 | // check time of last ack, and, if too old, kill connection |
|
|
51 | socklen_t len = sizeof (tcpi); |
|
|
52 | |
|
|
53 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
|
|
54 | { |
|
|
55 | if (tcpi.tcpi_snd_mss) |
|
|
56 | mss = tcpi.tcpi_snd_mss; |
|
|
57 | |
|
|
58 | #if 0 |
|
|
59 | fprintf (stderr, "uack %d ack %d lost %d ret %d fack %d sst %d cwnd %d mss %d pmtu %d advmss %d EXC %d\n", |
|
|
60 | tcpi.tcpi_unacked, |
|
|
61 | tcpi.tcpi_sacked, |
|
|
62 | tcpi.tcpi_lost, |
|
|
63 | tcpi.tcpi_retrans, |
|
|
64 | tcpi.tcpi_fackets, |
|
|
65 | tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, |
|
|
66 | |
|
|
67 | tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); |
|
|
68 | #endif |
|
|
69 | |
|
|
70 | // do this only when player is active |
|
|
71 | if (pl && pl->active |
|
|
72 | && tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
|
|
73 | { |
|
|
74 | send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
|
|
75 | write_outputbuffer (); |
|
|
76 | |
|
|
77 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
|
|
78 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
|
|
79 | destroy (); |
|
|
80 | } |
|
|
81 | } |
|
|
82 | #endif |
|
|
83 | |
46 | |
84 | /** |
47 | /** |
85 | * Writes data to socket. |
48 | * Writes data to socket. |
86 | * |
49 | * |
87 | * When the socket is clear to write, and we have backlogged data, this |
50 | * When the socket is clear to write, and we have backlogged data, this |
… | |
… | |
100 | } |
63 | } |
101 | else |
64 | else |
102 | return; |
65 | return; |
103 | } |
66 | } |
104 | |
67 | |
105 | if (socket_ev.poll () & PE_W) |
68 | if (socket_ev.poll () & EV_WRITE) |
106 | return; |
69 | return; |
107 | |
70 | |
108 | last_send = NOW; |
71 | last_send = NOW; |
109 | write_outputbuffer (); |
72 | write_outputbuffer (); |
110 | } |
73 | } |
… | |
… | |
137 | // just retry |
100 | // just retry |
138 | } |
101 | } |
139 | else if (errno == EAGAIN) |
102 | else if (errno == EAGAIN) |
140 | { |
103 | { |
141 | // delay till ready |
104 | // delay till ready |
142 | socket_ev.poll (socket_ev.poll () | PE_W); |
105 | socket_ev.poll (socket_ev.poll () | EV_WRITE); |
143 | socket_ev.start (); |
106 | socket_ev.start (); |
144 | return; |
107 | return; |
145 | } |
108 | } |
146 | else |
109 | else |
147 | { |
110 | { |
… | |
… | |
149 | destroy (); |
112 | destroy (); |
150 | return; |
113 | return; |
151 | } |
114 | } |
152 | } |
115 | } |
153 | |
116 | |
154 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
117 | socket_ev.poll (socket_ev.poll () & ~EV_WRITE); |
155 | } |
118 | } |
156 | |
119 | |
157 | /****************************************************************************** |
120 | /****************************************************************************** |
158 | * |
121 | * |
159 | * Start of read routines. |
122 | * Start of read routines. |
… | |
… | |
218 | {"reply", SC(ReplyCmd) 0 }, |
181 | {"reply", SC(ReplyCmd) 0 }, |
219 | {"exti", SC(ExtiCmd) 0 }, // CF+ |
182 | {"exti", SC(ExtiCmd) 0 }, // CF+ |
220 | {"addme", SC(AddMeCmd) 0 }, |
183 | {"addme", SC(AddMeCmd) 0 }, |
221 | {"askface", SC(AskFaceCmd) 0 }, |
184 | {"askface", SC(AskFaceCmd) 0 }, |
222 | {"requestinfo", SC(RequestInfo) 0 }, |
185 | {"requestinfo", SC(RequestInfo) 0 }, |
223 | {"setfacemode", SC(SetFaceMode) 0 }, |
|
|
224 | {"setsound", SC(SetSound) 0 }, |
186 | {"setsound", SC(SetSound) 0 }, |
225 | {"setup", SC(SetUp) 0 }, |
187 | {"setup", SC(SetUp) 0 }, |
226 | {"version", SC(VersionCmd) 0 }, |
188 | {"version", SC(VersionCmd) 0 }, |
227 | {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */ |
|
|
228 | {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ |
189 | {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ |
229 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
|
|
230 | }; |
190 | }; |
231 | |
191 | |
232 | bool |
192 | bool |
233 | client::may_execute (const packet_type *pkt) const |
193 | client::may_execute (const packet_type *pkt) const |
234 | { |
194 | { |
… | |
… | |
254 | { |
214 | { |
255 | data += 6; |
215 | data += 6; |
256 | len -= 6; |
216 | len -= 6; |
257 | } |
217 | } |
258 | |
218 | |
259 | if (len > 4 && !strncmp (data, "say " , 4)) |
219 | if (len > 4 && data [ 3] == ' ' && !strncmp (data, "say " , 4)) return true; |
260 | return true; |
220 | if (len > 5 && data [ 4] == ' ' && !strncmp (data, "chat " , 5)) return true; |
261 | if (len > 5 && !strncmp (data, "chat ", 5)) |
221 | if (len > 6 && data [ 5] == ' ' && !strncmp (data, "shout " , 6)) return true; |
262 | return true; |
222 | if (len > 8 && data [ 7] == ' ' && !strncmp (data, "suicide " , 8)) return true; |
|
|
223 | if (len > 18 && data [17] == ' ' && !strncmp (data, "accept-invitation ", 18)) return true; |
|
|
224 | |
|
|
225 | if (len == 7 && !strcmp (data, "suicide")) return true; |
263 | |
226 | |
264 | return false; |
227 | return false; |
265 | } |
228 | } |
266 | |
229 | |
267 | void |
230 | void |
… | |
… | |
292 | pl && pl->ob ? &pl->ob->name : "[anonymous]"); |
255 | pl && pl->ob ? &pl->ob->name : "[anonymous]"); |
293 | destroy (); |
256 | destroy (); |
294 | return false; |
257 | return false; |
295 | } |
258 | } |
296 | |
259 | |
|
|
260 | uint8_t save_byte = inbuf [pkt_len]; // rather ugly |
297 | inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ |
261 | inbuf [pkt_len] = 0; /* temporarily terminate buffer - useful for string data */ |
298 | |
262 | |
299 | /* First, break out beginning word. There are at least |
263 | /* First, break out beginning word. There are at least |
300 | * a few commands that do not have any paremeters. If |
264 | * a few commands that do not have any paremeters. If |
301 | * we get such a command, don't worry about trying |
265 | * we get such a command, don't worry about trying |
302 | * to break it up. |
266 | * to break it up. |
… | |
… | |
326 | goto next_packet; |
290 | goto next_packet; |
327 | } |
291 | } |
328 | |
292 | |
329 | // If we get here, we didn't find a valid command. |
293 | // If we get here, we didn't find a valid command. |
330 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
294 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
|
|
295 | |
331 | next_packet: |
296 | next_packet: |
|
|
297 | inbuf [pkt_len] = save_byte; // rather ugly |
332 | skip_packet (pkt_len); |
298 | skip_packet (pkt_len); |
333 | |
299 | |
334 | // input buffer has space again |
300 | // input buffer has space again |
335 | socket_ev.poll (socket_ev.poll () | PE_R); |
301 | socket_ev.poll (socket_ev.poll () | EV_READ); |
336 | |
302 | |
337 | return true; |
303 | return true; |
338 | } |
304 | } |
339 | |
305 | |
340 | // callback called when socket is either readable or writable |
306 | // callback called when socket is either readable or writable |
341 | void |
307 | void |
342 | client::socket_cb (iow &w, int got) |
308 | client::socket_cb (iow &w, int revents) |
343 | { |
309 | { |
344 | //TODO remove when we have better socket cleanup logic |
310 | //TODO remove when we have better socket cleanup logic |
345 | if (destroyed ()) |
311 | if (destroyed ()) |
346 | { |
312 | { |
347 | socket_ev.poll (0); |
313 | socket_ev.poll (0); |
348 | return; |
314 | return; |
349 | } |
315 | } |
350 | |
316 | |
351 | if (got & PE_W) |
317 | if (revents & EV_WRITE) |
352 | { |
318 | { |
353 | write_outputbuffer (); |
319 | write_outputbuffer (); |
354 | |
320 | |
355 | if (!outputbuffer.len) |
321 | if (!outputbuffer.len) |
356 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
322 | socket_ev.poll (socket_ev.poll () & ~EV_WRITE); |
357 | } |
323 | } |
358 | |
324 | |
359 | if (got & PE_R) |
325 | if (revents & EV_READ) |
360 | { |
326 | { |
361 | //TODO: rate-limit tcp connection in better ways, important |
327 | //TODO: rate-limit tcp connection in better ways, important |
362 | |
328 | |
363 | int amount = sizeof (inbuf) - inbuf_len; |
329 | int amount = sizeof (inbuf) - inbuf_len; |
364 | |
330 | |
365 | if (!amount) |
331 | if (!amount) |
366 | { |
332 | { |
367 | // input buffer full |
333 | // input buffer full |
368 | socket_ev.poll (socket_ev.poll () & ~PE_R); |
334 | socket_ev.poll (socket_ev.poll () & ~EV_READ); |
369 | return; |
335 | return; |
370 | } |
336 | } |
371 | |
337 | |
372 | amount = read (fd, inbuf + inbuf_len, amount); |
338 | amount = read (fd, inbuf + inbuf_len, amount); |
373 | |
339 | |
… | |
… | |
389 | } |
355 | } |
390 | else |
356 | else |
391 | { |
357 | { |
392 | inbuf_len += amount; |
358 | inbuf_len += amount; |
393 | |
359 | |
394 | cmd_ev.start (); |
|
|
395 | } |
|
|
396 | } |
|
|
397 | } |
|
|
398 | |
|
|
399 | // called whenever we have additional commands to process |
|
|
400 | void |
|
|
401 | client::cmd_cb (iw &w) |
|
|
402 | { |
|
|
403 | if (handle_packet ()) |
360 | if (handle_packet ()) |
404 | w.start (); |
361 | { |
405 | else |
362 | while (handle_packet ()) |
406 | flush (); |
363 | ; |
|
|
364 | |
|
|
365 | flush (); |
|
|
366 | } |
|
|
367 | } |
|
|
368 | } |
407 | } |
369 | } |
408 | |
370 | |
409 | /******************************************************************************* |
371 | /******************************************************************************* |
410 | * |
372 | * |
411 | * Start of write related routines. |
373 | * Start of write related routines. |
… | |
… | |
515 | va_end (ap); |
477 | va_end (ap); |
516 | |
478 | |
517 | send_packet (sl); |
479 | send_packet (sl); |
518 | } |
480 | } |
519 | |
481 | |
520 | // returns true when the message needs special (read: perl) treatment |
|
|
521 | static bool |
|
|
522 | msg_is_special (const char *msg) |
|
|
523 | { |
|
|
524 | return msg [strcspn (msg, "<[&\n")]; |
|
|
525 | } |
|
|
526 | |
|
|
527 | void |
482 | void |
528 | client::send_msg (int color, const char *type, const char *msg) |
483 | client::send_msg (int color, const char *type, const char *msg) |
529 | { |
484 | { |
|
|
485 | if (!msg || !type) // it can happen (for example, missing attack messages cause this) |
|
|
486 | { |
|
|
487 | LOG (logBacktrace | llevError, "send_msg(%d,%p,%p) called with NULL msg or type.\n", color, type, msg); |
|
|
488 | return; |
|
|
489 | } |
|
|
490 | |
|
|
491 | int len = strlen (msg); |
|
|
492 | |
|
|
493 | if (!(color & NDI_VERBATIM) |
530 | if (msg_is_special (msg)) |
494 | && (msg_is_special (msg) |
|
|
495 | || (type [0] == 'c' && type [1] == '/') || len > (MAXSOCKBUF - 128))) |
531 | cfperl_send_msg (this, color, type, msg); |
496 | cfperl_send_msg (this, color, type, msg); |
532 | else if (can_msg) |
497 | else |
533 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
498 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
534 | else if (color < 0) |
|
|
535 | return; // client cannot handle this |
|
|
536 | else |
|
|
537 | send_packet_printf ("drawinfo %d %s", color & NDI_COLOR_MASK, msg); |
|
|
538 | } |
499 | } |
539 | |
500 | |
540 | void |
501 | void |
541 | client::send_drawinfo (const char *msg, int flags) |
502 | client::send_drawinfo (const char *msg, int flags) |
542 | { |
503 | { |