EQStream.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator 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. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef _EQPROTOCOL_H
  17. #define _EQPROTOCOL_H
  18. #include <string>
  19. #include <vector>
  20. #include <deque>
  21. #include <map>
  22. #include <set>
  23. #ifndef WIN32
  24. #include <netinet/in.h>
  25. #endif
  26. #include "EQPacket.h"
  27. #include "Mutex.h"
  28. #include "opcodemgr.h"
  29. #include "misc.h"
  30. #include "Condition.h"
  31. #include "Crypto.h"
  32. #include "zlib.h"
  33. #include "timer.h"
  34. using namespace std;
  35. typedef enum {
  36. ESTABLISHED,
  37. CLOSING,
  38. CLOSED
  39. } EQStreamState;
  40. #define FLAG_COMPRESSED 0x01
  41. #define FLAG_ENCODED 0x04
  42. #define RATEBASE 1048576 // 1 MB
  43. #define DECAYBASE 78642 // RATEBASE/10
  44. #pragma pack(1)
  45. struct SessionRequest {
  46. uint32 UnknownA;
  47. uint32 Session;
  48. uint32 MaxLength;
  49. };
  50. struct SessionResponse {
  51. uint32 Session;
  52. uint32 Key;
  53. uint8 UnknownA;
  54. uint8 Format;
  55. uint8 UnknownB;
  56. uint32 MaxLength;
  57. uint32 UnknownD;
  58. };
  59. //Deltas are in ms, representing round trip times
  60. struct ClientSessionStats {
  61. /*000*/ uint16 RequestID;
  62. /*002*/ uint32 last_local_delta;
  63. /*006*/ uint32 average_delta;
  64. /*010*/ uint32 low_delta;
  65. /*014*/ uint32 high_delta;
  66. /*018*/ uint32 last_remote_delta;
  67. /*022*/ uint64 packets_sent;
  68. /*030*/ uint64 packets_recieved;
  69. /*038*/
  70. };
  71. struct ServerSessionStats {
  72. uint16 RequestID;
  73. uint32 current_time;
  74. uint32 unknown1;
  75. uint32 received_packets;
  76. uint32 unknown2;
  77. uint32 sent_packets;
  78. uint32 unknown3;
  79. uint32 sent_packets2;
  80. uint32 unknown4;
  81. uint32 received_packets2;
  82. };
  83. #pragma pack()
  84. class OpcodeManager;
  85. extern OpcodeManager *EQNetworkOpcodeManager;
  86. class EQStreamFactory;
  87. typedef enum {
  88. UnknownStream=0,
  89. LoginStream,
  90. WorldStream,
  91. ZoneStream,
  92. ChatOrMailStream,
  93. ChatStream,
  94. MailStream,
  95. EQ2Stream,
  96. } EQStreamType;
  97. class EQStream {
  98. protected:
  99. uint32 received_packets;
  100. uint32 sent_packets;
  101. uint32 remote_ip;
  102. uint16 remote_port;
  103. uint8 buffer[8192];
  104. unsigned char *oversize_buffer;
  105. uint32 oversize_offset,oversize_length;
  106. uint8 app_opcode_size;
  107. EQStreamType StreamType;
  108. bool compressed,encoded;
  109. //uint32 buffer_len;
  110. uint16 sessionAttempts;
  111. bool streamactive;
  112. uint32 Session, Key;
  113. uint16 NextInSeq;
  114. uint16 NextOutSeq;
  115. uint32 MaxLen;
  116. uint16 MaxSends;
  117. int8 timeout_delays;
  118. uint8 active_users; //how many things are actively using this
  119. Mutex MInUse;
  120. EQStreamState State;
  121. Mutex MState;
  122. uint32 LastPacket;
  123. Mutex MVarlock;
  124. EQApplicationPacket* CombinedAppPacket;
  125. Mutex MCombinedAppPacket;
  126. long LastSeqSent;
  127. Mutex MLastSeqSent;
  128. void SetLastSeqSent(uint32);
  129. // Ack sequence tracking.
  130. long MaxAckReceived,NextAckToSend,LastAckSent;
  131. long GetMaxAckReceived();
  132. long GetNextAckToSend();
  133. long GetLastAckSent();
  134. void SetMaxAckReceived(uint32 seq);
  135. void SetNextAckToSend(uint32);
  136. void SetLastAckSent(uint32);
  137. Mutex MAcks;
  138. // Packets waiting to be sent
  139. deque<EQProtocolPacket *> NonSequencedQueue;
  140. deque<EQProtocolPacket *> SequencedQueue;
  141. map<uint16, EQProtocolPacket *> OutOfOrderpackets;
  142. Mutex MOutboundQueue;
  143. // Packes waiting to be processed
  144. deque<EQApplicationPacket *> InboundQueue;
  145. Mutex MInboundQueue;
  146. static uint16 MaxWindowSize;
  147. sint32 BytesWritten;
  148. Mutex MRate;
  149. sint32 RateThreshold;
  150. sint32 DecayRate;
  151. EQStreamFactory *Factory;
  152. public:
  153. Mutex MCombineQueueLock;
  154. bool CheckCombineQueue();
  155. deque<EQ2Packet*> combine_queue;
  156. Timer* combine_timer;
  157. Crypto* crypto;
  158. int8 EQ2_Compress(EQ2Packet* app, int8 offset = 3);
  159. z_stream stream;
  160. uchar* stream_buffer;
  161. int32 stream_buffer_size;
  162. bool eq2_compressed;
  163. int8 compressed_offset;
  164. int16 client_version;
  165. int16 GetClientVersion(){ return client_version; }
  166. void SetClientVersion(int16 version){ client_version = version; }
  167. EQStream() { init(); remote_ip = 0; remote_port = 0; State = CLOSED; StreamType = UnknownStream; compressed = true;
  168. encoded = false; app_opcode_size = 2;}
  169. EQStream(sockaddr_in addr);
  170. virtual ~EQStream() {
  171. RemoveData();
  172. safe_delete(crypto);
  173. safe_delete(combine_timer);
  174. safe_delete(resend_que_timer);
  175. safe_delete_array(oversize_buffer);
  176. deque<EQ2Packet*>::iterator cmb;
  177. MCombineQueueLock.lock();
  178. for (cmb = combine_queue.begin(); cmb != combine_queue.end(); cmb++){
  179. safe_delete(*cmb);
  180. }
  181. MCombineQueueLock.unlock();
  182. deflateEnd(&stream);
  183. map<int16, EQProtocolPacket*>::iterator oop;
  184. for (oop = OutOfOrderpackets.begin(); oop != OutOfOrderpackets.end(); oop++){
  185. safe_delete(oop->second);
  186. }
  187. }
  188. inline void SetFactory(EQStreamFactory *f) { Factory=f; }
  189. void init(bool resetSession = true);
  190. void SetMaxLen(uint32 length) { MaxLen=length; }
  191. int8 getTimeoutDelays(){ return timeout_delays; }
  192. void addTimeoutDelay(){ timeout_delays++; }
  193. void EQ2QueuePacket(EQ2Packet* app, bool attempted_combine = false);
  194. void PreparePacket(EQ2Packet* app, int8 offset = 0);
  195. void UnPreparePacket(EQ2Packet* app);
  196. void EncryptPacket(EQ2Packet* app, int8 compress_offset, int8 offset);
  197. void FlushCombinedPacket();
  198. void SendPacket(EQApplicationPacket *p);
  199. void QueuePacket(EQProtocolPacket *p);
  200. void SendPacket(EQProtocolPacket *p);
  201. vector<EQProtocolPacket *> convert(EQApplicationPacket *p);
  202. void NonSequencedPush(EQProtocolPacket *p);
  203. void SequencedPush(EQProtocolPacket *p);
  204. Mutex MResendQue;
  205. Mutex MCompressData;
  206. deque<EQProtocolPacket*>resend_que;
  207. void CheckResend(int eq_fd);
  208. void Write(int eq_fd);
  209. void SetActive(bool val) { streamactive = val; }
  210. void WritePacket(int fd,EQProtocolPacket *p);
  211. void EncryptPacket(uchar* data, int16 size);
  212. uint32 GetKey() { return Key; }
  213. void SetKey(uint32 k) { Key=k; }
  214. void SetSession(uint32 s) { Session=s; }
  215. void SetLastPacketTime(uint32 t) {LastPacket=t;}
  216. void Process(const unsigned char *data, const uint32 length);
  217. void ProcessPacket(EQProtocolPacket *p);
  218. bool HandleEmbeddedPacket(EQProtocolPacket *p, int16 offset = 2, int16 length = 0);
  219. EQProtocolPacket * ProcessEncryptedPacket(EQProtocolPacket *p);
  220. EQProtocolPacket * ProcessEncryptedData(uchar* data, int32 size, int16 opcode);
  221. virtual void DispatchPacket(EQApplicationPacket *p) { p->DumpRaw(); }
  222. void SendSessionResponse();
  223. void SendSessionRequest();
  224. void SendDisconnect(bool setstate = true);
  225. void SendAck(uint16 seq);
  226. void SendOutOfOrderAck(uint16 seq);
  227. bool CheckTimeout(uint32 now, uint32 timeout=30) { return (LastPacket && (now-LastPacket) > timeout); }
  228. bool Stale(uint32 now, uint32 timeout=30) { return (LastPacket && (now-LastPacket) > timeout); }
  229. void InboundQueuePush(EQApplicationPacket *p);
  230. EQApplicationPacket *PopPacket(); // InboundQueuePop
  231. void InboundQueueClear();
  232. void OutboundQueueClear();
  233. bool HasOutgoingData();
  234. void SendKeyRequest();
  235. int16 processRSAKey(EQProtocolPacket *p);
  236. void RemoveData() { InboundQueueClear(); OutboundQueueClear(); if (CombinedAppPacket) delete CombinedAppPacket; }
  237. //
  238. inline bool IsInUse() { bool flag; MInUse.lock(); flag=(active_users>0); MInUse.unlock(); return flag; }
  239. inline void PutInUse() { MInUse.lock(); active_users++; MInUse.unlock(); }
  240. inline void ReleaseFromUse() { MInUse.lock(); if(active_users > 0) active_users--; MInUse.unlock(); }
  241. inline EQStreamState GetState() { return State; }
  242. inline void SetState(EQStreamState state) { State=state; }
  243. inline uint32 GetRemoteIP() { return remote_ip; }
  244. inline uint32 GetrIP() { return remote_ip; }
  245. inline uint16 GetRemotePort() { return remote_port; }
  246. inline uint16 GetrPort() { return remote_port; }
  247. static EQProtocolPacket *Read(int eq_fd, sockaddr_in *from);
  248. static sint8 CompareSequence(uint16 expected_seq , uint16 seq);
  249. void Close() { SendDisconnect(); }
  250. bool CheckActive() { return GetState()==ESTABLISHED; }
  251. bool CheckClosed() { return GetState()==CLOSED; }
  252. void SetOpcodeSize(uint8 s) { app_opcode_size = s; }
  253. void SetStreamType(EQStreamType t);
  254. inline const EQStreamType GetStreamType() const { return StreamType; }
  255. void ProcessQueue();
  256. EQProtocolPacket* RemoveQueue(uint16 seq);
  257. void Decay();
  258. void AdjustRates(uint32 average_delta);
  259. Timer* resend_que_timer;
  260. };
  261. #endif