1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
… | |
… | |
132 | if (inbuf_len >= 2 + pkt_len) |
133 | if (inbuf_len >= 2 + pkt_len) |
133 | return 2 + pkt_len; |
134 | return 2 + pkt_len; |
134 | |
135 | |
135 | if (inbuf_len == sizeof (inbuf)) |
136 | if (inbuf_len == sizeof (inbuf)) |
136 | { |
137 | { |
137 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED | NDI_REPLY); |
138 | send_packet_printf ("msg %d log input buffer overflow - closing connection.", NDI_RED | NDI_REPLY); |
138 | destroy (); |
139 | destroy (); |
139 | return -1; |
140 | return -1; |
140 | } |
141 | } |
141 | } |
142 | } |
142 | |
143 | |
… | |
… | |
150 | memmove (inbuf, inbuf + len, inbuf_len); |
151 | memmove (inbuf, inbuf + len, inbuf_len); |
151 | } |
152 | } |
152 | |
153 | |
153 | /***************************************************************************** |
154 | /***************************************************************************** |
154 | * Start of command dispatch area. |
155 | * Start of command dispatch area. |
155 | * The commands here are protocol commands. |
156 | * The commands here are protocol commands. |
156 | ****************************************************************************/ |
157 | ****************************************************************************/ |
157 | |
158 | |
158 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
159 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
159 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb), |
160 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb), |
160 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
161 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
… | |
… | |
167 | |
168 | |
168 | /** |
169 | /** |
169 | * Dispatch table for the server. |
170 | * Dispatch table for the server. |
170 | */ |
171 | */ |
171 | static struct packet_type packets[] = { |
172 | static struct packet_type packets[] = { |
172 | {"ncom", PC(NewPlayerCmd) PF_PLAYING | PF_COMMAND6 }, |
|
|
173 | {"command", PC(PlayerCmd) PF_PLAYING | PF_COMMAND0 }, |
173 | {"command", PC(PlayerCmd) PF_PLAYING | PF_COMMAND }, |
174 | |
174 | |
175 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
175 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
176 | {"ex", PC(ExCmd) PF_PLAYING }, |
176 | {"ex", PC(ExCmd) PF_PLAYING }, |
177 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
177 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
178 | {"lookat", PC(LookAt) PF_PLAYING }, |
178 | {"lookat", PC(LookAt) PF_PLAYING }, |
… | |
… | |
208 | // but kept seperate to emphasise the hack aspect, i.e. |
208 | // but kept seperate to emphasise the hack aspect, i.e. |
209 | // do it better, then remove. |
209 | // do it better, then remove. |
210 | static bool |
210 | static bool |
211 | always_immediate (const client *ns, const packet_type *pkt, const char *data, int len) |
211 | always_immediate (const client *ns, const packet_type *pkt, const char *data, int len) |
212 | { |
212 | { |
213 | if (!(pkt->flags & (PF_COMMAND0 | PF_COMMAND6))) |
213 | if (!(pkt->flags & PF_COMMAND)) |
214 | return false; |
214 | return false; |
215 | |
215 | |
216 | if (!ns->pl || !ns->pl->ob || !ns->pl->ob->map) |
216 | if (!ns->pl || !ns->pl->ob || !ns->pl->ob->map) |
217 | return false; |
217 | return false; |
218 | |
|
|
219 | if (pkt->flags & PF_COMMAND6) |
|
|
220 | { |
|
|
221 | data += 6; |
|
|
222 | len -= 6; |
|
|
223 | } |
|
|
224 | |
218 | |
225 | if (len > 4 && data [ 3] == ' ' && !strncmp (data, "say " , 4)) return true; |
219 | if (len > 4 && data [ 3] == ' ' && !strncmp (data, "say " , 4)) return true; |
226 | if (len > 5 && data [ 4] == ' ' && !strncmp (data, "chat " , 5)) return true; |
220 | if (len > 5 && data [ 4] == ' ' && !strncmp (data, "chat " , 5)) return true; |
227 | if (len > 6 && data [ 5] == ' ' && !strncmp (data, "shout " , 6)) return true; |
221 | if (len > 6 && data [ 5] == ' ' && !strncmp (data, "shout " , 6)) return true; |
228 | if (len > 8 && data [ 7] == ' ' && !strncmp (data, "suicide " , 8)) return true; |
222 | if (len > 8 && data [ 7] == ' ' && !strncmp (data, "suicide " , 8)) return true; |
… | |
… | |
246 | } |
240 | } |
247 | else |
241 | else |
248 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
242 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
249 | } |
243 | } |
250 | else |
244 | else |
251 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name); |
245 | send_packet_printf ("msg %d log ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name); |
252 | } |
246 | } |
253 | |
247 | |
254 | bool |
248 | bool |
255 | client::handle_packet () |
249 | client::handle_packet () |
256 | { |
250 | { |
… | |
… | |
286 | { |
280 | { |
287 | data = (char *)inbuf + 2; // better read garbage than segfault |
281 | data = (char *)inbuf + 2; // better read garbage than segfault |
288 | datalen = 0; |
282 | datalen = 0; |
289 | } |
283 | } |
290 | |
284 | |
291 | for (packet_type *pkt = packets; pkt < packets + array_length (packets); ++pkt) |
285 | for (packet_type *pkt = packets; pkt < packets + ecb_array_length (packets); ++pkt) |
292 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
286 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
293 | { |
287 | { |
294 | if (pkt->flags & PF_PLAYER && !always_immediate (this, pkt, data, datalen)) |
288 | if (pkt->flags & PF_PLAYER && !always_immediate (this, pkt, data, datalen)) |
295 | queue_command (pkt, data, datalen); |
289 | queue_command (pkt, data, datalen); |
296 | else |
290 | else |
… | |
… | |
298 | |
292 | |
299 | goto next_packet; |
293 | goto next_packet; |
300 | } |
294 | } |
301 | |
295 | |
302 | // If we get here, we didn't find a valid command. |
296 | // If we get here, we didn't find a valid command. |
303 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
297 | send_packet_printf ("msg %d log ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
304 | |
298 | |
305 | next_packet: |
299 | next_packet: |
306 | inbuf [pkt_len] = save_byte; // rather ugly |
300 | inbuf [pkt_len] = save_byte; // rather ugly |
307 | skip_packet (pkt_len); |
301 | skip_packet (pkt_len); |
308 | |
302 | |
… | |
… | |
351 | if (ws_version) |
345 | if (ws_version) |
352 | { |
346 | { |
353 | if (ws_inbuf_len + 2048 > ws_inbuf_alloc) |
347 | if (ws_inbuf_len + 2048 > ws_inbuf_alloc) |
354 | ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); |
348 | ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); |
355 | |
349 | |
356 | rbuf = ws_inbuf; |
350 | rbuf = ws_inbuf + ws_inbuf_len; |
357 | amount = ws_inbuf_alloc - ws_inbuf_len; |
351 | amount = ws_inbuf_alloc - ws_inbuf_len; |
358 | } |
352 | } |
359 | else |
353 | else |
360 | { |
354 | { |
361 | rbuf = inbuf; |
355 | rbuf = inbuf + inbuf_len; |
362 | amount = sizeof (inbuf) - inbuf_len; |
356 | amount = sizeof (inbuf) - inbuf_len; |
363 | } |
357 | } |
364 | |
358 | |
365 | if (!amount) |
359 | if (!amount) |
366 | { |
360 | { |
… | |
… | |
368 | LOG (llevError, "input buffer overflow."); |
362 | LOG (llevError, "input buffer overflow."); |
369 | destroy (); |
363 | destroy (); |
370 | return; |
364 | return; |
371 | } |
365 | } |
372 | |
366 | |
373 | amount = read (fd, ws_inbuf + ws_inbuf_len, amount); |
367 | amount = read (fd, rbuf, amount); |
374 | |
368 | |
375 | if (!amount) |
369 | if (!amount) |
376 | { |
370 | { |
377 | destroy (); |
371 | destroy (); |
378 | return; |
372 | return; |
… | |
… | |
653 | void |
647 | void |
654 | client::send_packet (const char *buf, int len) |
648 | client::send_packet (const char *buf, int len) |
655 | { |
649 | { |
656 | packet sl; |
650 | packet sl; |
657 | |
651 | |
658 | sl << data (buf, len); |
652 | sl << data_n (buf, len); |
659 | send_packet (sl); |
653 | send_packet (sl); |
660 | } |
654 | } |
661 | |
655 | |
662 | void |
656 | void |
663 | client::send_packet (const char *buf) |
657 | client::send_packet (const char *buf) |
… | |
… | |
695 | cfperl_send_msg (this, color, type, msg); |
689 | cfperl_send_msg (this, color, type, msg); |
696 | else |
690 | else |
697 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
691 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
698 | } |
692 | } |
699 | |
693 | |
700 | void |
|
|
701 | client::send_drawinfo (const char *msg, int flags) |
|
|
702 | { |
|
|
703 | send_msg (flags, "log", msg); |
|
|
704 | } |
|
|
705 | |
|
|
706 | /*********************************************************************** |
694 | /*********************************************************************** |
707 | * |
695 | * |
708 | * packet functions/utilities |
696 | * packet functions/utilities |
709 | * |
697 | * |
710 | **********************************************************************/ |
698 | **********************************************************************/ |
… | |
… | |
731 | { |
719 | { |
732 | val >>= 7; |
720 | val >>= 7; |
733 | *--p = (val & 0x7F) | 0x80; |
721 | *--p = (val & 0x7F) | 0x80; |
734 | } |
722 | } |
735 | |
723 | |
736 | return *this << data (p, buf + maxlen - p); |
724 | return *this << data_n (p, buf + maxlen - p); |
737 | } |
725 | } |
738 | |
726 | |
739 | packet &packet::operator <<(const data &v) |
727 | packet &packet::operator <<(const data_n &v) |
740 | { |
728 | { |
741 | if (room () < v.len) |
729 | if (room () < v.len) |
742 | reset (); |
730 | reset (); |
743 | else |
731 | else |
744 | { |
732 | { |
… | |
… | |
753 | } |
741 | } |
754 | |
742 | |
755 | packet &packet::operator <<(const data8 &v) |
743 | packet &packet::operator <<(const data8 &v) |
756 | { |
744 | { |
757 | unsigned int len = min (v.len, 0x00FF); |
745 | unsigned int len = min (v.len, 0x00FF); |
758 | return *this << uint8 (len) << data (v.ptr, len); |
746 | return *this << uint8 (len) << data_n (v.ptr, len); |
759 | } |
747 | } |
760 | |
748 | |
761 | packet &packet::operator <<(const data16 &v) |
749 | packet &packet::operator <<(const data16 &v) |
762 | { |
750 | { |
763 | unsigned int len = min (v.len, 0xFFFF); |
751 | unsigned int len = min (v.len, 0xFFFF); |
764 | return *this << uint16 (len) << data (v.ptr, len); |
752 | return *this << uint16 (len) << data_n (v.ptr, len); |
765 | } |
753 | } |
766 | |
754 | |
767 | packet &packet::operator <<(const char *v) |
755 | packet &packet::operator <<(const char *v) |
768 | { |
756 | { |
769 | return *this << data (v, strlen (v ? v : 0)); |
757 | return *this << data_n (v, v ? strlen (v) : 0); |
770 | } |
758 | } |
771 | |
759 | |
772 | void |
760 | void |
773 | packet::vprintf (const char *format, va_list ap) |
761 | packet::vprintf (const char *format, va_list ap) |
774 | { |
762 | { |