9
3

EQStream.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. uint32 Session, Key;
  111. uint16 NextInSeq;
  112. uint16 NextOutSeq;
  113. uint32 MaxLen;
  114. uint16 MaxSends;
  115. int8 timeout_delays;
  116. uint8 active_users; //how many things are actively using this
  117. Mutex MInUse;
  118. EQStreamState State;
  119. Mutex MState;
  120. uint32 LastPacket;
  121. Mutex MVarlock;
  122. EQApplicationPacket* CombinedAppPacket;
  123. Mutex MCombinedAppPacket;
  124. long LastSeqSent;
  125. Mutex MLastSeqSent;
  126. void SetLastSeqSent(uint32);
  127. // Ack sequence tracking.
  128. long MaxAckReceived,NextAckToSend,LastAckSent;
  129. long GetMaxAckReceived();
  130. long GetNextAckToSend();
  131. long GetLastAckSent();
  132. void SetMaxAckReceived(uint32 seq);
  133. void SetNextAckToSend(uint32);
  134. void SetLastAckSent(uint32);
  135. Mutex MAcks;
  136. // Packets waiting to be sent
  137. deque<EQProtocolPacket *> NonSequencedQueue;
  138. deque<EQProtocolPacket *> SequencedQueue;
  139. map<uint16, EQProtocolPacket *> OutOfOrderpackets;
  140. Mutex MOutboundQueue;
  141. // Packes waiting to be processed
  142. deque<EQApplicationPacket *> InboundQueue;
  143. Mutex MInboundQueue;
  144. static uint16 MaxWindowSize;
  145. sint32 BytesWritten;
  146. Mutex MRate;
  147. sint32 RateThreshold;
  148. sint32 DecayRate;
  149. EQStreamFactory *Factory;
  150. public:
  151. Mutex MCombineQueueLock;
  152. bool CheckCombineQueue();
  153. deque<EQ2Packet*> combine_queue;
  154. Timer* combine_timer;
  155. Crypto* crypto;
  156. int8 EQ2_Compress(EQ2Packet* app, int8 offset = 3);
  157. z_stream stream;
  158. uchar* stream_buffer;
  159. int32 stream_buffer_size;
  160. bool eq2_compressed;
  161. int8 compressed_offset;
  162. int16 client_version;
  163. int16 GetClientVersion(){ return client_version; }
  164. void SetClientVersion(int16 version){ client_version = version; }
  165. EQStream() { init(); remote_ip = 0; remote_port = 0; State = CLOSED; StreamType = UnknownStream; compressed = true;
  166. encoded = false; app_opcode_size = 2;}
  167. EQStream(sockaddr_in addr);
  168. virtual ~EQStream() {
  169. RemoveData();
  170. safe_delete(crypto);
  171. safe_delete(combine_timer);
  172. safe_delete(resend_que_timer);
  173. safe_delete_array(oversize_buffer);
  174. deque<EQ2Packet*>::iterator cmb;
  175. MCombineQueueLock.lock();
  176. for (cmb = combine_queue.begin(); cmb != combine_queue.end(); cmb++){
  177. safe_delete(*cmb);
  178. }
  179. MCombineQueueLock.unlock();
  180. deflateEnd(&stream);
  181. map<int16, EQProtocolPacket*>::iterator oop;
  182. for (oop = OutOfOrderpackets.begin(); oop != OutOfOrderpackets.end(); oop++){
  183. safe_delete(oop->second);
  184. }
  185. }
  186. inline void SetFactory(EQStreamFactory *f) { Factory=f; }
  187. void init();
  188. void SetMaxLen(uint32 length) { MaxLen=length; }
  189. int8 getTimeoutDelays(){ return timeout_delays; }
  190. void addTimeoutDelay(){ timeout_delays++; }
  191. void EQ2QueuePacket(EQ2Packet* app, bool attempted_combine = false);
  192. void PreparePacket(EQ2Packet* app, int8 offset = 0);
  193. void UnPreparePacket(EQ2Packet* app);
  194. void EncryptPacket(EQ2Packet* app, int8 compress_offset, int8 offset);
  195. void FlushCombinedPacket();
  196. void SendPacket(EQApplicationPacket *p);
  197. void QueuePacket(EQProtocolPacket *p);
  198. void SendPacket(EQProtocolPacket *p);
  199. vector<EQProtocolPacket *> convert(EQApplicationPacket *p);
  200. void NonSequencedPush(EQProtocolPacket *p);
  201. void SequencedPush(EQProtocolPacket *p);
  202. Mutex MResendQue;
  203. Mutex MCompressData;
  204. deque<EQProtocolPacket*>resend_que;
  205. void CheckResend(int eq_fd);
  206. void Write(int eq_fd);
  207. void WritePacket(int fd,EQProtocolPacket *p);
  208. void EncryptPacket(uchar* data, int16 size);
  209. uint32 GetKey() { return Key; }
  210. void SetKey(uint32 k) { Key=k; }
  211. void SetSession(uint32 s) { Session=s; }
  212. void SetLastPacketTime(uint32 t) {LastPacket=t;}
  213. void Process(const unsigned char *data, const uint32 length);
  214. void ProcessPacket(EQProtocolPacket *p);
  215. bool HandleEmbeddedPacket(EQProtocolPacket *p, int16 offset = 2, int16 length = 0);
  216. EQProtocolPacket * ProcessEncryptedPacket(EQProtocolPacket *p);
  217. EQProtocolPacket * ProcessEncryptedData(uchar* data, int32 size, int16 opcode);
  218. virtual void DispatchPacket(EQApplicationPacket *p) { p->DumpRaw(); }
  219. void SendSessionResponse();
  220. void SendSessionRequest();
  221. void SendDisconnect(bool setstate = true);
  222. void SendAck(uint16 seq);
  223. void SendOutOfOrderAck(uint16 seq);
  224. bool CheckTimeout(uint32 now, uint32 timeout=30) { return (LastPacket && (now-LastPacket) > timeout); }
  225. bool Stale(uint32 now, uint32 timeout=30) { return (LastPacket && (now-LastPacket) > timeout); }
  226. void InboundQueuePush(EQApplicationPacket *p);
  227. EQApplicationPacket *PopPacket(); // InboundQueuePop
  228. void InboundQueueClear();
  229. void OutboundQueueClear();
  230. bool HasOutgoingData();
  231. void SendKeyRequest();
  232. int16 processRSAKey(EQProtocolPacket *p);
  233. void RemoveData() { InboundQueueClear(); OutboundQueueClear(); if (CombinedAppPacket) delete CombinedAppPacket; }
  234. //
  235. inline bool IsInUse() { bool flag; MInUse.lock(); flag=(active_users>0); MInUse.unlock(); return flag; }
  236. inline void PutInUse() { MInUse.lock(); active_users++; MInUse.unlock(); }
  237. inline void ReleaseFromUse() { MInUse.lock(); if(active_users > 0) active_users--; MInUse.unlock(); }
  238. inline EQStreamState GetState() { return State; }
  239. inline void SetState(EQStreamState state) { State=state; }
  240. inline uint32 GetRemoteIP() { return remote_ip; }
  241. inline uint32 GetrIP() { return remote_ip; }
  242. inline uint16 GetRemotePort() { return remote_port; }
  243. inline uint16 GetrPort() { return remote_port; }
  244. static EQProtocolPacket *Read(int eq_fd, sockaddr_in *from);
  245. static sint8 CompareSequence(uint16 expected_seq , uint16 seq);
  246. void Close() { SendDisconnect(); }
  247. bool CheckActive() { return GetState()==ESTABLISHED; }
  248. bool CheckClosed() { return GetState()==CLOSED; }
  249. void SetOpcodeSize(uint8 s) { app_opcode_size = s; }
  250. void SetStreamType(EQStreamType t);
  251. inline const EQStreamType GetStreamType() const { return StreamType; }
  252. void Decay();
  253. void AdjustRates(uint32 average_delta);
  254. Timer* resend_que_timer;
  255. };
  256. #endif