[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / WorldSocket.cpp
blob36da6924fcf24b6cc6d9561666bac8ebbe7d7417
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <ace/Message_Block.h>
20 #include <ace/OS_NS_string.h>
21 #include <ace/OS_NS_unistd.h>
22 #include <ace/os_include/arpa/os_inet.h>
23 #include <ace/os_include/netinet/os_tcp.h>
24 #include <ace/os_include/sys/os_types.h>
25 #include <ace/os_include/sys/os_socket.h>
26 #include <ace/OS_NS_string.h>
27 #include <ace/Reactor.h>
28 #include <ace/Auto_Ptr.h>
30 #include "WorldSocket.h"
31 #include "Common.h"
33 #include "Util.h"
34 #include "World.h"
35 #include "WorldPacket.h"
36 #include "SharedDefines.h"
37 #include "ByteBuffer.h"
38 #include "Opcodes.h"
39 #include "Database/DatabaseEnv.h"
40 #include "Auth/Sha1.h"
41 #include "WorldSession.h"
42 #include "WorldSocketMgr.h"
43 #include "Log.h"
44 #include "WorldLog.h"
46 #if defined( __GNUC__ )
47 #pragma pack(1)
48 #else
49 #pragma pack(push,1)
50 #endif
52 struct ServerPktHeader
54 /**
55 * size is the length of the payload _plus_ the length of the opcode
57 ServerPktHeader(uint32 size, uint16 cmd) : size(size)
59 uint8 headerIndex=0;
60 if(isLargePacket())
62 sLog.outDebug("initializing large server to client packet. Size: %u, cmd: %u", size, cmd);
63 header[headerIndex++] = 0x80|(0xFF &(size>>16));
65 header[headerIndex++] = 0xFF &(size>>8);
66 header[headerIndex++] = 0xFF &size;
68 header[headerIndex++] = 0xFF & cmd;
69 header[headerIndex++] = 0xFF & (cmd>>8);
72 uint8 getHeaderLength()
74 // cmd = 2 bytes, size= 2||3bytes
75 return 2+(isLargePacket()?3:2);
78 bool isLargePacket()
80 return size > 0x7FFF;
83 const uint32 size;
84 uint8 header[5];
87 struct ClientPktHeader
89 uint16 size;
90 uint32 cmd;
93 #if defined( __GNUC__ )
94 #pragma pack()
95 #else
96 #pragma pack(pop)
97 #endif
99 WorldSocket::WorldSocket (void) :
100 WorldHandler (),
101 m_Session (0),
102 m_RecvWPct (0),
103 m_RecvPct (),
104 m_Header (sizeof (ClientPktHeader)),
105 m_OutBuffer (0),
106 m_OutBufferSize (65536),
107 m_OutActive (false),
108 m_Seed (static_cast<uint32> (rand32 ())),
109 m_OverSpeedPings (0),
110 m_LastPingTime (ACE_Time_Value::zero)
112 reference_counting_policy ().value (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
114 msg_queue()->high_water_mark(8*1024*1024);
115 msg_queue()->low_water_mark(8*1024*1024);
118 WorldSocket::~WorldSocket (void)
120 if (m_RecvWPct)
121 delete m_RecvWPct;
123 if (m_OutBuffer)
124 m_OutBuffer->release ();
126 closing_ = true;
128 peer ().close ();
131 bool WorldSocket::IsClosed (void) const
133 return closing_;
136 void WorldSocket::CloseSocket (void)
139 ACE_GUARD (LockType, Guard, m_OutBufferLock);
141 if (closing_)
142 return;
144 closing_ = true;
145 peer ().close_writer ();
149 ACE_GUARD (LockType, Guard, m_SessionLock);
151 m_Session = NULL;
155 const std::string& WorldSocket::GetRemoteAddress (void) const
157 return m_Address;
160 int WorldSocket::SendPacket (const WorldPacket& pct)
162 ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
164 if (closing_)
165 return -1;
167 // Dump outgoing packet.
168 if (sWorldLog.LogWorld ())
170 sWorldLog.Log ("SERVER:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n",
171 (uint32) get_handle (),
172 pct.size (),
173 LookupOpcodeName (pct.GetOpcode ()),
174 pct.GetOpcode ());
176 uint32 p = 0;
177 while (p < pct.size ())
179 for (uint32 j = 0; j < 16 && p < pct.size (); j++)
180 sWorldLog.Log ("%.2X ", const_cast<WorldPacket&>(pct)[p++]);
182 sWorldLog.Log ("\n");
185 sWorldLog.Log ("\n\n");
188 ServerPktHeader header(pct.size()+2, pct.GetOpcode());
189 m_Crypt.EncryptSend ((uint8*)header.header, header.getHeaderLength());
191 if (m_OutBuffer->space () >= pct.size () + header.getHeaderLength() && msg_queue()->is_empty())
193 // Put the packet on the buffer.
194 if (m_OutBuffer->copy ((char*) header.header, header.getHeaderLength()) == -1)
195 ACE_ASSERT (false);
197 if (!pct.empty ())
198 if (m_OutBuffer->copy ((char*) pct.contents (), pct.size ()) == -1)
199 ACE_ASSERT (false);
201 else
203 // Enqueue the packet.
204 ACE_Message_Block* mb;
206 ACE_NEW_RETURN(mb, ACE_Message_Block(pct.size () + header.getHeaderLength()), -1);
208 mb->copy((char*) header.header, header.getHeaderLength());
210 if (!pct.empty ())
211 mb->copy((const char*)pct.contents(), pct.size ());
213 if(msg_queue()->enqueue_tail(mb,(ACE_Time_Value*)&ACE_Time_Value::zero) == -1)
215 sLog.outError("WorldSocket::SendPacket enqueue_tail");
216 mb->release();
217 return -1;
221 return 0;
224 long WorldSocket::AddReference (void)
226 return static_cast<long> (add_reference ());
229 long WorldSocket::RemoveReference (void)
231 return static_cast<long> (remove_reference ());
234 int WorldSocket::open (void *a)
236 ACE_UNUSED_ARG (a);
238 // Prevent double call to this func.
239 if (m_OutBuffer)
240 return -1;
242 // This will also prevent the socket from being Updated
243 // while we are initializing it.
244 m_OutActive = true;
246 // Hook for the manager.
247 if (sWorldSocketMgr->OnSocketOpen (this) == -1)
248 return -1;
250 // Allocate the buffer.
251 ACE_NEW_RETURN (m_OutBuffer, ACE_Message_Block (m_OutBufferSize), -1);
253 // Store peer address.
254 ACE_INET_Addr remote_addr;
256 if (peer ().get_remote_addr (remote_addr) == -1)
258 sLog.outError ("WorldSocket::open: peer ().get_remote_addr errno = %s", ACE_OS::strerror (errno));
259 return -1;
262 m_Address = remote_addr.get_host_addr ();
264 // Send startup packet.
265 WorldPacket packet (SMSG_AUTH_CHALLENGE, 4);
266 packet << m_Seed;
268 if (SendPacket (packet) == -1)
269 return -1;
271 // Register with ACE Reactor
272 if (reactor ()->register_handler(this, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK) == -1)
274 sLog.outError ("WorldSocket::open: unable to register client handler errno = %s", ACE_OS::strerror (errno));
275 return -1;
278 // reactor takes care of the socket from now on
279 remove_reference ();
281 return 0;
284 int WorldSocket::close (int)
286 shutdown ();
288 closing_ = true;
290 remove_reference ();
292 return 0;
295 int WorldSocket::handle_input (ACE_HANDLE)
297 if (closing_)
298 return -1;
300 switch (handle_input_missing_data ())
302 case -1 :
304 if ((errno == EWOULDBLOCK) ||
305 (errno == EAGAIN))
307 return Update (); // interesting line ,isn't it ?
310 DEBUG_LOG ("WorldSocket::handle_input: Peer error closing connection errno = %s", ACE_OS::strerror (errno));
312 errno = ECONNRESET;
313 return -1;
315 case 0:
317 DEBUG_LOG ("WorldSocket::handle_input: Peer has closed connection");
319 errno = ECONNRESET;
320 return -1;
322 case 1:
323 return 1;
324 default:
325 return Update (); // another interesting line ;)
328 ACE_NOTREACHED(return -1);
331 int WorldSocket::handle_output (ACE_HANDLE)
333 ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
335 if (closing_)
336 return -1;
338 const size_t send_len = m_OutBuffer->length ();
340 if (send_len == 0)
341 return handle_output_queue (Guard);
343 #ifdef MSG_NOSIGNAL
344 ssize_t n = peer ().send (m_OutBuffer->rd_ptr (), send_len, MSG_NOSIGNAL);
345 #else
346 ssize_t n = peer ().send (m_OutBuffer->rd_ptr (), send_len);
347 #endif // MSG_NOSIGNAL
349 if (n == 0)
350 return -1;
351 else if (n == -1)
353 if (errno == EWOULDBLOCK || errno == EAGAIN)
354 return schedule_wakeup_output (Guard);
356 return -1;
358 else if (n < send_len) //now n > 0
360 m_OutBuffer->rd_ptr (static_cast<size_t> (n));
362 // move the data to the base of the buffer
363 m_OutBuffer->crunch ();
365 return schedule_wakeup_output (Guard);
367 else //now n == send_len
369 m_OutBuffer->reset ();
371 return handle_output_queue (Guard);
374 ACE_NOTREACHED (return 0);
377 int WorldSocket::handle_output_queue (GuardType& g)
379 if(msg_queue()->is_empty())
380 return cancel_wakeup_output(g);
382 ACE_Message_Block *mblk;
384 if(msg_queue()->dequeue_head(mblk, (ACE_Time_Value*)&ACE_Time_Value::zero) == -1)
386 sLog.outError("WorldSocket::handle_output_queue dequeue_head");
387 return -1;
390 const size_t send_len = mblk->length ();
392 #ifdef MSG_NOSIGNAL
393 ssize_t n = peer ().send (mblk->rd_ptr (), send_len, MSG_NOSIGNAL);
394 #else
395 ssize_t n = peer ().send (mblk->rd_ptr (), send_len);
396 #endif // MSG_NOSIGNAL
398 if (n == 0)
400 mblk->release();
402 return -1;
404 else if (n == -1)
406 if (errno == EWOULDBLOCK || errno == EAGAIN)
408 msg_queue()->enqueue_head(mblk, (ACE_Time_Value*) &ACE_Time_Value::zero);
409 return schedule_wakeup_output (g);
412 mblk->release();
413 return -1;
415 else if (n < send_len) //now n > 0
417 mblk->rd_ptr (static_cast<size_t> (n));
419 if (msg_queue()->enqueue_head(mblk, (ACE_Time_Value*) &ACE_Time_Value::zero) == -1)
421 sLog.outError("WorldSocket::handle_output_queue enqueue_head");
422 mblk->release();
423 return -1;
426 return schedule_wakeup_output (g);
428 else //now n == send_len
430 mblk->release();
432 return msg_queue()->is_empty() ? cancel_wakeup_output(g) : ACE_Event_Handler::WRITE_MASK;
435 ACE_NOTREACHED(return -1);
438 int WorldSocket::handle_close (ACE_HANDLE h, ACE_Reactor_Mask)
440 // Critical section
442 ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
444 closing_ = true;
446 if (h == ACE_INVALID_HANDLE)
447 peer ().close_writer ();
450 // Critical section
452 ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
454 m_Session = NULL;
457 return 0;
460 int WorldSocket::Update (void)
462 if (closing_)
463 return -1;
465 if (m_OutActive || (m_OutBuffer->length () == 0 && msg_queue()->is_empty()))
466 return 0;
468 int ret;
470 ret = handle_output (get_handle ());
471 while( ret > 0 );
473 return ret;
476 int WorldSocket::handle_input_header (void)
478 ACE_ASSERT (m_RecvWPct == NULL);
480 ACE_ASSERT (m_Header.length () == sizeof (ClientPktHeader));
482 m_Crypt.DecryptRecv ((uint8*) m_Header.rd_ptr (), sizeof (ClientPktHeader));
484 ClientPktHeader& header = *((ClientPktHeader*) m_Header.rd_ptr ());
486 EndianConvertReverse(header.size);
487 EndianConvert(header.cmd);
489 if ((header.size < 4) || (header.size > 10240) || (header.cmd > 10240))
491 sLog.outError ("WorldSocket::handle_input_header: client sent mailformed packet size = %d , cmd = %d",
492 header.size, header.cmd);
494 errno = EINVAL;
495 return -1;
498 header.size -= 4;
500 ACE_NEW_RETURN (m_RecvWPct, WorldPacket ((uint16) header.cmd, header.size), -1);
502 if(header.size > 0)
504 m_RecvWPct->resize (header.size);
505 m_RecvPct.base ((char*) m_RecvWPct->contents (), m_RecvWPct->size ());
507 else
509 ACE_ASSERT(m_RecvPct.space() == 0);
512 return 0;
515 int WorldSocket::handle_input_payload (void)
517 // set errno properly here on error !!!
518 // now have a header and payload
520 ACE_ASSERT (m_RecvPct.space () == 0);
521 ACE_ASSERT (m_Header.space () == 0);
522 ACE_ASSERT (m_RecvWPct != NULL);
524 const int ret = ProcessIncoming (m_RecvWPct);
526 m_RecvPct.base (NULL, 0);
527 m_RecvPct.reset ();
528 m_RecvWPct = NULL;
530 m_Header.reset ();
532 if (ret == -1)
533 errno = EINVAL;
535 return ret;
538 int WorldSocket::handle_input_missing_data (void)
540 char buf [4096];
542 ACE_Data_Block db ( sizeof (buf),
543 ACE_Message_Block::MB_DATA,
544 buf,
547 ACE_Message_Block::DONT_DELETE,
550 ACE_Message_Block message_block(&db,
551 ACE_Message_Block::DONT_DELETE,
554 const size_t recv_size = message_block.space ();
556 const ssize_t n = peer ().recv (message_block.wr_ptr (),
557 recv_size);
559 if (n <= 0)
560 return n;
562 message_block.wr_ptr (n);
564 while (message_block.length () > 0)
566 if (m_Header.space () > 0)
568 //need to receive the header
569 const size_t to_header = (message_block.length () > m_Header.space () ? m_Header.space () : message_block.length ());
570 m_Header.copy (message_block.rd_ptr (), to_header);
571 message_block.rd_ptr (to_header);
573 if (m_Header.space () > 0)
575 // Couldn't receive the whole header this time.
576 ACE_ASSERT (message_block.length () == 0);
577 errno = EWOULDBLOCK;
578 return -1;
581 // We just received nice new header
582 if (handle_input_header () == -1)
584 ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN));
585 return -1;
589 // Its possible on some error situations that this happens
590 // for example on closing when epoll receives more chunked data and stuff
591 // hope this is not hack ,as proper m_RecvWPct is asserted around
592 if (!m_RecvWPct)
594 sLog.outError ("Forcing close on input m_RecvWPct = NULL");
595 errno = EINVAL;
596 return -1;
599 // We have full read header, now check the data payload
600 if (m_RecvPct.space () > 0)
602 //need more data in the payload
603 const size_t to_data = (message_block.length () > m_RecvPct.space () ? m_RecvPct.space () : message_block.length ());
604 m_RecvPct.copy (message_block.rd_ptr (), to_data);
605 message_block.rd_ptr (to_data);
607 if (m_RecvPct.space () > 0)
609 // Couldn't receive the whole data this time.
610 ACE_ASSERT (message_block.length () == 0);
611 errno = EWOULDBLOCK;
612 return -1;
616 //just received fresh new payload
617 if (handle_input_payload () == -1)
619 ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN));
620 return -1;
624 return n == recv_size ? 1 : 2;
627 int WorldSocket::cancel_wakeup_output (GuardType& g)
629 if (!m_OutActive)
630 return 0;
632 m_OutActive = false;
634 g.release ();
636 if (reactor ()->cancel_wakeup
637 (this, ACE_Event_Handler::WRITE_MASK) == -1)
639 // would be good to store errno from reactor with errno guard
640 sLog.outError ("WorldSocket::cancel_wakeup_output");
641 return -1;
644 return 0;
647 int WorldSocket::schedule_wakeup_output (GuardType& g)
649 if (m_OutActive)
650 return 0;
652 m_OutActive = true;
654 g.release ();
656 if (reactor ()->schedule_wakeup
657 (this, ACE_Event_Handler::WRITE_MASK) == -1)
659 sLog.outError ("WorldSocket::schedule_wakeup_output");
660 return -1;
663 return 0;
666 int WorldSocket::ProcessIncoming (WorldPacket* new_pct)
668 ACE_ASSERT (new_pct);
670 // manage memory ;)
671 ACE_Auto_Ptr<WorldPacket> aptr (new_pct);
673 const ACE_UINT16 opcode = new_pct->GetOpcode ();
675 if (closing_)
676 return -1;
678 // Dump received packet.
679 if (sWorldLog.LogWorld ())
681 sWorldLog.Log ("CLIENT:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n",
682 (uint32) get_handle (),
683 new_pct->size (),
684 LookupOpcodeName (new_pct->GetOpcode ()),
685 new_pct->GetOpcode ());
687 uint32 p = 0;
688 while (p < new_pct->size ())
690 for (uint32 j = 0; j < 16 && p < new_pct->size (); j++)
691 sWorldLog.Log ("%.2X ", (*new_pct)[p++]);
692 sWorldLog.Log ("\n");
694 sWorldLog.Log ("\n\n");
697 try {
698 switch(opcode)
700 case CMSG_PING:
701 return HandlePing (*new_pct);
702 case CMSG_AUTH_SESSION:
703 if (m_Session)
705 sLog.outError ("WorldSocket::ProcessIncoming: Player send CMSG_AUTH_SESSION again");
706 return -1;
709 return HandleAuthSession (*new_pct);
710 case CMSG_KEEP_ALIVE:
711 DEBUG_LOG ("CMSG_KEEP_ALIVE ,size: %d", new_pct->size ());
713 return 0;
714 default:
716 ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
718 if (m_Session != NULL)
720 // OK ,give the packet to WorldSession
721 aptr.release ();
722 // WARNINIG here we call it with locks held.
723 // Its possible to cause deadlock if QueuePacket calls back
724 m_Session->QueuePacket (new_pct);
725 return 0;
727 else
729 sLog.outError ("WorldSocket::ProcessIncoming: Client not authed opcode = %u", uint32(opcode));
730 return -1;
735 catch(ByteBufferException &)
737 sLog.outError("WorldSocket::ProcessIncoming ByteBufferException occured while parsing an instant handled packet (opcode: %u) from client %s, accountid=%i. Disconnected client.",
738 opcode, GetRemoteAddress().c_str(), m_Session?m_Session->GetAccountId():-1);
739 if(sLog.IsOutDebug())
741 sLog.outDebug("Dumping error causing packet:");
742 new_pct->hexlike();
745 return -1;
748 ACE_NOTREACHED (return 0);
751 int WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
753 // NOTE: ATM the socket is singlethread, have this in mind ...
754 uint8 digest[20];
755 uint32 clientSeed;
756 uint32 unk2, unk3;
757 uint32 BuiltNumberClient;
758 uint32 id, security;
759 uint8 expansion = 0;
760 LocaleConstant locale;
761 std::string account;
762 Sha1Hash sha1;
763 BigNumber v, s, g, N;
764 WorldPacket packet, SendAddonPacked;
766 BigNumber K;
768 // Read the content of the packet
769 recvPacket >> BuiltNumberClient; // for now no use
770 recvPacket >> unk2;
771 recvPacket >> account;
772 recvPacket >> unk3;
773 recvPacket >> clientSeed;
774 recvPacket.read (digest, 20);
776 DEBUG_LOG ("WorldSocket::HandleAuthSession: client %u, unk2 %u, account %s, unk3 %u, clientseed %u",
777 BuiltNumberClient,
778 unk2,
779 account.c_str (),
780 unk3,
781 clientSeed);
783 // Get the account information from the realmd database
784 std::string safe_account = account; // Duplicate, else will screw the SHA hash verification below
785 loginDatabase.escape_string (safe_account);
786 // No SQL injection, username escaped.
788 QueryResult *result =
789 loginDatabase.PQuery ("SELECT "
790 "id, " //0
791 "gmlevel, " //1
792 "sessionkey, " //2
793 "last_ip, " //3
794 "locked, " //4
795 "v, " //5
796 "s, " //6
797 "expansion, " //7
798 "mutetime, " //8
799 "locale " //9
800 "FROM account "
801 "WHERE username = '%s'",
802 safe_account.c_str ());
804 // Stop if the account is not found
805 if (!result)
807 packet.Initialize (SMSG_AUTH_RESPONSE, 1);
808 packet << uint8 (AUTH_UNKNOWN_ACCOUNT);
810 SendPacket (packet);
812 sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (unknown account).");
813 return -1;
816 Field* fields = result->Fetch ();
818 expansion = ((sWorld.getConfig(CONFIG_EXPANSION) > fields[7].GetUInt8()) ? fields[7].GetUInt8() : sWorld.getConfig(CONFIG_EXPANSION));
820 N.SetHexStr ("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7");
821 g.SetDword (7);
823 v.SetHexStr(fields[5].GetString());
824 s.SetHexStr (fields[6].GetString ());
826 const char* sStr = s.AsHexStr (); //Must be freed by OPENSSL_free()
827 const char* vStr = v.AsHexStr (); //Must be freed by OPENSSL_free()
829 DEBUG_LOG ("WorldSocket::HandleAuthSession: (s,v) check s: %s v: %s",
830 sStr,
831 vStr);
833 OPENSSL_free ((void*) sStr);
834 OPENSSL_free ((void*) vStr);
836 ///- Re-check ip locking (same check as in realmd).
837 if (fields[4].GetUInt8 () == 1) // if ip is locked
839 if (strcmp (fields[3].GetString (), GetRemoteAddress ().c_str ()))
841 packet.Initialize (SMSG_AUTH_RESPONSE, 1);
842 packet << uint8 (AUTH_FAILED);
843 SendPacket (packet);
845 delete result;
846 sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs).");
847 return -1;
851 id = fields[0].GetUInt32 ();
852 security = fields[1].GetUInt16 ();
853 if(security > SEC_ADMINISTRATOR) // prevent invalid security settings in DB
854 security = SEC_ADMINISTRATOR;
856 K.SetHexStr (fields[2].GetString ());
858 time_t mutetime = time_t (fields[8].GetUInt64 ());
860 locale = LocaleConstant (fields[9].GetUInt8 ());
861 if (locale >= MAX_LOCALE)
862 locale = LOCALE_enUS;
864 delete result;
866 // Re-check account ban (same check as in realmd)
867 QueryResult *banresult =
868 loginDatabase.PQuery ("SELECT "
869 "bandate, "
870 "unbandate "
871 "FROM account_banned "
872 "WHERE id = '%u' "
873 "AND active = 1",
874 id);
876 if (banresult) // if account banned
878 packet.Initialize (SMSG_AUTH_RESPONSE, 1);
879 packet << uint8 (AUTH_BANNED);
880 SendPacket (packet);
882 delete banresult;
884 sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned).");
885 return -1;
888 // Check locked state for server
889 AccountTypes allowedAccountType = sWorld.GetPlayerSecurityLimit ();
891 if (allowedAccountType > SEC_PLAYER && AccountTypes(security) < allowedAccountType)
893 WorldPacket Packet (SMSG_AUTH_RESPONSE, 1);
894 Packet << uint8 (AUTH_UNAVAILABLE);
896 SendPacket (packet);
898 sLog.outBasic ("WorldSocket::HandleAuthSession: User tries to login but his security level is not enough");
899 return -1;
902 // Check that Key and account name are the same on client and server
903 Sha1Hash sha;
905 uint32 t = 0;
906 uint32 seed = m_Seed;
908 sha.UpdateData (account);
909 sha.UpdateData ((uint8 *) & t, 4);
910 sha.UpdateData ((uint8 *) & clientSeed, 4);
911 sha.UpdateData ((uint8 *) & seed, 4);
912 sha.UpdateBigNumbers (&K, NULL);
913 sha.Finalize ();
915 if (memcmp (sha.GetDigest (), digest, 20))
917 packet.Initialize (SMSG_AUTH_RESPONSE, 1);
918 packet << uint8 (AUTH_FAILED);
920 SendPacket (packet);
922 sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (authentification failed).");
923 return -1;
926 std::string address = GetRemoteAddress ();
928 DEBUG_LOG ("WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.",
929 account.c_str (),
930 address.c_str ());
932 // Update the last_ip in the database
933 // No SQL injection, username escaped.
934 loginDatabase.escape_string (address);
936 loginDatabase.PExecute ("UPDATE account "
937 "SET last_ip = '%s' "
938 "WHERE username = '%s'",
939 address.c_str (),
940 safe_account.c_str ());
942 // NOTE ATM the socket is single-threaded, have this in mind ...
943 ACE_NEW_RETURN (m_Session, WorldSession (id, this, AccountTypes(security), expansion, mutetime, locale), -1);
945 m_Crypt.Init(&K);
947 m_Session->LoadGlobalAccountData();
948 m_Session->LoadTutorialsData();
949 m_Session->ReadAddonsInfo(recvPacket);
951 // In case needed sometime the second arg is in microseconds 1 000 000 = 1 sec
952 ACE_OS::sleep (ACE_Time_Value (0, 10000));
954 sWorld.AddSession (m_Session);
956 return 0;
959 int WorldSocket::HandlePing (WorldPacket& recvPacket)
961 uint32 ping;
962 uint32 latency;
964 // Get the ping packet content
965 recvPacket >> ping;
966 recvPacket >> latency;
968 if (m_LastPingTime == ACE_Time_Value::zero)
969 m_LastPingTime = ACE_OS::gettimeofday (); // for 1st ping
970 else
972 ACE_Time_Value cur_time = ACE_OS::gettimeofday ();
973 ACE_Time_Value diff_time (cur_time);
974 diff_time -= m_LastPingTime;
975 m_LastPingTime = cur_time;
977 if (diff_time < ACE_Time_Value (27))
979 ++m_OverSpeedPings;
981 uint32 max_count = sWorld.getConfig (CONFIG_MAX_OVERSPEED_PINGS);
983 if (max_count && m_OverSpeedPings > max_count)
985 ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
987 if (m_Session && m_Session->GetSecurity () == SEC_PLAYER)
989 sLog.outError ("WorldSocket::HandlePing: Player kicked for "
990 "over-speed pings address = %s",
991 GetRemoteAddress ().c_str ());
993 return -1;
997 else
998 m_OverSpeedPings = 0;
1001 // critical section
1003 ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
1005 if (m_Session)
1006 m_Session->SetLatency (latency);
1007 else
1009 sLog.outError ("WorldSocket::HandlePing: peer sent CMSG_PING, "
1010 "but is not authenticated or got recently kicked,"
1011 " address = %s",
1012 GetRemoteAddress ().c_str ());
1013 return -1;
1017 WorldPacket packet (SMSG_PONG, 4);
1018 packet << ping;
1019 return SendPacket (packet);