EQPacket.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. #include "debug.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <iostream>
  20. #include <iomanip>
  21. #include "EQPacket.h"
  22. #include "misc.h"
  23. #include "op_codes.h"
  24. #include "CRC16.h"
  25. #include "opcodemgr.h"
  26. #include "packet_dump.h"
  27. #include <map>
  28. #include "Log.h"
  29. #include <time.h>
  30. using namespace std;
  31. extern map<int16,OpcodeManager*>EQOpcodeManager;
  32. uint8 EQApplicationPacket::default_opcode_size=2;
  33. EQPacket::EQPacket(const uint16 op, const unsigned char *buf, uint32 len)
  34. {
  35. this->opcode=op;
  36. this->pBuffer=NULL;
  37. this->size=0;
  38. version = 0;
  39. setTimeInfo(0,0);
  40. if (len>0) {
  41. this->size=len;
  42. pBuffer= new unsigned char[this->size];
  43. if (buf) {
  44. memcpy(this->pBuffer,buf,this->size);
  45. } else {
  46. memset(this->pBuffer,0,this->size);
  47. }
  48. }
  49. }
  50. int8 EQ2Packet::PreparePacket(int16 MaxLen){
  51. int16 OpcodeVersion = GetOpcodeVersion(version);
  52. // stops a crash for incorrect version
  53. if(EQOpcodeManager.count(OpcodeVersion) == 0)
  54. {
  55. LogWrite(PACKET__ERROR, 0, "Packet", "Version %i is not listed in the opcodes table.",version);
  56. return -1;
  57. }
  58. packet_prepared = true;
  59. int16 login_opcode = EQOpcodeManager[OpcodeVersion]->EmuToEQ(login_op);
  60. int8 offset = 0;
  61. //one of the int16s is for the seq, other is for the EQ2 opcode and compressed flag (OP_Packet is the header, not the opcode)
  62. int32 new_size = size + sizeof(int16) + sizeof(int8);
  63. bool oversized = false;
  64. if(login_opcode != 2){
  65. new_size+=sizeof(int8); //for opcode
  66. if(login_opcode >= 255){
  67. new_size += sizeof(int16);
  68. oversized = true;
  69. }
  70. else
  71. login_opcode = ntohs(login_opcode);
  72. }
  73. uchar* new_buffer = new uchar[new_size];
  74. memset(new_buffer,0,new_size);
  75. uchar* ptr = new_buffer + sizeof(int16); // sequence is first
  76. if(login_opcode != 2){
  77. if(oversized){
  78. ptr += sizeof(int8); //compressed flag
  79. int8 addon = 0xff;
  80. memcpy(ptr, &addon, sizeof(int8));
  81. ptr += sizeof(int8);
  82. }
  83. memcpy(ptr, &login_opcode, sizeof(int16));
  84. ptr += sizeof(int16);
  85. }
  86. else{
  87. memcpy(ptr, &login_opcode, sizeof(int8));
  88. ptr += sizeof(int8);
  89. }
  90. memcpy(ptr, pBuffer, size);
  91. safe_delete_array(pBuffer);
  92. pBuffer = new_buffer;
  93. offset = new_size - size - 1;
  94. size = new_size;
  95. return offset;
  96. }
  97. uint32 EQProtocolPacket::serialize(unsigned char *dest, int8 offset) const
  98. {
  99. if (opcode>0xff) {
  100. *(uint16 *)dest=opcode;
  101. } else {
  102. *(dest)=0;
  103. *(dest+1)=opcode;
  104. }
  105. memcpy(dest+2,pBuffer+offset,size-offset);
  106. return size+2;
  107. }
  108. uint32 EQApplicationPacket::serialize(unsigned char *dest) const
  109. {
  110. if (app_opcode_size==1)
  111. *(unsigned char *)dest=opcode;
  112. else
  113. *(uint16 *)dest=opcode;
  114. memcpy(dest+app_opcode_size,pBuffer,size);
  115. return size+app_opcode_size;
  116. }
  117. EQPacket::~EQPacket()
  118. {
  119. safe_delete_array(pBuffer);
  120. pBuffer=NULL;
  121. }
  122. void EQPacket::DumpRawHeader(uint16 seq, FILE* to) const
  123. {
  124. /*if (timestamp.tv_sec) {
  125. char temp[20];
  126. tm t;
  127. const time_t sec = timestamp.tv_sec;
  128. localtime_s(&t, &sec);
  129. strftime(temp, 20, "%F %T", &t);
  130. fprintf(to, "%s.%06lu ", temp, timestamp.tv_usec);
  131. }*/
  132. DumpRawHeaderNoTime(seq, to);
  133. }
  134. const char* EQPacket::GetOpcodeName(){
  135. int16 OpcodeVersion = GetOpcodeVersion(version);
  136. if(EQOpcodeManager.count(OpcodeVersion) > 0)
  137. return EQOpcodeManager[OpcodeVersion]->EQToName(opcode);
  138. else
  139. return NULL;
  140. }
  141. void EQPacket::DumpRawHeaderNoTime(uint16 seq, FILE *to) const
  142. {
  143. if (src_ip) {
  144. string sIP,dIP;;
  145. sIP=long2ip(src_ip);
  146. dIP=long2ip(dst_ip);
  147. fprintf(to, "[%s:%d->%s:%d] ",sIP.c_str(),src_port,dIP.c_str(),dst_port);
  148. }
  149. if (seq != 0xffff)
  150. fprintf(to, "[Seq=%u] ",seq);
  151. string name;
  152. int16 OpcodeVersion = GetOpcodeVersion(version);
  153. if(EQOpcodeManager.count(OpcodeVersion) > 0)
  154. name = EQOpcodeManager[OpcodeVersion]->EQToName(opcode);
  155. fprintf(to, "[OpCode 0x%04x (%s) Size=%u]\n",opcode,name.c_str(),size);
  156. }
  157. void EQPacket::DumpRaw(FILE *to) const
  158. {
  159. DumpRawHeader();
  160. if (pBuffer && size)
  161. dump_message_column(pBuffer, size, " ", to);
  162. fprintf(to, "\n");
  163. }
  164. EQProtocolPacket::EQProtocolPacket(const unsigned char *buf, uint32 len, int in_opcode)
  165. {
  166. uint32 offset = 0;
  167. if(in_opcode>=0)
  168. opcode = in_opcode;
  169. else{
  170. offset=2;
  171. opcode=ntohs(*(const uint16 *)buf);
  172. }
  173. if (len-offset) {
  174. pBuffer= new unsigned char[len-offset];
  175. size=len-offset;
  176. if(buf)
  177. memcpy(pBuffer,buf+offset,len-offset);
  178. else
  179. memset(pBuffer,0,size);
  180. } else {
  181. pBuffer=NULL;
  182. size=0;
  183. }
  184. version = 0;
  185. eq2_compressed = false;
  186. packet_prepared = false;
  187. packet_encrypted = false;
  188. sent_time = 0;
  189. attempt_count = 0;
  190. sequence = 0;
  191. }
  192. bool EQ2Packet::AppCombine(EQ2Packet* rhs){
  193. bool result = false;
  194. uchar* tmpbuffer = 0;
  195. bool over_sized_packet = false;
  196. int32 new_size = 0;
  197. //bool whee = false;
  198. // DumpPacket(this);
  199. // DumpPacket(rhs);
  200. /*if(rhs->size >= 255){
  201. DumpPacket(this);
  202. DumpPacket(rhs);
  203. whee = true;
  204. }*/
  205. if (opcode==OP_AppCombined && ((size + rhs->size + 3) < 512)){
  206. int16 tmp_size = rhs->size - 2;
  207. if(tmp_size >= 255){
  208. new_size = size+tmp_size+3;
  209. over_sized_packet = true;
  210. }
  211. else
  212. new_size = size+tmp_size+1;
  213. tmpbuffer = new uchar[new_size];
  214. uchar* ptr = tmpbuffer;
  215. memcpy(ptr, pBuffer, size);
  216. ptr += size;
  217. if(over_sized_packet){
  218. memset(ptr, 255, sizeof(int8));
  219. ptr += sizeof(int8);
  220. tmp_size = htons(tmp_size);
  221. memcpy(ptr, &tmp_size, sizeof(int16));
  222. ptr += sizeof(int16);
  223. }
  224. else{
  225. memcpy(ptr, &tmp_size, sizeof(int8));
  226. ptr += sizeof(int8);
  227. }
  228. memcpy(ptr, rhs->pBuffer+2, rhs->size-2);
  229. delete[] pBuffer;
  230. size = new_size;
  231. pBuffer=tmpbuffer;
  232. safe_delete(rhs);
  233. result=true;
  234. }
  235. else if((size + rhs->size + 6) < 512){
  236. int32 tmp_size = size - 2;
  237. int32 tmp_size2 = rhs->size - 2;
  238. opcode=OP_AppCombined;
  239. bool over_sized_packet2 = false;
  240. new_size = size;
  241. if(tmp_size >= 255){
  242. new_size += 5;
  243. over_sized_packet = true;
  244. }
  245. else
  246. new_size += 3;
  247. if(tmp_size2 >= 255){
  248. new_size += tmp_size2+3;
  249. over_sized_packet2 = true;
  250. }
  251. else
  252. new_size += tmp_size2+1;
  253. tmpbuffer = new uchar[new_size];
  254. tmpbuffer[2]=0;
  255. tmpbuffer[3]=0x19;
  256. uchar* ptr = tmpbuffer+4;
  257. if(over_sized_packet){
  258. memset(ptr, 255, sizeof(int8));
  259. ptr += sizeof(int8);
  260. tmp_size = htons(tmp_size);
  261. memcpy(ptr, &tmp_size, sizeof(int16));
  262. ptr += sizeof(int16);
  263. }
  264. else{
  265. memcpy(ptr, &tmp_size, sizeof(int8));
  266. ptr += sizeof(int8);
  267. }
  268. memcpy(ptr, pBuffer+2, size-2);
  269. ptr += (size-2);
  270. if(over_sized_packet2){
  271. memset(ptr, 255, sizeof(int8));
  272. ptr += sizeof(int8);
  273. tmp_size2 = htons(tmp_size2);
  274. memcpy(ptr, &tmp_size2, sizeof(int16));
  275. ptr += sizeof(int16);
  276. }
  277. else{
  278. memcpy(ptr, &tmp_size2, sizeof(int8));
  279. ptr += sizeof(int8);
  280. }
  281. memcpy(ptr, rhs->pBuffer+2, rhs->size-2);
  282. size = new_size;
  283. delete[] pBuffer;
  284. pBuffer=tmpbuffer;
  285. safe_delete(rhs);
  286. result=true;
  287. }
  288. /*if(whee){
  289. DumpPacket(this);
  290. cout << "fsdfsdf";
  291. }*/
  292. //DumpPacket(this);
  293. return result;
  294. }
  295. bool EQProtocolPacket::combine(const EQProtocolPacket *rhs)
  296. {
  297. bool result=false;
  298. //if(dont_combine)
  299. // return false;
  300. //if (opcode==OP_Combined && size+rhs->size+5<256) {
  301. if (opcode == OP_Combined && size + rhs->size + 5 < 256) {
  302. auto tmpbuffer = new unsigned char[size + rhs->size + 3];
  303. memcpy(tmpbuffer, pBuffer, size);
  304. uint32 offset = size;
  305. tmpbuffer[offset++] = rhs->Size();
  306. offset += rhs->serialize(tmpbuffer + offset);
  307. size = offset;
  308. delete[] pBuffer;
  309. pBuffer = tmpbuffer;
  310. result = true;
  311. }
  312. else if (size + rhs->size + 7 < 256) {
  313. auto tmpbuffer = new unsigned char[size + rhs->size + 6];
  314. uint32 offset = 0;
  315. tmpbuffer[offset++] = Size();
  316. offset += serialize(tmpbuffer + offset);
  317. tmpbuffer[offset++] = rhs->Size();
  318. offset += rhs->serialize(tmpbuffer + offset);
  319. size = offset;
  320. delete[] pBuffer;
  321. pBuffer = tmpbuffer;
  322. opcode = OP_Combined;
  323. result = true;
  324. }
  325. return result;
  326. }
  327. EQApplicationPacket::EQApplicationPacket(const unsigned char *buf, uint32 len, uint8 opcode_size)
  328. {
  329. uint32 offset=0;
  330. app_opcode_size=(opcode_size==0) ? EQApplicationPacket::default_opcode_size : opcode_size;
  331. if (app_opcode_size==1) {
  332. opcode=*(const unsigned char *)buf;
  333. offset++;
  334. } else {
  335. opcode=*(const uint16 *)buf;
  336. offset+=2;
  337. }
  338. if ((len-offset)>0) {
  339. pBuffer=new unsigned char[len-offset];
  340. memcpy(pBuffer,buf+offset,len-offset);
  341. size=len-offset;
  342. } else {
  343. pBuffer=NULL;
  344. size=0;
  345. }
  346. emu_opcode = OP_Unknown;
  347. }
  348. bool EQApplicationPacket::combine(const EQApplicationPacket *rhs)
  349. {
  350. cout << "CALLED AP COMBINE!!!!\n";
  351. return false;
  352. }
  353. void EQApplicationPacket::SetOpcode(EmuOpcode emu_op) {
  354. if(emu_op == OP_Unknown) {
  355. opcode = 0;
  356. emu_opcode = OP_Unknown;
  357. return;
  358. }
  359. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(emu_op);
  360. if(opcode == OP_Unknown) {
  361. LogWrite(PACKET__DEBUG, 0, "Packet", "Unable to convert Emu opcode %s (%d) into an EQ opcode.", OpcodeNames[emu_op], emu_op);
  362. }
  363. //save the emu opcode we just set.
  364. emu_opcode = emu_op;
  365. }
  366. const EmuOpcode EQApplicationPacket::GetOpcodeConst() const {
  367. if(emu_opcode != OP_Unknown) {
  368. return(emu_opcode);
  369. }
  370. if(opcode == 10000) {
  371. return(OP_Unknown);
  372. }
  373. EmuOpcode emu_op;
  374. emu_op = EQOpcodeManager[GetOpcodeVersion(version)]->EQToEmu(opcode);
  375. if(emu_op == OP_Unknown) {
  376. LogWrite(PACKET__DEBUG, 1, "Packet", "Unable to convert EQ opcode 0x%.4X (%i) to an emu opcode (%s)", opcode, opcode, __FUNCTION__);
  377. }
  378. return(emu_op);
  379. }
  380. EQApplicationPacket *EQProtocolPacket::MakeApplicationPacket(uint8 opcode_size) const {
  381. EQApplicationPacket *res = new EQApplicationPacket;
  382. res->app_opcode_size=(opcode_size==0) ? EQApplicationPacket::default_opcode_size : opcode_size;
  383. if (res->app_opcode_size==1) {
  384. res->pBuffer= new unsigned char[size+1];
  385. memcpy(res->pBuffer+1,pBuffer,size);
  386. *(res->pBuffer)=htons(opcode)&0xff;
  387. res->opcode=opcode&0xff;
  388. res->size=size+1;
  389. } else {
  390. res->pBuffer= new unsigned char[size];
  391. memcpy(res->pBuffer,pBuffer,size);
  392. res->opcode=opcode;
  393. res->size=size;
  394. }
  395. res->copyInfo(this);
  396. return(res);
  397. }
  398. bool EQProtocolPacket::ValidateCRC(const unsigned char *buffer, int length, uint32 Key)
  399. {
  400. bool valid=false;
  401. // OP_SessionRequest, OP_SessionResponse, OP_OutOfSession are not CRC'd
  402. if (buffer[0]==0x00 && (buffer[1]==OP_SessionRequest || buffer[1]==OP_SessionResponse || buffer[1]==OP_OutOfSession)) {
  403. valid=true;
  404. } else if(buffer[2] == 0x00 && buffer[3] == 0x19){
  405. valid = true;
  406. }
  407. else {
  408. uint16 comp_crc=CRC16(buffer,length-2,Key);
  409. uint16 packet_crc=ntohs(*(const uint16 *)(buffer+length-2));
  410. #ifdef EQN_DEBUG
  411. if (packet_crc && comp_crc != packet_crc) {
  412. cout << "CRC mismatch: comp=" << hex << comp_crc << ", packet=" << packet_crc << dec << endl;
  413. }
  414. #endif
  415. valid = (!packet_crc || comp_crc == packet_crc);
  416. }
  417. return valid;
  418. }
  419. uint32 EQProtocolPacket::Decompress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize)
  420. {
  421. uint32 newlen=0;
  422. uint32 flag_offset=0;
  423. newbuf[0]=buffer[0];
  424. if (buffer[0]==0x00) {
  425. flag_offset=2;
  426. newbuf[1]=buffer[1];
  427. } else
  428. flag_offset=1;
  429. if (length>2 && buffer[flag_offset]==0x5a) {
  430. LogWrite(PACKET__DEBUG, 0, "Packet", "In Decompress 1");
  431. newlen=Inflate(const_cast<unsigned char *>(buffer+flag_offset+1),length-(flag_offset+1)-2,newbuf+flag_offset,newbufsize-flag_offset)+2;
  432. newbuf[newlen++]=buffer[length-2];
  433. newbuf[newlen++]=buffer[length-1];
  434. } else if (length>2 && buffer[flag_offset]==0xa5) {
  435. LogWrite(PACKET__DEBUG, 0, "Packet", "In Decompress 2");
  436. memcpy(newbuf+flag_offset,buffer+flag_offset+1,length-(flag_offset+1));
  437. newlen=length-1;
  438. } else {
  439. memcpy(newbuf,buffer,length);
  440. newlen=length;
  441. }
  442. return newlen;
  443. }
  444. uint32 EQProtocolPacket::Compress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize) {
  445. uint32 flag_offset=1,newlength;
  446. //dump_message_column(buffer,length,"Before: ");
  447. newbuf[0]=buffer[0];
  448. if (buffer[0]==0) {
  449. flag_offset=2;
  450. newbuf[1]=buffer[1];
  451. }
  452. if (length>30) {
  453. newlength=Deflate(const_cast<unsigned char *>(buffer+flag_offset),length-flag_offset,newbuf+flag_offset+1,newbufsize);
  454. *(newbuf+flag_offset)=0x5a;
  455. newlength+=flag_offset+1;
  456. } else {
  457. memmove(newbuf+flag_offset+1,buffer+flag_offset,length-flag_offset);
  458. *(newbuf+flag_offset)=0xa5;
  459. newlength=length+1;
  460. }
  461. //dump_message_column(newbuf,length,"After: ");
  462. return newlength;
  463. }
  464. void EQProtocolPacket::ChatDecode(unsigned char *buffer, int size, int DecodeKey)
  465. {
  466. if (buffer[1]!=0x01 && buffer[0]!=0x02 && buffer[0]!=0x1d) {
  467. int Key=DecodeKey;
  468. unsigned char *test=(unsigned char *)malloc(size);
  469. buffer+=2;
  470. size-=2;
  471. int i;
  472. for (i = 0 ; i+4 <= size ; i+=4)
  473. {
  474. int pt = (*(int*)&buffer[i])^(Key);
  475. Key = (*(int*)&buffer[i]);
  476. *(int*)&test[i]=pt;
  477. }
  478. unsigned char KC=Key&0xFF;
  479. for ( ; i < size ; i++)
  480. {
  481. test[i]=buffer[i]^KC;
  482. }
  483. memcpy(buffer,test,size);
  484. free(test);
  485. }
  486. }
  487. void EQProtocolPacket::ChatEncode(unsigned char *buffer, int size, int EncodeKey)
  488. {
  489. if (buffer[1]!=0x01 && buffer[0]!=0x02 && buffer[0]!=0x1d) {
  490. int Key=EncodeKey;
  491. char *test=(char*)malloc(size);
  492. int i;
  493. buffer+=2;
  494. size-=2;
  495. for ( i = 0 ; i+4 <= size ; i+=4)
  496. {
  497. int pt = (*(int*)&buffer[i])^(Key);
  498. Key = pt;
  499. *(int*)&test[i]=pt;
  500. }
  501. unsigned char KC=Key&0xFF;
  502. for ( ; i < size ; i++)
  503. {
  504. test[i]=buffer[i]^KC;
  505. }
  506. memcpy(buffer,test,size);
  507. free(test);
  508. }
  509. }
  510. bool EQProtocolPacket::IsProtocolPacket(const unsigned char* in_buff, uint32_t len, bool bTrimCRC) {
  511. bool ret = false;
  512. uint16_t opcode = ntohs(*(uint16_t*)in_buff);
  513. uint32_t offset = 2;
  514. switch (opcode) {
  515. case OP_SessionRequest:
  516. case OP_SessionDisconnect:
  517. case OP_KeepAlive:
  518. case OP_SessionStatResponse:
  519. case OP_Packet:
  520. case OP_Combined:
  521. case OP_Fragment:
  522. case OP_Ack:
  523. case OP_OutOfOrderAck:
  524. case OP_OutOfSession:
  525. {
  526. ret = true;
  527. break;
  528. }
  529. }
  530. return ret;
  531. }
  532. void DumpPacketHex(const EQApplicationPacket* app)
  533. {
  534. DumpPacketHex(app->pBuffer, app->size);
  535. }
  536. void DumpPacketAscii(const EQApplicationPacket* app)
  537. {
  538. DumpPacketAscii(app->pBuffer, app->size);
  539. }
  540. void DumpPacket(const EQProtocolPacket* app) {
  541. DumpPacketHex(app->pBuffer, app->size);
  542. }
  543. void DumpPacket(const EQApplicationPacket* app, bool iShowInfo) {
  544. if (iShowInfo) {
  545. cout << "Dumping Applayer: 0x" << hex << setfill('0') << setw(4) << app->GetOpcode() << dec;
  546. cout << " size:" << app->size << endl;
  547. }
  548. DumpPacketHex(app->pBuffer, app->size);
  549. // DumpPacketAscii(app->pBuffer, app->size);
  550. }
  551. void DumpPacketBin(const EQApplicationPacket* app) {
  552. DumpPacketBin(app->pBuffer, app->size);
  553. }