9
3

EQPacket.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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;}
  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. }
  79. EQProtocolPacket(const unsigned char *buf, uint32 len, int in_opcode = -1);
  80. bool combine(const EQProtocolPacket *rhs);
  81. uint32 serialize (unsigned char *dest, int8 offset = 0) const;
  82. static bool ValidateCRC(const unsigned char *buffer, int length, uint32 Key);
  83. static uint32 Decompress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize);
  84. static uint32 Compress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize);
  85. static void ChatDecode(unsigned char *buffer, int size, int DecodeKey);
  86. static void ChatEncode(unsigned char *buffer, int size, int EncodeKey);
  87. EQProtocolPacket *Copy() {
  88. EQProtocolPacket* new_packet = new EQProtocolPacket(opcode,pBuffer,size);
  89. new_packet->eq2_compressed = this->eq2_compressed;
  90. new_packet->packet_prepared = this->packet_prepared;
  91. new_packet->packet_encrypted = this->packet_encrypted;
  92. return new_packet;
  93. }
  94. EQApplicationPacket *MakeApplicationPacket(uint8 opcode_size=0) const;
  95. bool eq2_compressed;
  96. bool packet_prepared;
  97. bool packet_encrypted;
  98. int32 sent_time;
  99. int8 attempt_count;
  100. int32 sequence;
  101. private:
  102. EQProtocolPacket(const EQProtocolPacket &p) { }
  103. //bool dont_combine;
  104. };
  105. class EQ2Packet : public EQProtocolPacket {
  106. public:
  107. EQ2Packet(const EmuOpcode in_login_op, const unsigned char *buf, uint32 len) : EQProtocolPacket(OP_Packet,buf,len){
  108. login_op = in_login_op;
  109. eq2_compressed = false;
  110. packet_prepared = false;
  111. packet_encrypted = false;
  112. }
  113. bool AppCombine(EQ2Packet* rhs);
  114. EQ2Packet* Copy() {
  115. EQ2Packet* new_packet = new EQ2Packet(login_op,pBuffer,size);
  116. new_packet->eq2_compressed = this->eq2_compressed;
  117. new_packet->packet_prepared = this->packet_prepared;
  118. new_packet->packet_encrypted = this->packet_encrypted;
  119. return new_packet;
  120. }
  121. int8 PreparePacket(int16 MaxLen);
  122. EmuOpcode login_op;
  123. };
  124. class EQApplicationPacket : public EQPacket {
  125. friend class EQProtocolPacket;
  126. friend class EQStream;
  127. public:
  128. EQApplicationPacket() : EQPacket(0,NULL,0) { emu_opcode = OP_Unknown; app_opcode_size=default_opcode_size; }
  129. EQApplicationPacket(const EmuOpcode op) : EQPacket(0,NULL,0) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  130. EQApplicationPacket(const EmuOpcode op, const uint32 len) : EQPacket(0,NULL,len) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  131. EQApplicationPacket(const EmuOpcode op, const unsigned char *buf, const uint32 len) : EQPacket(0,buf,len) { SetOpcode(op); app_opcode_size=default_opcode_size; }
  132. bool combine(const EQApplicationPacket *rhs);
  133. uint32 serialize (unsigned char *dest) const;
  134. uint32 Size() const { return size+app_opcode_size; }
  135. EQApplicationPacket *Copy() const {
  136. EQApplicationPacket *it = new EQApplicationPacket;
  137. try {
  138. it->pBuffer= new unsigned char[size];
  139. memcpy(it->pBuffer,pBuffer,size);
  140. it->size=size;
  141. it->opcode = opcode;
  142. it->emu_opcode = emu_opcode;
  143. it->version = version;
  144. return(it);
  145. }
  146. catch( bad_alloc &ba )
  147. {
  148. cout << ba.what() << endl;
  149. if( NULL != it )
  150. delete it;
  151. }
  152. return NULL;
  153. }
  154. void SetOpcodeSize(uint8 s) { app_opcode_size=s; }
  155. void SetOpcode(EmuOpcode op);
  156. const EmuOpcode GetOpcodeConst() const;
  157. inline const EmuOpcode GetOpcode() const { return(GetOpcodeConst()); }
  158. //caching version of get
  159. inline const EmuOpcode GetOpcode() { EmuOpcode r = GetOpcodeConst(); emu_opcode = r; return(r); }
  160. static uint8 default_opcode_size;
  161. protected:
  162. //this is just a cache so we dont look it up several times on Get()
  163. EmuOpcode emu_opcode;
  164. private:
  165. //this constructor should only be used by EQProtocolPacket, as it
  166. //assumes the first two bytes of buf are the opcode.
  167. EQApplicationPacket(const unsigned char *buf, uint32 len, uint8 opcode_size=0);
  168. EQApplicationPacket(const EQApplicationPacket &p) { emu_opcode = OP_Unknown; app_opcode_size=default_opcode_size; }
  169. uint8 app_opcode_size;
  170. };
  171. void DumpPacketHex(const EQApplicationPacket* app);
  172. void DumpPacket(const EQProtocolPacket* app);
  173. void DumpPacketAscii(const EQApplicationPacket* app);
  174. void DumpPacket(const EQApplicationPacket* app, bool iShowInfo = false);
  175. void DumpPacketBin(const EQApplicationPacket* app);
  176. #endif