EQPacket.cpp 15 KB

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