EQStream.h 9.3 KB

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