9
3

EQPacket.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 _EQPACKET_H
  17. #define _EQPACKET_H
  18. #include "types.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #ifdef WIN32
  22. #include <time.h>
  23. #include <WinSock2.h>
  24. #else
  25. #include <sys/time.h>
  26. #include <netinet/in.h>
  27. #endif
  28. #include "emu_opcodes.h"
  29. #include "op_codes.h"
  30. #include "packet_dump.h"
  31. class OpcodeManager;
  32. class EQStream;
  33. class EQPacket {
  34. friend class EQStream;
  35. public:
  36. unsigned char *pBuffer;
  37. uint32 size;
  38. uint32 src_ip,dst_ip;
  39. uint16 src_port,dst_port;
  40. uint32 priority;
  41. timeval timestamp;
  42. int16 version;
  43. ~EQPacket();
  44. void DumpRawHeader(uint16 seq=0xffff, FILE *to = stdout) const;
  45. void DumpRawHeaderNoTime(uint16 seq=0xffff, FILE *to = stdout) const;
  46. void DumpRaw(FILE *to = stdout) const;
  47. const char* GetOpcodeName();
  48. void setVersion(int16 new_version){ version = new_version; }
  49. void setSrcInfo(uint32 sip, uint16 sport) { src_ip=sip; src_port=sport; }
  50. void setDstInfo(uint32 dip, uint16 dport) { dst_ip=dip; dst_port=dport; }
  51. void setTimeInfo(uint32 ts_sec, uint32 ts_usec) { timestamp.tv_sec=ts_sec; timestamp.tv_usec=ts_usec; }
  52. void copyInfo(const EQPacket *p) { src_ip=p->src_ip; src_port=p->src_port; dst_ip=p->dst_ip; dst_port=p->dst_port; timestamp.tv_sec=p->timestamp.tv_sec; timestamp.tv_usec=p->timestamp.tv_usec; }
  53. uint32 Size() const { return size+2; }
  54. //no reason to have this method in zone or world
  55. uint16 GetRawOpcode() const { return(opcode); }
  56. inline bool operator<(const EQPacket &rhs) {
  57. return (timestamp.tv_sec < rhs.timestamp.tv_sec || (timestamp.tv_sec==rhs.timestamp.tv_sec && timestamp.tv_usec < rhs.timestamp.tv_usec));
  58. }
  59. void SetProtocolOpcode(int16 new_opcode){
  60. opcode = new_opcode;
  61. }
  62. protected:
  63. uint16 opcode;
  64. EQPacket(const uint16 op, const unsigned char *buf, const uint32 len);
  65. EQPacket(const EQPacket &p) { version = 0; }
  66. EQPacket() { opcode=0; pBuffer=NULL; size=0; version = 0; setTimeInfo(0, 0); }
  67. };
  68. class EQApplicationPacket;
  69. class EQProtocolPacket : public EQPacket {
  70. public:
  71. EQProtocolPacket(uint16 op, const unsigned char *buf, uint32 len) : EQPacket(op,buf,len) {
  72. eq2_compressed = false;
  73. packet_prepared = false;
  74. packet_encrypted = false;
  75. sequence = 0;
  76. sent_time = 0;
  77. attempt_count = 0;
  78. acked = false;
  79. }
  80. EQProtocolPacket(const unsigned char *buf, uint32 len, int in_opcode = -1);
  81. bool combine(const EQProtocolPacket *rhs);
  82. uint32 serialize (unsigned char *dest, int8 offset = 0) const;
  83. static bool ValidateCRC(const unsigned char *buffer, int length, uint32 Key);
  84. static uint32 Decompress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize);
  85. static uint32 Compress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize);
  86. static void ChatDecode(unsigned char *buffer, int size, int DecodeKey);
  87. static void ChatEncode(unsigned char *buffer, int size, int EncodeKey);
  88. static bool IsProtocolPacket(const unsigned char* in_buff, uint32_t len, bool bTrimCRC);
  89. EQProtocolPacket *Copy() {
  90. EQProtocolPacket* new_packet = new EQProtocolPacket(opcode,pBuffer,size);
  91. new_packet->eq2_compressed = this->eq2_compressed;
  92. new_packet->packet_prepared = this->packet_prepared;
  93. new_packet->packet_encrypted = this->packet_encrypted;
  94. return new_packet;
  95. }
  96. EQApplicationPacket *MakeApplicationPacket(uint8 opcode_size=0) const;
  97. bool eq2_compressed;
  98. bool packet_prepared;
  99. bool packet_encrypted;
  100. bool acked;
  101. int32 sent_time;
  102. int8 attempt_count;
  103. int32 sequence;
  104. private:
  105. EQProtocolPacket(const EQProtocolPacket &p) { }
  106. //bool dont_combine;
  107. };
  108. class EQ2Packet : public EQProtocolPacket {
  109. public:
  110. EQ2Packet(const EmuOpcode in_login_op, const unsigned char *buf, uint32 len) : EQProtocolPacket(OP_Packet,buf,len){
  111. login_op = in_login_op;
  112. eq2_compressed = false;
  113. packet_prepared = false;
  114. packet_encrypted = false;
  115. }
  116. bool AppCombine(EQ2Packet* rhs);
  117. EQ2Packet* Copy() {
  118. EQ2Packet* new_packet = new EQ2Packet(login_op,pBuffer,size);
  119. new_packet->eq2_compressed = this->eq2_compressed;
  120. new_packet->packet_prepared = this->packet_prepared;
  121. new_packet->packet_encrypted = this->packet_encrypted;
  122. return new_packet;
  123. }
  124. int8 PreparePacket(int16 MaxLen);
  125. const char* GetOpcodeName();
  126. EmuOpcode login_op;
  127. };
  128. class EQApplicationPacket : public EQPacket {
  129. friend class EQProtocolPacket;
  130. friend class EQStream;
  131. public:
  132. EQApplicationPacket() : EQPacket(0,NULL,0) { emu_opcode = OP_Unknown; app_opcode_size=default_opcode_size; }
  133. EQApplicationPacket(const EmuOpcode op) : EQPacket(0,NULL,0) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  134. EQApplicationPacket(const EmuOpcode op, const uint32 len) : EQPacket(0,NULL,len) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  135. EQApplicationPacket(const EmuOpcode op, const unsigned char *buf, const uint32 len) : EQPacket(0,buf,len) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  136. bool combine(const EQApplicationPacket *rhs);
  137. uint32 serialize (unsigned char *dest) const;
  138. uint32 Size() const { return size+app_opcode_size; }
  139. EQApplicationPacket *Copy() const {
  140. EQApplicationPacket *it = new EQApplicationPacket;
  141. try {
  142. it->pBuffer= new unsigned char[size];
  143. memcpy(it->pBuffer,pBuffer,size);
  144. it->size=size;
  145. it->opcode = opcode;
  146. it->emu_opcode = emu_opcode;
  147. it->version = version;
  148. return(it);
  149. }
  150. catch( bad_alloc &ba )
  151. {
  152. cout << ba.what() << endl;
  153. if( NULL != it )
  154. delete it;
  155. }
  156. return NULL;
  157. }
  158. void SetOpcodeSize(uint8 s) { app_opcode_size=s; }
  159. void SetOpcode(EmuOpcode op);
  160. const EmuOpcode GetOpcodeConst() const;
  161. inline const EmuOpcode GetOpcode() const { return(GetOpcodeConst()); }
  162. //caching version of get
  163. inline const EmuOpcode GetOpcode() { EmuOpcode r = GetOpcodeConst(); emu_opcode = r; return(r); }
  164. static uint8 default_opcode_size;
  165. protected:
  166. //this is just a cache so we dont look it up several times on Get()
  167. EmuOpcode emu_opcode;
  168. private:
  169. //this constructor should only be used by EQProtocolPacket, as it
  170. //assumes the first two bytes of buf are the opcode.
  171. EQApplicationPacket(const unsigned char *buf, uint32 len, uint8 opcode_size=0);
  172. EQApplicationPacket(const EQApplicationPacket &p) { emu_opcode = OP_Unknown; app_opcode_size=default_opcode_size; }
  173. uint8 app_opcode_size;
  174. };
  175. void DumpPacketHex(const EQApplicationPacket* app);
  176. void DumpPacket(const EQProtocolPacket* app);
  177. void DumpPacketAscii(const EQApplicationPacket* app);
  178. void DumpPacket(const EQApplicationPacket* app, bool iShowInfo = false);
  179. void DumpPacketBin(const EQApplicationPacket* app);
  180. #endif