root/trunk/src/game/WorldSocket.cpp @ 28

Revision 16, 25.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Remove debugging code from WorldSocket?.cpp.
* Small cleanup fix in configure.ac.

Original author: derex_tri
Date: 2008-10-06 04:14:44-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "Common.h"
20#include "WorldSocket.h"
21
22#include <ace/Message_Block.h>
23#include <ace/OS_NS_string.h>
24#include <ace/OS_NS_unistd.h>
25#include <ace/os_include/arpa/os_inet.h>
26#include <ace/os_include/netinet/os_tcp.h>
27#include <ace/os_include/sys/os_types.h>
28#include <ace/os_include/sys/os_socket.h>
29#include <ace/OS_NS_string.h>
30#include <ace/Reactor.h>
31#include <ace/Auto_Ptr.h>
32
33#include "Util.h"
34#include "World.h"
35#include "WorldPacket.h"
36#include "SharedDefines.h"
37#include "ByteBuffer.h"
38#include "AddonHandler.h"
39#include "Opcodes.h"
40#include "Database/DatabaseEnv.h"
41#include "Auth/Sha1.h"
42#include "WorldSession.h"
43#include "WorldSocketMgr.h"
44#include "Log.h"
45#include "WorldLog.h"
46
47#if defined( __GNUC__ )
48#pragma pack(1)
49#else
50#pragma pack(push,1)
51#endif
52
53struct ServerPktHeader
54{
55  ACE_UINT16 size;
56  ACE_UINT16 cmd;
57};
58
59struct ClientPktHeader
60{
61  ACE_UINT16 size;
62  ACE_UINT32 cmd;
63};
64
65#if defined( __GNUC__ )
66#pragma pack()
67#else
68#pragma pack(pop)
69#endif
70
71WorldSocket::WorldSocket (void) :
72WorldHandler (),
73m_Session (0),
74m_RecvWPct (0),
75m_RecvPct (),
76m_Header (sizeof (ClientPktHeader)),
77m_OutBuffer (0),
78m_OutBufferSize (65536),
79m_OutActive (false),
80m_Seed (static_cast<uint32> (rand32 ())),
81m_OverSpeedPings (0),
82m_LastPingTime (ACE_Time_Value::zero)
83{
84  this->reference_counting_policy ().value (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
85}
86
87WorldSocket::~WorldSocket (void)
88{
89  if (m_RecvWPct)
90    delete m_RecvWPct;
91
92  if (m_OutBuffer)
93    m_OutBuffer->release ();
94
95  this->closing_ = true;
96
97  this->peer ().close ();
98
99  WorldPacket* pct;
100  while (m_PacketQueue.dequeue_head (pct) == 0)
101    delete pct;
102}
103
104bool
105WorldSocket::IsClosed (void) const
106{
107  return this->closing_;
108}
109
110void
111WorldSocket::CloseSocket (void)
112{
113  {
114    ACE_GUARD (LockType, Guard, m_OutBufferLock);
115
116    if (this->closing_)
117      return;
118
119    this->closing_ = true;
120
121    this->peer ().close_writer ();
122  }
123
124  {
125    ACE_GUARD (LockType, Guard, m_SessionLock);
126
127    m_Session = NULL;
128  }
129
130}
131
132const std::string&
133WorldSocket::GetRemoteAddress (void) const
134{
135  return m_Address;
136}
137
138int
139WorldSocket::SendPacket (const WorldPacket& pct)
140{
141  ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
142
143  if (this->closing_)
144    return -1;
145
146  // Dump outgoing packet.
147  if (sWorldLog.LogWorld ())
148    {
149      sWorldLog.Log ("SERVER:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n",
150                     (uint32) get_handle (),
151                     pct.size (),
152                     LookupOpcodeName (pct.GetOpcode ()),
153                     pct.GetOpcode ());
154
155      uint32 p = 0;
156      while (p < pct.size ())
157        {
158          for (uint32 j = 0; j < 16 && p < pct.size (); j++)
159            sWorldLog.Log ("%.2X ", const_cast<WorldPacket&> (pct)[p++]);
160
161          sWorldLog.Log ("\n");
162        }
163
164      sWorldLog.Log ("\n\n");
165    }
166
167  if (iSendPacket (pct) == -1)
168    {
169      WorldPacket* npct;
170
171      ACE_NEW_RETURN (npct, WorldPacket (pct), -1);
172
173      // NOTE maybe check of the size of the queue can be good ?
174      // to make it bounded instead of unbounded
175      if (m_PacketQueue.enqueue_tail (npct) == -1)
176        {
177          delete npct;
178          sLog.outError ("WorldSocket::SendPacket: m_PacketQueue.enqueue_tail failed");
179          return -1;
180        }
181    }
182
183  return 0;
184}
185
186long
187WorldSocket::AddReference (void)
188{
189  return static_cast<long> (this->add_reference ());
190}
191
192long
193WorldSocket::RemoveReference (void)
194{
195  return static_cast<long> (this->remove_reference ());
196}
197
198int
199WorldSocket::open (void *a)
200{
201  ACE_UNUSED_ARG (a);
202
203  // Prevent double call to this func.
204  if (m_OutBuffer)
205    return -1;
206
207  // This will also prevent the socket from being Updated
208  // while we are initializing it.
209  m_OutActive = true;
210
211  // Hook for the manager.
212  if (sWorldSocketMgr->OnSocketOpen (this) == -1)
213    return -1;
214
215  // Allocate the buffer.
216  ACE_NEW_RETURN (m_OutBuffer, ACE_Message_Block (m_OutBufferSize), -1);
217
218  // Store peer address.
219  ACE_INET_Addr remote_addr;
220
221  if (this->peer ().get_remote_addr (remote_addr) == -1)
222    {
223      sLog.outError ("WorldSocket::open: peer ().get_remote_addr errno = %s", ACE_OS::strerror (errno));
224      return -1;
225    }
226
227  m_Address = remote_addr.get_host_addr ();
228
229  // Send startup packet.
230  WorldPacket packet (SMSG_AUTH_CHALLENGE, 4);
231  packet << m_Seed;
232
233  if (SendPacket (packet) == -1)
234    return -1;
235
236  // Register with ACE Reactor
237  if (this->reactor ()->register_handler
238      (this,
239       ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK) == -1)
240    {
241      sLog.outError ("WorldSocket::open: unable to register client handler errno = %s", ACE_OS::strerror (errno));
242      return -1;
243    }
244
245  // reactor takes care of the socket from now on
246  this->remove_reference ();
247
248  return 0;
249}
250
251int
252WorldSocket::close (int)
253{
254  this->shutdown ();
255
256  this->closing_ = true;
257
258  this->remove_reference ();
259
260  return 0;
261}
262
263int
264WorldSocket::handle_input (ACE_HANDLE)
265{
266  if (this->closing_)
267    return -1;
268
269  switch (this->handle_input_missing_data ())
270    {
271    case -1 :
272      {
273        if ((errno == EWOULDBLOCK) ||
274            (errno == EAGAIN))
275          {
276            //return 0;
277            return this->Update (); // interesting line ,isnt it ?
278          }
279
280        DEBUG_LOG ("WorldSocket::handle_input: Peer error closing connection errno = %s", ACE_OS::strerror (errno));
281
282        return -1;
283      }
284    case 0:
285      {
286        DEBUG_LOG ("WorldSocket::handle_input: Peer has closed connection\n");
287
288        errno = ECONNRESET;
289
290        return -1;
291      }
292    case 1:
293      return 1;
294    }
295
296  //return 0;
297  return this->Update (); // another interesting line ;)
298}
299
300int
301WorldSocket::handle_output (ACE_HANDLE)
302{
303  ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
304
305  if (this->closing_)
306    return -1;
307
308  const size_t send_len = m_OutBuffer->length ();
309
310  if (send_len == 0)
311    return this->cancel_wakeup_output (Guard);
312
313  // TODO SO_NOSIGPIPE on platforms that support it
314#ifdef MSG_NOSIGNAL
315  ssize_t n = this->peer ().send (m_OutBuffer->rd_ptr (), send_len, MSG_NOSIGNAL);
316#else
317  ssize_t n = this->peer ().send (m_OutBuffer->rd_ptr (), send_len);
318#endif // MSG_NOSIGNAL
319
320  if (n == 0)
321    return -1;
322  else if (n == -1)
323    {
324      if (errno == EWOULDBLOCK || errno == EAGAIN)
325        return this->schedule_wakeup_output (Guard);
326
327      return -1;
328    }
329  else if (n < send_len) //now n > 0
330    {
331      m_OutBuffer->rd_ptr (static_cast<size_t> (n));
332
333      // move the data to the base of the buffer
334      m_OutBuffer->crunch ();
335
336      return this->schedule_wakeup_output (Guard);
337    }
338  else //now n == send_len
339    {
340      m_OutBuffer->reset ();
341
342      if (!iFlushPacketQueue ())
343        return this->cancel_wakeup_output (Guard);
344      else
345        return this->schedule_wakeup_output (Guard);
346    }
347
348  ACE_NOTREACHED (return 0);
349}
350
351int
352WorldSocket::handle_close (ACE_HANDLE h, ACE_Reactor_Mask)
353{
354  // Critical section
355  {
356    ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
357
358    this->closing_ = true;
359
360    if (h == ACE_INVALID_HANDLE)
361      this->peer ().close_writer ();
362  }
363
364  // Critical section
365  {
366    ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
367
368    m_Session = NULL;
369  }
370
371  return 0;
372}
373
374int
375WorldSocket::Update (void)
376{
377  if (this->closing_)
378    return -1;
379
380  if (m_OutActive || m_OutBuffer->length () == 0)
381    return 0;
382
383  return this->handle_output (this->get_handle ());
384}
385
386int
387WorldSocket::handle_input_header (void)
388{
389  ACE_ASSERT (m_RecvWPct == NULL);
390
391  if (m_Header.length () != sizeof (ClientPktHeader))
392    {
393      sLog.outError ("WorldSocket::handle_input_header: internal error: invalid header");
394      errno = EINVAL;
395      return -1;
396    }
397
398  m_Crypt.DecryptRecv ((ACE_UINT8*) m_Header.rd_ptr (), sizeof (ClientPktHeader));
399
400  ClientPktHeader& header = *((ClientPktHeader*) m_Header.rd_ptr ());
401
402  header.size = ACE_NTOHS (header.size);
403
404#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN
405  header.cmd = ACE_SWAP_LONG (header.cmd)
406#endif // ACE_BIG_ENDIAN
407
408  if ((header.size < 4) ||
409      (header.size > 10240) ||
410      (header.cmd <= 0) ||
411      (header.cmd > 10240)
412      )
413    {
414      sLog.outError ("WorldSocket::handle_input_header: client sent mailformed packet size = %d , cmd = %d",
415                     header.size,
416                     header.cmd);
417
418      errno = EINVAL;
419      return -1;
420    }
421
422  header.size -= 4;
423
424  ACE_NEW_RETURN (m_RecvWPct, WorldPacket ((uint16) header.cmd, header.size), -1);
425
426  if (header.size > 0)
427    {
428      m_RecvWPct->resize (header.size);
429      m_RecvPct.base ((char*) m_RecvWPct->contents (), m_RecvWPct->size ());
430    }
431  else
432    {
433      ACE_ASSERT (m_RecvPct.space () == 0);
434    }
435
436
437  return 0;
438}
439
440int
441WorldSocket::handle_input_payload (void)
442{
443  // set errno properly here on error !!!
444  // now have a header and payload
445
446  ACE_ASSERT (m_RecvPct.space () == 0);
447  ACE_ASSERT (m_Header.space () == 0);
448  ACE_ASSERT (m_RecvWPct != NULL);
449
450  const int ret = this->ProcessIncoming (m_RecvWPct);
451
452  m_RecvPct.base (NULL, 0);
453  m_RecvPct.reset ();
454  m_RecvWPct = NULL;
455
456  m_Header.reset ();
457
458  if (ret == -1)
459    errno = EINVAL;
460
461  return ret;
462}
463
464int
465WorldSocket::handle_input_missing_data (void)
466{
467  char buf [1024];
468
469  ACE_Data_Block db (sizeof (buf),
470                     ACE_Message_Block::MB_DATA,
471                     buf,
472                     0,
473                     0,
474                     ACE_Message_Block::DONT_DELETE,
475                     0);
476
477  ACE_Message_Block message_block (&db,
478                                   ACE_Message_Block::DONT_DELETE,
479                                   0);
480
481  const size_t recv_size = message_block.space ();
482
483  const ssize_t n = this->peer ().recv (message_block.wr_ptr (),
484                                        recv_size);
485
486  if (n <= 0)
487    return n;
488
489  message_block.wr_ptr (n);
490
491  while (message_block.length () > 0)
492    {
493      if (m_Header.space () > 0)
494        {
495          //need to recieve the header
496          const size_t to_header = (message_block.length () > m_Header.space () ? m_Header.space () : message_block.length ());
497          m_Header.copy (message_block.rd_ptr (), to_header);
498          message_block.rd_ptr (to_header);
499
500          if (m_Header.space () > 0)
501            {
502              //couldnt recieve the whole header this time
503              ACE_ASSERT (message_block.length () == 0);
504              errno = EWOULDBLOCK;
505              return -1;
506            }
507
508          //we just recieved nice new header
509          if (this->handle_input_header () == -1)
510            {
511              ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN));
512              return -1;
513            }
514        }
515
516      // Its possible on some error situations that this happens
517      // for example on closing when epoll recieves more chunked data and stuff
518      // hope this is not hack ,as proper m_RecvWPct is asserted around
519      if (!m_RecvWPct)
520        {
521          sLog.outError ("Forsing close on input m_RecvWPct = NULL");
522          errno = EINVAL;
523          return -1;
524        }
525
526      // We have full readed header, now check the data payload
527      if (m_RecvPct.space () > 0)
528        {
529          //need more data in the payload
530          const size_t to_data = (message_block.length () > m_RecvPct.space () ? m_RecvPct.space () : message_block.length ());
531          m_RecvPct.copy (message_block.rd_ptr (), to_data);
532          message_block.rd_ptr (to_data);
533
534          if (m_RecvPct.space () > 0)
535            {
536              //couldnt recieve the whole data this time
537              ACE_ASSERT (message_block.length () == 0);
538              errno = EWOULDBLOCK;
539              return -1;
540            }
541        }
542
543      //just recieved fresh new payload
544      if (this->handle_input_payload () == -1)
545        {
546          ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN));
547          return -1;
548        }
549    }
550
551  return n == recv_size ? 1 : 2;
552}
553
554int
555WorldSocket::cancel_wakeup_output (GuardType& g)
556{
557  if (!m_OutActive)
558    return 0;
559
560  m_OutActive = false;
561
562  g.release ();
563
564  if (this->reactor ()->cancel_wakeup
565      (this, ACE_Event_Handler::WRITE_MASK) == -1)
566    {
567      // would be good to store errno from reactor with errno guard
568      sLog.outError ("WorldSocket::cancel_wakeup_output");
569      return -1;
570    }
571
572  return 0;
573}
574
575int
576WorldSocket::schedule_wakeup_output (GuardType& g)
577{
578  if (m_OutActive)
579    return 0;
580
581  m_OutActive = true;
582
583  g.release ();
584
585  if (this->reactor ()->schedule_wakeup
586      (this, ACE_Event_Handler::WRITE_MASK) == -1)
587    {
588      sLog.outError ("WorldSocket::schedule_wakeup_output");
589      return -1;
590    }
591
592  return 0;
593}
594
595int
596WorldSocket::ProcessIncoming (WorldPacket* new_pct)
597{
598  ACE_ASSERT (new_pct);
599
600  // manage memory ;)
601  ACE_Auto_Ptr<WorldPacket> aptr (new_pct);
602
603  const ACE_UINT16 opcode = new_pct->GetOpcode ();
604
605  if (this->closing_)
606    return -1;
607
608  // dump recieved packet
609  if (sWorldLog.LogWorld ())
610    {
611      sWorldLog.Log ("CLIENT:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n",
612                     (uint32) get_handle (),
613                     new_pct->size (),
614                     LookupOpcodeName (new_pct->GetOpcode ()),
615                     new_pct->GetOpcode ());
616
617      uint32 p = 0;
618      while (p < new_pct->size ())
619        {
620          for (uint32 j = 0; j < 16 && p < new_pct->size (); j++)
621            sWorldLog.Log ("%.2X ", (*new_pct)[p++]);
622          sWorldLog.Log ("\n");
623        }
624      sWorldLog.Log ("\n\n");
625    }
626
627  // like one switch ;)
628  if (opcode == CMSG_PING)
629    {
630      return HandlePing (*new_pct);
631    }
632  else if (opcode == CMSG_AUTH_SESSION)
633    {
634      if (m_Session)
635        {
636          sLog.outError ("WorldSocket::ProcessIncoming: Player send CMSG_AUTH_SESSION again");
637          return -1;
638        }
639
640      return HandleAuthSession (*new_pct);
641    }
642  else if (opcode == CMSG_KEEP_ALIVE)
643    {
644      DEBUG_LOG ("CMSG_KEEP_ALIVE ,size: %d", new_pct->size ());
645
646      return 0;
647    }
648  else
649    {
650      ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
651
652      if (m_Session != NULL)
653        {
654          // OK ,give the packet to WorldSession
655          aptr.release ();
656          // WARNINIG here we call it with locks held.
657          // Its possible to cause deadlock if QueuePacket calls back
658          m_Session->QueuePacket (new_pct);
659          return 0;
660        }
661      else
662        {
663          sLog.outError ("WorldSocket::ProcessIncoming: Client not authed opcode = ", opcode);
664          return -1;
665        }
666    }
667
668  ACE_NOTREACHED (return 0);
669}
670
671int
672WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
673{
674  uint8 digest[20];
675  uint32 clientSeed;
676  uint32 unk2;
677  uint32 BuiltNumberClient;
678  uint32 id, security;
679  bool tbc = false;
680  LocaleConstant locale;
681  std::string account;
682  Sha1Hash sha1;
683  BigNumber v, s, g, N, x, I;
684  WorldPacket packet, SendAddonPacked;
685
686  BigNumber K;
687
688  if (recvPacket.size () < (4 + 4 + 1 + 4 + 20))
689    {
690      sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size");
691      return -1;
692    }
693
694  // Read the content of the packet
695  recvPacket >> BuiltNumberClient; // for now no use
696  recvPacket >> unk2;
697  recvPacket >> account;
698
699  if (recvPacket.size () < (4 + 4 + (account.size () + 1) + 4 + 20))
700    {
701      sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size second check");
702      return -1;
703    }
704
705  recvPacket >> clientSeed;
706  recvPacket.read (digest, 20);
707
708  DEBUG_LOG ("WorldSocket::HandleAuthSession: client %u, unk2 %u, account %s, clientseed %u",
709             BuiltNumberClient,
710             unk2,
711             account.c_str (),
712             clientSeed);
713
714  // Get the account information from the realmd database
715  std::string safe_account = account; // Duplicate, else will screw the SHA hash verification below
716  loginDatabase.escape_string (safe_account);
717  // No SQL injection, username escaped.
718
719  QueryResult *result =
720          loginDatabase.PQuery ("SELECT "
721                                "id, " //0
722                                "gmlevel, " //1
723                                "sessionkey, " //2
724                                "last_ip, " //3
725                                "locked, " //4
726                                "sha_pass_hash, " //5
727                                "v, " //6
728                                "s, " //7
729                                "tbc, " //8
730                                "mutetime, " //9
731                                "locale " //10
732                                "FROM account "
733                                "WHERE username = '%s'",
734                                safe_account.c_str ());
735
736  // Stop if the account is not found
737  if (!result)
738    {
739      packet.Initialize (SMSG_AUTH_RESPONSE, 1);
740      packet << uint8 (AUTH_UNKNOWN_ACCOUNT);
741
742      SendPacket (packet);
743
744      sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (unknown account).");
745      return -1;
746    }
747
748  Field* fields = result->Fetch ();
749
750  tbc = fields[8].GetUInt8 () && sWorld.getConfig (CONFIG_EXPANSION) > 0;
751
752  N.SetHexStr ("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7");
753  g.SetDword (7);
754  I.SetHexStr (fields[5].GetString ());
755
756  //In case of leading zeros in the I hash, restore them
757  uint8 mDigest[SHA_DIGEST_LENGTH];
758  memset (mDigest, 0, SHA_DIGEST_LENGTH);
759
760  if (I.GetNumBytes () <= SHA_DIGEST_LENGTH)
761    memcpy (mDigest, I.AsByteArray (), I.GetNumBytes ());
762
763  std::reverse (mDigest, mDigest + SHA_DIGEST_LENGTH);
764
765  s.SetHexStr (fields[7].GetString ());
766  sha1.UpdateData (s.AsByteArray (), s.GetNumBytes ());
767  sha1.UpdateData (mDigest, SHA_DIGEST_LENGTH);
768  sha1.Finalize ();
769  x.SetBinary (sha1.GetDigest (), sha1.GetLength ());
770  v = g.ModExp (x, N);
771
772  const char* sStr = s.AsHexStr (); //Must be freed by OPENSSL_free()
773  const char* vStr = v.AsHexStr (); //Must be freed by OPENSSL_free()
774  const char* vold = fields[6].GetString ();
775
776  DEBUG_LOG ("WorldSocket::HandleAuthSession: "
777             "(s,v) check s: %s v_old: %s v_new: %s",
778             sStr,
779             vold,
780             vStr);
781
782  loginDatabase.PExecute ("UPDATE account "
783                          "SET "
784                          "v = '0', "
785                          "s = '0' "
786                          "WHERE username = '%s'",
787                          safe_account.c_str ());
788
789  if (!vold || strcmp (vStr, vold))
790    {
791      packet.Initialize (SMSG_AUTH_RESPONSE, 1);
792      packet << uint8 (AUTH_UNKNOWN_ACCOUNT);
793      SendPacket (packet);
794      delete result;
795      OPENSSL_free ((void*) sStr);
796      OPENSSL_free ((void*) vStr);
797
798      sLog.outBasic ("WorldSocket::HandleAuthSession: User not logged.");
799      return -1;
800    }
801
802  OPENSSL_free ((void*) sStr);
803  OPENSSL_free ((void*) vStr);
804
805  ///- Re-check ip locking (same check as in realmd).
806  if (fields[4].GetUInt8 () == 1) // if ip is locked
807    {
808      if (strcmp (fields[3].GetString (), GetRemoteAddress ().c_str ()))
809        {
810          packet.Initialize (SMSG_AUTH_RESPONSE, 1);
811          packet << uint8 (AUTH_FAILED);
812          SendPacket (packet);
813
814          delete result;
815          sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs).");
816          return -1;
817        }
818    }
819
820  id = fields[0].GetUInt32 ();
821  security = fields[1].GetUInt16 ();
822  K.SetHexStr (fields[2].GetString ());
823
824  time_t mutetime = time_t (fields[9].GetUInt64 ());
825
826  locale = LocaleConstant (fields[10].GetUInt8 ());
827  if (locale >= MAX_LOCALE)
828    locale = LOCALE_enUS;
829
830  delete result;
831
832  // Re-check account ban (same check as in realmd)
833  QueryResult *banresult =
834          loginDatabase.PQuery ("SELECT "
835                                "bandate, "
836                                "unbandate "
837                                "FROM account_banned "
838                                "WHERE id = '%u' "
839                                "AND active = 1",
840                                id);
841
842  if (banresult) // if account banned
843    {
844      packet.Initialize (SMSG_AUTH_RESPONSE, 1);
845      packet << uint8 (AUTH_BANNED);
846      SendPacket (packet);
847
848      delete banresult;
849
850      sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned).");
851      return -1;
852    }
853
854  // Check locked state for server
855  AccountTypes allowedAccountType = sWorld.GetPlayerSecurityLimit ();
856
857  if (allowedAccountType > SEC_PLAYER && security < allowedAccountType)
858    {
859      WorldPacket Packet (SMSG_AUTH_RESPONSE, 1);
860      Packet << uint8 (AUTH_UNAVAILABLE);
861
862      SendPacket (packet);
863
864      sLog.outBasic ("WorldSocket::HandleAuthSession: User tryes to login but his security level is not enough");
865      return -1;
866    }
867
868  // Check that Key and account name are the same on client and server
869  Sha1Hash sha;
870
871  uint32 t = 0;
872  uint32 seed = m_Seed;
873
874  sha.UpdateData (account);
875  sha.UpdateData ((uint8 *) & t, 4);
876  sha.UpdateData ((uint8 *) & clientSeed, 4);
877  sha.UpdateData ((uint8 *) & seed, 4);
878  sha.UpdateBigNumbers (&K, NULL);
879  sha.Finalize ();
880
881  if (memcmp (sha.GetDigest (), digest, 20))
882    {
883      packet.Initialize (SMSG_AUTH_RESPONSE, 1);
884      packet << uint8 (AUTH_FAILED);
885
886      SendPacket (packet);
887
888      sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (authentification failed).");
889      return -1;
890    }
891
892  std::string address = this->GetRemoteAddress ();
893
894  DEBUG_LOG ("WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.",
895             account.c_str (),
896             address.c_str ());
897
898  // Update the last_ip in the database
899  // No SQL injection, username escaped.
900  loginDatabase.escape_string (address);
901
902  loginDatabase.PExecute ("UPDATE account "
903                          "SET last_ip = '%s' "
904                          "WHERE username = '%s'",
905                          address.c_str (),
906                          safe_account.c_str ());
907
908  // TODO protect here probably ?
909  // Althought atm the socket is singlethreaded
910  ACE_NEW_RETURN (m_Session, WorldSession (id, this, security, tbc, mutetime, locale), -1);
911
912  m_Crypt.SetKey (&K);
913  m_Crypt.Init ();
914
915  // In case needed sometime the second arg is in microseconds 1 000 000 = 1 sec
916  ACE_OS::sleep (ACE_Time_Value (0, 10000));
917
918  // TODO error handling
919  sWorld.AddSession (this->m_Session);
920
921  // Create and send the Addon packet
922  if (sAddOnHandler.BuildAddonPacket (&recvPacket, &SendAddonPacked))
923    SendPacket (SendAddonPacked);
924
925  return 0;
926}
927
928int
929WorldSocket::HandlePing (WorldPacket& recvPacket)
930{
931  uint32 ping;
932  uint32 latency;
933
934  if (recvPacket.size () < 8)
935    {
936      sLog.outError ("WorldSocket::_HandlePing wrong packet size");
937      return -1;
938    }
939
940  // Get the ping packet content
941  recvPacket >> ping;
942  recvPacket >> latency;
943
944  if (m_LastPingTime == ACE_Time_Value::zero)
945    m_LastPingTime = ACE_OS::gettimeofday (); // for 1st ping
946  else
947    {
948      ACE_Time_Value cur_time = ACE_OS::gettimeofday ();
949      ACE_Time_Value diff_time (cur_time);
950      diff_time -= m_LastPingTime;
951      m_LastPingTime = cur_time;
952
953      if (diff_time < ACE_Time_Value (27))
954        {
955          ++m_OverSpeedPings;
956
957          uint32 max_count = sWorld.getConfig (CONFIG_MAX_OVERSPEED_PINGS);
958
959          if (max_count && m_OverSpeedPings > max_count)
960            {
961              ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
962
963              if (m_Session && m_Session->GetSecurity () == SEC_PLAYER)
964                {
965                  sLog.outError ("WorldSocket::HandlePing: Player kicked for "
966                                 "overspeeded pings adress = %s",
967                                 GetRemoteAddress ().c_str ());
968
969                  return -1;
970                }
971            }
972        }
973      else
974        m_OverSpeedPings = 0;
975    }
976
977  // critical section
978  {
979    ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1);
980
981    if (m_Session)
982      m_Session->SetLatency (latency);
983    else
984      {
985        sLog.outError ("WorldSocket::HandlePing: peer sent CMSG_PING, "
986                       "but is not authenticated or got recently kicked,"
987                       " adress = %s",
988                       this->GetRemoteAddress ().c_str ());
989        return -1;
990      }
991  }
992
993  WorldPacket packet (SMSG_PONG, 4);
994  packet << ping;
995  return this->SendPacket (packet);
996}
997
998int
999WorldSocket::iSendPacket (const WorldPacket& pct)
1000{
1001  if (m_OutBuffer->space () < pct.size () + sizeof (ServerPktHeader))
1002    {
1003      errno = ENOBUFS;
1004      return -1;
1005    }
1006
1007  ServerPktHeader header;
1008
1009  header.cmd = pct.GetOpcode ();
1010
1011#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN
1012  header.cmd = ACE_SWAP_WORD (header.cmd)
1013#endif
1014
1015          header.size = (uint16) pct.size () + 2;
1016  header.size = ACE_HTONS (header.size);
1017
1018  m_Crypt.EncryptSend ((uint8*) & header, sizeof (header));
1019
1020  if (m_OutBuffer->copy ((char*) & header, sizeof (header)) == -1)
1021    ACE_ASSERT (false);
1022
1023  if (!pct.empty ())
1024    if (m_OutBuffer->copy ((char*) pct.contents (), pct.size ()) == -1)
1025      ACE_ASSERT (false);
1026
1027  return 0;
1028}
1029
1030bool
1031WorldSocket::iFlushPacketQueue ()
1032{
1033  WorldPacket *pct;
1034  bool haveone = false;
1035
1036  while (m_PacketQueue.dequeue_head (pct) == 0)
1037    {
1038      if (iSendPacket (*pct) == -1)
1039        {
1040          if (m_PacketQueue.enqueue_head (pct) == -1)
1041            {
1042              delete pct;
1043              sLog.outError ("WorldSocket::iFlushPacketQueue m_PacketQueue->enqueue_head");
1044              return false;
1045            }
1046
1047          break;
1048        }
1049      else
1050        {
1051          haveone = true;
1052          delete pct;
1053        }
1054    }
1055
1056  return haveone;
1057}
Note: See TracBrowser for help on using the browser.