EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EVESession.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-common.h"
27 
28 #include "marshal/EVEMarshal.h"
29 #include "marshal/EVEUnmarshal.h"
30 #include "network/EVESession.h"
31 #include "packets/Crypto.h"
32 #include "python/PyVisitor.h"
33 #include "python/PyRep.h"
34 #include "python/PyPacket.h"
35 #include "python/PyDumpVisitor.h"
36 #include "EVEVersion.h"
37 
39 : mNet( *n ),
40 mPacketHandler( nullptr )
41 {
42  *n = nullptr;
43 }
44 
46  // Destroy connection we used
47  delete mNet;
48 }
49 
51  mPacketHandler = nullptr;
52 
54  // Connection has been lost, there's no point in reset
55  return;
56 
57  VersionExchangeServer version;
58  _GetVersion( version );
59 
60  PyRep* res(version.Encode());
61  mNet->QueueRep(res);
62 
64 }
65 
67  if (packet == nullptr)
68  return;
69 
70  PyRep* res(packet->Encode());
71 
72  if (res == nullptr) {
73  sLog.Error("QueuePacket", "%s: Failed to encode a packet.", GetAddress().c_str());
74  return;
75  }
76 
77  mNet->QueueRep( res );
78 }
79 
81  PyRep* rep(mNet->PopRep());
82  if (rep == nullptr)
83  return nullptr;
84 
85  if (is_log_enabled(NET__PRES_REP)) {
86  _log(NET__PRES_REP, "%s: Raw Rep Dump:", GetAddress().c_str());
87  rep->Dump(NET__PRES_REP, " ");
88  }
89 
90  assert( mPacketHandler );
91  return ( this->*mPacketHandler )( rep );
92 }
93 
95  //we are waiting for their version information...
96  VersionExchangeClient ve;
97  if ( !ve.Decode( &rep ) )
98  sLog.Error("_HandleVersion", "%s: Received invalid version exchange!", GetAddress().c_str());
99  else if ( _VerifyVersion( ve ) )
101 
102  // recurse
103  return PopPacket();
104 }
105 
107  //check if it actually is tuple
108  if ( !rep->IsTuple() )
109  sLog.Error("_HandleCommand", "%s: Invalid packet during waiting for command (tuple expected).", GetAddress().c_str());
110  else if ( rep->AsTuple()->size() == 2 ) { // decode
111  //QC = Queue Check
112  NetCommand_QC cmd;
113  if ( !cmd.Decode( &rep ) )
114  sLog.Error("_HandleCommand", "%s: Failed to decode 2-arg command.", GetAddress().c_str());
115  else {
116  sLog.Debug("_HandleCommand", "%s: Got Queue Check command.", GetAddress().c_str());
117 
118  //they return position in queue
119  PyRep* rsp = new PyInt( _GetQueuePosition() );
120  mNet->QueueRep( rsp );
121 
122  //now reset connection
123  Reset();
124  }
125  } else if ( rep->AsTuple()->size() == 3 ) {
126  //this is sent when client is logging in
127  NetCommand_VK cmd;
128  if ( !cmd.Decode( &rep ) ) {
129  sLog.Error("_HandleCommand", "%s: Failed to decode 3-arg command.", GetAddress().c_str());
130  } else {
131  sLog.Debug("_HandleCommand", "%s: Got VK command, vipKey=%s.", GetAddress().c_str(), cmd.vipKey.c_str());
132 
133  if ( _VerifyVIPKey( cmd.vipKey ) )
135  }
136  } else {
137  if (is_log_enabled(NET__PRES_ERROR)) {
138  _log(NET__PRES_ERROR, "%s: Received invalid command packet:", GetAddress().c_str());
139  rep->Dump(NET__PRES_ERROR, " ");
140  }
141  }
142 
143  // recurse
144  return PopPacket();
145 }
146 
148  CryptoRequestPacket cr;
149  if ( !cr.Decode( &rep ) )
150  sLog.Error("_HandleCrypto", "%s: Received invalid crypto request!", GetAddress().c_str());
151  else if ( _VerifyCrypto( cr ) )
153 
154  // recurse
155  return PopPacket();
156 }
157 
159  //just to be sure
160  CryptoChallengePacket ccp;
161  if ( !ccp.Decode( &rep ) )
162  sLog.Error("_HandleAuthentication", "%s: Received invalid crypto challenge!", GetAddress().c_str());
163  else if ( _VerifyLogin( ccp ) )
165 
166  return PopPacket();
167 }
168 
170  CryptoHandshakeResult hr;
171  if ( !hr.Decode( &rep ) )
172  sLog.Error("_HandleFuncResult", "%s: Received invalid crypto handshake result!", GetAddress().c_str());
173  else if ( _VerifyFuncResult( hr ) )
175 
176  return PopPacket();
177 }
178 
180  //take the PyRep and turn it into a PyPacket
181  PyPacket* p = new PyPacket();
182  if ( !p->Decode( &rep ) ) { //rep is consumed here
183  sLog.Error("_HandlePacket", "%s: Failed to decode packet rep", GetAddress().c_str());
184  SafeDelete( p );
185  PySafeDecRef(rep);
186  return PopPacket();
187  }
188 
189  return p;
190 }
Base Python wire object.
Definition: PyRep.h:66
PyPacket * _HandleAuthentication(PyRep *rep)
Definition: EVESession.cpp:158
PyTuple * AsTuple()
Definition: PyRep.h:138
void Reset()
Resets session.
Definition: EVESession.cpp:50
virtual bool _VerifyLogin(CryptoChallengePacket &ccp)=0
Verifies login.
#define _log(type, fmt,...)
Definition: logsys.h:124
PyPacket * _HandlePacket(PyRep *rep)
Definition: EVESession.cpp:179
PyPacket *(EVEClientSession::* mPacketHandler)(PyRep *rep)
Definition: EVESession.h:159
size_t size() const
Definition: PyRep.h:591
bool IsTuple() const
Definition: PyRep.h:108
PyPacket * _HandleFuncResult(PyRep *rep)
Definition: EVESession.cpp:169
std::string GetAddress() const
Definition: EVESession.h:67
void QueueRep(const PyRep *rep, bool compress=true)
Queues given PyRep into send queue.
PyRep * PopRep()
Pops PyRep from receive queue.
void Dump(FILE *into, const char *pfx) const
Dumps object to file.
Definition: PyRep.cpp:84
virtual bool _VerifyFuncResult(CryptoHandshakeResult &result)=0
Verifies function result.
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
PyPacket * _HandleCrypto(PyRep *rep)
Definition: EVESession.cpp:147
#define is_log_enabled(type)
Definition: logsys.h:78
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
EVE derivation of TCP connection.
virtual bool _VerifyVIPKey(const std::string &vipKey)=0
Verifies VIP key.
Python integer.
Definition: PyRep.h:231
virtual bool _VerifyCrypto(CryptoRequestPacket &cr)=0
Verifies crypto.
virtual bool _VerifyVersion(VersionExchangeClient &version)=0
Verifies version.
PyPacket * _HandleVersion(PyRep *rep)
Definition: EVESession.cpp:94
EVETCPConnection *const mNet
Definition: EVESession.h:155
void QueuePacket(PyPacket *packet)
Queues new packet, retaking ownership.
Definition: EVESession.cpp:66
PyRep * Encode()
Definition: PyPacket.cpp:240
virtual ~EVEClientSession()
Destroys contained connection.
Definition: EVESession.cpp:45
PyPacket * _HandleCommand(PyRep *rep)
Definition: EVESession.cpp:106
bool Decode(PyRep **packet)
Definition: PyPacket.cpp:115
PyPacket * PopPacket()
Pops new packet from queue.
Definition: EVESession.cpp:80
virtual void _GetVersion(VersionExchangeServer &version)=0
Obtains version.
state_t GetState() const
Definition: EVESession.h:65
EVEClientSession(EVETCPConnection **n)
Definition: EVESession.cpp:38
#define PySafeDecRef(op)
Definition: PyRep.h:61
virtual uint32 _GetQueuePosition()=0