ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.C
(Generate patch)

Comparing deliantra/server/socket/lowlevel.C (file contents):
Revision 1.44 by root, Sat Jun 9 22:54:28 2007 UTC vs.
Revision 1.72 by root, Tue Oct 13 18:20:08 2009 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 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 it 7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * under the terms of the GNU General Public License as published by the Free 8 * the terms of the Affero GNU General Public License as published by the
9 * Software Foundation; either version 2 of the License, or (at your option) 9 * Free Software Foundation, either version 3 of the License, or (at your
10 * any later version. 10 * 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 Affero GNU General Public License
18 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 18 * and the GNU General Public License along with this program. If not, see
19 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * <http://www.gnu.org/licenses/>.
20 * 20 *
21 * 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>
22 */
23
24/**
25 * \file
26 * Low-level socket-related functions.
27 *
28 * \date 2003-12-02
29 *
30 * Contains some base functions that both the client and server
31 * can use. As such, depending what we are being compiled for will
32 * determine what we can include. the client is designed have
33 * CFCLIENT defined as part of its compile flags.
34 */ 22 */
35 23
36using namespace std; 24using namespace std;
37 25
38#include <global.h> 26#include <global.h>
39#include <sproto.h> 27#include <sproto.h>
40#include <cstdarg> 28#include <cstdarg>
41 29
42#ifdef __linux__ 30#if HAVE_TCP_INFO
43# include <sys/types.h> 31# include <sys/types.h>
44# include <sys/socket.h> 32# include <sys/socket.h>
45# include <netinet/in.h> 33# include <netinet/in.h>
46# include <netinet/tcp.h> 34# include <netinet/tcp.h>
47#endif 35#endif
48 36
49// disconnect a socket after this many seconds without an ack
50#define SOCKET_TIMEOUT 8.
51
52// force a packet when idle for more than this many seconds, 37// force a packet when idle for more than this many seconds,
53// forcing an ack regularly. 38// forcing an ack regularly.
54#define IDLE_PING 2. 39#define IDLE_PING 2.
55 40
56void 41void
57client::flush () 42client::flush ()
58{ 43{
59 if (destroyed ()) 44 if (destroyed ())
60 return; 45 return;
61
62#ifdef __linux__ //TODO: this is available on bsds, too, use a configure test.
63 // check about once per second, spread evenly over all clients
64 if (!((pticks + fd) & 7))
65 {
66 // check time of last ack, and, if too old, kill connection
67 struct tcp_info tcpi;
68 socklen_t len = sizeof (tcpi);
69
70 if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi))
71 {
72 rtt = tcpi.tcpi_rtt;
73 rttvar = tcpi.tcpi_rttvar;
74
75 if (tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000))
76 {
77 send_msg (NDI_RED, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)");
78 write_outputbuffer ();
79
80 LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
81 (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked);
82 destroy ();
83 }
84 }
85 }
86#endif
87 46
88 /** 47 /**
89 * Writes data to socket. 48 * Writes data to socket.
90 * 49 *
91 * 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
93 */ 52 */
94 53
95 // write a nop to the socket at least every IDLE_NOP seconds. 54 // write a nop to the socket at least every IDLE_NOP seconds.
96 if (!outputbuffer.len) 55 if (!outputbuffer.len)
97 { 56 {
98 if (last_send + IDLE_PING <= NOW) 57 if (last_send + IDLE_PING <= NOW && pl && pl->active)
99 { 58 {
100 // this is a bit ugly, but map1/map1a seem to be the only 59 // this is a bit ugly, but map1/map1a seem to be the only
101 // nop'able commands and they are quite small. 60 // nop'able commands and they are quite small.
102 packet sl (mapmode == Map1Cmd ? "map1" : "map1a"); 61 packet sl (mapmode == Map1Cmd ? "map1" : "map1a");
103 send_packet (sl); 62 send_packet (sl);
104 } 63 }
105 else 64 else
106 return; 65 return;
107 } 66 }
108 67
109 if (socket_ev.poll () & PE_W) 68 if (socket_ev.poll () & EV_WRITE)
110 return; 69 return;
111 70
112 last_send = NOW; 71 last_send = NOW;
113 write_outputbuffer (); 72 write_outputbuffer ();
114} 73}
141 // just retry 100 // just retry
142 } 101 }
143 else if (errno == EAGAIN) 102 else if (errno == EAGAIN)
144 { 103 {
145 // delay till ready 104 // delay till ready
146 socket_ev.poll (socket_ev.poll () | PE_W); 105 socket_ev.poll (socket_ev.poll () | EV_WRITE);
147 socket_ev.start (); 106 socket_ev.start ();
148 return; 107 return;
149 } 108 }
150 else 109 else
151 { 110 {
153 destroy (); 112 destroy ();
154 return; 113 return;
155 } 114 }
156 } 115 }
157 116
158 socket_ev.poll (socket_ev.poll () & ~PE_W); 117 socket_ev.poll (socket_ev.poll () & ~EV_WRITE);
159} 118}
160 119
161/****************************************************************************** 120/******************************************************************************
162 * 121 *
163 * Start of read routines. 122 * Start of read routines.
174 if (inbuf_len >= 2 + pkt_len) 133 if (inbuf_len >= 2 + pkt_len)
175 return 2 + pkt_len; 134 return 2 + pkt_len;
176 135
177 if (inbuf_len == sizeof (inbuf)) 136 if (inbuf_len == sizeof (inbuf))
178 { 137 {
179 send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); 138 send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED | NDI_REPLY);
180 destroy (); 139 destroy ();
181 return -1; 140 return -1;
182 } 141 }
183 } 142 }
184 143
222 {"reply", SC(ReplyCmd) 0 }, 181 {"reply", SC(ReplyCmd) 0 },
223 {"exti", SC(ExtiCmd) 0 }, // CF+ 182 {"exti", SC(ExtiCmd) 0 }, // CF+
224 {"addme", SC(AddMeCmd) 0 }, 183 {"addme", SC(AddMeCmd) 0 },
225 {"askface", SC(AskFaceCmd) 0 }, 184 {"askface", SC(AskFaceCmd) 0 },
226 {"requestinfo", SC(RequestInfo) 0 }, 185 {"requestinfo", SC(RequestInfo) 0 },
227 {"setfacemode", SC(SetFaceMode) 0 },
228 {"setsound", SC(SetSound) 0 }, 186 {"setsound", SC(SetSound) 0 },
229 {"setup", SC(SetUp) 0 }, 187 {"setup", SC(SetUp) 0 },
230 {"version", SC(VersionCmd) 0 }, 188 {"version", SC(VersionCmd) 0 },
231 {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */
232 {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ 189 {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */
233 {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */
234}; 190};
235 191
236bool 192bool
237client::may_execute (const packet_type *pkt) const 193client::may_execute (const packet_type *pkt) const
238{ 194{
258 { 214 {
259 data += 6; 215 data += 6;
260 len -= 6; 216 len -= 6;
261 } 217 }
262 218
263 if (len > 4 && !strncmp (data, "say " , 4)) 219 if (len > 4 && data [ 3] == ' ' && !strncmp (data, "say " , 4)) return true;
264 return true; 220 if (len > 5 && data [ 4] == ' ' && !strncmp (data, "chat " , 5)) return true;
265 if (len > 5 && !strncmp (data, "chat ", 5)) 221 if (len > 6 && data [ 5] == ' ' && !strncmp (data, "shout " , 6)) return true;
266 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;
267 226
268 return false; 227 return false;
269} 228}
270 229
271void 230void
278 ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); 237 ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl);
279 else 238 else
280 ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); 239 ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this);
281 } 240 }
282 else 241 else
283 send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); 242 send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name);
284} 243}
285 244
286bool 245bool
287client::handle_packet () 246client::handle_packet ()
288{ 247{
296 pl && pl->ob ? &pl->ob->name : "[anonymous]"); 255 pl && pl->ob ? &pl->ob->name : "[anonymous]");
297 destroy (); 256 destroy ();
298 return false; 257 return false;
299 } 258 }
300 259
260 uint8_t save_byte = inbuf [pkt_len]; // rather ugly
301 inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ 261 inbuf [pkt_len] = 0; /* temporarily terminate buffer - useful for string data */
302 262
303 /* First, break out beginning word. There are at least 263 /* First, break out beginning word. There are at least
304 * a few commands that do not have any paremeters. If 264 * a few commands that do not have any paremeters. If
305 * we get such a command, don't worry about trying 265 * we get such a command, don't worry about trying
306 * to break it up. 266 * to break it up.
329 289
330 goto next_packet; 290 goto next_packet;
331 } 291 }
332 292
333 // If we get here, we didn't find a valid command. 293 // If we get here, we didn't find a valid command.
334 send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); 294 send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2);
295
335next_packet: 296next_packet:
297 inbuf [pkt_len] = save_byte; // rather ugly
336 skip_packet (pkt_len); 298 skip_packet (pkt_len);
337 299
338 // input buffer has space again 300 // input buffer has space again
339 socket_ev.poll (socket_ev.poll () | PE_R); 301 socket_ev.poll (socket_ev.poll () | EV_READ);
340 302
341 return true; 303 return true;
342} 304}
343 305
344// callback called when socket is either readable or writable 306// callback called when socket is either readable or writable
345void 307void
346client::socket_cb (iow &w, int got) 308client::socket_cb (iow &w, int revents)
347{ 309{
348 //TODO remove when we have better socket cleanup logic 310 //TODO remove when we have better socket cleanup logic
349 if (destroyed ()) 311 if (destroyed ())
350 { 312 {
351 socket_ev.poll (0); 313 socket_ev.poll (0);
352 return; 314 return;
353 } 315 }
354 316
355 if (got & PE_W) 317 if (revents & EV_WRITE)
356 { 318 {
357 write_outputbuffer (); 319 write_outputbuffer ();
358 320
359 if (!outputbuffer.len) 321 if (!outputbuffer.len)
360 socket_ev.poll (socket_ev.poll () & ~PE_W); 322 socket_ev.poll (socket_ev.poll () & ~EV_WRITE);
361 } 323 }
362 324
363 if (got & PE_R) 325 if (revents & EV_READ)
364 { 326 {
365 //TODO: rate-limit tcp connection in better ways, important 327 //TODO: rate-limit tcp connection in better ways, important
366 328
367 int amount = sizeof (inbuf) - inbuf_len; 329 int amount = sizeof (inbuf) - inbuf_len;
368 330
369 if (!amount) 331 if (!amount)
370 { 332 {
371 // input buffer full 333 // input buffer full
372 socket_ev.poll (socket_ev.poll () & ~PE_R); 334 socket_ev.poll (socket_ev.poll () & ~EV_READ);
373 return; 335 return;
374 } 336 }
375 337
376 amount = read (fd, inbuf + inbuf_len, amount); 338 amount = read (fd, inbuf + inbuf_len, amount);
377 339
393 } 355 }
394 else 356 else
395 { 357 {
396 inbuf_len += amount; 358 inbuf_len += amount;
397 359
398 cmd_ev.start ();
399 }
400 }
401}
402
403// called whenever we have additional commands to process
404void
405client::cmd_cb (iw &w)
406{
407 if (handle_packet ()) 360 if (handle_packet ())
408 w.start (); 361 {
409 else 362 while (handle_packet ())
410 flush (); 363 ;
364
365 flush ();
366 }
367 }
368 }
411} 369}
412 370
413/******************************************************************************* 371/*******************************************************************************
414 * 372 *
415 * Start of write related routines. 373 * Start of write related routines.
471client::send_packet (packet &sl) 429client::send_packet (packet &sl)
472{ 430{
473 if (destroyed ()) 431 if (destroyed ())
474 return; 432 return;
475 433
476 if (sl.length () >= MAXSOCKBUF) 434 if (sl.length () > MAXSOCKBUF)
477 { 435 {
478 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); 436 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ());
479 /* Almost certainly we've overflowed a buffer, so quit now to make 437 /* Almost certainly we've overflowed a buffer, so quit now to make
480 * it easier to debug. 438 * it easier to debug.
481 */ 439 */
520 478
521 send_packet (sl); 479 send_packet (sl);
522} 480}
523 481
524void 482void
483client::send_msg (int color, const char *type, const char *msg)
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)
494 && (msg_is_special (msg)
495 || (type [0] == 'c' && type [1] == '/') || len > (MAXSOCKBUF - 128)))
496 cfperl_send_msg (this, color, type, msg);
497 else
498 send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg);
499}
500
501void
525client::send_drawinfo (const char *msg, int flags) 502client::send_drawinfo (const char *msg, int flags)
526{ 503{
527 send_packet_printf ("drawinfo %d %s", flags, msg); 504 send_msg (flags, "log", msg);
528}
529
530void
531client::send_msg (int color, const char *type, const char *msg)
532{
533 if (can_msg)
534 send_packet_printf ("msg %d %s %s", color, type, msg);
535 else if (color < 0)
536 return; // client cannot handle this
537 else if (strchr (msg, '<') || strchr (msg, '&'))
538 {
539 //TODO: should escape/modify to old syntax
540 send_packet_printf ("drawinfo %d %s", color, msg);
541 }
542 else
543 send_packet_printf ("drawinfo %d %s", color, msg);
544} 505}
545 506
546/*********************************************************************** 507/***********************************************************************
547 * 508 *
548 * packet functions/utilities 509 * packet functions/utilities

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines